belongsTo('app\common\model\user\User'); } /** * 股东用户 * @return \think\model\relation\BelongsTo */ public function shareholderHeads() { return $this->belongsTo('app\common\model\user\User', 'heads_id'); } /** * 一级股东用户 * @return \think\model\relation\BelongsTo */ public function shareholderFirst() { return $this->belongsTo('app\common\model\user\User', 'first_user_id'); } /** * 二级股东用户 * @return \think\model\relation\BelongsTo */ public function shareholderSecond() { return $this->belongsTo('app\common\model\user\User', 'second_user_id'); } /** * 三级股东用户 * @return \think\model\relation\BelongsTo */ public function shareholderThird() { 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 $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 orderFirst() { return (new static())->where('is_settled', '=', 0)->order(['create_time' => 'asc'])->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('bonus_type', '=', $bonusType) ->sum('order_price'); } /** * 创建分红订单记录 */ public static function createOrder($order, $order_type = OrderTypeEnum::MASTER) { // 订单是否已完成 if ($order['order_status']['value'] != 30) { return false; } // 分红订单模型 $model = new self; // 股东基本设置 $setting = Setting::getItem('basic', $order['app_id']); // 是否开启分红功能 if (!$setting['is_open']) { return false; } // 如果没有股东,不记录订单 if(User::shareholderCount() <= 0) { return false; } // 重新整理订单 $capital = $model->getCapitalByOrder($order); return $model->save([ 'user_id' => $order['user_id'], 'order_id' => $order['order_id'], 'order_type' => $order_type, 'order_price' => $capital['orderPrice'], 'is_settled' => 0, 'shop_supplier_id' => $order['shop_supplier_id'], 'app_id' => $order['app_id'] ]); } /** * 计算订单金额 * @param $order * @return array */ protected function getCapitalByOrder($order) { // 分红设置 $setting = Setting::getItem('basic', $order['app_id']); $capital = [ // 订单总金额(不含运费) 'orderPrice' => bcsub($order['pay_price'], $order['express_price'], 2), // 是否记录 'is_record' => true ]; $total_count = count($order['product']); $productPriceTotal = 0; // 计算分红 foreach ($order['product'] as $product) { // 判断商品存在售后退款则不计算佣金 if ($this->checkProductRefund($product)) { continue; } // 商品实付款金额 $productPriceTotal += min($capital['orderPrice'], $product['total_pay_price']); } if($total_count == 0){ $capital['is_record'] = false; } return $capital; } /** * 验证商品是否存在售后 * @param $product * @return bool */ private function checkProductRefund($product) { return !empty($product['refund']) && $product['refund']['type']['value'] != 20 && $product['refund']['is_agree']['value'] != 20; } }