CaiWuXiTong/tests/Unit/EcommerceFinance/IcbcBankStatementPdfParserTest.php

125 lines
4.7 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']);
}
/**
* @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);
}
}
}