| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers\Reports; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\Framework\Http\Request\Request; |
| 7 |
use FluentCart\App\Http\Controllers\Controller; |
| 8 |
use FluentCart\App\Services\Report\ReportHelper; |
| 9 |
use FluentCart\App\Services\Report\SubscriptionReportService; |
| 10 |
|
| 11 |
class SubscriptionReportController extends Controller |
| 12 |
{ |
| 13 |
protected $params = [ |
| 14 |
'paymentStatus', |
| 15 |
'subscriptionType', |
| 16 |
]; |
| 17 |
|
| 18 |
public function getRetentionChart(Request $request) |
| 19 |
{ |
| 20 |
$params = ReportHelper::processParams($request->get('params'), ['customDays']); |
| 21 |
|
| 22 |
$service = SubscriptionReportService::make(); |
| 23 |
|
| 24 |
$retentionStats = $service->getRetentionChart($params); |
| 25 |
|
| 26 |
return [ |
| 27 |
'chartData' => $retentionStats, |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
public function getDailySignups(Request $request) |
| 32 |
{ |
| 33 |
$params = ReportHelper::processParams($request->get('params'), $this->params); |
| 34 |
|
| 35 |
$service = SubscriptionReportService::make(); |
| 36 |
|
| 37 |
$dailySignups = $service->getDailySignups($params); |
| 38 |
|
| 39 |
return [ |
| 40 |
'signups' => $dailySignups, |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
public function getSubscriptionChart(Request $request) |
| 45 |
{ |
| 46 |
$params = ReportHelper::processParams($request->get('params'), $this->params); |
| 47 |
|
| 48 |
$service = SubscriptionReportService::make(); |
| 49 |
|
| 50 |
$currentMetrics = $service->getChartData($params); |
| 51 |
|
| 52 |
$summary = [ |
| 53 |
'future_installments' => $service->getFutureInstallments($params), |
| 54 |
'total_subscriptions' => $currentMetrics['totalSubscriptions'] |
| 55 |
]; |
| 56 |
|
| 57 |
$compareMetrics = []; |
| 58 |
if ($params['comparePeriod']) { |
| 59 |
$params['startDate'] = $params['comparePeriod'][0]; |
| 60 |
$params['endDate'] = $params['comparePeriod'][1]; |
| 61 |
|
| 62 |
$compareMetrics = $service->getChartData($params); |
| 63 |
} |
| 64 |
|
| 65 |
return [ |
| 66 |
'currentMetrics' => $currentMetrics['grouped'], |
| 67 |
'compareMetrics' => $compareMetrics['grouped'], |
| 68 |
'summary' => $summary, |
| 69 |
'fluctuations' => [] |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
public function getFutureRenewals(Request $request) |
| 74 |
{ |
| 75 |
$params = ReportHelper::processParams($request->get('params'), ['startDate', 'endDate']); |
| 76 |
|
| 77 |
$service = SubscriptionReportService::make(); |
| 78 |
|
| 79 |
return $service->getFutureRenewals($params); |
| 80 |
} |
| 81 |
|
| 82 |
public function getRetentionData(Request $request) |
| 83 |
{ |
| 84 |
$params = ReportHelper::processParams($request->get('params')); |
| 85 |
|
| 86 |
$service = SubscriptionReportService::make(); |
| 87 |
|
| 88 |
return [ |
| 89 |
'retention_data' => $service->getRetentionData($params) |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
public function getCohortData(Request $request) |
| 94 |
{ |
| 95 |
$requestParams = $request->get('params', []); |
| 96 |
$params = ReportHelper::processParams($requestParams); |
| 97 |
|
| 98 |
// Supported groupBy: 'month' or 'year' (week not supported with current snapshots) |
| 99 |
$groupBy = $requestParams['groupBy'] ?? 'year'; |
| 100 |
if (!in_array($groupBy, ['month', 'year'])) { |
| 101 |
$groupBy = 'year'; |
| 102 |
} |
| 103 |
|
| 104 |
$params['groupBy'] = $groupBy; |
| 105 |
$params['metric'] = $requestParams['metric'] ?? 'subscribers'; |
| 106 |
|
| 107 |
// Convert variation_ids to product_ids for snapshot filtering |
| 108 |
// The snapshot table stores product_id, not variation_id |
| 109 |
$productIds = []; |
| 110 |
if (!empty($params['variationIds'])) { |
| 111 |
$productIds = App::db()->table('fct_product_variations') |
| 112 |
->whereIn('id', $params['variationIds']) |
| 113 |
->distinct() |
| 114 |
->pluck('post_id') |
| 115 |
->toArray(); |
| 116 |
} |
| 117 |
$params['productIds'] = $productIds; |
| 118 |
|
| 119 |
// Calculate maxPeriods based on groupBy |
| 120 |
if ($groupBy === 'year') { |
| 121 |
// For yearly view: default 8 years, or date range + 1 (whichever is greater) |
| 122 |
$defaultYears = 8; |
| 123 |
if (isset($params['startDate']) && isset($params['endDate'])) { |
| 124 |
$yearDiff = $params['endDate']->diff($params['startDate'])->y + 1; |
| 125 |
$params['maxPeriods'] = max($defaultYears, $yearDiff); |
| 126 |
} else { |
| 127 |
$params['maxPeriods'] = $defaultYears; |
| 128 |
} |
| 129 |
} else { |
| 130 |
// For monthly view: 18 months to capture yearly subscription renewals |
| 131 |
$params['maxPeriods'] = 18; |
| 132 |
} |
| 133 |
|
| 134 |
$service = SubscriptionReportService::make(); |
| 135 |
|
| 136 |
return $service->getCohortData($params); |
| 137 |
} |
| 138 |
} |
| 139 |
|