saveGroup(null, [ 'name' => '边界测试快递', 'express_names' => ['边界测试快递'], 'rebook_months' => 1, ]); DB::connection(config('database.supplier_payable_maintenance_connection'))->table('supplier_payable_overrides')->insert([ 'company_id' => $this->company->id, 'dataset' => Rebooks::BOOKKEEPING_DATASET, 'record_key' => 'boundary:1', 'values' => json_encode([ 'summary_month' => '2026-10', 'tracking_no' => 'BOUNDARY100', 'carrier' => '边界测试快递', 'settle_bookkeeping' => '100.00', 'product_name' => '确认时商品', ]), 'created_at' => now(), 'updated_at' => now(), ]); Rebooks::db()->table('courier_bill_rebook_reviews')->insert([ 'waybill_no' => 'BOUNDARY100', 'deduct_month' => '2026-10', 'current_amount' => 0, 'updated_at' => now(), ]); return Rebooks::db()->table('courier_bill_statements')->insertGetId([ 'group_id' => $group, 'bill_month' => '2026-11', 'status' => 'open', 'created_at' => now(), 'updated_at' => now(), ]); } public function test_carrier_matching_keeps_trimming_surrounding_whitespace(): void { $id = $this->statement(); $connection = DB::connection(config('database.supplier_payable_maintenance_connection')); $record = $connection->table('supplier_payable_overrides')->where('record_key', 'boundary:1')->first(); $values = json_decode($record->values, true); $values['carrier'] = " \t边界测试快递\r\n "; $connection->table('supplier_payable_overrides')->where('id', $record->id) ->update(['values' => json_encode($values)]); $rows = app(Rebooks::class)->forStatement($id)['rows']; $this->assertCount(1, $rows); $this->assertSame('边界测试快递', $rows[0]['carrier']); $this->assertSame(100.0, $rows[0]['amount']); } public function test_refund_imported_in_same_month_is_not_paid_again(): void { $id = $this->statement(); Rebooks::db()->table('courier_bill_rebooks')->insert([ 'waybill_no' => 'BOUNDARY100', 'deduct_month' => '2026-10', 'refund_month' => '2026-11', 'amount' => 100, 'source' => 'import', 'created_at' => now(), ]); $rows = app(Rebooks::class)->forStatement($id)['rows']; $this->assertSame(0.0, (float) array_sum(array_column($rows, 'amount'))); $this->assertSame('已补(2026-11)', $rows[0]['status']); app(CourierBillMatcher::class)->refreshSummary($id); app(CourierBillStore::class)->confirm($id, 'tester'); $this->assertSame(0, Rebooks::db()->table('courier_bill_rebooks')->where('statement_id', $id)->count()); } public function test_confirmed_amount_and_export_are_frozen_after_review_and_group_changes(): void { $id = $this->statement(); app(CourierBillMatcher::class)->refreshSummary($id); app(CourierBillStore::class)->confirm($id, 'tester'); Rebooks::db()->table('courier_bill_rebook_reviews')->where('waybill_no', 'BOUNDARY100')->update(['current_amount' => 40]); Rebooks::db()->table('courier_bill_groups')->update(['rebook_months' => 0]); DB::connection(config('database.supplier_payable_maintenance_connection'))->table('supplier_payable_overrides')->delete(); $rows = app(Rebooks::class)->forStatement($id)['rows']; $this->assertSame(100.0, $rows[0]['amount']); $this->assertSame(0.0, $rows[0]['current']); $this->assertSame('确认时商品', $rows[0]['info']['product_name']); $path = tempnam(sys_get_temp_dir(), 'rebook-frozen-'); try { $stream = fopen($path, 'wb'); try { app(StatementExporter::class)->export($id, $stream); } finally { fclose($stream); } $book = IOFactory::createReader('Xlsx')->load($path); try { $sheet = $book->getSheetByName('补10月记账'); $this->assertNotNull($sheet); $this->assertEquals([100, 0, 100], array_slice($sheet->toArray(null, false, false)[1], -3)); } finally { $book->disconnectWorksheets(); } } finally { @unlink($path); } } public function test_manual_summary_mismatch_blocks_confirmation_without_writing_ledger(): void { $id = $this->statement(); app(CourierBillMatcher::class)->refreshSummary($id); app(CourierBillStore::class)->addSummaryItem($id, '补10月记账', null, 60); try { app(CourierBillStore::class)->confirm($id, 'tester'); $this->fail('不同金额不应确认成功。'); } catch (RuntimeException $exception) { $this->assertStringContainsString('小汇总与逐单复核金额不一致', $exception->getMessage()); } $this->assertSame('open', Rebooks::db()->table('courier_bill_statements')->where('id', $id)->value('status')); $this->assertSame(0, Rebooks::db()->table('courier_bill_rebooks')->count()); } public function test_stale_summary_blocks_confirmation_and_refresh_recovers(): void { $id = $this->statement(); app(CourierBillMatcher::class)->refreshSummary($id); Rebooks::db()->table('courier_bill_rebook_reviews')->update(['current_amount' => 40]); try { app(CourierBillStore::class)->confirm($id, 'tester'); $this->fail('过期金额不应被悄悄确认。'); } catch (RuntimeException $exception) { $this->assertStringContainsString('小汇总与逐单复核金额不一致', $exception->getMessage()); } app(CourierBillMatcher::class)->refreshSummary($id); app(CourierBillStore::class)->confirm($id, 'tester'); $this->assertSame(60.0, (float) Rebooks::db()->table('courier_bill_rebooks')->where('statement_id', $id)->sum('amount')); app(CourierBillStore::class)->unconfirm($id); $this->assertSame(0, Rebooks::db()->table('courier_bill_rebooks')->count()); } public function test_same_deduction_cannot_be_imported_as_refunded_twice_across_months(): void { $this->statement(); $path = tempnam(sys_get_temp_dir(), 'rebook-dedup-'); file_put_contents($path, "单号,扣款月份,补款账期,补款金额\nBOUNDARY100,2026-10,2026-11,100\nBOUNDARY100,2026-10,2026-12,100\n"); try { $result = app(Rebooks::class)->importRefunds($path, 'csv'); $this->assertSame(1, $result['inserted']); $this->assertSame(100.0, (float) Rebooks::db()->table('courier_bill_rebooks')->sum('amount')); } finally { @unlink($path); } } }