diff --git a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php index 9d6ed4775..b97f4a915 100644 --- a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php +++ b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php @@ -348,6 +348,48 @@ class SupplierPayableOverrideStore ->all(); } + /** + * 只读取指定厂家、月份的终版数据,避免厂家分享页加载整个历史数据集。 + * + * @return array> + */ + public function listRowsForSupplierMonth( + string $dataset, + string $supplier, + string $month, + int $companyId, + ): array { + if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { + return []; + } + + $connection = $this->connection(); + $query = $connection->table('supplier_payable_overrides') + ->where('company_id', $companyId) + ->where('dataset', $dataset); + $this->whereSupplier($query, $supplier); + + $monthExpression = $connection->getDriverName() === 'mysql' + ? 'CASE WHEN COALESCE(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.send_time")), "null"), ""), "") <> "" ' + . 'THEN LEFT(REPLACE(REPLACE(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.send_time")), "/", "-"), ".", "-"), 7) ' + . 'ELSE LEFT(REPLACE(REPLACE(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.summary_month")), "/", "-"), ".", "-"), 7) END' + : "CASE WHEN COALESCE(NULLIF(NULLIF(json_extract(`values`, '$.send_time'), 'null'), ''), '') <> '' " + . "THEN substr(replace(replace(json_extract(`values`, '$.send_time'), '/', '-'), '.', '-'), 1, 7) " + . "ELSE substr(replace(replace(json_extract(`values`, '$.summary_month'), '/', '-'), '.', '-'), 1, 7) END"; + + return $query + ->whereRaw("{$monthExpression} = ?", [$month]) + ->orderBy('id') + ->get(['record_key', 'values']) + ->map(static function (object $record): array { + $values = json_decode((string) $record->values, true) ?: []; + $values['_action_key'] = (string) $record->record_key; + + return $values; + }) + ->all(); + } + /** @return list */ public function distinctSuppliersInDataset(string $dataset, int $companyId): array { diff --git a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php index cfb5f9c6f..6032eb4d4 100644 --- a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php +++ b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php @@ -1111,21 +1111,14 @@ class SupplierReconciliationPageData $result[$supplier] = []; } - $allRows = $this->store->listRows( - PayablesFinalActions::DATASET_PREFIX . 'supplier:return-reconciliation', - $companyId, - ); - foreach ($allRows as $row) { - $supplier = trim((string) ($row['supplier'] ?? '')); - if (! array_key_exists($supplier, $result) - || $this->returnRowMonth($row) !== (string) $normalizedTargets[$supplier]) { - continue; - } - - $result[$supplier][] = $row; - } - - foreach ($result as $supplier => &$rows) { + $dataset = PayablesFinalActions::DATASET_PREFIX . 'supplier:return-reconciliation'; + foreach ($normalizedTargets as $supplier => $month) { + $rows = $this->store->listRowsForSupplierMonth( + $dataset, + $supplier, + $month, + $companyId, + ); if ($rows === []) { continue; } @@ -1135,8 +1128,8 @@ class SupplierReconciliationPageData $row = $this->returnAccounting->enrich($row); } unset($row); + $result[$supplier] = $rows; } - unset($rows); return $result; } diff --git a/tests/Feature/Business/SupplierPayableFinalPagesTest.php b/tests/Feature/Business/SupplierPayableFinalPagesTest.php index 8bb93d8e1..5725457e5 100644 --- a/tests/Feature/Business/SupplierPayableFinalPagesTest.php +++ b/tests/Feature/Business/SupplierPayableFinalPagesTest.php @@ -22,6 +22,47 @@ class SupplierPayableFinalPagesTest extends FeatureTestCase ); } + public function test_supplier_month_rows_ignore_unrelated_suppliers_and_months(): void + { + $dataset = 'final:supplier:return-reconciliation'; + app(SupplierPayableOverrideStore::class)->saveBatch($dataset, [ + 'target:month' => [ + 'summary_month' => '2026-11', + 'send_time' => '2026-11-08', + 'supplier' => '华韵', + 'barcode' => 'TARGET-MONTH', + ], + 'target:fallback-month' => [ + 'send_time' => '2026-11-09 10:00:00', + 'supplier' => '华韵', + 'barcode' => 'TARGET-FALLBACK', + ], + 'other:month' => [ + 'summary_month' => '2026-10', + 'send_time' => '2026-10-08', + 'supplier' => '华韵', + 'barcode' => 'OTHER-MONTH', + ], + 'other:supplier' => [ + 'summary_month' => '2026-11', + 'send_time' => '2026-11-08', + 'supplier' => '其他厂家', + 'barcode' => 'OTHER-SUPPLIER', + ], + ], (int) $this->company->id, (int) $this->user->id); + + $rows = app(SupplierPayableOverrideStore::class)->listRowsForSupplierMonth( + $dataset, + '华韵', + '2026-11', + (int) $this->company->id, + ); + $barcodes = array_column($rows, 'barcode'); + sort($barcodes); + + $this->assertSame(['TARGET-FALLBACK', 'TARGET-MONTH'], $barcodes); + } + public function test_final_table_page_keeps_the_maintenance_toolbar_and_starts_empty(): void { $this->loginAs()