← All changes
|
app/Services/Report/SubscriptionReportService.php
+16
-10
1.3.20
→
1.6.5
View file →
| @@ -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(' |
| @@ -270,13 +276,13 @@ | ||
| 270 | 276 | }) |
| 271 | 277 | ->first(); |
| 272 | 278 | |
| 273 | 279 | $newSubscriptions = (int) $stats->new_subscriptions; |
| 274 | - $newSubscriptionsMrr = round($stats->new_subscriptions_mrr, 2); | |
| 280 | + $newSubscriptionsMrr = round((float) $stats->new_subscriptions_mrr, 2); | |
| 275 | 281 | $activeSubscriptions = (int) $stats->active_subscriptions; |
| 276 | 282 | $startSubscriptions = (int) $stats->start_subscriptions; |
| 277 | - $mrr = round($stats->mrr, 2); | |
| 278 | - $startMrr = round($stats->start_mrr, 2); | |
| 283 | + $mrr = round((float) $stats->mrr, 2); | |
| 284 | + $startMrr = round((float) $stats->start_mrr, 2); | |
| 279 | 285 | |
| 280 | 286 | // Retention Rate = ((End Count - New Count) / Start Count) * 100 |
| 281 | 287 | $retentionRate = 0; |
| 282 | 288 | if ($startSubscriptions > 0) { |
| @@ -300,9 +306,9 @@ | ||
| 300 | 306 | 'year' => $dt->format('Y'), |
| 301 | 307 | 'new_subscriptions' => $newSubscriptions, |
| 302 | 308 | 'new_subscriptions_mrr' => $newSubscriptionsMrr, |
| 303 | 309 | 'churned_subscriptions' => (int) $stats->churned_subscriptions, |
| 304 | - 'churned_subscriptions_mrr' => round($stats->churned_subscriptions_mrr, 2), | |
| 310 | + 'churned_subscriptions_mrr' => round((float) $stats->churned_subscriptions_mrr, 2), | |
| 305 | 311 | 'active_subscriptions' => (string) $activeSubscriptions, |
| 306 | 312 | 'active_paid_subscriptions' => (string) $stats->active_paid_subscriptions, |
| 307 | 313 | 'active_free_subscriptions' => (string) $stats->active_free_subscriptions, |
| 308 | 314 | 'mrr' => number_format($mrr, 2, '.', ''), |
| @@ -307,9 +313,9 @@ | ||
| 307 | 313 | 'active_free_subscriptions' => (string) $stats->active_free_subscriptions, |
| 308 | 314 | 'mrr' => number_format($mrr, 2, '.', ''), |
| 309 | 315 | 'retention_rate' => round($retentionRate, 2), |
| 310 | 316 | 'retention_rate_money' => round($retentionRateMoney, 2), |
| 311 | - 'period_gross' => round($stats->period_gross, 2), | |
| 317 | + 'period_gross' => round((float) $stats->period_gross, 2), | |
| 312 | 318 | 'period_subscriptions' => $newSubscriptions, |
| 313 | 319 | ]; |
| 314 | 320 | } |
| 315 | 321 | |