CaiWuXiTong/tests/Feature/Business/ShippingFreightServiceTest.php
limeng f106ef988d feat(发货数据): 按报价管理的报价核算运费,结果显示在发货数据页面 (#1228)
从 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

> 开工时标题使用 `WIP: <任务名称>` 并先填写“任务占用与边界”。Codex 完成独立合并前自审后补齐其余内容并移除 `WIP:`。

## 任务占用与边界

- 任务名称:
- 实际开发者:
- 任务来源/团队分工:
- 目标结果:
- 预计修改路径:
- 将写入的公共契约(路由、Controller 方法、Repository、表/视图、`source_key`、`scope`、权限名、菜单键或共享组件):
- 仅复用、明确不修改的公共契约:
- 数据源、表/视图和字段:
- 上游 `source_key` / `scope`:
- 权限名、菜单键或共享组件:
- 明确不修改的范围:

## 开工碰撞检查

- [ ] Codex 已执行 `scripts/codex-task-preflight.ps1`
- [ ] 已检查最新 `main` 和所有远程任务分支
- [ ] 已搜索主线既有页面、接口、Repository 和数据查询
- [ ] 已把“将写入”和“仅复用、不修改”的公共契约分开;仅复用项不作为单独阻断条件
- [ ] 未发现实际写入重叠;或已在下方写明相关分支、具体重叠项和协调结论

相关分支、重叠项与协调结论:

## 实际改动

- 完成内容:
- Codex 实际修改的文件:
- 新增或变更的公共契约:
- 与原计划的差异:

## 验证证据

- 正常流程:
- 空数据/错误输入/无权限场景:
- 受影响旧功能:
- 自动测试及结果:
- 实际页面、接口或只读业务查询结果:

## 风险检查

- [ ] 不涉及金额、权限、审批、数据库迁移、生产配置或数据修复
- [ ] 如涉及上述高风险内容,已在下方写明影响、回滚和所需授权
- [ ] 未提交 `.env`、密码、Token、私钥、真实财务数据、生产日志或构建产物
- [ ] 前端改动符合 `DESIGN.md`;无前端改动则写“不适用”

高风险影响、回滚和授权状态:

## 合并前复查

- [ ] 已重新拉取远程引用并用实际修改范围再次运行碰撞检查
- [ ] PR 不再是 `WIP:`,所有测试结果已经补齐
- [ ] Codex 已重新读取原始需求和验收标准,并逐文件审核 `origin/main...任务分支` 的完整 diff
- [ ] Codex 自审确认无越界修改、重复实现、敏感内容、未处理冲突和未验证结果
- [ ] 合并顺序不会覆盖其他未合并任务
- [ ] 已授权当前任务的 Codex 执行 Squash merge、验证 `main` 并清理任务分支
- [ ] 如涉及生产迁移、数据修复、删除、覆盖配置或部署,已另行取得对应高风险授权

Reviewed-on: #1228
2026-11-18 14:03:04 +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,
]);
}
}