|
|
@@ -29,8 +29,15 @@ class FranchisorDashboardService
|
|
|
ReceivableStatus::CANCELLED->value,
|
|
|
];
|
|
|
|
|
|
- public function overview(array $unitIds = []): array
|
|
|
+ public function __construct(
|
|
|
+ private readonly TbrCalculationService $tbrCalculationService,
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ public function overview(array $unitIds = [], ?Carbon $startDate = null, ?Carbon $endDate = null): array
|
|
|
{
|
|
|
+ $startDate ??= Carbon::now()->startOfMonth();
|
|
|
+ $endDate ??= Carbon::now()->endOfMonth();
|
|
|
+
|
|
|
return [
|
|
|
'enrollments' => $this->enrollments($unitIds),
|
|
|
'birthdays' => $this->birthdays($unitIds),
|
|
|
@@ -38,6 +45,46 @@ public function overview(array $unitIds = []): array
|
|
|
'pending_tasks' => $this->pendingTasks($unitIds),
|
|
|
'general_revenue' => $this->generalRevenue($unitIds),
|
|
|
'product_stock' => $this->productStock(),
|
|
|
+ 'units_summary' => $this->unitsSummary($unitIds, $startDate, $endDate),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Receita usa a mesma base do cálculo do TBR. O status é derivado da
|
|
|
+ * existência de contrato de franquia vigente na data atual.
|
|
|
+ */
|
|
|
+ private function unitsSummary(array $unitIds, Carbon $startDate, Carbon $endDate): array
|
|
|
+ {
|
|
|
+ $units = DB::table('units')
|
|
|
+ ->whereNull('deleted_at')
|
|
|
+ ->when(!empty($unitIds), fn ($q) => $q->whereIn('id', $unitIds));
|
|
|
+
|
|
|
+ $filteredUnitIds = (clone $units)->pluck('id')->map(fn ($id) => (int) $id)->all();
|
|
|
+ $total = count($filteredUnitIds);
|
|
|
+ $today = Carbon::today()->toDateString();
|
|
|
+
|
|
|
+ $activeContracts = $total === 0
|
|
|
+ ? 0
|
|
|
+ : (int) DB::table('franchisee_contracts')
|
|
|
+ ->whereNull('deleted_at')
|
|
|
+ ->whereIn('unit_id', $filteredUnitIds)
|
|
|
+ ->whereNotNull('start_date')
|
|
|
+ ->where('start_date', '<=', $today)
|
|
|
+ ->where(function ($q) use ($today) {
|
|
|
+ $q->whereNull('end_date')->orWhere('end_date', '>=', $today);
|
|
|
+ })
|
|
|
+ ->distinct()
|
|
|
+ ->count('unit_id');
|
|
|
+
|
|
|
+ $revenue = $total === 0
|
|
|
+ ? 0.0
|
|
|
+ : $this->tbrCalculationService->resolveRevenueBetween($filteredUnitIds, $startDate, $endDate);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'total' => $total,
|
|
|
+ 'revenue' => round($revenue, 2),
|
|
|
+ 'active_contracts' => $activeContracts,
|
|
|
+ 'without_active_contract' => max(0, $total - $activeContracts),
|
|
|
];
|
|
|
}
|
|
|
|