db->beginTransaction(); try { //库存变化记录 $params = [ 'mall_id' => $mall_id, 'goods_id' => $goods_id, 'user_id' => 0, 'changes_dir' => CloudStockEnum::STOCK_CHANGE_SUB, 'order_id' => $order_id, 'num' => $num, 'order_type' => CloudStockEnum::STOCK_ORDER_TYPE_REPLENISH, 'changes_type' => CloudStockEnum::STOCK_CHANGE_TYPE202, ]; $stock_log = CloudStockGoodsStockLog::setData($mall_id,$params); if (!$stock_log) throw new \Exception(CloudStockGoodsStockLog::getStaticError()); $agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]); $user = User::findOne(['id' => $user_id]); if (empty($user) || empty($agent)) throw new \Exception('代理商不存在'); $user_parent_agent = CloudStockAgent::getUserParentAgent($user_id, $agent['level']); $changes_type = $this->changes_type; if(strpos($fill_order['order_no'],'CSFL')!==false){ $changes_type = CloudStockEnum::STOCK_CHANGE_TYPE105; } //生成代理商商品库存 $params = [ 'mall_id' => $mall_id, 'user_id' => $user_id, 'goods_id' => $goods_id, 'num' => $num, 'order_id' => $fill_order['id'], 'changes_type' => $changes_type, 'changes_dir' => CloudStockEnum::STOCK_CHANGE_ADD, 'order_type' => CloudStockEnum::STOCK_ORDER_TYPE_REPLENISH, ]; $increase_detail = null; $res = CloudStockAgentGoods::addData($params, $increase_detail); if (!$res) throw new \Exception(CloudStockAgentGoods::getStaticError()); CloudStockAgent::fixStock($mall_id,$user_id); //库存递归扣减逻辑 $obj = new FillDeductionLogic($user, $user_parent_agent, $stock_log, $fill_order, $fill_order['num']); $obj->order_type = 'fill'; $obj->order_no = $fill_order['order_no']; $obj->user_agent_level = $agent['level']; if (!$obj->deduct()) throw new \Exception('库存递归扣减异常'); !empty($obj->fill_insert_wallet) && ($this->fill_insert_wallet = $obj->fill_insert_wallet); //同步分销奖励 self::distributionReward($user_id, $mall_id, $fill_order); $t->commit(); if (LockHelper::lock($lock_key, $identification, 5, 10)){ (new CloudStockPushDelayLog())->updateDelayLog($mall_id,$goods_id,$user_id,$num); } //触发库存变动事件 if (isset($increase_detail)) { $event = new CloudStockChangeSyncEvent($increase_detail['mall_id']); $event_data = [ 'id' => $increase_detail->id, 'type' => CloudStockEnum::STOCK_CHANGE_ADD, 'mall_id' => $increase_detail['mall_id'], ]; \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners); } if (!empty($obj->event_arr)) { foreach ($obj->event_arr as $event_data) { $event = new CloudStockChangeSyncEvent($event_data['mall_id']); \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners); } } return true; } catch (\Exception $e) { LockHelper::uLock($lock_key, $identification); $t->rollBack(); $exception = FormatHelper::exception($e, $e->getMessage()); echo $exception; \Yii::error($exception); return false; } } /** * @desc:同步分销奖励 * @Author: hua * @Date: 2022/3/3 * @Time: 12:03 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $user_id * @param $mall_id * @param $fill_order * @return bool|void */ public static function distributionReward($user_id, $mall_id, $fill_order) { $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic'); if (empty($configs['is_sync_distribution_level'])) return true; if (MallAddons::isInstall($mall_id, DistributionEnum::ADDONS_NAME) == 0) return true; $setting = \Yii::$app->services->setting->addons->get($mall_id, DistributionEnum::ADDONS_NAME, 'basic'); if (empty($setting['level'])) return; $distribution_list = Distribution::getDistributionList($setting['is_self_buy'], $mall_id, $user_id); $goods = Goods::findOne(['id' => $fill_order['goods_id']]); $order_detail = [ 'id' => $fill_order['id'], 'goods_id' => $fill_order['goods_id'], 'order_id' => $fill_order['id'], 'order_no' => $fill_order['order_no'], 'goods_name' => $goods['goods_name']??"", 'num' => $fill_order['num'], 'goods_price' =>$fill_order['pay_money'], 'original_goods_price' => bcmul($goods['price'],$fill_order['num'],2), 'cost_price' => $goods['cost_price'] * $fill_order['num'], ]; $setting['compute_type'] = ComputingEnum::ORDER_PAY; $agent_reward_obj = new DistributionReward($mall_id, $user_id, $setting, $distribution_list, $order_detail); $agent_reward_obj->extension = ['source_table' => 'cloud_stock_agent_order', 'source_table_id' =>$fill_order['id'], 'from_type' =>'Distribution']; $agent_reward_obj->addFrozenCommission(); } //业绩奖励 public function performanceReward(){} }