250 lines
11 KiB
PHP
250 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class ImportBankBillsTest extends FeatureTestCase
|
|
{
|
|
public function test_imports_cmb_rows_with_debit_as_expense_and_credit_as_income(): void
|
|
{
|
|
$connection = DB::connection('business_mirror');
|
|
$connection->table('fin_accounts')->insert([
|
|
'company_name' => '测试招商公司',
|
|
'account_short' => '测试招行',
|
|
'account_type' => '公户',
|
|
'bank_name' => '招商银行常州分行',
|
|
'account_no' => '5199050000000001',
|
|
'account_should_keep' => '是',
|
|
]);
|
|
$this->configureHeaders('招商银行', [
|
|
'账号', '账号名称', '交易日', '交易时间', '交易类型',
|
|
'借方金额', '贷方金额', '余额', '摘要', '流水号',
|
|
]);
|
|
$path = $this->workbook([
|
|
['测试导出文件'],
|
|
['账号', '账号名称', '交易日', '交易时间', '交易类型', '借方金额', '贷方金额', '余额', '摘要', '流水号'],
|
|
['5199050000000001', '测试招商公司', '2026-11-23', '09:35:03', '收款', '-', '10000.00', '16265.71', '货款', 'CMB-IN-1'],
|
|
['5199050000000001', '测试招商公司', '2026-11-23', '09:46:06', '付款', '9881.75', '-', '6383.96', '快递款', 'CMB-OUT-1'],
|
|
]);
|
|
|
|
try {
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $path,
|
|
'--bank' => '招商银行',
|
|
])->assertSuccessful();
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $path,
|
|
'--bank' => '招商银行',
|
|
])->assertSuccessful();
|
|
} finally {
|
|
@unlink($path);
|
|
}
|
|
|
|
$this->assertDatabaseCount('fin_bank_direct_bills', 2, 'business_mirror');
|
|
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
|
'bank_name' => '招商银行',
|
|
'finance_account_id' => 1,
|
|
'account_short' => '测试招行',
|
|
'record_code' => 'CMB-IN-1',
|
|
'income' => 10000,
|
|
'expense' => 0,
|
|
'balance' => 16265.71,
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
|
'record_code' => 'CMB-OUT-1',
|
|
'income' => 0,
|
|
'expense' => 9881.75,
|
|
'balance' => 6383.96,
|
|
], 'business_mirror');
|
|
|
|
$row = $connection->table('fin_bank_direct_bills')
|
|
->where('record_code', 'CMB-IN-1')
|
|
->first();
|
|
$this->assertNotNull($row);
|
|
$this->assertSame(
|
|
10000.0,
|
|
(float) json_decode((string) $row->raw_data, true, 512, JSON_THROW_ON_ERROR)['source_c007'],
|
|
);
|
|
}
|
|
|
|
public function test_imports_huaxia_rows_and_honours_dry_run(): void
|
|
{
|
|
$this->configureHeaders('华夏银行', [
|
|
'单位名称', '银行账号', '账户名称', '交易时间',
|
|
'收入金额', '支出金额', '账户余额', '币种', '对方户名', '附言', '凭证号',
|
|
]);
|
|
$path = $this->workbook([
|
|
['单位名称', '银行账号', '账户名称', '交易时间', '收入金额', '支出金额', '账户余额', '币种', '对方户名', '附言', '凭证号'],
|
|
['测试华夏公司', '13156000000067738', '测试华夏公司', '2026-11-24 11:23:00', '-', '200000.00', '12965.09', '人民币', '李风英', '无折转账转出', 'HX-1'],
|
|
['测试华夏公司', '13156000000067738', '测试华夏公司', '2026-11-24 10:49:41', '190000.00', '-', '212965.09', '人民币', '测试收款公司', '无折转账', 'HX-2'],
|
|
]);
|
|
|
|
try {
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $path,
|
|
'--bank' => '华夏银行',
|
|
'--dry-run' => true,
|
|
])->assertSuccessful();
|
|
$this->assertDatabaseCount('fin_bank_direct_bills', 0, 'business_mirror');
|
|
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $path,
|
|
'--bank' => '华夏银行',
|
|
])->assertSuccessful();
|
|
} finally {
|
|
@unlink($path);
|
|
}
|
|
|
|
$this->assertDatabaseCount('fin_bank_direct_bills', 2, 'business_mirror');
|
|
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
|
'bank_name' => '华夏银行',
|
|
'record_code' => 'HX-1',
|
|
'expense' => 200000,
|
|
'income' => 0,
|
|
'balance' => 12965.09,
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
|
'record_code' => 'HX-2',
|
|
'income' => 190000,
|
|
'expense' => 0,
|
|
'counterparty_name' => '测试收款公司',
|
|
], 'business_mirror');
|
|
}
|
|
|
|
public function test_imports_wangshang_metadata_and_uses_income_expense_direction(): void
|
|
{
|
|
DB::connection('business_mirror')->table('fin_accounts')->insert([
|
|
'company_name' => '测试网商公司',
|
|
'account_short' => '测试网商',
|
|
'account_type' => '网商银行',
|
|
'bank_name' => '浙江网商银行',
|
|
'account_no' => '8888888359445895',
|
|
'account_should_keep' => '是',
|
|
]);
|
|
$path = $this->workbook([
|
|
['浙江网商银行企业账户交易明细'],
|
|
['企业名称', '测试网商公司', null, null, '企业账号', '8888888359445895(人民币)'],
|
|
['借方交易笔数', '1笔', null, null, '借方交易金额', '¥100.50'],
|
|
['贷方交易笔数', '1笔', null, null, '贷方交易金额', '¥40.00'],
|
|
['账务流水号', '提交时间', '交易时间', '交易名称', '借方金额(收)', '贷方金额(支)', '余额', '对方户名', '对方账号', '对方机构', '备注'],
|
|
['WS-IN-1', '2026-11-20 16:19:27', '2026-11-20 16:19:27', '他行转入', '100.50', null, '160.50', '测试付款方', '6222000000000001', '测试银行', '转入'],
|
|
['WS-OUT-1', '2026-11-20 17:19:27', '2026-11-20 17:19:27', '跨行转账', null, '40.00', '120.50', '测试收款方', '6222000000000002', '测试银行', '货款'],
|
|
]);
|
|
|
|
try {
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $path,
|
|
'--bank' => '网商银行',
|
|
])->assertSuccessful();
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $path,
|
|
'--bank' => '网商银行',
|
|
])->assertSuccessful();
|
|
} finally {
|
|
@unlink($path);
|
|
}
|
|
|
|
$this->assertDatabaseCount('fin_bank_direct_bills', 2, 'business_mirror');
|
|
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
|
'bank_name' => '网商银行',
|
|
'company_name' => '测试网商公司',
|
|
'account_short' => '测试网商',
|
|
'account_no' => '8888888359445895',
|
|
'record_code' => 'WS-IN-1',
|
|
'income' => 100.50,
|
|
'expense' => 0,
|
|
'balance' => 160.50,
|
|
'counterparty_name' => '测试付款方',
|
|
'counterparty_bank' => '测试银行',
|
|
'currency' => '人民币',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
|
'record_code' => 'WS-OUT-1',
|
|
'income' => 0,
|
|
'expense' => 40,
|
|
'balance' => 120.50,
|
|
'counterparty_name' => '测试收款方',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_accounts', [
|
|
'account_no' => '8888888359445895',
|
|
'current_balance' => 120.50,
|
|
], 'business_mirror');
|
|
}
|
|
|
|
public function test_replace_removes_obsolete_rows_for_the_same_bank(): void
|
|
{
|
|
$this->configureHeaders('招商银行', [
|
|
'账号', '账号名称', '交易日', '交易时间', '借方金额', '贷方金额', '余额', '流水号',
|
|
]);
|
|
$first = $this->workbook([
|
|
['账号', '账号名称', '交易日', '交易时间', '借方金额', '贷方金额', '余额', '流水号'],
|
|
['5199050000000001', '旧公司', '2026-11-01', '09:00:00', '10.00', '-', '990.00', 'OLD-1'],
|
|
['5199050000000001', '旧公司', '2026-11-01', '10:00:00', '-', '20.00', '1010.00', 'KEEP-1'],
|
|
]);
|
|
$second = $this->workbook([
|
|
['账号', '账号名称', '交易日', '交易时间', '借方金额', '贷方金额', '余额', '流水号'],
|
|
['5199050000000001', '新公司', '2026-11-01', '10:00:00', '-', '20.00', '1010.00', 'KEEP-1'],
|
|
]);
|
|
|
|
try {
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $first,
|
|
'--bank' => '招商银行',
|
|
])->assertSuccessful();
|
|
$this->artisan('finance-ledger:import-bank-bills', [
|
|
'path' => $second,
|
|
'--bank' => '招商银行',
|
|
'--replace' => true,
|
|
])->assertSuccessful();
|
|
} finally {
|
|
@unlink($first);
|
|
@unlink($second);
|
|
}
|
|
|
|
$this->assertDatabaseCount('fin_bank_direct_bills', 1, 'business_mirror');
|
|
$this->assertDatabaseMissing('fin_bank_direct_bills', ['record_code' => 'OLD-1'], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
|
'record_code' => 'KEEP-1',
|
|
'company_name' => '新公司',
|
|
], 'business_mirror');
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $headers
|
|
*/
|
|
private function configureHeaders(string $bank, array $headers): void
|
|
{
|
|
$rows = [];
|
|
foreach ($headers as $index => $header) {
|
|
$rows[] = [
|
|
'bank_name' => $bank,
|
|
'column_key' => 'source_c' . str_pad((string) ($index + 1), 3, '0', STR_PAD_LEFT),
|
|
'label' => $header,
|
|
'source_header' => $header,
|
|
'data_type' => str_contains($header, '金额') || str_contains($header, '余额') ? 'number' : 'text',
|
|
'sort_order' => $index + 1,
|
|
'visible_default' => true,
|
|
];
|
|
}
|
|
DB::connection('business_mirror')->table('fin_bank_direct_bill_columns')->insert($rows);
|
|
}
|
|
|
|
/**
|
|
* @param list<list<string>> $rows
|
|
*/
|
|
private function workbook(array $rows): string
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()->fromArray($rows, null, 'A1', true);
|
|
$path = sys_get_temp_dir() . '/bank-bills-' . bin2hex(random_bytes(6)) . '.xlsx';
|
|
(new Xlsx($spreadsheet))->save($path);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
return $path;
|
|
}
|
|
}
|