CaiWuXiTong/tests/Unit/DingInvoiceTextParserTest.php
yueyang cc204fbb97 feat(发票数据): 自动增量抓取并补入OA发票 (#1496)
自动识别 PDF、图片、XLS/XLSX、ZIP、RAR、7Z 发票附件,每 15 分钟补扫未处理、失败和部分识别流程。
2026-11-27 10:44:37 +08:00

109 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Unit;
use Modules\EcommerceFinance\Services\DingInvoiceTextParser;
use Tests\TestCase;
class DingInvoiceTextParserTest extends TestCase
{
public function test_parses_standard_electronic_invoice_text(): void
{
$text = <<<'TEXT'
电子发票(普通发票)
发票号码:26320000000000000001
开票日期:2026年10月10日
购买方信息
名称:常州测试家居有限公司
统一社会信用代码/纳税人识别号:91320411MA00000001
销售方信息
名称:常州测试供应商有限公司
统一社会信用代码/纳税人识别号:91320411MA00000002
*办公用品*打印纸
金额:884.96
税率/征收率:13%
税额:115.04
价税合计(小写):¥1000.00
TEXT;
$invoice = app(DingInvoiceTextParser::class)->parse($text);
$this->assertNotNull($invoice);
$this->assertSame('电子发票(普通发票)', $invoice['invoice_type']);
$this->assertSame('26320000000000000001', $invoice['invoice_number']);
$this->assertSame('2026-10-10', $invoice['invoice_date']);
$this->assertSame('常州测试家居有限公司', $invoice['buyer_name']);
$this->assertSame('常州测试供应商有限公司', $invoice['seller_name']);
$this->assertEqualsWithDelta(1000.0, $invoice['invoice_total'], 0.001);
$this->assertSame('*办公用品*打印纸', $invoice['item_name']);
}
public function test_ignores_non_invoice_attachment_text(): void
{
$this->assertNull(
app(DingInvoiceTextParser::class)->parse('银行电子回单 金额 1000.00 无发票号码'),
);
}
public function test_rejects_incomplete_or_implausible_invoice_number(): void
{
$parser = app(DingInvoiceTextParser::class);
$this->assertNull($parser->parse(
'电子发票 发票号码:2632916682700356866 价税合计:¥164.00',
));
$this->assertNull($parser->parse(
'电子发票 发票号码:263291668270035686661 价税合计:¥164.00',
));
}
public function test_parses_pdf_text_with_labels_before_values(): void
{
$text = <<<'TEXT'
电子发票(普通发票) 发票号码:
开票日期:
26312000005356291576
2026年08月24日
常州悦宅家居有限公司
91320412MAEDY3UW0G
上海哩布哩布科技有限公司
91310104MADEA4HC59
*生产生活服务*软件信息技术服务
价税合计(大写)
合 计
(小写)
肆佰陆拾玖圆整 ¥469.00
442.45¥ 26.55¥
TEXT;
$invoice = app(DingInvoiceTextParser::class)->parse($text);
$this->assertNotNull($invoice);
$this->assertSame('26312000005356291576', $invoice['invoice_number']);
$this->assertEqualsWithDelta(469.0, $invoice['invoice_total'], 0.001);
}
public function test_parses_railway_ticket_and_split_invoice_number(): void
{
$text = <<<'TEXT'
电子发票(铁路电子客票)
发票号码:263291191 06000229017
开票日期:2026年09月26日
武进站
D8192
南京南站
票价:¥50.00
购买方名称:常州聚之时家居有限公司
统一社会信用代码:91320411MAK9R96P6H
TEXT;
$invoice = app(DingInvoiceTextParser::class)->parse($text);
$this->assertNotNull($invoice);
$this->assertSame('26329119106000229017', $invoice['invoice_number']);
$this->assertSame('电子发票(铁路电子客票)', $invoice['invoice_type']);
$this->assertSame('常州聚之时家居有限公司', $invoice['buyer_name']);
$this->assertEqualsWithDelta(50.0, $invoice['invoice_total'], 0.001);
}
}