6
0
Fork 0

签约经销商的渠道补贴自动结算

release
李静 2022-03-28 10:44:09 +08:00
parent a08bdab76c
commit c8da24009e
3 changed files with 219 additions and 44 deletions

View File

@ -59,10 +59,6 @@ class DealerChannelSubsidyController extends AdminController
return $this->settle_at?->toDateTimeString();
})->sortable();
$grid->column('status', '状态')->display(function ($v) {
if (! $this->isSettled()) {
return "<i class='fa fa-circle' style='font-size: 13px;color: #b9c3cd'></i>&nbsp;&nbsp;待结算";
}
return "<i class='fa fa-circle' style='font-size: 13px;color: {$v->color()}'></i>&nbsp;&nbsp;{$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);
});

View File

@ -0,0 +1,192 @@
<?php
namespace App\Console\Commands\Dealer;
use App\Admin\Services\DealerEarningService;
use App\Enums\DealerOrderSettleState;
use App\Enums\DealerSalesValueLogType;
use App\Models\Dealer;
use App\Models\DealerChannelSubsidyLog;
use App\Models\DealerEarning;
use App\Models\DealerManagerSalesLog;
use App\Models\DealerManageSubsidyLog;
use App\Models\DealerOrder;
use App\Models\DealerPurchaseLog;
use App\Models\DealerSalesValueLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class ChannelSubsidySettleCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dealer:channel-subsidy-settle';
/**
* The console command description.
*
* @var string
*/
protected $description = '结算签约经销商的渠道补贴';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
while (true) {
$page = 0;
DealerEarning::channelSubsidy()->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();
}
}

View File

@ -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)