diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index b7e0073a..ab22e03b 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -2811,6 +2811,75 @@ class Pages extends Controller ]; } + /** + * 商品成本合计明细:与页面成本合计使用同一有效单价口径, + * 按发货仓库透视商品数量与商品成本。 + */ + public function productCostSummaryDetails(Request $request): \Illuminate\Http\JsonResponse + { + $month = trim((string) $request->query('month', '')); + if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { + return response()->json(['message' => '请选择有效月份。'], 422); + } + + $filterValues = [ + 'platform' => trim((string) $request->query('platform', '')), + 'store_name' => trim((string) $request->query('store_name', '')), + 'online_order_no' => trim((string) $request->query('online_order_no', '')), + 'product_code' => trim((string) $request->query('product_code', '')), + 'warehouse_short_name' => trim((string) $request->query('warehouse_short_name', '')), + ]; + + $connection = (string) config('upstream_mirrors.connection', 'business_mirror'); + $query = DB::connection($connection) + ->table(self::PRODUCT_COST_TABLE) + ->where('report_month', $month . '-01') + ->whereNotNull('unit_price'); + + if ($filterValues['platform'] !== '') { + $query->where('platform', $filterValues['platform']); + } + foreach (['store_name', 'online_order_no', 'product_code'] as $column) { + if ($filterValues[$column] !== '') { + $query->where( + $column, + 'like', + '%' . addcslashes($filterValues[$column], '%_\\') . '%' + ); + } + } + if ($filterValues['warehouse_short_name'] !== '') { + $query->where('warehouse_short_name', $filterValues['warehouse_short_name']); + } + + $warehouseExpression = "COALESCE(NULLIF(warehouse, ''), '未填写发货仓库')"; + $rows = $query + ->selectRaw($warehouseExpression . ' as warehouse') + ->selectRaw('SUM(product_quantity) as product_quantity') + ->selectRaw('SUM(product_cost) as product_cost') + ->groupByRaw($warehouseExpression) + ->orderByDesc('product_cost') + ->orderBy('warehouse') + ->get() + ->map(static fn ($row): array => [ + 'warehouse' => (string) ($row->warehouse ?? '未填写发货仓库'), + 'quantity' => round((float) ($row->product_quantity ?? 0), 4), + 'cost' => round((float) ($row->product_cost ?? 0), 2), + ]) + ->values(); + + return response()->json([ + 'summary' => [ + 'warehouse_count' => $rows->count(), + 'quantity_total' => round((float) $rows->sum('quantity'), 4), + 'cost_total' => round((float) $rows->sum('cost'), 2), + ], + 'rows' => $rows, + ], 200, [ + 'Cache-Control' => 'private, no-store, max-age=0', + ]); + } + /** * 商品成本待核验明细:当前单价为空或仓库简称未匹配的行。 * 每次打开或刷新弹框都重新查询共享镜像,已修复的行会自然从结果中消失。 diff --git a/resources/views/business/pages/financial-operations/product-costs.blade.php b/resources/views/business/pages/financial-operations/product-costs.blade.php index ea6e30c2..fffc8604 100644 --- a/resources/views/business/pages/financial-operations/product-costs.blade.php +++ b/resources/views/business/pages/financial-operations/product-costs.blade.php @@ -22,13 +22,18 @@ : $formatted; }; $metricCards = [ - ['label' => '商品成本合计', 'value' => number_format((float) ($summary['cost_total'] ?? 0) / 10000, 2), 'unit' => '万'], + [ + 'label' => '商品成本合计', + 'value' => number_format((float) ($summary['cost_total'] ?? 0) / 10000, 2), + 'unit' => '万', + 'dialog' => 'cost_summary', + ], [ 'label' => '待核验成本', 'value' => number_format((int) ($summary['pending_rows'] ?? 0)), 'unit' => '行', 'tone' => 'warn', - 'dialog' => true, + 'dialog' => 'pending', ], ]; $tableColumns = [ @@ -49,6 +54,23 @@ ); $pendingDetailsUrl = route('business.financial-operations.product-costs.pending-details', $pendingQuery); $pendingExportUrl = route('business.financial-operations.product-costs.pending-details.export', $pendingQuery); + $costSummaryQuery = array_merge( + ['company_id' => company_id()], + request()->except([ + 'page', + 'per_page', + 'pending_page', + 'pending_type', + 'warehouse_keyword', + 'sort_by', + 'sort_direction', + ]), + ['month' => $filters['month'] ?? ''] + ); + $costSummaryDetailsUrl = route( + 'business.financial-operations.product-costs.summary-details', + $costSummaryQuery + ); @endphp