belongsTo('app\common\model\user\User'); } /** * 一级分销商用户 * @return \think\model\relation\BelongsTo */ public function agentFirst() { return $this->belongsTo('app\common\model\user\User', 'first_user_id'); } /** * 二级分销商用户 * @return \think\model\relation\BelongsTo */ public function agentSecond() { return $this->belongsTo('app\common\model\user\User', 'second_user_id'); } /** * 三级分销商用户 * @return \think\model\relation\BelongsTo */ public function agentThird() { return $this->belongsTo('app\common\model\user\User', 'third_user_id'); } /** * 订单类型 * @param $value * @return array */ public function getOrderTypeAttr($value) { $types = OrderTypeEnum::getTypeName(); return ['text' => $types[$value], 'value' => $value]; } /** * 订单详情 * @param $orderId * @param $orderType * @return array|\think\Model|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getDetailByOrderId($orderId, $orderType) { return (new static())->where('order_id', '=', $orderId) ->where('order_type', '=', $orderType) ->find(); } /** * 发放分销订单佣金 * @param $order * @param int $orderType * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function grantMoney($order, $orderType = OrderTypeEnum::MASTER) { // 订单是否已完成 if ($order['order_status']['value'] != 30) { return false; } // 分销订单详情 $model = self::getDetailByOrderId($order['order_id'], $orderType); if (!$model || $model['is_settled'] == 1) { return false; } // 佣金结算天数 $settleDays = Setting::getItem('settlement', $order['app_id'])['settle_days']; // 写入结算时间 $deadlineTime = $model['settle_end_time']; if($deadlineTime == 0){ $deadlineTime = $order['receipt_time'] + $settleDays * 86400; $model->save([ 'settle_end_time' => $deadlineTime ]); } if ($deadlineTime > time()) { return false; } // 重新计算分销佣金 $capital = $model->getCapitalByOrder($order); // 发放一级分销商佣金 $model['first_user_id'] > 0 && User::grantMoney($model['first_user_id'], $capital['first_money']); // 发放二级分销商佣金 $model['second_user_id'] > 0 && User::grantMoney($model['second_user_id'], $capital['second_money']); // 发放三级分销商佣金 $model['third_user_id'] > 0 && User::grantMoney($model['third_user_id'], $capital['third_money']); // 更新分销订单记录 $model->save([ 'order_price' => $capital['orderPrice'], 'first_money' => $model['first_user_id'] > 0? $capital['first_money']:0, 'second_money' => $model['second_user_id'] > 0? $capital['second_money']:0, 'third_money' => $model['third_user_id'] > 0? $capital['third_money']:0, 'is_settled' => 1, 'settle_time' => time() ]); event('AgentUserGrade', $model['first_user_id']); event('AgentUserGrade', $model['second_user_id']); event('AgentUserGrade', $model['third_user_id']); return true; } /** * 计算订单分销佣金 * @param $order * @return array */ protected function getCapitalByOrder($order, $source = 'create') { // 分销佣金设置 $setting = Setting::getItem('commission', $order['app_id']); // 分销层级 $level = Setting::getItem('basic', $order['app_id'])['level']; // 分销订单佣金数据 $capital = [ // 订单总金额(不含运费) 'orderPrice' => bcsub($order['pay_price'], $order['express_price'], 2), // 一级分销佣金 'first_money' => 0.00, // 二级分销佣金 'second_money' => 0.00, // 三级分销佣金 'third_money' => 0.00, // 是否记录 'is_record' => true ]; $total_count = count($order['product']); // 计算分销佣金 foreach ($order['product'] as $product) { // 判断商品是否开放分销 if(ProductModel::detail($product['product_id'])['is_agent'] == 0 && $source == 'create'){ $total_count--; continue; } // 判断商品存在售后退款则不计算佣金 if ($this->checkProductRefund($product)) { continue; } // 商品实付款金额 $productPrice = min($capital['orderPrice'], $product['total_pay_price']); // 计算商品实际佣金 $productCapital = $this->calculateProductCapital($setting, $product, $productPrice); // 累积分销佣金 $level >= 1 && $capital['first_money'] += $productCapital['first_money']; $level >= 2 && $capital['second_money'] += $productCapital['second_money']; $level == 3 && $capital['third_money'] += $productCapital['third_money']; } if($total_count == 0){ $capital['is_record'] = false; } return $capital; } /** * 计算商品实际佣金 * @param $setting * @param $product * @param $productPrice * @return float[]|int[] */ private function calculateProductCapital($setting, $product, $productPrice) { $first_user = AgentUserModel::detail($this['first_user_id']); $second_user = AgentUserModel::detail($this['second_user_id']); $third_user = AgentUserModel::detail($this['third_user_id']); $add_first_money = $productPrice * ($first_user['first_percent'] * 0.01); $add_second_money = $productPrice * ($second_user['second_percent'] * 0.01); $add_third_money = $productPrice * ($third_user['third_percent'] * 0.01); $agent_product = AgentProductModel::detail($product['product_id']); // 全局分销 if ($agent_product['is_ind_agent'] == false) { // 全局分销比例 return [ 'first_money' => $productPrice * ($setting['first_money'] * 0.01) + $add_first_money, 'second_money' => $productPrice * ($setting['second_money'] * 0.01) + $add_second_money, 'third_money' => $productPrice * ($setting['third_money'] * 0.01) + $add_third_money ]; } // 商品单独分销 if ($agent_product['agent_money_type'] == 10) { // 分销佣金类型:百分比 return [ 'first_money' => $productPrice * ($agent_product['first_money'] * 0.01) + $add_first_money, 'second_money' => $productPrice * ($agent_product['second_money'] * 0.01) + $add_second_money, 'third_money' => $productPrice * ($agent_product['third_money'] * 0.01) + $add_third_money ]; } else { return [ 'first_money' => $product['total_num'] * $agent_product['first_money'] + $add_first_money, 'second_money' => $product['total_num'] * $agent_product['second_money'] + $add_second_money, 'third_money' => $product['total_num'] * $agent_product['third_money'] + $add_third_money ]; } } /** * 验证商品是否存在售后 * @param $product * @return bool */ private function checkProductRefund($product) { return !empty($product['refund']) && $product['refund']['type']['value'] != 20 && $product['refund']['is_agree']['value'] != 20; } }