186 lines
8.7 KiB
PHP
186 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\EcommerceFinance\Services\DingInvoiceWorkbookImporter;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Tests\TestCase;
|
|
|
|
class ImportDingInvoiceWorkbookTest extends TestCase
|
|
{
|
|
private string $file;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
'foreign_key_constraints' => true,
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
]);
|
|
DB::purge('business_mirror');
|
|
$this->createTables();
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setTitle('报销发票识别');
|
|
$sheet->fromArray([
|
|
[
|
|
'单号/标题', '流程号', '提交人', '报销类型', '费用类型', '收款人',
|
|
'打款时间', '10月付款金额', '付款合计', '发票类型', '销售方名称',
|
|
'销售方税号', '购买方名称', '购买方税号', '发票价税合计', '发票号码',
|
|
'开票日期', '项目名称', '金额', '税率', '税额', '金额校对', '差异金额',
|
|
],
|
|
[
|
|
'10月办公用品报销', 'FLOW-INVOICE-1', '张三', '公摊日常报销', '办公用品',
|
|
'测试供应商', '2026-10-12', 1000, 1000, '电子发票(普通发票)',
|
|
'测试供应商', '913200000000000001', '测试主体', '913200000000000002',
|
|
1000, '26320000000000000001', '2026-10-10', '*办公用品*打印纸',
|
|
884.96, '13%', 115.04, '一致', 0,
|
|
],
|
|
[
|
|
null, null, null, null, null, null, null, null, null,
|
|
'电子发票(普通发票)', '测试供应商', '913200000000000001',
|
|
'测试主体', '913200000000000002', 1000, '26320000000000000001',
|
|
'2026-10-10', '*办公用品*打印纸', 884.96, '13%', 115.04, null, null,
|
|
],
|
|
[
|
|
'10月借支销账', 'FLOW-NO-INVOICE', '李四', '公摊借支-销账', '借支',
|
|
'员工', '2026-10-13', 500, 500, '无票', null, null, null, null,
|
|
0, null, null, null, null, null, null, '无票', null,
|
|
],
|
|
[
|
|
'10月软件服务费', 'FLOW-INVOICE-2', '王五', '公摊日常报销', '软件服务费',
|
|
'测试服务商', '2026-10-14', 200, 200, '电子发票(普通发票)',
|
|
'测试服务商', '913200000000000003', '测试主体', '913200000000000002',
|
|
200, '26320000000000000001', '2026-10-14', '*软件服务*服务费',
|
|
188.68, '6%', 11.32, '一致', 0,
|
|
],
|
|
]);
|
|
$this->file = tempnam(sys_get_temp_dir(), 'invoice-import-') . '.xlsx';
|
|
(new Xlsx($spreadsheet))->save($this->file);
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if (isset($this->file) && is_file($this->file)) {
|
|
unlink($this->file);
|
|
}
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_imports_one_invoice_row_per_invoice_and_keeps_no_invoice_flow(): void
|
|
{
|
|
$result = app(DingInvoiceWorkbookImporter::class)->import($this->file);
|
|
|
|
$this->assertSame(3, $result['flows']);
|
|
$this->assertSame(2, $result['invoices']);
|
|
$this->assertSame(['2026-10'], $result['months']);
|
|
$this->assertEqualsWithDelta(1700.0, $result['payment_total'], 0.001);
|
|
$this->assertEqualsWithDelta(1200.0, $result['invoice_total'], 0.001);
|
|
$this->assertDatabaseHas('ding_invoice_flow_mirror', [
|
|
'business_id' => 'FLOW-INVOICE-1',
|
|
'invoice_count' => 1,
|
|
'scan_status' => 'recognized',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('ding_invoice_flow_mirror', [
|
|
'business_id' => 'FLOW-NO-INVOICE',
|
|
'invoice_count' => 0,
|
|
'scan_status' => 'no_invoice',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseCount('ding_invoice_record_mirror', 2, 'business_mirror');
|
|
}
|
|
|
|
private function createTables(): void
|
|
{
|
|
$schema = Schema::connection('business_mirror');
|
|
$schema->create('business_mirror_table_states', function (Blueprint $table): void {
|
|
$table->string('dataset_key')->primary();
|
|
$table->string('source_key');
|
|
$table->string('source_object');
|
|
$table->string('target_table');
|
|
$table->string('status')->default('pending');
|
|
$table->string('run_mode')->nullable();
|
|
$table->uuid('generation')->nullable();
|
|
$table->json('cursor')->nullable();
|
|
$table->json('high_watermark')->nullable();
|
|
$table->dateTime('window_started_at')->nullable();
|
|
$table->dateTime('window_ends_at')->nullable();
|
|
$table->dateTime('initial_completed_at')->nullable();
|
|
$table->dateTime('last_full_succeeded_at')->nullable();
|
|
$table->dateTime('last_attempted_at')->nullable();
|
|
$table->dateTime('last_succeeded_at')->nullable();
|
|
$table->unsignedBigInteger('last_upserted_rows')->default(0);
|
|
$table->unsignedBigInteger('mirror_row_count')->default(0);
|
|
$table->boolean('mirror_row_count_is_exact')->default(false);
|
|
$table->string('last_error_code')->nullable();
|
|
$table->timestamps();
|
|
});
|
|
$schema->create('ding_invoice_flow_mirror', function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->string('source_scope');
|
|
$table->string('business_id');
|
|
$table->string('process_instance_id')->nullable();
|
|
$table->string('paid_month');
|
|
$table->string('title')->nullable();
|
|
$table->string('originator_name')->nullable();
|
|
$table->string('document_type')->nullable();
|
|
$table->string('expense_type')->nullable();
|
|
$table->string('payee_name')->nullable();
|
|
$table->dateTime('paid_at')->nullable();
|
|
$table->decimal('payment_amount', 18, 2)->default(0);
|
|
$table->decimal('payment_total', 18, 2)->default(0);
|
|
$table->unsignedInteger('invoice_count')->default(0);
|
|
$table->decimal('invoice_total', 18, 2)->default(0);
|
|
$table->string('scan_status')->default('imported');
|
|
$table->unsignedInteger('attachment_count')->default(0);
|
|
$table->unsignedInteger('scanned_file_count')->default(0);
|
|
$table->text('last_error')->nullable();
|
|
$table->string('source_name')->nullable();
|
|
$table->dateTime('scanned_at')->nullable();
|
|
$table->timestamps();
|
|
$table->unique(['source_scope', 'business_id']);
|
|
});
|
|
$schema->create('ding_invoice_record_mirror', function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->string('source_scope');
|
|
$table->string('business_id');
|
|
$table->string('process_instance_id')->nullable();
|
|
$table->string('paid_month');
|
|
$table->string('invoice_key');
|
|
$table->string('invoice_type')->nullable();
|
|
$table->string('seller_name')->nullable();
|
|
$table->string('seller_tax_no')->nullable();
|
|
$table->string('buyer_name')->nullable();
|
|
$table->string('buyer_tax_no')->nullable();
|
|
$table->decimal('invoice_total', 18, 2)->default(0);
|
|
$table->string('invoice_number')->nullable();
|
|
$table->date('invoice_date')->nullable();
|
|
$table->text('item_name')->nullable();
|
|
$table->decimal('amount_without_tax', 18, 2)->nullable();
|
|
$table->string('tax_rate')->nullable();
|
|
$table->decimal('tax_amount', 18, 2)->nullable();
|
|
$table->string('source_file_id')->nullable();
|
|
$table->string('source_file_name')->nullable();
|
|
$table->string('source_file_type')->nullable();
|
|
$table->string('source_field')->nullable();
|
|
$table->string('source_path')->nullable();
|
|
$table->string('source_hash')->nullable();
|
|
$table->string('ocr_provider')->nullable();
|
|
$table->json('recognition_payload')->nullable();
|
|
$table->timestamps();
|
|
$table->unique(['source_scope', 'business_id', 'invoice_key']);
|
|
});
|
|
}
|
|
}
|