[ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', ], 'upstream_mirrors.connection' => 'business_mirror', ]); DB::purge('business_mirror'); Schema::connection('business_mirror')->create('fin_accounts', function (Blueprint $table): void { $table->id(); $table->string('account_no')->nullable(); $table->string('account_short')->nullable(); $table->string('company_name')->nullable(); }); Schema::connection('business_mirror')->create('fin_flow_summary_account_sources', function (Blueprint $table): void { $table->id(); $table->string('account_short')->unique(); $table->string('data_source')->nullable(); $table->string('updated_by')->nullable(); $table->timestamps(); }); Schema::connection('business_mirror')->create(BankDirectBalanceRepository::TABLE, function (Blueprint $table): void { $table->id(); $table->unsignedBigInteger('finance_account_id'); $table->string('bank_name'); $table->string('account_no'); $table->string('account_name')->nullable(); $table->string('account_short'); $table->string('company_name')->nullable(); $table->string('currency')->default('人民币'); $table->decimal('account_balance', 20, 6)->nullable(); $table->decimal('available_balance', 20, 6); $table->date('balance_date'); $table->dateTime('balance_at'); $table->string('source'); $table->string('source_file_name'); $table->char('source_sha256', 64); $table->dateTime('imported_at'); $table->timestamps(); $table->unique(['finance_account_id', 'balance_date', 'source']); }); $this->workbookPath = sys_get_temp_dir() . '/bank-direct-' . uniqid('', true) . '.xlsx'; } protected function tearDown(): void { if (isset($this->workbookPath) && is_file($this->workbookPath)) { unlink($this->workbookPath); } DB::purge('business_mirror'); parent::tearDown(); } public function testImportsAvailableBalanceByExactBankAccountAndMarksSourceDirect(): void { $firstId = DB::connection('business_mirror')->table('fin_accounts')->insertGetId([ 'account_no' => '12345678901234567', 'account_short' => '华夏基本户', 'company_name' => '测试公司甲', ]); $secondId = DB::connection('business_mirror')->table('fin_accounts')->insertGetId([ 'account_no' => '76543210987654321', 'account_short' => '华夏一般户', 'company_name' => '测试公司乙', ]); DB::connection('business_mirror')->table('fin_flow_summary_account_sources')->insert([ 'account_short' => '华夏基本户', 'data_source' => '手动', 'updated_by' => '原配置', ]); $this->writeWorkbook([ ['1234 5678 9012 34567', '华夏甲账号', '基本户', '测试公司甲', '华夏银行', '人民币', '基本户', 1000.50, 900.25, '2026-11-19 17:08:04'], ['76543210987654321', '华夏乙账号', '一般户', '测试公司乙', '华夏银行', '人民币', '一般户', 2000.75, 1500.60, '2026-11-19 17:08:04'], ], accountHeader: ''); $result = app(BankDirectBalanceWorkbookImporter::class)->import( $this->workbookPath, '2026-11-19', ); $this->assertSame(2, $result['total']); $this->assertSame(0, $result['replaced']); $this->assertSame(2400.85, $result['available_total']); $this->assertDatabaseHas(BankDirectBalanceRepository::TABLE, [ 'finance_account_id' => $firstId, 'account_short' => '华夏基本户', 'available_balance' => 900.25, 'balance_date' => '2026-11-19', ], 'business_mirror'); $this->assertDatabaseHas(BankDirectBalanceRepository::TABLE, [ 'finance_account_id' => $secondId, 'account_short' => '华夏一般户', 'available_balance' => 1500.60, 'balance_date' => '2026-11-19', ], 'business_mirror'); $this->assertDatabaseHas('fin_flow_summary_account_sources', [ 'account_short' => '华夏基本户', 'data_source' => '银企直连', ], 'business_mirror'); $this->assertDatabaseHas('fin_flow_summary_account_sources', [ 'account_short' => '华夏一般户', 'data_source' => '银企直连', ], 'business_mirror'); } public function testReimportAtomicallyReplacesTheSameBankAndDate(): void { $accountId = DB::connection('business_mirror')->table('fin_accounts')->insertGetId([ 'account_no' => '12345678901234567', 'account_short' => '华夏基本户', 'company_name' => '测试公司甲', ]); $this->writeWorkbook([ ['12345678901234567', '华夏账号', '基本户', '测试公司甲', '华夏银行', '人民币', '基本户', 1000, 900, '2026-11-19 16:00:00'], ]); $importer = app(BankDirectBalanceWorkbookImporter::class); $importer->import($this->workbookPath, '2026-11-19'); $this->writeWorkbook([ ['12345678901234567', '华夏账号', '基本户', '测试公司甲', '华夏银行', '人民币', '基本户', 1100, 950.88, '2026-11-19 18:00:00'], ], overwrite: false); $result = $importer->import($this->workbookPath, '2026-11-19'); $this->assertSame(1, $result['replaced']); $this->assertSame(1, DB::connection('business_mirror')->table(BankDirectBalanceRepository::TABLE) ->where('finance_account_id', $accountId) ->where('balance_date', '2026-11-19') ->count()); $this->assertDatabaseHas(BankDirectBalanceRepository::TABLE, [ 'finance_account_id' => $accountId, 'available_balance' => 950.88, 'balance_at' => '2026-11-19 18:00:00', ], 'business_mirror'); } public function testUnmatchedAccountKeepsExistingSnapshotsAndSourcesUntouched(): void { DB::connection('business_mirror')->table('fin_accounts')->insert([ 'account_no' => '12345678901234567', 'account_short' => '华夏基本户', 'company_name' => '测试公司甲', ]); DB::connection('business_mirror')->table('fin_flow_summary_account_sources')->insert([ 'account_short' => '华夏基本户', 'data_source' => '手动', 'updated_by' => '原配置', ]); $this->writeWorkbook([ ['99999999999999999', '未知账号', '未知户', '测试公司甲', '华夏银行', '人民币', '基本户', 1000, 900, '2026-11-19 17:08:04'], ]); try { app(BankDirectBalanceWorkbookImporter::class)->import($this->workbookPath); $this->fail('未匹配账号应阻止导入。'); } catch (\InvalidArgumentException $exception) { $this->assertStringContainsString('未匹配 1 个银行账号', $exception->getMessage()); } $this->assertDatabaseCount(BankDirectBalanceRepository::TABLE, 0, 'business_mirror'); $this->assertDatabaseHas('fin_flow_summary_account_sources', [ 'account_short' => '华夏基本户', 'data_source' => '手动', 'updated_by' => '原配置', ], 'business_mirror'); } public function testDryRunValidatesWithoutWriting(): void { DB::connection('business_mirror')->table('fin_accounts')->insert([ 'account_no' => '12345678901234567', 'account_short' => '华夏基本户', 'company_name' => '测试公司甲', ]); $this->writeWorkbook([ ['12345678901234567', '华夏账号', '基本户', '测试公司甲', '华夏银行', '人民币', '基本户', 1000, 900, '2026-11-19 17:08:04'], ]); $result = app(BankDirectBalanceWorkbookImporter::class)->import( $this->workbookPath, null, true, ); $this->assertTrue($result['dry_run']); $this->assertSame('2026-11-19', $result['balance_date']); $this->assertDatabaseCount(BankDirectBalanceRepository::TABLE, 0, 'business_mirror'); $this->assertDatabaseCount('fin_flow_summary_account_sources', 0, 'business_mirror'); } /** * @param list> $rows */ private function writeWorkbook(array $rows, string $accountHeader = '银行账号', bool $overwrite = true): void { if (! $overwrite && is_file($this->workbookPath)) { unlink($this->workbookPath); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('sheet1'); $sheet->fromArray([[ $accountHeader, '账户名称', '账户别名', '所属企业', '所属银行', '币种', '账户类型', '账户余额', '可用余额', '余额更新日期', ], ...$rows]); foreach ($rows as $index => $row) { $sheet->getCell('A' . ($index + 2))->setValueExplicit( (string) $row[0], DataType::TYPE_STRING, ); } (new Xlsx($spreadsheet))->save($this->workbookPath); $spreadsheet->disconnectWorksheets(); } }