fix(发票数据): 增加有票无票筛选并自动接入新月份 #1735

Open
yueyang wants to merge 1 commits from fix/zhangyuanyuan-invoice-flow-status-months into main
5 changed files with 227 additions and 29 deletions

View File

@ -2,6 +2,7 @@
return [
'dataset_key' => 'operations_replica.rpt.ding_invoices',
'first_supported_month' => '2026-10',
'document_types' => [
'公摊日常报销',
'店铺日常报销',

View File

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

View File

@ -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();

View File

@ -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
<div class="invoice-data-page" data-invoice-data-page>
@ -187,6 +203,15 @@
font-weight: 600;
}
.invoice-data-status a {
color: var(--invoice-accent-dark);
text-decoration: none;
}
.invoice-data-status a:hover {
text-decoration: underline;
}
.invoice-data-kpis {
display: grid;
margin-top: 0.75rem;
@ -203,6 +228,24 @@
border-right: 1px solid var(--invoice-border);
}
.invoice-data-kpi.is-link {
display: block;
color: inherit;
text-decoration: none;
transition: background 0.15s ease;
}
.invoice-data-kpi.is-link:hover {
background: var(--invoice-soft);
}
.invoice-data-kpi.is-link:focus-visible {
position: relative;
z-index: 2;
outline: 2px solid var(--invoice-accent-dark);
outline-offset: -2px;
}
.invoice-data-kpi:last-child {
border-right: 0;
}
@ -231,6 +274,15 @@
color: var(--invoice-accent-dark);
}
.invoice-data-kpi-action {
display: block;
margin-top: 0.25rem;
color: var(--invoice-accent-dark);
font-size: 0.625rem;
font-style: normal;
line-height: 1.4;
}
.invoice-data-tabs {
display: flex;
align-items: center;
@ -291,7 +343,7 @@
}
.invoice-data-table.is-flow {
min-width: 104rem;
min-width: 110rem;
}
.invoice-data-table.is-attachments {
@ -786,18 +838,21 @@
@endif
· OCR {{ $invoiceOcrConfigured ? '已配置' : '未配置' }}
· {{ $invoiceScope }}
· <a href="{{ $needsReviewFlowUrl }}">待复核 {{ number_format((int) ($summary['needs_review_flows'] ?? 0)) }}</a>
</span>
</div>
<section class="invoice-data-kpis" aria-label="发票数据汇总">
<div class="invoice-data-kpi">
<a class="invoice-data-kpi is-link" href="{{ $flowTabUrl }}" title="查看全部流程">
<span>付款流程</span>
<strong>{{ number_format((int) ($summary['flows'] ?? 0)) }}</strong>
</div>
<div class="invoice-data-kpi">
<span>含票流程</span>
<em class="invoice-data-kpi-action">查看全部流程</em>
</a>
<a class="invoice-data-kpi is-link" href="{{ $hasInvoiceFlowUrl }}" title="查看有票流程">
<span>有票流程</span>
<strong>{{ number_format((int) ($summary['invoice_flows'] ?? 0)) }}</strong>
</div>
<em class="invoice-data-kpi-action">查看有票流程</em>
</a>
<div class="invoice-data-kpi">
<span>发票张数</span>
<strong>{{ number_format((int) ($summary['invoice_count'] ?? 0)) }}</strong>
@ -815,10 +870,11 @@
title="¥{{ number_format((float) ($summary['payment_amount'] ?? 0), 2) }}"
>¥{{ $wan($summary['payment_amount'] ?? 0) }}</strong>
</div>
<div class="invoice-data-kpi">
<a class="invoice-data-kpi is-link" href="{{ $noInvoiceFlowUrl }}" title="查看无票流程">
<span>无票流程</span>
<strong>{{ number_format((int) ($summary['no_invoice_flows'] ?? 0)) }}</strong>
</div>
<em class="invoice-data-kpi-action">查看无票流程</em>
</a>
<div class="invoice-data-kpi">
<span>开票金额覆盖率</span>
<strong>{{ number_format((float) ($summary['invoice_rate'] ?? 0) * 100, 1) }}%</strong>
@ -848,7 +904,7 @@
data-invoice-tab="flows"
>
<span class="material-icons-outlined" aria-hidden="true">table_rows</span>
<span>按流程汇总</span>
<span>按流程汇总 ({{ number_format($flowRows->total()) }})</span>
</button>
<button
type="button"
@ -971,9 +1027,9 @@
class="invoice-data-secondary"
title="{{ $hasSourceAttachment
? '原始附件已识别,但该文件格式暂不支持页内预览'
: '该发票来自工作台账导入,未保存原 OA 附件关联信息' }}"
: '该发票来自历史 Excel 台账导入,没有保存审批附件文件,因此不能预览;可在“从流程抓取发票”后补回原件关联' }}"
>
{{ $hasSourceAttachment ? '原件暂不支持预览' : '原票未关联' }}
{{ $hasSourceAttachment ? '原件暂不支持预览' : '历史导入·无原件' }}
</span>
@endif
</td>
@ -1040,7 +1096,8 @@
<th scope="col" class="is-number">发票张数</th>
<th scope="col" class="is-number">发票合计</th>
<th scope="col" class="is-number">付款差额</th>
<th scope="col">状态</th>
<th scope="col">发票结论</th>
<th scope="col">识别状态</th>
<th scope="col">打款时间</th>
</tr>
</thead>
@ -1048,6 +1105,15 @@
@forelse ($flowRows as $row)
@php
$difference = round((float) $row->payment_amount - (float) $row->invoice_total, 2);
$hasInvoice = (int) $row->invoice_count > 0;
$confirmedNoInvoice = ! $hasInvoice
&& (string) $row->scan_status === 'no_invoice'
&& trim((string) $row->last_error) === '';
$invoiceConclusion = match (true) {
$hasInvoice => '有票',
$confirmedNoInvoice => '无票',
default => '待确认',
};
@endphp
<tr>
<td class="is-nowrap">
@ -1077,14 +1143,19 @@
¥{{ $money($difference) }}
</span>
</td>
<td>
<span @class([
'invoice-data-tag',
'is-warning' => $invoiceConclusion === '无票',
'is-danger' => $invoiceConclusion === '待确认',
])>{{ $invoiceConclusion }}</span>
</td>
<td>
@if (in_array($row->scan_status, ['partial', 'failed'], true) || $row->last_error)
<span class="invoice-data-tag is-danger">{{ $statusLabels[$row->scan_status] ?? '待复核' }}</span>
<span class="invoice-data-secondary" title="{{ $row->last_error }}">{{ \Illuminate\Support\Str::limit((string) $row->last_error, 28) }}</span>
@elseif ((int) $row->invoice_count > 0)
<span class="invoice-data-tag">{{ $statusLabels[$row->scan_status] ?? '有票' }}</span>
@else
<span class="invoice-data-tag is-warning">无票</span>
<span class="invoice-data-secondary">{{ $statusLabels[$row->scan_status] ?? '待扫描' }}</span>
@endif
</td>
<td class="is-nowrap">
@ -1093,7 +1164,7 @@
</tr>
@empty
<tr>
<td colspan="12" class="invoice-data-empty">当前筛选条件下没有流程汇总。</td>
<td colspan="13" class="invoice-data-empty">当前筛选条件下没有流程汇总。</td>
</tr>
@endforelse
</tbody>
@ -1253,7 +1324,7 @@
口径:统计从 {{ $startMonth }} 开始。发票附件由服务端下载并缓存用于识别;
图片和 PDF 可在页面内预览,不再跳转钉钉临时地址。发票按打款时间归入月份,
一张发票一行,同一流程多张发票分别列示。
“原票未关联”表示该行来自早期汇总表导入,原数据没有保存 OA 附件文件信息,需要重新补扫后恢复预览。
“历史导入·无原件”表示该发票来自早期 Excel 汇总表,原数据没有保存审批附件文件,因此只能看到字段,不能预览原件;重新抓取流程附件后可补回关联。
“原始附件”按付款月份保存所有已取得的截图、图片、PDF 和表格转换件,识别不出发票的附件也会保留。
无票流程在“按流程汇总”中保留。开票金额覆盖率可能超过 100%,表示开票金额高于本月付款,
需要按具体流程复核。

