230 lines
15 KiB
PHP
230 lines
15 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']);
|
||
}
|
||
|
||
public function test_numeric_actual_waybill_is_not_misclassified_as_replaced(): void
|
||
{
|
||
$this->pickup(1, '98234567', ['express_company' => '德邦']);
|
||
DB::connection('business_mirror')->table('js_ekf_return_pickup_truth_express')->insert(['mId' => 1, 'truthExpressNumber' => '98234567']);
|
||
$row = app(ReturnPickupLookup::class)->details(['98234567'], ['常州德邦逆向'])['98234567'];
|
||
$this->assertArrayNotHasKey('_unshipped', $row);
|
||
$this->assertNotNull($row['qc_freight']);
|
||
}
|
||
|
||
public function test_combined_size_weight_surcharge_uses_return_package_weight_and_quote_rate(): void
|
||
{
|
||
$text = '最长边大于2米;②计费重量大于60KG,即认定为超长超重货,该单货物将在原收费基础上加收超长超重费。加收标准按照计费重量*1元/KG计算,最低加收20元/票。';
|
||
$ruleId = DB::table('business_courier_surcharge_quotes')->insertGetId([
|
||
'company_id' => $this->company->id, 'courier' => '中通快运逆向', 'scope' => '自有快递',
|
||
'reconciler' => '测试', 'types' => json_encode(['超长费', '超重费']), 'mode' => '按尺寸/最长边',
|
||
'standard' => $text, 'condition' => $text, 'effective_at' => '2026-01-01', 'status' => 'active',
|
||
]);
|
||
$this->pickup(1, 'EDGE', [], ['grossWeight' => 60, 'length' => 200, 'width' => 1, 'height' => 1]);
|
||
$this->pickup(2, 'HEAVY', [], ['grossWeight' => 60.01, 'length' => 100, 'width' => 1, 'height' => 1]);
|
||
$this->pickup(3, 'LONG', [], ['grossWeight' => 10, 'length' => 201, 'width' => 1, 'height' => 1]);
|
||
$this->pickup(4, 'MULTI', [], ['grossWeight' => 35.2, 'length' => 100, 'width' => 1, 'height' => 1, 'returnQuantity' => 2]);
|
||
$this->pickup(5, 'MISSING', [], ['grossWeight' => 0, 'length' => 0, 'width' => 0, 'height' => 0]);
|
||
$this->pickup(6, 'CANCELLED', ['orderStatus' => 'ExpCanceled']);
|
||
$this->pickup(7, 'OTHER', ['express_company' => '德邦']);
|
||
$input = array_map(fn ($no) => ['waybill_no' => $no, 'item' => '超重费'], ['EDGE', 'HEAVY', 'LONG', 'MULTI', 'MISSING', 'CANCELLED', 'OTHER']);
|
||
$calc = app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
|
||
$result = $calc->calculate($input, 'courier', '2026-11', ['中通快运逆向']);
|
||
foreach (['EDGE' => 0, 'HEAVY' => 61, 'LONG' => 20, 'MULTI' => 71] as $no => $amount) {
|
||
$this->assertEquals($amount, $result['rows'][$no]['超重费'][0]->settlement_amount);
|
||
}
|
||
foreach (['MISSING', 'CANCELLED'] as $no) $this->assertStringContainsString('缺少已核算计费重量', $result['notes'][$no]['超重费']);
|
||
$this->assertStringContainsString('没有启用', $result['notes']['OTHER']['超重费']);
|
||
$this->assertSame('退件取件管理', app(ShippingLookup::class)->details(['MULTI'], ['中通快运逆向'])['MULTI']['data_source']);
|
||
|
||
// 一票多商品仍只计一份附加费,重复账单行不能按对方金额再次计费。
|
||
$db = DB::connection('business_mirror');
|
||
$group = app(CourierBillStore::class)->saveGroup(null, ['name' => '中通快运逆向', 'express_names' => ['中通快运逆向']]);
|
||
$statement = $db->table('courier_bill_statements')->insertGetId(['group_id' => $group, 'bill_month' => '2026-11', 'status' => 'open']);
|
||
$file = $db->table('courier_bill_files')->insertGetId(['statement_id' => $statement, 'group_id' => $group,
|
||
'bill_month' => '2026-11', 'status' => 'parsed', 'original_name' => '测试账单.xlsx', 'stored_path' => 'test.xlsx']);
|
||
foreach ([2, 3] as $line) $db->table('courier_bill_surcharges')->insert([
|
||
'file_id' => $file, 'sheet' => '附加费', 'row_no' => $line, 'waybill_no' => 'MULTI', 'item' => '超重费', 'amount' => 999,
|
||
]);
|
||
$matcher = app(\App\Support\CourierBills\SurchargeSettlementMatcher::class);
|
||
$matcher->courier($statement);
|
||
$matched = $matcher->query('courier', $statement)->orderBy('id')->get();
|
||
$this->assertEquals(71, $matched->sum('self_amount'));
|
||
$this->assertSame('重复', $matched[1]->match_status);
|
||
$this->assertEquals(76.45, app(ShippingLookup::class)->summaries(['MULTI'], ['中通快运逆向'])['MULTI']['calc_freight']);
|
||
|
||
// 单价和最低金额取报价;不一致或追加未支持的条件不得默默忽略。
|
||
$changed = str_replace(['*1元/KG', '20元/票'], ['*2元/KG', '50元/票'], $text);
|
||
DB::table('business_courier_surcharge_quotes')->where('id', $ruleId)->update(['standard' => $changed, 'condition' => $changed]);
|
||
$revised = $calc->calculate($input, 'courier', '2026-11', ['中通快运逆向']);
|
||
$this->assertEquals(122, $revised['rows']['HEAVY']['超重费'][0]->settlement_amount);
|
||
$this->assertEquals(50, $revised['rows']['LONG']['超重费'][0]->settlement_amount);
|
||
DB::table('business_courier_surcharge_quotes')->where('id', $ruleId)->update(['condition' => $changed . '并且客服通过']);
|
||
$this->assertStringContainsString('尚未支持自动核验', $calc->calculate($input, 'courier', '2026-11', ['中通快运逆向'])['notes']['HEAVY']['超重费']);
|
||
}
|
||
}
|