get('courier-freight-financial-version:' . $companyId, 0); } /** * 返回仅包含有快递运费来源的月份。调用方据此区分「本月无数据」和「金额为 0」。 * * @param list $reportMonths YYYY-MM * @return array> 月份 => [店铺 => 快递运费] */ public function monthlyStoreTotals(array $reportMonths, int $companyId): array { $reportMonths = array_values(array_unique(array_filter( $reportMonths, static fn (string $month): bool => preg_match('/^\d{4}-\d{2}$/', $month) === 1, ))); $wantedMonths = array_fill_keys($reportMonths, true); if ($wantedMonths === []) { return []; } $cacheKey = 'courier-freight-financial-monthly:v1:' . $companyId . ':' . $this->version($companyId) . ':' . hash('sha256', implode(',', $reportMonths)); return cache()->remember($cacheKey, now()->addHour(), function () use ( $companyId, $wantedMonths, $reportMonths, ): array { // 先利用 (company_id, source) 索引找出目标月份对应的来源, // 避免在 100 万级明细上直接分组,也避免没有当月来源时仍扫描全表。 $sources = $this->databases->connection()->table('courier_freight_rows') ->where('company_id', $companyId) ->whereNull('deleted_at') ->distinct() ->orderBy('source') ->pluck('source') ->map(static fn ($source): string => trim((string) $source)) ->filter() ->values() ->all(); $matchingSources = []; foreach ($sources as $source) { $month = $this->sourceMonth($source, $reportMonths); if ($month !== null && isset($wantedMonths[$month])) { $matchingSources[$source] = true; } } if ($matchingSources === []) { return []; } return $this->buildMonthlyStoreTotals($companyId, array_keys($wantedMonths), array_keys($matchingSources)); }); } /** * @param list $reportMonths * @param list $sources * @return array> */ private function buildMonthlyStoreTotals(int $companyId, array $reportMonths, array $sources): array { $differences = $this->summaries->mapByMonth( $companyId, CourierPayableSummaryMap::PAGE_FREIGHT_DIFFERENCE, ); $surcharges = $this->summaries->mapByMonth( $companyId, CourierPayableSummaryMap::PAGE_OTHER_SURCHARGE, ); $returnFees = $this->summaries->mapByMonth( $companyId, CourierPayableSummaryMap::PAGE_RETURN_FEE, ); $totals = []; $feeApplied = []; $rows = $this->databases->connection()->table('courier_freight_rows') ->where('company_id', $companyId) ->whereNull('deleted_at') ->whereIn('source', $sources) ->select(['source', 'shop_name', 'transaction_no']) ->selectRaw('SUM(COALESCE(freight_amount, 0)) AS freight_amount') ->groupBy('source', 'shop_name', 'transaction_no') ->orderBy('source') ->orderBy('shop_name') ->orderBy('transaction_no') ->cursor(); foreach ($rows as $row) { $month = $this->sourceMonth( (string) $row->source, $reportMonths, ); if ($month === null || ! in_array($month, $reportMonths, true)) { continue; } // 即使店铺为空也保留月份,表示该月已有快递成本来源。 $totals[$month] ??= []; $storeName = trim((string) $row->shop_name); if ($storeName === '') { continue; } $transactionNo = trim((string) $row->transaction_no); $amount = (float) $row->freight_amount; $feeKey = $month . "\0" . $transactionNo; if ($transactionNo !== '' && ! isset($feeApplied[$feeKey])) { $amount += $this->numeric($differences[$month][$transactionNo] ?? null); $amount += $this->numeric($surcharges[$month][$transactionNo] ?? null); $amount += $this->numeric($returnFees[$month][$transactionNo] ?? null); $feeApplied[$feeKey] = true; } $totals[$month][$storeName] = round( ($totals[$month][$storeName] ?? 0.0) + $amount, 2, ); } return $totals; } /** * 来源通常为「提供成本9月汇总」。优先取来源里的年份;没有年份时, * 用当前报表年份补齐。若来源是「5-7月」范围,取最后月份作为该汇总的账期。 * * @param list $reportMonths */ private function sourceMonth(string $source, array $reportMonths): ?string { $source = trim($source); if ($source === '') { return null; } if (preg_match( '/(?