diff --git a/app/Admin/Controllers/DealerChannelSubsidyController.php b/app/Admin/Controllers/DealerChannelSubsidyController.php index fa9b2ecd..1bec7053 100644 --- a/app/Admin/Controllers/DealerChannelSubsidyController.php +++ b/app/Admin/Controllers/DealerChannelSubsidyController.php @@ -59,10 +59,6 @@ class DealerChannelSubsidyController extends AdminController return $this->settle_at?->toDateTimeString(); })->sortable(); $grid->column('status', '状态')->display(function ($v) { - if (! $this->isSettled()) { - return "  待结算"; - } - return "  {$v->text()}"; }); $grid->column('pay_at', '付款时间')->display(function () { @@ -72,50 +68,35 @@ class DealerChannelSubsidyController extends AdminController return $this->created_at?->toDateTimeString(); }); - $grid->showRowSelector(); + // $grid->showRowSelector(); + // $grid->tools(function ($tools) { + // $tools->batch(function ($batch) { + // $batch->disableDelete(); - $grid->tools(function ($tools) { - $tools->batch(function ($batch) { - $batch->disableDelete(); + // if (Admin::user()->can('dcat.admin.dealer_channel_subsidies.batch_pay')) { + // $batch->add(new DealerChannelSubsidyBatchPay()); + // } + // }); + // }); - if (Admin::user()->can('dcat.admin.dealer_channel_subsidies.batch_pay')) { - $batch->add(new DealerChannelSubsidyBatchPay()); - } - }); - }); - - $grid->actions(function (Grid\Displayers\Actions $actions) { - if ( - $actions->row->isSettled() && - $actions->row->isPending() && - Admin::user()->can('dcat.admin.dealer_channel_subsidies.pay') - ) { - $actions->append(new DealerChannelSubsidyPay()); - } - }); + $grid->disableActions(); + // $grid->actions(function (Grid\Displayers\Actions $actions) { + // if ( + // $actions->row->isSettled() && + // $actions->row->isPending() && + // Admin::user()->can('dcat.admin.dealer_channel_subsidies.pay') + // ) { + // $actions->append(new DealerChannelSubsidyPay()); + // } + // }); $grid->filter(function (Grid\Filter $filter) { $filter->panel(); $filter->equal('user.phone', '手机号')->width(3); - $filter->where('status', function ($query) { - switch ($this->input) { - case 'pending': - $query->whereNull('settle_at')->where('status', DealerEarningStatus::Pending); - break; - - case 'paying': - $query->whereNotNull('settle_at')->where('status', DealerEarningStatus::Pending); - break; - - case 'completed': - $query->where('status', DealerEarningStatus::Completed); - break; - } - }, '状态')->select([ - 'pending' => '待结算', - 'paying' => '待付款', - 'completed' => '已完成', + $filter->equal('status', '状态')->select([ + DealerEarningStatus::Pending->value => '待付款', + DealerEarningStatus::Completed->value => '已完成', ])->width(3); $filter->between('settle_at', '结算时间')->datetime()->width(6); }); diff --git a/app/Console/Commands/Dealer/ChannelSubsidySettleCommand.php b/app/Console/Commands/Dealer/ChannelSubsidySettleCommand.php new file mode 100644 index 00000000..0eac0b73 --- /dev/null +++ b/app/Console/Commands/Dealer/ChannelSubsidySettleCommand.php @@ -0,0 +1,192 @@ +withoutPayer()->onlyPending()->chunkById(200, function ($earnings) use (&$page) { + $earnings->load('dealer.userInfo'); + + foreach ($earnings as $earning) { + try { + DB::beginTransaction(); + + $earning->update([ + 'settle_at' => now(), + ]); + + (new DealerEarningService())->pay($earning); + + DB::commit(); + } catch (Throwable $e) { + DB::rollBack(); + + report($e); + } + } + + $page++; + }); + + if ($page === 0) { + sleep(60); + } elseif ($page === 1) { + sleep(30); + } else { + sleep(15); + } + } + } + + /** + * 处理经销商订单 + * + * @param \App\Models\DealerOrder $order + * @return void + */ + protected function handleDealerOrder(DealerOrder $order) + { + $this->handleManagerSalesLogs($order); + + $this->handleManageSubsidyLogs($order); + + $this->handleChannelSubsidyLogs($order); + + $this->handlePurchaseLogs($order); + + $this->handleOrder($order); + } + + protected function handleManagerSalesLogs(DealerOrder $order) + { + DealerManagerSalesLog::where('order_id', $order->id)->update([ + 'order_completed_at' => $order->shippinged_time, + ]); + } + + protected function handleManageSubsidyLogs(DealerOrder $order) + { + DealerManageSubsidyLog::where('order_id', $order->id)->update([ + 'order_completed_at' => $order->shippinged_time, + ]); + } + + protected function handleChannelSubsidyLogs(DealerOrder $order) + { + $channelSubsidyLogIds = DealerChannelSubsidyLog::where('order_id', $order->id)->get('id')->toArray(); + + DealerChannelSubsidyLog::whereIn('id', $channelSubsidyLogIds)->update([ + 'order_completed_at' => $order->shippinged_time, + ]); + + if ($order->consignor_id !== null) { + DealerEarning::where('earningable_type', 'dealer_channel_subsidy_log')->whereIn('earningable_id', $channelSubsidyLogIds)->update([ + 'settle_at' => now(), + ]); + } + } + + protected function handlePurchaseLogs(DealerOrder $order) + { + DealerPurchaseLog::where('order_id', $order->id)->update([ + 'order_completed_at' => $order->shippinged_time, + ]); + } + + protected function handleOrder(DealerOrder $order) + { + $salesValue = $order->total_amount; + + if (bccomp($salesValue, '0', 2) < 1) { + return; + } + + $salesValueLogs = []; + + if (bccomp($salesValue, '0', 2) === 1) { + $ts = now()->toDateTimeString(); + + $order->dealer->update([ + 'self_sales_value' => DB::raw("self_sales_value+{$salesValue}"), + ]); + + $salesValueLogs[] = [ + 'user_id' => $order->user_id, + 'loggable_type' => $order->getMorphClass(), + 'loggable_id' => $order->id, + 'type' => DealerSalesValueLogType::Personal, + 'change_sales_value' => $salesValue, + 'remark' => '个人进货', + 'created_at' => $ts, + 'updated_at' => $ts, + ]; + + if (count($pids = $order->dealer->userInfo->parent_ids) > 0) { + foreach ($order->dealer->userInfo->parent_ids as $pid) { + $salesValueLogs[] = [ + 'user_id' => $pid, + 'loggable_type' => $order->getMorphClass(), + 'loggable_id' => $order->id, + 'type' => DealerSalesValueLogType::Team, + 'change_sales_value' => $salesValue, + 'remarks' => '团队成员进货', + 'created_at' => $ts, + 'updated_at' => $ts, + ]; + } + + // 更新上级的团队团队业绩 + Dealer::whereIn('user_id', $pids)->update([ + 'team_sales_value' => DB::raw("team_sales_value + {$salesValue}"), + ]); + } + } + + // 保存销售值日志 + DealerSalesValueLog::insert($salesValueLogs); + + // 将订单标记未已结算 + $order->forceFill([ + 'settle_state' => DealerOrderSettleState::Completed, + ])->save(); + } +} diff --git a/app/Console/Commands/Dealer/OrderSettleCommand.php b/app/Console/Commands/Dealer/OrderSettleCommand.php index 709d4cf8..7e3a46f8 100644 --- a/app/Console/Commands/Dealer/OrderSettleCommand.php +++ b/app/Console/Commands/Dealer/OrderSettleCommand.php @@ -117,9 +117,11 @@ class OrderSettleCommand extends Command 'order_completed_at' => $order->shippinged_time, ]); - DealerEarning::where('earningable_type', 'dealer_channel_subsidy_log')->whereIn('earningable_id', $channelSubsidyLogIds)->update([ - 'settle_at' => now(), - ]); + if ($order->consignor_id !== null) { + DealerEarning::where('earningable_type', 'dealer_channel_subsidy_log')->whereIn('earningable_id', $channelSubsidyLogIds)->update([ + 'settle_at' => now(), + ]); + } } protected function handlePurchaseLogs(DealerOrder $order)