CaiWuXiTong/tests/Feature/Business/ShippingFreightServiceTest.php
limeng 1cf60dd7b0 feat(发货数据): 按报价管理的报价核算运费,结果显示在发货数据页面
从 2026-11-01 起,按发货日在报价管理里生效的报价版本(与报价管理「当前生效」同一判断:未作废、
已审核/签署生效、执行期间覆盖发货日,取执行日期最新)逐包裹核算运费,写入 jst_oms_shipping_freight。
- 结算重量:实称>线上>重量;按各快递计泡系数取 max(重量, 长×宽×高÷系数);免抛仓(含「配件」仓)不计泡、不收多件费;
  超长费计入,其他附加费不计;城市单独报价、整省「X元/公斤」、洞洞板 9000、个别版本系数等按 10 月逐单核对的口径;
- 计算引擎移植自 10 月离线核算脚本,10 月 29.5 万个包裹逐单比对结果一致(仅 x.xx5 边界按正确四舍五入进位);
- 发货数据同步完成后核算该窗口;报价版本新增/作废/签署归档后按该快递从执行日期起自动重算;
- 新命令 jushuitan:calc-freight --from --to [--carrier];
- 发货数据页面/导出新增「报价核算」列:核算运费、核算-聚水潭运费、报价版本、结算重量依据、核算结算重量、
  计费重量、首重、续重/费率、面单、一口价、多件/超长加收、核算公式、核算状态(合并口径每包裹一行,明细只在第一行)。

同时修复发货数据同步 11 月起频繁失败:副本库查发货视图超时后报 20047 DBPROCESS is dead,
原重试只识别 20003/dbnextrow,整次同步失败(11-17 只同步到 62%)。现把 20047 视为可重连重试,
发货视图每批出库单由 300 降到 100。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014nvGZkg27YMEdhry6eiuwf
2026-11-18 13:55:40 +08:00

