diff --git a/app/Domains/Virtual/Services/CardService.php b/app/Domains/Virtual/Services/CardService.php index 30f59491..c645b824 100644 --- a/app/Domains/Virtual/Services/CardService.php +++ b/app/Domains/Virtual/Services/CardService.php @@ -63,43 +63,46 @@ class CardService extends Service $limit = $conditions['limit'] ?? 20; $conditions['type'] = [0, 1, 2]; - $query = $this->orderCardPartitionRepository->selectRaw('DISTINCT(sim) sim, created_at')->withConditions($conditions)->applyConditions(); + $query = $this->orderCardPartitionRepository->selectRaw('sim')->withConditions($conditions)->applyConditions(); switch ($conditions['card_status']) { + case 0: - $res = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { + $query = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->where('created_at', '<', Carbon::now()->subMonths(6))->paginate($limit); + })->where('created_at', '<', Carbon::now()->subMonths(6)); break; case 1: - $res = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { + $query = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->where('created_at', '>=', Carbon::now()->subMonths(6))->paginate($limit); + })->where('created_at', '>=', Carbon::now()->subMonths(6)); break; case 2: - $res = $query->whereNotNull('service_start_at') - ->where('service_start_at', '<=', date('Y-m-d H:i:s')) - ->where('service_end_at', '>=', date('Y-m-d H:i:s')) + $havingRaw = sprintf("MAX(service_end_at) >= '%s'", date('Y-m-d H:i:s')); + + $query = $query->whereNotNull('service_start_at') + ->groupBy('sim')->havingRaw($havingRaw) ->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->paginate($limit); + }); break; case 3: - $res = $query->whereNotNull('service_start_at') - ->where('service_start_at', '<=', date('Y-m-d H:i:s')) - ->where('service_end_at', '>=', date('Y-m-d H:i:s')) + $havingRaw = sprintf("MAX(service_end_at) < '%s'", date('Y-m-d H:i:s')); + + $query = $query->whereNotNull('service_start_at') + ->groupBy('sim')->havingRaw($havingRaw) ->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->orderBy('created_at', 'desc')->paginate($limit); + }); break; case 4: - $res = $query->whereNotNull('service_start_at')->whereHas('card', function ($relation) { + $query = $query->whereHas('card', function ($relation) { $relation->whereNotNull('cancelled_at'); - })->paginate($limit); + }); break; default: @@ -107,7 +110,7 @@ class CardService extends Service break; } - return $res; + return $query->paginate($limit); } /**