diff --git a/app/Services/Stats/SalesStatService.php b/app/Services/Stats/SalesStatService.php index e3654a11..53743f5b 100644 --- a/app/Services/Stats/SalesStatService.php +++ b/app/Services/Stats/SalesStatService.php @@ -61,16 +61,19 @@ public function aggregateDaily(int $tenantId, Carbon $date): int ->distinct('client_id') ->count('client_id'); - // 출하 집계 (shipments) + // 출하 집계 (shipments + vehicle_dispatches 운송비) $shipmentStats = DB::connection('mysql') - ->table('shipments') - ->where('tenant_id', $tenantId) - ->where('scheduled_date', $dateStr) - ->where('status', 'completed') - ->whereNull('deleted_at') + ->table('shipments as s') + ->leftJoin('shipment_vehicle_dispatches as d', function ($join) { + $join->on('d.shipment_id', '=', 's.id')->whereNull('d.deleted_at'); + }) + ->where('s.tenant_id', $tenantId) + ->where('s.scheduled_date', $dateStr) + ->where('s.status', 'completed') + ->whereNull('s.deleted_at') ->selectRaw(' - COUNT(*) as shipment_count, - COALESCE(SUM(shipping_cost), 0) as shipment_amount + COUNT(DISTINCT s.id) as shipment_count, + COALESCE(SUM(CAST(JSON_UNQUOTE(JSON_EXTRACT(d.options, "$.total_amount")) AS DECIMAL(12,0))), 0) as shipment_amount ') ->first();