View File

@ -189,10 +189,15 @@ class InvoiceDataPageTest extends FeatureTestCase
->assertSee('发票状态')
->assertSee('发票明细')
->assertSee('按流程汇总')
->assertSee('按流程汇总 (2)')
->assertSee('查看全部流程')
->assertSee('查看有票流程')
->assertSee('查看无票流程')
->assertSee('待复核 0')
->assertSee('抓取发票')
->assertSee('data-invoice-refresh', false)
->assertSee('付款流程')
->assertSee('含票流程')
->assertSee('有票流程')
->assertSee('发票价税合计(万元)')
->assertSee('开票金额覆盖率')
->assertSee('本月付款(万元)')
@ -246,7 +251,7 @@ class InvoiceDataPageTest extends FeatureTestCase
);
$this->assertNotEmpty($flowHeader);
$this->assertMatchesRegularExpression(
'/>状态<\/th>\s*<th scope="col">打款时间<\/th>\s*<\/tr>$/su',
'/>发票结论<\/th>\s*<th scope="col">识别状态<\/th>\s*<th scope="col">打款时间<\/th>\s*<\/tr>$/su',
$flowHeader[0],
);
@ -320,7 +325,7 @@ class InvoiceDataPageTest extends FeatureTestCase
]))
->assertOk()
->assertSee('data-preview-kind="pdf"', false)
->assertSee('原票未关联')
->assertSee('历史导入·无原件')
->assertDontSee('原件暂不支持预览');
}
@ -737,6 +742,72 @@ class InvoiceDataPageTest extends FeatureTestCase
);
}
public function test_automatic_scan_indexes_new_payment_months(): void
{
$schema = Schema::connection('business_mirror');
$schema->create('ding_process_detail_mirror', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->string('source_scope');
$table->string('business_id');
$table->string('document_type')->nullable();
$table->string('purpose')->nullable();
$table->string('originator_name')->nullable();
$table->string('account_name')->nullable();
$table->string('payee_name')->nullable();
$table->decimal('amount', 18, 2)->default(0);
$table->timestamps();
});
$schema->create('ding_process_payment_line_mirror', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->string('source_scope');
$table->string('business_id');
$table->string('process_instance_id')->nullable();
$table->string('paid_month');
$table->decimal('amount', 18, 2)->default(0);
$table->string('paid_at')->nullable();
$table->timestamps();
});
$now = now();
DB::connection('business_mirror')->table('ding_process_detail_mirror')->insert([
'source_scope' => 'personal_permission',
'business_id' => 'FLOW-NEW-MONTH',
'document_type' => '公摊日常报销',
'purpose' => '11月新报销流程',
'originator_name' => '测试人员',
'account_name' => '管理费用',
'payee_name' => '测试收款人',
'amount' => 123.45,
'created_at' => $now,
'updated_at' => $now,
]);
DB::connection('business_mirror')->table('ding_process_payment_line_mirror')->insert([
'source_scope' => 'personal_permission',
'business_id' => 'FLOW-NEW-MONTH',
'process_instance_id' => 'PROCESS-NEW-MONTH',
'paid_month' => '2026-11',
'amount' => 123.45,
'paid_at' => '2026-11-15',
'created_at' => $now,
'updated_at' => $now,
]);
$this->assertSame(
'2026-11',
app(DingInvoiceSyncService::class)->latestPendingMonth(),
);
$this->assertDatabaseHas('ding_invoice_flow_mirror', [
'source_scope' => 'personal_permission',
'business_id' => 'FLOW-NEW-MONTH',
'paid_month' => '2026-11',
'document_type' => '公摊日常报销',
], 'business_mirror');
$this->assertContains(
'2026-11',
app(DingInvoiceDataMirrorRepository::class)->months(),
);
}
private function createInvoiceMirrorTables(): void
{
$schema = Schema::connection('business_mirror');