| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentCart\App\Services\Report; |
| 4 | 4 | |
| 5 | 5 | use FluentCart\App\App; |
| 6 | 6 | use FluentCart\App\Helpers\Status; |
| 7 | +use FluentCart\Framework\Support\Arr; | |
| 7 | 8 | use FluentCart\App\Services\Report\Concerns\Subscription\FutureRenewals; |
| 8 | 9 | |
| 9 | 10 | class SubscriptionReportService extends ReportService |
| 10 | 11 | { |
| @@ -11,21 +12,26 @@ | ||
| 11 | 12 | use FutureRenewals; |
| 12 | 13 | |
| 13 | 14 | public function getRetentionChart($params = []) |
| 14 | 15 | { |
| 16 | + $customDays = max(0, (int) Arr::get($params, 'customDays', 0)); | |
| 17 | + $variationIds = Arr::get($params, 'variationIds', []); | |
| 18 | + $startDate = Arr::get($params, 'startDate'); | |
| 19 | + $endDate = Arr::get($params, 'endDate'); | |
| 20 | + | |
| 15 | 21 | $baseQuery = App::db()->query() |
| 16 | 22 | ->from('fct_subscriptions as s') |
| 17 | - ->whereBetween('s.created_at', [$params['startDate'], $params['endDate']]) | |
| 18 | - ->when($params['variationIds'], fn ($q) => $q->whereIn('s.variation_id', $params['variationIds'])); | |
| 23 | + ->whereBetween('s.created_at', [$startDate, $endDate]) | |
| 24 | + ->when($variationIds, fn ($q) => $q->whereIn('s.variation_id', $variationIds)); | |
| 19 | 25 | |
| 20 | - if ($params['customDays']) { | |
| 21 | - $query = $baseQuery->selectRaw("COUNT(*) AS day_{$params['customDays']}") | |
| 26 | + if ($customDays) { | |
| 27 | + $query = $baseQuery->selectRaw("COUNT(*) AS day_{$customDays}") | |
| 22 | 28 | ->whereRaw(' |
| 23 | 29 | DATEDIFF( |
| 24 | 30 | COALESCE(s.canceled_at, NOW()), |
| 25 | 31 | s.created_at |
| 26 | 32 | ) <= ? |
| 27 | - ', $params['customDays']); | |
| 33 | + ', [$customDays]); | |
| 28 | 34 | } else { |
| 29 | 35 | $baseQuery->selectRaw('DATEDIFF(COALESCE(s.canceled_at, NOW()), s.created_at) AS lifespan'); |
| 30 | 36 | |
| 31 | 37 | $query = App::db()->query()->selectRaw(' |