Compare commits

...

7 Commits

9 changed files with 1094 additions and 37 deletions

View File

@ -0,0 +1,198 @@
<?php
namespace App\Business;
use App\Models\Business\InputInvoiceReport;
use App\Models\Business\OutputInvoiceReport;
use Illuminate\Support\Collection;
use Throwable;
/**
* 发票数据报表:发票情况汇总在前,进项/销项明细在后。
*
* 汇总按公司名称合并进项报表与销项报表,金额均使用两张报表的合计口径;
* 差额 = 销项 − 进项,正数表示销项大于进项。
*/
class InvoiceReportsPageData
{
private const TABS = ['summary', 'input', 'output'];
public function page(?string $tab): array
{
$tab = in_array($tab, self::TABS, true) ? $tab : 'summary';
$inputRows = $this->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);
}
}

View File

@ -29,6 +29,8 @@ use App\Models\Business\CourierQuoteSignature;
use App\Models\Business\CourierQuoteVersion;
use App\Models\Business\CourierSigningSubject;
use App\Models\Business\InstallationDetail;
use App\Models\Business\InvoiceBasic;
use App\Models\Business\InvoiceBasicDetail;
use App\Models\Business\InventoryWarehouseNameMapping;
use App\Models\Business\Todo;
use App\Business\CourierSurchargeAuditLogRepository;
@ -3938,6 +3940,13 @@ class Pages extends Controller
): array {
$descriptor = null;
$rows = collect();
$isInvoiceBasicPage = ($section['slug'] ?? null) === 'invoice-management'
&& ($page['slug'] ?? null) === 'basic-information';
$invoiceBasicFilters = ['seller_entity' => '', 'store_name' => '', 'period' => ''];
$invoiceBasicPeriodOptions = collect();
$invoiceBasicPageTotals = ['invoice_limit' => 0.0, 'output_amount' => 0.0, 'input_amount' => 0.0];
$invoiceBasicAllTotals = ['invoice_limit' => 0.0, 'output_amount' => 0.0, 'input_amount' => 0.0];
$invoiceBasicDetails = collect();
$tabValue = null;
$tabColumn = null;
$upstreamError = null;
@ -4094,6 +4103,66 @@ class Pages extends Controller
} else {
$query = ($descriptor->model)::query();
if ($isInvoiceBasicPage) {
$invoiceBasicFilters = [
'seller_entity' => mb_substr(trim((string) request()->query('seller_entity', '')), 0, 100),
'store_name' => mb_substr(trim((string) request()->query('store_name', '')), 0, 100),
'period' => trim((string) request()->query('period', '')),
];
if ($invoiceBasicFilters['period'] !== ''
&& preg_match('/^\d{4}-\d{2}$/', $invoiceBasicFilters['period']) !== 1) {
$invoiceBasicFilters['period'] = '';
}
if ($invoiceBasicFilters['seller_entity'] !== '') {
$needle = '%' . addcslashes($invoiceBasicFilters['seller_entity'], '%_\\') . '%';
$query->where('store_entity', 'like', $needle);
}
if ($invoiceBasicFilters['store_name'] !== '') {
$needle = '%' . addcslashes($invoiceBasicFilters['store_name'], '%_\\') . '%';
$pairs = collect();
try {
$pairs = InvoiceBasicDetail::query()
->where('shop_name', 'like', $needle)
->select(['seller_entity', 'period'])
->distinct()
->limit(500)
->get();
} catch (Throwable $exception) {
report($exception);
}
$query->where(function ($builder) use ($needle, $pairs): void {
$builder->where('store_name', 'like', $needle);
foreach ($pairs as $pair) {
$builder->orWhere(function ($sub) use ($pair): void {
$sub->where('store_entity', $pair->seller_entity)
->where('period', $pair->period);
});
}
});
}
if ($invoiceBasicFilters['period'] !== '') {
$query->where('period', $invoiceBasicFilters['period']);
}
$invoiceBasicPeriodOptions = InvoiceBasic::query()
->select('period')
->where('period', '<>', '')
->distinct()
->orderByDesc('period')
->pluck('period');
$invoiceBasicAllTotals = [
'invoice_limit' => round((float) (clone $query)->sum('invoice_limit'), 2),
'output_amount' => round((float) (clone $query)->sum('output_amount'), 2),
'input_amount' => round((float) (clone $query)->sum('input_amount'), 2),
];
}
if ($descriptor->tabFilterColumn && $activeTab) {
$tabValue = TableRegistry::tabValue($section['slug'], $page['slug'], $activeTab['slug']);
@ -4109,6 +4178,35 @@ class Pages extends Controller
}
$rows = $query->orderByDesc('id')->paginate(50)->withQueryString();
if ($isInvoiceBasicPage) {
$invoiceBasicPageRows = $rows->getCollection();
$invoiceBasicPageTotals = [
'invoice_limit' => round((float) $invoiceBasicPageRows->sum('invoice_limit'), 2),
'output_amount' => round((float) $invoiceBasicPageRows->sum('output_amount'), 2),
'input_amount' => round((float) $invoiceBasicPageRows->sum('input_amount'), 2),
];
}
if (($section['slug'] ?? null) === 'invoice-management'
&& ($page['slug'] ?? null) === 'basic-information') {
try {
$pageRows = $rows->getCollection();
$periods = $pageRows->pluck('period')->filter()->unique()->values();
$sellers = $pageRows->pluck('store_entity')->filter()->unique()->values();
if ($periods->isNotEmpty() && $sellers->isNotEmpty()) {
$invoiceBasicDetails = InvoiceBasicDetail::query()
->whereIn('period', $periods->all())
->whereIn('seller_entity', $sellers->all())
->orderByDesc('output_amount')
->get()
->groupBy(static fn (InvoiceBasicDetail $detail): string => $detail->seller_entity . '|' . $detail->period);
}
} catch (Throwable $exception) {
report($exception);
}
}
}
}
}
@ -4140,6 +4238,11 @@ class Pages extends Controller
'inventoryUnmappedWarehouseError' => $inventoryUnmappedWarehouseError,
'inventoryWarehouseMappingView' => $inventoryWarehouseMappingView,
'receiptStats' => $receiptStats,
'invoiceBasicDetails' => $invoiceBasicDetails,
'invoiceBasicFilters' => $invoiceBasicFilters,
'invoiceBasicPeriodOptions' => $invoiceBasicPeriodOptions,
'invoiceBasicPageTotals' => $invoiceBasicPageTotals,
'invoiceBasicAllTotals' => $invoiceBasicAllTotals,
'payablesFinal' => $this->payablesFinalData($section, $page),
'kukaPostingBase' => $kukaPostingBase,
];
@ -4882,6 +4985,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(),

