From 8d9b190cfddf58cb23c39d5060423e69f534a090 Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Fri, 4 Dec 2026 16:57:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=8F=91=E7=A5=A8=E6=95=B0=E6=8D=AE):=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=89=E7=A5=A8=E6=97=A0=E7=A5=A8=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E5=B9=B6=E8=87=AA=E5=8A=A8=E6=8E=A5=E5=85=A5=E6=96=B0?= =?UTF-8?q?=E6=9C=88=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/ding_invoice_data.php | 1 + .../DingInvoiceDataMirrorRepository.php | 21 ++-- .../Services/DingInvoiceSyncService.php | 48 ++++++++ .../invoice-data.blade.php | 109 +++++++++++++++--- .../Feature/Business/InvoiceDataPageTest.php | 77 ++++++++++++- 5 files changed, 227 insertions(+), 29 deletions(-) diff --git a/config/ding_invoice_data.php b/config/ding_invoice_data.php index 8144caf2f..29a5a36c8 100644 --- a/config/ding_invoice_data.php +++ b/config/ding_invoice_data.php @@ -2,6 +2,7 @@ return [ 'dataset_key' => 'operations_replica.rpt.ding_invoices', + 'first_supported_month' => '2026-10', 'document_types' => [ '公摊日常报销', '店铺日常报销', diff --git a/modules/EcommerceFinance/Repositories/DingInvoiceDataMirrorRepository.php b/modules/EcommerceFinance/Repositories/DingInvoiceDataMirrorRepository.php index 36c924ab6..c173a34a0 100644 --- a/modules/EcommerceFinance/Repositories/DingInvoiceDataMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/DingInvoiceDataMirrorRepository.php @@ -23,10 +23,10 @@ class DingInvoiceDataMirrorRepository public const VIEW_ATTACHMENTS = 'attachments'; public const INVOICE_STATES = [ - 'all' => '全部状态', - 'has_invoice' => '有发票', - 'no_invoice' => '无票', - 'needs_review' => '待复核', + 'all' => '全部流程', + 'has_invoice' => '有票流程', + 'no_invoice' => '无票流程', + 'needs_review' => '待复核流程', ]; private ConnectionInterface $connection; @@ -381,13 +381,14 @@ class DingInvoiceDataMirrorRepository 'has_invoice' => $query->where("{$alias}.invoice_count", '>', 0), 'no_invoice' => $query ->where("{$alias}.invoice_count", '=', 0) + ->whereIn("{$alias}.scan_status", ['no_invoice', 'recognized', 'imported']) ->where(function (Builder $nested) use ($alias): void { $nested->whereNull("{$alias}.last_error") ->orWhere("{$alias}.last_error", '=', ''); }), 'needs_review' => $query ->where(function (Builder $nested) use ($alias): void { - $nested->whereIn("{$alias}.scan_status", ['partial', 'failed']) + $nested->whereIn("{$alias}.scan_status", ['pending', 'partial', 'failed']) ->orWhere(function (Builder $failed) use ($alias): void { $failed->whereNotNull("{$alias}.last_error") ->where("{$alias}.last_error", '!=', ''); @@ -457,12 +458,13 @@ class DingInvoiceDataMirrorRepository $query->where('invoice_count', '>', 0); } elseif (($filters['invoice_state'] ?? 'all') === 'no_invoice') { $query->where('invoice_count', 0) + ->whereIn('scan_status', ['no_invoice', 'recognized', 'imported']) ->where(function (Builder $nested): void { $nested->whereNull('last_error')->orWhere('last_error', ''); }); } elseif (($filters['invoice_state'] ?? 'all') === 'needs_review') { $query->where(function (Builder $nested): void { - $nested->whereIn('scan_status', ['partial', 'failed']) + $nested->whereIn('scan_status', ['pending', 'partial', 'failed']) ->orWhere(function (Builder $failed): void { $failed->whereNotNull('last_error')->where('last_error', '!=', ''); }); @@ -546,10 +548,15 @@ class DingInvoiceDataMirrorRepository 'invoice_total' => $invoice, 'no_invoice_flows' => $rows->filter( static fn (object $row): bool => (int) $row->invoice_count === 0 + && in_array((string) $row->scan_status, ['no_invoice', 'recognized', 'imported'], true) && ($row->last_error === null || $row->last_error === ''), )->count(), 'needs_review_flows' => $rows->filter( - static fn (object $row): bool => in_array((string) $row->scan_status, ['partial', 'failed'], true) + static fn (object $row): bool => in_array( + (string) $row->scan_status, + ['pending', 'partial', 'failed'], + true, + ) || ($row->last_error !== null && $row->last_error !== ''), )->count(), 'invoice_rate' => $payment > 0 ? $invoice / $payment : 0, diff --git a/modules/EcommerceFinance/Services/DingInvoiceSyncService.php b/modules/EcommerceFinance/Services/DingInvoiceSyncService.php index 46a065d0d..7d670732b 100644 --- a/modules/EcommerceFinance/Services/DingInvoiceSyncService.php +++ b/modules/EcommerceFinance/Services/DingInvoiceSyncService.php @@ -157,6 +157,8 @@ class DingInvoiceSyncService public function latestPendingMonth(): ?string { + $this->ensureMissingAvailableMonths(); + $monthQuery = $this->connection->table('ding_invoice_flow_mirror') ->where('source_scope', $this->scope()) ->whereNotNull('process_instance_id') @@ -168,6 +170,52 @@ class DingInvoiceSyncService return trim((string) $month) ?: null; } + private function ensureMissingAvailableMonths(): void + { + $schema = Schema::connection($this->connection->getName()); + if (! $schema->hasTable('ding_process_payment_line_mirror') + || ! $schema->hasTable('ding_process_detail_mirror')) { + return; + } + + $scope = $this->scope(); + $query = $this->connection->table('ding_process_payment_line_mirror as payment') + ->join('ding_process_detail_mirror as detail', function ($join): void { + $join->on('detail.source_scope', '=', 'payment.source_scope') + ->on('detail.business_id', '=', 'payment.business_id'); + }) + ->where('payment.source_scope', $scope) + ->whereNotNull('payment.paid_month') + ->where('payment.paid_month', '!=', '') + ->where( + 'payment.paid_month', + '>=', + (string) config('ding_invoice_data.first_supported_month', '2026-10'), + ) + ->where('payment.paid_month', '<=', now()->format('Y-m')); + DingInvoiceProcessScope::apply($query, 'detail'); + + $months = $query + ->distinct() + ->orderBy('payment.paid_month') + ->pluck('payment.paid_month') + ->map(static fn (mixed $month): string => trim((string) $month)) + ->filter() + ->values(); + + foreach ($months as $month) { + $hasFlows = $this->connection->table('ding_invoice_flow_mirror') + ->where('source_scope', $scope) + ->where('paid_month', $month); + DingInvoiceProcessScope::apply($hasFlows); + if ($hasFlows->exists()) { + continue; + } + + $this->ensureFlowsForMonth($month, $scope); + } + } + private function applyScanCandidates(Builder $query): void { $now = now(); diff --git a/resources/views/business/pages/financial-operations/invoice-data.blade.php b/resources/views/business/pages/financial-operations/invoice-data.blade.php index 65e3e1ffe..452dbb8af 100644 --- a/resources/views/business/pages/financial-operations/invoice-data.blade.php +++ b/resources/views/business/pages/financial-operations/invoice-data.blade.php @@ -9,11 +9,12 @@ $activeView = $invoiceActiveView ?? 'detail'; $startMonth = $invoiceStartMonth ?? '2026-10'; $statusLabels = [ - 'imported' => '已扫描', + 'imported' => '已导入', 'recognized' => '已识别', - 'no_invoice' => '无票', + 'no_invoice' => '已扫描', 'partial' => '部分识别', 'failed' => '识别失败', + 'pending' => '待扫描', ]; $mirrorStatusLabels = [ 'succeeded' => '已同步', @@ -36,6 +37,21 @@ 'invoice_view' => 'attachments', 'per_page' => $invoicePerPage, ])); + $hasInvoiceFlowUrl = $invoicePageUrl . '?' . http_build_query(array_merge($filterQuery, [ + 'invoice_state' => 'has_invoice', + 'invoice_view' => 'flows', + 'per_page' => $invoicePerPage, + ])); + $noInvoiceFlowUrl = $invoicePageUrl . '?' . http_build_query(array_merge($filterQuery, [ + 'invoice_state' => 'no_invoice', + 'invoice_view' => 'flows', + 'per_page' => $invoicePerPage, + ])); + $needsReviewFlowUrl = $invoicePageUrl . '?' . http_build_query(array_merge($filterQuery, [ + 'invoice_state' => 'needs_review', + 'invoice_view' => 'flows', + 'per_page' => $invoicePerPage, + ])); @endphp