160 lines
9.3 KiB
PHP
160 lines
9.3 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Business;
|
||
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Mockery;
|
||
use Modules\EcommerceFinance\Repositories\LinehaulFreightRepository;
|
||
use Modules\EcommerceFinance\Repositories\ShippingOrderRepository;
|
||
use Modules\EcommerceFinance\Services\BusinessMirrorSynchronizer;
|
||
use Modules\EcommerceFinance\Services\Freight\FreightCarrierRules;
|
||
use Modules\EcommerceFinance\Services\Freight\ShippingFreightService;
|
||
use Tests\Feature\FeatureTestCase;
|
||
|
||
class LinehaulFreightTest extends FeatureTestCase
|
||
{
|
||
public function test_distributed_freight_is_used_once_per_package_and_shown_in_details(): void
|
||
{
|
||
$this->readyMirror();
|
||
$db = DB::connection('business_mirror');
|
||
foreach ([101 => ['中铁物流', 181.02], 102 => ['武进安能', 378.98], 103 => ['壹米滴答专线', 0]] as $id => [$carrier, $fee]) {
|
||
$this->shipment($id, $carrier);
|
||
$this->mirrorRow($id, $carrier, $fee, ['totalFreight' => 560]);
|
||
}
|
||
$this->shipment(101, '中铁物流', ['sku_source_id' => 2, 'barcode' => 'SECOND', 'freight' => null]);
|
||
$before = $db->table('jst_oms_shipping')->get()->toJson();
|
||
$result = app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', LinehaulFreightRepository::CARRIERS);
|
||
$this->assertSame(['packages' => 3, 'ok' => 3], $result);
|
||
foreach ([101 => 181.02, 102 => 378.98, 103 => 0] as $id => $fee) {
|
||
$row = $db->table('jst_oms_shipping_freight')->where('source_id', $id)->sole();
|
||
$this->assertEquals($fee, $row->freight);
|
||
$this->assertSame('OK', $row->status);
|
||
$this->assertNull($row->settle_weight);
|
||
$this->assertStringContainsString('工作平台专线运费', $row->formula);
|
||
}
|
||
$this->assertSame($before, $db->table('jst_oms_shipping')->get()->toJson());
|
||
$details = app(ShippingOrderRepository::class)->paginate(['express_no' => 'LINE-101'], 1, 50, 'detail');
|
||
$fees = array_column($details['data'], 'qc_freight');
|
||
$this->assertEquals(181.02, array_sum(array_filter($fees, 'is_numeric')));
|
||
$this->assertCount(1, array_filter($fees, 'is_numeric'));
|
||
}
|
||
|
||
public function test_missing_or_unfilled_linehaul_amounts_stay_pending_without_quote_fallback(): void
|
||
{
|
||
$this->readyMirror();
|
||
$this->shipment(111, '中铁物流');
|
||
$this->shipment(112, '武进安能');
|
||
$this->mirrorRow(112, '武进安能', null, ['checksgn' => false, 'status' => '待组合']);
|
||
$result = app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', LinehaulFreightRepository::CARRIERS);
|
||
$this->assertSame(['packages' => 2, 'ok' => 0], $result);
|
||
$rows = DB::connection('business_mirror')->table('jst_oms_shipping_freight')->orderBy('source_id')->get();
|
||
$this->assertSame('工作平台专线运费未找到对应出库记录', $rows[0]->status);
|
||
$this->assertSame('工作平台专线运费尚未填写有效金额', $rows[1]->status);
|
||
$this->assertNull($rows[0]->freight);
|
||
$this->assertNull($rows[1]->freight);
|
||
}
|
||
|
||
public function test_reused_waybills_match_outbound_id_or_exact_time_not_the_latest_amount(): void
|
||
{
|
||
$this->readyMirror();
|
||
$this->shipment(2000000201, '中铁物流', ['express_no' => '@REUSED', 'ship_time' => '2026-11-09 15:33:10']);
|
||
$this->shipment(222, '中铁物流', ['express_no' => 'REUSED', 'ship_time' => '2026-11-20 14:55:18']);
|
||
$this->mirrorRow(201, '中铁物流', 0, ['Expressno' => 'REUSED', 'Shiptime' => '2026-11-09 15:33:10']);
|
||
$this->mirrorRow(202, '中铁物流', 29.76, ['Expressno' => 'REUSED', 'Shiptime' => '2026-11-20 14:55:18']);
|
||
$result = app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', ['中铁物流']);
|
||
$this->assertSame(['packages' => 2, 'ok' => 2], $result);
|
||
$db = DB::connection('business_mirror');
|
||
$this->assertEquals(0, $db->table('jst_oms_shipping_freight')->where('source_id', 2000000201)->value('freight'));
|
||
$this->assertEquals(29.76, $db->table('jst_oms_shipping_freight')->where('source_id', 222)->value('freight'));
|
||
}
|
||
|
||
public function test_ambiguous_same_day_and_wrong_carrier_are_not_arbitrarily_matched(): void
|
||
{
|
||
$this->readyMirror();
|
||
$this->shipment(131, '中铁物流');
|
||
$this->mirrorRow(131, '中铁物流', 10, ['Shiptime' => '2026-11-03 11:00:00']);
|
||
$this->mirrorRow(132, '中铁物流', 20, ['Expressno' => 'LINE-131', 'Shiptime' => '2026-11-03 13:00:00']);
|
||
$this->shipment(133, '武进安能');
|
||
$this->mirrorRow(133, '中铁物流', 30);
|
||
$result = app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', LinehaulFreightRepository::CARRIERS);
|
||
$this->assertSame(['packages' => 2, 'ok' => 0], $result);
|
||
$this->assertSame('工作平台专线运费匹配到多条出库记录,待核对',
|
||
DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 131)->value('status'));
|
||
}
|
||
|
||
public function test_initial_sync_must_complete_and_other_carriers_keep_existing_calculation(): void
|
||
{
|
||
$this->shipment(141, '中铁物流');
|
||
$this->shipment(142, '中铁客退');
|
||
$result = app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', ['中铁物流']);
|
||
$this->assertSame(['packages' => 1, 'ok' => 0], $result);
|
||
$this->assertSame('工作平台专线运费尚未完成同步',
|
||
DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 141)->value('status'));
|
||
$this->assertFalse(LinehaulFreightRepository::supports('中铁客退'));
|
||
$this->assertSame(0, DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 142)->count());
|
||
}
|
||
|
||
public function test_completed_mirror_refresh_recalculates_only_the_three_linehaul_carriers(): void
|
||
{
|
||
$sync = Mockery::mock(BusinessMirrorSynchronizer::class);
|
||
$sync->shouldReceive('sync')->once()->with(LinehaulFreightRepository::DATASET, true, null, null)
|
||
->andReturn(['status' => 'succeeded', 'batches' => 1, 'rows' => 2]);
|
||
$freight = Mockery::mock(ShippingFreightService::class);
|
||
$freight->shouldReceive('recalculate')->once()->with(FreightCarrierRules::START_DATE,
|
||
now()->addDay()->format('Y-m-d'), LinehaulFreightRepository::CARRIERS)
|
||
->andReturn(['packages' => 2, 'ok' => 2]);
|
||
$this->app->instance(BusinessMirrorSynchronizer::class, $sync);
|
||
$this->app->instance(ShippingFreightService::class, $freight);
|
||
$this->artisan('business-mirror:sync-tables', ['dataset' => LinehaulFreightRepository::DATASET, '--full' => true])
|
||
->expectsOutputToContain('专线运费已更新:2 个包裹,匹配成功 2 个')->assertSuccessful();
|
||
}
|
||
|
||
private function readyMirror(): void
|
||
{
|
||
Schema::connection('business_mirror')->create('business_mirror_table_states', static function (Blueprint $table): void {
|
||
$table->string('dataset_key')->primary();
|
||
$table->string('source_key');
|
||
$table->string('source_object');
|
||
$table->string('target_table');
|
||
$table->string('status');
|
||
$table->dateTime('initial_completed_at')->nullable();
|
||
$table->timestamps();
|
||
});
|
||
Schema::connection('business_mirror')->create(LinehaulFreightRepository::TABLE, static function (Blueprint $table): void {
|
||
$table->integer('Id')->primary();
|
||
$table->dateTime('Shiptime');
|
||
$table->string('ExName');
|
||
$table->string('Expressno');
|
||
$table->decimal('FreightNew', 18, 2)->nullable();
|
||
$table->decimal('totalFreight', 18, 2)->nullable();
|
||
$table->boolean('checksgn');
|
||
$table->string('status');
|
||
$table->string('combsgn');
|
||
});
|
||
DB::connection('business_mirror')->table('business_mirror_table_states')->insert([
|
||
'dataset_key' => LinehaulFreightRepository::DATASET, 'source_key' => 'jiansun',
|
||
'source_object' => 'dbo.jst_shipFreight_view', 'target_table' => LinehaulFreightRepository::TABLE,
|
||
'status' => 'succeeded', 'initial_completed_at' => now(), 'created_at' => now(), 'updated_at' => now(),
|
||
]);
|
||
}
|
||
|
||
private function shipment(int $id, string $carrier, array $extra = []): void
|
||
{
|
||
DB::connection('business_mirror')->table('jst_oms_shipping')->insert(array_replace([
|
||
'source_id' => $id, 'sku_source_id' => 1, 'express_no' => 'LINE-' . $id, 'express_name1' => $carrier,
|
||
'ship_time' => '2026-11-03 12:00:00', 'barcode' => 'LINE-SKU', 'qty' => 1,
|
||
'freight' => 999, 'online_weight' => 5, 'actual_weight' => 4, 'weight_kg' => 6,
|
||
], $extra));
|
||
}
|
||
|
||
private function mirrorRow(int $id, string $carrier, ?float $fee, array $extra = []): void
|
||
{
|
||
DB::connection('business_mirror')->table(LinehaulFreightRepository::TABLE)->insert(array_replace([
|
||
'Id' => $id, 'Shiptime' => '2026-11-03 12:00:00', 'ExName' => $carrier, 'Expressno' => 'LINE-' . $id,
|
||
'FreightNew' => $fee, 'totalFreight' => $fee, 'checksgn' => true, 'status' => '已审核', 'combsgn' => 'TEST-COMB',
|
||
], $extra));
|
||
}
|
||
}
|