diff --git a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php index ef783c428..12109ef00 100644 --- a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php +++ b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php @@ -743,6 +743,84 @@ class SupplierPayableOverrideStore return $suppliers; } + /** + * 退货数据优先按发货时间归月;MySQL 下使用已有日期索引列,避免逐行 JSON 函数计算。 + * + * @return list + */ + public function distinctSuppliersForSendTimeMonth( + string $dataset, + int $companyId, + string $month, + string $reconciler = '', + ): array { + if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1) { + return []; + } + + $connection = $this->connection(); + $schema = Schema::connection($connection->getName()); + if ($connection->getDriverName() !== 'mysql' + || ! $schema->hasColumn('supplier_payable_overrides', 'summary_month_key') + || ! $schema->hasColumn('supplier_payable_overrides', 'record_date_key') + || ! $schema->hasColumn('supplier_payable_overrides', 'supplier_name_key')) { + return $this->distinctSuppliersForFilters( + $dataset, + $companyId, + $month, + $reconciler, + true, + ); + } + + $start = $month . '-01'; + $next = \Illuminate\Support\Carbon::createFromFormat('!Y-m', $month, 'Asia/Shanghai') + ->addMonthNoOverflow() + ->toDateString(); + $sendTime = $this->jsonStringExpression($connection, 'send_time'); + $sendTimeEmpty = "COALESCE(NULLIF(NULLIF({$sendTime}, 'null'), ''), '') = ''"; + $sendTimePresent = "COALESCE(NULLIF(NULLIF({$sendTime}, 'null'), ''), '') <> ''"; + + $values = $connection->table('supplier_payable_overrides') + ->where('company_id', $companyId) + ->where('dataset', $dataset); + if ($reconciler !== '') { + $values->where('values->reconciler', $reconciler); + } + $values = $values + ->where(function ($query) use ($connection, $month, $start, $next, $sendTimeEmpty, $sendTimePresent): void { + $query->where(function ($dateQuery) use ($start, $next): void { + $dateQuery->whereNotNull('record_date_key') + ->where('record_date_key', '>=', $start) + ->where('record_date_key', '<', $next); + })->orWhere(function ($summaryQuery) use ($month, $sendTimeEmpty): void { + $summaryQuery->whereRaw($sendTimeEmpty) + ->where('summary_month_key', $month); + })->orWhere(function ($fallbackQuery) use ($connection, $month, $sendTimePresent): void { + $fallbackQuery->whereNull('record_date_key') + ->whereRaw($sendTimePresent) + ->whereRaw( + $this->jsonMonthExpression($connection, 'send_time') . ' = ?', + [$month], + ); + }); + }) + ->whereNotNull('supplier_name_key') + ->where('supplier_name_key', '<>', '') + ->distinct() + ->pluck('supplier_name_key'); + + $suppliers = $values + ->map(static fn (mixed $value): string => trim((string) $value)) + ->filter() + ->unique() + ->values() + ->all(); + sort($suppliers, SORT_NATURAL | SORT_FLAG_CASE); + + return $suppliers; + } + /** * 只返回指定账期涉及的供应商,供月度催办统计使用。 * diff --git a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php index 8faf32fd5..f0014e76f 100644 --- a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php +++ b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php @@ -87,12 +87,11 @@ class SupplierReconciliationPageData ) as $name) { $suppliers[$name] = true; } - foreach ($this->store->distinctSuppliersForFilters( + foreach ($this->store->distinctSuppliersForSendTimeMonth( $returnDataset, $companyId, $month, $reconciler, - true, ) as $name) { $suppliers[$name] = true; }