CaiWuXiTong/tests/Feature/Business/ReverseCourierBillTest.php

170 lines
9.9 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Business\CourierQuoteVersion;
use App\Support\CourierBills\Category;
use App\Support\CourierBills\CourierBillMatcher;
use App\Support\CourierBills\CourierBillStore;
use App\Support\CourierBills\ReturnPickupLookup;
use App\Support\CourierBills\ShippingLookup;
use App\Support\CourierBills\StatementExporter;
use App\Support\ReverseLogisticsQuoteSnapshot;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use Tests\Feature\FeatureTestCase;
final class ReverseCourierBillTest extends FeatureTestCase
{
private string $quotePath;
protected function setUp(): void
{
parent::setUp();
Storage::fake('local');
$schema = Schema::connection('business_mirror');
$schema->create('js_ekf_return_pickup_management', function (Blueprint $t): void {
$t->integer('Id')->primary();
foreach (['returnExpressNo', 'express_company', 'orderStatus', 'expressPickupTime', 'province', 'city', 'transactionNo', 'shopName'] as $c) $t->string($c)->nullable();
});
$schema->create('js_ekf_return_pickup_management_item', function (Blueprint $t): void {
$t->increments('id'); $t->integer('mainTableId');
foreach (['receiverAddress', 'receiverName', 'warehouse', 'productName', 'barcode'] as $c) $t->string($c)->nullable();
foreach (['grossWeight', 'length', 'width', 'height', 'returnQuantity'] as $c) $t->float($c)->nullable();
});
$schema->create('js_ekf_return_pickup_truth_express', function (Blueprint $t): void {
$t->increments('Id'); $t->integer('mId'); $t->string('truthExpressNumber');
});
if (! $schema->hasTable('business_mirror_table_states')) {
$schema->create('business_mirror_table_states', function (Blueprint $t): void {
$t->string('dataset_key')->primary(); $t->string('source_key'); $t->string('source_object');
$t->string('target_table'); $t->dateTime('initial_completed_at')->nullable();
});
}
foreach (['js_ekf_return_pickup_management', 'js_ekf_return_pickup_management_item', 'js_ekf_return_pickup_truth_express'] as $table) {
DB::connection('business_mirror')->table('business_mirror_table_states')->insert([
'dataset_key' => $table, 'source_key' => 'test', 'source_object' => $table,
'target_table' => $table, 'initial_completed_at' => now(),
]);
}
$this->quotePath = tempnam(sys_get_temp_dir(), 'reverse-quote-');
file_put_contents($this->quotePath, json_encode(['sheets' => [[
'name' => '中通快运逆向', 'values' => [
['中通快运逆向2026.4.21起'], ['省份', '面单费', '省份', '0-30kg', '31-60kg', '备注'],
['浙江省', 9, '浙江省', 0.95, 0.95, '不足15kg按15kg'],
],
], [
'name' => '常州德邦逆向', 'values' => [
['常州德邦逆向报价2026.4.7'], ['出发省份', '到达省份江苏', null, '备注:'],
[null, '面单价格', '续重价格'], ['浙江', 9, 1, '面单+重量*费率'],
],
], [
'name' => '申通退货', 'values' => [
['报价单2026.5.1'], ['出发省份', '到达省份', '江苏', null],
[null, null, '面单价格', '续重价格'], [null, '浙江', 9, 1],
],
]]], JSON_UNESCAPED_UNICODE));
$this->app->instance(ReverseLogisticsQuoteSnapshot::class, new ReverseLogisticsQuoteSnapshot($this->quotePath));
}
protected function tearDown(): void
{
@unlink($this->quotePath);
parent::tearDown();
}
private function pickup(int $id, string $number, array $main = [], array $item = []): void
{
$db = DB::connection('business_mirror');
$db->table('js_ekf_return_pickup_management')->insert($main + [
'Id' => $id, 'returnExpressNo' => $number, 'express_company' => '中通快运',
'orderStatus' => 'ExpAccepted', 'expressPickupTime' => '2026-11-05 12:00:00', 'province' => '浙江省',
]);
$db->table('js_ekf_return_pickup_management_item')->insert($item + [
'mainTableId' => $id, 'grossWeight' => 39.1, 'length' => 112, 'width' => 45, 'height' => 24,
'receiverAddress' => '江苏省常州市某仓', 'returnQuantity' => 1, 'productName' => '测试桌', 'barcode' => 'ITEM1',
]);
}
public function test_reverse_statement_matches_without_shipping_and_exports_return_details(): void
{
$this->pickup(1, 'OLD1');
$this->pickup(2, 'UNBILLED');
$this->pickup(3, 'CANCELED', ['orderStatus' => 'ExpCanceled']);
$this->pickup(4, 'PRIOR', ['expressPickupTime' => '2026-10-31 23:00:00']);
$this->pickup(5, 'UNKNOWN', [], ['grossWeight' => 0, 'length' => 0]);
$db = DB::connection('business_mirror');
$db->table('js_ekf_return_pickup_truth_express')->insert(['mId' => 1, 'truthExpressNumber' => 'NEW1']);
$group = app(CourierBillStore::class)->saveGroup(null, ['name' => '中通快运逆向', 'express_names' => ['中通快运逆向']]);
$id = $db->table('courier_bill_statements')->insertGetId(['group_id' => $group, 'bill_month' => '2026-11', 'status' => 'open']);
foreach (['NEW1', 'NEW1', 'OLD1', 'CANCELED', 'PRIOR', 'UNKNOWN', 'MISSING'] as $i => $number) {
$db->table('courier_bill_lines')->insert(['statement_id' => $id, 'file_id' => 0, 'sheet' => '测试账单',
'raw' => '[]', 'waybill_no' => $number, 'bill_amount' => 47, 'row_no' => $i + 1]);
}
app(CourierBillMatcher::class)->match($id);
$lines = $db->table('courier_bill_lines')->where('statement_id', $id)->orderBy('id')->get();
$this->assertSame([Category::MATCH, Category::DUPLICATE, Category::UNSHIPPED, Category::UNSHIPPED,
Category::PRIOR, Category::UNCALCULATED, Category::UNMATCHED], $lines->pluck('category')->all());
$this->assertSame(47.0, (float) $lines[0]->calc_freight);
$this->assertSame('中通快运逆向', $lines[0]->express_name);
$this->assertSame(['UNBILLED'], $db->table('courier_bill_unprovided')->where('statement_id', $id)->orderBy('waybill_no')->pluck('waybill_no')->all());
$row = app(ShippingLookup::class)->details(['NEW1'], ['中通快运逆向'])['NEW1'];
$this->assertSame('退件取件管理', $row['data_source']);
$this->assertSame('ITEM1', $row['barcode']);
$this->assertSame(40.0, $row['qc_charge_weight']);
$out = fopen('php://temp', 'w+');
app(StatementExporter::class)->export($id, $out);
rewind($out);
$this->assertSame('PK', fread($out, 2));
fclose($out);
}
public function test_each_reverse_carrier_is_distinct_and_does_not_override_forward_lookup(): void
{
$this->pickup(1, 'DEPPON', ['express_company' => '德邦'], ['grossWeight' => 8.7, 'length' => 100, 'width' => 10, 'height' => 10]);
$this->pickup(2, 'STO', ['express_company' => '速寄递'], ['grossWeight' => 8.7, 'length' => 100, 'width' => 10, 'height' => 10]);
$this->pickup(3, 'OLDQUOTE', ['expressPickupTime' => '2026-04-01 12:00:00']);
$rows = app(ReturnPickupLookup::class)->details(['DEPPON', 'STO', 'OLDQUOTE'], ['常州德邦逆向']);
$this->assertSame(17.7, $rows['DEPPON']['qc_freight']);
$this->assertSame(17.0, $rows['STO']['qc_freight']);
$this->assertSame('申通退货', $rows['STO']['express_name1']);
$this->assertNull($rows['OLDQUOTE']['qc_freight']);
$this->assertSame([], app(ShippingLookup::class)->summaries(['DEPPON'], ['常州德邦']));
}
public function test_haosheng_uses_actual_carrier_and_effective_route_quote(): void
{
CourierQuoteVersion::create(['company_id' => $this->company->id, 'company_name' => '好省邮百世',
'source_quote_id' => 'TEST', 'source_change_id' => 1, 'sheet_name' => '好省邮百世',
'carrier_name' => '好省邮百世', 'created_by' => $this->user->id, 'approved_by' => $this->user->id,
'version_code' => 'HAO-TEST', 'quote_type' => '标准快递报价', 'effective_date' => '2026-10-01',
'signature_status' => '已签署并归档', 'status' => 'approved', 'values_json' => json_encode([
['出发省', '到达省', '组合', '首重10kg', '续重', '首重20kg', '续重', '首重30kg', '续重'],
['浙江', '江苏', '', 20, 1, 30, 2, 40, 3],
])]);
$this->company->makeCurrent();
$this->pickup(1, 'HAO1', ['express_company' => '百世'], ['grossWeight' => 35]);
$row = app(ReturnPickupLookup::class)->details(['HAO1'], ['好省邮快递'])['HAO1'];
$this->assertSame('好省邮快递', $row['express_name1']);
$this->assertSame('HAO-TEST', $row['qc_quote_version']);
$this->assertSame(55.0, $row['qc_freight']);
}
public function test_multiple_products_sum_weight_and_volume_but_charge_one_label(): void
{
$this->pickup(1, 'MULTI', [], ['grossWeight' => 10, 'length' => 60, 'width' => 50, 'height' => 40, 'returnQuantity' => 2]);
DB::connection('business_mirror')->table('js_ekf_return_pickup_management_item')->insert([
'mainTableId' => 1, 'grossWeight' => 5, 'length' => 0, 'width' => 0, 'height' => 0,
'returnQuantity' => 1, 'receiverAddress' => '江苏省常州市某仓',
]);
$row = app(ReturnPickupLookup::class)->details(['MULTI'], ['中通快运逆向'])['MULTI'];
$this->assertSame(25.0, $row['weight_kg']);
$this->assertSame(240000.0, $row['volume_cm3']);
$this->assertSame(40.0, $row['qc_charge_weight']);
$this->assertSame(9.0, $row['qc_miandan']);
$this->assertSame(47.0, $row['qc_freight']);
}
}