| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343 |
- <?php
- namespace addons\CloudStock\common\logic\deduction;
- use addons\CloudStock\common\enums\CloudStockEnum;
- use addons\CloudStock\common\events\order\FillEvent;
- use addons\CloudStock\common\events\order\FillEventB;
- use addons\CloudStock\common\logic\addons\Boss\RewardLogLogic;
- use addons\CloudStock\common\logic\reward\EqualReward;
- use addons\CloudStock\common\logic\reward\OverReward;
- use addons\CloudStock\common\logic\reward\PoolReward;
- use addons\CloudStock\common\models\CloudStockAgent;
- use addons\CloudStock\common\models\CloudStockAgentGoods;
- use addons\CloudStock\common\models\CloudStockDeductionDetail;
- use addons\CloudStock\common\models\CloudStockFillOrder;
- use addons\CloudStock\common\models\CloudStockGoods;
- use addons\CloudStock\common\models\CloudStockPriceDifferenceGiftPackage;
- use addons\CloudStock\common\models\CloudStockPriceDifferenceOrder;
- use addons\CloudStock\common\models\CloudStockPriceDifferenceOrderDetail;
- use addons\CloudStock\common\models\CloudStockPushDelayLog;
- use addons\CloudStock\common\services\CloudStockPriceDifferenceGiftPackageOrderService;
- use addons\CloudStock\common\services\CloudStockPriceDifferenceUserTeamStatisticsService;
- use addons\CloudStock\common\services\EqualAwardService;
- use addons\CloudStock\common\services\LossAmountService;
- use addons\CloudStock\common\services\PaymentLogService;
- use addons\CloudStock\common\services\PriceDifferenceProfitService;
- use addons\CloudStock\common\sms\SmsConfig;
- use addons\CloudStock\common\sms\TimeLimitFillMessage;
- use addons\CloudStock\common\events\order\CloudStockChangeEvent;
- use addons\CloudStock\common\models\CloudStockLevel;
- use addons\CloudStock\common\events\order\FillDeductByPlatformEvent;
- use common\enums\AddonsEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\RabbitMqEnum;
- 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 common\models\user\UserPartitionRelationship;
- use Overtrue\EasySms\EasySms;
- use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
- use services\common\UserWalletService;
- use yii\db\Exception;
- 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;
- public $user_agent_level = null;
- public $fill_insert_wallet = [];
- public $payment_insert_wallet = [];//云仓货款
- public $total_price_difference = 0;
- public $order_no = '';
- public $store_id = 0;
- public $event_arr = [];
- public $recommend_reward_arr = [];
- public $not_get_price_difference = 0;
- public $not_distribute_payment_for_goods = 0;
- public $configs;
- public function __construct($user, $user_parent_agent, $stock_log, $order, $not_yet_num, $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->getStockGoods();
- //定义资金变动类型和描述
- $this->getChangesType();
- //获取限时补货配置
- $this->getIsTimeLimitFill();
- //获取会员买货扣分润后货款商品单价
- $this->getUnitGoodsPrice();
- }
- /**
- * 获取商品货款单价
- * @return mixed
- */
- protected abstract function getUnitGoodsPrice();
- /**
- * 获取库存商品
- */
- protected function getStockGoods()
- {
- //库存商品奖励配置
- $this->stock_goods = CloudStockGoods::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 = CloudStockEnum::STOCK_CHANGE_TYPE102;
- $this->changes_desc = "会员买货货款收益";
- $this->bonus_over_changes_type = CloudStockEnum::CHANGE_TYPE_INCREASE_GOODS_BUY_OVER_BONUS;
- $this->bonus_over_changes_desc = "会员买货越级奖发放";
- $this->bonus_equal_changes_type = CloudStockEnum::CHANGE_TYPE_INCREASE_GOODS_BUY_EQUAL_BONUS;
- $this->bonus_equal_changes_desc = "会员买货平级奖发放";
- } elseif ($this->order_type == 'fill') {
- $this->changes_type = CloudStockEnum::CHANGE_TYPE_INCREASE_FILL_PAYMENT;
- $this->changes_desc = "下级补货货款收益";
- $this->bonus_over_changes_type = CloudStockEnum::CHANGE_TYPE_INCREASE_FILL_OVER_BONUS;
- $this->bonus_over_changes_desc = "下级补货越级奖发放";
- $this->bonus_equal_changes_type = CloudStockEnum::CHANGE_TYPE_INCREASE_FILL_EQUAL_BONUS;
- $this->bonus_equal_changes_desc = "下级补货平级奖发放";
- }
- }
- /**
- * 获取限时补货配置
- * @return int|mixed
- */
- protected function getIsTimeLimitFill()
- {
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, '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, $setting = [])
- {
- switch (true) {
- case $agent['level'] > $direct_agent['level']:
- //越级奖
- $this->sendOverReward($direct_agent, $stock_level, 1, $over_desc, $num, !empty($setting['is_by_buy_agent_level']), $agent['level']);
- 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;
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- $is_frozen = true;
- if (empty($setting['settlement_cycle_of_equal_prize'])) {
- $is_frozen = false;//平级奖结算周期:立即结算
- }
- $obj = new EqualReward(
- $parent_user_agent,
- $this->stock_goods,
- $this->unit_goods_price,
- $this->order['id'],
- $num,
- $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, $is_by_buy_agent_level = false, $buy_level = 0)
- {
- 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;
- $obj->buy_level = $buy_level;
- $obj->is_by_buy_agent_level = $is_by_buy_agent_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']);
- }
- /**
- * 价差礼包推荐奖励
- * @return true|void
- */
- protected function computeRecommendReward()
- {
- $obj = new CloudStockPriceDifferenceGiftPackageOrderService(['mall_id' => $this->order['mall_id'], 'user_id' => $this->order['user_id']]);
- $order = CloudStockPriceDifferenceOrder::getOne([['id' => $this->order['price_difference_order_id']]], true, ['detail']);
- if (empty($order)) {
- return true;
- }
- $package = CloudStockPriceDifferenceGiftPackage::find()->where(['id' => $order['price_difference_gift_package_id']])->one();
- $order_detail_arr = array_column($order['detail'], null, 'id');
- $data = [
- 'user_id' => $this->order['user_id'],
- 'mall_id' => $this->order['mall_id'],
- 'id' => $this->order['id'],
- 'order_no' => $this->order['order_no'],
- 'pay_money' => $order_detail_arr[$this->order['price_difference_order_detail_id']]['goods_price'] ?? 0,
- ];
- $this->recommend_reward_arr = $obj->recommendReward($data, $package);
- $this->not_get_price_difference = $package['not_get_price_difference'] ?? 0;
- $this->not_distribute_payment_for_goods = $package['not_distribute_payment_for_goods'] ?? 0;
- }
- protected function sendRecommendReward($recommend_reward_arr)
- {
- if (!empty($recommend_reward_arr)) {
- if (!empty($this->order['price_difference_order_detail_id'])) {
- $price_difference_order_detail = CloudStockPriceDifferenceOrderDetail::findOne(['id' => $this->order['price_difference_order_detail_id']]);
- }
- if ($this->not_distribute_payment_for_goods) {
- $res = UserWalletService::batchHandleByQueue($recommend_reward_arr);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- } else {
- if (empty($price_difference_order_detail['is_send_recommend_reward'])) {
- $res = UserWalletService::batchHandleByQueue($recommend_reward_arr);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- CloudStockPriceDifferenceOrderDetail::updateAll(['is_send_recommend_reward' => 1], ['id' => $this->order['price_difference_order_detail_id']]);
- }
- }
- // if (isset($price_difference_order_detail)) {
- // }
- }
- }
- public function deduct()
- {
- //没有上级代理商,直接扣平台的;
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- $this->configs = $configs;
- if ($this->order_type == 'fill' && !empty($this->order['price_difference_order_id']) && !empty($configs['fill_price_difference'])) {
- $this->computeRecommendReward();
- }
- if (isset($this->order['order_no']) && strpos($this->order['order_no'], 'CSFL') !== false) {
- $this->stock_log['changes_type'] = CloudStockEnum::STOCK_CHANGE_TYPE105;
- }
- if (empty($this->parent_agent)) {
- $nums = $this->not_yet_num;
- $params = [
- 'stock_log_id' => $this->stock_log_id,
- 'user_id' => 0,
- 'num' => $this->not_yet_num,
- 'not_yet_num' => 0,
- 'order_type' => $this->order_type == 'fill' ? CloudStockEnum::STOCK_ORDER_TYPE_REPLENISH : CloudStockEnum::STOCK_ORDER_TYPE_USER,
- 'changes_type' => $this->stock_log['changes_type'],
- 'order_id' => $this->stock_log['order_id'],
- 'goods_id' => $this->goods_id,
- 'mall_id' => $this->mall_id,
- ];
- $res = CloudStockDeductionDetail::setData($params);
- $this->not_yet_num = 0;
- if ($res == false) throw new \Exception(CloudStockDeductionDetail::getStaticError());
- $this->sendRecommendReward($this->recommend_reward_arr);
- //触发库存变动事件
- $event = new CloudStockChangeEvent($this->mall_id);
- $event_data = [
- 'id' => $res->id,
- 'type' => CloudStockEnum::STOCK_CHANGE_SUB,
- 'mall_id' => $this->mall_id,
- ];
- \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners);
- $this->event_arr[] = [
- 'id' => $res->id,
- 'type' => CloudStockEnum::STOCK_CHANGE_SUB,
- 'mall_id' => $this->mall_id,
- ];
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT);
- $is_install_b = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT_B);
- if ($this->order_type == 'fill') {
- if ($is_install) $this->triggerAchievement($params);
- if ($is_install_b) $this->triggerAchievementB($params);
- $this->triggerStatistics($params);
- $fill_unit_price = bcdiv($this->order['pay_money'], $this->order['num'], 2);
- var_dump('投入分红池,进货价:' . $fill_unit_price);
- PoolReward::inPool($this->mall_id, $this->goods_id, $nums, $fill_unit_price);
- $order_type = 2;
- } else {
- $orderDetail = OrderDetail::findOne($this->order['order_detail_id']);
- $fill_unit_price = bcdiv($orderDetail['goods_price'], $orderDetail['num'], 2);
- $order_type = 1;
- if ($is_install) {
- $statisticalMethods = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT, 'basic.statistical_methods', true);
- if ($statisticalMethods) { // 业绩奖励,业绩统计方式 = 1(支付完成),当订单支付后,直接触发
- $this->triggerAchievement($params);
- } else { // 业绩奖励,业绩统计方式 = 0 (订单完成),但订单完成后,需要判断订单是不是真的完成,才去触发
- if ($orderDetail['order_status'] == OrderStatusEnum::ACCOMPLISH) {
- $this->triggerAchievement($params);
- }
- }
- }
- var_dump($is_install_b . '========');
- if ($is_install_b) {
- $statisticalMethods = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT_B, 'basic.statistical_methods', true);
- if ($statisticalMethods) { // 业绩奖励,业绩统计方式 = 1(支付完成),当订单支付后,直接触发
- $this->triggerAchievementB($params);
- } else { // 业绩奖励,业绩统计方式 = 0 (订单完成),但订单完成后,需要判断订单是不是真的完成,才去触发
- if ($orderDetail['order_status'] == OrderStatusEnum::ACCOMPLISH) {
- $this->triggerAchievementB($params);
- }
- }
- }
- }
- $fillDeductByPlatformEvent = new FillDeductByPlatformEvent($this->mall_id);
- $fillDeductByPlatformData = [
- 'order_id' => $this->order['id'],
- 'changes_type' => $this->stock_log['changes_type'],
- 'user_id' => $this->user_id,
- 'mall_id' => $this->mall_id,
- 'goods_id' => $this->goods_id,
- 'num' => $nums,
- 'fill_unit_price' => $fill_unit_price,
- 'order_type' => $order_type,
- ];
- \Yii::$app->services->events->dispatch($fillDeductByPlatformEvent, $fillDeductByPlatformData, $fillDeductByPlatformEvent->listeners);
- if (!empty($configs['is_sync_boss_bonus']) && $this->order_type == 'fill') {
- //云库存增加股东分红(云库存补货订单,扣平台库存)
- $post_data = [
- 'id' => $this->order['id'],
- 'mall_id' => $this->order['mall_id'],
- 'user_id' => $this->order['user_id'],
- 'order_no' => $this->order_no,
- 'goods_id' => $this->order['goods_id'],
- 'pay_money' => bcmul($this->unit_goods_price, $nums, 2),
- 'num' => $nums,
- ];
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $post_data, RewardLogLogic::class, 'handleSync');
- }
- return true;
- }
- $parent_agent_goods = CloudStockAgentGoods::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id, 'goods_id' => $this->goods_id, 'status' => StatusEnum::ENABLED]);
- //如果为买货订单,开启是否跳过等级不扣库存且上级库存不为空时
- if ($this->order_type == 'buying' && $configs['is_jump_level'] == 1 && !empty($parent_agent_goods)) {
- if (!empty($configs['jump_level'])) {
- $configs['jump_level'] = json_decode($configs['jump_level'], true);
- //判断用户是否在设置的等级内,是则上级代理库存赋值为空
- if (in_array($this->parent_agent->level, $configs['jump_level'])) {
- // $parent_agent_goods = [];
- $agent_user_id = $this->parent_agent->user_id;
- $level = $this->parent_agent->level;
- //没有开启限时补货或者下单人的上级代理商不存在,递归往上找,直到找平台扣库存
- $type = 2;
- if ($this->order_type == 'buying') {
- $type = 3;
- }
- (new LossAmountService(['mall_id' => $this->mall_id]))->compute($type, $this->not_yet_num, $this->order, $this->parent_agent);//补货、会员买货触发流失金额
- $parent_agent = CloudStockAgent::getUserParentAgent($agent_user_id, $level);
- $this->parent_agent = $parent_agent;
- return $this->deduct();
- }
- }
- }
- //价差礼包-产生的补货单、只能扣一个上级的库存
- if ($this->order_type == 'fill' && !empty($this->order['price_difference_order_id'])) {
- if (!empty($parent_agent_goods) && $parent_agent_goods['num'] < $this->order['num']) {
- if ($this->order_type == 'fill' && !empty($configs['fill_price_difference'])) {
- $this->sendPriceDifference($configs);
- return true;
- }
- if ($this->order_type == 'buying' && !empty($configs['buy_price_difference'])) {
- $this->sendPriceDifference($configs);
- return true;
- }
- return $this->deductOrJob();
- }
- }
- //代理商没有库存商品或者商品库存为0,递归找有库存的上级供应商扣减;
- if (empty($parent_agent_goods) || $parent_agent_goods->num == 0) {
- if ($this->order_type == 'fill' && !empty($configs['fill_price_difference'])) {
- $this->sendPriceDifference($configs);
- return true;
- }
- if ($this->order_type == 'buying' && !empty($configs['buy_price_difference'])) {
- $this->sendPriceDifference($configs);
- return true;
- }
- return $this->deductOrJob();
- } //代理商扣减库存,返回未扣库存数
- $agent_goods_deduct_not_yet_num = $this->agentGoodsDeduct($parent_agent_goods, $this->stock_log_id, $this->not_yet_num);
- CloudStockAgent::fixStock($this->mall_id, $this->parent_agent['user_id']);
- if (empty($agent_goods_deduct_not_yet_num)) {
- //代理商上级够库存给下级扣减
- return true;
- } else {
- //代理商上级不够库存,给下级扣减
- $this->not_yet_num = $agent_goods_deduct_not_yet_num;
- if ($this->order_type == 'fill' && !empty($configs['fill_price_difference'])) {
- $this->sendPriceDifference($configs);
- return true;
- }
- if ($this->order_type == 'buying' && !empty($configs['buy_price_difference'])) {
- $this->sendPriceDifference($configs);
- return true;
- }
- }
- //未扣库存数大于0,递归找有库存的上级供应商扣减;
- if ($this->not_yet_num > 0) return $this->deductOrJob();
- return true;
- }
- protected function sendPriceDifference($configs, $diff_price = 0, $is_first = true, $price_difference = 0)
- {
- $price_difference_type = !empty($configs['price_difference_type']) ? $configs['price_difference_type'] : 0;
- var_dump("price_difference::{$price_difference}");
- $parent_agent_goods = CloudStockAgentGoods::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id, 'goods_id' => $this->goods_id, 'status' => StatusEnum::ENABLED]);
- if ($this->order_type == 'buying' && $configs['is_jump_level'] == 1 && !empty($parent_agent_goods)) {
- if (!empty($configs['jump_level'])) {
- if (!is_array($configs['jump_level'])) $configs['jump_level'] = json_decode($configs['jump_level'], true);
- //判断用户是否在设置的等级内,是则上级代理库存赋值为空
- if (in_array($this->parent_agent->level, $configs['jump_level'])) {
- $parent_agent_goods = [];
- }
- }
- }
- if ($price_difference_type == 2 && !empty($parent_agent_goods['num'])) {
- //需要校验,每个拿差价的库存
- $agent_goods_deduct_not_yet_num = $this->agentGoodsDeduct($parent_agent_goods, $this->stock_log_id, $this->not_yet_num, $price_difference, $is_first);
- if (empty($agent_goods_deduct_not_yet_num)) {
- //代理商上级够库存给下级扣减
- return true;
- }
- $this->not_yet_num = $agent_goods_deduct_not_yet_num;
- }
- $cloud_stock_goods = CloudStockGoods::findOne(['mall_id' => $this->mall_id, 'goods_id' => $this->goods_id, 'status' => StatusEnum::ENABLED]);
- if (empty($cloud_stock_goods)) return false;
- $agent_price_arr = json_decode($cloud_stock_goods['agent_price'], true);
- $price_arr = array_column($agent_price_arr, 'price', 'level');
- $parent_agent_level = $this->parent_agent->level;
- if (!isset($this->user_agent_level)) return false;
- $insert_wallet = [];
- if (empty($this->user_agent_level) || $is_first) {
- $user_agent_price = $this->unit_goods_price;
- } else {
- $user_agent_price = $price_arr[$this->user_agent_level] ?? 0;
- }
- // TODO: 云库存货款延时结算
- $commission_is_frozen = false;
- if (isset($configs['compute_delay_day']) && !empty($configs['compute_delay_day'])) {
- $commission_is_frozen = true;
- }
- if ($this->order_type == 'buying') {
- $source_table = OrderDetail::tableName();
- $source_table_id = $this->order['order_detail_id'];
- $desc = '会员买货时,无货拿进货差价';
- $capital_type = UserWalletEnum::CLOUD_STOCK_BUY_PRICE_DIFFERENCE;
- $is_frozen = true;
- // 判断结算方式,兼容无结算方式设置条件的客户,默认为"支付完成后"结算
- if (!$commission_is_frozen && (!isset($configs['compute_type']) || empty($configs['compute_type']))) {
- $commission_is_frozen = true;
- }
- } else {
- $source_table = CloudStockFillOrder::tableName();
- $source_table_id = $this->order['id'];
- $desc = '补货时,无货拿补货差价';
- $capital_type = UserWalletEnum::CLOUD_STOCK_FILL_PRICE_DIFFERENCE;
- $is_frozen = false;
- }
- $parent_agent_price = $price_arr[$parent_agent_level] ?? 0;
- $price_difference = $user_agent_price - $parent_agent_price;
- if ($price_difference <= 0) return false;
- $price_difference = $price_difference - $diff_price;
- if ($price_difference > 0) {
- $this->addPriceDifference($commission_is_frozen, $price_difference, $desc, $source_table, $source_table_id, $capital_type, $this->not_yet_num, $insert_wallet);
- }
- //差价平级奖
- $diff_price = 0;
- $stock_level = CloudStockLevel::findOne(['mall_id' => $this->mall_id, 'level' => $this->parent_agent->level, 'status' => StatusEnum::ENABLED]);
- if (!empty($configs['is_difference_equal']) && !empty($stock_level['is_equal'])) {
- $user = User::findOne($this->parent_agent->user_id);
- if (!empty($user['parent_id'])) {
- //紧凑平级奖
- $equal_level_list = $this->stock_goods['equal_level_list'] ? json_decode($this->stock_goods['equal_level_list'], true) : [];
- if (!empty($configs['is_find_equal'])) {
- $this->compactLevelingAward($this->parent_agent->user_id, $equal_level_list, $is_frozen, $desc, $source_table, $source_table_id, $diff_price, $insert_wallet);
- } else {
- $this->noCompactLevelingAward($user['parent_id'], $equal_level_list, $is_frozen, $desc, $source_table, $source_table_id, $diff_price, $insert_wallet);
- }
- }
- }
- if (!empty($insert_wallet)) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (!$res) return false;
- (new EqualAwardService(['mall_id' => $this->mall_id]))->setStatistics($insert_wallet, UserWalletEnum::EQUAL);
- }
- //所有代理拿差价
- if (!empty($price_difference_type)) {
- $agent_user_id = $this->parent_agent->user_id;
- $level = $this->parent_agent->level;
- $parent_agent = CloudStockAgent::getUserParentAgent($agent_user_id, $level);
- if ($parent_agent) {
- $this->user_agent_level = $this->parent_agent->level;
- $this->parent_agent = $parent_agent;
- $this->total_price_difference += $price_difference;
- $this->sendPriceDifference($configs, $diff_price, false, $this->total_price_difference);
- } else {
- $nums = $this->not_yet_num;
- $params = [
- 'stock_log_id' => $this->stock_log_id,
- 'user_id' => 0,
- 'order_type' => $this->order_type == 'fill' ? CloudStockEnum::STOCK_ORDER_TYPE_REPLENISH : CloudStockEnum::STOCK_ORDER_TYPE_USER,
- 'num' => $this->not_yet_num,
- 'not_yet_num' => 0,
- 'changes_type' => $this->stock_log['changes_type'],
- 'order_id' => $this->stock_log['order_id'],
- 'goods_id' => $this->goods_id,
- 'mall_id' => $this->mall_id,
- ];
- $res = CloudStockDeductionDetail::setData($params);
- $this->not_yet_num = 0;
- if ($res == false) throw new \Exception(CloudStockDeductionDetail::getStaticError());
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT);
- $is_install_b = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT_B);
- $params['deduction_detail_id'] = $res['id'];
- if ($is_install) $this->triggerAchievement($params);
- if ($is_install_b) $this->triggerAchievementB($params);
- if ($this->order_type == 'fill') {
- $fill_unit_price = bcdiv($this->order['pay_money'], $this->order['num'], 2);
- var_dump('投入分红池,进货价:' . $fill_unit_price);
- PoolReward::inPool($this->mall_id, $this->goods_id, $nums, $fill_unit_price);
- }
- //开启价差礼包-推荐奖励截止至货款获得者按钮
- if ($this->not_distribute_payment_for_goods) {
- foreach ($this->recommend_reward_arr as &$item) {
- $total_num = $this->order['num'];
- //重新计算佣金,当前扣除上级库存件数*每件可获取佣金
- $item['money'] = bcmul($item['money'] / $total_num, $params['num'], 2);
- $desc_array = explode(',', $item['desc']);
- $item['desc'] = $desc_array[0].','.$desc_array[1].','.$desc_array[2].',金额:'.$item['money'];
- }
- }
- $this->sendRecommendReward($this->recommend_reward_arr);
- return true;
- }
- }
- return true;
- }
- //紧凑平级奖
- public function compactLevelingAward($user_id, $equal_level_list, $is_frozen, $desc, $source_table, $source_table_id, &$diff_price, &$insert_wallet)
- {
- $agent = CloudStockAgent::findOne(['user_id' => $user_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
- $parent_id_arr = array_reverse($parent_id_arr);
- $is_find_equal = false;
- $diff_price = 0;
- foreach ($parent_id_arr as $pid) {
- $parent_agent = CloudStockAgent::findOne(['user_id' => $pid, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- //找平级
- if (!empty($parent_agent) && empty($parent_agent['is_equal_award_restriction'])) {
- if ($parent_agent['level'] == $agent['level']) {
- if (!$is_find_equal) {
- $direct_agent_price = 0;
- foreach ($equal_level_list as $value) {
- if ($value['level'] == $parent_agent['level'] && $value['config_key'] == CloudStockEnum::CONFIG_KEY_EQUAL_LEVEL_ONE) {
- $direct_agent_price = !empty($value['config_value']) ? $value['config_value'] : 0;
- break;
- }
- }
- if (!empty($direct_agent_price) && $direct_agent_price > 0) {
- if (!empty($this->configs['difference_equal_type'])) {
- $diff_price += $direct_agent_price;
- }
- //发放平一级奖励
- $insert_wallet[] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $parent_agent['user_id'],
- 'is_frozen' => $is_frozen,
- 'wallet_type' => 'commission',
- 'money' => $direct_agent_price * $this->not_yet_num,
- 'desc' => $desc . '平一级奖励',
- 'source_table' => $source_table,
- 'source_table_id' => $source_table_id,
- 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
- 'capital_type' => UserWalletEnum::CLOUD_STOCK_PRICE_DIFFERENCE_EQUAL,
- 'order_no' => $this->order_no,
- ];
- }
- $is_find_equal = true;
- } else {
- foreach ($equal_level_list as $value) {
- if ($value['level'] == $parent_agent['level'] && $value['config_key'] == CloudStockEnum::CONFIG_KEY_EQUAL_LEVEL_TWO) {
- $second_parent_agent_price = !empty($value['config_value']) ? $value['config_value'] : 0;
- break;
- }
- }
- if (!empty($second_parent_agent_price) && $second_parent_agent_price > 0) {
- if (!empty($this->configs['difference_equal_type'])) {
- $diff_price += $second_parent_agent_price;
- }
- $insert_wallet[] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $parent_agent['user_id'],
- 'is_frozen' => $is_frozen,
- 'wallet_type' => 'commission',
- 'money' => $second_parent_agent_price * $this->not_yet_num,
- 'desc' => $desc . '平二级奖励',
- 'source_table' => $source_table,
- 'source_table_id' => $source_table_id,
- 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
- 'capital_type' => UserWalletEnum::CLOUD_STOCK_PRICE_DIFFERENCE_EQUAL,
- 'order_no' => $this->order_no,
- ];
- }
- break;
- }
- }
- }
- }
- }
- public function noCompactLevelingAward($parent_id, $equal_level_list, $is_frozen, $desc, $source_table, $source_table_id, &$diff_price, &$insert_wallet)
- {
- $direct_agent = CloudStockAgent::findOne(['user_id' => $parent_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- $direct_agent_price = 0;
- //获取平一级代理商数据
- if ($direct_agent && $direct_agent['level'] == $this->parent_agent->level && !empty($equal_level_list) && empty($direct_agent['is_equal_award_restriction'])) {
- $config_key = CloudStockEnum::CONFIG_KEY_EQUAL_LEVEL_ONE;
- foreach ($equal_level_list as $value) {
- if ($value['level'] == $direct_agent['level'] && $value['config_key'] == $config_key) {
- $direct_agent_price = !empty($value['config_value']) ? $value['config_value'] : 0;
- break;
- }
- }
- if ($direct_agent_price > 0) {
- if (!empty($this->configs['difference_equal_type'])) {
- $diff_price += $direct_agent_price;
- }
- //发放平一级奖励
- $insert_wallet[] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $direct_agent->user_id,
- 'is_frozen' => $is_frozen,
- 'wallet_type' => 'commission',
- 'money' => $direct_agent_price * $this->not_yet_num,
- 'desc' => $desc . '平一级奖励',
- 'source_table' => $source_table,
- 'source_table_id' => $source_table_id,
- 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
- 'capital_type' => UserWalletEnum::CLOUD_STOCK_PRICE_DIFFERENCE_EQUAL,
- 'order_no' => $this->order_no
- ];
- }
- }
- //获取平二级代理商数据
- if (!empty($user['second_parent_id'])) {
- //获取平二级代理商数据
- $second_parent_agent = CloudStockAgent::findOne(['user_id' => $user['second_parent_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
- $second_parent_agent_price = 0;
- if ($second_parent_agent && $second_parent_agent['level'] == $this->parent_agent->level && empty($second_parent_agent['is_equal_award_restriction'])) {
- $config_key = CloudStockEnum::CONFIG_KEY_EQUAL_LEVEL_TWO;
- foreach ($equal_level_list as $value) {
- if ($value['level'] == $second_parent_agent['level'] && $value['config_key'] == $config_key) {
- $second_parent_agent_price = !empty($value['config_value']) ? $value['config_value'] : 0;
- break;
- }
- }
- if ($second_parent_agent_price > 0) {
- if (!empty($this->configs['difference_equal_type'])) {
- $diff_price += $second_parent_agent_price;
- }
- $insert_wallet[] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $second_parent_agent->user_id,
- 'is_frozen' => $is_frozen,
- 'wallet_type' => 'commission',
- 'money' => $second_parent_agent_price * $this->not_yet_num,
- 'desc' => $desc . '平二级奖励',
- 'source_table' => $source_table,
- 'source_table_id' => $source_table_id,
- 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
- 'capital_type' => UserWalletEnum::CLOUD_STOCK_PRICE_DIFFERENCE_EQUAL,
- 'order_no' => $this->order_no,
- ];
- }
- }
- }
- }
- protected function triggerAchievement($params)
- {
- if ($this->order_type == 'fill') {
- try {
- \Yii::warning('触发业绩');
- $event = new FillEvent($this->mall_id);
- $goods_price = $this->order['pay_money'] / $this->order['num'] * $params['num'];
- $order_detail_list[] = [
- 'id' => $this->order['id'],
- 'goods_id' => $this->order['goods_id'],
- 'goods_price' => $goods_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_no,
- 'order_detail_list' => $order_detail_list,
- 'type' => CloudStockEnum::STOCK_CHANGE_SUB,
- 'deduction_detail_id' => $params['deduction_detail_id'] ?? 0,
- ];
- var_dump($data);
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- } catch (\Exception $e) {
- \Yii::error('云库存补货订单业绩' . PHP_EOL . $e->getFile() . '(' . $e->getLine() . ')' . PHP_EOL . "message:" . $e->getMessage());
- }
- }
- if ($this->order_type == 'buying') {
- //触发业绩
- try {
- // 触发云库存订单支付事件
- $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'] / $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_no,
- 'order_detail_list' => $order_detail_list,
- 'type' => CloudStockEnum::STOCK_CHANGE_SUB,
- 'deduction_detail_id' => $params['deduction_detail_id'] ?? 0,
- ];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- } catch (\Exception $e) {
- \Yii::error('会员前端购买订单业绩' . PHP_EOL . $e->getFile() . '(' . $e->getLine() . ')' . PHP_EOL . "message:" . $e->getMessage());
- }
- }
- }
- protected function triggerStatistics($params)
- {
- //扣平台库存,统计用户上级业绩
- if ($this->order['price_difference_order_detail_id']) {
- $model = \addons\CloudStock\common\models\CloudStockPriceDifferenceOrderDetail::findOne(['id' => $this->order['price_difference_order_detail_id']]);
- $goods_price = $model['goods_price'] ?? 0;
- } else {
- $goods_price = $this->order['pay_money'] / $this->order['num'] * $params['num'];
- }
- $post_data = [
- 'mall_id' => $this->mall_id,
- 'goods_price' => $goods_price,
- 'user_id' => $this->order['user_id'],
- 'order_id' => $this->order['id'],
- ];
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, $post_data,
- CloudStockPriceDifferenceUserTeamStatisticsService::class, 'handleSync');
- }
- /**
- * 业绩奖励B
- * @param $params
- * @return void
- */
- protected function triggerAchievementB($params)
- {
- var_dump('业绩奖励B');
- if ($this->order_type == 'fill') {
- try {
- \Yii::warning('触发业绩B');
- $event = new FillEventB($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_no,
- 'order_detail_list' => $order_detail_list
- ];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- } catch (\Exception $e) {
- \Yii::error('云库存补货订单业绩' . PHP_EOL . $e->getFile() . '(' . $e->getLine() . ')' . PHP_EOL . "message:" . $e->getMessage());
- }
- }
- if ($this->order_type == 'buying') {
- //触发业绩
- try {
- // 触发云库存订单支付事件
- $event = new FillEventB($this->mall_id);
- $order_detail_list[] = [
- 'id' => $this->order['id'],
- 'goods_id' => $this->order['goods_id'],
- 'goods_price' => $this->order['after_distribution_price'] / $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_no,
- 'order_detail_list' => $order_detail_list
- ];
- var_dump($data);
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- } 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, $price_difference = 0, $is_first = true)
- {
- var_dump('减之前stock_log_id:parent_agent_goods->num' . $parent_agent_goods->num . ':user_id:' . $parent_agent_goods['user_id']);
- var_dump('减之前stock_log_id:not_yet_num' . $not_yet_num);
- var_dump($price_difference);
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- // 判断上级是否允许给下级扣库存,不允许则直接返回
- if (
- !empty($setting['is_show_setting']) &&
- !empty($setting['is_show_stock_permission']) &&
- !empty($this->parent_agent->is_stock_deducted_by_subordinate)
- ) {
- return $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,
- 'order_type' => $this->order_type == 'fill' ? CloudStockEnum::STOCK_ORDER_TYPE_REPLENISH : CloudStockEnum::STOCK_ORDER_TYPE_USER,
- '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'],
- ];
- if (!$parent_agent_goods->save()) throw new Exception($parent_agent_goods->getErrorMessage());
- var_dump('减之后stock_log_id:parent_agent_goods->num' . $parent_agent_goods->num . ':user_id:' . $parent_agent_goods['user_id']);
- $res = CloudStockDeductionDetail::setData($params);
- if (!$res) throw new Exception(CloudStockDeductionDetail::getStaticError());
- //触发库存变动事件
- $event = new CloudStockChangeEvent($this->mall_id);
- $event_data = [
- 'id' => $res->id,
- 'type' => CloudStockEnum::STOCK_CHANGE_SUB,
- 'mall_id' => $this->mall_id,
- ];
- \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners);
- $this->event_arr[] = [
- 'id' => $res->id,
- 'type' => CloudStockEnum::STOCK_CHANGE_SUB,
- 'mall_id' => $this->mall_id,
- ];
- $is_true = false;
- if ($this->order_type == 'fill') {
- if ($setting['fill_price_difference'] && !empty($setting['price_difference_type']) && $setting['price_difference_type'] == 2) {
- $is_true = true;
- }
- } elseif ($this->order_type == 'buying') {
- if ($setting['buy_price_difference'] && !empty($setting['price_difference_type']) && $setting['price_difference_type'] == 2) {
- $is_true = true;
- }
- }
- $price_differences = 0;//扣货款者的差价
- if ($is_true && !empty($setting['payment_for_goods_price_difference'])) {//货款差价
- //货款修改为按照代理商自身等级的进货价结算
- //差价修改为结算货款者还可拿到差价,然后才截止
- if (isset($this->user_agent_level)) {
- $cloud_stock_goods = CloudStockGoods::findOne(['mall_id' => $this->mall_id, 'goods_id' => $this->goods_id, 'status' => StatusEnum::ENABLED]);
- if (empty($cloud_stock_goods)) return false;
- $agent_price_arr = json_decode($cloud_stock_goods['agent_price'], true);
- $price_arr = array_column($agent_price_arr, 'price', 'level');//代理商等级价格
- $parent_agent_level = $this->parent_agent->level;//代理等级
- $parent_agent_price = $price_arr[$parent_agent_level] ?? 0;
- $payment_for_goods = $parent_agent_price * $num;//货款
- if (empty($this->user_agent_level) || $is_first) {
- $user_agent_price = $this->unit_goods_price;
- } else {
- $user_agent_price = $price_arr[$this->user_agent_level] ?? 0;
- }
- $price_differences = $user_agent_price - $parent_agent_price;
- }
- } else {
- $payment_for_goods = ($this->unit_goods_price - $price_difference) * $num;
- }
- var_dump('单价:::' . $this->unit_goods_price);
- var_dump('货款:::' . $payment_for_goods);
- //货款收益大于0,新增货款收益记录;
- if ($payment_for_goods > 0) {
- if ($this->parent_agent) {
- // TODO: 云库存货款延时结算
- $commission_is_frozen = false;
- if (isset($setting['compute_delay_day']) && !empty($setting['compute_delay_day'])) {
- $commission_is_frozen = true;
- }
- if (empty($setting['payment_is_not_in_commission'])) {
- if ($this->order_type == 'buying') {
- $order = Order::findOne(['id' => $this->order['order_id']]);
- //订单完成后才结算货款
- $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);
- }
- // 判断结算方式,兼容无结算方式设置条件的客户,默认为"支付完成后"结算
- if (!$commission_is_frozen && (!isset($setting['compute_type']) || empty($setting['compute_type']))) {
- $commission_is_frozen = true;
- $orderDetail = OrderDetail::findOne($this->order['order_detail_id']);
- if ($orderDetail['order_status'] == -4 && $orderDetail['is_feedback'] == 2) { // 是否退款完成:订单详情已关闭 && 维权完成
- $commission_is_frozen = -1; // 不发佣金
- } else if ($order['order_status'] == 4) { // 订单已完成
- $commission_is_frozen = false;
- }
- }
- } else {
- $source_table = CloudStockFillOrder::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);
- }
- }
- if ($commission_is_frozen !== -1) {
- $this->fill_insert_wallet[] = $insert_wallet [] = ['mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id,
- 'is_frozen' => $commission_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,
- 'capital_type' => $capital_type,
- 'order_no' => $this->order_no,
- ];
- }
- //投递到资产队列
- if (!empty($insert_wallet)) {
- CloudStockAgent::sendUserWalletQueue($insert_wallet);
- }
- } else {
- //开启货款不计入佣金
- $order_id = $order_detail_id = 0;
- switch ($this->order_type) {
- case 'fill':
- $order_id = $this->order['id'];
- $order_detail_id = $this->order['id'];
- break;
- case 'buying':
- $order_id = $this->order['order_id'];
- $order_detail_id = $this->order['order_detail_id'];
- if (!$commission_is_frozen && (!isset($setting['compute_type']) || empty($setting['compute_type']))) {
- $commission_is_frozen = true;
- $orderDetail = OrderDetail::findOne($this->order['order_detail_id']);
- $order = Order::findOne(['id' => $this->order['order_id']]);
- if ($orderDetail['order_status'] == -4 && $orderDetail['is_feedback'] == 2) { // 是否退款完成:订单详情已关闭 && 维权完成
- $commission_is_frozen = -1; // 不发货款了
- } else if ($order['order_status'] == 4) { // 订单已完成
- $commission_is_frozen = false;
- }
- }
- break;
- }
- if ($commission_is_frozen !== -1) {
- $goods = Goods::findOne(['id' => $this->order['goods_id']]);
- if (empty($setting['profit_in_commission'])) {
- //货款全部进入云仓货款
- $add_data = [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'type' => $this->order_type,
- 'user_id' => $this->parent_agent->user_id,
- 'buy_user_id' => $this->order['user_id'],
- 'goods_id' => $this->order['goods_id'],
- 'goods_name' => $goods['goods_name'],
- 'pic_url' => $goods['cover_pic'],
- 'order_id' => $order_id,
- 'order_detail_id' => $order_detail_id,
- 'num' => $num,
- 'order_no' => $this->order_no,
- 'money' => $payment_for_goods,
- 'status' => $commission_is_frozen ? 0 : 1,
- ];
- } else {
- //拿货价计入货款,差价计入佣金
- $stock_goods = $this->stock_goods;
- $agent_price_arr = json_decode($stock_goods['agent_price'], true);
- $agent_price_arr = array_column($agent_price_arr, null, 'level');
- $profit = $warehouse_payment = 0;
- if (isset($agent_price_arr) && isset($agent_price_arr[$this->parent_agent['level']])) {
- $agent_price = $agent_price_arr[$this->parent_agent['level']]['price'] * $num;//拿货价
- if ($payment_for_goods > $agent_price) {
- $profit = $payment_for_goods - $agent_price;
- $warehouse_payment = $agent_price;
- } else {
- $warehouse_payment = $agent_price;
- }
- }
- if ($profit > 0) {
- $this->profitInCommission($commission_is_frozen, $profit);
- }
- if ($warehouse_payment > 0) {
- $add_data = [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'type' => $this->order_type,
- 'user_id' => $this->parent_agent->user_id,
- 'buy_user_id' => $this->order['user_id'],
- 'goods_id' => $this->order['goods_id'],
- 'goods_name' => $goods['goods_name'],
- 'pic_url' => $goods['cover_pic'],
- 'order_id' => $order_id,
- 'order_detail_id' => $order_detail_id,
- 'num' => $num,
- 'order_no' => $this->order_no,
- 'money' => $warehouse_payment,
- 'status' => $commission_is_frozen ? 0 : 1,
- ];
- }
- }
- if ($this->order_type == 'buying') {
- if (isset($add_data)) {
- $this->payment_insert_wallet[] = $add_data;
- }
- }
- (new PaymentLogService(['mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id]))->add($add_data);
- }
- }
- if ($this->order_type == 'buying' && isset($commission_is_frozen) && $commission_is_frozen == false) {
- $obj = new PriceDifferenceProfitService(['mall_id' => $this->mall_id]);
- $obj->order_no = $this->order_no;
- $obj->compute(3, $num, $this->order, $this->parent_agent);//会员买货触发差价利润金额
- }
- }
- if (!empty($this->recommend_reward_arr)) {
- if ($this->not_distribute_payment_for_goods) {
- $new_recommend_reward_arr = [];
- foreach ($this->recommend_reward_arr as $val) {
- $total_num = $this->order->num;
- //重新计算佣金,当前扣除上级库存件数*每件可获取佣金
- $val['money'] = bcmul($val['money'] / $total_num, $num, 2);
- $desc_array = explode(',', $val['desc']);
- $val['desc'] = $desc_array[0].','.$desc_array[1].','.$desc_array[2].',金额:'.$val['money'];
- $new_recommend_reward_arr[] = $val;
- if ($val['user_id'] == $this->parent_agent->user_id) {
- break;
- }
- }
- } else {
- $new_recommend_reward_arr = $this->recommend_reward_arr;
- }
- $this->sendRecommendReward($new_recommend_reward_arr);
- }
- }
- if ($this->order_type == 'fill') {
- (new PriceDifferenceProfitService(['mall_id' => $this->order['mall_id']]))->compute(2, $num, $this->order, $this->parent_agent);//补货触发差价利润金额
- }
- if ($price_differences > 0) {
- $commission_is_frozen = false;
- if (isset($setting['compute_delay_day']) && !empty($setting['compute_delay_day'])) {
- $commission_is_frozen = true;
- }
- if ($this->order_type == 'buying') {
- $source_table = OrderDetail::tableName();
- $source_table_id = $this->order['order_detail_id'];
- $desc = '会员买货时,结算货款者拿补货差价';
- $capital_type = UserWalletEnum::CLOUD_STOCK_BUY_PRICE_DIFFERENCE;
- $is_frozen = true;
- // 判断结算方式,兼容无结算方式设置条件的客户,默认为"支付完成后"结算
- if (!$commission_is_frozen && (!isset($setting['compute_type']) || empty($setting['compute_type']))) {
- $commission_is_frozen = true;
- }
- } else {
- $source_table = CloudStockFillOrder::tableName();
- $source_table_id = $this->order['id'];
- $desc = '补货时,结算货款者拿补货差价';
- $capital_type = UserWalletEnum::CLOUD_STOCK_FILL_PRICE_DIFFERENCE;
- $is_frozen = false;
- }
- $insert_wallet = [];
- $this->addPriceDifference($commission_is_frozen, $price_differences, $desc, $source_table, $source_table_id, $capital_type, $num, $insert_wallet);
- if (!empty($insert_wallet)) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (!$res) return false;
- (new EqualAwardService(['mall_id' => $this->mall_id]))->setStatistics($insert_wallet, UserWalletEnum::EQUAL);
- }
- }
- return $not_yet_num;
- }
- protected function addPriceDifference($commission_is_frozen, $price_difference, $desc, $source_table, $source_table_id, $capital_type, $num, &$insert_wallet)
- {
- if ($this->not_get_price_difference && !empty($this->recommend_reward_arr)) {
- $recommend_reward_arr = array_column($this->recommend_reward_arr, null, 'user_id');
- if (isset($recommend_reward_arr[$this->parent_agent->user_id])) {
- return true;
- }
- }
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- if (empty($setting['payment_is_not_in_commission'])) {
- $this->fill_insert_wallet[] = $insert_wallet[] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $this->parent_agent->user_id,
- 'is_frozen' => $commission_is_frozen,
- 'wallet_type' => 'commission',
- 'money' => $price_difference * $num,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $source_table_id,
- 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
- 'capital_type' => $capital_type,
- 'order_no' => $this->order_no,
- ];
- } else {
- if ($this->order_type == 'buying') {
- $order_id = $this->order['order_id'];
- $order_detail_id = $this->order['order_detail_id'];
- } else {
- $order_id = $this->order['id'];
- $order_detail_id = $this->order['id'];
- }
- $goods = Goods::findOne(['id' => $this->order['goods_id']]);
- $add_data = [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'type' => $this->order_type,
- 'user_id' => $this->parent_agent->user_id,
- 'buy_user_id' => $this->order['user_id'],
- 'goods_id' => $this->order['goods_id'],
- 'goods_name' => $goods['goods_name'],
- 'pic_url' => $goods['cover_pic'],
- 'order_id' => $order_id,
- 'order_detail_id' => $order_detail_id,
- 'num' => $num,
- 'order_no' => $this->order_no,
- 'money' => $price_difference * $num,
- 'status' => $commission_is_frozen ? 0 : 1,
- ];
- $this->payment_insert_wallet[] = $add_data;
- (new PaymentLogService(['mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id]))->add($add_data);
- }
- }
- protected function profitInCommission($commission_is_frozen, $profit)
- {
- if ($this->order_type == 'buying') {
- $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 = CloudStockFillOrder::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);
- }
- }
- $insert_wallet = [];
- $this->fill_insert_wallet[] = $insert_wallet [] = ['mall_id' => $this->mall_id, 'user_id' => $this->parent_agent->user_id,
- 'is_frozen' => $commission_is_frozen, 'wallet_type' => 'commission', 'money' => $profit,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $source_table_id,
- 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
- 'capital_type' => $capital_type,
- ];
- //投递到资产队列
- if ($insert_wallet) {
- CloudStockAgent::sendUserWalletQueue($insert_wallet);
- }
- }
- 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)) {
- //过了限时补货,而不补货,再往上找符合条件的代理商扣库存
- $type = 2;
- if ($this->order_type == 'buying') {
- $type = 3;
- }
- (new LossAmountService(['mall_id' => $this->mall_id]))->compute($type, $this->not_yet_num, $this->order, $this->parent_agent);//补货、会员买货触发流失金额
- $parent_agent = CloudStockAgent::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,
- 'goods_id' => $this->goods_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,
- ];
- var_dump('订单id:' . $this->order['id'] . '触发限时补货,时间是:' . $this->fill_hour);
- var_dump($data);
- CloudStockPushDelayLog::setData($data);
- \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'];
- $goods_id = $this->goods_id;
- mb_substr($goods_name, 0, 24);
- $message = new TimeLimitFillMessage($this->mall_id, $this->fill_hour, $goods_id, $this->not_yet_num);
- $user = User::findOne(['id' => $agent_user_id]);
- $easy_sms->send($user['mobile'], $message);
- } catch (NoGatewayAvailableException $e) {
- $res = $e->getExceptions();
- if (!empty($res)) {
- foreach ($res as $exception) {
- if (is_string($exception->getMessage())) {
- var_dump('云库存补货通知短信发送失败:' . $exception->getMessage());
- }
- }
- }
- } catch (\Exception $e) {
- var_dump('云库存补货通知短信发送失败:' . $e->getMessage());
- }
- return true;
- }
- } else {
- //没有开启限时补货或者下单人的上级代理商不存在,递归往上找,直到找平台扣库存
- $type = 2;
- if ($this->order_type == 'buying') {
- $type = 3;
- }
- (new LossAmountService(['mall_id' => $this->mall_id]))->compute($type, $this->not_yet_num, $this->order, $this->parent_agent);//补货、会员买货触发流失金额
- $this->parent_agent = CloudStockAgent::getUserParentAgent($agent_user_id, $level);
- return $this->deduct();
- }
- }
- }
|