From cdbaf45f276c79fa8dfefc6b5e995f91bdc4110e Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Fri, 27 Nov 2026 11:15:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=82=E5=AE=B6=E8=AE=B0=E8=B4=A6=E6=8C=89?= =?UTF-8?q?=E6=88=90=E6=9C=AC=E4=BB=B7=E6=A0=B8=E7=AE=97=E6=B2=90=E9=87=8E?= =?UTF-8?q?=E7=AB=B9=E6=9C=A8=20(#1497)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/厂家记账金额规则.md | 2 + .../Services/SupplierBookkeepingValuation.php | 30 +++++++++-- .../SupplierBookkeepingValuationTest.php | 54 +++++++++++++++++++ 3 files changed, 81 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..d0fd146d 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']))); @@ -135,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) { @@ -163,14 +169,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'] = null; + $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 +209,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..8c81e775 100644 --- a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php +++ b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php @@ -365,4 +365,58 @@ 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->assertNull($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_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]; + $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']); + } }