View File

@ -0,0 +1,27 @@
<?php
namespace App\Models\Business;
class InvoiceBasicDetail extends BusinessModel
{
protected $table = 'ak_biz_invoice_basic_details';
protected $fillable = [
'company_id',
'period',
'seller_entity',
'shop_name',
'invoice_count',
'output_amount',
'tax_amount',
'net_amount',
];
protected $casts = [
'company_id' => 'integer',
'invoice_count' => 'integer',
'output_amount' => 'decimal:2',
'tax_amount' => 'decimal:2',
'net_amount' => 'decimal:2',
];
}

View File

@ -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'],
],

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('ak_biz_invoice_basic_details', function (Blueprint $table): void {
$table->increments('id');
$table->unsignedInteger('company_id')->default(1);
$table->string('period', 10)->comment('期间,YYYY-MM');
$table->string('seller_entity')->comment('销方开票主体');
$table->string('shop_name')->comment('店铺名称');
$table->unsignedInteger('invoice_count')->default(0)->comment('成功开票张数');
$table->decimal('output_amount', 18, 2)->default(0)->comment('销项金额(含税)');
$table->decimal('tax_amount', 18, 2)->default(0)->comment('税额');
$table->decimal('net_amount', 18, 2)->default(0)->comment('不含税金额');
$table->timestamps();
$table->unique(
['company_id', 'period', 'seller_entity', 'shop_name'],
'invoice_basic_details_unique'
);
$table->index(
['company_id', 'period', 'seller_entity'],
'invoice_basic_details_lookup'
);
});
}
public function down(): void
{
Schema::dropIfExists('ak_biz_invoice_basic_details');
}
};

