From ff2f6c9b905a0d3cf78f99a3942ac94fa2862e09 Mon Sep 17 00:00:00 2001 From: WuYongJie Date: Tue, 10 Nov 2026 14:26:07 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E5=8F=91=E7=A5=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=8A=A5=E8=A1=A8=E6=96=B0=E5=A2=9E=E5=8F=91=E7=A5=A8?= =?UTF-8?q?=E6=83=85=E5=86=B5=E6=B1=87=E6=80=BB=E9=A1=B5=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Business/InvoiceReportsPageData.php | 198 +++++++++++++++ app/Http/Controllers/Business/Pages.php | 1 + config/business_navigation.php | 1 + .../business/pages/invoice_reports.blade.php | 228 +++++++++++++++--- .../Business/InvoiceReportsSummaryTest.php | 202 ++++++++++++++++ 5 files changed, 594 insertions(+), 36 deletions(-) create mode 100644 app/Business/InvoiceReportsPageData.php create mode 100644 tests/Feature/Business/InvoiceReportsSummaryTest.php diff --git a/app/Business/InvoiceReportsPageData.php b/app/Business/InvoiceReportsPageData.php new file mode 100644 index 00000000..dc7a04dc --- /dev/null +++ b/app/Business/InvoiceReportsPageData.php @@ -0,0 +1,198 @@ +reportRows(InputInvoiceReport::class); + $outputRows = $this->reportRows(OutputInvoiceReport::class); + + if ($tab === 'summary') { + $allRows = $this->summaryRows($inputRows, $outputRows); + $filters = $this->summaryFilters(); + $summaryRows = $this->filterSummaryRows($allRows, $filters); + + return [ + 'view' => 'business.pages.invoice_reports', + 'summaryRows' => $summaryRows, + 'summaryTotals' => $this->summaryTotals($summaryRows), + 'summaryFilters' => $filters, + 'companyOptions' => $allRows->pluck('company_name') + ->filter(static fn ($name): bool => (string) $name !== '') + ->unique() + ->sort() + ->values(), + 'taxpayerTypeOptions' => $allRows->pluck('taxpayer_type') + ->filter(static fn ($type): bool => (string) $type !== '' && (string) $type !== '—') + ->unique() + ->sort() + ->values(), + ]; + } + + return [ + 'view' => 'business.pages.invoice_reports', + 'items' => $tab === 'output' ? $outputRows : $inputRows, + ]; + } + + /** + * 报表表在部分老环境可能尚未迁移,缺失时保持页面可用并展示空表。 + * + * @param class-string $model + */ + private function reportRows(string $model): Collection + { + try { + return $model::query() + ->orderBy('company_name') + ->orderBy('id') + ->get(); + } catch (Throwable $exception) { + report($exception); + + return collect(); + } + } + + private function summaryRows(Collection $inputRows, Collection $outputRows): Collection + { + $inputs = $inputRows->groupBy(fn ($row): string => trim((string) $row->company_name)); + $outputs = $outputRows->groupBy(fn ($row): string => trim((string) $row->company_name)); + + $companyNames = $inputs->keys() + ->merge($outputs->keys()) + ->filter(static fn ($name): bool => $name !== '') + ->unique() + ->sort() + ->values(); + + $taxpayerTypes = $this->taxpayerTypes($companyNames); + + return $companyNames->map(function (string $companyName) use ($inputs, $outputs, $taxpayerTypes): object { + $inputRows = $inputs->get($companyName, collect()); + $outputRows = $outputs->get($companyName, collect()); + + $inputAmount = $this->sumField($inputRows, 'input_total_amount'); + $inputTax = $this->sumField($inputRows, 'input_total_tax'); + $outputAmount = $this->sumField($outputRows, 'output_total_amount'); + $outputTax = $this->sumField($outputRows, 'output_total_tax'); + $ratio = $outputAmount > 0 ? $inputAmount / $outputAmount : null; + + return (object) [ + 'company_name' => $companyName, + 'taxpayer_type' => $taxpayerTypes->get($companyName, '—'), + 'input_total_amount' => $inputAmount, + 'input_total_tax' => $inputTax, + 'output_total_amount' => $outputAmount, + 'output_total_tax' => $outputTax, + 'input_output_ratio' => $ratio, + 'is_warning' => $ratio !== null && $ratio < 0.8, + ]; + }); + } + + private function summaryTotals(Collection $rows): object + { + return (object) [ + 'input_total_amount' => round((float) $rows->sum('input_total_amount'), 2), + 'input_total_tax' => round((float) $rows->sum('input_total_tax'), 2), + 'output_total_amount' => round((float) $rows->sum('output_total_amount'), 2), + 'output_total_tax' => round((float) $rows->sum('output_total_tax'), 2), + ]; + } + + /** + * @return array{company:string,taxpayer_type:string,warning:string} + */ + private function summaryFilters(): array + { + $warning = strtolower(trim((string) request()->query('warning', ''))); + + return [ + 'company' => trim((string) request()->query('company', '')), + 'taxpayer_type' => trim((string) request()->query('taxpayer_type', '')), + 'warning' => in_array($warning, ['yes', 'no'], true) ? $warning : '', + ]; + } + + /** + * @param array{company:string,taxpayer_type:string,warning:string} $filters + */ + private function filterSummaryRows(Collection $rows, array $filters): Collection + { + return $rows->filter(function (object $row) use ($filters): bool { + if ($filters['company'] !== '' && $row->company_name !== $filters['company']) { + return false; + } + + if ($filters['taxpayer_type'] !== '' && $row->taxpayer_type !== $filters['taxpayer_type']) { + return false; + } + + if ($filters['warning'] === 'yes' && ! $row->is_warning) { + return false; + } + + if ($filters['warning'] === 'no' && $row->is_warning) { + return false; + } + + return true; + })->values(); + } + + /** + * 纳税人类型取公司基础信息共享镜像;镜像未就绪或公司未登记时显示「—」。 + */ + private function taxpayerTypes(Collection $companyNames): Collection + { + $names = $companyNames + ->map(static fn ($name): string => trim((string) $name)) + ->filter(static fn (string $name): bool => $name !== '') + ->unique() + ->values() + ->all(); + + if ($names === []) { + return collect(); + } + + try { + $rows = app(\Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository::class) + ->allCompaniesForClassification(['_company_names' => $names]); + + return collect($rows) + ->mapWithKeys(static fn (array $row): array => [ + trim((string) ($row['company_name'] ?? '')) => trim((string) ($row['taxpayer_category'] ?? '')), + ]) + ->filter(static fn ($value, $key): bool => $key !== '' && (string) $value !== ''); + } catch (Throwable $exception) { + report($exception); + + return collect(); + } + } + + private function sumField(Collection $rows, string $field): float + { + return round($rows->sum(static fn ($row): float => (float) $row->{$field}), 2); + } +} diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 33b7e360..cf7d4413 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -4871,6 +4871,7 @@ class Pages extends Controller 'preview-statistics' => $this->contentForPreviewStatistics(), 'planting-details' => $this->contentForPlantingDetails(), 'jst-order-labels' => $this->contentForJstOrderLabels(), + 'invoice-reports' => app(\App\Business\InvoiceReportsPageData::class)->page($tab), 'planting-reimbursements' => app(\Modules\EcommerceFinance\View\PlantingReimbursementPageData::class)->page(), 'planting-loss' => app(\Modules\EcommerceFinance\View\PlantingLossPageData::class)->page(), 'finance-data' => app(\Modules\EcommerceFinance\View\SupplierReconciliationPageData::class)->financeData(), diff --git a/config/business_navigation.php b/config/business_navigation.php index bd874e2c..3475c391 100644 --- a/config/business_navigation.php +++ b/config/business_navigation.php @@ -659,6 +659,7 @@ $navigation = [ 'slug' => 'invoice-reports', 'title' => '发票数据报表', 'tabs' => [ + ['slug' => 'summary', 'title' => '发票情况汇总', 'icon' => 'summarize'], ['slug' => 'input', 'title' => '进项报表', 'icon' => 'download'], ['slug' => 'output', 'title' => '销项报表', 'icon' => 'upload'], ], diff --git a/resources/views/business/pages/invoice_reports.blade.php b/resources/views/business/pages/invoice_reports.blade.php index f742b881..befd5edb 100644 --- a/resources/views/business/pages/invoice_reports.blade.php +++ b/resources/views/business/pages/invoice_reports.blade.php @@ -1,44 +1,200 @@ @php - // Determine which fields to show based on active tab - $isInputTab = isset($activeTab) && $activeTab['slug'] === 'input'; - $pageTitle = $isInputTab ? '进项报表' : '销项报表'; - if ($isInputTab) { - $displayFields = ['id', 'company_name', 'input_goods_amount', 'input_goods_tax', 'input_service_amount', 'input_service_tax', 'input_total_amount', 'input_total_tax']; - $displayHeaders = ['序号', '公司名称', '商品金额', '商品税额', '服务费金额', '服务费税额', '金额合计', '税额合计']; + $tabSlug = $activeTab['slug'] ?? 'summary'; + $isSummaryTab = $tabSlug === 'summary'; + $pageTitle = $activeTab['title'] ?? '发票数据报表'; + + $numericFields = [ + 'input_goods_amount', 'input_goods_tax', 'input_service_amount', 'input_service_tax', + 'input_total_amount', 'input_total_tax', + 'output_goods_amount', 'output_goods_tax', 'output_service_amount', 'output_service_tax', + 'output_total_amount', 'output_total_tax', + ]; + + if ($tabSlug === 'input') { + $displayFields = ['company_name', 'input_goods_amount', 'input_goods_tax', 'input_service_amount', 'input_service_tax', 'input_total_amount', 'input_total_tax']; + $displayHeaders = ['公司名称', '商品金额', '商品税额', '服务费金额', '服务费税额', '金额合计', '税额合计']; + } elseif ($tabSlug === 'output') { + $displayFields = ['company_name', 'output_goods_amount', 'output_goods_tax', 'output_service_amount', 'output_service_tax', 'output_total_amount', 'output_total_tax']; + $displayHeaders = ['公司名称', '商品金额', '商品税额', '服务费金额', '服务费税额', '金额合计', '税额合计']; } else { - $displayFields = ['id', 'company_name', 'output_goods_amount', 'output_goods_tax', 'output_service_amount', 'output_service_tax', 'output_total_amount', 'output_total_tax']; - $displayHeaders = ['序号', '公司名称', '商品金额', '商品税额', '服务费金额', '服务费税额', '金额合计', '税额合计']; + $displayFields = []; + $displayHeaders = []; } + + $summaryRows = $summaryRows ?? collect(); + $summaryTotals = $summaryTotals ?? null; + $summaryFilters = $summaryFilters ?? ['company' => '', 'taxpayer_type' => '', 'warning' => '']; + $companyOptions = $companyOptions ?? collect(); + $taxpayerTypeOptions = $taxpayerTypeOptions ?? collect(); @endphp +
- {{ $pageTitle ?? '发票数据报表' }} + {{ $pageTitle }}
-
- - - - @foreach($displayHeaders as $h) - - @endforeach - - - - @foreach($items as $item) - - @foreach($displayFields as $f) - @if($f === 'id') - - @elseif(in_array($f, ['input_goods_amount', 'input_goods_tax', 'input_service_amount', 'input_service_tax', 'input_total_amount', 'input_total_tax', 'output_goods_amount', 'output_goods_tax', 'output_service_amount', 'output_service_tax', 'output_total_amount', 'output_total_tax'])) - - @else - - @endif + +
+ @include('business.pages.partials.upstream-data-styles') + + + @if ($isSummaryTab) +
+
+ +
- @endforeach - @if($items->isEmpty()) - + + +
+ + +
+
+ + +
+
+ + + 重置 + +
+ + @endif + +
+
+ @if ($isSummaryTab) +
{{ $h }}
{{ $loop->iteration }}{{ number_format($item->$f, 2) }}{{ $item->$f }}
暂无数据
+ + + + + + + + + + + + + + @forelse ($summaryRows as $row) + @php + $ratioTitle = $row->input_output_ratio === null + ? '销项金额为 0,无法计算比例' + : '进项/销项 = ' . number_format($row->input_output_ratio * 100, 1) . '%'; + @endphp + + + + + + + + + + + @empty + + @endforelse + + @if ($summaryRows->isNotEmpty() && $summaryTotals) + + + + + + + + + + + + + @endif +
序号公司纳税人类型销项金额销项税额进项金额进项税额是否预警
{{ $loop->iteration }}{{ $row->company_name }}{{ $row->taxpayer_type }}{{ number_format($row->output_total_amount, 2) }}{{ number_format($row->output_total_tax, 2) }}{{ number_format($row->input_total_amount, 2) }}{{ number_format($row->input_total_tax, 2) }} + + {{ $row->is_warning ? '是' : '否' }} + +
暂无数据
合计—{{ number_format($summaryTotals->output_total_amount, 2) }}{{ number_format($summaryTotals->output_total_tax, 2) }}{{ number_format($summaryTotals->input_total_amount, 2) }}{{ number_format($summaryTotals->input_total_tax, 2) }}—
+ @else + + + + + @foreach ($displayHeaders as $header) + + @endforeach + + + + @forelse ($items as $item) + + + @foreach ($displayFields as $field) + @if ($field === 'company_name') + + @else + + @endif + @endforeach + + @empty + + @endforelse + +
序号 $loop->index > 0])>{{ $header }}
{{ $loop->iteration }}{{ $item->$field }} + {{ in_array($field, $numericFields, true) ? number_format((float) $item->$field, 2) : $item->$field }} +
暂无数据
@endif - - -
\ No newline at end of file + +
预警口径:进项金额 ÷ 销项金额 < 80% 时为「是」,否则为「否」;合计行不参与预警判断。
+ + diff --git a/tests/Feature/Business/InvoiceReportsSummaryTest.php b/tests/Feature/Business/InvoiceReportsSummaryTest.php new file mode 100644 index 00000000..ecfee548 --- /dev/null +++ b/tests/Feature/Business/InvoiceReportsSummaryTest.php @@ -0,0 +1,202 @@ +user->attachPermission( + Permission::where('name', 'read-admin-panel')->firstOrFail() + ); + + $this->companyInformation = Mockery::mock(BusinessInformationMirrorRepository::class); + $this->companyInformation->shouldReceive('allCompaniesForClassification') + ->byDefault() + ->andReturn([]); + $this->app->instance(BusinessInformationMirrorRepository::class, $this->companyInformation); + } + + public function test_invoice_reports_puts_summary_tab_before_input_and_output(): void + { + $page = collect(collect(config('business_navigation')) + ->firstWhere('slug', 'invoice-management')['pages']) + ->firstWhere('slug', 'invoice-reports'); + + $this->assertSame( + ['summary', 'input', 'output'], + array_column($page['tabs'], 'slug') + ); + + $this->loginAs() + ->get(route('business.pages.show', [ + 'section' => 'invoice-management', + 'page' => 'invoice-reports', + ])) + ->assertRedirect(route('business.pages.show', [ + 'section' => 'invoice-management', + 'page' => 'invoice-reports', + 'tab' => 'summary', + ])); + } + + public function test_summary_combines_input_and_output_reports_by_company(): void + { + $this->createReports(); + + $companyInformation = Mockery::mock(BusinessInformationMirrorRepository::class); + $companyInformation->shouldReceive('allCompaniesForClassification') + ->once() + ->andReturn([ + ['company_name' => '甲公司', 'taxpayer_category' => '一般纳税人'], + ['company_name' => '乙公司', 'taxpayer_category' => '小规模纳税人'], + ]); + $this->app->instance(BusinessInformationMirrorRepository::class, $companyInformation); + + $this->loginAs() + ->get(route('business.pages.show', [ + 'section' => 'invoice-management', + 'page' => 'invoice-reports', + 'tab' => 'summary', + ])) + ->assertOk() + ->assertSeeText('发票情况汇总') + ->assertSeeText('公司') + ->assertSeeText('纳税人类型') + ->assertSeeText('销项金额') + ->assertSeeText('销项税额') + ->assertSeeText('进项金额') + ->assertSeeText('进项税额') + ->assertSeeText('是否预警') + ->assertSeeText('全部公司') + ->assertSeeText('全部类型') + ->assertSeeText('甲公司') + ->assertSeeText('乙公司') + ->assertSeeText('一般纳税人') + ->assertSeeText('小规模纳税人') + ->assertSeeText('150.00') + ->assertSeeText('16.00') + ->assertSeeText('100.00') + ->assertSeeText('10.00') + ->assertSeeText('320.00') + ->assertSeeText('41.60') + ->assertSeeText('合计') + ->assertSeeText('250.00') + ->assertSeeText('26.00') + ->assertSee('invoice-warning is-warning', false) + ->assertSee('进项/销项 = 46.9%', false) + ->assertSee('销项金额为 0,无法计算比例', false); + } + + public function test_summary_filters_by_company_taxpayer_type_and_warning(): void + { + $this->createReports(); + + $companyInformation = Mockery::mock(BusinessInformationMirrorRepository::class); + $companyInformation->shouldReceive('allCompaniesForClassification') + ->andReturn([ + ['company_name' => '甲公司', 'taxpayer_category' => '一般纳税人'], + ['company_name' => '乙公司', 'taxpayer_category' => '小规模纳税人'], + ]); + $this->app->instance(BusinessInformationMirrorRepository::class, $companyInformation); + + $this->loginAs(); + $url = route('business.pages.show', [ + 'section' => 'invoice-management', + 'page' => 'invoice-reports', + 'tab' => 'summary', + ]); + + $this->loginAs() + ->get($url . '?company=' . urlencode('甲公司')) + ->assertOk() + ->assertSee('