From f7eda7dffe4019d2d5f495b5a7a57b631a3212d7 Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Fri, 27 Nov 2026 03:07:41 +0000 Subject: [PATCH 1/3] =?UTF-8?q?feat(=E5=8E=82=E5=AE=B6=E8=AE=B0=E8=B4=A6):?= =?UTF-8?q?=20=E6=B2=90=E9=87=8E=E7=AB=B9=E6=9C=A8=E5=8F=AA=E8=AE=B0?= =?UTF-8?q?=E6=88=90=E6=9C=AC=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 供应商为沐野竹木的补偿款项、记账全款、运费损耗工单,一律按原单实际发货数量×商品成本价记账, 不加运费、不与原金额比较;缺发货/数量/成本仍待确认;与其他供应商混合的工单待确认。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UNTa9m18e8BPYt6zwCu86Z --- docs/厂家记账金额规则.md | 2 ++ .../Services/SupplierBookkeepingValuation.php | 29 ++++++++++++--- .../SupplierBookkeepingValuationTest.php | 35 +++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/docs/厂家记账金额规则.md b/docs/厂家记账金额规则.md index 35f8487b..9fdb3dc4 100644 --- a/docs/厂家记账金额规则.md +++ b/docs/厂家记账金额规则.md @@ -11,6 +11,8 @@ 4. 补偿、全款按原单商品的实际发货数量乘普通商品资料当前成本单价,再加实际包裹运费,与原金额取较小值。成本按商品编码精确匹配商品资料的当前 `cost_price`,不代表历史月份成本。 5. 运费损耗优先按正数实际运费记账,不受原金额限制;原金额为0也需要匹配。原单包裹运费为空或0时,按对应工单商品的当前成本单价×实际发货数量×0.2替代。多个包裹中只替代缺运费的包裹,其余仍取实际运费。替代需要有效商品成本与逐商品发货数量,缺失或数量含糊时待确认;已有正数实际运费不要求商品成本和数量。 +6. 供应商为「沐野竹木」的工单只记成本价:补偿款项、记账全款、运费损耗均按原单实际发货数量×商品资料当前成本单价记账,不加运费、不与原金额比较,也不要求原单运费;缺发货、数量或成本价时仍待确认。前面的配置、小额归零和代发仓限制照常适用。同一工单同时含沐野竹木和其他供应商时待确认。 + 金额等于50元或比例等于40%均不满足小额免记账。金额计算使用十进制运算,在最终金额处四舍五入到分。 ## 发货匹配 diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php index 93044d6c..70f00d9e 100644 --- a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php +++ b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php @@ -8,6 +8,9 @@ use Throwable; /** 两轮免记账之后,按原单发货记录核算实际可记账金额。 */ class SupplierBookkeepingValuation { + /** 只记成本价的供应商:补偿、全款、运费损耗均按发货数量×商品成本价记账,不加运费、不与原金额比较。 */ + public const COST_ONLY_SUPPLIERS = ['沐野竹木']; + public function __construct(private readonly Repository $repository) {} /** @param list> $rows @return list> */ @@ -100,7 +103,9 @@ class SupplierBookkeepingValuation $quantity = '0'; $cost = '0'; $wanted = array_fill_keys(array_map([Repository::class, 'sku'], Repository::numbers(($row['resolved_product_code'] ?? '') !== '' ? $row['resolved_product_code'] : ($row['product_code'] ?? ''))), false); - $freightOnly = $row['bookkeeping_type'] === '运费损耗'; + $costOnly = self::costOnlyMode($row); + if ($costOnly === null) return $this->review($row, '多个供应商中含只记成本价的' . implode('、', self::COST_ONLY_SUPPLIERS) . ',请人工确认'); + $freightOnly = !$costOnly && $row['bookkeeping_type'] === '运费损耗'; $lines = []; if (!$freightOnly && $wanted === []) return $this->review($row, '工单没有商品编码,无法匹配成本'); foreach ($pool as $shipment) { @@ -108,7 +113,7 @@ class SupplierBookkeepingValuation if (trim((string) $shipment['express_no']) === '' || count(Repository::numbers($shipment['express_no'])) !== 1) return $this->review($row, '包裹快递单号缺失或不唯一,请核对'); $package = (string) $shipment['source_id']; $packages[$package] ??= ['amount' => '0', 'has_freight' => false]; - if ($shipment['freight'] !== null && $shipment['freight'] !== '') { + if (!$costOnly && $shipment['freight'] !== null && $shipment['freight'] !== '') { if (!self::decimal($shipment['freight'])) return $this->review($row, '原单运费无效,请核对'); // 汇总表的包裹运费可能随每件商品重复关联,只取一次;冲突值不能相加。 if ($packages[$package]['has_freight'] @@ -119,7 +124,7 @@ class SupplierBookkeepingValuation $packages[$package]['has_freight'] = true; } } - $estimatedPackages = array_filter($packages, static fn ($package) => !$package['has_freight'] || bccomp($package['amount'], '0', 6) === 0); + $estimatedPackages = $costOnly ? [] : array_filter($packages, static fn ($package) => !$package['has_freight'] || bccomp($package['amount'], '0', 6) === 0); if ($freightOnly && $estimatedPackages !== []) { $shippedSkus = []; foreach ($pool as $shipment) array_push($shippedSkus, ...array_map([Repository::class, 'sku'], Repository::numbers($shipment['barcode']))); @@ -163,14 +168,19 @@ class SupplierBookkeepingValuation $packages[$package]['has_freight'] = true; } $freight = '0'; - foreach ($packages as $package) { + foreach ($costOnly ? [] : $packages as $package) { if (!$package['has_freight']) return $this->review($row, '原单运费尚未补齐,不能按0记账'); $freight = bcadd($freight, $package['amount'], 6); } $row['matched_freight'] = self::money($freight); $row['matched_quantity'] = $freightOnly && $estimatedPackages === [] ? '' : rtrim(rtrim($quantity, '0'), '.'); $row['matched_tracking_no'] = implode("\n", array_values(array_unique(array_column($pool, 'express_no')))); - if ($freightOnly) { + if ($costOnly) { + $row['matched_cost'] = self::money($cost); + $row['matched_freight'] = ''; + $row['amount'] = self::money($cost); + $basis = implode('、', self::COST_ONLY_SUPPLIERS) . '只记成本价:按发货数量×商品成本价记账,不含运费'; + } elseif ($freightOnly) { $row['amount'] = self::money($freight); if ($estimatedPackages !== []) { $row['matched_cost'] = self::money($cost); @@ -198,6 +208,15 @@ class SupplierBookkeepingValuation return $row; } + /** true=全部为只记成本价供应商;false=不含;null=与其他供应商混合,需人工确认。 */ + private static function costOnlyMode(array $row): ?bool + { + $suppliers = array_values(array_unique(array_filter(array_map('trim', explode("\n", (string) ($row['supplier'] ?? ''))), static fn ($name) => $name !== ''))); + $hits = array_intersect($suppliers, self::COST_ONLY_SUPPLIERS); + if ($hits === []) return false; + return count($hits) === count($suppliers) ? true : null; + } + private function review(array $row, string $reason): array { $row['deduction_status'] = SupplierBookkeepingDeductionRule::STATUS_REVIEW; diff --git a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php index 5c1e3e89..7a9ce4c8 100644 --- a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php +++ b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php @@ -365,4 +365,39 @@ class SupplierBookkeepingValuationTest extends TestCase $this->assertSame('待确认', $incomplete['deduction_status']); $this->assertStringContainsString('未匹配全部工单商品', $incomplete['deduction_basis']); } + + public function test_muye_supplier_books_cost_only_for_compensation_full_and_freight_loss(): void + { + $service = $this->service([$this->shipment()]); + foreach (['补偿款项' => '100.00', '记账全款' => '10.00', '运费损耗' => '0.00'] as $type => $amount) { + $row = $service->apply([$this->row(['supplier' => '沐野竹木', 'bookkeeping_type' => $type, 'amount' => $amount])])[0]; + $this->assertSame('40.00', $row['amount'], $type); + $this->assertSame('40.00', $row['matched_cost']); + $this->assertSame('', $row['matched_freight']); + $this->assertSame('2', $row['matched_quantity']); + $this->assertSame('扣款', $row['deduction_status']); + $this->assertStringContainsString('沐野竹木只记成本价', $row['remark']); + } + } + + public function test_muye_supplier_ignores_missing_or_conflicting_freight(): void + { + $row = $this->service([ + $this->shipment(['freight' => null]), + $this->shipment(['id' => 2, 'barcode' => 'SKU-B', 'qty' => '1', 'freight' => '9.00']), + ], ['SKU-A' => '20.00', 'SKU-B' => '5.00'])->apply([$this->row(['supplier' => ' 沐野竹木 ', 'product_code' => "SKU-A\nSKU-B"])])[0]; + $this->assertSame('45.00', $row['amount']); + $this->assertSame('扣款', $row['deduction_status']); + } + + public function test_muye_supplier_still_requires_cost_and_mixed_suppliers_need_review(): void + { + $missingCost = $this->service([$this->shipment()], [])->apply([$this->row(['supplier' => '沐野竹木', 'bookkeeping_type' => '运费损耗'])])[0]; + $this->assertSame('待确认', $missingCost['deduction_status']); + $mixed = $this->service([$this->shipment()])->apply([$this->row(['supplier' => "沐野竹木\n其他厂家"])])[0]; + $this->assertSame('待确认', $mixed['deduction_status']); + $this->assertSame('100.00', $mixed['amount']); + $other = $this->service([$this->shipment()])->apply([$this->row(['supplier' => '其他厂家'])])[0]; + $this->assertSame('48.50', $other['amount']); + } } -- 2.47.2 From 5e9218184cafd640e73ad1c384db7b008e927780 Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Fri, 27 Nov 2026 11:11:28 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(=E5=8E=82=E5=AE=B6=E8=AE=B0=E8=B4=A6):?= =?UTF-8?q?=20=E9=81=BF=E5=85=8D=E6=B2=90=E9=87=8E=E7=AB=B9=E6=9C=A8?= =?UTF-8?q?=E7=BB=84=E5=90=88=E8=A3=85=E7=BC=BA=E6=95=B0=E9=87=8F=E6=97=B6?= =?UTF-8?q?=E6=8C=89=E4=B8=80=E4=BB=B6=E6=89=A3=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/SupplierBookkeepingValuation.php | 1 + .../SupplierBookkeepingValuationTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php index 70f00d9e..03511029 100644 --- a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php +++ b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php @@ -140,6 +140,7 @@ class SupplierBookkeepingValuation $bundle = count($components) > 1; foreach ($components as $sku) { if (!array_key_exists($sku, $wanted)) continue; + if ($costOnly && $bundle) return $this->review($row, '组合装缺少逐商品实际发货数量,无法按成本价记账'); // 组合装导出只有合计数量,财务确认按各商品条码1件核价。 $lineQuantity = $bundle ? '1' : (string) ($shipment['qty'] ?? ''); if (!self::decimal($lineQuantity) || bccomp($lineQuantity, '0', 6) <= 0) { diff --git a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php index 7a9ce4c8..5319e7ab 100644 --- a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php +++ b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php @@ -390,6 +390,25 @@ class SupplierBookkeepingValuationTest extends TestCase $this->assertSame('扣款', $row['deduction_status']); } + public function test_muye_supplier_requires_actual_shipped_quantity_and_keeps_zero_compensation(): void + { + $muye = $this->row(['supplier' => '沐野竹木']); + foreach ([ + [], + [$this->shipment(['qty' => null])], + [$this->shipment(['barcode' => "SKU-A\nSKU-B", 'qty' => '5'])], + ] as $shipments) { + $row = $this->service($shipments)->apply([$muye])[0]; + $this->assertSame('待确认', $row['deduction_status']); + $this->assertSame('100.00', $row['amount']); + } + + foreach (['补偿款项', '记账全款'] as $type) { + $zero = array_replace($muye, ['bookkeeping_type' => $type, 'amount' => '0.00', 'original_amount' => '0.00']); + $this->assertSame([$zero], $this->service([$this->shipment()])->apply([$zero])); + } + } + public function test_muye_supplier_still_requires_cost_and_mixed_suppliers_need_review(): void { $missingCost = $this->service([$this->shipment()], [])->apply([$this->row(['supplier' => '沐野竹木', 'bookkeeping_type' => '运费损耗'])])[0]; -- 2.47.2 From 725a0aedfeeb5002eb482c3b012e5c8e72ef66fe Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Fri, 27 Nov 2026 11:14:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(=E5=8E=82=E5=AE=B6=E8=AE=B0=E8=B4=A6):?= =?UTF-8?q?=20=E6=88=90=E6=9C=AC=E4=BB=B7=E5=B7=A5=E5=8D=95=E8=BF=90?= =?UTF-8?q?=E8=B4=B9=E4=BD=BF=E7=94=A8=E7=A9=BA=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EcommerceFinance/Services/SupplierBookkeepingValuation.php | 2 +- .../Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php index 03511029..d0fd146d 100644 --- a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php +++ b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php @@ -178,7 +178,7 @@ class SupplierBookkeepingValuation $row['matched_tracking_no'] = implode("\n", array_values(array_unique(array_column($pool, 'express_no')))); if ($costOnly) { $row['matched_cost'] = self::money($cost); - $row['matched_freight'] = ''; + $row['matched_freight'] = null; $row['amount'] = self::money($cost); $basis = implode('、', self::COST_ONLY_SUPPLIERS) . '只记成本价:按发货数量×商品成本价记账,不含运费'; } elseif ($freightOnly) { diff --git a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php index 5319e7ab..8c81e775 100644 --- a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php +++ b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php @@ -373,7 +373,7 @@ class SupplierBookkeepingValuationTest extends TestCase $row = $service->apply([$this->row(['supplier' => '沐野竹木', 'bookkeeping_type' => $type, 'amount' => $amount])])[0]; $this->assertSame('40.00', $row['amount'], $type); $this->assertSame('40.00', $row['matched_cost']); - $this->assertSame('', $row['matched_freight']); + $this->assertNull($row['matched_freight']); $this->assertSame('2', $row['matched_quantity']); $this->assertSame('扣款', $row['deduction_status']); $this->assertStringContainsString('沐野竹木只记成本价', $row['remark']); -- 2.47.2