fix(发票数据): 按报销发票流程核对付款金额
This commit is contained in:
parent
143ea2ef3f
commit
be55b3df0f
@ -2,6 +2,15 @@
|
||||
|
||||
return [
|
||||
'dataset_key' => 'operations_replica.rpt.ding_invoices',
|
||||
'document_types' => [
|
||||
'公摊日常报销',
|
||||
'店铺日常报销',
|
||||
'公摊借支-销账',
|
||||
'店铺借支-销账',
|
||||
'日常费用报销(板材中心)',
|
||||
'外贸发货费用报销',
|
||||
'工资流程',
|
||||
],
|
||||
'flow_batch_size' => (int) env('DING_INVOICE_SCAN_BATCH_SIZE', 20),
|
||||
'max_files_per_flow' => (int) env('DING_INVOICE_MAX_FILES_PER_FLOW', 30),
|
||||
'max_file_bytes' => (int) env('DING_INVOICE_MAX_FILE_BYTES', 60 * 1024 * 1024),
|
||||
|
||||
@ -115,7 +115,7 @@ return [
|
||||
'payment_table' => '付款明细',
|
||||
],
|
||||
'PROC-657E5EEA-DEAA-4DC5-AD8B-A679FE9AD361' => [
|
||||
'document_type' => '板材流程',
|
||||
'document_type' => '日常费用报销(板材中心)',
|
||||
'detail_table' => '收款费用明细',
|
||||
'detail_amount_fields' => ['收款金额'],
|
||||
'amount_total_fields' => ['收款合计'],
|
||||
|
||||
@ -7,6 +7,7 @@ use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\EcommerceFinance\Services\DingInvoiceWorkbookImporter;
|
||||
use Modules\EcommerceFinance\Support\DingInvoiceProcessScope;
|
||||
use RuntimeException;
|
||||
|
||||
class DingInvoiceDataMirrorRepository
|
||||
@ -59,9 +60,11 @@ class DingInvoiceDataMirrorRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
return (int) $this->connection->table('ding_invoice_flow_mirror')
|
||||
->where('source_scope', $this->scope())
|
||||
->count() > 0;
|
||||
$query = $this->connection->table('ding_invoice_flow_mirror')
|
||||
->where('source_scope', $this->scope());
|
||||
DingInvoiceProcessScope::apply($query);
|
||||
|
||||
return (int) $query->count() > 0;
|
||||
}
|
||||
|
||||
/** @return array<string,mixed>|null */
|
||||
@ -86,8 +89,11 @@ class DingInvoiceDataMirrorRepository
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->connection->table('ding_invoice_flow_mirror')
|
||||
->where('source_scope', $this->scope())
|
||||
$query = $this->connection->table('ding_invoice_flow_mirror')
|
||||
->where('source_scope', $this->scope());
|
||||
DingInvoiceProcessScope::apply($query);
|
||||
|
||||
return $query
|
||||
->whereNotNull('paid_month')
|
||||
->where('paid_month', '!=', '')
|
||||
->where('paid_month', '>=', self::FIRST_SUPPORTED_MONTH)
|
||||
@ -156,19 +162,24 @@ class DingInvoiceDataMirrorRepository
|
||||
return ['invoice_types' => []];
|
||||
}
|
||||
|
||||
$query = $this->connection->table('ding_invoice_record_mirror')
|
||||
->where('source_scope', $this->scope());
|
||||
$query = $this->connection->table('ding_invoice_record_mirror as r')
|
||||
->join('ding_invoice_flow_mirror as f', function ($join): void {
|
||||
$join->on('f.business_id', '=', 'r.business_id')
|
||||
->on('f.source_scope', '=', 'r.source_scope');
|
||||
})
|
||||
->where('r.source_scope', $this->scope());
|
||||
DingInvoiceProcessScope::apply($query, 'f');
|
||||
if (($filters['month'] ?? '') !== '') {
|
||||
$query->where('paid_month', $filters['month']);
|
||||
$query->where('r.paid_month', $filters['month']);
|
||||
}
|
||||
|
||||
return [
|
||||
'invoice_types' => $query
|
||||
->whereNotNull('invoice_type')
|
||||
->where('invoice_type', '!=', '')
|
||||
->whereNotNull('r.invoice_type')
|
||||
->where('r.invoice_type', '!=', '')
|
||||
->distinct()
|
||||
->orderBy('invoice_type')
|
||||
->pluck('invoice_type')
|
||||
->orderBy('r.invoice_type')
|
||||
->pluck('r.invoice_type')
|
||||
->map(static fn (mixed $value): string => (string) $value)
|
||||
->values()
|
||||
->all(),
|
||||
@ -362,6 +373,7 @@ class DingInvoiceDataMirrorRepository
|
||||
private function applyFlowFilters(Builder $query, array $filters, string $alias = 'f'): void
|
||||
{
|
||||
$query->where("{$alias}.source_scope", $this->scope());
|
||||
DingInvoiceProcessScope::apply($query, $alias);
|
||||
if (($filters['month'] ?? '') !== '') {
|
||||
$query->where("{$alias}.paid_month", $filters['month']);
|
||||
}
|
||||
@ -437,6 +449,7 @@ class DingInvoiceDataMirrorRepository
|
||||
{
|
||||
$query = $this->connection->table('ding_invoice_flow_mirror')
|
||||
->where('source_scope', $this->scope());
|
||||
DingInvoiceProcessScope::apply($query);
|
||||
if (($filters['month'] ?? '') !== '') {
|
||||
$query->where('paid_month', $filters['month']);
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ namespace Modules\EcommerceFinance\Services;
|
||||
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Modules\EcommerceFinance\Support\DingInvoiceProcessScope;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
@ -48,9 +49,11 @@ class DingInvoiceLocalAttachmentImporter
|
||||
: '',
|
||||
$manifestRows,
|
||||
)));
|
||||
$flows = $this->connection->table('ding_invoice_flow_mirror')
|
||||
$flowQuery = $this->connection->table('ding_invoice_flow_mirror')
|
||||
->where('source_scope', $scope)
|
||||
->whereIn('business_id', $businessIds)
|
||||
->whereIn('business_id', $businessIds);
|
||||
DingInvoiceProcessScope::apply($flowQuery);
|
||||
$flows = $flowQuery
|
||||
->get()
|
||||
->keyBy('business_id');
|
||||
$result = [
|
||||
|
||||
@ -7,6 +7,7 @@ use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\EcommerceFinance\Support\DingInvoiceProcessScope;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
@ -44,16 +45,17 @@ class DingInvoiceSyncService
|
||||
$scope = $this->scope();
|
||||
$this->ensureFlowsForMonth($month, $scope);
|
||||
if ($force) {
|
||||
$this->connection->table('ding_invoice_flow_mirror')
|
||||
$forceQuery = $this->connection->table('ding_invoice_flow_mirror')
|
||||
->where('source_scope', $scope)
|
||||
->where('paid_month', $month)
|
||||
->update([
|
||||
'scan_status' => 'pending',
|
||||
'scanned_at' => null,
|
||||
'scan_attempts' => 0,
|
||||
'next_scan_at' => null,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
->where('paid_month', $month);
|
||||
DingInvoiceProcessScope::apply($forceQuery);
|
||||
$forceQuery->update([
|
||||
'scan_status' => 'pending',
|
||||
'scanned_at' => null,
|
||||
'scan_attempts' => 0,
|
||||
'next_scan_at' => null,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
$query = $this->connection->table('ding_invoice_flow_mirror')
|
||||
@ -61,6 +63,7 @@ class DingInvoiceSyncService
|
||||
->where('paid_month', $month)
|
||||
->whereNotNull('process_instance_id')
|
||||
->where('process_instance_id', '!=', '');
|
||||
DingInvoiceProcessScope::apply($query);
|
||||
$this->applyScanCandidates($query);
|
||||
$flows = $query
|
||||
->orderByDesc('paid_at')
|
||||
@ -75,6 +78,7 @@ class DingInvoiceSyncService
|
||||
->where('paid_month', $month)
|
||||
->whereNotNull('process_instance_id')
|
||||
->where('process_instance_id', '!=', '');
|
||||
DingInvoiceProcessScope::apply($remainingQuery);
|
||||
$this->applyScanCandidates($remainingQuery);
|
||||
$remaining = (int) $remainingQuery->count();
|
||||
|
||||
@ -139,6 +143,7 @@ class DingInvoiceSyncService
|
||||
->where('paid_month', $month)
|
||||
->whereNotNull('process_instance_id')
|
||||
->where('process_instance_id', '!=', '');
|
||||
DingInvoiceProcessScope::apply($remainingQuery);
|
||||
$this->applyScanCandidates($remainingQuery);
|
||||
$metrics['remaining_flows'] = (int) $remainingQuery->count();
|
||||
$this->updateState($metrics);
|
||||
@ -156,6 +161,7 @@ class DingInvoiceSyncService
|
||||
->where('source_scope', $this->scope())
|
||||
->whereNotNull('process_instance_id')
|
||||
->where('process_instance_id', '!=', '');
|
||||
DingInvoiceProcessScope::apply($monthQuery);
|
||||
$this->applyScanCandidates($monthQuery);
|
||||
$month = $monthQuery->max('paid_month');
|
||||
|
||||
@ -255,6 +261,14 @@ class DingInvoiceSyncService
|
||||
$businessId = trim((string) $businessId);
|
||||
$detail = ($details->get($businessId) ?? collect())->first();
|
||||
$current = $existing->get($businessId);
|
||||
$documentType = trim((string) (
|
||||
$detail?->document_type
|
||||
?: $current?->document_type
|
||||
?: ''
|
||||
));
|
||||
if (! DingInvoiceProcessScope::allows($documentType)) {
|
||||
continue;
|
||||
}
|
||||
$values = [
|
||||
'source_scope' => $scope,
|
||||
'business_id' => $businessId,
|
||||
|
||||
36
modules/EcommerceFinance/Support/DingInvoiceProcessScope.php
Normal file
36
modules/EcommerceFinance/Support/DingInvoiceProcessScope.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\EcommerceFinance\Support;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
final class DingInvoiceProcessScope
|
||||
{
|
||||
/** @return list<string> */
|
||||
public static function documentTypes(): array
|
||||
{
|
||||
return array_values(array_unique(array_filter(array_map(
|
||||
static fn (mixed $value): string => trim((string) $value),
|
||||
(array) config('ding_invoice_data.document_types', []),
|
||||
))));
|
||||
}
|
||||
|
||||
public static function allows(?string $documentType): bool
|
||||
{
|
||||
return in_array(trim((string) $documentType), self::documentTypes(), true);
|
||||
}
|
||||
|
||||
public static function apply(Builder $query, string $alias = ''): void
|
||||
{
|
||||
$column = $alias === '' ? 'document_type' : "{$alias}.document_type";
|
||||
$documentTypes = self::documentTypes();
|
||||
|
||||
if ($documentTypes === []) {
|
||||
$query->whereRaw('1 = 0');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereIn($column, $documentTypes);
|
||||
}
|
||||
}
|
||||
@ -777,7 +777,7 @@
|
||||
|
||||
<div class="invoice-data-status">
|
||||
<span>
|
||||
数据口径:<strong>{{ $startMonth }} 起 · 通过 OA 审批流程附件自动识别</strong> · 按打款时间归属月份 · 一张发票一行
|
||||
数据口径:<strong>{{ $startMonth }} 起 · 仅统计报销发票流程 · 通过审批流程附件自动识别</strong> · 按打款时间归属月份 · 一张发票一行
|
||||
</span>
|
||||
<span>
|
||||
扫描状态:<strong>{{ $mirrorStatusLabels[$invoiceMirrorStatus] ?? $invoiceMirrorStatus }}</strong>
|
||||
@ -810,7 +810,7 @@
|
||||
>¥{{ $wan($summary['invoice_total'] ?? 0) }}</strong>
|
||||
</div>
|
||||
<div class="invoice-data-kpi">
|
||||
<span>付款合计(万元)</span>
|
||||
<span>本月付款(万元)</span>
|
||||
<strong
|
||||
title="¥{{ number_format((float) ($summary['payment_amount'] ?? 0), 2) }}"
|
||||
>¥{{ $wan($summary['payment_amount'] ?? 0) }}</strong>
|
||||
|
||||
@ -195,10 +195,11 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
->assertSee('含票流程')
|
||||
->assertSee('发票价税合计(万元)')
|
||||
->assertSee('开票金额覆盖率')
|
||||
->assertSee('付款合计(万元)')
|
||||
->assertSee('本月付款(万元)')
|
||||
->assertDontSee('invoice-data-kpi-unit', false)
|
||||
->assertSee('title="¥1,000.00"', false)
|
||||
->assertSee('通过 OA 审批流程附件自动识别')
|
||||
->assertSee('仅统计报销发票流程')
|
||||
->assertSee('通过审批流程附件自动识别')
|
||||
->assertSee('FLOW-INVOICE-1')
|
||||
->assertSee('10月办公用品报销')
|
||||
->assertSee('26320000000000000001')
|
||||
@ -311,6 +312,17 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
]);
|
||||
|
||||
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
||||
$export = $this->loginAs()->get(route('business.invoice-data.export', [
|
||||
'month' => '2026-10',
|
||||
'invoice_view' => DingInvoiceDataMirrorRepository::VIEW_FLOWS,
|
||||
'invoice_state' => 'all',
|
||||
]));
|
||||
$export->assertOk();
|
||||
$this->assertStringNotContainsString(
|
||||
'FLOW-OUTSIDE-SCOPE',
|
||||
$export->streamedContent(),
|
||||
);
|
||||
|
||||
$this->loginAs()
|
||||
->get(route('business.pages.show', [
|
||||
'section' => 'process-data',
|
||||
@ -382,6 +394,105 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
$this->assertSame(1, $flowResult['summary']['no_invoice_flows']);
|
||||
}
|
||||
|
||||
public function test_invoice_page_excludes_processes_outside_invoice_scope(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$now = now();
|
||||
$connection->table('ding_invoice_flow_mirror')->insert([
|
||||
'source_scope' => 'personal_permission',
|
||||
'business_id' => 'FLOW-OUTSIDE-SCOPE',
|
||||
'process_instance_id' => 'PROCESS-OUTSIDE-SCOPE',
|
||||
'paid_month' => '2026-10',
|
||||
'title' => '不应进入发票台账的采购流程',
|
||||
'originator_name' => '测试人员',
|
||||
'document_type' => '板材流程',
|
||||
'expense_type' => '采购',
|
||||
'payee_name' => '测试供应商',
|
||||
'paid_at' => '2026-10-14 00:00:00',
|
||||
'payment_amount' => 9999.00,
|
||||
'payment_total' => 9999.00,
|
||||
'invoice_count' => 1,
|
||||
'invoice_total' => 9999.00,
|
||||
'scan_status' => 'recognized',
|
||||
'attachment_count' => 1,
|
||||
'scanned_file_count' => 1,
|
||||
'source_name' => 'OA attachment scan',
|
||||
'scanned_at' => $now,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
$record = (array) $connection->table('ding_invoice_record_mirror')
|
||||
->where('business_id', 'FLOW-INVOICE-1')
|
||||
->first();
|
||||
unset($record['id']);
|
||||
$record['business_id'] = 'FLOW-OUTSIDE-SCOPE';
|
||||
$record['process_instance_id'] = 'PROCESS-OUTSIDE-SCOPE';
|
||||
$record['invoice_key'] = hash('sha256', 'outside-scope');
|
||||
$record['invoice_type'] = '不应出现的发票类型';
|
||||
$record['invoice_number'] = '26320000000000999999';
|
||||
$record['invoice_total'] = 9999.00;
|
||||
$record['source_file_id'] = 'FILE-OUTSIDE-SCOPE';
|
||||
$record['source_file_name'] = '不应展示.pdf';
|
||||
$connection->table('ding_invoice_record_mirror')->insert($record);
|
||||
|
||||
$attachment = (array) $connection->table('ding_invoice_attachment_mirror')
|
||||
->where('business_id', 'FLOW-INVOICE-1')
|
||||
->first();
|
||||
unset($attachment['id']);
|
||||
$attachment['business_id'] = 'FLOW-OUTSIDE-SCOPE';
|
||||
$attachment['process_instance_id'] = 'PROCESS-OUTSIDE-SCOPE';
|
||||
$attachment['attachment_key'] = hash('sha256', 'outside-scope-attachment');
|
||||
$attachment['source_file_id'] = 'ATTACHMENT-OUTSIDE-SCOPE';
|
||||
$attachment['source_file_name'] = '不应展示.png';
|
||||
$connection->table('ding_invoice_attachment_mirror')->insert($attachment);
|
||||
|
||||
$repository = app(DingInvoiceDataMirrorRepository::class);
|
||||
$detail = $repository->paginate([
|
||||
'month' => '2026-10',
|
||||
'view' => DingInvoiceDataMirrorRepository::VIEW_DETAIL,
|
||||
'invoice_state' => 'all',
|
||||
'invoice_type' => '',
|
||||
'keyword' => '',
|
||||
], 1, 50);
|
||||
$flows = $repository->paginate([
|
||||
'month' => '2026-10',
|
||||
'view' => DingInvoiceDataMirrorRepository::VIEW_FLOWS,
|
||||
'invoice_state' => 'all',
|
||||
'invoice_type' => '',
|
||||
'keyword' => '',
|
||||
], 1, 50);
|
||||
$attachments = $repository->paginateAttachments([
|
||||
'month' => '2026-10',
|
||||
'view' => DingInvoiceDataMirrorRepository::VIEW_ATTACHMENTS,
|
||||
'invoice_state' => 'all',
|
||||
'invoice_type' => '',
|
||||
'keyword' => '',
|
||||
], 1, 50);
|
||||
|
||||
$this->assertSame(1, $detail['total']);
|
||||
$this->assertSame(2, $flows['total']);
|
||||
$this->assertSame(2, $attachments['total']);
|
||||
$this->assertSame(2, $flows['summary']['flows']);
|
||||
$this->assertSame(1500.00, $flows['summary']['payment_amount']);
|
||||
$this->assertSame(1000.00, $flows['summary']['invoice_total']);
|
||||
$this->assertNotContains(
|
||||
'不应出现的发票类型',
|
||||
$repository->filterOptions(['month' => '2026-10'])['invoice_types'],
|
||||
);
|
||||
|
||||
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
||||
$this->loginAs()
|
||||
->get(route('business.pages.show', [
|
||||
'section' => 'process-data',
|
||||
'page' => 'invoice-data',
|
||||
'month' => '2026-10',
|
||||
]))
|
||||
->assertOk()
|
||||
->assertDontSee('FLOW-OUTSIDE-SCOPE')
|
||||
->assertDontSee('不应进入发票台账的采购流程');
|
||||
}
|
||||
|
||||
public function test_invoice_months_start_from_october(): void
|
||||
{
|
||||
$now = now();
|
||||
@ -391,6 +502,7 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
'process_instance_id' => 'PROCESS-SEPTEMBER',
|
||||
'paid_month' => '2026-09',
|
||||
'title' => '9月不应展示的流程',
|
||||
'document_type' => '公摊日常报销',
|
||||
'payment_amount' => 10,
|
||||
'payment_total' => 10,
|
||||
'invoice_count' => 0,
|
||||
@ -515,6 +627,7 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
'business_id' => 'FLOW-PENDING-OLDER',
|
||||
'process_instance_id' => 'PROCESS-PENDING-OLDER',
|
||||
'paid_month' => '2026-09',
|
||||
'document_type' => '公摊日常报销',
|
||||
'payment_amount' => 10,
|
||||
'payment_total' => 10,
|
||||
'invoice_count' => 0,
|
||||
@ -531,6 +644,7 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
'business_id' => 'FLOW-FAILED-NEWER',
|
||||
'process_instance_id' => 'PROCESS-FAILED-NEWER',
|
||||
'paid_month' => '2026-11',
|
||||
'document_type' => '公摊日常报销',
|
||||
'payment_amount' => 10,
|
||||
'payment_total' => 10,
|
||||
'invoice_count' => 0,
|
||||
@ -548,6 +662,7 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
'business_id' => 'FLOW-IMPORTED-NEWER',
|
||||
'process_instance_id' => 'PROCESS-IMPORTED-NEWER',
|
||||
'paid_month' => '2026-11',
|
||||
'document_type' => '公摊日常报销',
|
||||
'payment_amount' => 10,
|
||||
'payment_total' => 10,
|
||||
'invoice_count' => 1,
|
||||
@ -565,6 +680,7 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
'business_id' => 'FLOW-RECOGNIZED-LATEST',
|
||||
'process_instance_id' => 'PROCESS-RECOGNIZED-LATEST',
|
||||
'paid_month' => '2026-12',
|
||||
'document_type' => '公摊日常报销',
|
||||
'payment_amount' => 10,
|
||||
'payment_total' => 10,
|
||||
'invoice_count' => 1,
|
||||
@ -582,6 +698,7 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
'business_id' => 'FLOW-FAILED-BACKOFF',
|
||||
'process_instance_id' => 'PROCESS-FAILED-BACKOFF',
|
||||
'paid_month' => '2026-12',
|
||||
'document_type' => '公摊日常报销',
|
||||
'payment_amount' => 10,
|
||||
'payment_total' => 10,
|
||||
'invoice_count' => 0,
|
||||
@ -596,6 +713,23 @@ class InvoiceDataPageTest extends FeatureTestCase
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
DB::connection('business_mirror')->table('ding_invoice_flow_mirror')->insert([
|
||||
'source_scope' => 'personal_permission',
|
||||
'business_id' => 'FLOW-OUTSIDE-SCOPE-PENDING',
|
||||
'process_instance_id' => 'PROCESS-OUTSIDE-SCOPE-PENDING',
|
||||
'paid_month' => '2027-01',
|
||||
'document_type' => '板材流程',
|
||||
'payment_amount' => 10,
|
||||
'payment_total' => 10,
|
||||
'invoice_count' => 0,
|
||||
'invoice_total' => 0,
|
||||
'scan_status' => 'pending',
|
||||
'attachment_count' => 0,
|
||||
'scanned_file_count' => 0,
|
||||
'source_name' => 'OA attachment scan',
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
$this->assertSame(
|
||||
'2026-12',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user