fix(附加费): 支持配置的查件原因和单边长度条件 (#1770)

This commit is contained in:
limeng 2026-12-06 10:48:54 +08:00
parent fd55a7736b
commit 77b1044565
3 changed files with 70 additions and 2 deletions

View File

@ -120,7 +120,7 @@ class PullCourierSurchargeOverlength extends Command
$condition = (string) $rule->condition;
$standard = (string) $rule->standard;
if (preg_match('/单边超\s*(\d+(?:\.\d+)?)\s*(?:cm|厘米|CM|㎝)/u', $condition, $matched) !== 1) {
if (preg_match('/单边(?:长)?超\s*(\d+(?:\.\d+)?)\s*(?:cm|厘米|CM|㎝)/u', $condition, $matched) !== 1) {
$this->warn('规则 #' . $rule->id . ' 的生效条件解析不出单边阈值,已跳过:' . $condition);
continue;
}

View File

@ -104,7 +104,7 @@ final class BillSurchargeQuoteCalculator
return [['settlement_amount' => $amount, 'evidence' => $evidence . '取本票已核算发出运费 ' . number_format($amount, 2, '.', '') . ' 元']];
}
if ($rule->mode === '按尺寸/最长边') {
if (! preg_match('/^(?:单边超|最长边超过?)\s*(\d+(?:\.\d+)?)\s*(?:cm|厘米)$/iu', trim($rule->condition), $threshold)
if (! preg_match('/^(?:单边(?:长)?超|最长边超过?)\s*(\d+(?:\.\d+)?)\s*(?:cm|厘米)$/iu', trim($rule->condition), $threshold)
|| ! preg_match('/^(\d+(?:\.\d+)?)\s*元?\s*\/\s*单$/u', trim($rule->standard), $price)) return '尺寸计费条件或单价尚未支持自动核验';
$sides = [(float) ($ship['length_cm'] ?? 0), (float) ($ship['width_cm'] ?? 0), (float) ($ship['height_cm'] ?? 0)];
if (min($sides) <= 0) return '发货商品缺少完整尺寸';
@ -193,6 +193,23 @@ final class BillSurchargeQuoteCalculator
if ($received === null) return '未找到对应订单在匹配范围内的有效退回进仓明细,待核实';
$evidence .= ';退回进仓 ' . $received;
}
} elseif (preg_match('/^查件类型\/查件原因是([^\/]+)\/([^\/]+)$/u', $condition, $path)) {
$expected = trim($path[1]) . '/' . trim($path[2]);
$candidates = array_filter($orders, static function ($order) use ($rule): bool {
$fields = array_column(json_decode($order->businessFieldList, true, 512, JSON_THROW_ON_ERROR) ?: [], 'value', 'fieldId');
return ($fields['logisticsField'] ?? '') === $rule->courier
&& substr($order->created, 0, 10) >= substr($rule->effective_at, 0, 10)
&& ! in_array($order->status, ['CANCELLED', 'CANCELED', 'DELETED', '取消', '已取消'], true);
});
if ($candidates === []) return '未找到同快递的有效查件工单,无法核验查件类型和原因';
usort($candidates, static fn ($a, $b) => [$b->created, $b->source_id] <=> [$a->created, $a->source_id]);
$latest = $candidates[0];
$fields = array_column(json_decode($latest->businessFieldList, true, 512, JSON_THROW_ON_ERROR) ?: [], 'value', 'fieldId');
$actual = trim((string) ($fields['SELECT_CASCADEField1413856620250813172526'] ?? ''));
if ($actual === '') return '查件工单未填写查件类型和原因,待核实';
$evidence = '聚工单 ' . $latest->woNo . ' 查件类型/原因为“' . $actual . '”';
if ($actual !== $expected) return [['settlement_amount' => 0, 'evidence' => $evidence . ',不符合报价条件“' . $expected . '”,不计费']];
$evidence .= ',符合报价条件';
} elseif (in_array($condition, ['聚工单客服回复通过', '改地址客服回复通过'], true)) {
$approved = false;
foreach ($orders as $order) {

View File

@ -509,6 +509,57 @@ class BillSurchargeMatchingTest extends FeatureTestCase
$this->assertStringContainsString('没有启用',$rows[4]->match_note);
}
public function test_fixed_fee_matches_the_configured_work_order_reason_without_requiring_approval(): void
{
$condition='查件类型/查件原因是改地址、电话/本省改地址.电话';
$this->quote('曹县德邦','改地址','固定单价','30元/单',$condition,'自有快递');
$this->quote('曹县德邦小件','改地址','固定单价','20元/单',$condition,'自有快递');
$input=[];
foreach(['LOCAL01','CROSS01','MISSING01','LATEST01','CANCEL01','WRONG01','SMALL01']as$no){
$this->ship($no,$no==='SMALL01'?'曹县德邦小件':'曹县德邦');
$input[]=['waybill_no'=>$no,'item'=>'改地址'];
}
foreach(['LOCAL01','LATEST01','CANCEL01']as$no)$this->workOrder($no,'曹县德邦','','',null,'2026-11-03 10:00:00','改地址、电话/本省改地址.电话');
$this->workOrder('SMALL01','曹县德邦小件','','',null,'2026-11-03','改地址、电话/本省改地址.电话');
$this->workOrder('CROSS01','曹县德邦','','',null,'2026-11-03','改地址、电话/跨省改地址,电话');
$this->workOrder('LATEST01','曹县德邦','','',null,'2026-11-04','改地址、电话/跨省改地址,电话');
$this->workOrder('CANCEL01','曹县德邦','','',null,'2026-11-04','改地址、电话/跨省改地址,电话','CANCELLED');
$this->workOrder('WRONG01','曹县德邦其他','','',null,'2026-11-03','改地址、电话/本省改地址.电话');
$r=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class)->calculate($input,'courier','2026-11',['曹县德邦','曹县德邦小件']);
foreach(['LOCAL01','CANCEL01']as$no)$this->assertEquals(30,$r['rows'][$no]['改地址'][0]->settlement_amount);
$this->assertEquals(20,$r['rows']['SMALL01']['改地址'][0]->settlement_amount);
foreach(['CROSS01','LATEST01']as$no){
$this->assertEquals(0,$r['rows'][$no]['改地址'][0]->settlement_amount);
$this->assertStringContainsString('不符合报价条件',$r['rows'][$no]['改地址'][0]->evidence);
}
foreach(['MISSING01','WRONG01']as$no)$this->assertStringContainsString('未找到同快递的有效查件工单',$r['notes'][$no]['改地址']);
// 实际账单金额不能取代报价,重复工单也只按票计一次。
$this->workOrder('LOCAL01','曹县德邦','','',null,'2026-11-05','改地址、电话/本省改地址.电话');
$store=app(CourierBillStore::class);$store->saveGroup(null,['name'=>'曹县德邦','express_names'=>['曹县德邦','曹县德邦小件']]);
$path=$this->book(['账单'=>[['单号','运费'],['LOCAL01',10],['SMALL01',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['LOCAL01',999,'改地址'],['SMALL01',999,'改地址']]]);
$fid=$store->upload([new UploadedFile($path,'曹县德邦11月.xlsx',null,null,true)],'2026-11',[],'测试')[0];
$sid=$store->db()->table('courier_bill_files')->where('id',$fid)->value('statement_id');
$rows=app(SurchargeSettlementMatcher::class)->query('courier',$sid)->get()->keyBy('waybill_no');
$this->assertEquals(30,$rows['LOCAL01']->self_amount);
$this->assertEquals(20,$rows['SMALL01']->self_amount);
}
public function test_long_side_wording_is_supported_without_ignoring_additional_conditions(): void
{
$this->ship('LONG01','曹县德邦其他');$this->ship('EDGE01','曹县德邦其他');
$db=DB::connection('business_mirror');$db->table('jst_oms_shipping')->update(['length_cm'=>160,'width_cm'=>20,'height_cm'=>20]);
$db->table('jst_oms_shipping')->where('express_no','LONG01')->update(['length_cm'=>160.01]);
$this->quote('曹县德邦其他','超长费','按尺寸/最长边','8元/单','单边长超160cm','自有快递');
$calc=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
$input=[['waybill_no'=>'LONG01','item'=>'超长费'],['waybill_no'=>'EDGE01','item'=>'超长费']];
$r=$calc->calculate($input,'courier','2026-11',['曹县德邦其他']);
$this->assertEquals(8,$r['rows']['LONG01']['超长费'][0]->settlement_amount);
$this->assertEquals(0,$r['rows']['EDGE01']['超长费'][0]->settlement_amount);
DB::table('business_courier_surcharge_quotes')->update(['condition'=>'单边长超160cm并且客服通过']);
$r=$calc->calculate($input,'courier','2026-11',['曹县德邦其他']);
$this->assertStringContainsString('尚未支持自动核验',$r['notes']['LONG01']['超长费']);
}
public function test_quote_scope_effective_date_missing_freight_and_size_boundaries(): void
{
$this->ship('QA11111111','测试快递');$this->ship('QA22222222','测试快递');