From fb088b5bb86660361c32a63a9d9d1edf84673535 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 17 Nov 2026 17:49:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=8F=91=E7=A5=A8?= =?UTF-8?q?=E9=A2=9D=E5=BA=A6=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Business/InvoiceQuotaPageData.php | 211 ++++++++++++ app/Http/Controllers/Business/Pages.php | 1 + config/business_navigation.php | 1 + .../business/pages/invoice_quota.blade.php | 319 ++++++++++++++++++ .../Feature/Business/InvoiceQuotaPageTest.php | 108 ++++++ 5 files changed, 640 insertions(+) create mode 100644 app/Business/InvoiceQuotaPageData.php create mode 100644 resources/views/business/pages/invoice_quota.blade.php create mode 100644 tests/Feature/Business/InvoiceQuotaPageTest.php diff --git a/app/Business/InvoiceQuotaPageData.php b/app/Business/InvoiceQuotaPageData.php new file mode 100644 index 00000000..ea1d8ecc --- /dev/null +++ b/app/Business/InvoiceQuotaPageData.php @@ -0,0 +1,211 @@ +select('period') + ->whereNotNull('period') + ->where('period', '<>', '') + ->distinct() + ->orderByDesc('period') + ->pluck('period') + ->map(static fn ($period): string => trim((string) $period)) + ->filter(fn (string $period): bool => $this->normalizePeriod($period) !== null) + ->unique() + ->values(); + + $requestedPeriod = $this->normalizePeriod( + trim((string) request()->query('period', '')) + ); + $period = $requestedPeriod ?? $periodOptions->first() ?? ''; + $sellerEntity = mb_substr( + trim((string) request()->query('seller_entity', '')), + 0, + 100 + ); + + $filtered = InvoiceBasic::query(); + + if ($period !== '') { + $filtered->where('period', $period); + } else { + $filtered->where(function ($query): void { + $query->whereNull('period')->orWhere('period', ''); + }); + } + + if ($sellerEntity !== '') { + $needle = '%' . addcslashes($sellerEntity, '%_\\') . '%'; + $filtered->where('store_entity', 'like', $needle); + } + + $aggregate = (clone $filtered) + ->reorder() + ->selectRaw( + 'COUNT(*) AS record_count, + COALESCE(SUM(invoice_limit), 0) AS quota_total, + COALESCE(SUM(output_amount), 0) AS used_total, + SUM(CASE WHEN output_amount > invoice_limit THEN 1 ELSE 0 END) AS exceeded_count, + SUM(CASE WHEN invoice_limit > 0 + AND output_amount > 0 + AND output_amount <= invoice_limit + AND output_amount >= invoice_limit * ? THEN 1 ELSE 0 END) AS warning_count', + [self::WARNING_RATE] + ) + ->first(); + + $rows = (clone $filtered) + ->select([ + 'id', + 'store_name', + 'store_entity', + 'invoice_issuer', + 'invoice_limit', + 'output_amount', + 'period', + ]) + ->orderByRaw( + 'CASE + WHEN output_amount > invoice_limit THEN 0 + WHEN invoice_limit > 0 AND output_amount >= invoice_limit * ? THEN 1 + WHEN invoice_limit > 0 THEN 2 + ELSE 3 + END', + [self::WARNING_RATE] + ) + ->orderBy('store_entity') + ->orderByDesc('id') + ->paginate(self::PER_PAGE) + ->withQueryString(); + + $rows->setCollection( + $rows->getCollection()->map( + fn (InvoiceBasic $row): object => $this->presentRow($row) + ) + ); + + $quotaTotal = round((float) ($aggregate->quota_total ?? 0), 2); + $usedTotal = round((float) ($aggregate->used_total ?? 0), 2); + $exceededCount = (int) ($aggregate->exceeded_count ?? 0); + $warningCount = (int) ($aggregate->warning_count ?? 0); + + return [ + 'view' => 'business.pages.invoice_quota', + 'title' => '发票额度', + 'period' => $period, + 'periodOptions' => $periodOptions, + 'sellerEntity' => $sellerEntity, + 'rows' => $rows, + 'summary' => [ + 'record_count' => (int) ($aggregate->record_count ?? 0), + 'quota_total' => $quotaTotal, + 'used_total' => $usedTotal, + 'remaining_total' => round($quotaTotal - $usedTotal, 2), + 'usage_rate' => $quotaTotal > 0 + ? round($usedTotal / $quotaTotal * 100, 1) + : null, + 'exceeded_count' => $exceededCount, + 'warning_count' => $warningCount, + 'attention_count' => $exceededCount + $warningCount, + ], + 'warningRate' => self::WARNING_RATE * 100, + 'loadError' => null, + ]; + } catch (Throwable $exception) { + report($exception); + + return $this->errorPayload(); + } + } + + private function presentRow(InvoiceBasic $row): object + { + $quota = round((float) $row->invoice_limit, 2); + $used = round((float) $row->output_amount, 2); + $remaining = round($quota - $used, 2); + $usageRate = $quota > 0 ? round($used / $quota * 100, 1) : null; + + if ($used > $quota) { + $status = '超额'; + $statusTone = 'danger'; + $statusIcon = 'error'; + } elseif ($quota <= 0) { + $status = '未设置额度'; + $statusTone = 'muted'; + $statusIcon = 'remove_circle_outline'; + } elseif ($usageRate !== null && $usageRate >= self::WARNING_RATE * 100) { + $status = '需关注'; + $statusTone = 'warning'; + $statusIcon = 'warning_amber'; + } else { + $status = '正常'; + $statusTone = 'success'; + $statusIcon = 'check_circle_outline'; + } + + return (object) [ + 'id' => $row->id, + 'store_name' => $row->store_name, + 'store_entity' => $row->store_entity, + 'invoice_issuer' => $row->invoice_issuer, + 'invoice_limit' => $quota, + 'output_amount' => $used, + 'remaining' => $remaining, + 'usage_rate' => $usageRate, + 'usage_bar_width' => $usageRate === null + ? 0 + : min(100, max(0, (int) round($usageRate))), + 'status' => $status, + 'status_tone' => $statusTone, + 'status_icon' => $statusIcon, + 'period' => $row->period, + ]; + } + + private function normalizePeriod(string $period): ?string + { + return preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $period) === 1 + ? $period + : null; + } + + private function errorPayload(): array + { + return [ + 'view' => 'business.pages.invoice_quota', + 'title' => '发票额度', + 'period' => '', + 'periodOptions' => collect(), + 'sellerEntity' => '', + 'rows' => new LengthAwarePaginator([], 0, self::PER_PAGE, 1, [ + 'path' => request()->url(), + 'query' => request()->query(), + ]), + 'summary' => [ + 'record_count' => 0, + 'quota_total' => 0.0, + 'used_total' => 0.0, + 'remaining_total' => 0.0, + 'usage_rate' => null, + 'exceeded_count' => 0, + 'warning_count' => 0, + 'attention_count' => 0, + ], + 'warningRate' => self::WARNING_RATE * 100, + 'loadError' => '发票额度数据暂时无法读取,请稍后重试。', + ]; + } +} diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 108a0fff..de3b4377 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -5231,6 +5231,7 @@ class Pages extends Controller 'preview-statistics' => $this->contentForPreviewStatistics(), 'planting-details' => $this->contentForPlantingDetails(), 'jst-order-labels' => $this->contentForJstOrderLabels(), + 'invoice-quota' => app(\App\Business\InvoiceQuotaPageData::class)->page(), 'invoice-reports' => app(\App\Business\InvoiceReportsPageData::class)->page($tab), 'planting-reimbursements' => app(\Modules\EcommerceFinance\View\PlantingReimbursementPageData::class)->page(), 'planting-reimbursement-workbench' => app(\Modules\EcommerceFinance\View\PlantingReimbursementWorkbenchPageData::class)->page(), diff --git a/config/business_navigation.php b/config/business_navigation.php index 5feed618..b9a91342 100644 --- a/config/business_navigation.php +++ b/config/business_navigation.php @@ -658,6 +658,7 @@ $navigation = [ 'pages' => [ ['slug' => 'automatic-invoicing', 'title' => '自动开票'], ['slug' => 'basic-information', 'title' => '发票基础信息'], + ['slug' => 'invoice-quota', 'title' => '发票额度'], [ 'slug' => 'invoice-reports', 'title' => '发票数据报表', diff --git a/resources/views/business/pages/invoice_quota.blade.php b/resources/views/business/pages/invoice_quota.blade.php new file mode 100644 index 00000000..b9906092 --- /dev/null +++ b/resources/views/business/pages/invoice_quota.blade.php @@ -0,0 +1,319 @@ +@php + $period = $period ?? ''; + $periodOptions = $periodOptions ?? collect(); + $sellerEntity = $sellerEntity ?? ''; + $rows = $rows ?? collect(); + $summary = $summary ?? []; + $warningRate = $warningRate ?? 80; + $loadError = $loadError ?? null; +@endphp + +
+ {{ $title ?? '发票额度' }} +
+ +
+ @include('business.pages.partials.upstream-data-styles') + + + @if ($loadError) + + @endif + +
+
+ + +
+
+ + +
+
+ + + + 重置 + +
+
+ + @unless ($loadError) +
+
+
当前开票额度
+
{{ number_format((float) ($summary['quota_total'] ?? 0), 2) }}
+ {{ $period !== '' ? $period . ' 期间' : '期间未设置' }} · {{ (int) ($summary['record_count'] ?? 0) }} 个主体 +
+
+
已开票金额
+
{{ number_format((float) ($summary['used_total'] ?? 0), 2) }}
+ 按当前筛选范围内的销项金额汇总 +
+
+
可用额度
+
(float) ($summary['remaining_total'] ?? 0) < 0])> + {{ number_format((float) ($summary['remaining_total'] ?? 0), 2) }} +
+ + @if (($summary['usage_rate'] ?? null) !== null) + 整体使用率 {{ number_format((float) $summary['usage_rate'], 1) }}% + @else + 当前范围未设置开票额度 + @endif + +
+
+
风险主体
+
{{ (int) ($summary['attention_count'] ?? 0) }}
+ 超额 {{ (int) ($summary['exceeded_count'] ?? 0) }} · 需关注 {{ (int) ($summary['warning_count'] ?? 0) }} +
+
+ @endunless + +
+
+ + + + + + + + + + + + + + + + @forelse ($rows as $row) + + + + + + + + + + + + @empty + + + + @endforelse + +
序号开票主体店铺范围开票员开票额度已开票金额可用额度使用率状态
{{ $rows->firstItem() + $loop->index }}{{ $row->store_entity }}{{ $row->store_name ?: '—' }}{{ $row->invoice_issuer ?: '—' }}{{ number_format((float) $row->invoice_limit, 2) }}{{ number_format((float) $row->output_amount, 2) }} $row->remaining < 0])> + {{ number_format((float) $row->remaining, 2) }} + + @if ($row->usage_rate !== null) +
+ + {{ number_format((float) $row->usage_rate, 1) }}% +
+ @else + — + @endif +
+ + + {{ $row->status }} + +
+ @if ($loadError) + 数据加载失败,请稍后重试。 + @else + 当前期间暂无可展示的发票额度数据,请先在发票基础信息中补充记录或切换期间。 + @endif +
+
+

