94 lines
7.2 KiB
PHP
94 lines
7.2 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Support\CourierBills\CourierBillMatcher;
|
||
use App\Support\CourierBills\SurchargeSettlementMatcher;
|
||
use App\Support\CourierBills\Rebooks;
|
||
use App\Support\DropshipFreight\DropshipConfirmation;
|
||
use App\Support\DropshipFreight\DropshipFreightStore;
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Throwable;
|
||
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
|
||
|
||
class MatchBillSurcharges extends Command
|
||
{
|
||
public const AUTO_MATCH_HOLDS = 'bill-surcharge-auto-match-holds';
|
||
|
||
protected $signature='courier-surcharge:match-bills {--month= : 账单月份 YYYY-MM,默认上月} {--all-months : 刷新全部已有账期,需同时指定 --all --existing-only} {--refresh-rebooks : 同时刷新未确认账单的补记账汇总} {--statement=* : 指定快递对账单} {--factory=* : 指定代发厂家} {--all : 匹配该月全部未确认账单} {--existing-only : 只更新已建立附加费匹配的账单} {--company=1}';
|
||
protected $description='匹配指定月份账单的附加费金额及商品信息,已确认账单不变';
|
||
|
||
public function handle(SurchargeSettlementMatcher $matcher): int
|
||
{
|
||
\App\Models\Common\Company::findOrFail((int) $this->option('company'))->makeCurrent();
|
||
if ($this->option('all-months')) {
|
||
if (! $this->option('all') || ! $this->option('existing-only') || $this->option('month')) {
|
||
$this->error('--all-months 需要 --all --existing-only,且不能指定 --month');
|
||
return self::FAILURE;
|
||
}
|
||
$months = DB::connection('business_mirror')->table(SurchargeSettlementMatcher::TABLE)->select('bill_month')
|
||
->union(DB::connection('business_mirror')->table('courier_bill_statements')->select('bill_month')->where('status', '<>', 'confirmed'))
|
||
->orderBy('bill_month')->pluck('bill_month');
|
||
foreach ($months as $month) {
|
||
$status = $this->call(self::class, ['--month' => $month, '--all' => true,
|
||
'--existing-only' => true, '--refresh-rebooks' => (bool) $this->option('refresh-rebooks'), '--company' => $this->option('company')]);
|
||
if ($status !== self::SUCCESS) return $status;
|
||
}
|
||
return self::SUCCESS;
|
||
}
|
||
$month=(string)($this->option('month')?:now('Asia/Shanghai')->subMonthNoOverflow()->format('Y-m'));
|
||
if(!preg_match('/^\d{4}-(0[1-9]|1[0-2])$/D',$month)){$this->error('请填写有效账单月份 YYYY-MM');return self::FAILURE;}
|
||
if(!$this->option('all')&&$this->option('statement')===[]&&$this->option('factory')===[]){$this->error('请选择对账单、厂家或 --all');return self::FAILURE;}
|
||
if(app()->environment('production')&&!env('FINANCE_BATCH_CONTAINER')){$this->error('请通过 run-finance-batch.sh 执行。');return self::FAILURE;}
|
||
$lock=Cache::lock('bill-surcharge-matching',3900);if(!$lock->get()){$this->error('附加费匹配正在运行');return self::FAILURE;}
|
||
$started = microtime(true);
|
||
try{
|
||
$db=DB::connection('business_mirror');$statements=[];$factories=[];
|
||
$holds=[];
|
||
if($this->option('existing-only'))foreach(app(SupplierPayableOverrideStore::class)->listRows(self::AUTO_MATCH_HOLDS,(int)$this->option('company')) as $hold){
|
||
if(($hold['enabled']??false)===true)$holds[$hold['_action_key']]=true;
|
||
}
|
||
if($this->option('all')||$this->option('statement')!==[]){
|
||
$statements=$db->table('courier_bill_statements')->where('bill_month',$month)->where('status','<>','confirmed')
|
||
->when($this->option('existing-only'),fn($q)=>$q->whereIn('id',$db->table(SurchargeSettlementMatcher::TABLE)->where('scope','courier')->where('bill_month',$month)->select('owner_id')))
|
||
->when(!$this->option('all'),fn($q)=>$q->whereIn('id',array_map('intval',$this->option('statement'))))->orderBy('id')->pluck('id')->all();
|
||
foreach($statements as $id){app(CourierBillMatcher::class)->refreshSurchargeSummary((int)$id);$this->line('快递对账单 #'.$id.' 附加费已匹配');}
|
||
}
|
||
if($this->option('all')||$this->option('factory')!==[]){
|
||
$store=app(DropshipFreightStore::class);
|
||
$factories=$db->table('dropship_bill_files')->where('bill_month',$month)->where('status','parsed')->whereNotNull('factory_id')
|
||
->when($this->option('existing-only'),fn($q)=>$q->whereIn('factory_id',$db->table('dropship_bill_surcharges')->where('bill_month',$month)->select('factory_id')))
|
||
->when(!$this->option('all'),fn($q)=>$q->whereIn('factory_id',array_map('intval',$this->option('factory'))))->distinct()->pluck('factory_id')->all();
|
||
foreach($factories as $id){
|
||
if(isset($holds['dropship|'.$id.'|'.$month])){$this->line('厂家 #'.$id.' 已保留当前附加费,跳过自动重匹配');continue;}
|
||
$state=app(DropshipConfirmation::class)->state($month,(int)$id,(int)$this->option('company'));
|
||
if(($state['status']??'')==='confirmed'){$this->line('厂家 #'.$id.' 已确认,跳过');continue;}
|
||
$db->transaction(function()use($db,$store,$id,$month){foreach($db->table('dropship_bill_files')->where('factory_id',$id)->where('bill_month',$month)->where('status','parsed')->get() as $file)$store->storeSurchargeRows($file,(int)$id);});
|
||
$matcher->dropship((int)$id,$month);$this->line('代发厂家 #'.$id.' 附加费已匹配');
|
||
}
|
||
}
|
||
if ($this->option('refresh-rebooks')) {
|
||
foreach ($db->table('courier_bill_statements')->where('bill_month', $month)->where('status', '<>', 'confirmed')
|
||
->when(! $this->option('all'), fn ($q) => $q->whereIn('id', array_map('intval', $this->option('statement'))))->orderBy('id')->pluck('id') as $id) {
|
||
$db->transaction(function () use ($db, $id): void {
|
||
$statement = $db->table('courier_bill_statements')->where('id', $id)->lockForUpdate()->first();
|
||
if (! $statement || $statement->status === 'confirmed') return;
|
||
$items = $db->table('courier_bill_summary_items')->where('statement_id', $id)->get();
|
||
foreach (app(Rebooks::class)->summaryItems((int) $id, $items) as $item) {
|
||
if (! $item->manual && Rebooks::isRebookLabel($item->label)) {
|
||
$db->table('courier_bill_summary_items')->where('id', $item->id)->update(['self_amount' => $item->self_amount]);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
}
|
||
$this->info('附加费匹配完成:'.$month.';快递对账单 '.count($statements).' 张,代发厂家 '.count($factories).' 家。');
|
||
$this->line(sprintf('耗时 %.2f 秒,PHP 峰值 %.2f MiB。', microtime(true) - $started, memory_get_peak_usage(true) / 1048576));
|
||
return self::SUCCESS;
|
||
}catch(Throwable $e){report($e);$this->error('附加费匹配失败:'.$e->getMessage());return self::FAILURE;}
|
||
finally{$lock->release();}
|
||
}
|
||
}
|