*/ public function financeData(): array { $companyId = $this->companyId(); $mine = $this->store->listRows( PayablesFinalActions::DATASET_PREFIX . 'supplier:payment-reconciliation', $companyId, ); $months = []; foreach ($mine as $row) { $month = substr(trim((string) ($row['summary_month'] ?? '')), 0, 7); if ($month !== '') { $months[$month] = true; } } $months = array_keys($months); rsort($months); $month = trim((string) request()->query('month', '')); if ($month !== '' && preg_match('/^\d{4}-\d{2}$/', $month) === 1) { $mine = array_values(array_filter( $mine, static fn (array $row): bool => substr(trim((string) ($row['summary_month'] ?? '')), 0, 7) === $month, )); } else { $month = ''; } $reconcilers = []; foreach ($mine as $row) { $name = trim((string) ($row['reconciler'] ?? '')); if ($name !== '') { $reconcilers[$name] = true; } } $reconcilers = array_keys($reconcilers); sort($reconcilers); $reconciler = trim((string) request()->query('reconciler', '')); if ($reconciler !== '') { $mine = array_values(array_filter( $mine, static fn (array $row): bool => trim((string) ($row['reconciler'] ?? '')) === $reconciler, )); } $suppliers = []; foreach ($mine as $row) { $name = trim((string) ($row['supplier'] ?? '')); if ($name !== '') { $suppliers[$name] = true; } } foreach ($this->statements->uploadedSuppliers($companyId) as $name) { $suppliers[$name] = true; } $supplierNames = array_keys($suppliers); sort($supplierNames); $uploads = []; foreach ($supplierNames as $name) { $latest = $this->statements->uploads($name, $companyId, 1); $uploads[$name] = $latest[0] ?? null; } // 选中厂家时页面切换成「比对明细」视图(列表不再渲染,避免 100+ 家时往下翻)。 $selected = trim((string) request()->query('supplier', '')); $detail = null; if ($selected !== '' && in_array($selected, $supplierNames, true)) { $detail = $this->detail($mine, $selected, $companyId); } else { $selected = ''; } return [ 'view' => 'business.pages.supplier-reconciliation.finance-data', 'supplierNames' => $supplierNames, 'uploads' => $uploads, 'months' => $months, 'month' => $month, 'reconcilers' => $reconcilers, 'reconciler' => $reconciler, 'selectedSupplier' => $selected, 'detail' => $detail, 'listUrl' => route('business.pages.show', array_filter([ 'section' => 'supplier-reconciliation', 'page' => 'finance-data', 'month' => $month, 'reconciler' => $reconciler, ], static fn (mixed $value): bool => $value !== '' && $value !== null)), 'statementTemplateUrl' => route('business.supplier-reconciliation.template'), 'uploadUrl' => route('business.supplier-reconciliation.statement.upload'), ]; } /** @return array */ public function supplierData(): array { $companyId = $this->companyId(); $mine = $this->store->listRows( PayablesFinalActions::DATASET_PREFIX . 'supplier:payment-reconciliation', $companyId, ); $suppliers = []; foreach ($mine as $row) { $name = trim((string) ($row['supplier'] ?? '')); if ($name !== '') { $suppliers[$name] = true; } } foreach ($this->statements->uploadedSuppliers($companyId) as $name) { $suppliers[$name] = true; } $supplierNames = array_keys($suppliers); sort($supplierNames); $selected = trim((string) request()->query('supplier', '')); if ($selected === '' || ! in_array($selected, $supplierNames, true)) { $selected = $supplierNames[0] ?? ''; } $page = max((int) request()->query('rows_page', 1), 1); $statementRows = $selected !== '' ? $this->statements->rows($selected, $companyId) : []; $returnRows = $selected !== '' ? $this->returnRows($selected, $companyId) : []; return [ 'view' => 'business.pages.supplier-reconciliation.supplier-data', 'supplierNames' => $supplierNames, 'selectedSupplier' => $selected, 'uploads' => $selected !== '' ? $this->statements->uploads($selected, $companyId) : [], 'statementRows' => array_slice($statementRows, ($page - 1) * self::PER_PAGE, self::PER_PAGE), 'statementTotal' => count($statementRows), 'returnRows' => array_slice($returnRows, ($page - 1) * self::PER_PAGE, self::PER_PAGE), 'returnTotal' => count($returnRows), 'rowsPage' => $page, 'perPage' => self::PER_PAGE, 'statementTemplateUrl' => route('business.supplier-reconciliation.template'), 'uploadUrl' => route('business.supplier-reconciliation.statement.upload'), ]; } /** @return array */ private function detail(array $mine, string $supplier, int $companyId): array { $mineRows = array_values(array_filter( $mine, static fn (array $row): bool => trim((string) ($row['supplier'] ?? '')) === $supplier, )); $theirRows = $this->statements->rows($supplier, $companyId); $compared = $this->matcher->compare($mineRows, $theirRows); $compared['mineRows'] = count($mineRows); $compared['theirsRows'] = count($theirRows); return $compared; } /** @return list> 该厂家的退货明细(我方数据,只读展示给厂家) */ private function returnRows(string $supplier, int $companyId): array { $rows = $this->store->listRows( PayablesFinalActions::DATASET_PREFIX . 'supplier:return-reconciliation', $companyId, ); return array_values(array_filter( $rows, static fn (array $row): bool => trim((string) ($row['supplier'] ?? '')) === $supplier, )); } private function companyId(): int { return (int) (company_id() ?: 1); } }