> $rows 未按页面筛选的整月核算结果 */ public function build(object $factory, string $month, array $rows): array { $template = $this->store->summaryTemplate($factory, $month); $billTotal = $settleTotal = null; $extras = []; foreach ([Category::MISMATCH, Category::DUPLICATE, Category::UNMATCHED, Category::UNSHIPPED] as $category) { $extras[DropshipReconciler::LABELS[$category]] = null; } foreach ($rows as $r) { if ($r['bill'] === null) { continue; } if (in_array($r['status'], DropshipReconciler::PAYABLE, true)) { $billTotal = (float) $billTotal + (float) $r['bill']; $final = $r['final'] ?? $r['settle']; if ($final !== null) { $settleTotal = (float) $settleTotal + (float) $final; } } elseif ($r['status'] === Category::LATER) { $label = Category::label(Category::LATER, (string) $r['status_month']); $extras[$label] = (float) ($extras[$label] ?? 0) + (float) $r['bill']; } elseif (array_key_exists($label = DropshipReconciler::LABELS[$r['status']] ?? '', $extras)) { $extras[$label] = (float) $extras[$label] + (float) $r['bill']; } } $extras = array_filter($extras, static fn ($v): bool => $v !== null); // 对方附加费保留原金额,自算合计读取同月逐票匹配结果。 $fee = 0.0; $hasFee = false; foreach ($this->store->db()->table('dropship_bill_files')->where('factory_id', $factory->id)->where('bill_month', $month) ->where('status', 'parsed')->get(['id', 'stored_path', 'sheets']) as $file) { $fees = $this->store->feesFor($file); if ($fees !== []) { $fee += array_sum($fees); $hasFee = true; } } // 上月手工汇总里的分类行(快递不符、未发出、跨月订单-X月……)这个月没有就不留空行 $labels = array_values(array_filter($template['labels'], static fn (string $l): bool => array_key_exists(trim($l), $extras) || ! preg_match('/^(快递不符|重复|匹配不到|未发出|跨月订单)/u', trim($l)))); if (! $hasFee) { $labels = array_values(array_filter($labels, static fn (string $l): bool => ! str_contains($l, '附加费'))); } if (($billTotal !== null || $settleTotal !== null || $extras !== []) && ! array_filter($labels, [DropshipSummaryTemplate::class, 'isFreight'])) { array_unshift($labels, '运费'); } foreach (array_keys($extras) as $label) { if (! in_array($label, array_map('trim', $labels), true)) { $labels[] = $label; } } $feeLabel = null; if ($hasFee) { $feeLabel = collect($labels)->first(static fn (string $l): bool => str_contains($l, '附加费')) ?? self::FEE_LABEL; if (! in_array($feeLabel, $labels, true)) { $labels[] = $feeLabel; } } $items = []; $sums = ['bill' => 0.0, 'settle' => 0.0]; foreach (array_values($labels) as $label) { [$bill, $settle] = match (true) { DropshipSummaryTemplate::isFreight($label) => [$billTotal === null ? null : round($billTotal, 2), $settleTotal === null ? null : round($settleTotal, 2)], array_key_exists(trim($label), $extras) => [round($extras[trim($label)], 2), 0.0], $label === $feeLabel => [round($fee, 2), $this->surchargeSelf((int) $factory->id, $month)], default => [null, null], }; $items[] = ['label' => $label, 'bill' => $bill, 'settle' => $settle, 'diff' => $bill === null && $settle === null ? null : round((float) $bill - (float) $settle, 2)]; $sums['bill'] += (float) $bill; $sums['settle'] += (float) $settle; } $sums = array_map(static fn (float $value): float => round($value, 2), $sums); $sums['diff'] = round($sums['bill'] - $sums['settle'], 2); return ['title' => $template['title'], 'headers' => array_slice(array_pad($template['headers'], 3, ''), 0, 3), 'rows' => $items, 'total_label' => $template['total'], 'totals' => $sums]; } private function surchargeSelf(int $factoryId, string $month): ?float { $matcher = app(\App\Support\CourierBills\SurchargeSettlementMatcher::class); if (! $matcher->ready()) return null; if (! $this->store->db()->table('dropship_bill_surcharges')->where('factory_id', $factoryId)->where('bill_month', $month)->exists()) return null; $state = app(DropshipConfirmation::class)->state($month, $factoryId, (int) (company_id() ?: 1)); if (($state['status'] ?? '') === 'confirmed') return null; return round((float) $matcher->query('dropship', $factoryId, $month)->sum('self_amount'), 2); } }