81 lines
5.4 KiB
PHP
81 lines
5.4 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Business;
|
||
|
||
use App\Support\CourierBills\CourierBillStore;
|
||
use App\Support\CourierBills\Rebooks;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Tests\Feature\FeatureTestCase;
|
||
|
||
final class CourierRebookReturnReviewTest extends FeatureTestCase
|
||
{
|
||
public function test_four_carrier_names_returns_fuzzy_match_and_prior_refund_are_accounted_once(): void
|
||
{
|
||
$db = Rebooks::db();
|
||
if (! Schema::connection($db->getName())->hasTable('courier_returned_parcels')) {
|
||
Schema::connection($db->getName())->create('courier_returned_parcels', function ($table): void {
|
||
$table->unsignedBigInteger('source_id')->primary();
|
||
$table->string('waybill');
|
||
$table->dateTime('received_at');
|
||
$table->decimal('returned_qty', 12, 4);
|
||
});
|
||
}
|
||
$names = ['新北中通', '新北中通其他', '新北中通配件', '新北中通其他配件'];
|
||
$group = app(CourierBillStore::class)->saveGroup(null, ['name' => '新北分组', 'express_names' => $names, 'rebook_months' => 2]);
|
||
$reasons = ['快递丢件', '双面单', '买家原因', '站点异常'];
|
||
$nums = ['301233122220', '301233122221', '301233122222', '301233122223'];
|
||
$store = DB::connection(config('database.supplier_payable_maintenance_connection'));
|
||
foreach ($names as $i => $name) {
|
||
$row = ['summary_month' => '2026-10', 'tracking_no' => $nums[$i], 'carrier' => $name,
|
||
'settle_bookkeeping' => '100', 'analysis' => '未退回', 'bookkeeping_reason' => $reasons[$i], 'cost' => '40', 'settle_freight' => '7'];
|
||
$store->table('supplier_payable_overrides')->insert(['company_id' => $this->company->id, 'dataset' => Rebooks::BOOKKEEPING_DATASET,
|
||
'record_key' => 'return-'.$i, 'values' => json_encode($row), 'created_at' => now(), 'updated_at' => now()]);
|
||
if ($i === 0) {
|
||
$store->table('supplier_payable_overrides')->insert(['company_id' => $this->company->id, 'dataset' => Rebooks::BOOKKEEPING_DATASET,
|
||
'record_key' => 'duplicate-source', 'values' => json_encode($row), 'created_at' => now(), 'updated_at' => now()]);
|
||
}
|
||
// 第二票通过去掉 T 前缀匹配;第四票未实际退回,不能触发补款。
|
||
$return = ['source_id' => 100 + $i, 'waybill' => $i === 1 ? 'T'.$nums[$i] : $nums[$i],
|
||
'received_at' => '2026-12-02 10:00:00', 'returned_qty' => $i === 3 ? 0 : 1];
|
||
if (Schema::connection($db->getName())->hasColumn('courier_returned_parcels', 'generation')) {
|
||
$return += ['generation' => 'test', 'synced_at' => now()];
|
||
}
|
||
$db->table('courier_returned_parcels')->insert($return);
|
||
}
|
||
$db->table('courier_bill_rebooks')->insert(['waybill_no' => $nums[0], 'deduct_month' => '2026-10',
|
||
'refund_month' => '2026-10', 'amount' => 85, 'source' => 'import', 'created_at' => now()]);
|
||
// 结算仍由已复核金额驱动;退回匹配不能擅自把其他分组未复核金额计入应付。
|
||
foreach ([0 => 15, 1 => 15, 2 => 0] as $i => $amount) {
|
||
$db->table('courier_bill_rebook_reviews')->insert(['waybill_no' => $nums[$i], 'deduct_month' => '2026-10',
|
||
'current_amount' => $amount, 'updated_at' => now()]);
|
||
}
|
||
$id = $db->table('courier_bill_statements')->insertGetId(['group_id' => $group, 'bill_month' => '2026-11', 'status' => 'open',
|
||
'created_at' => now(), 'updated_at' => now()]);
|
||
$plan = app(Rebooks::class)->forStatement($id);
|
||
$rows = collect($plan['rows'])->keyBy('waybill');
|
||
$this->assertCount(4, $rows);
|
||
$this->assertSame(200.0, $rows[$nums[0]]['deducted']);
|
||
$this->assertCount(2, $rows[$nums[0]]['info']['_rebook_sources']);
|
||
$this->assertSame(0.0, $rows[$nums[0]]['amount']);
|
||
$this->assertSame(15.0, $rows[$nums[0]]['current']);
|
||
$this->assertSame(85.0, $rows[$nums[1]]['amount']);
|
||
$this->assertSame('模糊匹配(格式或T前缀)', $rows[$nums[1]]['info']['_rebook_sources'][0]['_return_method']);
|
||
$this->assertSame(100.0, $rows[$nums[2]]['amount']);
|
||
$this->assertSame(0.0, $rows[$nums[3]]['amount']);
|
||
$this->assertSame(['2026-10' => 185.0], Rebooks::totals($plan['rows']));
|
||
$this->assertSame(15.0, $rows[$nums[1]]['info']['_rebook_sources'][0]['_return_current']);
|
||
$this->assertSame(0.0, $rows[$nums[2]]['info']['_rebook_sources'][0]['_return_current']);
|
||
$this->assertArrayNotHasKey('_return_current', $rows[$nums[3]]['info']['_rebook_sources'][0]);
|
||
|
||
$annotated = app(\App\Support\CourierBills\RebookReturns::class)->annotate([
|
||
['tracking_no' => $nums[0], 'analysis' => '未退回', 'bookkeeping_reason' => '丢件', 'cost' => null, 'settle_freight' => 7],
|
||
['tracking_no' => $nums[0], 'analysis' => '已退回,记账损耗+运费', 'bookkeeping_reason' => '丢件', 'cost' => 40, 'settle_freight' => 7],
|
||
]);
|
||
$this->assertSame('已退回,成本或结算运费缺失待核对', $annotated[0]['_return_status']);
|
||
$this->assertArrayNotHasKey('_return_current', $annotated[0]);
|
||
$this->assertSame('已退回,记账损耗+运费', $annotated[1]['analysis']);
|
||
$this->assertArrayNotHasKey('_return_analysis', $annotated[1]);
|
||
}
|
||
}
|