<?php namespace app\common\model\plus\team; use app\common\model\BaseModel; use app\common\enum\order\OrderTypeEnum; use app\common\model\plus\shareholder\Apply as ShareholderApplyModel; /** * 队长订单模型 */ class Order extends BaseModel { protected $name = 'team_order'; protected $pk = 'id'; /** * 订单所属用户 * @return \think\model\relation\BelongsTo */ public function user() { return $this->belongsTo('app\common\model\user\User'); } /** * 队长用户 * @return \think\model\relation\BelongsTo */ public function teamHeads() { return $this->belongsTo('app\common\model\user\User', 'heads_id'); } /** * 订单类型 * @param $value * @return array */ public function getOrderTypeAttr($value) { $types = OrderTypeEnum::getTypeName(); return ['text' => $types[$value], 'value' => $value]; } /** * 分红类型 * @param $value * @return array */ public function getBonusTypeAttr($value) { $bonus_type = ['10' => '分红', '20' => '级差奖', '30' => '平级奖']; return ['text' => $bonus_type[$value], 'value' => $value]; } /** * 订单详情 */ public static function orderDetail($id) { return (new static())->where('id', '=', $id)->find(); } /** * 订单详情 */ public static function getDetailByOrderId($orderId, $orderType) { return (new static())->where('order_id', '=', $orderId) ->where('order_type', '=', $orderType) ->select(); } // 统计队长团队业绩 public static function getOrderAllPrice($userId, $bonusType = 10) { return (new static())->where('heads_id', '=', $userId) //->where('is_settled', '=', 1) ->where('bonus_type', '=', $bonusType) ->sum('order_price'); } /** * 发放订单分红 * @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 = new static; // 分红订单详情 $teamOrderList = self::getDetailByOrderId($order['order_id'], $orderType); if(!$teamOrderList) { return false; } // 佣金结算天数 $settleDays = Setting::getItem('settlement', $order['app_id'])['settle_days']; $teamUser = []; // 写入结算时间 foreach ($teamOrderList as $orderItem) { $deadlineTime = $orderItem['settle_end_time']; if($deadlineTime == 0){ $deadlineTime = $order['receipt_time'] + $settleDays * 86400; $orderModel = self::orderDetail($orderItem['id']); $orderModel->save([ 'settle_end_time' => $deadlineTime ]); } $teamUser[] = [ 'heads_id' => $orderItem['heads_id'], 'bonus_type' => $orderItem['bonus_type'], 'bonus_percent' => $orderItem['bonus_percent'] //分红比例 ]; } // 判断该订单是否满足结算时间 (订单完成时间 + 佣金结算时间) ≤ 当前时间 $deadlineTime = $order['receipt_time'] + ((int)$settleDays * 86400); if ($settleDays > 0 && $deadlineTime > time()) { return false; } // 重新计算分红 $capital = $model->getCapitalByOrder($order, $teamUser); foreach ($teamOrderList as $item) { if ($item['is_settled'] == 1) { continue; } // 发放队长分红 $item['heads_id'] > 0 && User::grantMoney($item['heads_id'], $capital['user'][$item['heads_id']], $item['bonus_type']); $teamOrder = self::orderDetail($item['id']); $teamOrder->save([ 'is_settled' => 1, 'order_price' => $capital['orderPrice'], 'bonus_money' => $capital['user'][$item['heads_id']], 'settle_time' => time() ]); // 更新队长等级 if ($item['bonus_type']['value'] == 10) { // 根据分红订单判断股东 by yj $shareholderModel = new ShareholderApplyModel; $shareholderModel->becomeShareholderByTeam($item['user_id'], 70, $order['app_id']); if ($item['heads_id']) { $shareholderModel->becomeShareholderByTeam($item['heads_id'], 70, $order['app_id']); event('TeamUserGrade', $item['heads_id']); } // 自己的订单也检查更新等级 event('TeamUserGrade', $item['user_id']); } } return true; } /** * 计算订单分销佣金 * @param $order * @return array */ protected function getCapitalByOrder($order, $teamUser, $source = 'create') { // 分红设置 $setting = Setting::getItem('basic', $order['app_id']); $capital = [ // 订单总金额(不含运费) // 订单总金额(不含运费) 减去 结算价 = 订单利润 'orderPrice' => bcsub(bcsub($order['pay_price'], $order['express_price'], 2), $order['settlement_price'], 2), // 是否记录 'is_record' => true, 'user' => [] ]; $total_count = count($order['product']); $productPriceTotal = 0; // 计算分红 foreach ($order['product'] as $product) { // 判断商品存在售后退款则不计算佣金 if ($this->checkProductRefund($product)) { continue; } //log_write('is_enable_team:'.$product['is_enable_team']); if (empty($product['is_enable_team'])) { continue; } // 商品实付款金额 $productPriceTotal += min($capital['orderPrice'], $product['total_pay_price']); } foreach ($teamUser as $user) { $capital['user'][$user['heads_id']] = $productPriceTotal * $user['bonus_percent'] * 0.01; } 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) { // 分红佣金类型:百分比 return [ 'first_money' => $productPrice, 'second_money' => $productPrice, 'third_money' => $productPrice ]; } /** * 验证商品是否存在售后 * @param $product * @return bool */ private function checkProductRefund($product) { return !empty($product['refund']) && $product['refund']['type']['value'] != 20 && $product['refund']['is_agree']['value'] != 20; } }
|