View File

@ -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
<div class="business-page-header">
<span class="business-page-title">{{ $pageTitle ?? '发票数据报表' }}</span>
<span class="business-page-title">{{ $pageTitle }}</span>
</div>
<div class="business-table-container">
<table class="business-table">
<thead>
<tr>
@foreach($displayHeaders as $h)
<th>{{ $h }}</th>
@endforeach
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
@foreach($displayFields as $f)
@if($f === 'id')
<td>{{ $loop->iteration }}</td>
@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']))
<td>{{ number_format($item->$f, 2) }}</td>
@else
<td>{{ $item->$f }}</td>
@endif
<div class="upstream-data-page">
@include('business.pages.partials.upstream-data-styles')
<style>
.invoice-summary-table { min-width: 64rem; }
.invoice-filter-bar { margin-top: 1rem; }
.invoice-summary-table tfoot td {
background: var(--vip-content-accent-soft, #f7f0de);
font-weight: 600;
}
.invoice-warning {
display: inline-flex;
min-width: 40px;
height: 22px;
align-items: center;
justify-content: center;
padding: 0 9px;
border: 1px solid var(--vip-border, #d8d2c7);
border-radius: 999px;
background: #f3f4f1;
color: var(--vip-content-muted, #595959);
font-size: 11.5px;
}
.invoice-warning.is-warning {
border-color: #e6cf9f;
background: #fdf3e3;
color: var(--vip-warning, #935f07);
font-weight: 600;
}
.invoice-summary-note {
margin: 0;
padding: 0.75rem 1rem;
border-top: 1px solid var(--vip-border, #d8d2c7);
color: var(--vip-content-muted, #595959);
font-size: 0.75rem;
}
</style>
@if ($isSummaryTab)
<form method="GET" action="{{ request()->url() }}" class="upstream-data-toolbar invoice-filter-bar" aria-label="发票情况汇总筛选">
<div class="upstream-data-field">
<label for="invoice-filter-company">公司</label>
<select id="invoice-filter-company" name="company">
<option value="">全部公司</option>
@foreach ($companyOptions as $companyName)
<option value="{{ $companyName }}" @selected($summaryFilters['company'] === $companyName)>{{ $companyName }}</option>
@endforeach
</tr>
@endforeach
@if($items->isEmpty())
<tr><td colspan="{{ count($displayHeaders) }}" style="text-align:center; color:#9ca3af; padding:2rem;">暂无数据</td></tr>
</select>
</div>
<div class="upstream-data-field">
<label for="invoice-filter-taxpayer">纳税人类型</label>
<select id="invoice-filter-taxpayer" name="taxpayer_type">
<option value="">全部类型</option>
@foreach ($taxpayerTypeOptions as $taxpayerType)
<option value="{{ $taxpayerType }}" @selected($summaryFilters['taxpayer_type'] === $taxpayerType)>{{ $taxpayerType }}</option>
@endforeach
</select>
</div>
<div class="upstream-data-field">
<label for="invoice-filter-warning">是否预警</label>
<select id="invoice-filter-warning" name="warning">
<option value="">全部</option>
<option value="yes" @selected($summaryFilters['warning'] === 'yes')>是</option>
<option value="no" @selected($summaryFilters['warning'] === 'no')>否</option>
</select>
</div>
<div class="upstream-data-actions">
<button type="submit" class="upstream-data-button upstream-data-button--primary">
<span class="material-icons-outlined" aria-hidden="true">search</span><span>查询</span>
</button>
<a href="{{ route('business.pages.show', ['section' => $section['slug'], 'page' => $page['slug'], 'tab' => 'summary']) }}" class="upstream-data-button">
<span class="material-icons-outlined" aria-hidden="true">refresh</span><span>重置</span>
</a>
</div>
</form>
@endif
<div class="upstream-data-table-panel">
<div class="upstream-data-table-scroll">
@if ($isSummaryTab)
<table class="upstream-data-table invoice-summary-table" aria-label="发票情况汇总">
<thead>
<tr>
<th>序号</th>
<th>公司</th>
<th>纳税人类型</th>
<th class="is-number">销项金额</th>
<th class="is-number">销项税额</th>
<th class="is-number">进项金额</th>
<th class="is-number">进项税额</th>
<th>是否预警</th>
</tr>
</thead>
<tbody>
@forelse ($summaryRows as $row)
@php
$ratioTitle = $row->input_output_ratio === null
? '销项金额为 0,无法计算比例'
: '进项/销项 = ' . number_format($row->input_output_ratio * 100, 1) . '%';
@endphp
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $row->company_name }}</td>
<td>{{ $row->taxpayer_type }}</td>
<td class="is-number">{{ number_format($row->output_total_amount, 2) }}</td>
<td class="is-number">{{ number_format($row->output_total_tax, 2) }}</td>
<td class="is-number">{{ number_format($row->input_total_amount, 2) }}</td>
<td class="is-number">{{ number_format($row->input_total_tax, 2) }}</td>
<td>
<span class="invoice-warning{{ $row->is_warning ? ' is-warning' : '' }}" title="{{ $ratioTitle }}">
{{ $row->is_warning ? '是' : '否' }}
</span>
</td>
</tr>
@empty
<tr class="is-empty"><td colspan="8">暂无数据</td></tr>
@endforelse
</tbody>
@if ($summaryRows->isNotEmpty() && $summaryTotals)
<tfoot>
<tr>
<td></td>
<td>合计</td>
<td>—</td>
<td class="is-number">{{ number_format($summaryTotals->output_total_amount, 2) }}</td>
<td class="is-number">{{ number_format($summaryTotals->output_total_tax, 2) }}</td>
<td class="is-number">{{ number_format($summaryTotals->input_total_amount, 2) }}</td>
<td class="is-number">{{ number_format($summaryTotals->input_total_tax, 2) }}</td>
<td>—</td>
</tr>
</tfoot>
@endif
</table>
@else
<table class="upstream-data-table" aria-label="{{ $pageTitle }}">
<thead>
<tr>
<th>序号</th>
@foreach ($displayHeaders as $header)
<th @class(['is-number' => $loop->index > 0])>{{ $header }}</th>
@endforeach
</tr>
</thead>
<tbody>
@forelse ($items as $item)
<tr>
<td>{{ $loop->iteration }}</td>
@foreach ($displayFields as $field)
@if ($field === 'company_name')
<td>{{ $item->$field }}</td>
@else
<td class="is-number">
{{ in_array($field, $numericFields, true) ? number_format((float) $item->$field, 2) : $item->$field }}
</td>
@endif
@endforeach
</tr>
@empty
<tr class="is-empty"><td colspan="{{ count($displayHeaders) + 1 }}">暂无数据</td></tr>
@endforelse
</tbody>
</table>
@endif
</tbody>
</table>
</div>
</div>
<div class="invoice-summary-note">预警口径:进项金额 ÷ 销项金额 &lt; 80% 时为「是」,否则为「否」;合计行不参与预警判断。</div>
</div>
</div>

View File

@ -9,6 +9,8 @@
&& ($page['slug'] ?? null) === 'marketing-account-balance';
$isStoreReceivingAccountsPage = ($section['slug'] ?? null) === 'fund-management'
&& ($page['slug'] ?? null) === 'store-receiving-accounts';
$isInvoiceBasicPage = ($section['slug'] ?? null) === 'invoice-management'
&& ($page['slug'] ?? null) === 'basic-information';
$isSupplierWorkspace = in_array($section['slug'] ?? null, ['supplier-payables', 'supplier-payables-final'], true);
$supplierWorkspaceSection = $section;
$isCourierFinalDirectory = ($section['slug'] ?? null) === 'courier-payables-final';
@ -1054,6 +1056,77 @@
@elseif ($pageView)
@include($pageView)
@elseif ($descriptor)
@if ($isInvoiceBasicPage)
<style>
.invoice-expand-toggle {
display: inline-flex;
align-items: center;
gap: 3px;
padding: 0;
border: 0;
background: transparent;
color: var(--vip-content-accent-dark, #856632);
font: inherit;
cursor: pointer;
}
.invoice-expand-toggle .material-icons-outlined { font-size: 1rem; }
.invoice-expand-toggle:hover { color: #6a5129; }
.invoice-detail-row > td {
padding: 0;
background: #fffdf8;
border-bottom: 1px solid var(--vip-border, #d8d2c7);
}
.invoice-detail-panel { padding: 0.75rem 1rem 1rem; }
.invoice-detail-table {
width: 100%;
border-collapse: collapse;
font-size: 0.78125rem;
font-variant-numeric: tabular-nums;
}
.invoice-detail-table th,
.invoice-detail-table td {
padding: 0.45rem 0.65rem;
border-bottom: 1px solid var(--vip-border, #d8d2c7);
text-align: left;
white-space: nowrap;
}
.invoice-detail-table th {
background: var(--vip-content-accent-soft, #f7f0de);
color: var(--vip-content-ink, #171717);
font-size: 0.75rem;
font-weight: 600;
}
.invoice-detail-table .num { text-align: right; }
.invoice-total-summary {
border-top: 1px solid var(--vip-border, #d8d2c7);
background: var(--vip-content-accent-soft, #f7f0de);
}
.invoice-total-summary__row {
display: flex;
min-height: 2.5rem;
align-items: center;
gap: 1.5rem;
flex-wrap: wrap;
padding: 0.5rem 1rem;
border-bottom: 1px solid var(--vip-border, #d8d2c7);
font-variant-numeric: tabular-nums;
}
.invoice-total-summary__row:last-child { border-bottom: 0; }
.invoice-total-summary__label {
min-width: 5.5rem;
color: var(--vip-content-ink, #171717);
font-weight: 600;
}
.invoice-total-summary__item {
color: var(--vip-content-muted, #595959);
font-size: 0.78125rem;
}
.invoice-total-summary__item strong {
color: var(--vip-content-ink, #171717);
font-weight: 600;
}
</style>
@endif
<div class="biz-card mt-6">
@if ($page['slug'] === 'installation-details')
@include('business.pages.partials.date-range-picker')
@ -1102,6 +1175,58 @@
@endunless
</div>
@if ($isInvoiceBasicPage)
<form
class="biz-filter-bar"
method="GET"
action="{{ route('business.pages.show', [
'section' => $section['slug'],
'page' => $page['slug'],
]) }}"
role="search"
aria-label="发票基础信息筛选"
>
<div class="biz-filter-field">
<label for="invoice-filter-seller">店铺主体</label>
<input
id="invoice-filter-seller"
class="biz-filter-input"
type="search"
name="seller_entity"
value="{{ $invoiceBasicFilters['seller_entity'] ?? '' }}"
placeholder="输入销方主体"
>
</div>
<div class="biz-filter-field">
<label for="invoice-filter-shop">店铺名称</label>
<input
id="invoice-filter-shop"
class="biz-filter-input"
type="search"
name="store_name"
value="{{ $invoiceBasicFilters['store_name'] ?? '' }}"
placeholder="输入店铺名称"
>
</div>
<div class="biz-filter-field">
<label for="invoice-filter-period">期间</label>
<select id="invoice-filter-period" class="biz-filter-select" name="period">
<option value="">全部期间</option>
@foreach (($invoiceBasicPeriodOptions ?? collect()) as $periodOption)
<option value="{{ $periodOption }}" @selected(($invoiceBasicFilters['period'] ?? '') === $periodOption)>{{ $periodOption }}</option>
@endforeach
</select>
</div>
<button type="submit" class="biz-btn biz-btn-primary">查询</button>
@if (collect($invoiceBasicFilters ?? [])->filter()->isNotEmpty())
<a class="biz-btn biz-btn-ghost" href="{{ route('business.pages.show', [
'section' => $section['slug'],
'page' => $page['slug'],
]) }}">重置</a>
@endif
</form>
@endif
@if ($isStoreReceivingAccountsPage)
<form class="biz-filter-bar" method="GET" action="{{ route('business.pages.show', [
'section' => $section['slug'], 'page' => $page['slug'], 'tab' => $activeTab['slug'] ?? null,
@ -1146,8 +1271,16 @@
</thead>
<tbody>
@forelse ($rows as $row)
@php
$invoiceDetailsForRow = $isInvoiceBasicPage
? ($invoiceBasicDetails ?? collect())->get(($row->store_entity ?? '') . '|' . ($row->period ?? ''), collect())
: collect();
$showInvoiceDetails = $isInvoiceBasicPage
&& \Illuminate\Support\Str::startsWith((string) ($row->store_name ?? ''), '多店铺(')
&& $invoiceDetailsForRow->isNotEmpty();
@endphp
<tr @if ($page['slug'] === 'installation-details') class="installation-main-row" data-installation-row data-guid="{{ $row->related_guid ?? '' }}" tabindex="0" aria-label="查看安装商品明细" @endif>
@if ($page['slug'] !== 'installation-details')<td>{{ $row->id }}</td>@endif
@if ($page['slug'] !== 'installation-details')<td>{{ $isInvoiceBasicPage ? (($rows->firstItem() ?? 1) + $loop->index) : $row->id }}</td>@endif
@foreach ($descriptor->fields as $field)
@php
$val = $row->{$field['name']};
@ -1163,6 +1296,11 @@
{{ \Illuminate\Support\Carbon::parse($val)->format('Y-m-d') }}
@elseif ($field['currency'])
{{ number_format((float) $val, 2) }}
@elseif ($showInvoiceDetails && $field['name'] === 'store_name')
<button type="button" class="invoice-expand-toggle" data-invoice-expand="{{ $row->id }}" aria-expanded="false">
<span class="material-icons-outlined" aria-hidden="true">add</span>
<span>{{ $val }}</span>
</button>
@else
{{ $val }}
@endif
@ -1188,6 +1326,36 @@
</td>
@endunless
</tr>
@if ($showInvoiceDetails)
<tr class="invoice-detail-row" data-invoice-detail="{{ $row->id }}" hidden>
<td colspan="{{ count($descriptor->fields) + ($descriptor->readOnly ? 1 : 2) }}">
<div class="invoice-detail-panel">
<table class="invoice-detail-table" aria-label="{{ $row->store_entity }} {{ $row->period }} 店铺明细">
<thead>
<tr>
<th>店铺名称</th>
<th class="num">开票张数</th>
<th class="num">销项金额(含税)</th>
<th class="num">税额</th>
<th class="num">不含税金额</th>
</tr>
</thead>
<tbody>
@foreach ($invoiceDetailsForRow->sortByDesc('output_amount') as $detail)
<tr>
<td>{{ $detail->shop_name }}</td>
<td class="num">{{ number_format((int) $detail->invoice_count) }}</td>
<td class="num">{{ number_format((float) $detail->output_amount, 2) }}</td>
<td class="num">{{ number_format((float) $detail->tax_amount, 2) }}</td>
<td class="num">{{ number_format((float) $detail->net_amount, 2) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</td>
</tr>
@endif
@empty
<tr>
<td colspan="{{ count($descriptor->fields) + ($page['slug'] === 'installation-details' ? 0 : 1) + ($descriptor->readOnly ? 0 : 1) }}">
@ -1202,6 +1370,22 @@
</table>
</div>
@if ($isInvoiceBasicPage)
<div class="invoice-total-summary" aria-label="发票基础信息合计">
@foreach ([
'本页合计' => ($invoiceBasicPageTotals ?? []),
'全部合计' => ($invoiceBasicAllTotals ?? []),
] as $totalLabel => $totals)
<div class="invoice-total-summary__row">
<span class="invoice-total-summary__label">{{ $totalLabel }}</span>
<span class="invoice-total-summary__item">开票额度 <strong>{{ number_format((float) ($totals['invoice_limit'] ?? 0), 2) }}</strong></span>
<span class="invoice-total-summary__item">销项金额 <strong>{{ number_format((float) ($totals['output_amount'] ?? 0), 2) }}</strong></span>
<span class="invoice-total-summary__item">进项金额 <strong>{{ number_format((float) ($totals['input_amount'] ?? 0), 2) }}</strong></span>
</div>
@endforeach
</div>
@endif
<div class="biz-pagination">
<span>共 {{ $rows->total() }} 条 · 第 {{ $rows->currentPage() }} / {{ $rows->lastPage() }} 页</span>
<span>
@ -1277,6 +1461,24 @@
</script>
@endif
@if ($isInvoiceBasicPage)
<script>
(() => {
document.querySelectorAll('[data-invoice-expand]').forEach((button) => {
button.addEventListener('click', () => {
const detailRow = document.querySelector('[data-invoice-detail="' + button.dataset.invoiceExpand + '"]');
if (!detailRow) return;
const willOpen = detailRow.hidden;
detailRow.hidden = !willOpen;
button.setAttribute('aria-expanded', willOpen ? 'true' : 'false');
const icon = button.querySelector('.material-icons-outlined');
if (icon) icon.textContent = willOpen ? 'remove' : 'add';
});
});
})();
</script>
@endif
</div>
@unless ($descriptor->readOnly)

View File

@ -0,0 +1,129 @@
<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use App\Models\Business\InvoiceBasic;
use App\Models\Business\InvoiceBasicDetail;
use Tests\Feature\FeatureTestCase;
class InvoiceBasicFiltersTotalsTest extends FeatureTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->user->attachPermission(
Permission::where('name', 'read-admin-panel')->firstOrFail()
);
InvoiceBasic::create([
'company_id' => $this->company->id,
'store_name' => '多店铺(2)',
'store_entity' => '甲公司',
'invoice_issuer' => '甲开票员',
'invoice_limit' => 50,
'output_amount' => 300,
'input_amount' => 100,
'period' => '2026-10',
]);
InvoiceBasic::create([
'company_id' => $this->company->id,
'store_name' => '乙店铺',
'store_entity' => '乙公司',
'invoice_issuer' => '乙开票员',
'invoice_limit' => 20,
'output_amount' => 200,
'input_amount' => 50,
'period' => '2026-11',
]);
InvoiceBasicDetail::create([
'company_id' => $this->company->id,
'period' => '2026-10',
'seller_entity' => '甲公司',
'shop_name' => '一号店',
'invoice_count' => 1,
'output_amount' => 100,
'tax_amount' => 10,
'net_amount' => 90,
]);
InvoiceBasicDetail::create([
'company_id' => $this->company->id,
'period' => '2026-10',
'seller_entity' => '甲公司',
'shop_name' => '二号店',
'invoice_count' => 2,
'output_amount' => 200,
'tax_amount' => 20,
'net_amount' => 180,
]);
InvoiceBasicDetail::create([
'company_id' => $this->company->id,
'period' => '2026-11',
'seller_entity' => '乙公司',
'shop_name' => '三号店',
'invoice_count' => 1,
'output_amount' => 200,
'tax_amount' => 20,
'net_amount' => 180,
]);
}
public function test_invoice_basic_shows_filters_and_page_and_all_totals(): void
{
$this->loginAs()
->get($this->pageUrl())
->assertOk()
->assertSeeText('店铺主体')
->assertSeeText('店铺名称')
->assertSeeText('期间')
->assertSeeText('本页合计')
->assertSeeText('全部合计')
->assertSeeText('300.00')
->assertSeeText('200.00')
->assertSeeText('500.00')
->assertSeeText('150.00')
->assertSeeText('70.00')
->assertSee('<option value="2026-10"', false)
->assertSee('<option value="2026-11"', false);
}
public function test_invoice_basic_filters_by_seller_and_period(): void
{
$this->loginAs()
->get($this->pageUrl(['seller_entity' => '甲公司']))
->assertOk()
->assertSeeText('多店铺(2)')
->assertDontSeeText('乙公司')
->assertDontSeeText('乙店铺');
$this->loginAs()
->get($this->pageUrl(['period' => '2026-11']))
->assertOk()
->assertSeeText('乙店铺')
->assertSeeText('乙公司')
->assertDontSeeText('多店铺(2)');
}
public function test_invoice_basic_filters_by_shop_name_inside_details(): void
{
$this->loginAs()
->get($this->pageUrl(['store_name' => '二号店']))
->assertOk()
->assertSeeText('多店铺(2)')
->assertSeeText('甲公司')
->assertDontSeeText('乙公司')
->assertDontSeeText('三号店');
}
private function pageUrl(array $query = []): string
{
$url = route('business.pages.show', [
'section' => 'invoice-management',
'page' => 'basic-information',
]);
return $query === [] ? $url : $url . '?' . http_build_query($query);
}
}

View File

@ -0,0 +1,202 @@
<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use App\Models\Business\InputInvoiceReport;
use App\Models\Business\OutputInvoiceReport;
use Mockery;
use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository;
use Tests\Feature\FeatureTestCase;
class InvoiceReportsSummaryTest extends FeatureTestCase
{
private BusinessInformationMirrorRepository $companyInformation;
protected function setUp(): void
{
parent::setUp();
$this->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('<option value="甲公司"', false)
->assertSeeText('150.00')
->assertDontSeeText('100.00');
$this->loginAs()
->get($url . '?taxpayer_type=' . urlencode('小规模纳税人'))
->assertOk()
->assertSeeText('100.00')
->assertDontSeeText('150.00');
$this->loginAs()
->get($url . '?warning=yes')
->assertOk()
->assertSeeText('150.00')
->assertDontSeeText('100.00');
$this->loginAs()
->get($url . '?warning=no')
->assertOk()
->assertSeeText('100.00')
->assertDontSeeText('150.00');
}
public function test_input_and_output_tabs_keep_their_detail_columns(): void
{
$this->createReports();
foreach (['input' => '进项报表', 'output' => '销项报表'] as $tab => $title) {
$this->loginAs()
->get(route('business.pages.show', [
'section' => 'invoice-management',
'page' => 'invoice-reports',
'tab' => $tab,
]))
->assertOk()
->assertSeeText($title)
->assertSeeText('公司名称')
->assertSeeText('商品金额')
->assertSeeText('服务费税额')
->assertSeeText('税额合计');
}
}
private function createReports(): void
{
InputInvoiceReport::create([
'company_id' => $this->company->id,
'company_name' => '甲公司',
'input_goods_amount' => 100,
'input_goods_tax' => 13,
'input_service_amount' => 50,
'input_service_tax' => 3,
'input_total_amount' => 150,
'input_total_tax' => 16,
]);
InputInvoiceReport::create([
'company_id' => $this->company->id,
'company_name' => '乙公司',
'input_goods_amount' => 80,
'input_goods_tax' => 8,
'input_service_amount' => 20,
'input_service_tax' => 2,
'input_total_amount' => 100,
'input_total_tax' => 10,
]);
OutputInvoiceReport::create([
'company_id' => $this->company->id,
'company_name' => '甲公司',
'output_goods_amount' => 200,
'output_goods_tax' => 26,
'output_service_amount' => 120,
'output_service_tax' => 15.6,
'output_total_amount' => 320,
'output_total_tax' => 41.6,
]);
}
}