feat: 支持江南农商行账单导入 (#1466)
This commit is contained in:
parent
9edbc0a158
commit
828b5d4412
@ -10,7 +10,8 @@ class ImportBankBills extends Command
|
||||
{
|
||||
protected $signature = 'finance-ledger:import-bank-bills
|
||||
{path : 银行批量账单 XLSX/XLS/CSV 文件}
|
||||
{--bank= : 招商银行、华夏银行或网商银行}
|
||||
{--bank= : 账单所属银行模板}
|
||||
{--scope=direct : direct(银企直连)或 non_direct(非银企直连)}
|
||||
{--dry-run : 只校验,不写入账单数据}
|
||||
{--replace : 先清空该银行旧账单,再导入本次文件}';
|
||||
|
||||
@ -24,6 +25,8 @@ class ImportBankBills extends Command
|
||||
(string) $this->option('bank'),
|
||||
(bool) $this->option('dry-run'),
|
||||
(bool) $this->option('replace'),
|
||||
true,
|
||||
(string) $this->option('scope'),
|
||||
);
|
||||
} catch (Throwable $exception) {
|
||||
$this->error($exception->getMessage());
|
||||
|
||||
@ -64,6 +64,8 @@ final class BankBillStatementImporter
|
||||
&& $templateBank === '华夏银行个人卡';
|
||||
$isAgriculturalMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '农业银行';
|
||||
$isJiangnanMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '江南农商行';
|
||||
if (! is_file($path) || ! is_readable($path)) {
|
||||
throw new InvalidArgumentException('银行账单文件不存在或不可读取。');
|
||||
}
|
||||
@ -82,6 +84,8 @@ final class BankBillStatementImporter
|
||||
[$headerRow, $headers, $metadata] = $this->huaxiaPersonalHeaderAndMetadata($reader);
|
||||
} elseif ($templateBank === '农业银行') {
|
||||
[$headerRow, $headers, $metadata] = $this->agriculturalMonthlyHeaderAndMetadata($reader);
|
||||
} elseif ($templateBank === '江南农商行') {
|
||||
[$headerRow, $headers, $metadata] = $this->jiangnanMonthlyHeaderAndMetadata($reader);
|
||||
} elseif ($templateBank === '中国银行') {
|
||||
[$headerRow, $headers, $metadata] = $this->bocMonthlyHeaderAndMetadata($reader);
|
||||
} else {
|
||||
@ -211,7 +215,8 @@ final class BankBillStatementImporter
|
||||
$bankName === '网商银行',
|
||||
$isBocMonthly,
|
||||
$isHuaxiaPersonal,
|
||||
$isAgriculturalMonthly => (string) ($metadata['account_no'] ?? ''),
|
||||
$isAgriculturalMonthly,
|
||||
$isJiangnanMonthly => (string) ($metadata['account_no'] ?? ''),
|
||||
default => $this->accountNumber($valuesByLabel),
|
||||
};
|
||||
$account = $accountsByNumber[$this->digits($accountNo)] ?? null;
|
||||
@ -230,6 +235,7 @@ final class BankBillStatementImporter
|
||||
$bankName === '网商银行' => (string) ($metadata['company_name'] ?? ''),
|
||||
$isBocMonthly => $this->bocAccountHolderName($valuesByLabel),
|
||||
$isHuaxiaPersonal, $isAgriculturalMonthly => (string) ($metadata['company_name'] ?? ''),
|
||||
$isJiangnanMonthly => (string) ($account->company_name ?? ''),
|
||||
default => (string) ($metadata['company_name'] ?? $this->firstValue(
|
||||
$valuesByLabel,
|
||||
['单位名称', '公司名称', '账号名称', '账户名称', '己方银行户名'],
|
||||
@ -262,11 +268,11 @@ final class BankBillStatementImporter
|
||||
} else {
|
||||
$income = $this->amount($this->firstValue(
|
||||
$valuesByLabel,
|
||||
['收入金额', '收入金额(元)', '贷方金额'],
|
||||
['收入金额', '收入金额(元)', '贷方金额', '收入'],
|
||||
));
|
||||
$expense = $this->amount($this->firstValue(
|
||||
$valuesByLabel,
|
||||
['支出金额', '支出金额(元)', '借方金额'],
|
||||
['支出金额', '支出金额(元)', '借方金额', '支出'],
|
||||
));
|
||||
}
|
||||
$balanceValue = $this->firstValue(
|
||||
@ -284,6 +290,7 @@ final class BankBillStatementImporter
|
||||
'凭证号',
|
||||
'凭证号码',
|
||||
'交易流水号[ Transaction reference number ]',
|
||||
'序号',
|
||||
],
|
||||
);
|
||||
if ($isBocMonthly) {
|
||||
@ -294,7 +301,7 @@ final class BankBillStatementImporter
|
||||
} else {
|
||||
$counterpartyName = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
['对方户名', '对方姓名', '对手方别名', '收(付)方名称'],
|
||||
['对方户名', '对方姓名', '对手方别名', '对方账户名称', '收(付)方名称'],
|
||||
);
|
||||
$counterpartyAccount = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
@ -833,6 +840,79 @@ final class BankBillStatementImporter
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0:int,1:array<int,string>,2:array<string,mixed>}
|
||||
*/
|
||||
private function jiangnanMonthlyHeaderAndMetadata(PayablesWorkbookStreamReader $reader): array
|
||||
{
|
||||
$requiredHeaders = BankBillTemplateExporter::headers(
|
||||
'江南农商行',
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
);
|
||||
$headerRow = null;
|
||||
$headers = [];
|
||||
$accountNo = '';
|
||||
$periodStart = '';
|
||||
$periodEnd = '';
|
||||
|
||||
foreach ($reader->rows(0, null, 20) as $rowNumber => $cells) {
|
||||
$values = array_map(fn (mixed $value): string => $this->normaliseCell($value), $cells);
|
||||
$keys = array_keys($values);
|
||||
foreach ($values as $index => $value) {
|
||||
$position = array_search($index, $keys, true);
|
||||
$nextValue = '';
|
||||
for ($cursor = (int) $position + 1; $cursor < count($keys); $cursor++) {
|
||||
$candidate = trim((string) ($values[$keys[$cursor]] ?? ''));
|
||||
if ($candidate !== '') {
|
||||
$nextValue = $candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($nextValue === '') {
|
||||
continue;
|
||||
}
|
||||
if ($value === '账号') {
|
||||
$accountNo = $this->digits($nextValue);
|
||||
} elseif ($value === '开始日期') {
|
||||
$periodStart = $nextValue;
|
||||
} elseif ($value === '结束日期') {
|
||||
$periodEnd = $nextValue;
|
||||
}
|
||||
}
|
||||
|
||||
$candidateHeaders = [];
|
||||
foreach ($values as $index => $value) {
|
||||
if ($value !== '') {
|
||||
$candidateHeaders[(int) $index] = $value;
|
||||
}
|
||||
}
|
||||
if (count(array_intersect(array_values($candidateHeaders), $requiredHeaders))
|
||||
=== count($requiredHeaders)) {
|
||||
$headerRow = $rowNumber;
|
||||
$headers = $candidateHeaders;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($headerRow === null) {
|
||||
throw new InvalidArgumentException('没有找到江南农商行账单明细表头。');
|
||||
}
|
||||
if ($accountNo === '') {
|
||||
throw new InvalidArgumentException('江南农商行账单缺少账号。');
|
||||
}
|
||||
|
||||
return [
|
||||
$headerRow,
|
||||
$headers,
|
||||
[
|
||||
'account_no' => $accountNo,
|
||||
'currency' => '人民币',
|
||||
'period_start' => $periodStart,
|
||||
'period_end' => $periodEnd,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0:int,1:array<int,string>,2:array<string,mixed>}
|
||||
*/
|
||||
|
||||
@ -152,6 +152,18 @@ final class BankBillTemplateExporter
|
||||
'对方开户行',
|
||||
'摘要',
|
||||
],
|
||||
'江南农商行' => [
|
||||
'序号',
|
||||
'交易日期',
|
||||
'币种',
|
||||
'收入',
|
||||
'支出',
|
||||
'余额',
|
||||
'摘要',
|
||||
'备注',
|
||||
'对方账号',
|
||||
'对方账户名称',
|
||||
],
|
||||
'中国银行' => [
|
||||
'交易类型[ Transaction Type ]',
|
||||
'业务类型[ Business type ]',
|
||||
@ -310,7 +322,7 @@ final class BankBillTemplateExporter
|
||||
|
||||
return match ($bank) {
|
||||
'中国工商银行' => 'pdf',
|
||||
'华夏银行个人卡', '农业银行' => self::FORMAT_BANK_FILE,
|
||||
'华夏银行个人卡', '农业银行', '江南农商行' => self::FORMAT_BANK_FILE,
|
||||
default => 'xlsx',
|
||||
};
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ use App\Models\Auth\Role;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\EcommerceFinance\Services\BankBillStatementImporter;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerBankBalanceCurrentCalculator;
|
||||
use Modules\EcommerceFinance\Services\BankBillTemplateExporter;
|
||||
use Modules\EcommerceFinance\View\BankBillPageData;
|
||||
@ -513,17 +514,19 @@ class BankBillPageTest extends FeatureTestCase
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
));
|
||||
|
||||
$this->assertCount(6, $templates);
|
||||
$this->assertCount(7, $templates);
|
||||
$huaXia = $templates->firstWhere('bank', '华夏银行');
|
||||
$huaXiaPersonal = $templates->firstWhere('bank', '华夏银行个人卡');
|
||||
$hangzhou = $templates->firstWhere('bank', '杭州银行');
|
||||
$agricultural = $templates->firstWhere('bank', '农业银行');
|
||||
$jiangnan = $templates->firstWhere('bank', '江南农商行');
|
||||
$boc = $templates->firstWhere('bank', '中国银行');
|
||||
$icbc = $templates->firstWhere('bank', '中国工商银行');
|
||||
$this->assertIsArray($huaXia);
|
||||
$this->assertIsArray($huaXiaPersonal);
|
||||
$this->assertIsArray($hangzhou);
|
||||
$this->assertIsArray($agricultural);
|
||||
$this->assertIsArray($jiangnan);
|
||||
$this->assertIsArray($boc);
|
||||
$this->assertIsArray($icbc);
|
||||
$this->assertSame([
|
||||
@ -587,6 +590,19 @@ class BankBillPageTest extends FeatureTestCase
|
||||
'摘要',
|
||||
], $agricultural['headers']);
|
||||
$this->assertSame('bank_file', $agricultural['format']);
|
||||
$this->assertSame('bank_file', $jiangnan['format']);
|
||||
$this->assertSame([
|
||||
'序号',
|
||||
'交易日期',
|
||||
'币种',
|
||||
'收入',
|
||||
'支出',
|
||||
'余额',
|
||||
'摘要',
|
||||
'备注',
|
||||
'对方账号',
|
||||
'对方账户名称',
|
||||
], $jiangnan['headers']);
|
||||
$this->assertCount(38, $boc['headers']);
|
||||
$this->assertSame('交易类型[ Transaction Type ]', $boc['headers'][0]);
|
||||
$this->assertSame('交易金额[ Trade Amount ]', $boc['headers'][13]);
|
||||
@ -688,6 +704,17 @@ class BankBillPageTest extends FeatureTestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function test_jiangnan_bank_template_is_upload_only(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('江南农商行请直接上传银行导出的 XLS/XLSX 明细');
|
||||
|
||||
app(BankBillTemplateExporter::class)->create(
|
||||
'江南农商行',
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
);
|
||||
}
|
||||
|
||||
public function test_non_direct_template_route_downloads_the_monthly_workbook(): void
|
||||
{
|
||||
$this->user->roles()->sync([
|
||||
@ -1483,6 +1510,91 @@ class BankBillPageTest extends FeatureTestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_jiangnan_bank_import_maps_metadata_account_and_transactions(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$accountId = (int) $connection->table('fin_accounts')->insertGetId([
|
||||
'company_name' => '常州欧伯娜家居有限公司',
|
||||
'account_short' => '江南银行-欧伯娜',
|
||||
'account_type' => '公户',
|
||||
'bank_name' => '江苏江南农村商业银行股份有限公司常州市崔桥支行',
|
||||
'account_no' => '1069900000021044',
|
||||
'account_should_keep' => '是',
|
||||
'current_balance' => 0,
|
||||
'daily_available' => 0,
|
||||
'blocked_amount' => 0,
|
||||
]);
|
||||
$headers = [
|
||||
'序号',
|
||||
'交易日期',
|
||||
'币种',
|
||||
'收入',
|
||||
'支出',
|
||||
'余额',
|
||||
'摘要',
|
||||
'备注',
|
||||
'对方账号',
|
||||
'对方账户名称',
|
||||
];
|
||||
$path = $this->workbook([
|
||||
[],
|
||||
[null, null, '账号', '1069900000021044'],
|
||||
[null, null, '开始日期', '2026-11-01', null, '结束日期', '2026-11-30'],
|
||||
[],
|
||||
$headers,
|
||||
[
|
||||
'1', '2026-11-02 16:36:30', '人民币', '189754.82', null, '192372.24',
|
||||
'货款', '', '748467816042', '江苏一达通企业服务有限公司',
|
||||
],
|
||||
[
|
||||
'2', '2026-11-03 15:05:19', '人民币', null, '190000.00', '2372.24',
|
||||
'wl', 'wl', '13156000000067738', '常州欧伯娜家居有限公司',
|
||||
],
|
||||
]);
|
||||
|
||||
try {
|
||||
$result = app(BankBillStatementImporter::class)->import(
|
||||
$path,
|
||||
'江南农商行',
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
);
|
||||
|
||||
$this->assertSame(2, $result['rows']);
|
||||
$this->assertSame(1, $result['accounts']);
|
||||
$this->assertSame('2026-11-02', $result['first_date']);
|
||||
$this->assertSame('2026-11-03', $result['last_date']);
|
||||
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
||||
'finance_account_id' => $accountId,
|
||||
'bank_name' => '江南农商行',
|
||||
'company_name' => '常州欧伯娜家居有限公司',
|
||||
'account_short' => '江南银行-欧伯娜',
|
||||
'account_no' => '1069900000021044',
|
||||
'txn_at' => '2026-11-02 16:36:30',
|
||||
'counterparty_name' => '江苏一达通企业服务有限公司',
|
||||
'counterparty_account' => '748467816042',
|
||||
'detail' => '货款',
|
||||
'income' => 189754.82,
|
||||
'expense' => 0,
|
||||
'balance' => 192372.24,
|
||||
'source' => 'bank-monthly',
|
||||
], 'business_mirror');
|
||||
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
||||
'finance_account_id' => $accountId,
|
||||
'txn_at' => '2026-11-03 15:05:19',
|
||||
'counterparty_name' => '常州欧伯娜家居有限公司',
|
||||
'payment_remark' => 'wl',
|
||||
'income' => 0,
|
||||
'expense' => 190000,
|
||||
'balance' => 2372.24,
|
||||
], 'business_mirror');
|
||||
} finally {
|
||||
@unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $headers
|
||||
*/
|
||||
|
||||
@ -213,6 +213,57 @@ class ImportBankBillsTest extends FeatureTestCase
|
||||
], 'business_mirror');
|
||||
}
|
||||
|
||||
public function test_import_command_supports_non_direct_scope(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$connection->table('fin_accounts')->insert([
|
||||
'company_name' => '江南命令测试公司',
|
||||
'account_short' => '江南银行-命令测试',
|
||||
'account_type' => '公户',
|
||||
'bank_name' => '江苏江南农村商业银行股份有限公司常州市崔桥支行',
|
||||
'account_no' => '1069900000000099',
|
||||
'account_should_keep' => '是',
|
||||
]);
|
||||
$path = $this->workbook([
|
||||
[],
|
||||
[null, null, '账号', '1069900000000099'],
|
||||
[null, null, '开始日期', '2026-11-01', null, '结束日期', '2026-11-30'],
|
||||
[],
|
||||
['序号', '交易日期', '币种', '收入', '支出', '余额', '摘要', '备注', '对方账号', '对方账户名称'],
|
||||
['1', '2026-11-24 10:00:00', '人民币', '100.00', null, '1100.00', '货款', '', '6222000000000001', '测试付款方'],
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->artisan('finance-ledger:import-bank-bills', [
|
||||
'path' => $path,
|
||||
'--bank' => '江南农商行',
|
||||
'--scope' => 'non_direct',
|
||||
'--dry-run' => true,
|
||||
])->assertSuccessful();
|
||||
$this->assertDatabaseCount('fin_bank_direct_bills', 0, 'business_mirror');
|
||||
|
||||
$this->artisan('finance-ledger:import-bank-bills', [
|
||||
'path' => $path,
|
||||
'--bank' => '江南农商行',
|
||||
'--scope' => 'non_direct',
|
||||
])->assertSuccessful();
|
||||
} finally {
|
||||
@unlink($path);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
||||
'bank_name' => '江南农商行',
|
||||
'company_name' => '江南命令测试公司',
|
||||
'account_short' => '江南银行-命令测试',
|
||||
'account_no' => '1069900000000099',
|
||||
'record_code' => '1',
|
||||
'income' => 100,
|
||||
'expense' => 0,
|
||||
'balance' => 1100,
|
||||
'source' => 'bank-monthly',
|
||||
], 'business_mirror');
|
||||
}
|
||||
|
||||
public function test_imports_all_bank_bill_files_in_directory_and_syncs_once(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user