diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index f3cf6b39..be512d79 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -1057,6 +1057,30 @@ class Pages extends Controller return response()->json(['data' => $payload['data'], 'has_more' => $payload['has_more'] ?? false]); } + /** 安装明细页:按客服审核时间汇总安装公司的结算、收款明细金额。 */ + public function installationDetailsSummary(): \Illuminate\Http\JsonResponse + { + $section = collect(SidebarPermissions::navigationFor(user()))->firstWhere('slug', 'financial-operations'); + abort_unless($section && collect($section['pages'] ?? [])->contains('slug', 'installation-details'), 403, '当前账号没有该页面权限。'); + + $filters = InstallationDetailMirrorRepository::requestFilters(); + if (empty($filters['customer_review_from']) || empty($filters['customer_review_to'])) { + return response()->json(['message' => '请选择完整的客服审核起止日期'], 422); + } + $payload = $this->installationDetails->companySummary($filters); + if (! $payload['ready']) { + return response()->json(['message' => '安装明细镜像尚未完成首次同步'], 503); + } + + return response()->json([ + 'data' => $payload['data'], + 'totals' => [ + 'settled_amount' => $payload['total_settled'], + 'received_amount' => $payload['total_received'], + ], + ], 200, ['Cache-Control' => 'private, no-store']); + } + /** * 账单明细导出:按页面同一套筛选条件流式导出 CSV(全部筛选结果)。 */ diff --git a/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php b/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php index 8554558e..d952a91e 100644 --- a/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php @@ -100,6 +100,91 @@ class InstallationDetailMirrorRepository return ['rows' => (int) $row->row_count, 'requested' => (float) $row->requested, 'settled' => (float) $row->settled, 'paid' => (float) $row->paid]; } + /** + * 按客服审核时间直接从安装明细子表汇总,并用 SourceGuid 取得最新主表的安装公司。 + * 不能先按主表 kfshTime 过滤,否则主表日期更新滞后时会漏掉已同步的明细。 + * + * @return array{ready:bool,data:list,total_settled:float,total_received:float} + */ + public function companySummary(array $filters = []): array + { + $dataset = $this->datasets->get(self::DATASET); + $items = $this->datasets->get(self::ITEM_DATASET); + $connection = $this->mirror->connection(); + if (! $this->ready($dataset, $items, $connection->getName())) { + return ['ready' => false, 'data' => [], 'total_settled' => 0.0, 'total_received' => 0.0]; + } + + $from = trim((string) ($filters['customer_review_from'] ?? '')); + $to = trim((string) ($filters['customer_review_to'] ?? '')); + if ($from === '' && $to === '') { + return ['ready' => true, 'data' => [], 'total_settled' => 0.0, 'total_received' => 0.0]; + } + + $query = $connection->table($items->targetTable() . ' AS i') + ->whereNotNull('i.kfshTime'); + + if ($from !== '') { + $query->where('i.kfshTime', '>=', $from); + } + if ($to !== '') { + $query->where('i.kfshTime', '<', \Illuminate\Support\Carbon::parse($to)->addDay()->toDateString()); + } + + $itemTotals = $query + ->selectRaw('i.guid AS guid') + ->selectRaw('COALESCE(SUM(CAST(i.settleItemAmount AS DECIMAL(18,2))), 0) AS settled_amount') + ->selectRaw('COALESCE(SUM(CAST(i.acceptItemAmount AS DECIMAL(18,2))), 0) AS received_amount') + ->groupBy('i.guid') + ->get(); + + $companyByGuid = []; + $guids = $itemTotals->pluck('guid') + ->filter(static fn (mixed $guid): bool => trim((string) $guid) !== '') + ->unique() + ->values() + ->all(); + foreach (array_chunk($guids, 1000) as $guidChunk) { + $connection->table($dataset->targetTable()) + ->whereIn('SourceGuid', $guidChunk) + ->select(['Id', 'SourceGuid', 'installCompanyName']) + ->orderBy('Id') + ->get() + ->each(static function (object $row) use (&$companyByGuid): void { + $companyByGuid[(string) $row->SourceGuid] = trim((string) ($row->installCompanyName ?? '')); + }); + } + + $companyTotals = []; + foreach ($itemTotals as $item) { + $company = $companyByGuid[(string) $item->guid] ?? ''; + $company = $company !== '' ? $company : '未填写安装公司'; + $companyTotals[$company] ??= ['settled_amount' => 0.0, 'received_amount' => 0.0]; + $companyTotals[$company]['settled_amount'] += (float) $item->settled_amount; + $companyTotals[$company]['received_amount'] += (float) $item->received_amount; + } + + $rows = []; + foreach ($companyTotals as $company => $amounts) { + $rows[] = [ + 'installation_company' => $company, + 'settled_amount' => round($amounts['settled_amount'], 2), + 'received_amount' => round($amounts['received_amount'], 2), + ]; + } + usort($rows, static fn (array $left, array $right): int => + [$right['settled_amount'], $left['installation_company']] + <=> [$left['settled_amount'], $right['installation_company']] + ); + + return [ + 'ready' => true, + 'data' => $rows, + 'total_settled' => round(array_sum(array_column($rows, 'settled_amount')), 2), + 'total_received' => round(array_sum(array_column($rows, 'received_amount')), 2), + ]; + } + public static function requestFilters(): array { $rules = []; diff --git a/resources/views/business/pages/partials/date-range-picker.blade.php b/resources/views/business/pages/partials/date-range-picker.blade.php index a8712f85..0de248c4 100644 --- a/resources/views/business/pages/partials/date-range-picker.blade.php +++ b/resources/views/business/pages/partials/date-range-picker.blade.php @@ -280,6 +280,7 @@ input.value = day.getAttribute('data-calendar-day'); selected = parseIsoDate(input.value); syncClear(); + input.dispatchEvent(new Event('change', { bubbles: true })); close(); } }); @@ -302,6 +303,7 @@ input.value = ''; selected = null; syncClear(); + input.dispatchEvent(new Event('change', { bubbles: true })); close(); }); } diff --git a/resources/views/business/pages/show.blade.php b/resources/views/business/pages/show.blade.php index 166488e5..94098646 100644 --- a/resources/views/business/pages/show.blade.php +++ b/resources/views/business/pages/show.blade.php @@ -274,6 +274,74 @@ display: none; } + .installation-summary { + grid-column: span 3; + min-width: 0; + align-self: stretch; + overflow: hidden; + border: 1px solid #d8d2c7; + border-radius: 0.5rem; + background: #ffffff; + } + + .installation-summary__table { + width: 100%; + border-collapse: collapse; + table-layout: fixed; + font-size: 0.6875rem; + font-variant-numeric: tabular-nums; + } + + .installation-summary__table th, + .installation-summary__table td { + padding: 0.32rem 0.55rem; + border-bottom: 1px solid #eee8dd; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + } + + .installation-summary__table thead th { + background: #f7f0de; + color: #595959; + font-weight: 600; + text-align: left; + } + + .installation-summary__table th:not(:first-child), + .installation-summary__table td:not(:first-child) { + text-align: right; + } + + .installation-summary__table tbody td { + color: #171717; + } + + .installation-summary__table tfoot th, + .installation-summary__table tfoot td { + background: #fcfbf8; + color: #171717; + font-weight: 700; + } + + .installation-summary__table tfoot td { + text-align: right; + } + + .installation-summary__table tbody tr:last-child td, + .installation-summary__table tfoot tr:last-child th { + border-bottom: 0; + } + + .installation-summary__empty { + color: #7a746b; + text-align: center !important; + } + + .installation-summary__table[aria-busy="true"] tbody { + opacity: 0.56; + } + .installation-filter-actions { display: flex; align-items: center; @@ -283,6 +351,7 @@ @media (max-width: 1100px) { .installation-filter-bar { grid-template-columns: repeat(2, minmax(14rem, 1fr)); } .installation-date-filter { grid-column: 1 / -1; } + .installation-summary { grid-column: 1 / -1; } } @media (max-width: 650px) { .installation-filter-bar { grid-template-columns: minmax(0, 1fr); padding: 1rem; } @@ -291,6 +360,7 @@ align-items: stretch; gap: 0.25rem; } + .installation-summary { grid-column: 1 / -1; } } .biz-card-head { @@ -834,6 +904,33 @@ 清除 导出 +
+ + + + + + + + + + + + + + + + + + + +
安装公司结算明细金额收款明细金额
选择客服审核时间后自动汇总
+