104 lines
6.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Business;
use App\Models\Business\CourierQuoteChange;
use App\Models\Business\CourierQuoteVersion;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Repositories\ShippingOrderRepository;
use Modules\EcommerceFinance\Services\Freight\ShippingFreightService;
use Tests\Feature\FeatureTestCase;
class ShippingFreightServiceTest extends FeatureTestCase
{
public function test_packages_are_priced_by_the_quote_in_force_on_the_ship_date_and_shown_on_shipping_rows(): void
{
$this->version('QB-901', '新北中通', '2026-10-01', '2026-11-10', [['省份', '面单', '费率'], ['江苏', 3.5, 0.7]]);
$this->version('QB-902', '新北中通', '2026-11-11', null, [['省份', '面单', '费率'], ['江苏', 3.0, 0.6]]);
$db = DB::connection('business_mirror');
$row = static fn (int $sid, int $sku, string $time, array $extra = []) => $extra + [
'source_id' => $sid, 'sku_source_id' => $sku, 'express_no' => 'ZT' . $sid, 'express_name1' => '新北中通',
'warehouse_name' => '简尚-崔桥工厂仓', 'receiver_state' => '江苏省', 'receiver_city' => '南京市', 'ship_time' => $time,
'pkg_count' => '1/1', 'barcode' => 'SJ' . $sid . '-' . $sku, 'qty' => 1, 'freight' => 5,
'online_weight' => null, 'length_cm' => null, 'width_cm' => null, 'height_cm' => null,
];
$db->table('jst_oms_shipping')->insert([
// 两件商品一个包裹:重量求和 2+1=3kg,长宽高取最大 50×40×30 → 体积重 7.5kg
$row(1, 1, '2026-11-05 10:00:00', ['online_weight' => 2, 'length_cm' => 50, 'width_cm' => 40, 'height_cm' => 30]),
$row(1, 2, '2026-11-05 10:00:00', ['online_weight' => 1, 'length_cm' => 10, 'width_cm' => 10, 'height_cm' => 10, 'freight' => null]),
// 11-11 起用新报价;免抛仓不计泡
$row(2, 1, '2026-11-12 09:00:00', ['online_weight' => 4, 'length_cm' => 50, 'width_cm' => 40, 'height_cm' => 30, 'warehouse_name' => '常州简尚家居有限公司']),
// 核算起始日(11-01)以前不核算
$row(3, 1, '2026-10-30 09:00:00', ['online_weight' => 4]),
// 报价管理里没有的快递
$row(4, 1, '2026-11-12 09:00:00', ['online_weight' => 4, 'express_name1' => '某某快递']),
]);
$res = app(ShippingFreightService::class)->recalculate('2026-10-01', '2026-12-01');
$this->assertSame(['packages' => 3, 'ok' => 2], $res);
$r1 = $db->table('jst_oms_shipping_freight')->where('source_id', 1)->sole();
$this->assertSame('QB-901', $r1->quote_version);
$this->assertEquals(8.75, $r1->freight);
$this->assertEquals(7.5, $r1->settle_weight);
$this->assertEquals(5, $r1->jst_freight);
$r2 = $db->table('jst_oms_shipping_freight')->where('source_id', 2)->sole();
$this->assertSame('QB-902', $r2->quote_version);
$this->assertEquals(5.4, $r2->freight);
$this->assertSame(0, $db->table('jst_oms_shipping_freight')->where('source_id', 3)->count());
$this->assertSame('报价管理里没有「某某快递」的报价', $db->table('jst_oms_shipping_freight')->where('source_id', 4)->value('status'));
// 发货数据页面:合并口径每个包裹带核算列,明细口径只在包裹第一行
$attach = new \ReflectionMethod(ShippingOrderRepository::class, 'attachFreight');
$repo = app(ShippingOrderRepository::class);
$merged = $attach->invoke($repo, [['id' => 99, 'source_id' => 1]], false);
$this->assertEquals(8.75, $merged[0]['qc_freight']);
$this->assertEquals(3.75, $merged[0]['qc_diff']);
$this->assertSame('QB-901', $merged[0]['qc_quote_version']);
$this->assertSame('线上重量 3kg;体积重 7.5kg(÷8000)', $merged[0]['qc_basis']);
$ids = $db->table('jst_oms_shipping')->where('source_id', 1)->orderBy('id')->pluck('id')->all();
$detail = $attach->invoke($repo, [['id' => $ids[0], 'source_id' => 1], ['id' => $ids[1], 'source_id' => 1]], true);
$this->assertEquals(8.75, $detail[0]['qc_freight']);
$this->assertArrayNotHasKey('qc_freight', $detail[1]);
}
public function test_quote_changes_recalculate_that_carrier_from_the_effective_date(): void
{
DB::connection('business_mirror')->table('jst_oms_shipping')->insert([
'source_id' => 11, 'sku_source_id' => 1, 'express_no' => 'ZT11', 'express_name1' => '新北中通', 'warehouse_name' => '简尚-崔桥工厂仓',
'receiver_state' => '江苏省', 'ship_time' => '2026-11-16 09:00:00', 'pkg_count' => '1/1', 'online_weight' => 2,
]);
$this->version('QB-911', '新北中通', '2026-11-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
ShippingFreightService::flushPending();
$this->assertEquals(5.0, DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 11)->value('freight'));
// 补录 11-15 起的新报价:审核生效后自动按新报价重算
$this->version('QB-912', '新北中通', '2026-11-15', null, [['省份', '面单', '费率'], ['江苏', 4.0, 1.0]]);
ShippingFreightService::flushPending();
$row = DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 11)->sole();
$this->assertSame('QB-912', $row->quote_version);
$this->assertEquals(6.0, $row->freight);
}
private function version(string $code, string $name, string $from, ?string $to, array $vals): CourierQuoteVersion
{
$change = CourierQuoteChange::create([
'company_id' => $this->company->id, 'number' => 'BG-T-' . $code, 'source_quote_id' => $code,
'source_sheet_name' => $name, 'carrier_name' => $name, 'quote_type' => '标准快递报价', 'change_type' => '面单调整',
'status' => CourierQuoteChange::STATUS_APPROVED, 'effective_date' => $from, 'is_retrospective' => false,
'recalculation_status' => CourierQuoteChange::RECALCULATION_NOT_REQUIRED, 'summary' => '测试报价',
'created_by' => $this->user->id, 'submitted_by' => $this->user->id, 'submitted_at' => now(),
'approved_by' => $this->user->id, 'approved_at' => now(),
]);
return CourierQuoteVersion::create([
'company_id' => $this->company->id, 'version_code' => $code, 'source_quote_id' => $code, 'source_change_id' => $change->id,
'sheet_name' => $name, 'company_name' => $name, 'carrier_name' => $name, 'quote_type' => '标准快递报价',
'effective_date' => $from, 'end_date' => $to, 'source_range' => 'A1:C2',
'values_json' => json_encode($vals, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
'formulas_json' => json_encode([[]], JSON_THROW_ON_ERROR), 'status' => 'approved',
'signature_status' => '历史签署资料已归档', 'created_by' => $this->user->id, 'approved_by' => $this->user->id,
]);
}
}