diff --git a/modules/EcommerceFinance/Services/PlantingLossCalculator.php b/modules/EcommerceFinance/Services/PlantingLossCalculator.php index 4eb94ad16..fbc6967cd 100644 --- a/modules/EcommerceFinance/Services/PlantingLossCalculator.php +++ b/modules/EcommerceFinance/Services/PlantingLossCalculator.php @@ -16,6 +16,7 @@ use Modules\EcommerceFinance\Repositories\ShippingOrderRepository; * 2. 用线上订单号去匹配发货数据(jst_oms_shipping)中 M-1、M 两个月发货的交易单号,匹配到的保留; * 3. 任务备注或卖家备注写了“拍A发B”或“拍A发A”的不扣款,不论是否匹配到发货都单独列出发给运营; * 4. 实际只发了「信誉订单虚拟条码」「透明挂钩」这类商品的不扣款; + * XF001-JX、DF- 开头条码仅免对应商品,混合发货的普通商品仍计算扣款; * 5. 其余匹配到的作为扣款项,扣款金额 = Σ 发货商品成本价 × 数量 + 该订单在发货数据中的运费(2026-10-19 更正), * 成本价按发货条形码(=商品编码)取普通商品资料。未匹配到发货的不计入种菜损耗。 */ @@ -86,13 +87,13 @@ class PlantingLossCalculator return substr($window['from'], 0, 7) . '~' . $month; } - /** 命中的免扣字样(拍A发B / 拍A发A),未命中返回 null。全角、大小写、空格均兼容。 */ + /** 命中的免扣字样(拍A发B / 拍A发A / 拍A+B),未命中返回 null。全角、大小写、空格均兼容。 */ public static function exemptionKeyword(string ...$remarks): ?string { foreach ($remarks as $remark) { $text = mb_strtoupper((string) preg_replace('/\s+/u', '', mb_convert_kana($remark, 'as'))); - if (preg_match('/拍A发([AB])/u', $text, $match) === 1) { - return '拍A发' . $match[1]; + if (preg_match('/拍A发[AB]|拍A\+B(?![A-Z0-9])/u', $text, $match) === 1) { + return $match[0]; } } @@ -101,6 +102,9 @@ class PlantingLossCalculator public static function isFreeGoods(string $barcode, string $productName): bool { + if (self::isExemptBarcode($barcode)) { + return true; + } foreach (self::FREE_GOODS_KEYWORDS as $keyword) { if (str_contains($productName, $keyword) || str_contains($barcode, $keyword)) { return true; @@ -110,6 +114,14 @@ class PlantingLossCalculator return false; } + /** 只免该商品,不触发虚拟条码的整单免扣规则。 */ + public static function isExemptBarcode(string $barcode): bool + { + $code = strtoupper(trim($barcode)); + + return $code === 'XF001-JX' || str_starts_with($code, 'DF-'); + } + public static function isVirtualOrderItem(string $barcode, string $productName): bool { $code = strtoupper(trim($barcode)); @@ -139,7 +151,7 @@ class PlantingLossCalculator */ public function rows(string $month): array { - $key = 'planting-loss:rows:v3:' . $month; + $key = 'planting-loss:rows:v4:' . $month; $cached = Cache::get($key); if (is_array($cached)) { return $cached; @@ -178,6 +190,7 @@ class PlantingLossCalculator $deductLines = []; $missingCost = []; $cents = 0; + $exemptBarcodes = []; $virtualOrder = false; foreach ($shipment['lines'] ?? [] as $line) { if (self::isVirtualOrderItem($line['barcode'], $line['product_name'])) { @@ -186,6 +199,9 @@ class PlantingLossCalculator } foreach ($virtualOrder ? [] : ($shipment['lines'] ?? []) as $line) { if (self::isFreeGoods($line['barcode'], $line['product_name'])) { + if (self::isExemptBarcode($line['barcode'])) { + $exemptBarcodes[$line['barcode']] = true; + } continue; } $cost = $costs[$line['barcode']] ?? null; @@ -215,14 +231,19 @@ class PlantingLossCalculator // 拍A发B/拍A发A 不论是否匹配到发货都单独列出发给运营(2026-10-20 财务确认);匹配发货只用于挑出真实发货的单。 $keyword = self::exemptionKeyword((string) $row['task_remark'], (string) $row['seller_remark']); + $exemptionBasis = '备注含“' . $keyword . '”' . ($keyword === '拍A+B' ? '(按拍A发B处理)' : '') + . ($shipment === null ? '(未匹配到发货)' : ''); [$result, $basis] = match (true) { - $keyword !== null => [self::RESULT_EXEMPT, '备注含“' . $keyword . '”' . ($shipment === null ? '(未匹配到发货)' : '')], + $keyword !== null => [self::RESULT_EXEMPT, $exemptionBasis], $shipment === null => [self::RESULT_UNMATCHED, self::shippingLabel($month) . ' 发货数据中无此交易单号'], $virtualOrder => [self::RESULT_FREE_GOODS, '发货含信誉订单虚拟条码/透明挂钩,整单不扣款'], - $deductLines === [] => [self::RESULT_FREE_GOODS, '只发了虚拟条码/透明挂钩/无实物商品'], + $deductLines === [] => [self::RESULT_FREE_GOODS, $exemptBarcodes !== [] ? '只发了免扣条码/无实物商品' : '只发了虚拟条码/透明挂钩/无实物商品'], $missingText !== '' => [self::RESULT_DEDUCT, $missingText . ',扣款金额未含缺失部分'], default => [self::RESULT_DEDUCT, '已匹配发货,按成本价×数量+运费扣款'], }; + if ($exemptBarcodes !== [] && $keyword === null && ! $virtualOrder) { + $basis .= ';免扣条码:' . implode('、', array_keys($exemptBarcodes)); + } $row['loss_result'] = $result; $row['loss_basis'] = $basis; diff --git a/resources/views/business/pages/billing-transactions/planting-loss.blade.php b/resources/views/business/pages/billing-transactions/planting-loss.blade.php index 5eaaf4ac1..398eb1eda 100644 --- a/resources/views/business/pages/billing-transactions/planting-loss.blade.php +++ b/resources/views/business/pages/billing-transactions/planting-loss.blade.php @@ -13,7 +13,7 @@ @endphp
-

{{ $plantingFilters['month'] }} 月种菜报销明细(完成时间 {{ substr($plantingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingWindow['to'])->subDay()->format('Y-m-d') }})的线上订单号,匹配 {{ $shippingLabel }} 发货数据(发货时间 {{ substr($plantingShippingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingShippingWindow['to'])->subDay()->format('Y-m-d') }})中的交易单号。任务备注或卖家备注写了“拍A发B / 拍A发A”的(不论是否匹配到发货)、以及匹配到的单中只发了信誉订单虚拟条码/透明挂钩的不扣款,其余按发货商品成本价×数量+该单运费扣款。

+

{{ $plantingFilters['month'] }} 月种菜报销明细(完成时间 {{ substr($plantingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingWindow['to'])->subDay()->format('Y-m-d') }})的线上订单号,匹配 {{ $shippingLabel }} 发货数据(发货时间 {{ substr($plantingShippingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingShippingWindow['to'])->subDay()->format('Y-m-d') }})中的交易单号。任务备注或卖家备注写了“拍A发B / 拍A发A / 拍A+B”的(不论是否匹配到发货)、以及匹配到的单中只发了信誉订单虚拟条码/透明挂钩的不扣款,XF001-JX、DF- 开头条码仅免对应商品,混合发货的普通商品继续扣款;其余按发货商品成本价×数量+该单运费扣款。

{{ $syncLabels[$plantingSync['code']] ?? $plantingSync['code'] }}@if($plantingSync['last_succeeded_at']) · {{ \Illuminate\Support\Str::of($plantingSync['last_succeeded_at'])->substr(0, 16) }}@endif {{ $shippingLabels[$plantingShippingSync['code'] ?? 'unavailable'] ?? $plantingShippingSync['code'] }}@if($plantingShippingSync['last_succeeded_at'] ?? null) · {{ \Illuminate\Support\Str::of($plantingShippingSync['last_succeeded_at'])->substr(0, 16) }}@endif @@ -39,10 +39,10 @@
当月种菜报销{{ number_format($plantingSummary['total']) }}匹配到发货 {{ number_format($plantingSummary['matched']) }} 单
扣款金额(元){{ $money($deduct['deduction_amount']) }}扣款 {{ number_format($deduct['count']) }} 单@if($deduct['cost_missing'] > 0) · {{ number_format($deduct['cost_missing']) }} 单成本价/运费缺失@endif 不扣款(拍A发B/拍A发A){{ number_format($exempt['count']) }}需发运营 · 最终报销 {{ $money($exempt['reimbursement_amount']) }} - 不扣款(虚拟条码/透明挂钩){{ number_format($free['count']) }}最终报销 {{ $money($free['reimbursement_amount']) }} + 不扣款(虚拟条码/透明挂钩等){{ number_format($free['count']) }}最终报销 {{ $money($free['reimbursement_amount']) }} 未匹配发货(不计入){{ number_format($unmatched['count']) }}最终报销 {{ $money($unmatched['reimbursement_amount']) }}
-
计算口径

① 取该月种菜报销明细:工单类型“种菜报销”、状态“已完成”,完成时间 M 月 2 日 00:00 至次月 1 日 23:59:59(与「种菜报销」页同一口径)。② 用线上订单号(交易单号)精确匹配「快递应付 → 发货数据」中发货时间在上月 1 日至本月末的交易单号,匹配到的保留,同一单多个包裹/商品合并为一行。③ 任务备注或卖家备注写了“拍A发B”或“拍A发A”(不区分全半角、大小写和空格)的不做扣款,不论是否匹配到发货都单独列出,「导出拍A发B(发运营)」按种菜报销字段导出。④ 发货数据里该交易单号下出现“信誉订单虚拟条码”(X00006/X00002)或透明挂钩(条形码 GG 开头)的整单不扣款(同单号下的其它商品视为合并发货的别的订单);只发了“无实物”商品的也不扣款。⑤ 其余匹配到的为扣款项:扣款金额 = Σ 发货条形码(商品编码)在「普通商品资料」中的成本价 × 发货数量 + 该订单在「发货数据」中的运费合计;普通商品资料里没有成本价(空或 0)的商品、发货数据里运费为空的订单分别标为“成本价缺失”“运费缺失”,缺失部分不计入扣款金额,补齐后自动重算。未匹配到发货的单不计入种菜损耗,仅供核对。

+
计算口径

① 取该月种菜报销明细:工单类型“种菜报销”、状态“已完成”,完成时间 M 月 2 日 00:00 至次月 1 日 23:59:59(与「种菜报销」页同一口径)。② 用线上订单号(交易单号)精确匹配「快递应付 → 发货数据」中发货时间在上月 1 日至本月末的交易单号,匹配到的保留,同一单多个包裹/商品合并为一行。③ 任务备注或卖家备注写了“拍A发B”“拍A发A”或“拍A+B”(按拍A发B处理,不区分全半角、大小写和空格)的不做扣款,不论是否匹配到发货都单独列出,「导出拍A发B(发运营)」按种菜报销字段导出。④ 发货数据里该交易单号下出现“信誉订单虚拟条码”(X00006/X00002)或透明挂钩(条形码 GG 开头)的整单不扣款(同单号下的其它商品视为合并发货的别的订单);只发了“无实物”商品的也不扣款。⑤ 发货条码 XF001-JX 和 DF- 开头条码免对应商品成本(不区分大小写),混合发货的普通商品继续按成本价和既有订单运费计算;仅有免扣商品的整单不扣款。⑥ 其余匹配到的为扣款项:扣款金额 = Σ 发货条形码(商品编码)在「普通商品资料」中的成本价 × 发货数量 + 该订单在「发货数据」中的运费合计;普通商品资料里没有成本价(空或 0)的商品、发货数据里运费为空的订单分别标为“成本价缺失”“运费缺失”,缺失部分不计入扣款金额,补齐后自动重算。未匹配到发货的单不计入种菜损耗,仅供核对。

@if(($plantingShippingSync['code'] ?? '') !== 'succeeded' && ! $plantingError)
发货数据当前状态为“{{ $shippingLabels[$plantingShippingSync['code'] ?? 'unavailable'] ?? $plantingShippingSync['code'] }}”{{ ($plantingShippingSync['warning'] ?? '') !== '' ? ':' . $plantingShippingSync['warning'] : '' }},匹配结果可能不完整。
@endif @if($plantingError)@endif
diff --git a/tests/Feature/Business/PlantingWorkOrderPagesTest.php b/tests/Feature/Business/PlantingWorkOrderPagesTest.php index 2353c5d1f..aff4b8453 100644 --- a/tests/Feature/Business/PlantingWorkOrderPagesTest.php +++ b/tests/Feature/Business/PlantingWorkOrderPagesTest.php @@ -103,7 +103,7 @@ class PlantingWorkOrderPagesTest extends FeatureTestCase { $this->mockData([ $this->row(), - $this->row(['work_order_no' => 'GD-EXEMPT', 'task_remark' => '拍A发B', 'loss_result' => Calculator::RESULT_EXEMPT]), + $this->row(['work_order_no' => 'GD-EXEMPT', 'task_remark' => '拍A+B+DF-A3ZP001501', 'loss_result' => Calculator::RESULT_EXEMPT]), ]); $response = $this->loginAs()->get(route('business.planting-data.planting-loss.export', [ @@ -114,7 +114,7 @@ class PlantingWorkOrderPagesTest extends FeatureTestCase $response->assertOk(); $csv = $response->streamedContent(); $this->assertStringContainsString('工单编号,线上订单号,任务备注,损耗判定', $csv); - $this->assertStringContainsString("GD-EXEMPT,'3316392626048007769,拍A发B", $csv); + $this->assertStringContainsString("GD-EXEMPT,'3316392626048007769,拍A+B+DF-A3ZP001501", $csv); $this->assertStringNotContainsString('GD565967381', $csv); // 发运营:按种菜报销字段导出,忽略损耗页的列设置。 diff --git a/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php b/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php index 3025bd1d5..cdba295ba 100644 --- a/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php +++ b/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php @@ -143,11 +143,21 @@ class PlantingLossCalculatorTest extends TestCase $this->assertSame('拍A发B', Calculator::exemptionKeyword('拍A发B+GG003-SZ')); $this->assertSame('拍A发A', Calculator::exemptionKeyword('', '客户要求 拍a 发A')); $this->assertSame('拍A发B', Calculator::exemptionKeyword('CCDD', '拍a发b,供货价一致')); + $this->assertSame('拍A+B', Calculator::exemptionKeyword('拍A+B+DF-A3ZP001501')); + $this->assertSame('拍A+B', Calculator::exemptionKeyword('', '客户要求 拍a + b')); + $this->assertNull(Calculator::exemptionKeyword('拍A+BB', '拍A+B123')); $this->assertNull(Calculator::exemptionKeyword('AABB', '拍B发A')); $this->assertNull(Calculator::exemptionKeyword('转挂钩发货', '')); $this->assertTrue(Calculator::isFreeGoods('XN001', '信誉订单虚拟条码')); $this->assertTrue(Calculator::isFreeGoods('GG003-SZ', '透明挂钩3个')); $this->assertFalse(Calculator::isFreeGoods('DNZ-01', '儿童学习椅')); + $this->assertTrue(Calculator::isFreeGoods('xf001-jx', '普通名称')); + $this->assertTrue(Calculator::isFreeGoods(' df-A3ZP001501 ', '普通名称')); + $this->assertFalse(Calculator::isExemptBarcode('XF001-JX-OTHER')); + $this->assertFalse(Calculator::isExemptBarcode('ADF-123')); + $this->assertFalse(Calculator::isExemptBarcode('DF123')); + $this->assertFalse(Calculator::isVirtualOrderItem('XF001-JX', '普通名称')); + $this->assertFalse(Calculator::isVirtualOrderItem('DF-123', '普通名称')); $this->assertTrue(Calculator::isFreeGoods('tcxj', '书柜 晒图有奖励(无实物)')); $this->assertTrue(Calculator::isVirtualOrderItem('X00006', '')); $this->assertTrue(Calculator::isVirtualOrderItem('GG003-SZ', '透明挂钩3个')); @@ -236,4 +246,49 @@ class PlantingLossCalculatorTest extends TestCase $this->assertSame('GG003-SZ', $row['product_code']); $this->assertSame('2026-09-02 00:00:00', $row['completed_at']); } + + public function test_ab_shorthand_exempts_real_goods_even_when_freight_is_missing(): void + { + $this->workOrder(1, 'GD635802033', '3316534251275002174', '2026-11-05 11:22:44', [], '拍A+B+DF-A3ZP001501'); + $this->workOrder(2, 'GD-SELLER-AB', 'NO-SHIPMENT', '2026-11-06 10:00:00', [], '', '拍 a + b'); + $this->ship('3316534251275002174', '2026-10-28 10:00:00', 'YT001', 5.01, 'DF-A3ZP001501', '实物商品'); + DB::connection('business_mirror')->table('fixture_sku')->insert([ + 'autoid' => 5, 'sku_id' => 'DF-A3ZP001501', 'sku_type' => 'normal', 'cost_price' => 5.01, + ]); + DB::connection('planting_shipping')->table('jst_oms_shipping')->update(['freight' => null]); + + $rows = collect(app(Calculator::class)->rows('2026-11'))->keyBy('work_order_no'); + foreach ($rows as $row) { + $this->assertSame(Calculator::RESULT_EXEMPT, $row['loss_result']); + $this->assertNull($row['deduction_amount']); + $this->assertFalse($row['cost_missing']); + } + $this->assertCount(2, $rows); + $this->assertSame('备注含“拍A+B”(按拍A发B处理)', $rows['GD635802033']['loss_basis']); + $this->assertSame('备注含“拍A+B”(按拍A发B处理)(未匹配到发货)', $rows['GD-SELLER-AB']['loss_basis']); + } + + public function test_exempt_barcodes_skip_only_their_goods_in_mixed_shipments(): void + { + $this->workOrder(1, 'GD-ONLY-EXEMPT', 'ONLY-EXEMPT', '2026-11-05 11:22:44'); + $this->workOrder(2, 'GD-MIXED-EXEMPT', 'MIXED-EXEMPT', '2026-11-05 11:22:44'); + foreach (['ONLY-EXEMPT', 'MIXED-EXEMPT'] as $order) { + $this->ship($order, '2026-11-01 10:00:00', 'YT001', 20, 'xf001-jx', '普通商品'); + $this->ship($order, '2026-11-01 10:00:00', 'YT001', 30, 'df-123', '另一商品'); + } + // 免扣商品没有成本价也不应报缺失;混合订单保留普通商品成本与既有订单运费。 + $this->ship('MIXED-EXEMPT', '2026-11-01 10:00:00', 'YT001', 40.6, 'DNZ-01', '儿童学习椅', 2); + DB::connection('planting_shipping')->table('jst_oms_shipping')->update(['freight' => 0]); + DB::connection('planting_shipping')->table('jst_oms_shipping')->where('barcode', 'DNZ-01')->update(['freight' => 3.2]); + $rows = collect(app(Calculator::class)->rows('2026-11'))->keyBy('work_order_no'); + + $this->assertSame(Calculator::RESULT_FREE_GOODS, $rows['GD-ONLY-EXEMPT']['loss_result']); + $this->assertNull($rows['GD-ONLY-EXEMPT']['deduction_amount']); + $this->assertFalse($rows['GD-ONLY-EXEMPT']['cost_missing']); + $this->assertSame(Calculator::RESULT_DEDUCT, $rows['GD-MIXED-EXEMPT']['loss_result']); + $this->assertSame('204.20', $rows['GD-MIXED-EXEMPT']['deduction_amount']); + $this->assertSame("DNZ-01 × 2 × 100.5 = 201.00\n运费 3.20", $rows['GD-MIXED-EXEMPT']['deduction_detail']); + $this->assertFalse($rows['GD-MIXED-EXEMPT']['cost_missing']); + $this->assertStringContainsString('免扣条码:xf001-jx、df-123', $rows['GD-MIXED-EXEMPT']['loss_basis']); + } }