fix(附加费对账): 支持彩艺买家原因及破损固定单价 #1753

Closed
limeng wants to merge 1 commits from fix/limeng-caiyi-surcharge-conditions into main
3 changed files with 61 additions and 5 deletions

View File

@ -144,7 +144,7 @@ final class BillSurchargeQuoteCalculator
return $result ?: ($found ? '对应工单附加费未审核通过' : '未找到对应单号、费用类型和快递的客服费用明细');
}
private function buyerReason(object $rule, array $orders, array $confirmations, bool $requireBuyer = true): array|string
private function buyerReason(object $rule, array $orders, array $confirmations, bool $requireBuyer = true, bool $allowDamage = false): array|string
{
$reasons = [];
foreach ($orders as $order) {
@ -162,7 +162,8 @@ final class BillSurchargeQuoteCalculator
$latest = $deductions[0] ?? $reasons[0];
[$operation, $cause] = array_map('trim', array_pad(explode('/', $latest['reason'], 2), 2, ''));
if ($operation === '' || ($requireBuyer && ($cause === '' || $cause === '买家-'))) return '查件工单原因不完整,无法核实买家原因';
$eligible = $operation === '扣单' && (! $requireBuyer || str_starts_with($cause, '买家-'));
$eligible = $operation === '扣单' && (! $requireBuyer || str_starts_with($cause, '买家-')
|| ($allowDamage && in_array($cause, ['快递-破损', '破损'], true)));
$confirmation = $confirmations[$latest['order']->woNo] ?? [];
if (! $eligible && $operation === '扣单' && ($confirmation['buyer_reason_confirmed'] ?? false) === true
&& ($confirmation['original_reason'] ?? '') === $latest['reason'] && trim($confirmation['evidence'] ?? '') !== '') {
@ -171,7 +172,8 @@ final class BillSurchargeQuoteCalculator
}
return ['eligible' => $eligible, 'order_id' => $latest['order_id'], 'operation' => $operation,
'evidence' => '聚工单 ' . $latest['order']->woNo . ' 原因:' . $latest['reason']
. ($eligible ? ($requireBuyer ? ',符合买家原因条件' : ',符合扣单条件') : ',不符合扣单且买家原因条件,不计费')];
. ($eligible ? ($requireBuyer ? ($allowDamage ? ',符合买家原因或破损条件' : ',符合买家原因条件') : ',符合扣单条件')
: ($allowDamage ? ',不符合买家原因或破损条件,不计费' : ',不符合扣单且买家原因条件,不计费'))];
}
private function fixedPrice(object $rule, string $type, array $orders, array $returns): array|string
@ -181,8 +183,8 @@ final class BillSurchargeQuoteCalculator
}
$condition = trim($rule->condition);
$evidence = '';
if (in_array($condition, ['查件扣单', '查件扣单并且买家原因', '查件扣单并且买家原因已退回', '查件扣单并且买家原因已退回的'], true)) {
$reason = $this->buyerReason($rule, $orders, [], $condition !== '查件扣单');
if (in_array($condition, ['查件扣单', '查件扣单并且买家原因', '查件扣单并且买家原因或破损', '查件扣单并且买家原因已退回', '查件扣单并且买家原因已退回的'], true)) {
$reason = $this->buyerReason($rule, $orders, [], $condition !== '查件扣单', $condition === '查件扣单并且买家原因或破损');
if (is_string($reason)) return $reason;
if (! $reason['eligible']) return [['settlement_amount' => 0, 'evidence' => $reason['evidence']]];
$evidence = $reason['evidence'];
@ -198,6 +200,14 @@ final class BillSurchargeQuoteCalculator
if (($fields['logisticsField']['value'] ?? '') !== $rule->courier
|| substr($order->created, 0, 10) < substr($rule->effective_at, 0, 10)
|| in_array($order->status, ['CANCELLED', 'CANCELED', 'DELETED', '取消', '已取消'], true)) continue;
// 改地址固定报价也接受工单中明确的客服通过回复,不把“已联系快递修改”视作通过。
$reason = (string) ($fields['SELECT_CASCADEField1413856620250813172526']['value'] ?? '');
if ($type === '改地址' && str_starts_with($reason, '改地址')
&& (in_array(trim((string) ($fields['SELECT_CASCADEField1413856620250814100750']['value'] ?? '')), ['通过', '已通过'], true)
|| in_array(trim((string) ($fields['TEXTField1413856620250902151101']['value'] ?? '')), ['通过', '已通过', '客服回复已通过'], true))) {
$approved = true;
$evidence = '聚工单 ' . $order->woNo . ' 改地址客服明确回复已通过';
}
foreach ($fields[ServiceFees::FEE_TABLE]['childFieldList'] ?? [] as $line) {
$values = array_column($line, 'value', 'fieldId');
if (SurchargeSettlementMatcher::type((string) ($values[ServiceFees::FEE_TYPE] ?? '')) === $type

View File

@ -18,6 +18,8 @@
固定单价自动核价读取报价中的金额(如30元/单、30/单),每票同类费用只计一次,不乘商品数量。空条件或“按单收取”按票计费;“查件扣单”核对同快递有效工单;带“买家原因”的条件核对扣单原因;带“已退回”的条件还要求该工单订单在最近三个自然月有实退数量大于0的进仓记录。退回状态通过已接入售后镜像的订单和售后单索引关联读取,无需全表扫描;缺少进仓依据保持未匹配,不推测已退回。客服通过类条件仍要求同单号、同快递、同费用类型的审核通过明细,金额取报价固定单价,不取客服填写的金额。暂不支持的条件或金额格式保留未匹配原因,不能回退到旧结算金额绕过条件。
固定单价条件“查件扣单并且买家原因或破损”接受扣单原因的“买家-*”“快递-破损”或“破损”,无需退回进仓;仅报价明确采用该条件时适用,不扩大其他报价的范围。改地址固定单价也接受同快递改地址工单中客服结果字段明确为“通过/已通过”,或客服回复字段明确为“通过/已通过/客服回复已通过”;“已联系快递修改”、待跟进及未通过均不是审核通过。
用户要求保留某厂家某月现有金额时,在既有覆盖值表的 `bill-surcharge-auto-match-holds` 数据集记录 `dropship|厂家ID|月份`、`enabled=true` 及原因,并写审计;`--existing-only` 的每日自动重匹配跳过该账期。新账单导入、其他厂家或月份不受影响,后续获授权的指定账期重算仍可正常执行。
客服费用通过现有单号索引读取共享工单,要求同一快递、费用类型一致、逐项审核通过且工单在账单月份结束前创建;按工单归集,不按商品数重复计费。“重量60kg以上”先检查本票已核算计费重量,低于门槛计0;缺重量、缺审核、无报价分别显示原因。原始账单、报价和发货运费均不改写。

View File

@ -429,6 +429,50 @@ class BillSurchargeMatchingTest extends FeatureTestCase
$this->assertEquals(30,$db->table('bill_surcharge_matches')->where('owner_id',$factory)->value('self_amount'));
}
public function test_fixed_buyer_or_damage_condition_does_not_require_return_and_does_not_change_other_rules(): void
{
$rule=$this->quote('测试彩艺','拦截费','固定单价','30元/单','查件扣单并且买家原因或破损');
$cases=[['DAMAGE001','扣单/买家-不想要',30],['DAMAGE002','扣单/快递-破损',30],
['DAMAGE003','扣单/破损',30],['DAMAGE004','扣单/超时未更新物流信息',0],
['DAMAGE005','催件/快递-破损',0],['DAMAGE006','扣单/',null]];
$input=[];foreach($cases as [$no,$reason,$expected]){
$this->ship($no,'测试彩艺');$this->workOrder($no,'测试彩艺','','',null,'2026-11-02',$reason);
$input[]=['waybill_no'=>$no,'item'=>'拦截退回'];
}
$calc=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
$result=$calc->calculate($input,'dropship','2026-11',['测试彩艺']);
foreach($cases as [$no,$reason,$expected]){
if($expected===null)$this->assertSame([],$result['rows'][$no]['拦截费']);
else $this->assertEquals($expected,$result['rows'][$no]['拦截费'][0]->settlement_amount);
}
$this->assertStringContainsString('快递-破损',$result['rows']['DAMAGE002']['拦截费'][0]->evidence);
DB::table('business_courier_surcharge_quotes')->where('id',$rule)->update(['condition'=>'查件扣单并且买家原因']);
$result=$calc->calculate([$input[1]],'dropship','2026-11',['测试彩艺']);
$this->assertEquals(0,$result['rows']['DAMAGE002']['拦截费'][0]->settlement_amount);
}
public function test_fixed_readdress_accepts_explicit_customer_reply_only(): void
{
$this->quote('测试彩艺','改地址','固定单价','30元/单','改地址客服回复通过');
$cases=[['REPLY0001','待客服跟进','已联系快递修改',null],['REPLY0002','通过','',30],
['REPLY0003','待客服跟进','已通过',30],['REPLY0004','未通过','客服未通过',null],
['REPLY0005','待客服跟进','客服回复已通过',30]];
$db=DB::connection('business_mirror');$input=[];
foreach($cases as [$no,$status,$reply,$expected]){
$this->ship($no,'测试彩艺');$this->workOrder($no,'测试彩艺','','',null,'2026-11-02','改地址、电话/本省改地址.电话');
$row=$db->table('js_jst_work_order_info')->orderByDesc('Id')->first();$fields=json_decode($row->businessFieldList,true);
$fields[]=['fieldId'=>'SELECT_CASCADEField1413856620250814100750','value'=>$status];
$fields[]=['fieldId'=>'TEXTField1413856620250902151101','value'=>$reply];
$db->table('js_jst_work_order_info')->where('Id',$row->Id)->update(['businessFieldList'=>json_encode($fields)]);
$input[]=['waybill_no'=>$no,'item'=>'改地址'];
}
$result=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class)->calculate($input,'dropship','2026-11',['测试彩艺']);
foreach($cases as [$no,$status,$reply,$expected]){
if($expected===null){$this->assertSame([],$result['rows'][$no]['改地址']);$this->assertStringContainsString('审核通过',$result['notes'][$no]['改地址']);}
else $this->assertEquals($expected,$result['rows'][$no]['改地址'][0]->settlement_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'=>['测试德邦']]);