Преглед изворни кода

feat: refactor FranchisorDashboardService to calculate unit receivables and remove unused constructor

ebagabee пре 5 дана
родитељ
комит
8cadf61585
1 измењених фајлова са 47 додато и 8 уклоњено
  1. 47 8
      app/Services/FranchisorDashboardService.php

+ 47 - 8
app/Services/FranchisorDashboardService.php

@@ -29,10 +29,6 @@ class FranchisorDashboardService
         ReceivableStatus::CANCELLED->value,
         ReceivableStatus::CANCELLED->value,
     ];
     ];
 
 
-    public function __construct(
-        private readonly TbrCalculationService $tbrCalculationService,
-    ) {}
-
     public function overview(array $unitIds = [], ?Carbon $startDate = null, ?Carbon $endDate = null): array
     public function overview(array $unitIds = [], ?Carbon $startDate = null, ?Carbon $endDate = null): array
     {
     {
         $startDate ??= Carbon::now()->startOfMonth();
         $startDate ??= Carbon::now()->startOfMonth();
@@ -79,18 +75,61 @@ private function unitsSummary(array $unitIds, Carbon $startDate, Carbon $endDate
                 ->distinct()
                 ->distinct()
                 ->count('unit_id');
                 ->count('unit_id');
 
 
-        $revenue = $total === 0
-            ? 0.0
-            : $this->tbrCalculationService->resolveRevenueBetween($filteredUnitIds, $startDate, $endDate);
+        $receivables = $total === 0
+            ? ['received' => 0.0, 'missing' => 0.0]
+            : $this->unitReceivables($filteredUnitIds, $startDate, $endDate);
 
 
         return [
         return [
             'total'                   => $total,
             'total'                   => $total,
-            'revenue'                 => round($revenue, 2),
+            'revenue'                 => round($receivables['received'], 2),
+            'missing_receivables'     => round($receivables['missing'], 2),
             'active_contracts'        => $activeContracts,
             'active_contracts'        => $activeContracts,
             'without_active_contract' => max(0, $total - $activeContracts),
             'without_active_contract' => max(0, $total - $activeContracts),
         ];
         ];
     }
     }
 
 
+    /**
+     * Separa os recebíveis das unidades entre valores efetivamente recebidos no
+     * período e valores com vencimento no período que ainda não foram recebidos.
+     * Mantém a mesma composição usada pelo faturamento-base do TBR.
+     */
+    private function unitReceivables(array $unitIds, Carbon $startDate, Carbon $endDate): array
+    {
+        $start = $startDate->toDateString();
+        $end = $endDate->toDateString();
+
+        $sources = [
+            DB::table('student_contract_installments')
+                ->whereNull('deleted_at')
+                ->whereIn('unit_id', $unitIds),
+            DB::table('unit_account_receivables')
+                ->whereNull('deleted_at')
+                ->whereIn('unit_id', $unitIds),
+            DB::table('franchisee_account_receives')
+                ->whereNull('deleted_at')
+                ->where('origin', 'manual_unit')
+                ->whereNotNull('unit_id')
+                ->whereIn('unit_id', $unitIds),
+        ];
+
+        $received = 0.0;
+        $missing = 0.0;
+
+        foreach ($sources as $source) {
+            $received += (float) (clone $source)
+                ->where('status', ReceivableStatus::PAID->value)
+                ->whereBetween('payment_date', [$start, $end])
+                ->sum('paid_value');
+
+            $missing += (float) (clone $source)
+                ->whereNotIn('status', self::SETTLED)
+                ->whereBetween('due_date', [$start, $end])
+                ->sum('value');
+        }
+
+        return ['received' => $received, 'missing' => $missing];
+    }
+
     /**
     /**
      * Matrículas (alunos cadastrados) por mês nos últimos 6 meses.
      * Matrículas (alunos cadastrados) por mês nos últimos 6 meses.
      */
      */