store_id ? $this->store_id : get_store_id(); \Yii::warning('=== 分销计算:订单id:' . $this->order->id . ',计算分销开始 ==='); $setting = Option::get('share_money_setting', $store_id); $setting = $setting ? Json::decode($setting['value']) : []; if (!$setting) { \Yii::warning('未开启分销设置1'); return false; } // if (!is_array($setting['commission_type']) || empty($setting['commission_type'])) { // \Yii::warning('未开启分销设置2'); // return false; // } // 获取到当前到订单佣金发放类型 1: 佣金 2:积分, 3:余额 $this->share_send_type = $setting['giveType'] ?? 1; // 获取积分和佣金的兑换比例 $integral = Option::get('integral', $this->order->store_id, 'store')['value']; if (!$integral && $this->share_send_type == 2) { \Yii::warning('发放积分佣金类型比例未设置'); return false; } $this->integral = $integral; // 订单分销 2 $this->order_share_rate = $setting['scale'] ?: 0; $this->setOrderProfit(); $this->setLimitProfit($setting); $order = $this->order; /* @var $orderShare Order */ $orderShare = $this->order; $orderShare->share_send_type = $this->share_send_type; $orderDetail = $this->getOrderDetail(); if (!$orderDetail) { \Yii::warning('订单详情不存在'); return false; } $user = User::findOne($this->order->user_id); if (!$user) { \Yii::warning('订单用户不存在'); return false; } $cParent1 = $user->parent_id; $orderShare->parent_id = $cParent1; if ($user->parent_id) { $parent = User::findOne($user->parent_id);//上级 $cParent2 = $parent->parent_id; $orderShare->parent_id_1 = $cParent2; if ($parent->parent_id) { $parent_1 = User::findOne($parent->parent_id);//上上级 $cParent3 = $parent_1->parent_id; $orderShare->parent_id_2 = $cParent3; } else { $orderShare->parent_id_2 = -1; } } else { $orderShare->parent_id_1 = -1; $orderShare->parent_id_2 = -1; } // 旧关系记录 if ($user->old_parent_id > 0) { $old_parent = User::findOne($user->old_parent_id);//上级 $orderShare->old_parent_id = $old_parent->id; if ($old_parent->old_parent_id > 0) { $old_parent_1 = User::findOne($old_parent->old_parent_id);//上上级 $orderShare->old_parent_id_1 = $old_parent_1->id; if ($old_parent_1->old_parent_id > 0) { $old_parent_2 = User::findOne($old_parent_1->old_parent_id);//上上级 $orderShare->old_parent_id_2 = $old_parent_2->id; } } } // $share_commission_money_first = 0;//一级分销总佣金 // $share_commission_money_second = 0;//二级分销总佣金 // $share_commission_money_third = 0;//三级分销总佣金 // foreach ($orderDetail as $item) { // // TODO: 价格 // $item_price = doubleval($item['price']); // // $item_price = doubleval($orderShare->pay_price); // // if ($item['individual_share'] == 1) { // $rate_first = doubleval($item['share_commission_first']); // $rate_second = doubleval($item['share_commission_second']); // $rate_third = doubleval($item['share_commission_third']); // $shareType = $item['share_type']; // } else { // $setting_shara = Option::get('share_money_setting', $order->store_id, 'share'); // $setting_shara = $setting_shara ? Json::decode($setting_shara['value']) : []; // $rate_first = doubleval($setting_shara['level_one']['value']); // $rate_second = doubleval($setting_shara['level_two']['value']); // $rate_third = doubleval($setting_shara['level_three']['value']); // $shareType = isset($setting_shara['price_type']) ? $setting_shara['price_type'] : 0; // } // if ($shareType == 1) { // 金钱 // if ($this->share_send_type == 2) { // // 兑换比例 // $share_commission_money_first += round($rate_first * $integral) * $item['num']; // $share_commission_money_second += round($rate_second * $integral) * $item['num']; // $share_commission_money_third += round($rate_third * $integral) * $item['num']; // } else { // $share_commission_money_first += $rate_first * $item['num']; // $share_commission_money_second += $rate_second * $item['num']; // $share_commission_money_third += $rate_third * $item['num']; // } // } else { // 比例 // if ($this->share_send_type == 2) { // $share_commission_money_first += round($item_price * ($rate_first / 100) * $integral); // $share_commission_money_second += round($item_price * ($rate_second / 100) * $integral); // $share_commission_money_third += round($item_price * ($rate_third / 100 ) * $integral); // } else { // $share_commission_money_first += $item_price * $rate_first / 100; // $share_commission_money_second += $item_price * $rate_second / 100; // $share_commission_money_third += $item_price * $rate_third / 100; // } // } // } $share_setting = Option::get('share_basic_setting', $store_id); $share_setting = $share_setting ? Json::decode($share_setting['value']) : []; // 如果开启自购返利 一级是自己 if ($share_setting['is_rebate']['value'] == 1 && $user->is_distributor == 1) { if ($share_setting['level']['value'] >= 1) { $orderShare->rebate = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->user_id, 1); } if ($share_setting['level']['value'] >= 2) { $orderShare->first_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id, 2); } if (intval($share_setting['level']['value']) === 3) { $orderShare->second_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id_1, 3); } $orderShare->third_price = 0; } else { $orderShare->rebate = 0; if ($share_setting['level']['value'] >= 1) { $orderShare->first_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id, 1); } if ($share_setting['level']['value'] >= 2) { $orderShare->second_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id_1, 2); } if (intval($share_setting['level']['value']) === 3) { $orderShare->third_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id_2, 3); } } // 计算股东分销 // 一级分销 // if ($orderShare->parent_id > 0) { // $share_holder_1 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->parent_id, 'is_delete' => 0, 'status' => 1]); // if ($share_holder_1) { // $level_id_1 = $share_holder_1->level_id; // $one_rate = ShareHolderLevel::findOne($level_id_1)->share_rate_one; // if ($one_rate > 0) { // $orderShare->holder_first_price = $orderShare->profit * doubleval($one_rate) / 100; // } // } // } // 二级分销 // if ($orderShare->parent_id_1 > 0) { // $share_holder_2 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->parent_id_1, 'is_delete' => 0, 'status' => 1]); // if ($share_holder_2) { // $level_id_2 = $share_holder_2->level_id; // $two_rate = ShareHolderLevel::findOne($level_id_2)->share_rate_two; // if ($two_rate > 0) { // $orderShare->holder_second_price = $orderShare->profit * doubleval($two_rate) / 100; // } // } // } // 三级分销 // if ($orderShare->parent_id_2 > 0) { // $share_holder_3 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->parent_id_2, 'is_delete' => 0, 'status' => 1]); // if ($share_holder_3) { // $level_id_3 = $share_holder_3->level_id; // $three_rate = ShareHolderLevel::findOne($level_id_3)->share_rate_three; // if ($two_rate > 0) { // $orderShare->holder_third_price = $orderShare->profit * doubleval($three_rate) / 100; // } // } // } // 计算股东分销(旧关系) // 一级分销 if ($orderShare->old_parent_id > 0) { $share_holder_1 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->old_parent_id, 'is_delete' => 0, 'status' => 1]); if ($share_holder_1) { $level_id_1 = $share_holder_1->level_id; $one_rate = ShareHolderLevel::findOne($level_id_1)->share_rate_one; if ($one_rate > 0) { $orderShare->old_holder_first_price = $orderShare->profit * doubleval($one_rate) / 100; } } } // 二级分销 if ($orderShare->old_parent_id_1 > 0) { $share_holder_2 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->old_parent_id_1, 'is_delete' => 0, 'status' => 1]); if ($share_holder_2) { $level_id_2 = $share_holder_2->level_id; $two_rate = ShareHolderLevel::findOne($level_id_2)->share_rate_two; if ($two_rate > 0) { $orderShare->old_holder_second_price = $orderShare->profit * doubleval($two_rate) / 100; } } } // 三级分销 if ($orderShare->old_parent_id_2 > 0) { $share_holder_3 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->old_parent_id_2, 'is_delete' => 0, 'status' => 1]); if ($share_holder_3) { $level_id_3 = $share_holder_3->level_id; $three_rate = ShareHolderLevel::findOne($level_id_3)->share_rate_three; if ($three_rate > 0) { $orderShare->old_holder_third_price = $orderShare->profit * doubleval($three_rate) / 100; } } } $res = $orderShare->save(); if (!$res) { \Yii::warning('=== 分销计算:订单id:' . $this->order->id . '分销订单生成失败 ==='); } \Yii::warning('=== 分销计算:订单id:' . $this->order->id . '分销订单生成成功 ==='); return $res; } public function getShareCommissionMoney($store_id, $orderDetail, $integral, $user_id, $type = 1) { $share = Share::findOne(['user_id' => $user_id, 'status' => Share::SHARE_AUDIT_PASS, 'is_delete' => Share::SHARE_NOT_DELETE]); $share_commission_money = 0; if ($share) { $share_level_id = $share->level; $share_level = ShareLevel::findOne(['level' => $share_level_id, 'store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON]); foreach ($orderDetail as $item) { if(ActivityRebateOrderN::passBaseRebate($store_id, $item['goods_id'], $this->order->parent_id)){ continue; } // TODO: 价格 $item_price = doubleval($item['price']); // $item_price = doubleval($orderShare->pay_price); $rate_first = 0; if ($item['individual_share'] == 1) { if ($type === 1) { $rate_first = $item['share_commission_new_first']['share_commission_level_' . $share_level_id]; } elseif ($type === 2) { $rate_first = $item['share_commission_new_second']['share_commission_level_' . $share_level_id]; } elseif ($type === 3) { $rate_first = $item['share_commission_new_third']['share_commission_level_' . $share_level_id]; } $shareType = $item['share_type']; } else { $setting_shara = Option::get('share_money_setting', $store_id, 'share'); $setting_shara = $setting_shara ? Json::decode($setting_shara['value']) : []; if ($type === 1) { $rate_first = $share_level->first_profit ?? ($setting_shara['level_one']['value'] ?? 0); } elseif ($type === 2) { $rate_first = $share_level->second_profit ?? ($setting_shara['level_two']['value'] ?? 0); } elseif ($type === 3) { $rate_first = $share_level->third_profit ?? ($setting_shara['level_three']['value'] ?? 0); } if ($share_level) { $shareType = intval($share_level->profit_type); } else { $shareType = 0; } } if ($shareType == 1) { // 金钱 if ($this->share_send_type == 2) { // 兑换比例 $share_commission_money += round($rate_first * $integral) * $item['num']; } else { $share_commission_money += $rate_first * $item['num']; } } else { // 比例 if ($this->share_send_type == 2) { $share_commission_money += round($item_price * ($rate_first / 100) * $integral); } else { $share_commission_money += $item_price * $rate_first / 100; } } } return sprintf("%.2f", $share_commission_money < 0.01 ? 0 : $share_commission_money); } return 0; } public function setScanCodePayData() { \Yii::error('==========================================='); $sotre_id = $this->store_id ? $this->store_id : get_store_id(); $setting = Option::get('share_money_setting', $sotre_id); $setting = $setting ? Json::decode($setting['value']) : []; if (!$setting) { \Yii::warning('未开启分销设置1'); return false; } // if (!is_array($setting['commission_type']) || empty($setting['commission_type'])) { // \Yii::warning('未开启分销设置2'); // return false; // } // 获取到当前到订单佣金发放类型 1: 佣金 2:积分, 3:余额 if (!empty($setting['giveType'])) { $this->share_send_type = $setting['giveType']; } // 获取积分和佣金的兑换比例 $integral = Option::get('integral', $this->order->store_id, 'store')['value']; if (!$integral && $this->share_send_type == 2) { \Yii::warning('发放积分佣金类型比例未设置'); return false; } // $commission_type = $setting['commission_type']; // 订单分销 2 // if (in_array(2, $commission_type) && $setting['scale'] > 0) { $this->order_share_rate = $setting['scale']; $this->setOrderProfit(); // } else { // \Yii::warning('未开启订单佣金分销'); // } // if (!in_array(1, $commission_type)) { // \Yii::warning('未开启会员佣金分销'); // return false; // } $user = User::findOne($this->order->user_id); if (!$user) { \Yii::warning('订单用户不存在'); return false; } /* @var $orderShare \app\plugins\scanCodePay\models\Order */ $orderShare = $this->order; $cParent1 = $user->parent_id; $orderShare->parent_id = $cParent1; if ($user->parent_id) { $parent = User::findOne($user->parent_id);//上级 $cParent2 = $parent->parent_id; $orderShare->parent_id_1 = $cParent2; if ($parent->parent_id) { $parent_1 = User::findOne($parent->parent_id);//上上级 $cParent3 = $parent_1->parent_id; $orderShare->parent_id_2 = $cParent3; } else { $orderShare->parent_id_2 = -1; } } else { $orderShare->parent_id_1 = -1; $orderShare->parent_id_2 = -1; } $res = $orderShare->save(); if (!$res) { \Yii::error('分销订单生成失败'); } \Yii::error('分销订单生成成功'); return $res; } /** * 普通订单 * @return array */ private function getOrderDetail() { /* @var $order Order */ $order = $this->order; /* @var $list OrderDetail */ if($this->yinbao){ $list = OrderPospalDetail::find()->where(['is_delete' => 0, 'order_id' => $order->id])->all(); }else{ $list = OrderDetail::find()->where(['is_delete' => 0, 'order_id' => $order->id])->all(); } $newList = []; foreach ($list as $value) { $goods = $value->goods; // 入住商商品未开启独立分销的时候 不能走平台的分销 因为分销佣金是入住商支付的 if($goods->mch_id > 0 && $goods->individual_share == 0){ continue; } $buyAttrList = Json::decode($value['attr']); $orderGoodsCancel = OrderGoodsCancel::find()->where(['order_id' => $this->order->id, 'order_detail_id' => $value->id, 'status' => [ OrderGoodsCancel::STATUS_PASS, OrderGoodsCancel::STATUS_PAY, OrderGoodsCancel::STATUS_PAY_FAIL ]])->groupBy('order_detail_id')->select('SUM(num) as num, SUM(refund_price) as refund_price')->asArray()->one(); $value['total_price'] = bcsub($value['total_price'], $orderGoodsCancel['refund_price'], 2); $value['num'] = bcsub($value['num'], $orderGoodsCancel['num']); if ($value['num'] <= 0) { continue; } if (intval($goods['attr_setting_type']) === 1) { $attrIdArr2 = []; foreach ($buyAttrList as $attrListItem2) { $attrIdArr2[] = $attrListItem2['attr_id']; } $goodsData = [ 'attr' => $goods['attr'], 'price' => $goods['price'], 'is_level' => $goods['is_level'], ]; $res = CommonGoods::currentGoodsAttr($goodsData, $attrIdArr2, [], 1, $order->user_id, $order->store_id); $newItem = [ 'individual_share' => $goods['individual_share'], 'share_commission_new_first' => $res['share_commission_new_first'], 'share_commission_new_second' => $res['share_commission_new_second'], 'share_commission_new_third' => $res['share_commission_new_third'], 'share_type' => $goods['share_type'], 'num' => $value['num'], 'price' => $value['total_price'], 'mch_id' => $goods['mch_id'] ]; } else { $newItem = [ 'individual_share' => $goods['individual_share'], 'share_commission_new_first' => $goods['share_commission_new_first'] ? json_decode($goods['share_commission_new_first'], true) : [], 'share_commission_new_second' => $goods['share_commission_new_second'] ? json_decode($goods['share_commission_new_second'], true) : [], 'share_commission_new_third' => $goods['share_commission_new_third'] ? json_decode($goods['share_commission_new_third'], true) : [], 'share_type' => $goods['share_type'], 'num' => $value['num'], 'price' => $value['total_price'], 'mch_id' => $goods['mch_id'] ]; } array_push($newList, array_merge($newItem, [ 'goods_id' => $value['goods_id'], ])); } return $newList; } /** * 订单佣金计算 */ private function setOrderProfit() { $price = $this->order->pay_price; $refund_price = OrderGoodsCancel::find()->where(['order_id' => $this->order->id, 'status' => [ OrderGoodsCancel::STATUS_PASS, OrderGoodsCancel::STATUS_PAY, OrderGoodsCancel::STATUS_PAY_FAIL ]])->sum('refund_price'); $price = bcsub($price, $refund_price, 2); if ($price <= 0) { \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算完成-可退款金额为0'); $this->order->share_order_profit = 0; if ($this->order->save()) { \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算完成'); return true; } return false; } $profit = 0; // 积分 if ($this->share_send_type == 2) { $profit = round($price * ($this->order_share_rate / 100) * $this->integral); } else { $profit = round($price * ($this->order_share_rate / 100), 2); } if ($profit < 0.01) { \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算为0'); return false; } $this->order->share_order_profit = $profit; $this->order->share_send_type = $this->share_send_type; if ($this->order->save()) { \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算完成'); return true; } \Yii::warning(['订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算失败', $this->order->errors]); return false; } /** * 临时绑定关系佣金计算 */ private function setLimitProfit($setting){ $price = $this->order->pay_price; $refund_price = OrderGoodsCancel::find()->where(['order_id' => $this->order->id, 'status' => [ OrderGoodsCancel::STATUS_PASS, OrderGoodsCancel::STATUS_PAY, OrderGoodsCancel::STATUS_PAY_FAIL ]])->sum('refund_price'); $price = bcsub($price, $refund_price, 2); if ($price <= 0) { return false; } $profit = $price * ($setting['scale']/100); //临时绑定关系发放佣金 $bind_log = UserBindLog::find()->where(['user_id' => $this->order->user_id])->select('id, bind_time, parent_id')->orderBy('id desc')->asArray()->one(); if ($bind_log) { if ($bind_log['parent_id']) { $share = Share::findOne(['user_id' => $bind_log['parent_id'], 'status' => 1, 'is_delete' => 0]); if (!$share) { return false; } } if ($bind_log['bind_time'] + ($setting['expire_day'] * 24 * 60 * 60) >= time()) { $this->order->limit_price = $profit; $this->order->limit_id = $bind_log['id']; if ($this->order->save()) { return true; } } } return false; } }