+ 可用额度 = 开票额度 - 已开票金额;使用率达到 {{ number_format((float) $warningRate, 0) }}% 标记“需关注”,超过开票额度标记“超额”。开票额度为 0 且有已开票金额时同样按超额处理。 +

+ + @if ($rows->total() > 0) +
+ + 共 {{ $rows->total() }} 条 · 第 {{ $rows->currentPage() }} / {{ $rows->lastPage() }} 页 + + + @if ($rows->onFirstPage()) + + + + @else + + + + @endif + @if ($rows->hasMorePages()) + + + + @else + + + + @endif + +
+ @endif +
+
diff --git a/tests/Feature/Business/InvoiceQuotaPageTest.php b/tests/Feature/Business/InvoiceQuotaPageTest.php new file mode 100644 index 00000000..cfeedbf8 --- /dev/null +++ b/tests/Feature/Business/InvoiceQuotaPageTest.php @@ -0,0 +1,108 @@ +user->attachPermission( + Permission::where('name', 'read-admin-panel')->firstOrFail() + ); + + $this->createInvoiceBasic('甲公司', '甲店铺组', 10000, 8500, '2026-11'); + $this->createInvoiceBasic('乙公司', '乙店铺', 2000, 2500, '2026-11'); + $this->createInvoiceBasic('丙公司', '丙店铺', 5000, 1000, '2026-11'); + $this->createInvoiceBasic('零额度公司', '零额度店铺', 0, 0, '2026-11'); + $this->createInvoiceBasic('历史公司', '历史店铺', 100, 100, '2026-10'); + } + + public function test_invoice_quota_is_listed_after_invoice_basic_information(): void + { + $pages = collect(config('business_navigation')) + ->firstWhere('slug', 'invoice-management')['pages']; + + $this->assertSame( + ['automatic-invoicing', 'basic-information', 'invoice-quota', 'invoice-reports', 'store-backend-invoicing'], + array_column($pages, 'slug') + ); + } + + public function test_invoice_quota_defaults_to_latest_period_and_shows_usage_summary(): void + { + $this->loginAs() + ->get($this->pageUrl()) + ->assertOk() + ->assertSeeText('发票额度') + ->assertSeeText('当前开票额度') + ->assertSeeText('17,000.00') + ->assertSeeText('12,000.00') + ->assertSeeText('5,000.00') + ->assertSeeText('甲公司') + ->assertSeeText('8,500.00') + ->assertSeeText('1,500.00') + ->assertSeeText('85.0%') + ->assertSeeText('乙公司') + ->assertSeeText('-500.00') + ->assertSeeText('超额') + ->assertSeeText('需关注') + ->assertSeeText('未设置额度') + ->assertSeeText('超额 1 · 需关注 1') + ->assertDontSeeText('历史公司') + ->assertSee('