diff --git a/app/Domains/Virtual/Repositories/Concerns/OrderCardConcern.php b/app/Domains/Virtual/Repositories/Concerns/OrderCardConcern.php index 6df8bba0..4bccaea7 100644 --- a/app/Domains/Virtual/Repositories/Concerns/OrderCardConcern.php +++ b/app/Domains/Virtual/Repositories/Concerns/OrderCardConcern.php @@ -168,12 +168,12 @@ trait OrderCardConcern } if (isset($conditions['sell_starttime']) && isset($conditions['sell_endtime'])) { - $query->whereRaw(sprintf("exists (SELECT sim FROM virtual_order_cards_partition WHERE type in (0, 1) AND created_at >= '%s' AND created_at <= '%s' AND deleted_at is null AND refunded_at is null)", $conditions['sell_starttime'], $conditions['sell_endtime'])); + $query->whereRaw(sprintf("exists (SELECT sim FROM virtual_order_cards_partition AS p WHERE virtual_order_cards_partition.sim = p.sim AND type in (0, 1) AND created_at >= '%s' AND created_at <= '%s' AND deleted_at is null AND refunded_at is null)", $conditions['sell_starttime'], $conditions['sell_endtime'])); } if (isset($conditions['has_type']) && !empty($conditions['has_type'])) { $conditions['has_type'] = array_wrap($conditions['has_type']); - $query->whereRaw(sprintf("exists (SELECT sim from virtual_order_cards_partition WHERE type in (%s))", implode(',', $conditions['has_type']))); + $query->whereRaw(sprintf("exists (SELECT sim from virtual_order_cards_partition AS p WHERE virtual_order_cards_partition.sim = p.sim AND type in (%s))", implode(',', $conditions['has_type']))); } }); @@ -183,15 +183,15 @@ trait OrderCardConcern switch ($conditions['card_status']) { case 0: - $hasRaw = sprintf("exists (SELECT sim FROM cards where cancelled_at IS NULL or cancelled_at < '%s')", $date); - + $hasRaw = sprintf("exists (SELECT sim FROM cards WHERE virtual_order_cards_partition.sim = cards.sim AND (cancelled_at IS NULL or cancelled_at < '%s'))", $date); + $this->model = $this->model->whereNull('service_start_at') ->whereRaw($hasRaw) ->where('created_at', '<', Carbon::now()->subMonths(6)); break; case 1: - $hasRaw = sprintf("exists (SELECT sim FROM cards where cancelled_at IS NULL or cancelled_at < '%s')", $date); + $hasRaw = sprintf("exists (SELECT sim FROM cards WHERE virtual_order_cards_partition.sim = cards.sim AND (cancelled_at IS NULL or cancelled_at < '%s'))", $date); $this->model = $this->model->whereNull('service_start_at') ->whereRaw($hasRaw) @@ -199,23 +199,23 @@ trait OrderCardConcern break; case 2: - $havingRaw = sprintf("exists (SELECT sim FROM virtual_order_cards_partition GROUP BY sim HAVING MAX(service_end_at) >= '%s')", $date); - $hasRaw = sprintf("exists (SELECT sim FROM cards where cancelled_at IS NULL or cancelled_at <= '%s')", $date); + $havingRaw = sprintf("exists (SELECT sim FROM virtual_order_cards_partition AS p WHERE virtual_order_cards_partition.sim = p.sim GROUP BY sim HAVING MAX(service_end_at) >= '%s')", $date); + $hasRaw = sprintf("exists (SELECT sim FROM cards WHERE virtual_order_cards_partition.sim = cards.sim AND (cancelled_at IS NULL or cancelled_at <= '%s'))", $date); $this->model = $this->model->whereNotNull('service_start_at') ->whereRaw($havingRaw) ->whereRaw($hasRaw); break; case 3: - $havingRaw = sprintf("exists (SELECT sim FROM virtual_order_cards_partition GROUP BY sim HAVING MAX(service_end_at) < '%s')", $date); - $hasRaw = sprintf("exists (SELECT sim FROM cards where cancelled_at IS NULL or cancelled_at <= '%s')", $date); + $havingRaw = sprintf("exists (SELECT sim FROM virtual_order_cards_partition AS p WHERE virtual_order_cards_partition.sim = p.sim GROUP BY sim HAVING MAX(service_end_at) < '%s')", $date); + $hasRaw = sprintf("exists (SELECT sim FROM cards WHERE virtual_order_cards_partition.sim = cards.sim AND (cancelled_at IS NULL or cancelled_at <= '%s'))", $date); $this->model = $this->model->whereNotNull('service_start_at') ->whereRaw($havingRaw) ->whereRaw($hasRaw); break; case 4: - $hasRaw = sprintf("exists (SELECT sim FROM cards where cancelled_at IS NOT NULL AND cancelled_at >= '%s')", $date); + $hasRaw = sprintf("exists (SELECT sim FROM cards WHERE virtual_order_cards_partition.sim = cards.sim AND (cancelled_at IS NOT NULL AND cancelled_at >= '%s'))", $date); $this->model = $this->model->whereRaw($hasRaw); break;