select('period') ->whereNotNull('period') ->where('period', '<>', '') ->distinct() ->orderByDesc('period') ->pluck('period') ->map(static fn ($period): string => trim((string) $period)) ->filter(fn (string $period): bool => $this->normalizePeriod($period) !== null) ->unique() ->values(); $requestedPeriod = $this->normalizePeriod( trim((string) request()->query('period', '')) ); $period = $requestedPeriod ?? $periodOptions->first() ?? ''; $sellerEntity = mb_substr( trim((string) request()->query('seller_entity', '')), 0, 100 ); $filtered = InvoiceBasic::query(); if ($period !== '') { $filtered->where('period', $period); } else { $filtered->where(function ($query): void { $query->whereNull('period')->orWhere('period', ''); }); } if ($sellerEntity !== '') { $needle = '%' . addcslashes($sellerEntity, '%_\\') . '%'; $filtered->where('store_entity', 'like', $needle); } $aggregate = (clone $filtered) ->reorder() ->selectRaw( 'COUNT(*) AS record_count, COALESCE(SUM(invoice_limit), 0) AS quota_total, COALESCE(SUM(output_amount), 0) AS used_total, SUM(CASE WHEN output_amount > invoice_limit THEN 1 ELSE 0 END) AS exceeded_count, SUM(CASE WHEN invoice_limit > 0 AND output_amount > 0 AND output_amount <= invoice_limit AND output_amount >= invoice_limit * ? THEN 1 ELSE 0 END) AS warning_count', [self::WARNING_RATE] ) ->first(); $rows = (clone $filtered) ->select([ 'id', 'store_name', 'store_entity', 'invoice_issuer', 'invoice_limit', 'output_amount', 'period', ]) ->orderByRaw( 'CASE WHEN output_amount > invoice_limit THEN 0 WHEN invoice_limit > 0 AND output_amount >= invoice_limit * ? THEN 1 WHEN invoice_limit > 0 THEN 2 ELSE 3 END', [self::WARNING_RATE] ) ->orderBy('store_entity') ->orderByDesc('id') ->paginate(self::PER_PAGE) ->withQueryString(); $rows->setCollection( $rows->getCollection()->map( fn (InvoiceBasic $row): object => $this->presentRow($row) ) ); $quotaTotal = round((float) ($aggregate->quota_total ?? 0), 2); $usedTotal = round((float) ($aggregate->used_total ?? 0), 2); $exceededCount = (int) ($aggregate->exceeded_count ?? 0); $warningCount = (int) ($aggregate->warning_count ?? 0); return [ 'view' => 'business.pages.invoice_quota', 'title' => '发票额度', 'period' => $period, 'periodOptions' => $periodOptions, 'sellerEntity' => $sellerEntity, 'rows' => $rows, 'summary' => [ 'record_count' => (int) ($aggregate->record_count ?? 0), 'quota_total' => $quotaTotal, 'used_total' => $usedTotal, 'remaining_total' => round($quotaTotal - $usedTotal, 2), 'usage_rate' => $quotaTotal > 0 ? round($usedTotal / $quotaTotal * 100, 1) : null, 'exceeded_count' => $exceededCount, 'warning_count' => $warningCount, 'attention_count' => $exceededCount + $warningCount, ], 'warningRate' => self::WARNING_RATE * 100, 'loadError' => null, ]; } catch (Throwable $exception) { report($exception); return $this->errorPayload(); } } private function presentRow(InvoiceBasic $row): object { $quota = round((float) $row->invoice_limit, 2); $used = round((float) $row->output_amount, 2); $remaining = round($quota - $used, 2); $usageRate = $quota > 0 ? round($used / $quota * 100, 1) : null; if ($used > $quota) { $status = '超额'; $statusTone = 'danger'; $statusIcon = 'error'; } elseif ($quota <= 0) { $status = '未设置额度'; $statusTone = 'muted'; $statusIcon = 'remove_circle_outline'; } elseif ($usageRate !== null && $usageRate >= self::WARNING_RATE * 100) { $status = '需关注'; $statusTone = 'warning'; $statusIcon = 'warning_amber'; } else { $status = '正常'; $statusTone = 'success'; $statusIcon = 'check_circle_outline'; } return (object) [ 'id' => $row->id, 'store_name' => $row->store_name, 'store_entity' => $row->store_entity, 'invoice_issuer' => $row->invoice_issuer, 'invoice_limit' => $quota, 'output_amount' => $used, 'remaining' => $remaining, 'usage_rate' => $usageRate, 'usage_bar_width' => $usageRate === null ? 0 : min(100, max(0, (int) round($usageRate))), 'status' => $status, 'status_tone' => $statusTone, 'status_icon' => $statusIcon, 'period' => $row->period, ]; } private function normalizePeriod(string $period): ?string { return preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $period) === 1 ? $period : null; } private function errorPayload(): array { return [ 'view' => 'business.pages.invoice_quota', 'title' => '发票额度', 'period' => '', 'periodOptions' => collect(), 'sellerEntity' => '', 'rows' => new LengthAwarePaginator([], 0, self::PER_PAGE, 1, [ 'path' => request()->url(), 'query' => request()->query(), ]), 'summary' => [ 'record_count' => 0, 'quota_total' => 0.0, 'used_total' => 0.0, 'remaining_total' => 0.0, 'usage_rate' => null, 'exceeded_count' => 0, 'warning_count' => 0, 'attention_count' => 0, ], 'warningRate' => self::WARNING_RATE * 100, 'loadError' => '发票额度数据暂时无法读取,请稍后重试。', ]; } }