178 lines
5.1 KiB
PHP
178 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Dealer;
|
|
|
|
use App\Enums\DealerOrderSettleState;
|
|
use App\Enums\DealerOrderStatus;
|
|
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 OrderSettleCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'dealer:order-settle';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '结算已完成的订单';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
DealerOrder::where([
|
|
'status' => DealerOrderStatus::Completed,
|
|
'settle_state' => DealerOrderSettleState::Processed,
|
|
])->whereNotNull('shippinged_time')->chunkById(200, function ($orders) {
|
|
$orders->load('dealer.userInfo');
|
|
|
|
foreach ($orders as $order) {
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$this->handleDealerOrder($order);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $e) {
|
|
DB::rollBack();
|
|
|
|
report($e);
|
|
}
|
|
}
|
|
});
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* 处理经销商订单
|
|
*
|
|
* @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,
|
|
]);
|
|
|
|
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,
|
|
'remarks' => '个人进货',
|
|
'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();
|
|
}
|
|
}
|