| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757 |
- <?php
- namespace addons\CloudStockTwo\common\logic\deduction;
- use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
- use addons\CloudStockTwo\common\events\order\FillEvent;
- use addons\CloudStockTwo\common\logic\reward\EqualReward;
- use addons\CloudStockTwo\common\logic\reward\OverReward;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgent;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgentGoods;
- use addons\CloudStockTwo\common\models\CloudStockTwoDeductionDetail;
- use addons\CloudStockTwo\common\models\CloudStockTwoFillOrder;
- use addons\CloudStockTwo\common\models\CloudStockGoods;
- use addons\CloudStockTwo\common\models\CloudStockTwoGoods;
- use addons\CloudStockTwo\common\models\CloudStockTwoLevel;
- use addons\CloudStockTwo\common\sms\SmsConfig;
- use addons\CloudStockTwo\common\sms\TimeLimitFillMessage;
- use common\enums\AddonsEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\BcHelper;
- use common\helpers\sms\Sms;
- use common\models\goods\Goods;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use Overtrue\EasySms\EasySms;
- use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
- use yii\db\Exception;
- use addons\CloudStockTwo\common\events\order\SalesCompanyEvent;
- use addons\SalesCompany\common\models\SalesCompanyAgent;
- abstract class DeductionLogic
- {
- public $order_type;//会员订单还是补货订单
- protected $user_id; //买货会员用户id,补货用户id
- protected $goods_id; //商品id
- protected $parent_agent; //上级代理商对象 类型:StockAgent
- protected $mall_id; //商城id
- protected $not_yet_num;//剩余未扣库存数量;
- protected $stock_log_id;//库存变动记录表id
- protected $unit_goods_price; //货款单价
- protected $stock_goods;//库存商品
- protected $order;//订单模型model
- protected $changes_desc;//资金变动描述
- protected $changes_type;//资金变动类型
- protected $bonus_over_changes_type; //平级奖资金变动类型
- protected $bonus_over_changes_desc; //平级奖资金变动描述
- protected $bonus_equal_changes_type; //越级奖资金变动类型
- protected $bonus_equal_changes_desc; //越级奖资金变动描述
- protected $is_frozen = 0; //收益是否结算;
- protected $is_time_limit_fill; //是否开启限时补货
- protected $fill_hour = 0; //限时补货小时数
- protected $time_limit_fill_user_ids = []; //执行过限时补货的代理商用户id,防止重复
- protected $is_job;
- protected $user;
- protected $stock_log;
- protected $reward_list;//奖励数组
- protected $parent_agent_list;//上级代理数组
- protected $team_desc;//团队奖变动描述
- protected $diff_desc;//极差奖变动描述
- protected $wait_deduct_equal_money;
- public function __construct($user, $user_parent_agent, $stock_log, $order, $not_yet_num,$reward_list, $parent_agent_list,$wait_deduct_equal_money,$order_type,$time_limit_fill_user_ids = [], $is_job = 0)
- {
- $this->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);
- }
- }
|