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]), // 核算起始日(9-01)之前的不核算 $row(6, 1, '2026-08-31 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-08-01', '2026-12-01'); // 9-30 的包裹也核算了,但新北中通 9 月没有生效报价 $this->assertSame(['packages' => 5, '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('发货日没有生效的报价', $db->table('jst_oms_shipping_freight')->where('source_id', 5)->value('status')); $this->assertSame(0, $db->table('jst_oms_shipping_freight')->where('source_id', 6)->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_imported_measured_weights_take_priority_when_every_barcode_is_measured(): void { $this->version('QB-911', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]); $db = DB::connection('business_mirror'); $row = static fn (int $sid, int $sku, string $barcode, array $extra = []) => $extra + [ 'source_id' => $sid, 'sku_source_id' => $sku, 'express_no' => 'ZT' . $sid, 'express_name1' => '新北中通', 'warehouse_name' => '常州简尚家居有限公司', 'receiver_state' => '江苏省', 'receiver_city' => '南京市', 'ship_time' => '2026-11-05 10:00:00', 'pkg_count' => '1/1', 'barcode' => $barcode, 'qty' => 1, 'online_weight' => null, 'actual_weight' => null, ]; $db->table('jst_oms_shipping')->insert([ // 一个包裹两个条码都有称重:A 6kg×2 件 + B 3kg → 15kg;实称重量 9kg 不用 $row(21, 1, 'BC-A', ['qty' => 2, 'online_weight' => 8, 'actual_weight' => 9]), $row(21, 2, 'bc-b'), // 只有部分条码有称重:整单重量拆不出单件,不用称重,照旧按实称重量 $row(23, 1, 'BC-A', ['online_weight' => 10, 'actual_weight' => 11]), $row(23, 2, 'BC-X'), // 称重只登记给别的快递:照旧按实称重量 $row(22, 1, 'BC-C', ['online_weight' => 4, 'actual_weight' => 5]), ]); DB::table('shipping_measured_weights')->insert([ ['company_id' => company_id() ?? 1, 'carrier_name' => '新北中通', 'barcode' => 'BC-A', 'weight_kg' => 6, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()], ['company_id' => company_id() ?? 1, 'carrier_name' => '新北中通', 'barcode' => 'BC-B', 'weight_kg' => 3, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()], ['company_id' => company_id() ?? 1, 'carrier_name' => '武进韵达', 'barcode' => 'BC-C', 'weight_kg' => 1, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()], ]); app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01'); $r21 = $db->table('jst_oms_shipping_freight')->where('source_id', 21)->sole(); $this->assertSame('称重重量', $r21->base_source); $this->assertEquals(15, $r21->base_weight); $r23 = $db->table('jst_oms_shipping_freight')->where('source_id', 23)->sole(); $this->assertSame('实称重量', $r23->base_source); $this->assertEquals(11, $r23->base_weight); $r22 = $db->table('jst_oms_shipping_freight')->where('source_id', 22)->sole(); $this->assertSame('实称重量', $r22->base_source); $this->assertEquals(5, $r22->base_weight); } public function test_measured_weights_from_another_company_are_not_used_without_a_current_company(): void { // 命令行(calc-freight)没有当前公司:按 jushuitan.freight_company_id 取本公司称重,不能取到别家公司同条码的称重 $this->version('QB-931', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]); DB::connection('business_mirror')->table('jst_oms_shipping')->insert([ 'source_id' => 31, 'sku_source_id' => 1, 'express_no' => 'ZT31', 'express_name1' => '新北中通', 'warehouse_name' => '常州简尚家居有限公司', 'receiver_state' => '江苏省', 'ship_time' => '2026-11-05 10:00:00', 'pkg_count' => '1/1', 'barcode' => 'BC-M', 'qty' => 1, 'online_weight' => 8, 'actual_weight' => 9, ]); $other = (int) $this->company->id + 1000; DB::table('shipping_measured_weights')->insert([ ['company_id' => $this->company->id, 'carrier_name' => '新北中通', 'barcode' => 'BC-M', 'weight_kg' => 5, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()], ['company_id' => $other, 'carrier_name' => '新北中通', 'barcode' => 'BC-M', 'weight_kg' => 12, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()], ]); config(['jushuitan.freight_company_id' => $this->company->id]); \App\Models\Common\Company::forgetCurrent(); $this->assertNull(company_id()); app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01'); $r = DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 31)->sole(); $this->assertSame('称重重量', $r->base_source); $this->assertEquals(5, $r->base_weight); } 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, ]); } }