diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 3c9feae9..018f24d0 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -86,6 +86,7 @@ use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore; use Modules\EcommerceFinance\Services\SupplierFreeShippingStore; use Modules\EcommerceFinance\View\PlatformPromotionAccountPageData; use Modules\EcommerceFinance\View\OrderDataPageData; +use Modules\EcommerceFinance\View\SupplierNegativeAlertPageData; use RuntimeException; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -4717,6 +4718,7 @@ class Pages extends Controller 'planting-loss' => app(\Modules\EcommerceFinance\View\PlantingLossPageData::class)->page(), 'finance-data' => app(\Modules\EcommerceFinance\View\SupplierReconciliationPageData::class)->financeData(), 'supplier-data' => app(\Modules\EcommerceFinance\View\SupplierReconciliationPageData::class)->supplierData(), + 'supplier-negative-alert' => app(SupplierNegativeAlertPageData::class)->page(), 'store-report-data-logic' => $this->prepareStoreReportLogic(), 'tax-forecast-data-logic' => $this->contentForTaxForecastLogic(), default => null, diff --git a/modules/EcommerceFinance/View/SupplierNegativeAlertPageData.php b/modules/EcommerceFinance/View/SupplierNegativeAlertPageData.php new file mode 100644 index 00000000..7b451e02 --- /dev/null +++ b/modules/EcommerceFinance/View/SupplierNegativeAlertPageData.php @@ -0,0 +1,147 @@ + */ + public function page(): array + { + $rows = $this->store->listRows( + PayablesFinalActions::DATASET_PREFIX . self::DATASET, + (int) (company_id() ?: 1), + ); + + $negativeRows = []; + foreach ($rows as $row) { + $balance = $this->number($row['supplier_balance'] ?? null); + if ($balance === null || $balance >= 0) { + continue; + } + + $row['supplier_balance'] = $balance; + $negativeRows[] = $row; + } + + usort($negativeRows, static function (array $left, array $right): int { + return strcmp( + (string) ($right['summary_month'] ?? ''), + (string) ($left['summary_month'] ?? ''), + ) ?: strnatcasecmp( + (string) ($left['supplier'] ?? ''), + (string) ($right['supplier'] ?? ''), + ); + }); + + $months = []; + foreach ($negativeRows as $row) { + $month = trim((string) ($row['summary_month'] ?? '')); + if (preg_match('/^\d{4}-\d{2}$/', $month) === 1) { + $months[$month] = true; + } + } + $months = array_keys($months); + rsort($months); + + $requestedMonth = trim((string) request()->query('month', '')); + $month = in_array($requestedMonth, $months, true) ? $requestedMonth : ''; + if ($month !== '') { + $negativeRows = array_values(array_filter( + $negativeRows, + static fn (array $row): bool => (string) ($row['summary_month'] ?? '') === $month, + )); + } + + $supplierNames = []; + foreach ($negativeRows as $row) { + $supplier = trim((string) ($row['supplier'] ?? '')); + if ($supplier !== '') { + $supplierNames[$supplier] = true; + } + } + $supplierNames = array_keys($supplierNames); + sort($supplierNames, SORT_NATURAL | SORT_FLAG_CASE); + + $supplierQuery = mb_substr(trim((string) request()->query('supplier', '')), 0, 191); + if ($supplierQuery !== '') { + $needle = mb_strtolower($supplierQuery); + $negativeRows = array_values(array_filter( + $negativeRows, + static function (array $row) use ($needle): bool { + $haystack = mb_strtolower(implode("\n", [ + (string) ($row['supplier'] ?? ''), + (string) ($row['company'] ?? ''), + ])); + + return str_contains($haystack, $needle); + }, + )); + } + + $supplierCounts = []; + foreach ($negativeRows as $row) { + $key = trim((string) ($row['company'] ?? '')) . "\0" . trim((string) ($row['supplier'] ?? '')); + $supplierCounts[$key] = true; + } + + $total = count($negativeRows); + $pages = max((int) ceil($total / self::PER_PAGE), 1); + $page = min(max((int) request()->query('page', 1), 1), $pages); + $items = array_map( + static fn (array $row): object => (object) $row, + array_slice($negativeRows, ($page - 1) * self::PER_PAGE, self::PER_PAGE), + ); + + return [ + 'view' => 'business.pages.negative-alerts.supplier-negative-alert', + 'negativeAlertRows' => new LengthAwarePaginator( + $items, + $total, + self::PER_PAGE, + $page, + [ + 'path' => request()->url(), + 'query' => request()->except('page'), + ], + ), + 'negativeAlertMonths' => $months, + 'negativeAlertMonth' => $month, + 'negativeAlertSupplier' => $supplierQuery, + 'negativeAlertSupplierNames' => $supplierNames, + 'negativeAlertSupplierCount' => count($supplierCounts), + 'negativeAlertTotal' => $total, + 'negativeAlertSourceUrl' => route('business.pages.show', [ + 'section' => 'supplier-payables-final', + 'page' => 'supplier-payables-report', + ]), + ]; + } + + private function number(mixed $value): ?float + { + if ($value === null || trim((string) $value) === '') { + return null; + } + + $normalized = str_replace([',', ',', ' '], '', trim((string) $value)); + + return is_numeric($normalized) ? (float) $normalized : null; + } +} diff --git a/resources/views/business/pages/negative-alerts/supplier-negative-alert.blade.php b/resources/views/business/pages/negative-alerts/supplier-negative-alert.blade.php index 5bde3569..c5fa23a3 100644 --- a/resources/views/business/pages/negative-alerts/supplier-negative-alert.blade.php +++ b/resources/views/business/pages/negative-alerts/supplier-negative-alert.blade.php @@ -1,13 +1,411 @@ -@include('business.pages.negative-alerts._placeholder', [ - 'icon' => 'factory', - 'scopeLabel' => '供应商', - 'statusTitle' => '供应商负数判定口径待确认', - 'description' => '待确认按“供应商余额”还是“应付余额”判定负数,并接入真实数据后,这里将展示需要关注的供应商、负数金额及异常原因。', - 'scopeItems' => [ - ['label' => '判定字段', 'value' => '待确认'], - ['label' => '数据来源', 'value' => '待接入'], - ['label' => '数据日期', 'value' => '待接入'], - ], - 'emptyTitle' => '供应商预警数据将在口径确认后接入', - 'emptyDescription' => '接入后将按供应商汇总负数余额,并保留可追溯的负数原因和数据日期。', -]) +@php + $rows = $negativeAlertRows ?? null; + $months = $negativeAlertMonths ?? []; + $month = $negativeAlertMonth ?? ''; + $supplierQuery = $negativeAlertSupplier ?? ''; + $supplierNames = $negativeAlertSupplierNames ?? []; + $supplierCount = (int) ($negativeAlertSupplierCount ?? 0); + $total = (int) ($negativeAlertTotal ?? 0); + $sourceUrl = $negativeAlertSourceUrl ?? '#'; + $money = static fn (mixed $value): string => $value === null ? '-' : number_format((float) $value, 2); +@endphp + +
+ + +
+ + + +
+

