From 7516a5d1c133f887669a368f0e503b2353acc718 Mon Sep 17 00:00:00 2001
From: limeng <1131177064@qq.com>
Date: Mon, 9 Nov 2026 09:15:49 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E5=8E=82=E5=AE=B6?=
=?UTF-8?q?=E8=AE=B0=E8=B4=A6=E6=9B=BF=E4=BB=A3=E8=BF=90=E8=B4=B9=E5=8F=8A?=
=?UTF-8?q?=E7=BB=84=E5=90=88=E8=A3=85=E8=AE=A1=E4=BB=B6=E8=A7=84=E5=88=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Services/SupplierBookkeepingValuation.php | 54 ++++++++------
.../manufacturer-bookkeeping.blade.php | 2 +-
.../Business/SupplierBookkeepingPageTest.php | 38 ++++++++++
.../SupplierBookkeepingValuationTest.php | 71 ++++++++++++++++---
4 files changed, 133 insertions(+), 32 deletions(-)
diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php
index d35316b3..93044d6c 100644
--- a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php
+++ b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php
@@ -119,37 +119,42 @@ class SupplierBookkeepingValuation
$packages[$package]['has_freight'] = true;
}
}
- $estimatedPackages = $freightOnly ? array_filter($packages, static fn ($package) => !$package['has_freight'] || bccomp($package['amount'], '0', 6) === 0) : [];
- if ($estimatedPackages !== []) {
+ $estimatedPackages = 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'])));
if (array_diff(array_keys($wanted), $shippedSkus) !== []) return $this->review($row, '原单发货明细未匹配全部工单商品,无法估算运费');
}
$packageCosts = [];
+ $usedBundleQuantity = false;
foreach ($pool as $shipment) {
$package = (string) $shipment['source_id'];
if ($freightOnly && !isset($estimatedPackages[$package])) continue;
if ($wanted === []) return $this->review($row, '工单没有商品编码,无法匹配替代运费所需成本');
- $sku = Repository::sku($shipment['barcode']);
- $components = array_map([Repository::class, 'sku'], Repository::numbers($shipment['barcode']));
- if (count($components) > 1 && array_intersect(array_keys($wanted), $components) !== []) return $this->review($row, '多商品包裹只有合计数量,缺少逐商品发货数量');
- if (!array_key_exists($sku, $wanted)) continue;
- if (!self::decimal($shipment['qty']) || bccomp((string) $shipment['qty'], '0', 6) <= 0) {
- return $this->review($row, '匹配发货数量缺失或无效');
+ $components = array_values(array_unique(array_map([Repository::class, 'sku'], Repository::numbers($shipment['barcode']))));
+ $bundle = count($components) > 1;
+ foreach ($components as $sku) {
+ if (!array_key_exists($sku, $wanted)) continue;
+ // 组合装导出只有合计数量,财务确认按各商品条码1件核价。
+ $lineQuantity = $bundle ? '1' : (string) ($shipment['qty'] ?? '');
+ if (!self::decimal($lineQuantity) || bccomp($lineQuantity, '0', 6) <= 0) {
+ return $this->review($row, '匹配发货数量缺失或无效');
+ }
+ $usedBundleQuantity = $usedBundleQuantity || $bundle;
+ $lineKey = $package . '|' . $sku;
+ if (isset($lines[$lineKey])) {
+ if (bccomp($lines[$lineKey], $lineQuantity, 6) !== 0) return $this->review($row, '同一包裹商品数量不一致,请核对');
+ continue;
+ }
+ $lines[$lineKey] = $lineQuantity;
+ $quantity = bcadd($quantity, $lineQuantity, 6);
+ $price = $costs[$sku] ?? null;
+ if (!self::decimal($price) || bccomp((string) $price, '0', 6) <= 0) return $this->review($row, '商品成本价缺失或为0:' . $sku);
+ $lineCost = bcmul((string) $price, $lineQuantity, 6);
+ $cost = bcadd($cost, $lineCost, 6);
+ $packageCosts[$package] = bcadd($packageCosts[$package] ?? '0', $lineCost, 6);
+ $wanted[$sku] = true;
}
- $lineKey = $package . '|' . $sku;
- if (isset($lines[$lineKey])) {
- if (bccomp($lines[$lineKey], (string) $shipment['qty'], 6) !== 0) return $this->review($row, '同一包裹商品数量不一致,请核对');
- continue;
- }
- $lines[$lineKey] = (string) $shipment['qty'];
- $quantity = bcadd($quantity, (string) $shipment['qty'], 6);
- $price = $costs[$sku] ?? null;
- if (!self::decimal($price) || bccomp((string) $price, '0', 6) <= 0) return $this->review($row, '商品成本价缺失或为0:' . $sku);
- $lineCost = bcmul((string) $price, (string) $shipment['qty'], 6);
- $cost = bcadd($cost, $lineCost, 6);
- $packageCosts[$package] = bcadd($packageCosts[$package] ?? '0', $lineCost, 6);
- $wanted[$sku] = true;
}
if (!$freightOnly && in_array(false, $wanted, true)) return $this->review($row, '原单发货明细未匹配全部工单商品');
foreach (array_keys($estimatedPackages) as $package) {
@@ -177,13 +182,16 @@ class SupplierBookkeepingValuation
} else {
$row['matched_cost'] = self::money($cost);
$ceiling = self::money(bcadd($cost, $freight, 6));
+ $freightLabel = $estimatedPackages === [] ? '实际运费' : '运费';
if (bccomp((string) $row['amount'], $ceiling, 2) > 0) {
$row['amount'] = $ceiling;
- $basis = '原记账金额大于商品成本+实际运费,按成本+运费记账';
+ $basis = '原记账金额大于商品成本+' . $freightLabel . ',按成本+运费记账';
} else {
- $basis = '原记账金额不大于商品成本+实际运费,保留记账金额';
+ $basis = '原记账金额不大于商品成本+' . $freightLabel . ',保留记账金额';
}
+ if ($estimatedPackages !== []) $basis .= ';原单运费缺失或为0,按商品成本×0.2替代运费';
}
+ if ($usedBundleQuantity) $basis .= ';组合装按各条码1件计';
$row['deduction_basis'] .= ';' . $basis;
$row['remark'] = trim((string) ($row['remark'] ?? '')) === '' ? $basis : $row['remark'] . "\n" . $basis;
if (bccomp((string) $row['amount'], '0', 2) === 0) $row['deduction_status'] = SupplierBookkeepingDeductionRule::STATUS_SKIP;
diff --git a/resources/views/business/pages/supplier-payables/manufacturer-bookkeeping.blade.php b/resources/views/business/pages/supplier-payables/manufacturer-bookkeeping.blade.php
index 3175374b..69494dcb 100644
--- a/resources/views/business/pages/supplier-payables/manufacturer-bookkeeping.blade.php
+++ b/resources/views/business/pages/supplier-payables/manufacturer-bookkeeping.blade.php
@@ -54,7 +54,7 @@
与聚工单「协同工单列表 → 记账」视图一致:工单类型为“记账”;工单状态为待审核、已拒绝、待处理、处理中、已完成(不含已取消);记账原因为“厂家记账”下 12 项与“仓库记账”下 5 项二级原因;发起时间不早于 2026-09-01。同一工单号只取最新一版,状态或原因在聚工单变更后随镜像同步自动更新;多件商品的工单在商品列逐行展示,记账金额只计一次。数据只读,不在此修改。 厂家扣款判定:记账原因为“厂家记账”的,按「厂家记账配置」中供应商的记账方式判定:“正常记账”以及配置表中没有的供应商(含配置不在生效期内)计入扣款,“仅代发仓发错货记账”“不记账”不扣;记账原因为“仓库记账”的,只有订单发货仓库为“某某代发仓”且二级原因为“仓库发错货”时才作为厂家扣款项(供应商配置为“不记账”的除外),其余仓库记账一律不扣。工单未带供应商时先按普通商品编码、可识别组合编码或原单发货明细拆出实际商品编码,到商品资料匹配供应商,再按顺序判断。仍缺供应商、或多件商品分属不同供应商且判定不一致的显示“待确认”,不计入扣款金额。接入口径
小额免记账:“补偿款项”“记账全款”依次用买家实付合计、订单实付金额判断,原记账金额小于50元且原记账金额 ÷ 对应实付小于40%时改为0,备注“小于50且小于0.4,记账改0”。金额等于50元、比例等于40%时不免记账;实付缺失、无效或为0的一轮不自动改0。
-单号匹配前自动清理前后的单引号、@和空格。剩余可扣款工单按原快递单号匹配实际发货;未提供快递单号时按线上订单号匹配。查找期间为工单发起月份及前五个月(如10月取5—10月)。商品成本按商品编码匹配普通商品资料成本单价,再乘对应商品的实际发货数量;“补偿款项”“记账全款”取原记账金额与商品成本+实际运费的较小值。“运费损耗”优先取实际运费,包裹运费为空或0时按对应商品成本合计×20%替代,并在判定说明中标注。包裹运费只累计一次;缺发货、成本或逐商品数量等无法核算的工单标为待确认,不计入扣款合计。原金额及原备注保留,页面、筛选、合计和导出使用同一结果。
+单号匹配前自动清理前后的单引号、@和空格。剩余可扣款工单按原快递单号匹配实际发货;未提供快递单号时按线上订单号匹配。查找期间为工单发起月份及前五个月(如10月取5—10月)。商品成本按商品编码匹配普通商品资料成本单价,再乘对应商品的实际发货数量;组合装只有合计数量、缺逐商品数量时,按各条码1件计,并在判定说明中标注。“补偿款项”“记账全款”取原记账金额与商品成本+运费的较小值;“运费损耗”按运费记账。运费优先取实际运费,包裹运费为空或0时按对应商品成本合计×20%替代,并在判定说明中标注。包裹运费只累计一次;仍缺发货、成本或非组合装发货数量等无法核算的工单标为待确认,不计入扣款合计。原金额及原备注保留,页面、筛选、合计和导出使用同一结果。
@if($bookkeepingError)