CaiWuXiTong/tests/Unit/EcommerceFinance/IcbcBankStatementPdfParserTest.php
shanshi 48304f4603 fix: support current ICBC PDF summary layout
Support the current ICBC PDF summary layout while preserving the legacy parser.
2026-12-01 11:53:02 +08:00

218 lines
8.3 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use DOMDocument;
use Modules\EcommerceFinance\Services\IcbcBankStatementPdfParser;
use PHPUnit\Framework\TestCase;
class IcbcBankStatementPdfParserTest extends TestCase
{
public function test_it_parses_icbc_bbox_text_with_multiline_and_reversal_rows(): void
{
$document = new DOMDocument('1.0', 'UTF-8');
$root = $document->createElement('doc');
$page = $document->createElement('page');
$root->appendChild($page);
$this->addLine($document, $page, 30.34, [
[52.1, '账号:'],
[74.5, '1105055609100026936'],
[542.4, '币种:'],
[564.9, '人民币'],
[676.2, '单位:'],
[698.6, '元'],
]);
$this->addLine($document, $page, 44.84, [
[52.1, '本方账号户名:'],
[102.5, '常州东仕卡电子商务有限公司'],
[542.4, '本方账号开户行:'],
[599.9, '工行常州崔桥支行'],
[676.2, '时间范围:'],
[712.6, '20261101'],
[739.9, '-'],
[744.0, '20261125'],
]);
$this->addLine($document, $page, 59.36, [
[64.7, '凭证号'],
[124.8, '对方账号'],
[188.0, '交易时间'],
[251.1, '借贷标志'],
[314.3, '对方单位'],
[377.4, '对方行号'],
[446.6, '用途'],
[509.7, '摘要'],
[572.9, '附言'],
[630.0, '转入金额'],
[693.2, '转出金额'],
[762.3, '余额'],
]);
$this->addLine($document, $page, 76.86, [
[61.2, '000000000'],
[124.4, '210401293'],
[176.3, '2026-11-25'],
[204.2, '17:13:31'],
[260.1, '借'],
[296.3, '网银在线(北京)支付'],
[372.8, '911100000005'],
[435.8, 'B2B在线支付'],
[499.0, 'B2B在线支付'],
[548.4, '2026112517130540118604'],
[695.4, '3,000.00'],
[757.2, '33,557.68'],
]);
$this->addLine($document, $page, 82.86, [
[308.3, '科技有限公司'],
[567.8, '17881770'],
]);
$this->addLine($document, $page, 99.86, [
[61.2, '000000000'],
[124.4, '13150000001959988'],
[176.3, '2026-11-03'],
[204.2, '15:24:00'],
[260.1, '借'],
[296.3, '常州宸拓安家居有限公司'],
[372.8, '304100040000'],
[435.8, '货款'],
[499.0, '货款'],
[695.4, '-50,000.00'],
[757.2, '61,670.34'],
]);
$this->addLine($document, $page, 130.0, [
[400.0, '重要提示'],
]);
$document->appendChild($root);
$result = (new IcbcBankStatementPdfParser())->parse($document->saveXML());
$this->assertSame('1105055609100026936', $result['account_no']);
$this->assertSame('常州东仕卡电子商务有限公司', $result['account_name']);
$this->assertSame('工行常州崔桥支行', $result['opening_bank']);
$this->assertSame('人民币', $result['currency']);
$this->assertSame('2026-11-01', $result['from']);
$this->assertSame('2026-11-25', $result['to']);
$this->assertCount(2, $result['rows']);
$this->assertSame('2026-11-25 17:13:31', $result['rows'][0]['txn_at']);
$this->assertSame('网银在线(北京)支付科技有限公司', $result['rows'][0]['counterparty_name']);
$this->assertSame('202611251713054011860417881770', $result['rows'][0]['remark']);
$this->assertSame(0.0, $result['rows'][0]['income']);
$this->assertSame(3000.0, $result['rows'][0]['expense']);
$this->assertSame(33557.68, $result['rows'][0]['balance']);
$this->assertSame('2026-11-03 15:24:00', $result['rows'][1]['txn_at']);
$this->assertSame(50000.0, $result['rows'][1]['income']);
$this->assertSame(0.0, $result['rows'][1]['expense']);
$this->assertSame(61670.34, $result['rows'][1]['balance']);
}
public function test_it_parses_icbc_summary_layout_with_occurrence_amount_column(): void
{
$document = new DOMDocument('1.0', 'UTF-8');
$root = $document->createElement('doc');
$page = $document->createElement('page');
$root->appendChild($page);
$this->addLine($document, $page, 30.34, [
[52.1, '账号:'],
[74.5, '1100000000000000001'],
[542.4, '币种:'],
[564.9, '人民币'],
[676.2, '单位:'],
[698.6, '元'],
]);
$this->addLine($document, $page, 44.84, [
[52.1, '本方账号户名:'],
[102.5, '测试企业有限公司'],
[542.4, '本方账号开户行:'],
[599.9, '工行测试支行'],
[676.2, '时间范围:'],
[712.6, '20260101'],
[739.9, '-'],
[744.0, '20260131'],
]);
$this->addLine($document, $page, 59.36, [
[64.7, '凭证号'],
[124.8, '对方账号'],
[188.0, '交易时间'],
[251.1, '借贷标志'],
[314.3, '对方单位'],
[383.4, '用途'],
[446.6, '摘要'],
[509.7, '附言'],
[572.9, '余额'],
[630.0, '转出金额'],
[693.2, '转入金额'],
[759.3, '发生额'],
]);
$this->addLine($document, $page, 79.86, [
[61.2, '000000000'],
[110.5, '6222000000000000001'],
[176.3, '2026-01-15'],
[204.2, '09:22:09'],
[260.1, '借'],
[317.3, '测试收款人'],
[383.4, '报销'],
[446.6, '报销'],
[569.1, '2,345.67'],
[630.9, '1,234.56'],
[757.2, '1,234.56'],
]);
$this->addLine($document, $page, 102.86, [
[61.2, '000000000'],
[114.7, '6222000000000000002'],
[176.3, '2026-01-16'],
[204.2, '10:16:26'],
[260.1, '贷'],
[317.3, '测试付款人'],
[446.6, '跨行'],
[567.7, '12,222.21'],
[694.0, '9,876.54'],
[757.2, '9,876.54'],
]);
$this->addLine($document, $page, 130.0, [
[400.0, '重要提示'],
]);
$document->appendChild($root);
$result = (new IcbcBankStatementPdfParser())->parse($document->saveXML());
$this->assertSame('1100000000000000001', $result['account_no']);
$this->assertSame('测试企业有限公司', $result['account_name']);
$this->assertSame('2026-01-01', $result['from']);
$this->assertSame('2026-01-31', $result['to']);
$this->assertCount(2, $result['rows']);
$this->assertSame('2026-01-15 09:22:09', $result['rows'][0]['txn_at']);
$this->assertSame('测试收款人', $result['rows'][0]['counterparty_name']);
$this->assertSame('', $result['rows'][0]['counterparty_bank']);
$this->assertSame('报销', $result['rows'][0]['purpose']);
$this->assertSame('报销', $result['rows'][0]['summary']);
$this->assertSame(0.0, $result['rows'][0]['income']);
$this->assertSame(1234.56, $result['rows'][0]['expense']);
$this->assertSame(2345.67, $result['rows'][0]['balance']);
$this->assertSame('2026-01-16 10:16:26', $result['rows'][1]['txn_at']);
$this->assertSame(9876.54, $result['rows'][1]['income']);
$this->assertSame(0.0, $result['rows'][1]['expense']);
$this->assertSame(12222.21, $result['rows'][1]['balance']);
}
/**
* @param list<array{0:float,1:string}> $words
*/
private function addLine(DOMDocument $document, \DOMElement $page, float $y, array $words): void
{
foreach ($words as [$x, $text]) {
$word = $document->createElement('word', $text);
$word->setAttribute('xMin', (string) $x);
$word->setAttribute('xMax', (string) ($x + max(4, mb_strlen($text) * 3)));
$word->setAttribute('yMin', (string) $y);
$word->setAttribute('yMax', (string) ($y + 6));
$page->appendChild($word);
}
}
}