diff --git a/docs/厂家记账金额规则.md b/docs/厂家记账金额规则.md index 099dd6f5..604015e9 100644 --- a/docs/厂家记账金额规则.md +++ b/docs/厂家记账金额规则.md @@ -18,6 +18,7 @@ - 查找时间为工单发起月份及前五个月,右端不含下月1日。例如10月工单读取5月1日至11月1日前的发货记录。六个月未完整导入时待确认。 - 有物流单号时精确匹配该单号或换单前单号,同时核对工单提供的线上订单号;无物流单号时按线上订单号匹配全部相关包裹。合并订单按拆分后的完整单号建立索引。 - 不以平均运费替代原单运费,不借同订单其他快递单的运费;多个单号只匹配部分时待确认。 +- “未发出”辅助列按用户确认:`#N/A` 表示未命中未发出名单,可正常匹配;返回单号的整个包裹排除,成本、数量和运费均不累计。按订单匹配时仅核算剩余已发出包裹;原单全部未发出或工单明确列出的单号被部分排除时待确认、不计扣款,保留原金额。其他辅助列错误待确认。 - 工单商品须全部匹配。一个单元格合并多个商品编码、仅有总数量时,不推测每个商品的数量,标为待确认。 - 包裹运费只取一次;同包裹同商品的重复行不重复累计数量。运费或数量冲突时待确认,不相加、不擅自选值。 - 缺发货、有效数量、成本价或运费时待确认,保留原金额,不计扣款合计。明确0运费有效,空运费不能当0。 diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php index 74b3c606..4b99dd3b 100644 --- a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php +++ b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php @@ -74,9 +74,23 @@ class SupplierBookkeepingValuation } } if ($pool === []) return $this->review($row, '六个月内未匹配到原订单/快递单号的发货记录'); + $unshippedPackages = []; + foreach ($pool as $shipment) { + $flag = trim((string) ($shipment['unshipped'] ?? '')); + // 工作平台导出的查找辅助列:#N/A 未命中名单,返回单号则确实未发出。 + if ($flag === '' || $flag === '#N/A') continue; + if (str_starts_with($flag, '#')) return $this->review($row, '未发出辅助列存在未识别错误,请核对'); + $unshippedPackages[(string) $shipment['source_id']] = true; + } + if ($unshippedPackages !== []) { + $pool = array_filter($pool, static fn ($shipment) => !isset($unshippedPackages[(string) $shipment['source_id']])); + if ($pool === []) return $this->review($row, '原单标记未发出,已排除核价,不计厂家扣款'); + $row['deduction_basis'] .= ';已排除未发出包裹'; + } $matchedReferences = []; foreach ($pool as $shipment) array_push($matchedReferences, ...($tracking !== [] ? ($shipment['tracking_numbers'] ?? [$shipment['express_no']]) : ($shipment['order_numbers'] ?? [$shipment['trade_no']]))); if (array_diff($tracking !== [] ? $tracking : $orders, $matchedReferences) !== []) { + if ($unshippedPackages !== []) return $this->review($row, '原单号包含未发出包裹,暂不能按部分金额记账'); return $this->review($row, '原单号包含未匹配发货记录,暂不能按部分金额记账'); } diff --git a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php index 46194192..6b8afae9 100644 --- a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php +++ b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php @@ -199,4 +199,43 @@ class SupplierBookkeepingValuationTest extends TestCase 'tracking_numbers' => ['EXP-NEW', 'EXP-1'], 'order_numbers' => ['ORDER-1', 'ORDER-2']])])->apply([$this->row()])[0]; $this->assertSame('48.50', $row['amount']); } + + public function test_unshipped_lookup_hit_excludes_the_whole_package_from_cost_and_freight(): void + { + $service = $this->service([ + $this->shipment(['unshipped' => '#N/A']), + $this->shipment(['id' => 2, 'source_id' => 20, 'express_no' => 'EXP-2', 'unshipped' => ' EXP-2 ', 'qty' => '9', 'freight' => '80']), + $this->shipment(['id' => 3, 'source_id' => 20, 'express_no' => 'EXP-2', 'unshipped' => '#N/A', 'barcode' => 'SKU-B', 'freight' => '80']), + ]); + $row = $service->apply([$this->row(['tracking_no' => ''])])[0]; + $this->assertSame('48.50', $row['amount']); + $this->assertSame('2', $row['matched_quantity']); + $this->assertSame('EXP-1', $row['matched_tracking_no']); + $this->assertStringContainsString('已排除未发出包裹', $row['deduction_basis']); + $freight = $service->apply([$this->row(['tracking_no' => '', 'bookkeeping_type' => '运费损耗'])])[0]; + $this->assertSame('8.50', $freight['amount']); + } + + public function test_only_unshipped_or_partially_excluded_explicit_tracking_never_deducts(): void + { + $service = $this->service([ + $this->shipment(['unshipped' => '#N/A']), + $this->shipment(['id' => 2, 'source_id' => 20, 'express_no' => 'EXP-2', 'unshipped' => 'EXP-2']), + ]); + foreach (['EXP-2', "EXP-1\nEXP-2"] as $tracking) { + foreach (['补偿款项', '运费损耗'] as $type) { + $row = $service->apply([$this->row(['tracking_no' => $tracking, 'bookkeeping_type' => $type])])[0]; + $this->assertSame('待确认', $row['deduction_status']); + $this->assertSame('100.00', $row['amount']); + $this->assertStringContainsString('未发出', $row['deduction_basis']); + } + } + } + + public function test_unknown_lookup_error_is_reviewed_instead_of_treated_as_a_tracking_hit(): void + { + $row = $this->service([$this->shipment(['unshipped' => '#REF!'])])->apply([$this->row()])[0]; + $this->assertSame('待确认', $row['deduction_status']); + $this->assertStringContainsString('未识别错误', $row['deduction_basis']); + } }