CaiWuXiTong/tests/Feature/Business/ShippingFreightServiceTest.php

108 lines
7.2 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' => '常州简尚家居有限公司']),
// 10 月历史数据使用当时生效的报价
$row(3, 1, '2026-10-01 00:00:00', ['online_weight' => 4]),
$row(5, 1, '2026-09-30 23:59:59', ['online_weight' => 4]),
// 报价管理里没有的快递
$row(4, 1, '2026-11-12 09:00:00', ['online_weight' => 4, 'express_name1' => '某某快递']),
]);
$res = app(ShippingFreightService::class)->recalculate('2026-09-30', '2026-12-01');
$this->assertSame(['packages' => 4, 'ok' => 3], $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);
$r3 = $db->table('jst_oms_shipping_freight')->where('source_id', 3)->sole();
$this->assertSame('QB-901', $r3->quote_version);
$this->assertEquals(6.3, $r3->freight);
$this->assertSame(0, $db->table('jst_oms_shipping_freight')->where('source_id', 5)->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,
]);
}
}