供应商余额小于 0 的终版数据

+

数据实时读取自“供应商应付(终版)→ 供应商应付表”,仅显示供应商余额 < 0 的记录。

+
+
+ +
+
+ 负数记录 + {{ number_format($total) }} 条 +
+
+ 涉及供应商 + {{ number_format($supplierCount) }} 家 +
+
+ 当前筛选月份 + {{ $month !== '' ? $month : '全部月份' }} +
+
+ +
+ + + +
+ +
+ @if ($rows && $rows->isNotEmpty()) +
+ + + + + + + + + + + + + + + + @foreach ($rows as $row) + + + + + + + + + + + + @endforeach + +
月份供应商公司主体名称供应商简称供应商余额应付余额对账人结算方式负数原因备注
{{ $row->summary_month ?? '-' }}{{ ($row->company ?? null) ?: '-' }}{{ ($row->supplier ?? null) ?: '-' }}{{ $money($row->supplier_balance ?? null) }} (float) ($row->should_pay ?? 0) < 0])> + {{ $money($row->should_pay ?? null) }} + {{ ($row->checker ?? null) ?: '-' }}{{ ($row->settlement_method ?? null) ?: '-' }}{{ ($row->negative_reason ?? null) ?: '-' }}{{ ($row->remarks ?? null) ?: '-' }}
+
+
+ + 共 {{ number_format($rows->total()) }} 条 · 第 {{ $rows->currentPage() }} / {{ $rows->lastPage() }} 页 + + + @if (!$rows->onFirstPage()) + 上一页 + @endif + 每页 {{ $rows->perPage() }} 条 + @if ($rows->hasMorePages()) + 下一页 + @endif + +
+ @else +
+ + 当前筛选没有负数供应商记录 + 请调整月份或供应商条件后重新查询。数据仍来自供应商应付(终版)中的供应商应付表。 +
+ @endif +
+
diff --git a/tests/Feature/Business/NegativeAlertPagesTest.php b/tests/Feature/Business/NegativeAlertPagesTest.php index 1b93eacd..f645b754 100644 --- a/tests/Feature/Business/NegativeAlertPagesTest.php +++ b/tests/Feature/Business/NegativeAlertPagesTest.php @@ -2,9 +2,11 @@ namespace Tests\Feature\Business; +use App\Http\Controllers\Business\PayablesFinalActions; use App\Listeners\Menu\ShowInAdmin; use App\Models\Auth\Permission; use App\Utilities\BusinessNavigation; +use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore; use Tests\Feature\FeatureTestCase; class NegativeAlertPagesTest extends FeatureTestCase @@ -41,6 +43,12 @@ class NegativeAlertPagesTest extends FeatureTestCase public function test_supplier_and_courier_negative_alert_pages_render_as_tabs(): void { + $this->saveSupplierPayable('negative:1', '负余额供应商', '-128.50', '2026-09', [ + 'company' => '测试供应商有限公司', + 'negative_reason' => '历史多付款', + ]); + $this->saveSupplierPayable('positive:1', '正余额供应商', '88.00', '2026-09'); + $this->loginAs() ->get(route('business.pages.show', [ 'section' => 'negative-alerts', @@ -49,8 +57,11 @@ class NegativeAlertPagesTest extends FeatureTestCase ->assertOk() ->assertSeeText('供应商负数预警') ->assertSeeText('快递负数预警') - ->assertSeeText('供应商负数判定口径待确认') - ->assertSeeText('待接入'); + ->assertSeeText('供应商余额小于 0 的终版数据') + ->assertSeeText('负余额供应商') + ->assertSeeText('-128.50') + ->assertSeeText('历史多付款') + ->assertDontSeeText('正余额供应商'); $this->loginAs() ->get(route('business.pages.show', [ @@ -64,6 +75,24 @@ class NegativeAlertPagesTest extends FeatureTestCase ->assertSeeText('待接入'); } + public function test_supplier_negative_alert_filters_by_month_and_supplier(): void + { + $this->saveSupplierPayable('negative:sep', '九月负余额供应商', '-99.00', '2026-09'); + $this->saveSupplierPayable('negative:aug', '八月负余额供应商', '-66.00', '2026-08'); + + $this->loginAs() + ->get(route('business.pages.show', [ + 'section' => 'negative-alerts', + 'page' => 'supplier-negative-alert', + 'month' => '2026-08', + 'supplier' => '八月负余额', + ])) + ->assertOk() + ->assertSeeText('八月负余额供应商') + ->assertSeeText('-66.00') + ->assertDontSeeText('九月负余额供应商'); + } + public function test_supplier_negative_alert_route_no_longer_renders_under_supplier_reconciliation(): void { $this->withExceptionHandling() @@ -74,4 +103,24 @@ class NegativeAlertPagesTest extends FeatureTestCase ])) ->assertRedirect(); } + + private function saveSupplierPayable( + string $key, + string $supplier, + string $supplierBalance, + string $month, + array $extra = [], + ): void { + app(SupplierPayableOverrideStore::class)->save( + PayablesFinalActions::DATASET_PREFIX . 'supplier:supplier-payables-report', + $key, + $extra + [ + 'summary_month' => $month, + 'supplier' => $supplier, + 'supplier_balance' => $supplierBalance, + ], + (int) $this->company->id, + (int) $this->user->id, + ); + } }