user_id = $user->id; $this->parent_agent = $user_parent_agent; $this->order = $order; $this->goods_id = $order['goods_id']; $this->not_yet_num = $not_yet_num; $this->mall_id = $order['mall_id']; $this->stock_log_id = $stock_log['id']; $this->stock_log = $stock_log; $this->time_limit_fill_user_ids = $time_limit_fill_user_ids; $this->is_job = $is_job; $this->user = $user; $this->reward_list = $reward_list; $this->parent_agent_list = $parent_agent_list; $this->order_type = $order_type; $this->wait_deduct_equal_money = $wait_deduct_equal_money; //库存商品模型对象 $this->getStockGoods(); //定义资金变动类型和描述 $this->getChangesType(); //获取限时补货配置 $this->getIsTimeLimitFill(); //获取会员买货扣分润后货款商品单价 $this->getUnitGoodsPrice(); } /** * 获取商品货款单价 * @return mixed */ protected abstract function getUnitGoodsPrice(); /** * 获取库存商品 */ protected function getStockGoods() { //库存商品奖励配置 $this->stock_goods = CloudStockTwoGoods::findOne(['goods_id' => $this->goods_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]); } /** * 定义资金变动类型和描述 */ protected function getChangesType() { if ($this->order_type == 'buying') { $this->changes_type = CloudStockTwoEnum::STOCK_CHANGE_TYPE102; $this->changes_desc = "会员买货货款收益"; $this->bonus_over_changes_type = CloudStockTwoEnum::CHANGE_TYPE_INCREASE_GOODS_BUY_OVER_BONUS; $this->bonus_over_changes_desc = "会员买货越级奖发放"; $this->bonus_equal_changes_type = CloudStockTwoEnum::CHANGE_TYPE_INCREASE_GOODS_BUY_EQUAL_BONUS; $this->bonus_equal_changes_desc = "会员买货平级奖发放"; $this->team_desc = "会员买货团队奖发放"; $this->diff_desc = "会员买货极差奖发放"; } elseif ($this->order_type == 'fill') { $this->changes_type = CloudStockTwoEnum::CHANGE_TYPE_INCREASE_FILL_PAYMENT; $this->changes_desc = "下级补货货款收益"; $this->bonus_over_changes_type = CloudStockTwoEnum::CHANGE_TYPE_INCREASE_FILL_OVER_BONUS; $this->bonus_over_changes_desc = "下级补货越级奖发放"; $this->bonus_equal_changes_type = CloudStockTwoEnum::CHANGE_TYPE_INCREASE_FILL_EQUAL_BONUS; $this->bonus_equal_changes_desc = "下级补货平级奖发放"; $this->team_desc = "下级补货团队奖发放"; $this->diff_desc = "下级补货极差奖发放"; } } /** * 获取限时补货配置 * @return int|mixed */ protected function getIsTimeLimitFill() { $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic'); $this->is_time_limit_fill = isset($configs['is_time_limit_fill']) ? $configs['is_time_limit_fill'] : 0; $this->fill_hour = isset($configs['fill_hour']) ? $configs['fill_hour'] : 0; } ///平一级 public function equalOne($agent, $direct_agent, $stock_level, $over_desc, $equal_desc, $num) { switch (true) { case $agent['level'] > $direct_agent['level']: //越级奖 $this->sendOverReward($direct_agent, $stock_level, 1, $over_desc, $num); break; case $agent['level'] == $direct_agent['level']: //平级奖 $this->sendEqualReward($direct_agent, $stock_level, 1, $equal_desc, $num); break; default: } } //发放平级奖励 public function sendEqualReward($parent_user_agent, $stock_level, $equal_level, $equal_desc, $num) { //该代理商等级是否开启平级奖 if (empty($stock_level['is_equal'])) return $this->unit_goods_price; $obj = new EqualReward( $parent_user_agent, $this->stock_goods, $this->unit_goods_price, $this->order['id'], $num, $this->is_frozen, $this->bonus_equal_changes_type, $equal_desc, $this->is_job ); $obj->equal_level = $equal_level; $count_bonus_money = $obj->sendReward(); $this->unit_goods_price = 0; if ($count_bonus_money > 0) $this->unit_goods_price = BcHelper::floorFloat($count_bonus_money / $num); } //发放越级奖励 public function sendOverReward($parent_user_agent, $stock_level, $over_level, $over_desc, $num) { if (empty($stock_level['is_over'])) return $this->unit_goods_price; $obj = new OverReward( $parent_user_agent, $this->stock_goods, $this->unit_goods_price, $this->order['id'], $num, $this->is_frozen, $this->order, $over_desc, $this->is_job ); $obj->over_level = $over_level; $count_bonus_money = $obj->sendReward(); $this->unit_goods_price = 0; if ($count_bonus_money > 0) $this->unit_goods_price = BcHelper::floorFloat($count_bonus_money / $this->order['num']); } public function deduct() { //销售公司成员 $sales_company_arr = []; if(MallAddons::isInstall($this->mall_id,AddonsEnum::ADDONS_NAME_SALES_COMPANY)){ $salesCompanyAgent = SalesCompanyAgent::lists(['where' => [['mall_id' => $this->mall_id] ,['status'=>StatusEnum::ENABLED]], 'index' => 'user_id'], true); if($salesCompanyAgent){ $sales_company_arr = $salesCompanyAgent; } } //没有上级代理商,直接扣平台的; if (empty($this->parent_agent)) { $params = [ 'stock_log_id' => $this->stock_log_id, 'user_id' => 0, 'num' => $this->not_yet_num, 'not_yet_num' => 0, 'changes_type' => $this->stock_log['changes_type'], 'order_type' => $this->stock_log['order_type'], 'order_id' => $this->stock_log['order_id'], 'goods_id' => $this->goods_id, 'mall_id' => $this->mall_id, 'is_sales' => 0, ]; //会员补货 $company_user_id = 0; if(isset($sales_company_arr[$this->user_id]) && $this->order_type == 'fill'){ $params['is_sales'] = 1; $params['company_id'] = $sales_company_arr[$this->user_id]['company_id']; $company_user_id = $this->user_id; } //下单用户为最高等级,上级代理有绑定销售公司 $agent = CloudStockTwoAgent::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]); $user_level = $agent ? $agent->level : 0; $user_arr = []; if(!empty($this->parent_agent_list)){ foreach($this->parent_agent_list as $parent_agent){ if($parent_agent['level'] >= $user_level){ $user_arr[] = $parent_agent->user_id; } } } if(!empty($user_arr) && !$params['is_sales']){ foreach($user_arr as $user_id){ if(isset($sales_company_arr[$user_id])){ $params['is_sales'] = 1; $params['company_id'] = $sales_company_arr[$user_id]['company_id']; $company_user_id = $user_id; break; } } } $res = CloudStockTwoDeductionDetail::setData($params); if ($res == false) throw new \Exception(CloudStockTwoDeductionDetail::getStaticError()); $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT); if ($is_install) $this->triggerAchievement($params); if(!empty($this->parent_agent_list) && $this->user_id != $company_user_id){ foreach($this->parent_agent_list as $key => $agent){ if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::DIFF])){ $this->reward_list[$agent['user_id']][UserWalletEnum::DIFF]['num'] = $this->not_yet_num; } if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::EQUAL])){ $this->reward_list[$agent['user_id']][UserWalletEnum::EQUAL]['num'] = $this->not_yet_num; } if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::OVER])){ $this->reward_list[$agent['user_id']][UserWalletEnum::OVER]['num'] = $this->not_yet_num; } } $this->rewardTeamPrice(UserWalletEnum::DIFF); $this->rewardTeamPrice(UserWalletEnum::OVER); $this->rewardTeamPrice(UserWalletEnum::EQUAL); } if($params['is_sales']==1){ $this->salesCompany($company_user_id); } $this->not_yet_num = 0; $this->wait_deduct_equal_money = 0; return true; } $parent_agent_goods = CloudStockTwoAgentGoods::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id, 'goods_id' => $this->goods_id, 'status' => StatusEnum::ENABLED]); //代理商没有库存商品或者商品库存为0,递归找有库存的上级供应商扣减; if (empty($parent_agent_goods) || $parent_agent_goods->num == 0){ if(empty($this->is_time_limit_fill) || (!empty($this->is_time_limit_fill) && in_array($this->parent_agent->user_id, $this->time_limit_fill_user_ids))){ //当前会员无货 拿极差奖 //获取最高代理商等级,当代理商等级为最高且没有库存时,直接扣平台库存(销售公司) $level_arr = array_column($this->parent_agent_list,'level'); $max_level = max($level_arr); // $max_level = CloudStockTwoLevel::getMaxLevel($this->mall_id,$this->user_id); if(MallAddons::isInstall($this->mall_id,AddonsEnum::ADDONS_NAME_SALES_COMPANY) && $this->parent_agent->level == $max_level){ $params = [ 'stock_log_id' => $this->stock_log_id, 'user_id' => 0, 'num' => $this->not_yet_num, 'not_yet_num' => 0, 'changes_type' => $this->stock_log['changes_type'], 'order_type' => $this->stock_log['order_type'], 'order_id' => $this->stock_log['order_id'], 'goods_id' => $this->goods_id, 'mall_id' => $this->mall_id, 'is_sales' => 0, ]; $company_user_id = 0; if(isset($sales_company_arr[$this->parent_agent->user_id])){ $params['is_sales'] = 1; $params['company_id'] = $sales_company_arr[$this->parent_agent->user_id]['company_id']; $company_user_id = $this->parent_agent->user_id; } $user_arr = []; if(!empty($this->parent_agent_list)){ foreach($this->parent_agent_list as $key => $agent){ if($agent['level'] >= $this->parent_agent->level){ $user_arr[] = $agent['user_id']; } } } if(!empty($user_arr) && !$params['is_sales']){ foreach($user_arr as $user_id){ if(isset($sales_company_arr[$user_id])){ $params['is_sales'] = 1; $params['company_id'] = $sales_company_arr[$user_id]['company_id']; $company_user_id = $user_id; break; } } } $res = CloudStockTwoDeductionDetail::setData($params); if ($res == false) throw new \Exception(CloudStockTwoDeductionDetail::getStaticError()); $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT); if ($is_install) $this->triggerAchievement($params); //销售公司途径 所有代理发放极差 if(!empty($this->parent_agent_list)){ foreach($this->parent_agent_list as $key => $agent){ if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::DIFF])){ $this->reward_list[$agent['user_id']][UserWalletEnum::DIFF]['num'] = $this->not_yet_num; } if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::EQUAL])){ $this->reward_list[$agent['user_id']][UserWalletEnum::EQUAL]['num'] = $this->not_yet_num; } if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::OVER])){ $this->reward_list[$agent['user_id']][UserWalletEnum::OVER]['num'] = $this->not_yet_num; } //当前代理绑定了销售公司情况下,找到下一个极差结束 if($this->parent_agent->level == $agent['level'] && $company_user_id == $this->parent_agent->user_id){ $this->wait_deduct_equal_money = 0; break; }elseif($this->parent_agent->level < $agent['level']){ break; } } $this->rewardTeamPrice(UserWalletEnum::DIFF); $this->rewardTeamPrice(UserWalletEnum::OVER); $this->rewardTeamPrice(UserWalletEnum::EQUAL); } if($params['is_sales']==1){ $this->salesCompany($company_user_id); } $this->not_yet_num = 0; $this->wait_deduct_equal_money = 0; return true; }else{ return $this->deductOrJob(); } }else{ return $this->deductOrJob(); } } //代理商扣减库存,返回未扣库存数 $agent_goods_deduct_not_yet_num = $this->agentGoodsDeduct($parent_agent_goods, $this->stock_log_id, $this->not_yet_num); if (empty($agent_goods_deduct_not_yet_num)) { //代理商上级够库存给下级扣减 return true; } else { //代理商上级不够库存,给下级扣减 $this->not_yet_num = $agent_goods_deduct_not_yet_num; } //未扣库存数大于0,递归找有库存的上级供应商扣减; if ($this->not_yet_num > 0) return $this->deductOrJob(); return true; } protected function triggerAchievement($params){ if ($this->order_type == 'fill') { try { \Yii::warning('触发业绩'); $event = new FillEvent($this->mall_id); $order_detail_list[] = [ 'id'=>$this->order['id'], 'goods_id'=>$this->order['goods_id'], 'goods_price'=>$this->order['pay_money']/$this->order['num']*$params['num'], 'num'=>$params['num'], 'user_id'=>$this->user_id, ]; $data = [ 'id' => $this->order['id'], 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'created_at' => $this->order['created_at'], 'is_cloud' => 1, 'goods_id_arr' => [$this->goods_id], 'order_no' => $this->order['order_no'], 'order_detail_list'=>$order_detail_list ]; \Yii::$app->services->events->dispatch($event, $data, $event->listeners); // AchievementOrderLogic::addData($this->mall_id, // $this->order['order_no'], $this->user_id, $grant['id'], $this->order['created_at'], // 1, [$this->goods_id], $order_detail_list); } catch (\Exception $e) { \Yii::error('云库存补货订单业绩' . PHP_EOL . $e->getFile() . '(' . $e->getLine() . ')' . PHP_EOL . "message:" . $e->getMessage()); } } if ($this->order_type == 'buying') { //触发业绩 try { $statistical_methods = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT, 'basic.statistical_methods', true); // if(!empty($statistical_methods)){ // 触发云库存订单支付事件 $event = new FillEvent($this->mall_id); $order_detail_list[] = [ 'id'=>$this->order['id'], 'goods_id'=>$this->order['goods_id'], 'goods_price'=>$this->order['after_distribution_price'], 'num'=>$params['num'], 'user_id'=>$this->user_id, ]; $data = [ 'id' => $this->order['id'], 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'created_at' => $this->order['created_at'], 'is_cloud' => 1, 'goods_id_arr' => [$this->goods_id], 'order_no' => $this->order->order['order_no'], 'order_detail_list'=>$order_detail_list ]; \Yii::$app->services->events->dispatch($event, $data, $event->listeners); // AchievementOrderLogic::addData($this->mall_id, // $this->order->order['order_no'], $this->user_id, $grant['id'], $this->order['created_at'], // 1, [$this->goods_id], $order_detail_list); // } } catch (\Exception $e) { \Yii::error('会员前端购买订单业绩' . PHP_EOL . $e->getFile() . '(' . $e->getLine() . ')' . PHP_EOL . "message:" . $e->getMessage()); } } } /** * 代理商库存足够扣,扣减相应库存数,代理商库存不够扣,有多少扣多少,返回未扣库存数 * 扣谁的库存,谁拿货款收益,扣多少数量库存,拿多少数量货款收益; * @param $parent_agent_goods //库存商品 * @param $stock_log_id //库存变动日志id * @param $not_yet_num //剩余未扣数量 * @return false|int */ protected function agentGoodsDeduct($parent_agent_goods, $stock_log_id, $not_yet_num) { if ($parent_agent_goods->num >= $not_yet_num) { $parent_agent_goods->num -= $not_yet_num;//代理商库存足够扣,扣减扣库存数 $num = $not_yet_num; $not_yet_num = 0; } else { $parent_agent_deduction_num = $parent_agent_goods->num;//代理商库存不足够扣,有多少扣多少 $parent_agent_goods->num = 0;//代理商库存归0 $not_yet_num -= $parent_agent_deduction_num; //记录此次扣的库存数剩余数量 $num = $parent_agent_deduction_num; } $params = [ 'stock_log_id' => $stock_log_id, 'not_yet_num' => $not_yet_num, 'num' => $num, 'user_id' => $parent_agent_goods->user_id, 'changes_type' => $this->stock_log['changes_type'], 'order_id' => $this->stock_log['order_id'], 'goods_id' => $parent_agent_goods['goods_id'], 'mall_id' => $parent_agent_goods['mall_id'], 'order_type' => $this->stock_log['order_type'], ]; if (!$parent_agent_goods->save()) throw new Exception($parent_agent_goods->getErrorMessage()); $res = CloudStockTwoDeductionDetail::setData($params); if (!$res) throw new Exception(CloudStockTwoDeductionDetail::getStaticError()); $unit_goods_price = $this->unit_goods_price; //发放极差奖 if(!empty($this->parent_agent_list)){ $diff_price = 0;//未结算平级/越级奖励 foreach($this->parent_agent_list as $key => $agent){ if($agent['user_id'] == $this->parent_agent->user_id){ if($diff_price > 0){ $unit_goods_price -= $diff_price; } break; } if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::DIFF])){ $this->reward_list[$agent['user_id']][UserWalletEnum::DIFF]['num'] = $num; $unit_goods_price -= $this->reward_list[$agent['user_id']][UserWalletEnum::DIFF]['diff_price']; $diff_price = 0;//找到下一个拿极差的代理 } if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::EQUAL])){ $this->reward_list[$agent['user_id']][UserWalletEnum::EQUAL]['num'] = $num; $diff_price += $this->reward_list[$agent['user_id']][UserWalletEnum::EQUAL]['unit_goods_price']; } if(isset($this->reward_list[$agent['user_id']][UserWalletEnum::OVER])){ $this->reward_list[$agent['user_id']][UserWalletEnum::OVER]['num'] = $num; $diff_price += $this->reward_list[$agent['user_id']][UserWalletEnum::OVER]['unit_goods_price']; } } } $this->rewardTeamPrice(UserWalletEnum::DIFF); $this->rewardTeamPrice(UserWalletEnum::OVER); $this->rewardTeamPrice(UserWalletEnum::EQUAL); $payment_for_goods = $unit_goods_price * $num; //货款收益大于0,新增货款收益记录; if ($payment_for_goods > 0) { if ($this->parent_agent) { if ($this->order_type == 'buying') { $order = Order::findOne(['id'=>$this->order['order_id']]); //订单完成后才结算货款 if($order['order_status']!=OrderStatusEnum::ACCOMPLISH)$this->is_frozen = true; $source_table = OrderDetail::tableName(); $capital_type = UserWalletEnum::PAYMENT_FOR_GOODS_BUY; $source_table_id = $this->order['order_detail_id']; $desc =''; if(is_string(UserWalletEnum::getCapitalTypeMapByCommission( UserWalletEnum::PAYMENT_FOR_GOODS_BUY))){ $desc = UserWalletEnum::getCapitalTypeMapByCommission( UserWalletEnum::PAYMENT_FOR_GOODS_BUY); } } else { $source_table = CloudStockTwoFillOrder::tableName(); $source_table_id = $this->order['id']; $capital_type = UserWalletEnum::PAYMENT_FOR_GOODS_FILL; $desc =''; if(is_string(UserWalletEnum::getCapitalTypeMapByCommission( UserWalletEnum::PAYMENT_FOR_GOODS_FILL))){ $desc = UserWalletEnum::getCapitalTypeMapByCommission( UserWalletEnum::PAYMENT_FOR_GOODS_FILL); } } //货款平台管理费 $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic'); if($configs['platform_fee']){ $agent_order_price = CloudStockTwoGoods::getReplenshPrice($this->mall_id,$this->parent_agent->level,[$this->goods_id]) ?? 0; $agent_order_price = $agent_order_price * $num; $service_charge = 0; if($payment_for_goods > $agent_order_price){ $service_charge = ($payment_for_goods-$agent_order_price) * $configs['platform_fee'] / 100; $service_charge = bcmul($service_charge,1,2); $payment_for_goods = bcsub($payment_for_goods,$service_charge,2); $desc .= ',平台管理费扣除:'.$service_charge.'元'; } } $insert_wallet [] = [ 'mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id, 'is_frozen' => $this->is_frozen, 'wallet_type' => 'commission', 'money' => $payment_for_goods, 'desc' => $desc, 'source_table' => $source_table, 'source_table_id' => $source_table_id, 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'capital_type' => $capital_type, 'service_charge' => $service_charge, ]; //投递到资产队列 if ($insert_wallet) { CloudStockTwoAgent::sendUserWalletQueue($insert_wallet); } } } return $not_yet_num; } public function deductOrJob() { //当前扣库存代理商 $agent_user_id = 0; $level = 0; if (isset($this->parent_agent)) { $agent_user_id = $this->parent_agent->user_id; $level = $this->parent_agent->level; } if (!empty($this->is_time_limit_fill) && !empty($agent_user_id)) { //当前代理商加入过队列 if (in_array($agent_user_id, $this->time_limit_fill_user_ids)) { //过了限时补货,而不补货,再往上找符合条件的代理商扣库存 $parent_agent = CloudStockTwoAgent::getUserParentAgent($agent_user_id, $level); $this->parent_agent = $parent_agent; return $this->deduct(); } else { //当前代理商没有加入过队列 //记录执行限时补货的代理商用户id array_push($this->time_limit_fill_user_ids, $agent_user_id); $data = [ 'user_id' => $this->user_id, 'agent_user_id' => $agent_user_id, 'stock_log_id' => $this->stock_log_id, 'order_id' => $this->order['id'], 'not_yet_num' => $this->not_yet_num, 'order_type' => $this->order_type, 'mall_id' => $this->mall_id, 'time_limit_fill_user_ids' => $this->time_limit_fill_user_ids, 'parent_agent_list' => $this->parent_agent_list, 'reward_list' => $this->reward_list, 'wait_deduct_equal_money' => $this->wait_deduct_equal_money, ]; \Yii::error('订单id:'.$this->order['id'].'触发限时补货,时间是:'.$this->fill_hour); \Yii::$app->services->rabbitMq->delay($this->fill_hour * 3600, $data, DeductionJob::class, 'handleSync'); //补货通知 try{ $SmsConfig = new Sms($this->mall_id); $easy_sms = $SmsConfig->easySms; $goods = Goods::findOne(['id' => $this->goods_id]); $goods_name = $goods['goods_name']; mb_substr($goods_name,0,24); $message = new TimeLimitFillMessage($this->mall_id, $this->fill_hour, $goods_name, $this->not_yet_num); $user = User::findOne(['id' => $agent_user_id]); // $easy_sms->send($user['mobile'], $message); try{ $easy_sms->send($user['mobile'], $message); }catch (NoGatewayAvailableException $e){ $res = $e->getExceptions(); if(!empty($res)){ foreach ($res as $exception){ if(is_string($exception->getMessage())){ \Yii::error('云库存补货通知短信发送失败:' .$exception->getMessage()); } } } } }catch (\Exception $e){ \Yii::error('云库存补货通知短信发送失败:' .$e->getMessage()); } return true; } } else { //没有开启限时补货或者下单人的上级代理商不存在,递归往上找,直到找平台扣库存 $this->parent_agent = CloudStockTwoAgent::getUserParentAgent($agent_user_id, $level); return $this->deduct(); } } /** * 团队奖 */ public function rewardTeamPrice($capital_type) { if(empty($this->reward_list)){ return true; } $insert_wallet = []; $is_frozen = false; if ($this->order_type == 'buying') { $source_table = OrderDetail::tableName(); $source_table_id = $this->order['order_detail_id']; $order = Order::findOne(['id'=>$this->order['order_id']]); //订单完成后才结算货款 if($order['order_status']!=OrderStatusEnum::ACCOMPLISH)$is_frozen = true; } else { $source_table = CloudStockTwoFillOrder::tableName(); $source_table_id = $this->order['id']; } $desc = ''; if($capital_type == UserWalletEnum::TEAM){ $desc = $this->team_desc; }elseif($capital_type == UserWalletEnum::EQUAL){ $desc = $this->bonus_equal_changes_desc; }elseif($capital_type == UserWalletEnum::OVER){ $desc = $this->bonus_over_changes_desc; }elseif($capital_type == UserWalletEnum::DIFF){ $desc = $this->diff_desc; } $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic'); foreach($this->parent_agent_list as $agent){ if(isset($this->reward_list[$agent['user_id']][$capital_type]) && $this->reward_list[$agent['user_id']][$capital_type]['num']){ $commission_num = $this->reward_list[$agent['user_id']][$capital_type]['unit_goods_price'] * $this->reward_list[$agent['user_id']][$capital_type]['num']; $service_charge = 0; if($configs['platform_fee']){ $service_charge = $commission_num * $configs['platform_fee'] / 100; $service_charge = bcmul($service_charge,1,2); $commission_num = bcsub($commission_num,$service_charge,2); if(empty($commission_num)) return $insert_wallet; } $insert_wallet[] = [ 'mall_id' => $this->mall_id, 'user_id' => $agent['user_id'], 'is_frozen' => $is_frozen, 'wallet_type' => 'commission', 'money' => $commission_num, 'desc' => $desc.',平台管理费扣除:'.$service_charge.'元', 'source_table' => $source_table, 'source_table_id' => $source_table_id, 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'capital_type' => $capital_type, 'service_charge' => $service_charge, ]; $this->reward_list[$agent['user_id']][$capital_type]['num'] = 0; } } //投递到资产队列 if ($insert_wallet) { CloudStockTwoAgent::sendUserWalletQueue($insert_wallet); } } /** * 销售公司 */ public function salesCompany($user_id=0){ if($this->parent_agent){ // $user_id = $this->parent_agent->user_id; $order_price = CloudStockTwoGoods::getReplenshPrice($this->mall_id,$this->parent_agent->level,[$this->goods_id]) ?? 0; }else{ // $user_id = $this->user_id; $order_price = $this->unit_goods_price; } $agent_order_price = CloudStockTwoGoods::getReplenshPrice($this->mall_id,0,[$this->goods_id]) ?? 0; $sales_price = bcsub($order_price,$agent_order_price,2); //销售公司承担未结算平级/越级奖 if($this->wait_deduct_equal_money){ $sales_price = bcsub($sales_price,$this->wait_deduct_equal_money,2); } //销售公司拿货价 = 代理拿货价 if(!$sales_price || $sales_price <= 0){ return true; } $is_frozen = 0; if ($this->order_type == 'buying') { $source_table = OrderDetail::tableName(); $source_table_id = $this->order['order_detail_id']; $desc ='会员买货货款,旗下成员(ID:'.$user_id.')收益后结算奖励'; $order = Order::findOne(['id'=>$this->order['order_id']]); //订单完成后才结算货款 if($order['order_status']!=OrderStatusEnum::ACCOMPLISH)$is_frozen = 1; } else { $source_table = CloudStockTwoFillOrder::tableName(); $source_table_id = $this->order['id']; $desc ='补货货款,旗下成员(ID:'.$user_id.')收益后结算奖励'; } \Yii::error('销售公司触发,order_id:'.$this->stock_log['order_id'].',user_id:'.$user_id); $event = new SalesCompanyEvent($this->mall_id); $data = [ 'source_table' => $source_table, 'source_table_id' => $source_table_id, 'num' => $this->not_yet_num, 'user_id' => $user_id, 'price' => $sales_price, 'desc' => $desc, 'is_frozen' => $is_frozen, ]; \Yii::$app->services->events->dispatch($event, $data, $event->listeners); } }