fix(附加费对账): 保留指定账期自动匹配结果 #1749
@ -10,9 +10,12 @@ 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,默认上月} {--statement=* : 指定快递对账单} {--factory=* : 指定代发厂家} {--all : 匹配该月全部未确认账单} {--existing-only : 只更新已建立附加费匹配的账单} {--company=1}';
|
||||
protected $description='匹配指定月份账单的附加费金额及商品信息,已确认账单不变';
|
||||
|
||||
@ -25,6 +28,10 @@ class MatchBillSurcharges extends Command
|
||||
$lock=Cache::lock('bill-surcharge-matching',3900);if(!$lock->get()){$this->error('附加费匹配正在运行');return self::FAILURE;}
|
||||
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')))
|
||||
@ -37,6 +44,7 @@ class MatchBillSurcharges extends Command
|
||||
->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);});
|
||||
|
||||
@ -18,4 +18,6 @@
|
||||
|
||||
固定单价自动核价读取报价中的金额(如30元/单、30/单),每票同类费用只计一次,不乘商品数量。空条件或“按单收取”按票计费;“查件扣单”核对同快递有效工单;带“买家原因”的条件核对扣单原因;带“已退回”的条件还要求该工单订单在最近三个自然月有实退数量大于0的进仓记录。退回状态通过已接入售后镜像的订单和售后单索引关联读取,无需全表扫描;缺少进仓依据保持未匹配,不推测已退回。客服通过类条件仍要求同单号、同快递、同费用类型的审核通过明细,金额取报价固定单价,不取客服填写的金额。暂不支持的条件或金额格式保留未匹配原因,不能回退到旧结算金额绕过条件。
|
||||
|
||||
用户要求保留某厂家某月现有金额时,在既有覆盖值表的 `bill-surcharge-auto-match-holds` 数据集记录 `dropship|厂家ID|月份`、`enabled=true` 及原因,并写审计;`--existing-only` 的每日自动重匹配跳过该账期。新账单导入、其他厂家或月份不受影响,后续获授权的指定账期重算仍可正常执行。
|
||||
|
||||
客服费用通过现有单号索引读取共享工单,要求同一快递、费用类型一致、逐项审核通过且工单在账单月份结束前创建;按工单归集,不按商品数重复计费。“重量60kg以上”先检查本票已核算计费重量,低于门槛计0;缺重量、缺审核、无报价分别显示原因。原始账单、报价和发货运费均不改写。
|
||||
|
||||
@ -414,6 +414,21 @@ class BillSurchargeMatchingTest extends FeatureTestCase
|
||||
$this->assertNull($rows[1]->self_amount);$this->assertStringContainsString('未找到有效',$rows[1]->match_note);
|
||||
}
|
||||
|
||||
public function test_daily_matching_honors_month_specific_hold_but_explicit_recalculation_is_available(): void
|
||||
{
|
||||
$store=app(DropshipFreightStore::class);$factory=$store->saveFactory(null,['name'=>'保留测试','warehouse'=>'测试仓','keywords'=>'测试']);
|
||||
$this->quote('测试固定','拦截费','固定单价','30元/单','按单收取');$this->ship('HOLD0001','测试固定');
|
||||
$path=$this->book(['账单'=>[['单号','运费'],['HOLD0001',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['HOLD0001',30,'拦截费']]]);
|
||||
$store->uploadFor('保留11月.xlsx',$path,'2026-11',$factory,'测试');
|
||||
$db=$store->db();$db->table('bill_surcharge_matches')->where('owner_id',$factory)->update(['self_amount'=>12]);
|
||||
$holds=app(\Modules\EcommerceFinance\Services\SupplierPayableOverrideStore::class);$dataset=\App\Console\Commands\MatchBillSurcharges::AUTO_MATCH_HOLDS;
|
||||
$holds->save($dataset,'dropship|'.$factory.'|2026-11',['enabled'=>true,'reason'=>'用户要求保留当前金额'],(int)$this->company->id,null);
|
||||
$this->artisan('courier-surcharge:match-bills',['--month'=>'2026-11','--factory'=>[$factory],'--existing-only'=>true,'--company'=>$this->company->id])->assertSuccessful();
|
||||
$this->assertEquals(12,$db->table('bill_surcharge_matches')->where('owner_id',$factory)->value('self_amount'));
|
||||
$this->artisan('courier-surcharge:match-bills',['--month'=>'2026-11','--factory'=>[$factory],'--company'=>$this->company->id])->assertSuccessful();
|
||||
$this->assertEquals(30,$db->table('bill_surcharge_matches')->where('owner_id',$factory)->value('self_amount'));
|
||||
}
|
||||
|
||||
public function test_customer_service_approval_weight_gate_and_missing_rule_are_explicit(): void
|
||||
{
|
||||
$store=app(CourierBillStore::class);$group=$store->saveGroup(null,['name'=>'测试德邦','express_names'=>['测试德邦']]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user