setName('settle_order') ->setDescription('结算订单'); } protected function execute(Input $input, Output $output) { try { $settleSettingConfig = [ //结算方式(包含车费) 'commission_settle' => ConfigService::get('settle_setting', 'commission_settle'), //结算周期 'commission_settle_cycle' => ConfigService::get('settle_setting', 'commission_settle_cycle'), //结算类型 'commission_settle_cycle_type' => ConfigService::get('settle_setting', 'commission_settle_cycle_type'), //结算天 'commission_settle_cycle_day' => ConfigService::get('settle_setting', 'commission_settle_cycle_day'), ]; $whereRaw = '((order_status = '.OrderEnum::ORDER_STATUS_SERVER_FINISH.') or (order_status = '.OrderEnum::ORDER_STATUS_CLOSE.' and total_order_amount > total_refund_amount and pay_status = '.PayEnum::ISPAID.')) and is_settle = 0'; $now = time(); //结算周期:1-按状态;2-周期 if(1 == $settleSettingConfig['commission_settle_cycle']){ //订单结束后X天 $overTime = $settleSettingConfig['commission_settle_cycle_day'] * 60 * 60 * 24; $whereRaw .= ' and true_server_finish_time + '.$overTime.' < '.$now; }else{ //按周期:1-每周、2-每月 if( 1== $settleSettingConfig['commission_settle_cycle_type']){ $date = date('N'); }else{ $date = date('j'); } if($date != $settleSettingConfig['commission_settle_cycle_day']){ return true; } } $lists = Order::whereRaw($whereRaw) ->append(['order_goods']) ->select() ->toArray(); if(empty($lists)){ return true; } $settleData = [ 'total_order_amount' => 0, 'total_car_amount' => 0, 'total_gap_amount' => 0, 'total_append_amount' => 0, 'total_commission' => 0, 'total_car_commission' => 0, 'total_car_coach_commission'=> 0, 'total_car_shop_commission' => 0, 'total_coach_commission' => 0, 'total_shop_commission' => 0, 'settle_num' => 0, 'settle_config' => $settleSettingConfig, ]; $settleOrderData = []; Db::startTrans(); $settle = Settle::create($settleData); foreach ($lists as $order){ $goods = $order['order_goods'][0]->toArray(); $commissionRatio = $goods['goods_snap']['commission_ratio'] ?? 0; $shopRatio = $goods['goods_snap']['shop_ratio'] ?? 0; $totalAmount = $order['total_order_amount']; $refundAmount = round($order['total_refund_amount'] - $order['refund_car_amount'],2); $totalAmount = round($totalAmount-$refundAmount-$order['car_amount'],2); $orderCarAmount = round($order['car_amount'] - $order['refund_car_amount'],2); $coachCarAmount = 0; $shopCarAmount = 0; $shopCommission = 0; $coachCommission = 0; //用于判断是否商家商品 $goodsShopId = $goods['goods_snap']['shop_id'] ?? $order['shop_id']; if($goodsShopId){ $shopRatio = round(100 - $commissionRatio,2); } //技师佣金 if($totalAmount > 0 && $commissionRatio > 0){ $coachCommission = round($totalAmount * ($commissionRatio / 100),2); } //商家佣金 if($totalAmount > 0 && $shopRatio > 0 && $order['shop_id']){ $shopCommission = round($totalAmount * ($shopRatio / 100),2); } //如果是包含车费 if(1 == $settleSettingConfig['commission_settle']){ if($orderCarAmount > 0){ if($commissionRatio > 0){ $coachCarAmount = round($orderCarAmount * ($commissionRatio / 100),2); } //商家车佣金 if($shopRatio > 0 && $order['shop_id']){ $shopCarAmount = round($orderCarAmount * ($shopRatio / 100),2); } } // }else{ $coachCarAmount = $orderCarAmount; // $coachCommission = round($coachCommission+$coachCarAmount,2); } if($coachCommission > 0){ Coach::where(['id'=>$order['coach_id']])->update([ 'money'=> Db::raw('money+'.$coachCommission) ]); CoachAccountLogLogic::add( $order['coach_id'], CoachAccountLogEnum::MONEY, CoachAccountLogEnum::ORDER_ADD_MONEY, 1, $coachCommission, $order['sn'] ); } //车费技师 if($coachCarAmount > 0){ Coach::where(['id'=>$order['coach_id']])->update([ 'money'=> Db::raw('money+'.$coachCarAmount) ]); CoachAccountLogLogic::add( $order['coach_id'], CoachAccountLogEnum::MONEY, CoachAccountLogEnum::ORDER_ADD_CART_MONEY, 1, $coachCarAmount, $order['sn'] ); } if($shopCommission > 0){ Shop::where(['id'=>$order['shop_id']])->update([ 'money'=> Db::raw('money+'.$shopCommission) ]); ShopAccountLogLogic::add( $order['shop_id'], ShopAccountLogEnum::MONEY, ShopAccountLogEnum::ORDER_ADD_MONEY, 1, $shopCommission, $order['sn'] ); } //结算商家车费 if($shopCarAmount > 0){ Shop::where(['id'=>$order['shop_id']])->update([ 'money'=> Db::raw('money+'.$shopCarAmount) ]); ShopAccountLogLogic::add( $order['shop_id'], ShopAccountLogEnum::MONEY, ShopAccountLogEnum::ORDER_ADD_CART_MONEY, 1, $shopCarAmount, $order['sn'] ); } $coachCommission = round($coachCommission+$coachCarAmount,2); $shopCommission = round($shopCommission+$shopCarAmount,2); $totalCommissionAmount = round($coachCommission+$shopCommission,2); $totalCarCommissionAmount = round($coachCarAmount+$shopCarAmount,2); //标记已结算 Order::where(['id'=>$order['id']])->update([ 'is_settle' => 1, 'settle_coach_amount' => $coachCommission, 'settle_shop_amount' => $shopCommission, 'settle_commission_amount' => $totalCommissionAmount, 'settle_coach_car_amount' => $coachCarAmount, 'settle_shop_car_amount' => $shopCarAmount, 'settle_car_commission_amount' => $totalCarCommissionAmount, 'settle_total_commission_amount' => round($totalCommissionAmount+$totalCarCommissionAmount,2), ]); (new OrderLogLogic()) ->record(OrderLogEnum::TYPE_SYSTEM,OrderLogEnum::SYSTEM_SETTLEMENT_ORDER,$order['id']); //结算订单表 $settleOrderData[] = [ 'order_id' => $order['id'], 'settle_id' => $settle['id'], //订单总金额 'total_order_amount'=> $order['total_order_amount'], 'order_amount' => $totalAmount, //订单车费 'car_amount' => $orderCarAmount, //技师车费佣金 'coach_car_amount' => $coachCarAmount, //商家车费佣金 'shop_car_amount' => $shopCarAmount, //总车费佣金 'total_car_amount' => round($coachCarAmount+$shopCarAmount,2), 'gap_amount' => $order['total_gap_amount'], 'append_amount' => $order['total_append_amount'], //技师佣金 'coach_commission' => $coachCommission, //商家佣金 'shop_commission' => $shopCommission, //总佣金 'total_commission' => $totalCommissionAmount, //技师佣金比例和商家佣金比例 'commission_ratio' => $goods['goods_snap']['commission_ratio'] ?? 0, 'shop_ratio' => $goods['goods_snap']['shop_ratio'] ?? 0, ]; //结算表 $settleData['total_car_amount'] += $order['car_amount']; $settleData['total_gap_amount'] += $order['total_gap_amount']; $settleData['total_append_amount'] += $order['total_append_amount']; $settleData['total_order_amount'] += $order['total_order_amount']; //总技师和商家佣金,总佣金 $settleData['total_coach_commission'] += $coachCommission; $settleData['total_shop_commission'] += $shopCommission; $settleData['total_commission'] += $totalCommissionAmount; //总技师和商家车费佣金,总佣金 $settleData['total_car_coach_commission'] += $coachCarAmount; $settleData['total_car_shop_commission'] += $shopCarAmount; $settleData['total_car_commission'] += $totalCarCommissionAmount; $settleData['settle_num']++; } Settle::where(['id'=>$settle->id])->update($settleData); (new \app\common\model\settle\SettleOrder())->saveAll($settleOrderData); Db::commit(); }catch (Exception $e){ Db::rollback(); Log::write('结算订单计划任务执行失败:'.implode('-', [ __CLASS__, __FUNCTION__, $e->getFile(), $e->getLine(), $e->getMessage() ])); } } }