diff --git a/config/order_after_sales_cost.php b/config/order_after_sales_cost.php index 43ef00f4..b35e110f 100644 --- a/config/order_after_sales_cost.php +++ b/config/order_after_sales_cost.php @@ -10,6 +10,10 @@ */ return [ + // 聚水潭「商品利润 → 订单售后费用数据」原表可靠覆盖的起始日期: + // 更早月份在原表里不完整(例如 6 月订单只有费用行、没有组件/成本),走导入文件。 + 'goods_profit_from' => '2026-07-01', + 'headers' => [ '内部订单号' => 'internal_order_no', '线上订单号' => 'online_order_no', diff --git a/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php b/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php index 3961d9b6..951420d2 100644 --- a/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php +++ b/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php @@ -184,15 +184,22 @@ class GoodsProfitAfsRepository // 只按单号(不限日期)查询:原表命中该单就用原表,否则交给导入历史数据兜底。 if ($from === '' && $to === '' && $orderNo !== '') { + $dateColumn = $columns['order_time'] ?? null; + foreach ($this->orderNumberFields($orderNo) as $field) { if (! isset($columns[$field])) { continue; } - if ($this->connection()->table(self::MAIN_TABLE) - ->where($columns[$field], 'like', $orderNo . '%') - ->limit(1) - ->exists()) { + $query = $this->connection()->table(self::MAIN_TABLE) + ->where($columns[$field], 'like', $orderNo . '%'); + + // 只有带订单时间的行才是完整明细;只有费用行的历史订单交给导入数据。 + if ($dateColumn !== null) { + $query->whereNotNull($dateColumn)->where($dateColumn, '<>', ''); + } + + if ($query->limit(1)->exists()) { return true; } } @@ -200,6 +207,12 @@ class GoodsProfitAfsRepository return false; } + $start = (string) config('order_after_sales_cost.goods_profit_from', '2026-07-01'); + + if ($start !== '' && $from !== '' && $from < $start) { + return false; + } + // “是否覆盖”只看日期范围,不掺业务筛选:筛选条件由 paginate 应用, // 筛不到就该显示空结果,而不是回退到旧订单镜像(否则筛选会被忽略)。 if ($from === '' && $to === '') { @@ -212,15 +225,17 @@ class GoodsProfitAfsRepository $expression = $this->dateExpression($columns['order_time']); $range = $this->connection()->table(self::MAIN_TABLE) - ->selectRaw("MIN({$expression}) as min_time, MAX({$expression}) as max_time") + ->selectRaw("MAX({$expression}) as max_time") ->first(); - if (! $range || $range->min_time === null) { + if (! $range || $range->max_time === null) { return false; } - return ($from === '' || substr((string) $range->min_time, 0, 10) <= $from) - && ($to === '' || substr((string) $range->max_time, 0, 10) >= $to); + // 起始日受 goods_profit_from 约束,结束日按原表实际最大日期判断(今天往往晚于最后一次同步)。 + $anchor = $from !== '' ? $from : $to; + + return $anchor === '' || substr((string) $range->max_time, 0, 10) >= $anchor; } /**