diff --git a/app/Http/Controllers/Business/SupplierReconciliation.php b/app/Http/Controllers/Business/SupplierReconciliation.php index 3ccb767c..b95a1468 100644 --- a/app/Http/Controllers/Business/SupplierReconciliation.php +++ b/app/Http/Controllers/Business/SupplierReconciliation.php @@ -130,6 +130,7 @@ class SupplierReconciliation extends Controller $request->validate([ 'month' => ['nullable', 'string', 'regex:/^\d{4}-\d{2}$/'], 'reconciler' => ['nullable', 'string', 'max:191'], + 'comparison_filter' => ['nullable', 'in:,supplier_over,supplier_under,mismatch,match'], ]); [$supplier, $companyId] = $this->returnTarget($request); $detail = $this->pageData->financeDetailForFilters($supplier, $companyId, $request->query()); @@ -172,6 +173,7 @@ class SupplierReconciliation extends Controller 'date' => ['nullable', 'string', 'max:32'], 'barcode' => ['nullable', 'string', 'max:191'], 'difference_type' => ['nullable', 'string', 'max:32'], + 'comparison_filter' => ['nullable', 'in:,supplier_over,supplier_under,mismatch,match'], 'selected_rows' => ['nullable', 'array', 'max:600'], 'selected_rows.*' => ['string', 'max:2048'], ]); diff --git a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php index ab63e0ec..d8e8f45a 100644 --- a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php +++ b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php @@ -18,6 +18,15 @@ use Modules\EcommerceFinance\Services\SupplierStatementStore; */ class SupplierReconciliationPageData { + /** 财务端比对结果筛选。 */ + private const COMPARISON_FILTERS = [ + '', + 'supplier_over', + 'supplier_under', + 'mismatch', + 'match', + ]; + /** 厂家上传的对账数据表:每页行数(短表 + 翻页)。 */ private const PER_PAGE = 15; @@ -166,8 +175,15 @@ class SupplierReconciliationPageData } $detail = null; + $comparisonFilter = ''; + $comparisonFilters = []; if ($selected !== '') { $detail = $this->detail($mine, $selected, $companyId); + $comparisonFilter = $this->normalizeComparisonFilter( + request()->query('comparison_filter', ''), + ); + $detail = $this->filterComparisonDetail($detail, $comparisonFilter); + $comparisonFilters = $this->comparisonFilterOptions($detail['comparison_counts']); $totalRows = count($detail['rows']); $pages = max((int) ceil($totalRows / self::COMPARISON_PER_PAGE), 1); $currentPage = min(max((int) request()->query('compare_page', 1), 1), $pages); @@ -229,6 +245,8 @@ class SupplierReconciliationPageData 'reconciler' => $reconciler, 'selectedSupplier' => $selected, 'detail' => $detail, + 'comparisonFilter' => $comparisonFilter, + 'comparisonFilters' => $comparisonFilters, 'financeReturnPanel' => $detail !== null ? $this->financeReturnPanel($selected, $companyId, $month, $reconciler) : null, @@ -398,7 +416,10 @@ class SupplierReconciliationPageData )); } - return $this->detail($mine, $supplier, $companyId); + return $this->filterComparisonDetail( + $this->detail($mine, $supplier, $companyId), + $this->normalizeComparisonFilter($input['comparison_filter'] ?? ''), + ); } /** @@ -592,6 +613,123 @@ class SupplierReconciliationPageData return $compared; } + /** + * 按财务端关注方向筛选比对行,并保留未筛选的汇总数据。 + * + * @param array $detail + * @return array + */ + private function filterComparisonDetail(array $detail, string $comparisonFilter): array + { + $allRows = array_values((array) ($detail['rows'] ?? [])); + $counts = [ + 'all' => count($allRows), + 'supplier_over' => 0, + 'supplier_under' => 0, + 'mismatch' => 0, + 'match' => 0, + ]; + $differenceCount = 0; + + foreach ($allRows as $row) { + if ((string) ($row['type'] ?? '') !== SupplierStatementMatcher::TYPE_MATCH) { + $differenceCount++; + } + foreach (['supplier_over', 'supplier_under', 'mismatch', 'match'] as $filter) { + if ($this->comparisonRowMatchesFilter($row, $filter)) { + $counts[$filter]++; + } + } + } + + $rows = array_values(array_filter( + $allRows, + fn (array $row): bool => $this->comparisonRowMatchesFilter($row, $comparisonFilter), + )); + $filteredDifferenceCount = count(array_filter( + $rows, + static fn (array $row): bool => (string) ($row['type'] ?? '') !== SupplierStatementMatcher::TYPE_MATCH, + )); + + $detail['all_total_rows'] = count($allRows); + $detail['total_rows'] = count($rows); + $detail['rows'] = $rows; + $detail['comparison_filter'] = $comparisonFilter; + $detail['comparison_counts'] = $counts; + $detail['difference_count'] = $filteredDifferenceCount; + $detail['all_difference_count'] = $differenceCount; + + return $detail; + } + + /** @param array $row */ + private function comparisonRowMatchesFilter(array $row, string $comparisonFilter): bool + { + $type = (string) ($row['type'] ?? ''); + $amountDifference = (float) ($row['amountDifference'] ?? 0); + + return match ($comparisonFilter) { + 'supplier_over' => $type === SupplierStatementMatcher::TYPE_SUPPLIER_EXTRA + || ($type !== SupplierStatementMatcher::TYPE_MATCH && $amountDifference > 0.005), + 'supplier_under' => $type === SupplierStatementMatcher::TYPE_SUPPLIER_MISSING + || ($type !== SupplierStatementMatcher::TYPE_MATCH && $amountDifference < -0.005), + 'mismatch' => $type === SupplierStatementMatcher::TYPE_MISMATCH, + 'match' => $type === SupplierStatementMatcher::TYPE_MATCH, + default => true, + }; + } + + private function normalizeComparisonFilter(mixed $value): string + { + if (! is_scalar($value)) { + return ''; + } + + $filter = trim((string) $value); + + return in_array($filter, self::COMPARISON_FILTERS, true) ? $filter : ''; + } + + /** + * @param array{all:int,supplier_over:int,supplier_under:int,mismatch:int,match:int} $counts + * @return list + */ + private function comparisonFilterOptions(array $counts): array + { + return [ + [ + 'value' => '', + 'label' => '全部结果', + 'count' => (int) ($counts['all'] ?? 0), + 'description' => '显示双方一致和全部差异行', + ], + [ + 'value' => 'supplier_over', + 'label' => '厂家多算', + 'count' => (int) ($counts['supplier_over'] ?? 0), + 'description' => '厂家金额高于我方,包含整条多报和金额多算', + ], + [ + 'value' => 'supplier_under', + 'label' => '厂家少算', + 'count' => (int) ($counts['supplier_under'] ?? 0), + 'description' => '厂家金额低于我方,包含漏报和金额少报', + ], + [ + 'value' => 'mismatch', + 'label' => '数量/金额不一致', + 'count' => (int) ($counts['mismatch'] ?? 0), + 'description' => '双方均已匹配,但数量或金额不一致', + ], + [ + 'value' => 'match', + 'label' => '双方一致', + 'count' => (int) ($counts['match'] ?? 0), + 'description' => '数量、金额一致,可正常结算', + ], + ]; + } + /** @return list> 该厂家的退货明细(我方数据,展示给厂家确认扣减) */ public function returnRows(string $supplier, int $companyId): array { diff --git a/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php b/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php index f1e2b89a..7edee983 100644 --- a/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php +++ b/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php @@ -1,17 +1,17 @@
@@ -126,27 +160,31 @@ 未找到名为「{{ $supplierQuery }}」的供应商,请输入完整名称并从搜索建议中选择。
@endif -

- 比对口径:双方都有快递单号时,第一层优先按「快递单号 + 商品编码」汇总核对;没有快递单号或第一层无法匹配时,再按「采购单号 + 日期 + 商品编码」核对,以我方货款对账的“商品编码”匹配厂家条码;同一匹配键的多行先合并数量与金额。 - 日期未对上时,如采购单号+商品编码一致、数量金额一致且日期相差不超过 7 天,仍按一致处理。
- 数量、金额一致 → 正常结算;厂家多(我方没有)→ 不结算、退回修改; - 数量或金额不一致 → 明确列出双方数量、单价、金额及厂家多算部分,取金额低的一方结算; - 厂家少(我方有)→ 按厂家数据结算(不付款)。厂家未填采购单号时,仅在同日期+商品编码能唯一匹配我方一条数据时兜底。
- 厂家登录:在「厂家登录账号管理」新增供应商并勾选可登录,供应商名称必须与对账数据中的厂家名称完全一致; - 厂家登录后只能查看自己的对账单上传记录、全部退货明细及导出数据。 -

+
+ + 比对口径 + 优先按「快递单号 + 商品编码」,无法匹配时再按「采购单号 + 日期 + 商品编码」;数量或金额不一致时按金额低的一方结算。 + +
+ 双方都有快递单号时,第一层优先按「快递单号 + 商品编码」汇总核对;没有快递单号或第一层无法匹配时,再按「采购单号 + 日期 + 商品编码」核对,以我方货款对账的“商品编码”匹配厂家条码;同一匹配键的多行先合并数量与金额。
+ 日期未对上时,如采购单号+商品编码一致、数量金额一致且日期相差不超过 7 天,仍按一致处理。
+ 数量、金额一致 → 正常结算;厂家多(我方没有)→ 不结算、退回修改; + 数量或金额不一致 → 明确列出双方数量、单价、金额及厂家多算部分,取金额低的一方结算; + 厂家少(我方有)→ 按厂家数据结算(不付款)。厂家未填采购单号时,仅在同日期+商品编码能唯一匹配我方一条数据时兜底。
+ 厂家登录:在「厂家登录账号管理」新增供应商并勾选可登录,供应商名称必须与对账数据中的厂家名称完全一致; + 厂家登录后只能查看自己的对账单上传记录、全部退货明细及导出数据。 +
+
@if ($detail) @php - $differenceCount = (int) $detail['summary']['supplierExtra'] - + (int) $detail['summary']['mismatch'] - + (int) $detail['summary']['supplierMissing']; + $differenceCount = (int) ($detail['difference_count'] ?? 0); @endphp

{{ $selectedSupplier }} · 比对明细

- + 月份 {{ $month !== '' ? $month : '全部' }} @if ($reconciler !== '') · 对账人 {{ $reconciler }} @endif @if (($uploads[$selectedSupplier]['uploaded_at'] ?? null) !== null) @@ -155,53 +193,56 @@ · 该厂家还没有上传对账单 @endif - - ← 返回供应商列表 - 导出比对汇总 - 下载对账单模板 - @if ($differenceCount > 0) -
- @csrf - - - - - -
-
- @csrf - - - - - -
- @endif +
+ ← 返回供应商列表 + 导出当前筛选 + @if ($differenceCount > 0) +
+ @csrf + + + + + + +
+
+ @csrf + + + + + + +
+ @endif +
-
+
厂家钉钉群机器人: - {{ $robot['configured'] ? '已配置' : '未配置,配置后才能推送差异' }} + {{ $robot['configured'] ? '已配置' : '未配置(点击展开配置)' }} @if ($robot['last_pushed_at'] !== '') · 上次推送 {{ $robot['last_pushed_at'] }}({{ $robot['last_push_count'] }} 条) @endif @@ -402,23 +443,65 @@ @if ($detail)

比对汇总(我方 {{ number_format($detail['mineRows']) }} 行 / 厂家 {{ number_format($detail['theirsRows']) }} 行)

-
-
双方一致{{ $detail['summary']['match'] }}
-
厂家多(退回修改){{ $detail['summary']['supplierExtra'] }}
-
数量/金额不一致(按低结算){{ $detail['summary']['mismatch'] }}
-
厂家少(按厂家结算){{ $detail['summary']['supplierMissing'] }}
-
我方金额合计{{ number_format($detail['summary']['mineAmount'], 2) }}
-
厂家金额合计{{ number_format($detail['summary']['theirsAmount'], 2) }}
-
厂家多算金额{{ number_format($detail['summary']['supplierOverAmount'], 2) }}
-
按口径结算合计{{ number_format($detail['summary']['settleAmount'], 2) }}
-
差异金额{{ number_format($detail['summary']['differenceAmount'], 2) }}
+ @php + $comparisonFilterUrl = fn (string $value): string => route('business.pages.show', array_merge(request()->query(), [ + 'section' => $section['slug'], + 'page' => $page['slug'], + 'comparison_filter' => $value, + 'compare_page' => 1, + ])); + @endphp +
+ +
+ 当前显示 {{ number_format((int) $detail['total_rows']) }} / {{ number_format((int) $detail['all_total_rows']) }} 条 + @if ($comparisonFilter !== '') + 清除筛选 + @endif +
+
+
+
+

比对结果

+
+
双方一致{{ $detail['summary']['match'] }}
+
厂家多(退回修改){{ $detail['summary']['supplierExtra'] }}
+
数量/金额不一致(按低结算){{ $detail['summary']['mismatch'] }}
+
厂家少(按厂家结算){{ $detail['summary']['supplierMissing'] }}
+
+
+
+

金额概览

+
+
我方金额合计{{ number_format($detail['summary']['mineAmount'], 2) }}
+
厂家金额合计{{ number_format($detail['summary']['theirsAmount'], 2) }}
+
厂家多算金额{{ number_format($detail['summary']['supplierOverAmount'], 2) }}
+
按口径结算合计{{ number_format($detail['summary']['settleAmount'], 2) }}
+
差异金额{{ number_format($detail['summary']['differenceAmount'], 2) }}
+
+
- +
@@ -489,7 +572,9 @@ 厂家{{ $row['amountDifference'] > 0 ? '多' : '少' }} ¥{{ number_format(abs($row['amountDifference']), 2) }} @endif - + @empty - + + + @endforelse
- + @if ($differenceCount > 0) + + @else + — + @endif 比对结果 采购单号{{ $row['note'] }} + {{ $row['note'] }} + {{ number_format($row['settleAmount'], 2) }} @if ($row['type'] !== '一致') @@ -500,6 +585,7 @@ + @@ -518,7 +604,18 @@
该厂家还没有上传对账单,或我方还没有这家供应商的结算数据。
+
+ @if ($comparisonFilter !== '') + 当前筛选没有比对记录。 + 查看全部结果 + @else + 该厂家还没有上传对账单,或我方还没有这家供应商的结算数据。 + @endif +
+
diff --git a/tests/Feature/Business/SupplierReconciliationTest.php b/tests/Feature/Business/SupplierReconciliationTest.php index 92d101fc..1ac8e746 100644 --- a/tests/Feature/Business/SupplierReconciliationTest.php +++ b/tests/Feature/Business/SupplierReconciliationTest.php @@ -239,7 +239,7 @@ class SupplierReconciliationTest extends FeatureTestCase ->assertSeeText('厂家多 1.00') ->assertSeeText('厂家多 ¥128.00') ->assertSeeText('编号-ZSJ-1N1097V01') - ->assertSeeText('导出比对汇总') + ->assertSeeText('导出当前筛选') ->assertSee('sr-comparison-scroll', false) ->assertSeeText('每页 15 条') ->assertSeeText('ZSJ-3') @@ -247,6 +247,96 @@ class SupplierReconciliationTest extends FeatureTestCase ->assertDontSeeText('各厂家上传情况'); } + public function test_finance_comparison_filters_supplier_over_and_under_amounts(): void + { + $companyId = (int) $this->company->id; + $store = app(SupplierPayableOverrideStore::class); + foreach ([ + 'mine:over' => ['purchaseNo' => 'PO-OVER', 'skuCode' => 'A-1'], + 'mine:under' => ['purchaseNo' => 'PO-UNDER', 'skuCode' => 'B-1'], + 'mine:match' => ['purchaseNo' => 'PO-MATCH', 'skuCode' => 'C-1'], + ] as $key => $row) { + $store->save('final:supplier:payment-reconciliation', $key, $row + [ + 'summary_month' => '2026-09', + 'date' => '2026-09-30', + 'supplier' => '晨丰', + 'qty' => '1', + 'unitPrice' => '100', + 'amount' => '100', + 'reconciler' => '张三', + ], $companyId, null); + } + $this->seedStatement('晨丰', [ + array_merge($this->statementRow('A-1N1097V01', '100.00', '2', '200.00'), [ + 'purchaseNo' => 'PO-OVER', + ]), + array_merge($this->statementRow('C-1N1097V01', '100.00', '1', '100.00'), [ + 'purchaseNo' => 'PO-MATCH', + ]), + array_merge($this->statementRow('D-1N1097V01', '100.00', '1', '100.00'), [ + 'purchaseNo' => 'PO-EXTRA', + ]), + ]); + + $detail = app(SupplierReconciliationPageData::class)->financeDetailForFilters( + '晨丰', + $companyId, + ['month' => '2026-09', 'comparison_filter' => 'supplier_over'], + ); + $this->assertSame(4, $detail['all_total_rows']); + $this->assertSame(2, $detail['total_rows']); + $this->assertSame(2, $detail['difference_count']); + $this->assertSame(2, $detail['comparison_counts']['supplier_over']); + $this->assertSame(1, $detail['comparison_counts']['supplier_under']); + + $pageUrl = fn (string $filter): string => route('business.pages.show', [ + 'section' => 'supplier-reconciliation', + 'page' => 'finance-data', + 'supplier' => '晨丰', + 'month' => '2026-09', + 'comparison_filter' => $filter, + ]); + + $this->loginAs()->get($pageUrl('supplier_over')) + ->assertOk() + ->assertSeeText('厂家多算') + ->assertSeeText('厂家少算') + ->assertSeeText('当前显示 2 / 4 条') + ->assertSeeText('A-1') + ->assertSeeText('D-1') + ->assertDontSeeText('B-1') + ->assertDontSeeText('C-1') + ->assertSeeText('推送全部差异(2 条)'); + + $this->loginAs()->get($pageUrl('supplier_under')) + ->assertOk() + ->assertSeeText('当前显示 1 / 4 条') + ->assertSeeText('B-1') + ->assertDontSeeText('A-1') + ->assertDontSeeText('D-1') + ->assertDontSeeText('C-1'); + + $this->loginAs()->get($pageUrl('not-a-filter')) + ->assertOk() + ->assertSeeText('当前显示 4 / 4 条') + ->assertSeeText('A-1') + ->assertSeeText('B-1') + ->assertSeeText('C-1') + ->assertSeeText('D-1'); + + $export = $this->loginAs()->get(route('business.supplier-reconciliation.comparison.export', [ + 'supplier' => '晨丰', + 'month' => '2026-09', + 'comparison_filter' => 'supplier_over', + ])); + $content = $export->streamedContent(); + $export->assertOk()->assertDownload(); + $this->assertStringContainsString('A-1', $content); + $this->assertStringContainsString('D-1', $content); + $this->assertStringNotContainsString('B-1', $content); + $this->assertStringNotContainsString('C-1', $content); + } + public function test_supplier_without_upload_does_not_generate_or_push_missing_differences(): void { $store = app(SupplierPayableOverrideStore::class);