feat: 增加支付宝账单导入模板 (#1581)
## 实际改动 - 新增支付宝“账务组合查询”导入模板,保留原账单 19 个字段、账号行和查询日期行。 - 支付宝模板导入后自动按支付宝账号定位账户,并写入“朱俊祥支付宝 · 全部明细”,同时维护账户映射和期初余额。 - 非银企直连模板列表新增“支付宝账单模板”。 ## 验证证据 - `BankBillPageTest`:27 项通过,242 个断言。 - 已验证导入数据能进入朱俊祥支付宝独立明细视图。 ## 风险检查 - 不涉及数据库结构迁移、生产配置、权限或上游连接变更。 Reviewed-on: #1581
This commit is contained in:
parent
cd9e0a39d9
commit
56563e9ae6
@ -24,6 +24,7 @@ final class BankBillStatementImporter
|
||||
public function __construct(
|
||||
DatabaseManager $databases,
|
||||
private readonly FinanceLedgerBankBalanceSynchronizer $balanceSynchronizer,
|
||||
private readonly CompanyAlipayStatementImporter $alipayStatementImporter,
|
||||
)
|
||||
{
|
||||
$this->connection = $databases->connection(
|
||||
@ -66,6 +67,8 @@ final class BankBillStatementImporter
|
||||
&& $templateBank === '农业银行';
|
||||
$isJiangnanMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '江南农商行';
|
||||
$isAlipayMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '支付宝';
|
||||
if (! is_file($path) || ! is_readable($path)) {
|
||||
throw new InvalidArgumentException('银行账单文件不存在或不可读取。');
|
||||
}
|
||||
@ -88,6 +91,8 @@ final class BankBillStatementImporter
|
||||
[$headerRow, $headers, $metadata] = $this->jiangnanMonthlyHeaderAndMetadata($reader);
|
||||
} elseif ($templateBank === '中国银行') {
|
||||
[$headerRow, $headers, $metadata] = $this->bocMonthlyHeaderAndMetadata($reader);
|
||||
} elseif ($isAlipayMonthly) {
|
||||
[$headerRow, $headers, $metadata] = $this->alipayHeaderAndMetadata($reader);
|
||||
} else {
|
||||
[$headerRow, $headers] = $this->headerRow($reader);
|
||||
}
|
||||
@ -113,6 +118,9 @@ final class BankBillStatementImporter
|
||||
[$headerRow, $headers] = $this->headerRow($reader);
|
||||
[$sourceIndexByKey, $labelBySourceIndex] = $this->configuredSourceIndexes($columns, $headers);
|
||||
}
|
||||
if ($isAlipayMonthly) {
|
||||
return $this->importAlipayStatement($path, $metadata, $dryRun);
|
||||
}
|
||||
$accountsByNumber = $this->accountsByNumber($bankName);
|
||||
$isBocMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT
|
||||
&& $templateBank === '中国银行';
|
||||
@ -216,7 +224,8 @@ final class BankBillStatementImporter
|
||||
$isBocMonthly,
|
||||
$isHuaxiaPersonal,
|
||||
$isAgriculturalMonthly,
|
||||
$isJiangnanMonthly => (string) ($metadata['account_no'] ?? ''),
|
||||
$isJiangnanMonthly,
|
||||
$isAlipayMonthly => (string) ($metadata['account_no'] ?? ''),
|
||||
default => $this->accountNumber($valuesByLabel),
|
||||
};
|
||||
$account = $accountsByNumber[$this->digits($accountNo)] ?? null;
|
||||
@ -268,22 +277,24 @@ final class BankBillStatementImporter
|
||||
} else {
|
||||
$income = $this->amount($this->firstValue(
|
||||
$valuesByLabel,
|
||||
['收入金额', '收入金额(元)', '贷方金额', '收入'],
|
||||
['收入金额', '收入金额(元)', '收入(+元)', '贷方金额', '收入'],
|
||||
));
|
||||
$expense = $this->amount($this->firstValue(
|
||||
$valuesByLabel,
|
||||
['支出金额', '支出金额(元)', '借方金额', '支出'],
|
||||
['支出金额', '支出金额(元)', '支出(-元)', '借方金额', '支出'],
|
||||
));
|
||||
}
|
||||
$balanceValue = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
['账户余额', '己方账户余额(元)', '余额', '交易后余额[ After-transaction balance ]'],
|
||||
['账户余额', '账户余额(元)', '己方账户余额(元)', '余额', '交易后余额[ After-transaction balance ]'],
|
||||
);
|
||||
$balance = $balanceValue === '' ? null : $this->amount($balanceValue);
|
||||
$recordCode = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
[
|
||||
'账务流水号',
|
||||
'支付宝流水号',
|
||||
'支付宝交易号',
|
||||
'银行流水号',
|
||||
'核心流水号',
|
||||
'流水号',
|
||||
@ -301,11 +312,11 @@ final class BankBillStatementImporter
|
||||
} else {
|
||||
$counterpartyName = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
['对方户名', '对方姓名', '对手方别名', '对方账户名称', '收(付)方名称'],
|
||||
['对方户名', '对方姓名', '对方名称', '对手方别名', '对方账户名称', '收(付)方名称'],
|
||||
);
|
||||
$counterpartyAccount = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
['对方账号', '对方卡/账号', '对手方银行账号', '收(付)方账号'],
|
||||
['对方账号', '对方账户', '对方卡/账号', '对手方银行账号', '收(付)方账号'],
|
||||
);
|
||||
$counterpartyBank = $this->firstValue(
|
||||
$valuesByLabel,
|
||||
@ -315,12 +326,15 @@ final class BankBillStatementImporter
|
||||
$detailParts = [];
|
||||
foreach ([
|
||||
'交易类型',
|
||||
'账务类型',
|
||||
'业务类型[ Business type ]',
|
||||
'摘要',
|
||||
'摘要[ Reference ]',
|
||||
'业务摘要',
|
||||
'其它摘要',
|
||||
'交易描述',
|
||||
'商品名称',
|
||||
'业务描述',
|
||||
'渠道',
|
||||
] as $label) {
|
||||
$value = trim((string) ($valuesByLabel[$label] ?? ''));
|
||||
@ -332,6 +346,7 @@ final class BankBillStatementImporter
|
||||
$valuesByLabel,
|
||||
[
|
||||
'备注',
|
||||
'付款备注',
|
||||
'用途',
|
||||
'附言',
|
||||
'附言(用途)',
|
||||
@ -656,6 +671,114 @@ final class BankBillStatementImporter
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0:int,1:array<int,string>,2:array<string,mixed>}
|
||||
*/
|
||||
private function alipayHeaderAndMetadata(PayablesWorkbookStreamReader $reader): array
|
||||
{
|
||||
$headerRow = null;
|
||||
$headers = [];
|
||||
$accountNo = '';
|
||||
$from = '';
|
||||
$to = '';
|
||||
|
||||
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 ($accountNo === '' && str_starts_with($value, '#账号:')) {
|
||||
preg_match('/#账号[::]\s*([^\s\[]+)/u', $value, $matches);
|
||||
$accountNo = $this->digits($matches[1] ?? '');
|
||||
}
|
||||
if (($from === '' || $to === '') && str_contains($value, '查询起始日期')) {
|
||||
preg_match(
|
||||
'/查询起始日期[::]\s*(\d{4}-\d{2}-\d{2}).*?查询终止日期[::]\s*(\d{4}-\d{2}-\d{2})/u',
|
||||
$value,
|
||||
$matches,
|
||||
);
|
||||
$from = $matches[1] ?? '';
|
||||
$to = $matches[2] ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
$candidateHeaders = [];
|
||||
foreach ($values as $index => $value) {
|
||||
if ($value !== '') {
|
||||
$candidateHeaders[(int) $index] = $value;
|
||||
}
|
||||
}
|
||||
if (count(array_intersect(
|
||||
array_values($candidateHeaders),
|
||||
['入账时间', '支付宝交易号', '收入(+元)', '支出(-元)', '账户余额(元)'],
|
||||
)) === 5) {
|
||||
$headerRow = $rowNumber;
|
||||
$headers = $candidateHeaders;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($headerRow === null) {
|
||||
throw new InvalidArgumentException('没有找到支付宝账单明细表头。');
|
||||
}
|
||||
if ($accountNo === '') {
|
||||
throw new InvalidArgumentException('支付宝账单缺少“#账号”信息。');
|
||||
}
|
||||
if ($from === '' || $to === '') {
|
||||
throw new InvalidArgumentException('支付宝账单缺少查询起始日期或终止日期。');
|
||||
}
|
||||
|
||||
return [
|
||||
$headerRow,
|
||||
$headers,
|
||||
[
|
||||
'account_no' => $accountNo,
|
||||
'currency' => '人民币',
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $metadata
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private function importAlipayStatement(
|
||||
string $path,
|
||||
array $metadata,
|
||||
bool $dryRun,
|
||||
): array {
|
||||
$accountNo = (string) ($metadata['account_no'] ?? '');
|
||||
$from = (string) ($metadata['from'] ?? '');
|
||||
$to = (string) ($metadata['to'] ?? '');
|
||||
$result = $this->alipayStatementImporter->import(
|
||||
$path,
|
||||
$accountNo,
|
||||
$from,
|
||||
$to,
|
||||
null,
|
||||
null,
|
||||
[],
|
||||
$dryRun,
|
||||
);
|
||||
|
||||
return [
|
||||
'scope' => BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
'bank' => '支付宝',
|
||||
'rows' => (int) ($result['rows'] ?? 0),
|
||||
'affected' => $dryRun ? 0 : (int) ($result['rows'] ?? 0),
|
||||
'inserted' => $dryRun ? 0 : (int) ($result['rows'] ?? 0),
|
||||
'updated' => 0,
|
||||
'skipped' => 0,
|
||||
'accounts' => 1,
|
||||
'synced_accounts' => 0,
|
||||
'first_date' => (string) ($result['from'] ?? $from),
|
||||
'last_date' => (string) ($result['to'] ?? $to),
|
||||
'duplicate_rows' => (int) ($result['replaced'] ?? 0),
|
||||
'dry_run' => $dryRun,
|
||||
'account_numbers' => [$accountNo],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0:int,1:array<int,string>,2:array<string,mixed>}
|
||||
*/
|
||||
@ -1270,6 +1393,7 @@ final class BankBillStatementImporter
|
||||
private function transactionDateTime(array $values): ?array
|
||||
{
|
||||
$dateValue = $this->firstValue($values, [
|
||||
'入账时间',
|
||||
'交易日',
|
||||
'交易日期',
|
||||
'记账日期',
|
||||
|
||||
@ -218,6 +218,27 @@ final class BankBillTemplateExporter
|
||||
'转出金额',
|
||||
'余额',
|
||||
],
|
||||
'支付宝' => [
|
||||
'序号',
|
||||
'入账时间',
|
||||
'支付宝交易号',
|
||||
'支付宝流水号',
|
||||
'商户订单号',
|
||||
'账务类型',
|
||||
'收入(+元)',
|
||||
'支出(-元)',
|
||||
'账户余额(元)',
|
||||
'对方账户',
|
||||
'对方名称',
|
||||
'银行订单号',
|
||||
'商品名称',
|
||||
'备注',
|
||||
'业务基础订单号',
|
||||
'业务订单号',
|
||||
'业务账单来源',
|
||||
'业务描述',
|
||||
'付款备注',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
@ -251,6 +272,7 @@ final class BankBillTemplateExporter
|
||||
'label' => match ($bank) {
|
||||
'中国工商银行' => '工商银行 PDF 明细',
|
||||
'华夏银行个人卡' => '华夏银行个人卡明细',
|
||||
'支付宝' => '支付宝账单模板',
|
||||
'农业银行' => '农业银行明细',
|
||||
default => $bank . '月度模板',
|
||||
},
|
||||
@ -365,9 +387,11 @@ final class BankBillTemplateExporter
|
||||
$headers = self::headers($bank, $scope);
|
||||
$isHuaXiaMonthly = $scope === self::SCOPE_NON_DIRECT && $bank === '华夏银行';
|
||||
$isBocMonthly = $scope === self::SCOPE_NON_DIRECT && $bank === '中国银行';
|
||||
$isAlipayMonthly = $scope === self::SCOPE_NON_DIRECT && $bank === '支付宝';
|
||||
$displayName = match (true) {
|
||||
$isHuaXiaMonthly => '华夏银行月度',
|
||||
$isBocMonthly => '中国银行月度',
|
||||
$isAlipayMonthly => '支付宝',
|
||||
default => $bank,
|
||||
};
|
||||
|
||||
@ -379,11 +403,13 @@ final class BankBillTemplateExporter
|
||||
$sheet->setTitle(match (true) {
|
||||
$isHuaXiaMonthly => '华夏月度明细',
|
||||
$isBocMonthly => '中行月度明细',
|
||||
$isAlipayMonthly => '支付宝明细',
|
||||
default => $bank,
|
||||
});
|
||||
$headerRow = match (true) {
|
||||
$isHuaXiaMonthly => 12,
|
||||
$isBocMonthly => 9,
|
||||
$isAlipayMonthly => 3,
|
||||
$bank === '网商银行' => 5,
|
||||
default => 1,
|
||||
};
|
||||
@ -447,6 +473,18 @@ final class BankBillTemplateExporter
|
||||
->setARGB('FFF7F0DE');
|
||||
$sheet->getStyle('A2:A4')->getFont()->setBold(true);
|
||||
$sheet->getStyle('E2:E4')->getFont()->setBold(true);
|
||||
} elseif ($isAlipayMonthly) {
|
||||
$sheet->fromArray([
|
||||
['#账号:请填写支付宝账号'],
|
||||
['#查询起始日期:请填写开始时间查询终止日期:请填写结束时间'],
|
||||
], null, 'A1', true);
|
||||
$sheet->mergeCells('A1:S1');
|
||||
$sheet->mergeCells('A2:S2');
|
||||
$sheet->getStyle('A1:S2')->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()
|
||||
->setARGB('FFF7F0DE');
|
||||
$sheet->getStyle('A1:S2')->getFont()->setBold(true);
|
||||
}
|
||||
|
||||
$sheet->fromArray([$headers], null, 'A' . $headerRow, true);
|
||||
@ -522,10 +560,15 @@ final class BankBillTemplateExporter
|
||||
|
||||
private function numberFormat(string $label): ?string
|
||||
{
|
||||
if (str_contains($label, '金额') || str_contains($label, '余额')) {
|
||||
if (str_contains($label, '金额')
|
||||
|| str_contains($label, '余额')
|
||||
|| str_contains($label, '收入(+元)')
|
||||
|| str_contains($label, '支出(-元)')) {
|
||||
return '#,##0.00';
|
||||
}
|
||||
if (str_starts_with($label, '交易时间') || $label === '提交时间') {
|
||||
if (str_starts_with($label, '交易时间')
|
||||
|| $label === '提交时间'
|
||||
|| $label === '入账时间') {
|
||||
return 'yyyy-mm-dd hh:mm:ss';
|
||||
}
|
||||
if ($label === '交易日'
|
||||
|
||||
@ -514,7 +514,7 @@ class BankBillPageTest extends FeatureTestCase
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
));
|
||||
|
||||
$this->assertCount(7, $templates);
|
||||
$this->assertCount(8, $templates);
|
||||
$huaXia = $templates->firstWhere('bank', '华夏银行');
|
||||
$huaXiaPersonal = $templates->firstWhere('bank', '华夏银行个人卡');
|
||||
$hangzhou = $templates->firstWhere('bank', '杭州银行');
|
||||
@ -522,6 +522,7 @@ class BankBillPageTest extends FeatureTestCase
|
||||
$jiangnan = $templates->firstWhere('bank', '江南农商行');
|
||||
$boc = $templates->firstWhere('bank', '中国银行');
|
||||
$icbc = $templates->firstWhere('bank', '中国工商银行');
|
||||
$alipay = $templates->firstWhere('bank', '支付宝');
|
||||
$this->assertIsArray($huaXia);
|
||||
$this->assertIsArray($huaXiaPersonal);
|
||||
$this->assertIsArray($hangzhou);
|
||||
@ -529,6 +530,7 @@ class BankBillPageTest extends FeatureTestCase
|
||||
$this->assertIsArray($jiangnan);
|
||||
$this->assertIsArray($boc);
|
||||
$this->assertIsArray($icbc);
|
||||
$this->assertIsArray($alipay);
|
||||
$this->assertSame([
|
||||
'序号',
|
||||
'交易日期',
|
||||
@ -610,6 +612,28 @@ class BankBillPageTest extends FeatureTestCase
|
||||
$this->assertSame('交易流水号[ Transaction reference number ]', $boc['headers'][17]);
|
||||
$this->assertSame('名义收款人名称[ Name of nominal payee ]', $boc['headers'][37]);
|
||||
$this->assertSame('pdf', $icbc['format']);
|
||||
$this->assertSame('支付宝账单模板', $alipay['label']);
|
||||
$this->assertSame([
|
||||
'序号',
|
||||
'入账时间',
|
||||
'支付宝交易号',
|
||||
'支付宝流水号',
|
||||
'商户订单号',
|
||||
'账务类型',
|
||||
'收入(+元)',
|
||||
'支出(-元)',
|
||||
'账户余额(元)',
|
||||
'对方账户',
|
||||
'对方名称',
|
||||
'银行订单号',
|
||||
'商品名称',
|
||||
'备注',
|
||||
'业务基础订单号',
|
||||
'业务订单号',
|
||||
'业务账单来源',
|
||||
'业务描述',
|
||||
'付款备注',
|
||||
], $alipay['headers']);
|
||||
$this->assertSame([
|
||||
'凭证号',
|
||||
'对方账号',
|
||||
@ -655,6 +679,19 @@ class BankBillPageTest extends FeatureTestCase
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
$this->assertSame($boc['headers'], $workbookHeaders);
|
||||
@unlink($bocTemplate['path']);
|
||||
|
||||
$alipayTemplate = app(BankBillTemplateExporter::class)->create(
|
||||
'支付宝',
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
);
|
||||
$this->assertSame('支付宝账单导入模板.xlsx', $alipayTemplate['filename']);
|
||||
$spreadsheet = IOFactory::load($alipayTemplate['path']);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$this->assertSame('#账号:请填写支付宝账号', $sheet->getCell('A1')->getValue());
|
||||
$workbookHeaders = $sheet->rangeToArray('A3:S3', null, true, false)[0];
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
$this->assertSame($alipay['headers'], $workbookHeaders);
|
||||
@unlink($alipayTemplate['path']);
|
||||
}
|
||||
|
||||
public function test_hua_xia_template_route_downloads_the_workbook(): void
|
||||
@ -1595,6 +1632,95 @@ class BankBillPageTest extends FeatureTestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_alipay_statement_import_maps_to_the_independent_account_page(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$connection->table('fin_accounts')->insert([
|
||||
'company_name' => '朱俊祥',
|
||||
'account_short' => '朱俊祥支付宝',
|
||||
'account_type' => '支付宝',
|
||||
'bank_name' => '/',
|
||||
'account_no' => '15302677690',
|
||||
'alipay_merchant_no' => '2088352584730874',
|
||||
'account_should_keep' => '是',
|
||||
'current_balance' => 0,
|
||||
'daily_available' => 0,
|
||||
'blocked_amount' => 0,
|
||||
]);
|
||||
$headers = collect(BankBillTemplateExporter::templates(
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
))->firstWhere('bank', '支付宝')['headers'];
|
||||
$path = $this->workbook([
|
||||
['#账号:15302677690[20883525847308740156]'],
|
||||
['#查询起始日期:2026-11-01 00:00:00查询终止日期:2026-11-30 17:07:23'],
|
||||
$headers,
|
||||
[
|
||||
'1',
|
||||
'2026-11-30 16:51:02',
|
||||
'2026113023001412571400853527',
|
||||
'1570843095388912571',
|
||||
'47960286562302235912575',
|
||||
'在线支付',
|
||||
'5360.00',
|
||||
null,
|
||||
'23093.33',
|
||||
'158******89',
|
||||
'**君',
|
||||
null,
|
||||
'收钱码收款',
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
'尾款',
|
||||
],
|
||||
]);
|
||||
|
||||
try {
|
||||
$result = app(BankBillStatementImporter::class)->import(
|
||||
$path,
|
||||
'支付宝',
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
BankBillTemplateExporter::SCOPE_NON_DIRECT,
|
||||
);
|
||||
|
||||
$this->assertSame(1, $result['rows']);
|
||||
$this->assertSame(1, $result['accounts']);
|
||||
$this->assertSame(['15302677690'], $result['account_numbers']);
|
||||
$this->assertDatabaseHas('fin_transactions', [
|
||||
'account_short' => '朱俊祥支付宝',
|
||||
'txn_date' => '2026-11-30',
|
||||
'income' => 5360,
|
||||
'expense' => 0,
|
||||
'balance' => 23093.33,
|
||||
'source' => 'alipay-company',
|
||||
'source_value' => '15302677690',
|
||||
], 'business_mirror');
|
||||
$this->assertDatabaseHas('fin_account_map', [
|
||||
'source' => 'alipay-company',
|
||||
'source_value' => '15302677690',
|
||||
'account_short' => '朱俊祥支付宝',
|
||||
], 'business_mirror');
|
||||
|
||||
$this->loginAs();
|
||||
request()->query->set('txns_view', 'zhu-junxiang-alipay');
|
||||
request()->query->set('txns_from', '2026-11-01');
|
||||
request()->query->set('txns_to', '2026-11-30');
|
||||
$page = app(FinanceLedgerPageData::class)->page('transactions', null, 'fund-ledger');
|
||||
$row = collect($page['ledgerTransactions']->items())
|
||||
->first(fn (object $item): bool => ($item->effective_account_short ?? '') === '朱俊祥支付宝');
|
||||
$this->assertNotNull($row);
|
||||
$this->assertSame('2026113023001412571400853527', (string) $row->approval_no);
|
||||
$this->assertSame(5360.0, (float) $row->income);
|
||||
$this->assertSame(23093.33, (float) $row->balance);
|
||||
} finally {
|
||||
@unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $headers
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user