[ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'foreign_key_constraints' => true, ], 'upstream_mirrors.connection' => 'business_mirror', 'upstream_mirrors.ding_process_details.scope' => 'personal_permission', ]); DB::purge('business_mirror'); $this->createInvoiceMirrorTables(); $now = now(); DB::connection('business_mirror')->table('business_mirror_table_states')->insert([ 'dataset_key' => 'operations_replica.rpt.ding_invoices', 'source_key' => 'operations_replica', 'source_object' => 'test', 'target_table' => 'ding_invoice_record_mirror', 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); DB::connection('business_mirror')->table('ding_invoice_flow_mirror')->insert([ [ 'source_scope' => 'personal_permission', 'business_id' => 'FLOW-INVOICE-1', 'process_instance_id' => 'PROCESS-1', 'paid_month' => '2026-10', 'title' => '10月办公用品报销', 'originator_name' => '张三', 'document_type' => '公摊日常报销', 'expense_type' => '办公用品', 'payee_name' => '测试供应商', 'paid_at' => '2026-10-12 00:00:00', 'payment_amount' => 1000.00, 'payment_total' => 1000.00, 'invoice_count' => 1, 'invoice_total' => 1000.00, 'scan_status' => 'recognized', 'attachment_count' => 1, 'scanned_file_count' => 1, 'last_error' => null, 'source_name' => 'test.xlsx', 'scanned_at' => $now, 'created_at' => $now, 'updated_at' => $now, ], [ 'source_scope' => 'personal_permission', 'business_id' => 'FLOW-NO-INVOICE', 'process_instance_id' => 'PROCESS-2', 'paid_month' => '2026-10', 'title' => '10月借支销账', 'originator_name' => '李四', 'document_type' => '公摊借支-销账', 'expense_type' => '借支', 'payee_name' => '员工', 'paid_at' => '2026-10-13 00:00:00', 'payment_amount' => 500.00, 'payment_total' => 500.00, 'invoice_count' => 0, 'invoice_total' => 0, 'scan_status' => 'no_invoice', 'attachment_count' => 0, 'scanned_file_count' => 0, 'last_error' => null, 'source_name' => 'test.xlsx', 'scanned_at' => $now, 'created_at' => $now, 'updated_at' => $now, ], ]); DB::connection('business_mirror')->table('ding_invoice_record_mirror')->insert([ 'source_scope' => 'personal_permission', 'business_id' => 'FLOW-INVOICE-1', 'process_instance_id' => 'PROCESS-1', 'paid_month' => '2026-10', 'invoice_key' => hash('sha256', 'number:26320000000000000001'), 'invoice_type' => '电子发票(普通发票)', 'seller_name' => '测试供应商', 'seller_tax_no' => '913200000000000001', 'buyer_name' => '测试主体', 'buyer_tax_no' => '913200000000000002', 'invoice_total' => 1000.00, 'invoice_number' => '26320000000000000001', 'invoice_date' => '2026-10-10', 'item_name' => '*办公用品*打印纸', 'amount_without_tax' => 884.96, 'tax_rate' => '13%', 'tax_amount' => 115.04, 'source_file_id' => null, 'source_file_name' => null, 'source_file_type' => null, 'source_field' => null, 'source_path' => null, 'source_hash' => null, 'ocr_provider' => 'test', 'recognition_payload' => null, 'created_at' => $now, 'updated_at' => $now, ]); } public function test_invoice_data_page_renders_detail_and_flow_views(): void { $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); $response = $this->loginAs()->get(route('business.pages.show', [ 'section' => 'process-data', 'page' => 'invoice-data', 'month' => '2026-10', 'per_page' => 50, ])); $response->assertOk() ->assertSee('发票数据') ->assertSee('付款月份') ->assertSee('发票状态') ->assertSee('发票明细') ->assertSee('按流程汇总') ->assertSee('抓取发票') ->assertSee('data-invoice-refresh', false) ->assertSee('付款流程') ->assertSee('发票价税合计') ->assertSee('开票金额覆盖率') ->assertSee('FLOW-INVOICE-1') ->assertSee('10月办公用品报销') ->assertSee('26320000000000000001') ->assertSee('测试供应商') ->assertSee('FLOW-NO-INVOICE') ->assertSee('无票'); $this->assertContains( 'invoice-data', array_column( collect(\App\Utilities\BusinessNavigation::all()) ->firstWhere('slug', 'process-data')['pages'] ?? [], 'slug', ), ); } public function test_repository_filters_invoice_type_and_state(): void { $repository = app(DingInvoiceDataMirrorRepository::class); $this->assertSame( '电子发票(普通发票)', DB::connection('business_mirror')->table('ding_invoice_record_mirror')->value('invoice_type'), ); $this->assertSame( 1, DB::connection('business_mirror') ->table('ding_invoice_record_mirror') ->where('item_name', 'like', '%打印纸%') ->count(), ); $invoiceResult = $repository->paginate([ 'month' => '2026-10', 'view' => DingInvoiceDataMirrorRepository::VIEW_DETAIL, 'invoice_state' => 'has_invoice', 'invoice_type' => '电子发票(普通发票)', 'keyword' => '打印纸', ], 1, 50); $flowResult = $repository->paginate([ 'month' => '2026-10', 'view' => DingInvoiceDataMirrorRepository::VIEW_FLOWS, 'invoice_state' => 'no_invoice', 'invoice_type' => '', 'keyword' => '', ], 1, 50); $this->assertSame(1, $invoiceResult['total']); $this->assertSame(1, $invoiceResult['summary']['flows']); $this->assertSame(1, $invoiceResult['summary']['invoice_count']); $this->assertSame(1, $flowResult['total']); $this->assertSame('FLOW-NO-INVOICE', $flowResult['data'][0]->business_id); $this->assertSame(1, $flowResult['summary']['flows']); $this->assertSame(1, $flowResult['summary']['no_invoice_flows']); } public function test_refresh_endpoint_queues_selected_month(): void { $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); Storage::fake('local'); $response = $this->loginAs()->postJson(route('business.invoice-data.refresh'), [ 'month' => '2026-10', 'force' => true, ]); $response->assertOk() ->assertJsonPath('success', true) ->assertJsonPath('queued', true) ->assertJsonPath('month', '2026-10'); $token = $response->json('token'); $this->getJson(route('business.invoice-data.refresh-status', ['token' => $token])) ->assertOk() ->assertJsonPath('refresh.status', 'pending') ->assertJsonPath('refresh.month', '2026-10'); } private function createInvoiceMirrorTables(): 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->dateTime('initial_completed_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->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(); }); $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(); }); } }