TimestampBehavior::class ] ]; } /** * @inheritdoc */ public function rules() { return [ [['id', 'store_id', 'user_id', 'type', 'type_id', 'created_at', 'updated_at', 'is_freeze', 'is_send', 'is_send_freeze', 'freeze_send_time', 'send_time', 'order_id'], 'integer'], [['amount', 'radix', 'profit', 'freeze_amount'], 'number'], [['group_data'], 'string'] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => '', 'store_id' => 'Store Id', 'user_id' => '用户ID', 'type' => '佣金类型:0直推 1成团 2帮扶', 'type_id' => '佣金来源表ID', 'amount' => '佣金金额', 'radix' => '佣金基数', 'profit' => '佣金计算比例', 'is_freeze' => '是否带冻结佣金', 'is_send' => '是否发放佣金', 'is_send_freeze' => '是否发放冻结佣金', 'send_time' => '发放时间', 'freeze_amount' => '冻结佣金金额 帮扶时冻结佣金+佣金金额 = 全部的佣金金额', 'group_data' => '成团数据', 'order_id' => '直推/帮扶订单ID', 'created_at' => '', 'updated_at' => '' ]; } //添加佣金记录 主要是直推以及帮扶 public static function addMoneyLog($type, $type_id, $store_id, $order_id) { try { $money_log = self::findOne(['type' => $type, 'type_id' => $type_id, 'order_id' => $order_id]); if ($money_log) { throw new \Exception('记录已经存在'); } $order_detail = OrderDetail::find()->where(['order_id' => $order_id])->select('goods_id, total_price') ->asArray()->all(); $share_group_setting = Option::get('share_group_setting', $store_id, 'share_group')['value']; $share_group_setting = json_decode($share_group_setting ?? '', true); if (empty($share_group_setting)) { throw new \Exception('未配置佣金设置'); } $goods_ids = explode(',', $share_group_setting['goods_ids'] ?? ''); $share_group_direct_profit = $share_group_setting['share_group_direct_profit']; $share_group_support_profit = $share_group_setting['share_group_support_profit']; $share_group_other_accounts_switch = $share_group_setting['share_group_other_accounts_switch']; $share_group_support_freeze_profit = $share_group_setting['share_group_support_freeze_profit']; $support_group_price_send_type = intval($share_group_setting['support_group_price_send_type'] ?? 0); $open = false; $radix = 0; foreach ($order_detail as $goods_item) { if (in_array($goods_item['goods_id'], $goods_ids)) { $open = true; $radix = bcadd($goods_item['total_price'], $radix, 2); } } if (!$open) { throw new \Exception('下单商品未在拼购商品列表内'); } $is_freeze = 0; $is_send = 0; $freeze_amount = 0; $profit = 0; $amount = 0; $user_id = 0; if ($type == self::TYPE_DIRECT) { $amount = bcmul($radix, bcdiv($share_group_direct_profit, 100, 2), 2); $profit = $share_group_direct_profit; //获取原上级的用户信息 $purchase_log = ShareGroupPurchaseParentLog::findOne($type_id); $user_id = $purchase_log->parent_user_id; //判断整个树中是否存在原上级 增加batch_id判断是为了优化下级先成为37拼购成员 上级后成为37拼购成员 后给原上级发放佣金的bug $checkParentLog = ShareGroupPurchaseParentLog::find()->where(['user_id' => $user_id])->andWhere(['OR', ['batch_id' => $purchase_log->batch_id], [ 'id' => $purchase_log->batch_id ]])->asArray()->one(); if (!$checkParentLog) { //如果不存在上级 可能存在问题就是 不是同一条树 可能是帮扶过来的 或者就是真的没有上级 // throw new \Exception('上级不存在'); $user_id = 0; } //判断是否存在扶持 //新逻辑 如果后台设置的帮扶区直推佣金发放给0原上级/1新上级 $shareGroupSupportLog = ShareGroupSupportLog::findOne(['user_id' => $purchase_log->user_id]); if ($shareGroupSupportLog) { if ($support_group_price_send_type) { //判断不是顶级的就需要给新上级发放佣金 if ($shareGroupSupportLog->parent_id != 0) { $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => $purchase_log->user_id]); if ($shareGroupPurchaseUser) { $user_id = $shareGroupPurchaseUser->parent_user_id; } } else { //如果是顶级的 还是需要给原上级发放佣金 $user_id = $shareGroupSupportLog->parent_user_id; } } else { $user_id = $shareGroupSupportLog->parent_user_id; } } if (!$user_id) { throw new \Exception('上级不存在'); } } elseif ($type == self::TYPE_SUPPORT) { //判断已经完成过拼购就不计算冻结佣金 否则需要将佣金冻结 $amount = bcmul($radix, bcdiv($share_group_support_profit, 100, 2), 2); $profit = $share_group_support_profit; $shareGroupParentSupportLog = ShareGroupSupportLog::findOne($type_id); $user_id = $shareGroupParentSupportLog->parent_user_id; //判断是否已经完成过拼购 $group_log = self::findOne(['type' => self::TYPE_GROUP, 'user_id' => $shareGroupParentSupportLog->parent_user_id]); $purchase_log = ShareGroupPurchaseParentLog::findOne(['user_id' => $user_id]); //是否冻结 if (!ShareGroupPurchaseParentLog::checkUnfreeze($user_id, $purchase_log->batch_id ?: $group_log->id)) { if (intval($share_group_other_accounts_switch) && $share_group_support_freeze_profit > 0) { $is_freeze = 1; $freeze_amount = bcmul($amount, bcdiv($share_group_support_freeze_profit, 100, 2), 2); $amount = bcsub($amount, $freeze_amount, 2); } } } $form = new self(); $form->store_id = $store_id; $form->user_id = $user_id; $form->type = $type; $form->type_id = $type_id; $form->amount = $amount; $form->radix = $radix; $form->profit = $profit; $form->is_freeze = $is_freeze; $form->is_send = $is_send; $form->order_id = $order_id; $form->freeze_amount = $freeze_amount; if (!$form->save()) { throw new \Exception(implode(';', array_values($form->firstErrors))); } return [ 'code' => 0, 'msg' => 'success' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public static function checkIsFinish($user_id) { $purchase_parent_user_log = ShareGroupPurchaseParentLog::find()->where(['user_id' => $user_id]) ->select('id')->asArray()->all(); foreach ($purchase_parent_user_log as $item) { $purchase_parent_child_user_log = ShareGroupPurchaseParentLog::find()->where(['parent_id' => $item['id']]) ->select('id')->column(); if (count($purchase_parent_child_user_log) == 2) { $purchase_parent_min_user_log = ShareGroupPurchaseParentLog::find()->where(['parent_id' => $purchase_parent_child_user_log]) ->select('id')->column(); if (count($purchase_parent_min_user_log) == 4) { return true; } } } return false; } //检测存在团里面有取消订单的情况的团 public static function checkCancelOrderMoney($order_id) { //通过订单获取对应的团 $shareGroupMoneySub = ShareGroupMoneySub::findOne(['order_id' => $order_id]); if ($shareGroupMoneySub) { $shareGroupMoneyId = ShareGroupMoney::findOne(['is_send' => 0, 'id' => $shareGroupMoneySub->share_money_id]); //判断当前团是否满足拼购人数需求 $open = false; $shareGroupMoneySubOrderId = ShareGroupMoneySub::find()->where(['share_money_id' => $shareGroupMoneyId->id]) ->select('order_id')->column(); if ($shareGroupMoneySubOrderId < 6) { return; } //获取已经取消的订单或者已经完成售后的订单 $cancel_order_id = []; $order = Order::find()->where(['id' => $shareGroupMoneySubOrderId, 'trade_status' => Order::ORDER_FLOW_CANCEL]) ->select('id')->column(); if ($order) { $cancel_order_id = $order; $open = true; } $orderRefund = OrderRefund::find()->where(['order_id' => $shareGroupMoneySubOrderId, 'is_delete' => 0, 'is_user_cancel' => 0, 'status' => [2, 3]]) ->select('order_id')->column(); if ($orderRefund) { $cancel_order_id = array_merge($cancel_order_id, $orderRefund); // 如果售后类型不是换货或拒绝退换货,不处理 $open = true; } if ($open) { $order_is_sale = Order::find()->where(['is_sale' => 1])->andWhere(['NOT IN', 'id', $cancel_order_id]) ->select('id')->column(); //如果订单中同时存在已取消或者已售后的订单且他俩数量 和 已完成售后的订单数量加起来小于6 那说明还存在未完成售后的订单 if (count($order_is_sale) + count($cancel_order_id) < 6) { return; } $order = Order::find()->where(['id' => $shareGroupMoneyId->order_id])->asArray()->one(); self::sendMoney($order); } } } public static function sendMoney($order) { debug_log(['order_id' => $order['id']], '20250422.log'); $t = \Yii::$app->db->beginTransaction(); try { // $groupPurchaseParentLog = ShareGroupPurchaseParentLog::find()->where(['order_id' => $order['id']]) // ->select('id')->column(); // // // $groupSupportLog = ShareGroupSupportLog::find()->where(['order_id' => $order['id']]) // ->select('id')->column(); //订单过售后期发放佣金 $freeze_amount_list = self::find()->where([ 'is_send' => 0, 'type' => [ self::TYPE_DIRECT, self::TYPE_SUPPORT, ], 'order_id' => $order['id'] ])->select('id')->column(); foreach ($freeze_amount_list as $freeze_amount_item) { $freeze_amount_item_ = self::findOne(['id' => $freeze_amount_item, 'is_send' => 0]); if (!$freeze_amount_item_) { continue; } $freeze_amount_item_->is_send = 1; $freeze_amount_item_->send_time = time(); $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => $freeze_amount_item_->user_id]); if (!$shareGroupPurchaseUser) { continue; } if ($freeze_amount_item_->type == self::TYPE_DIRECT) { $shareGroupPurchaseUser->direct_price += $freeze_amount_item_->amount; $shareGroupPurchaseUser->total_price += $freeze_amount_item_->amount; $shareGroupPurchaseUser->price += $freeze_amount_item_->amount; } if ($freeze_amount_item_->type == self::TYPE_SUPPORT) { $shareGroupPurchaseUser->support_price += $freeze_amount_item_->amount; $shareGroupPurchaseUser->total_price += $freeze_amount_item_->amount; $shareGroupPurchaseUser->price += $freeze_amount_item_->amount; } //判断用户是否存在 存在则开始发放 $user = User::findOne(['id' => $freeze_amount_item_->user_id, 'is_delete' => 0]); if ($user && $freeze_amount_item_->amount > 0) { $user->price = bcadd($user->price, $freeze_amount_item_->amount, 2); $user->total_price = bcadd($user->total_price, $freeze_amount_item_->amount, 2); if (!$user->save()) { throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE)); } $result = UserShareMoney::set($freeze_amount_item_->amount, $freeze_amount_item_->user_id, 0, 0, 12, $freeze_amount_item_->store_id, 0, '37拼购'.($freeze_amount_item_->type == ShareGroupMoney::TYPE_SUPPORT ? "扶持" : "直推").'佣金发放'); if (!$result) { throw new \Exception('保存失败'); } $group_parent_purchase_log = ShareGroupPurchaseParentLog::findOne(['user_id' => $freeze_amount_item_->user_id]); //判断是否已经完成过拼购 if (ShareGroupPurchaseParentLog::checkUnfreeze($freeze_amount_item_->user_id, $group_parent_purchase_log->batch_id ?: $group_parent_purchase_log->id) && intval($freeze_amount_item_->is_freeze) === 1 && intval($freeze_amount_item_->is_send_freeze) == 0 && $freeze_amount_item_->freeze_amount > 0 ) { $freeze_amount_item_->is_send_freeze = 1; $freeze_amount_item_->freeze_send_time = time(); $shareGroupPurchaseUser->support_price += $freeze_amount_item_->freeze_amount; $shareGroupPurchaseUser->total_price += $freeze_amount_item_->freeze_amount; $shareGroupPurchaseUser->price += $freeze_amount_item_->freeze_amount; $user->price = bcadd($user->price, $freeze_amount_item_->freeze_amount, 2); $user->total_price = bcadd($user->total_price, $freeze_amount_item_->freeze_amount, 2); if (!$user->save()) { throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE)); } $result = UserShareMoney::set($freeze_amount_item_->freeze_amount, $freeze_amount_item_->user_id, 0, 0, 12, $freeze_amount_item_->store_id, 0, '37拼购冻结佣金发放'); if (!$result) { throw new \Exception('保存失败'); } } } if (!$freeze_amount_item_->save()) { throw new \Exception(json_encode($freeze_amount_item_->errors, JSON_UNESCAPED_UNICODE)); } if (!$shareGroupPurchaseUser->save()) { throw new \Exception(json_encode($shareGroupPurchaseUser->errors, JSON_UNESCAPED_UNICODE)); } } $group_sub_order_log = ShareGroupMoneySub::find()->where(['order_id' => $order['id']])->select('share_money_id') ->asArray()->all(); foreach ($group_sub_order_log as $group_sub_order_log_item) { $shareGroupMoney = self::findOne(['id' => $group_sub_order_log_item['share_money_id']]);//, 'is_send' => 0 兼容之前问题导致的提前释放问题 if (!$shareGroupMoney) { continue; } $group_sub_log_ = ShareGroupMoneySub::find()->where(['share_money_id' => $group_sub_order_log_item['share_money_id']]) ->select('id, share_money_id, amount, is_send') ->asArray()->all(); if (count($group_sub_log_) < 6) { continue; } $group_sub_log_send_amount = 0; foreach ($group_sub_log_ as $group_sub_log_item) { if (intval($group_sub_log_item['is_send'])) { $group_sub_log_send_amount = bcadd($group_sub_log_send_amount, $group_sub_log_item['amount'], 2); } } $group_sub_log = ShareGroupMoneySub::find()->where(['share_money_id' => $group_sub_order_log_item['share_money_id'], 'is_send' => 0]) ->select('id, share_money_id, amount') ->asArray()->all(); $shareGroupMoneyAmount = $shareGroupMoney->amount; if ($shareGroupMoney->profit_type == self::PROFIT_TYPE_COMMON) { $shareGroupMoneyAmount = bcsub($shareGroupMoneyAmount, $group_sub_log_send_amount, 2); foreach ($group_sub_log as $group_sub_item) { $shareGroupMoneySub = ShareGroupMoneySub::findOne($group_sub_item['id']); $order = Order::findOne($shareGroupMoneySub->order_id); //如果订单在过完售后期且存在售后订单或者存在已经取消的订单 就计算剩余金额发放给用户 if ($order->trade_status == Order::ORDER_FLOW_CANCEL) { $shareGroupMoneyAmount = bcsub($shareGroupMoney->amount, $shareGroupMoneySub->amount, 2); continue; } $orderRefund = OrderRefund::findOne(['order_id' => $shareGroupMoneySub->order_id, 'is_delete' => 0, 'is_user_cancel' => 0]); if ($orderRefund && !\in_array($orderRefund->status, [2, 3])) { // 如果售后类型不是换货或拒绝退换货,不处理 $shareGroupMoneyAmount = bcsub($shareGroupMoney->amount, $shareGroupMoneySub->amount, 2); continue; } if (!intval($order->is_sale)) { goto order_sale; } } } if ($shareGroupMoneyAmount <= 0) { debug_log(['shareGroupMoneyAmount' => $shareGroupMoneyAmount, 'continue' => 1], '20250422.log'); $shareGroupMoney->is_send = 1; $shareGroupMoney->send_time = time(); if (!$shareGroupMoney->save()) { throw new \Exception(json_encode($shareGroupMoney->errors, JSON_UNESCAPED_UNICODE)); } continue; } $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => $shareGroupMoney->user_id]); if (!$shareGroupPurchaseUser) { continue; } debug_log(['shareGroupMoneyAmount' => $shareGroupMoneyAmount], '20250422.log'); $user_price = 0; //判断用户是否存在 存在则开始发放 $user = User::findOne(['id' => $shareGroupMoney->user_id, 'is_delete' => 0]); if ($user) { if ($shareGroupMoney->profit_type == self::PROFIT_TYPE_COMMON) { $user_price = $shareGroupMoneyAmount; } else { AccountLog::saveLog( $shareGroupMoney->user_id, $shareGroupMoneyAmount, AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, 0, 0, '37拼购成团余额发放' ); } } debug_log(['user_price' => $user_price], '20250422.log'); ShareGroupMoneySub::updateAll(['is_send' => 1, 'updated_at' => time()], ['share_money_id' => $shareGroupMoney->id]); $shareGroupMoney->is_send = 1; $shareGroupMoney->send_time = time(); if (!$shareGroupMoney->save()) { throw new \Exception(json_encode($shareGroupMoney->errors, JSON_UNESCAPED_UNICODE)); } //自动复购 if (intval($shareGroupMoney->profit_type) === self::PROFIT_TYPE_COMMON) { $share_group_setting = Option::get('share_group_setting', $shareGroupMoney->store_id, 'share_group')['value']; $share_group_setting = json_decode($share_group_setting ?? '', true); $share_group_commission_profit = $share_group_setting['share_group_commission_profit'];//成团佣金比例 $share_group_auto_switch = intval($share_group_setting['share_group_auto_switch']);//是否自动复购 if ($share_group_auto_switch) { $shareGroupPurchaseParentLog = ShareGroupPurchaseParentLog::findOne($shareGroupMoney->type_id); $old_order = Order::findOne($shareGroupPurchaseParentLog->order_id); $old_order = ArrayHelper::toArray($old_order); $old_order_detail_id = $shareGroupPurchaseParentLog->order_detail_id; $old_order_detail_id = explode(',', $old_order_detail_id); $old_order_detail_ = OrderDetail::find()->where(['id' => $old_order_detail_id]) ->asArray()->all(); $pay_price_ = $old_order['express_price']; $old_order_detail = []; foreach ($old_order_detail_ as $old_order_detail_item) { $goods = Goods::find()->where('id = :id FOR UPDATE', [':id' => $old_order_detail_item['goods_id']])->one(); // $goods = Goods::findOne($old_order_detail_item['goods_id']); $old_order_detail_item['attr'] = json_decode($old_order_detail_item['attr'], true); $attr_id_list = array_column($old_order_detail_item['attr'], 'attr_id'); try { $goodsNumSubResult = $goods->numSub($attr_id_list, $old_order_detail_item['num'], $old_order['md_id'] ?: -1); } catch (\Exception $e) { $goodsNumSubResult = false; } if ($goodsNumSubResult) { $old_order_detail_item['attr'] = json_encode($old_order_detail_item['attr'], JSON_UNESCAPED_UNICODE); $old_order_detail[] = $old_order_detail_item; $pay_price_ = bcadd($pay_price_, $old_order_detail_item['total_price'], 2); } } if (empty($old_order_detail)) { debug_log(['复购失败商品库存不足'], 'share_group_money.log'); goto order_price; } if ($shareGroupMoneyAmount < $pay_price_) { debug_log(['复购失败佣金小于订单金额'], 'share_group_money.log'); goto order_price; } unset($old_order['id']); $order = new Order(); $order->attributes = $old_order; $order->total_price = $order->pay_price = $pay_price_; $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL); $order->created_at = time(); $order->trade_status = 0; $order->is_sale = 0; $order->md_id = $old_order['md_id']; $order->arrival_time = $old_order['arrival_time']; $order->integral_price = 0; $order->integral_difference_price = 0; $order->confirm_time = 0; $order->send_time = 0; $order->pay_time = time(); $order->is_price = 0; // $order->rebate = 0; // $order->first_price = 0; // $order->second_price = 0; // $order->third_price = 0; $order->coupon_sub_price = 0; $order->integral = json_encode(["forehead" => "0.00", "forehead_integral" => "0.00"]); $order->give_integral = 0; $order->share_price = 0; $order->future_sales_time = 0; $order->take_price = 0; $order->send_price = 0; $result = $order->save(); if ($result) { debug_log(['error_order' => $order->getErrors()], 'share_group_money.log'); } $goods_profit = 0; foreach ($old_order_detail as $old_order_detail_item) { unset($old_order_detail_item['id']); $order_detail = new OrderDetail(); $order_detail->attributes = $old_order_detail_item; $order_detail->order_id = $order->id; $result = $order_detail->save(); if (!$result) { debug_log(['error_order' => $order_detail->getErrors()], 'share_group_money.log'); throw new \Exception('订单详情保存失败' . json_encode($order_detail->getErrors(), JSON_UNESCAPED_UNICODE)); } // $goods_profit = bcmul($old_order_detail_item['total_price'], bcdiv($share_group_commission_profit, 100, 2), 2); } $user_price = bcsub($user_price, $order->pay_price, 2); // $user_price = bcadd($user_price, $goods_profit); $form = new OrderComplete(); $form->order_id = $order->id; $form->order_type = 0; $form->store_id = $order->store_id; $form->is_auto_repeat = 1; $form->notify(); } } order_price: $user->price = bcadd($user->price, $user_price, 2); if (!$user->save()) { throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE)); } // UserShareMoney::set($user_price, $shareGroupMoney->user_id, 0, 0, 12, $shareGroupMoney->store_id, 0, '37拼购成团佣金发放'); // if (!$result) { // throw new \Exception('保存失败'); // } if ($shareGroupMoney->profit_type == self::PROFIT_TYPE_COMMON) { $result = UserShareMoney::set($user_price, $shareGroupMoney->user_id, 0, 0, 12, $shareGroupMoney->store_id, 0, '37拼购成团佣金发放'); // if (!$result) { // throw new \Exception('保存失败'); // } $shareGroupPurchaseUser->group_price += $user_price; $shareGroupPurchaseUser->total_price += $user_price; $shareGroupPurchaseUser->price += $user_price; if (!$shareGroupPurchaseUser->save()) { throw new \Exception(json_encode($shareGroupPurchaseUser->errors, JSON_UNESCAPED_UNICODE)); } } } order_sale: $t->commit(); return [ 'code' => 0, 'msg' => '' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine() . $e->getFile() ]; } } }