fix(银行账单): 正确导入个人卡流水 #1722
@ -63,6 +63,8 @@ final class BankBillStatementImporter
|
||||
$bankName = BankBillTemplateExporter::storageBankName($templateBank);
|
||||
$isHuaxiaPersonal = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '华夏银行个人卡';
|
||||
$isCmbPersonal = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '招商银行个人卡';
|
||||
$isAgriculturalMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '农业银行';
|
||||
$isJiangnanMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
@ -85,6 +87,8 @@ final class BankBillStatementImporter
|
||||
[$headerRow, $headers, $metadata] = $this->huaxiaMonthlyHeaderAndMetadata($reader);
|
||||
} elseif ($templateBank === '华夏银行个人卡') {
|
||||
[$headerRow, $headers, $metadata] = $this->huaxiaPersonalHeaderAndMetadata($reader);
|
||||
} elseif ($isCmbPersonal) {
|
||||
[$headerRow, $headers, $metadata] = $this->cmbPersonalHeaderAndMetadata($reader);
|
||||
} elseif ($templateBank === '农业银行') {
|
||||
[$headerRow, $headers, $metadata] = $this->agriculturalMonthlyHeaderAndMetadata($reader);
|
||||
} elseif ($templateBank === '江南农商行') {
|
||||
@ -192,6 +196,10 @@ final class BankBillStatementImporter
|
||||
if ($isHuaxiaPersonal && $this->isHuaxiaPersonalFooterRow($cells)) {
|
||||
continue;
|
||||
}
|
||||
if ($isCmbPersonal
|
||||
&& str_starts_with($this->normaliseCell($cells[0] ?? null), '#')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rawData = [];
|
||||
foreach ($sourceIndexByKey as $sourceIndex => $columnKey) {
|
||||
@ -222,10 +230,11 @@ final class BankBillStatementImporter
|
||||
$accountNo = match (true) {
|
||||
$bankName === '网商银行',
|
||||
$isBocMonthly,
|
||||
$isHuaxiaPersonal,
|
||||
$isCmbPersonal,
|
||||
$isAgriculturalMonthly,
|
||||
$isJiangnanMonthly,
|
||||
$isAlipayMonthly => (string) ($metadata['account_no'] ?? ''),
|
||||
$isHuaxiaPersonal => $this->huaxiaPersonalAccountNumber($metadata, $accountsByNumber),
|
||||
default => $this->accountNumber($valuesByLabel),
|
||||
};
|
||||
$account = $accountsByNumber[$this->digits($accountNo)] ?? null;
|
||||
@ -243,7 +252,7 @@ final class BankBillStatementImporter
|
||||
$companyName = match (true) {
|
||||
$bankName === '网商银行' => (string) ($metadata['company_name'] ?? ''),
|
||||
$isBocMonthly => $this->bocAccountHolderName($valuesByLabel),
|
||||
$isHuaxiaPersonal, $isAgriculturalMonthly => (string) ($metadata['company_name'] ?? ''),
|
||||
$isHuaxiaPersonal, $isCmbPersonal, $isAgriculturalMonthly => (string) ($metadata['company_name'] ?? ''),
|
||||
$isJiangnanMonthly => (string) ($account->company_name ?? ''),
|
||||
default => (string) ($metadata['company_name'] ?? $this->firstValue(
|
||||
$valuesByLabel,
|
||||
@ -309,6 +318,10 @@ final class BankBillStatementImporter
|
||||
$counterpartyName = $counterparty['name'];
|
||||
$counterpartyAccount = $counterparty['account'];
|
||||
$counterpartyBank = $counterparty['bank'];
|
||||
} elseif ($isCmbPersonal) {
|
||||
$counterpartyName = $this->firstValue($valuesByLabel, ['交易备注']);
|
||||
$counterpartyAccount = '';
|
||||
$counterpartyBank = '';
|
||||
} else {
|
||||
$counterpartyName = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
@ -355,6 +368,7 @@ final class BankBillStatementImporter
|
||||
'交易附言[ Remark ]',
|
||||
'用途[ Purpose ]',
|
||||
'备注[ Remarks ]',
|
||||
'交易备注',
|
||||
],
|
||||
);
|
||||
$currency = $bankName === '网商银行'
|
||||
@ -865,11 +879,8 @@ final class BankBillStatementImporter
|
||||
}
|
||||
|
||||
$companyName = trim((string) ($rawMetadata['company_name'] ?? ''));
|
||||
$accountNo = $this->digits((string) (
|
||||
$rawMetadata['account_no']
|
||||
?? $rawMetadata['card_no']
|
||||
?? ''
|
||||
));
|
||||
$accountNo = $this->digits((string) ($rawMetadata['account_no'] ?? ''));
|
||||
$cardNo = $this->digits((string) ($rawMetadata['card_no'] ?? ''));
|
||||
if ($accountNo === '') {
|
||||
throw new InvalidArgumentException('华夏银行个人卡明细缺少“账号”或“卡号”。');
|
||||
}
|
||||
@ -881,12 +892,103 @@ final class BankBillStatementImporter
|
||||
'company_name' => $companyName,
|
||||
'account_short' => $companyName,
|
||||
'account_no' => $accountNo,
|
||||
'card_no' => $this->digits((string) ($rawMetadata['card_no'] ?? '')),
|
||||
'card_no' => $cardNo,
|
||||
'currency' => trim((string) ($rawMetadata['currency'] ?? '')) ?: '人民币',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function huaxiaPersonalAccountNumber(array $metadata, array $accountsByNumber): string
|
||||
{
|
||||
$cardNo = $this->digits((string) ($metadata['card_no'] ?? ''));
|
||||
if ($cardNo !== '' && isset($accountsByNumber[$cardNo])) {
|
||||
return $cardNo;
|
||||
}
|
||||
|
||||
return $this->digits((string) ($metadata['account_no'] ?? ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0:int,1:array<int,string>,2:array<string,mixed>}
|
||||
*/
|
||||
private function cmbPersonalHeaderAndMetadata(PayablesWorkbookStreamReader $reader): array
|
||||
{
|
||||
$requiredHeaders = BankBillTemplateExporter::headers(
|
||||
'招商银行个人卡',
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
);
|
||||
$headerRow = null;
|
||||
$headers = [];
|
||||
$maskedAccountNo = '';
|
||||
|
||||
foreach ($reader->rows(0, null, 20) as $rowNumber => $cells) {
|
||||
$values = array_map(fn (mixed $value): string => $this->normaliseCell($value), $cells);
|
||||
foreach ($values as $value) {
|
||||
if ($maskedAccountNo === ''
|
||||
&& preg_match('/一卡通\s*[::]\s*([0-9*]{8,})/u', $value, $matches) === 1) {
|
||||
$maskedAccountNo = trim($matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
$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 (preg_match('/^(\d{4})\*+(\d{4})$/u', $maskedAccountNo, $matches) !== 1) {
|
||||
throw new InvalidArgumentException('招商银行个人卡明细缺少可用的一卡通账号信息。');
|
||||
}
|
||||
|
||||
$prefix = $matches[1];
|
||||
$suffix = $matches[2];
|
||||
$accounts = $this->connection->table('fin_accounts')
|
||||
->where('account_type', '个人卡')
|
||||
->where(static function ($query): void {
|
||||
$query->where('bank_name', 'like', '%招商银行%')
|
||||
->orWhere('account_short', 'like', '%招商银行%');
|
||||
})
|
||||
->whereNotNull('account_no')
|
||||
->where('account_no', '<>', '')
|
||||
->get(['id', 'company_name', 'account_short', 'account_no'])
|
||||
->filter(static function (object $account) use ($prefix, $suffix): bool {
|
||||
$accountNo = preg_replace('/\D+/', '', (string) $account->account_no) ?? '';
|
||||
|
||||
return str_starts_with($accountNo, $prefix) && str_ends_with($accountNo, $suffix);
|
||||
})
|
||||
->values();
|
||||
|
||||
if ($accounts->count() !== 1) {
|
||||
throw new InvalidArgumentException($accounts->isEmpty()
|
||||
? sprintf('未找到招商银行一卡通“%s********%s”对应的个人卡账户。', $prefix, $suffix)
|
||||
: sprintf('招商银行一卡通“%s********%s”匹配到多个个人卡账户,无法自动定位。', $prefix, $suffix));
|
||||
}
|
||||
|
||||
$account = $accounts->first();
|
||||
|
||||
return [
|
||||
$headerRow,
|
||||
$headers,
|
||||
[
|
||||
'company_name' => (string) $account->company_name,
|
||||
'account_short' => (string) $account->account_short,
|
||||
'account_no' => preg_replace('/\D+/', '', (string) $account->account_no) ?? '',
|
||||
'currency' => '人民币',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0:int,1:array<int,string>,2:array<string,mixed>}
|
||||
*/
|
||||
|
||||
@ -124,6 +124,15 @@ final class BankBillTemplateExporter
|
||||
'对方开户行',
|
||||
'附言',
|
||||
],
|
||||
'招商银行个人卡' => [
|
||||
'交易日期',
|
||||
'交易时间',
|
||||
'收入',
|
||||
'支出',
|
||||
'余额',
|
||||
'交易类型',
|
||||
'交易备注',
|
||||
],
|
||||
'杭州银行' => [
|
||||
'序号',
|
||||
'交易时间',
|
||||
@ -272,6 +281,7 @@ final class BankBillTemplateExporter
|
||||
'label' => match ($bank) {
|
||||
'中国工商银行' => '工商银行 PDF 明细',
|
||||
'华夏银行个人卡' => '华夏银行个人卡明细',
|
||||
'招商银行个人卡' => '招商银行个人卡明细',
|
||||
'支付宝' => '支付宝账单模板',
|
||||
'农业银行' => '农业银行明细',
|
||||
default => $bank . '月度模板',
|
||||
@ -344,16 +354,18 @@ final class BankBillTemplateExporter
|
||||
|
||||
return match ($bank) {
|
||||
'中国工商银行' => 'pdf',
|
||||
'华夏银行个人卡', '农业银行', '江南农商行' => self::FORMAT_BANK_FILE,
|
||||
'华夏银行个人卡', '招商银行个人卡', '农业银行', '江南农商行' => self::FORMAT_BANK_FILE,
|
||||
default => 'xlsx',
|
||||
};
|
||||
}
|
||||
|
||||
public static function storageBankName(string $templateBank): string
|
||||
{
|
||||
return $templateBank === '华夏银行个人卡'
|
||||
? '华夏银行'
|
||||
: $templateBank;
|
||||
return match ($templateBank) {
|
||||
'华夏银行个人卡' => '华夏银行',
|
||||
'招商银行个人卡' => '招商银行',
|
||||
default => $templateBank,
|
||||
};
|
||||
}
|
||||
|
||||
public static function uploadOnlyMessage(string $bank, string $scope): ?string
|
||||
|
||||
@ -535,9 +535,10 @@ class BankBillPageTest extends FeatureTestCase
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
));
|
||||
|
||||
$this->assertCount(8, $templates);
|
||||
$this->assertCount(9, $templates);
|
||||
$huaXia = $templates->firstWhere('bank', '华夏银行');
|
||||
$huaXiaPersonal = $templates->firstWhere('bank', '华夏银行个人卡');
|
||||
$cmbPersonal = $templates->firstWhere('bank', '招商银行个人卡');
|
||||
$hangzhou = $templates->firstWhere('bank', '杭州银行');
|
||||
$agricultural = $templates->firstWhere('bank', '农业银行');
|
||||
$jiangnan = $templates->firstWhere('bank', '江南农商行');
|
||||
@ -546,6 +547,7 @@ class BankBillPageTest extends FeatureTestCase
|
||||
$alipay = $templates->firstWhere('bank', '支付宝');
|
||||
$this->assertIsArray($huaXia);
|
||||
$this->assertIsArray($huaXiaPersonal);
|
||||
$this->assertIsArray($cmbPersonal);
|
||||
$this->assertIsArray($hangzhou);
|
||||
$this->assertIsArray($agricultural);
|
||||
$this->assertIsArray($jiangnan);
|
||||
@ -583,6 +585,17 @@ class BankBillPageTest extends FeatureTestCase
|
||||
'附言',
|
||||
], $huaXiaPersonal['headers']);
|
||||
$this->assertSame('bank_file', $huaXiaPersonal['format']);
|
||||
$this->assertSame('招商银行个人卡明细', $cmbPersonal['label']);
|
||||
$this->assertSame([
|
||||
'交易日期',
|
||||
'交易时间',
|
||||
'收入',
|
||||
'支出',
|
||||
'余额',
|
||||
'交易类型',
|
||||
'交易备注',
|
||||
], $cmbPersonal['headers']);
|
||||
$this->assertSame('bank_file', $cmbPersonal['format']);
|
||||
$this->assertSame([
|
||||
'序号',
|
||||
'交易时间',
|
||||
@ -1367,7 +1380,7 @@ class BankBillPageTest extends FeatureTestCase
|
||||
'account_short' => '李金林华夏',
|
||||
'account_type' => '个人卡',
|
||||
'bank_name' => '华夏银行',
|
||||
'account_no' => '1315000000000099',
|
||||
'account_no' => '6222000000000099',
|
||||
'account_should_keep' => '是',
|
||||
'current_balance' => 1000,
|
||||
]);
|
||||
@ -1438,7 +1451,7 @@ class BankBillPageTest extends FeatureTestCase
|
||||
'bank_name' => '华夏银行',
|
||||
'company_name' => '李金林测试',
|
||||
'account_short' => '李金林华夏',
|
||||
'account_no' => '1315000000000099',
|
||||
'account_no' => '6222000000000099',
|
||||
'txn_at' => '2026-11-25 17:39:29',
|
||||
'counterparty_name' => '测试收款方',
|
||||
'counterparty_account' => '6222000000000001',
|
||||
@ -1465,6 +1478,92 @@ class BankBillPageTest extends FeatureTestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_cmb_personal_csv_import_maps_masked_card_number_and_transactions(): void
|
||||
{
|
||||
$this->user->roles()->sync([
|
||||
Role::where('name', 'admin')->firstOrFail()->id,
|
||||
]);
|
||||
$this->user->unsetRelation('roles');
|
||||
$connection = DB::connection('business_mirror');
|
||||
$accountId = $connection->table('fin_accounts')->insertGetId([
|
||||
'company_name' => '招商个人测试',
|
||||
'account_short' => '招商个人测试',
|
||||
'account_type' => '个人卡',
|
||||
'bank_name' => '招商银行常州分行营业部',
|
||||
'account_no' => '6214850340219999',
|
||||
'account_should_keep' => '是',
|
||||
'current_balance' => 77737.85,
|
||||
]);
|
||||
$path = sys_get_temp_dir() . '/cmb-personal-' . bin2hex(random_bytes(6)) . '.csv';
|
||||
file_put_contents($path, implode("\n", [
|
||||
'"# 招商银行交易记录"',
|
||||
'"# 导出时间: [2026-12-03 11:19:39]"',
|
||||
'"# 账 号: [一卡通:6214********9999 招商银行]"',
|
||||
'"# 币 种: [人民币]"',
|
||||
'"# 起始日期: [20261101] 终止日期: [20261130]"',
|
||||
'"# 过滤设置: 无"',
|
||||
'',
|
||||
'"交易日期","交易时间","收入","支出","余额","交易类型","交易备注"',
|
||||
'" 20261130"," 16:52:36","","3729.44","53276.77","转账汇款"," 济南顺拓电子商务有限公司"',
|
||||
'" 20261129"," 09:47:10","3395.00","","77737.85","银联代付","支付宝支付科技有限公司"',
|
||||
'',
|
||||
'"# 收入合计: 1 笔,共 3395.00 元"',
|
||||
'"# 支出合计: 1 笔,共 3729.44 元"',
|
||||
]));
|
||||
$payload = [
|
||||
'scope' => BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
'bank' => '招商银行个人卡',
|
||||
'file' => new UploadedFile($path, '招商个人测试招行11.csv', null, null, true),
|
||||
];
|
||||
$previewUrl = route('business.financial-ledger.bank-bills.import.preview', [
|
||||
'company_id' => $this->company->id,
|
||||
]);
|
||||
$importUrl = route('business.financial-ledger.bank-bills.import', [
|
||||
'company_id' => $this->company->id,
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->loginAs()->post($previewUrl, $payload)
|
||||
->assertOk()
|
||||
->assertJsonPath('result.rows', 2)
|
||||
->assertJsonPath('result.accounts', 1)
|
||||
->assertJsonPath('result.skipped', 0)
|
||||
->assertJsonPath('result.first_date', '2026-11-29')
|
||||
->assertJsonPath('result.last_date', '2026-11-30');
|
||||
|
||||
$this->loginAs()->post($importUrl, $payload)
|
||||
->assertOk()
|
||||
->assertJsonPath('result.inserted', 2)
|
||||
->assertJsonPath('result.synced_accounts', 0);
|
||||
|
||||
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
||||
'finance_account_id' => $accountId,
|
||||
'bank_name' => '招商银行',
|
||||
'company_name' => '招商个人测试',
|
||||
'account_short' => '招商个人测试',
|
||||
'account_no' => '6214850340219999',
|
||||
'txn_at' => '2026-11-30 16:52:36',
|
||||
'counterparty_name' => '济南顺拓电子商务有限公司',
|
||||
'income' => 0,
|
||||
'expense' => 3729.44,
|
||||
'balance' => 53276.77,
|
||||
'source' => 'bank-monthly',
|
||||
], 'business_mirror');
|
||||
$this->assertDatabaseHas('fin_bank_direct_bills', [
|
||||
'finance_account_id' => $accountId,
|
||||
'account_no' => '6214850340219999',
|
||||
'txn_at' => '2026-11-29 09:47:10',
|
||||
'counterparty_name' => '支付宝支付科技有限公司',
|
||||
'income' => 3395,
|
||||
'expense' => 0,
|
||||
'balance' => 77737.85,
|
||||
'source' => 'bank-monthly',
|
||||
], 'business_mirror');
|
||||
} finally {
|
||||
@unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_agricultural_bank_import_maps_metadata_account_and_validates_footer_totals(): void
|
||||
{
|
||||
$this->user->roles()->sync([
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user