From 8b56efb66f3c505db7ea9f716ecfb1ed4b9ba4b5 Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Sun, 6 Dec 2026 09:26:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=8E=82=E5=AE=B6=E8=AE=B0=E8=B4=A6):=20?= =?UTF-8?q?=E6=8C=89=E8=B4=A2=E5=8A=A1=E7=A1=AE=E8=AE=A4=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E7=BC=BA=E6=96=87=E4=BB=B6=E6=A0=B8=E4=BB=B7=E4=B8=8E=E5=85=8D?= =?UTF-8?q?=E6=89=A3=E5=88=A4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 厂家记账对已确认未发出的单据仍显示待确认;缺月度发货文件时无法继续成本核价;同一快递号的其他发货来源会干扰工单对应商品的运费替代。 本次调整:全部未发出的工单明确不扣款;缺文件且无原单匹配的补偿/全款,保留原小额免记账顺序,按完整工单逐商品数量与当前成本、20%替代运费核价,再和原金额取小值;成本核价排除同号无关商品来源;增加公司级单工单免扣设置,应用财务确认的特例而不扩大到所有缺供应商工单。原单部分匹配、数量含糊、缺成本及运费损耗无记录等情况继续待确认。 修改范围:核价Repository、核价服务、商品组合解析、扣款规则、页面数据服务、对应单元/页面回归及规则文档。无数据库迁移;不改权限、上游、发货重量或发货运费计算。上线仅保存已获财务明确授权的一个工单免扣设置,并备份旧设置,不改原始工单。 验证:Docker 中以 www-data、禁用网络、内存 SQLite 运行相关核价、商品解析、规则、Repository 与页面回归,68 tests / 649 assertions 全通过(96.69秒,PHPUnit峰值121MB)。覆盖50元/40%边界、两轮实付、未发出、同号不同来源、多商品数量、缺项、页面筛选与CSV一致性。生产399条工单只读试算0.51秒、峰值52.5MiB,确认只有预期范围变化,敏感明细未放入Git或PR。 --- docs/厂家记账金额规则.md | 7 +- ...SupplierBookkeepingValuationRepository.php | 6 ++ .../SupplierBookkeepingDeductionRule.php | 6 +- .../SupplierBookkeepingProductResolver.php | 13 ++- .../Services/SupplierBookkeepingValuation.php | 88 +++++++++++++++++-- .../View/SupplierBookkeepingPageData.php | 4 +- .../Business/SupplierBookkeepingPageTest.php | 37 ++++++++ .../SupplierBookkeepingDeductionRuleTest.php | 11 +++ .../SupplierBookkeepingValuationTest.php | 71 ++++++++++++++- 9 files changed, 231 insertions(+), 12 deletions(-) diff --git a/docs/厂家记账金额规则.md b/docs/厂家记账金额规则.md index aed573a70..7585a5ca0 100644 --- a/docs/厂家记账金额规则.md +++ b/docs/厂家记账金额规则.md @@ -7,6 +7,7 @@ 1. 对“补偿款项”“记账全款”(兼容“记账全额”),原金额大于0且小于50元,先判断原金额÷商品买家实付合计是否小于40%。命中后金额为0,不扣款,追加备注 `小于50且小于0.4,记账改0`。 2. 未命中的,再用工单 `actual_paid_amount`(页面“实付金额”)作分母,独立判断同样的严格边界。任一轮实付缺失、无效或为0时,只跳过该轮;另一轮仍可正常判断。多商品买家实付缺一项时,不把部分金额当作完整合计。 3. 保留厂家记账配置、代发仓发错货限制和多供应商待确认规则。只有已判定可扣款的剩余工单进入核价,不用核价推翻“不扣款”或“待确认”。 + 财务明确指定的不记账工单,按公司设置 `supplier_bookkeeping.excluded_work_orders` 中的完整工单号排除,说明保留确认原因;不据此扩大为所有缺供应商工单不记账。 工单供应商为空时,先按普通商品编码匹配商品资料;完整的 `ZH-/MKH-商品编码-数量-商品编码-数量` 组合编码拆出组件后逐个精确匹配,普通商品精确命中优先。运营组合套装从六个月内原单发货明细拆出实际商品编码,必须覆盖全部指定单号并核对订单;未发出或未完整匹配时不采用部分明细。每个组件都查到商品资料供应商后,才按当前配置、小额归零和核价规则继续处理。保留原编码,展示实际商品编码、逐编码供应商匹配说明;任一组件缺供应商或多供应商判定不一致仍待确认,不使用部分供应商扣款。不覆盖工单已有供应商。 4. 补偿、全款按原单商品的实际发货数量乘普通商品资料当前成本单价,再加实际包裹运费,与原金额取较小值;原单运费为空或0时,按对应商品成本×0.2替代,并注明“原单运费缺失或为0,按商品成本×0.2替代运费”。成本按商品编码精确匹配商品资料的当前 `cost_price`,不代表历史月份成本。 5. 运费损耗优先按正数实际运费记账,不受原金额限制;原金额为0也需要匹配。原单包裹运费为空或0时,按对应工单商品的当前成本单价×实际发货数量×0.2替代。多个包裹中只替代缺运费的包裹,其余仍取实际运费。替代需要有效商品成本与逐商品发货数量,缺失或数量含糊时待确认;已有正数实际运费不要求商品成本和数量。 @@ -18,14 +19,16 @@ ## 发货匹配 - 运费取工作平台月度导出“运费”;5月“系统运费”经用户确认是相同口径。不使用“结算运费”,不借用覆盖不足的现有发货表或其他年份数据。 -- 查找时间为工单发起月份及前五个月,右端不含下月1日。例如10月工单读取5月1日至11月1日前的发货记录。六个月月度文件完整时保持原导出核价路径;缺文件且工单有明确快递号时,改为只读现有 `jst_oms_shipping` 自动同步明细,所有指定快递号和工单商品都匹配后,按同一金额规则核价,并注明“按系统同步发货明细核价(原快递单号完整匹配)”。无快递号、只有订单号时,仍须六个月月度文件完整,不按局部订单包裹核价。 +- 查找时间为工单发起月份及前五个月,右端不含下月1日。例如10月工单读取5月1日至11月1日前的发货记录。六个月月度文件完整时保持原导出核价路径;缺文件且工单有明确快递号时,优先只读现有 `jst_oms_shipping` 自动同步明细,所有指定快递号和工单商品都匹配后,按同一金额规则核价,并注明“按系统同步发货明细核价(原快递单号完整匹配)”。 +- 缺六个月文件且无原单匹配的补偿/全款,仍先按小于50元且占买家实付或订单实付比例小于0.4归零;未命中时,以完整工单逐商品数量×当前商品成本核价,缺运费按成本×0.2,和原记账金额取小值。完整组合编码中的逐件数量乘工单组合数;多行商品数量逐行对应,重复编码按各行数量相加。缺数量、缺成本、只能匹配部分包裹或已匹配包裹明细不全仍待确认。运费损耗不使用无发货记录的工单回退核价;沐野竹木仍只记成本价。判定说明注明使用工单数量,不将其称为实际发货数量。 - 自动同步来源须有成功同步记录,按现有快递号/订单号索引定点读取,换单前号仅在订单候选内精确核对;不直连上游、不改写发货数据。包裹行数须与 `pkg_count` 一致,沿用未发出名单排除,缺发货、部分单号、缺数量/商品/成本和冲突值仍待确认;自动同步来源不可用不影响文件完整的账期。 - 有物流单号时精确匹配该单号或换单前单号,同时核对工单提供的线上订单号;无物流单号时按线上订单号匹配全部相关包裹。合并订单按拆分后的完整单号建立索引。 - 单号及商品编码匹配前清理前后空白、不可见复制符、单引号、引号、`@` 及其全角形式;保留原始展示值及单号前导0,保留编码内部的 `-`、`+` 等合法内容,不做模糊匹配。 - 不以平均运费替代原单运费,不借同订单其他快递单的运费;多个单号只匹配部分时待确认。 -- “未发出”辅助列按用户确认:`#N/A` 表示未命中未发出名单,可正常匹配;返回单号的整个包裹排除,成本、数量和运费均不累计。按订单匹配时仅核算剩余已发出包裹;原单全部未发出或工单明确列出的单号被部分排除时待确认、不计扣款,保留原金额。其他辅助列错误待确认。 +- “未发出”辅助列按用户确认:`#N/A` 表示未命中未发出名单,可正常匹配;返回单号的整个包裹排除,成本、数量和运费均不累计。按订单匹配时仅核算剩余已发出包裹;原单全部未发出明确判“不扣款”,保留原金额作核对;工单明确列出的单号被部分排除时仍待确认、不计部分扣款。其他辅助列错误待确认。缺文件工单回退前同样先核对共享未发出名单。 - 工单商品须全部匹配。组合装一个单元格合并多个商品编码、仅有总数量时,沿用财务已确认的各条码1件口径,并注明“组合装按各条码1件计”;沐野竹木只记成本价的特殊规则仍要求逐商品实际数量。 - 包裹运费只取一次;同包裹同商品的重复行不重复累计数量。运费或数量冲突时待确认,不相加、不擅自选值。 +- 同一快递号对应多个来源发货单时,补偿/全款仅核算含工单商品的来源单,排除同号无关配件单;仍须覆盖全部指定单号和商品,不借用无关来源单运费。工单重复展示同一商品编码时,以实际发货数量核价,不能把重复编码再次乘数量。 - 缺发货、有效数量或成本价时待确认,保留原金额,不计扣款合计;空/0运费按第4、5条替代,成本与数量列展示替代部分的依据,不把缺失运费直接按0记账。 - 原记账金额、匹配成本/运费/数量/快递号/期间供页面核对与导出。 diff --git a/modules/EcommerceFinance/Repositories/SupplierBookkeepingValuationRepository.php b/modules/EcommerceFinance/Repositories/SupplierBookkeepingValuationRepository.php index 06128b38f..2b726b04f 100644 --- a/modules/EcommerceFinance/Repositories/SupplierBookkeepingValuationRepository.php +++ b/modules/EcommerceFinance/Repositories/SupplierBookkeepingValuationRepository.php @@ -142,6 +142,12 @@ class SupplierBookkeepingValuationRepository ->pluck('source_month')->map(static fn ($date) => substr($date, 0, 7))->all(); } + /** 缺发货文件时仍先核对共享未发出名单,不将未发出的订单转为按工单成本记账。 */ + public function unshippedTracking(array $tracking): array + { + return array_keys(app(UnshippedWaybills::class)->unshippedSources($tracking)); + } + /** @return array 规范化 SKU => 当前商品资料成本单价 */ public function costsFor(array $skus): array { diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingDeductionRule.php b/modules/EcommerceFinance/Services/SupplierBookkeepingDeductionRule.php index acd94765a..9cdc082e7 100644 --- a/modules/EcommerceFinance/Services/SupplierBookkeepingDeductionRule.php +++ b/modules/EcommerceFinance/Services/SupplierBookkeepingDeductionRule.php @@ -95,8 +95,12 @@ class SupplierBookkeepingDeductionRule * @param array|object> $configs 供应商名称 => 生效中的配置 * @return array{supplier_config:string,deduction_status:string,deduction_basis:string,amount?:string,remark?:string} */ - public static function decide(array $row, array $configs): array + public static function decide(array $row, array $configs, array $excludedWorkOrders = []): array { + $exclusion = $excludedWorkOrders[(string) ($row['work_order_no'] ?? '')] ?? null; + if (is_string($exclusion) && trim($exclusion) !== '') { + return self::result('', self::STATUS_SKIP, '财务确认不记账:' . trim($exclusion)); + } $level1 = (string) ($row['reason_level_1'] ?? ''); $level2 = (string) ($row['reason_level_2'] ?? ''); $date = substr((string) ($row['started_at'] ?? ''), 0, 10); diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingProductResolver.php b/modules/EcommerceFinance/Services/SupplierBookkeepingProductResolver.php index 23703b570..38deee4ed 100644 --- a/modules/EcommerceFinance/Services/SupplierBookkeepingProductResolver.php +++ b/modules/EcommerceFinance/Services/SupplierBookkeepingProductResolver.php @@ -90,11 +90,22 @@ class SupplierBookkeepingProductResolver /** 仅识别完整的“ZH-/MKH- 商品编码-数量”结构,组件仍须逐个命中普通商品资料。 */ private static function encodedComponents(string $code): array + { + return array_keys(self::encodedQuantities($code)); + } + + /** 完整组合编码中明确给出的逐商品件数;不猜测普通商品内部连字符的含义。 */ + public static function encodedQuantities(string $code): array { if (!preg_match('/^(?:ZH-|MKH-)(.+)$/u', $code, $prefix)) return []; preg_match_all('/(.+?)-([1-9]\d*)(?:-|$)/u', $prefix[1], $matches, PREG_SET_ORDER); if (count($matches) < 2 || implode('', array_column($matches, 0)) !== $prefix[1]) return []; - return array_values(array_unique(array_map(static fn ($match) => Shipping::sku($match[1]), $matches))); + $quantities = []; + foreach ($matches as $match) { + $sku = Shipping::sku($match[1]); + $quantities[$sku] = bcadd($quantities[$sku] ?? '0', $match[2], 6); + } + return $quantities; } private function shippedComponents(array $row, array $shipments): array diff --git a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php index d1ebc1388..368a7a24c 100644 --- a/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php +++ b/modules/EcommerceFinance/Services/SupplierBookkeepingValuation.php @@ -66,7 +66,7 @@ class SupplierBookkeepingValuation $missingMonth = Repository::missingMonth($row, $readyMonths); $tracking = Repository::numbers($row['tracking_no'] ?? ''); $orders = Repository::numbers($row['online_order_no'] ?? ''); - if ($missingMonth !== null && $tracking === []) return $this->review($row, '六个月发货文件尚未补齐:' . $missingMonth); + if ($missingMonth !== null && $tracking === []) return $this->valueFromWorkOrder($row, $costs, $missingMonth); $pool = []; foreach ($tracking !== [] ? $tracking : $orders as $number) { foreach (($tracking !== [] ? $byTracking : $byOrder)[$number] ?? [] as $shipment) { @@ -77,12 +77,11 @@ class SupplierBookkeepingValuation $pool[$shipment['id']] = $shipment; } } - if ($pool === []) return $this->review($row, $missingMonth === null - ? '六个月内未匹配到原订单/快递单号的发货记录' - : '六个月发货文件尚未补齐:' . $missingMonth . ';同步发货明细未匹配到原快递单号'); + if ($pool === []) return $missingMonth !== null + ? $this->valueFromWorkOrder($row, $costs, $missingMonth) + : $this->review($row, '六个月内未匹配到原订单/快递单号的发货记录'); $unshippedPackages = []; foreach ($pool as $shipment) { - if (isset($shipment['valuation_error'])) return $this->review($row, $shipment['valuation_error']); $flag = trim((string) ($shipment['unshipped'] ?? '')); // 工作平台导出的查找辅助列:#N/A 未命中名单,返回单号则确实未发出。 if ($flag === '' || $flag === '#N/A') continue; @@ -91,9 +90,16 @@ class SupplierBookkeepingValuation } if ($unshippedPackages !== []) { $pool = array_filter($pool, static fn ($shipment) => !isset($unshippedPackages[(string) $shipment['source_id']])); - if ($pool === []) return $this->review($row, '原单标记未发出,已排除核价,不计厂家扣款'); + if ($pool === []) { + $row['deduction_status'] = SupplierBookkeepingDeductionRule::STATUS_SKIP; + $row['deduction_basis'] .= ';原单标记未发出,不计厂家扣款'; + return $row; + } $row['deduction_basis'] .= ';已排除未发出包裹'; } + foreach ($pool as $shipment) { + if (isset($shipment['valuation_error'])) return $this->review($row, $shipment['valuation_error']); + } $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) !== []) { @@ -110,6 +116,21 @@ class SupplierBookkeepingValuation $freightOnly = !$costOnly && $row['bookkeeping_type'] === '运费损耗'; $lines = []; if (!$freightOnly && $wanted === []) return $this->review($row, '工单没有商品编码,无法匹配成本'); + if (!$freightOnly) { + // 同一快递号可对应不同发货单;仅核算包含工单商品的发货单,不借用同号配件单的运费。 + $relevantPackages = []; + foreach ($pool as $shipment) { + if (array_intersect(array_keys($wanted), array_map([Repository::class, 'sku'], Repository::numbers($shipment['barcode']))) !== []) { + $relevantPackages[(string) $shipment['source_id']] = true; + } + } + if ($relevantPackages !== []) { + $pool = array_filter($pool, static fn ($shipment) => isset($relevantPackages[(string) $shipment['source_id']])); + $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) !== []) return $this->review($row, '原单号包含未匹配工单商品的包裹,暂不能按部分金额记账'); + } + } foreach ($pool as $shipment) { if ($shipment['ship_time'] === null) return $this->review($row, '匹配发货日期无效,请核对'); if (trim((string) $shipment['express_no']) === '' || count(Repository::numbers($shipment['express_no'])) !== 1) return $this->review($row, '包裹快递单号缺失或不唯一,请核对'); @@ -212,6 +233,61 @@ class SupplierBookkeepingValuation return $row; } + /** 仅在缺月文件且未匹配到原单时,按明确的工单商品数量核价;不采用部分发货记录。 */ + private function valueFromWorkOrder(array $row, array $costs, string $missingMonth): array + { + $prefix = '六个月发货文件尚未补齐:' . $missingMonth; + if (!in_array($row['bookkeeping_type'], ['补偿款项', '记账全款', '记账全额'], true)) return $this->review($row, $prefix); + $tracking = Repository::numbers($row['tracking_no'] ?? ''); + try { + $unshipped = $this->repository->unshippedTracking($tracking); + } catch (Throwable $exception) { + report($exception); + return $this->review($row, $prefix . ';未发出名单暂不可读取'); + } + $hits = array_intersect($tracking, $unshipped); + if ($hits !== []) { + if (count($hits) !== count($tracking)) return $this->review($row, '原单号包含未发出包裹,暂不能按部分金额记账'); + $row['deduction_status'] = SupplierBookkeepingDeductionRule::STATUS_SKIP; + $row['deduction_basis'] .= ';原单标记未发出,不计厂家扣款'; + return $row; + } + $codes = array_map([Repository::class, 'sku'], preg_split('/[\r\n,,、;;]+/u', trim((string) ($row['product_code'] ?? ''))) ?: []); + $quantities = preg_split('/[\r\n,,、;;]+/u', trim((string) ($row['product_quantity'] ?? ''))) ?: []; + if ($codes === [] || count($codes) !== count($quantities)) return $this->review($row, $prefix . ';工单缺少逐商品数量'); + $wanted = array_map([Repository::class, 'sku'], Repository::numbers(($row['resolved_product_code'] ?? '') ?: ($row['product_code'] ?? ''))); + $counted = []; $cost = '0'; $quantity = '0'; + foreach ($codes as $index => $code) { + $qty = trim($quantities[$index]); + if ($code === '' || !self::decimal($qty) || bccomp($qty, '0', 6) <= 0) return $this->review($row, $prefix . ';工单商品数量无效'); + $parts = in_array($code, $wanted, true) ? [$code => '1'] : SupplierBookkeepingProductResolver::encodedQuantities($code); + if ($parts === [] || array_diff(array_keys($parts), $wanted) !== []) return $this->review($row, $prefix . ';无法还原工单逐商品数量'); + foreach ($parts as $sku => $units) { + $price = $costs[$sku] ?? null; + if (!self::decimal($price) || bccomp($price, '0', 6) <= 0) return $this->review($row, $prefix . ';商品成本价缺失或为0:' . $sku); + $lineQuantity = bcmul($qty, $units, 6); + $quantity = bcadd($quantity, $lineQuantity, 6); + $cost = bcadd($cost, bcmul($lineQuantity, $price, 6), 6); + $counted[$sku] = true; + } + } + if (array_diff($wanted, array_keys($counted)) !== []) return $this->review($row, $prefix . ';未匹配全部工单商品'); + $costOnly = self::costOnlyMode($row); + if ($costOnly === null) return $this->review($row, '多个供应商中含只记成本价的' . implode('、', self::COST_ONLY_SUPPLIERS) . ',请人工确认'); + $freight = $costOnly ? '0' : bcmul($cost, '0.2', 6); + $ceiling = self::money(bcadd($cost, $freight, 6)); + if ($costOnly || bccomp($row['amount'], $ceiling, 2) > 0) $row['amount'] = $ceiling; + $row['matched_cost'] = self::money($cost); + $row['matched_freight'] = $costOnly ? null : self::money($freight); + $row['matched_quantity'] = rtrim(rtrim($quantity, '0'), '.'); + $basis = $prefix . ';按工单逐商品数量×商品资料成本核价;' . ($costOnly + ? '沐野竹木只记成本价,不含运费' + : '原单运费缺失或为0,按商品成本×0.2替代运费;按成本+运费与原记账金额取小值'); + $row['deduction_basis'] .= ';' . $basis; + $row['remark'] = trim((string) ($row['remark'] ?? '')) === '' ? $basis : $row['remark'] . "\n" . $basis; + return $row; + } + /** true=全部为只记成本价供应商;false=不含;null=与其他供应商混合,需人工确认。 */ private static function costOnlyMode(array $row): ?bool { diff --git a/modules/EcommerceFinance/View/SupplierBookkeepingPageData.php b/modules/EcommerceFinance/View/SupplierBookkeepingPageData.php index ba9335d2f..91b029b26 100644 --- a/modules/EcommerceFinance/View/SupplierBookkeepingPageData.php +++ b/modules/EcommerceFinance/View/SupplierBookkeepingPageData.php @@ -107,6 +107,8 @@ class SupplierBookkeepingPageData $to = $filters['started_to'] !== '' ? $filters['started_to'] . ' 23:59:59' : ''; $deduction = $filters['deduction'] ?? ''; $configs = $this->configs->activeConfigs(); + $excludedWorkOrders = setting('supplier_bookkeeping.excluded_work_orders', []); + $excludedWorkOrders = is_array($excludedWorkOrders) ? $excludedWorkOrders : []; $matched = []; foreach ($rows ?? $this->repository->rows() as $row) { $row['original_amount'] = $row['amount']; @@ -121,7 +123,7 @@ class SupplierBookkeepingPageData $resolved = []; foreach ($this->products->apply($matched) as $row) { // 先补全供应商,再按当前配置及既定金额顺序判断;不缓存判定。 - $row = Rule::decide($row, $configs) + $row; + $row = Rule::decide($row, $configs, $excludedWorkOrders) + $row; if ($row['supplier_match_basis'] !== '') $row['deduction_basis'] = $row['supplier_match_basis'] . ';' . $row['deduction_basis']; foreach (self::TEXT_FILTERS as $key => $label) { $value = (string) ($row[$key] ?? ''); diff --git a/tests/Feature/Business/SupplierBookkeepingPageTest.php b/tests/Feature/Business/SupplierBookkeepingPageTest.php index 546a15468..56c1f42b5 100644 --- a/tests/Feature/Business/SupplierBookkeepingPageTest.php +++ b/tests/Feature/Business/SupplierBookkeepingPageTest.php @@ -286,6 +286,43 @@ class SupplierBookkeepingPageTest extends FeatureTestCase $this->assertStringNotContainsString('GD-MIXED', $csv); } + public function test_finance_confirmed_exclusion_is_shared_by_totals_filters_and_export(): void + { + setting()->set('supplier_bookkeeping.excluded_work_orders', ['GD-EXCLUDED' => '财务确认无需扣款']); + $this->mockRepository([ + $this->row(['work_order_no' => 'GD-EXCLUDED', 'supplier' => '新厂', 'amount' => '100.00']), + $this->row(['work_order_no' => 'GD-NORMAL', 'supplier' => '新厂', 'amount' => '60.00']), + ]); + $this->loginAs()->get($this->url(['deduction' => '不扣款']))->assertOk()->assertSee('GD-EXCLUDED')->assertDontSee('GD-NORMAL')->assertSee('财务确认不记账'); + $this->get($this->url(['deduction' => '扣款']))->assertOk()->assertSee('GD-NORMAL')->assertDontSee('GD-EXCLUDED'); + $csv = $this->get(route('business.supplier-bookkeeping.export', ['company_id' => $this->company->id, 'deduction' => '扣款', + 'columns' => 'work_order_no,amount']))->assertOk()->streamedContent(); + $this->assertStringContainsString('GD-NORMAL,60.00', $csv); + $this->assertStringNotContainsString('GD-EXCLUDED', $csv); + } + + public function test_missing_month_work_order_cost_and_unshipped_results_are_shared_with_export(): void + { + $this->mockRepository([ + $this->row(['work_order_no' => 'GD-COST', 'supplier' => '新厂', 'product_code' => 'SKU-A', 'product_quantity' => '2', 'amount' => '100.00', 'tracking_no' => 'MISSING']), + $this->row(['work_order_no' => 'GD-UNSHIPPED', 'supplier' => '新厂', 'product_code' => 'SKU-A', 'product_quantity' => '2', 'amount' => '100.00', 'tracking_no' => 'UNSHIPPED']), + ], 'ready', true); + $shipping = \Mockery::mock(\Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository::class); + $shipping->shouldReceive('readyMonths')->andReturn([]); + $shipping->shouldReceive('shipmentsFor')->andReturn([]); + $shipping->shouldReceive('unshippedTracking')->andReturnUsing(static fn ($numbers) => array_values(array_intersect($numbers, ['UNSHIPPED']))); + $shipping->shouldReceive('costsFor')->andReturn(['SKU-A' => '20.00']); + $this->app->instance(\Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository::class, $shipping); + $this->loginAs()->get($this->url(['deduction' => '扣款']))->assertOk()->assertSee('GD-COST')->assertDontSee('GD-UNSHIPPED'); + $summary = app(\Modules\EcommerceFinance\View\SupplierBookkeepingPageData::class)->page()['bookkeepingSummary']; + $this->assertSame(4800, $summary['deduction_cents']); + $this->get($this->url(['deduction' => '不扣款']))->assertOk()->assertSee('GD-UNSHIPPED')->assertDontSee('GD-COST'); + $csv = $this->get(route('business.supplier-bookkeeping.export', ['company_id' => $this->company->id, 'deduction' => '扣款', + 'columns' => 'work_order_no,amount,matched_cost,matched_freight']))->assertOk()->streamedContent(); + $this->assertStringContainsString('GD-COST,48.00,40.00,8.00', $csv); + $this->assertStringNotContainsString('GD-UNSHIPPED', $csv); + } + public function test_missing_month_uses_synced_parcels_for_page_totals_filters_and_export(): void { $this->mockRepository([ diff --git a/tests/Unit/EcommerceFinance/SupplierBookkeepingDeductionRuleTest.php b/tests/Unit/EcommerceFinance/SupplierBookkeepingDeductionRuleTest.php index 0245941ab..b811f4ec4 100644 --- a/tests/Unit/EcommerceFinance/SupplierBookkeepingDeductionRuleTest.php +++ b/tests/Unit/EcommerceFinance/SupplierBookkeepingDeductionRuleTest.php @@ -21,6 +21,17 @@ class SupplierBookkeepingDeductionRuleTest extends TestCase $this->assertSame(Rule::MODE_DROPSHIP_WRONG, Rule::parseRuleText('仅代发仓发错货记账')['mode']); } + public function test_finance_exclusion_applies_only_to_the_exact_work_order(): void + { + $exclusions = ['GD-EXCLUDED' => '未匹配供应商,财务确认不记账']; + $row = $this->row(['work_order_no' => 'GD-EXCLUDED', 'supplier' => '', 'amount' => '100.00']); + $result = Rule::decide($row, $this->configs(), $exclusions); + $this->assertSame(Rule::STATUS_SKIP, $result['deduction_status']); + $this->assertStringContainsString('财务确认不记账', $result['deduction_basis']); + $this->assertSame(Rule::STATUS_REVIEW, Rule::decide(array_replace($row, ['work_order_no' => 'GD-OTHER']), $this->configs(), $exclusions)['deduction_status']); + $this->assertSame(Rule::STATUS_REVIEW, Rule::decide($row, $this->configs(), ['GD-EXCLUDED' => ''])['deduction_status']); + } + public function test_rejects_unknown_rule_text(): void { $this->expectException(InvalidArgumentException::class); diff --git a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php index e095745b5..da1df8fd4 100644 --- a/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php +++ b/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationTest.php @@ -288,6 +288,49 @@ class SupplierBookkeepingValuationTest extends TestCase $this->assertSame('100.00', $service->apply([$this->row()])[0]['amount']); } + public function test_missing_month_without_shipment_uses_complete_work_order_quantities_and_cost_ceiling(): void + { + $repository = $this->createMock(Repository::class); + $repository->method('readyMonths')->willReturn([]); + $repository->method('shipmentsFor')->willReturn([]); + $repository->method('unshippedTracking')->willReturn([]); + $repository->method('costsFor')->willReturn(['SKU-A' => '20.00', 'SKU-B' => '5.00']); + $service = new SupplierBookkeepingValuation($repository); + $cases = [ + ['product_code' => 'SKU-A', 'product_quantity' => '2'], + ['product_code' => "SKU-A\nSKU-A", 'product_quantity' => "1\n1"], + ['product_code' => 'ZH-SKU-A-2-SKU-B-1', 'resolved_product_code' => "SKU-A\nSKU-B", 'product_quantity' => '1'], + ['product_code' => "SKU-A\nSKU-B", 'product_quantity' => "2\n3"], + ]; + foreach ($cases as $i => $case) { + $row = $service->apply([$this->row($case)])[0]; + $this->assertSame(['48.00', '48.00', '54.00', '66.00'][$i], $row['amount']); + $this->assertSame('扣款', $row['deduction_status']); + $this->assertStringContainsString('按工单逐商品数量', $row['deduction_basis']); + } + $lower = $service->apply([$this->row($cases[0] + ['amount' => '42.00'])])[0]; + $this->assertSame('42.00', $lower['amount']); + foreach ([['product_quantity' => ''], ['product_quantity' => '0'], ['product_code' => "SKU-A\nSKU-B", 'product_quantity' => '1'], + ['product_code' => 'MISSING', 'product_quantity' => '1']] as $patch) { + $row = $service->apply([$this->row($patch)])[0]; + $this->assertSame('待确认', $row['deduction_status']); + $this->assertSame('100.00', $row['amount']); + } + } + + public function test_missing_month_work_order_cost_does_not_override_unshipped_or_partial_shipments(): void + { + $repository = $this->createMock(Repository::class); + $repository->method('readyMonths')->willReturn([]); + $repository->method('shipmentsFor')->willReturn([]); + $repository->method('unshippedTracking')->willReturn(['EXP-1']); + $repository->method('costsFor')->willReturn(['SKU-A' => '20.00']); + $service = new SupplierBookkeepingValuation($repository); + $this->assertSame('不扣款', $service->apply([$this->row(['product_quantity' => '1'])])[0]['deduction_status']); + $partial = $service->apply([$this->row(['product_quantity' => '1', 'tracking_no' => 'EXP-1,EXP-2'])])[0]; + $this->assertSame('待确认', $partial['deduction_status']); + } + public function test_incomplete_synced_package_is_pending_even_when_the_wanted_sku_exists(): void { $repository = $this->createMock(Repository::class); @@ -333,13 +376,39 @@ class SupplierBookkeepingValuationTest extends TestCase 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($tracking === 'EXP-2' ? '不扣款' : '待确认', $row['deduction_status']); $this->assertSame('100.00', $row['amount']); $this->assertStringContainsString('未发出', $row['deduction_basis']); } } } + public function test_unshipped_is_skipped_even_when_its_product_detail_is_incomplete(): void + { + $row = $this->service([$this->shipment(['unshipped' => 'EXP-1', 'valuation_error' => '同步包裹商品明细尚未齐全'])]) + ->apply([$this->row()])[0]; + $this->assertSame('不扣款', $row['deduction_status']); + $this->assertSame('100.00', $row['amount']); + } + + public function test_full_payment_costs_repeated_work_order_sku_by_actual_quantity_without_unrelated_source(): void + { + $service = $this->service([ + $this->shipment(['qty' => 2, 'freight' => '0']), + $this->shipment(['id' => 2, 'source_id' => 20, 'barcode' => 'ACCESSORY', 'qty' => 1, 'freight' => '0']), + ]); + $row = $service->apply([$this->row(['bookkeeping_type' => '记账全款', 'product_code' => "SKU-A\nSKU-A"])])[0]; + $this->assertSame('40.00', $row['matched_cost']); + $this->assertSame('8.00', $row['matched_freight']); + $this->assertSame('48.00', $row['amount']); + $this->assertSame('2', $row['matched_quantity']); + $this->assertSame('扣款', $row['deduction_status']); + $partial = $service->apply([$this->row(['tracking_no' => 'EXP-1,EXP-2'])])[0]; + $this->assertSame('待确认', $partial['deduction_status']); + $mismatch = $service->apply([$this->row(['product_code' => 'SKU-A,MISSING'])])[0]; + $this->assertSame('待确认', $mismatch['deduction_status']); + } + 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];