Procházet zdrojové kódy

feat(financial): avulsos entram no dashboard da unidade e no faturamento do TBR

- UnitFinancialDashboardService: recebíveis a receber/recebido/vencidos
  passam a somar os avulsos manuais (pagáveis já incluíam).
- TbrCalculationService::resolveRevenue: base de faturamento (royalties)
  = parcelas de aluno + recebíveis avulsos do mês de referência.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ebagabee před 3 týdny
rodič
revize
194674e99c

+ 16 - 5
app/Services/TbrCalculationService.php

@@ -9,6 +9,7 @@
 use App\Models\StudentContractInstallment;
 use App\Models\Tbr;
 use App\Models\TbrCalculation;
+use App\Models\UnitAccountReceivable;
 use App\Models\UnitFinancial;
 use Carbon\Carbon;
 use Illuminate\Pagination\LengthAwarePaginator;
@@ -42,14 +43,24 @@ public function resolveRevenue(int $unitId, int $referenceYear, int $referenceMo
             $base = $base->subMonthNoOverflow();
         }
 
-        return (float) StudentContractInstallment::whereNull('deleted_at')
+        $startDate = $base->copy()->startOfMonth()->toDateString();
+        $endDate   = $base->copy()->endOfMonth()->toDateString();
+
+        // Base de faturamento = parcelas de aluno + recebíveis avulsos (manuais)
+        // do mês de referência (decisão da franqueadora: avulsos compõem o royalty).
+        $installments = (float) StudentContractInstallment::whereNull('deleted_at')
+            ->where('unit_id', $unitId)
+            ->where('status', '!=', 'cancelled')
+            ->whereBetween('due_date', [$startDate, $endDate])
+            ->sum('value');
+
+        $manual = (float) UnitAccountReceivable::whereNull('deleted_at')
             ->where('unit_id', $unitId)
             ->where('status', '!=', 'cancelled')
-            ->whereBetween('due_date', [
-                $base->copy()->startOfMonth()->toDateString(),
-                $base->copy()->endOfMonth()->toDateString(),
-            ])
+            ->whereBetween('due_date', [$startDate, $endDate])
             ->sum('value');
+
+        return $installments + $manual;
     }
 
     /**

+ 24 - 9
app/Services/UnitFinancialDashboardService.php

@@ -4,11 +4,13 @@
 
 use App\Models\StudentContractInstallment;
 use App\Models\UnitAccountPayable;
+use App\Models\UnitAccountReceivable;
 use Carbon\Carbon;
 
 /**
- * Visão geral financeira da unidade: agrega Contas a Receber (parcelas de aluno)
- * e Contas a Pagar (unit_account_payables) do mês corrente.
+ * Visão geral financeira da unidade: agrega Contas a Receber (parcelas de aluno
+ * + recebíveis avulsos manuais) e Contas a Pagar (unit_account_payables, que já
+ * inclui os lançamentos manuais) do mês corrente.
  */
 class UnitFinancialDashboardService
 {
@@ -25,30 +27,43 @@ public function overview(int $unitId): array
         $recOverdueQ  = StudentContractInstallment::where('unit_id', $unitId)
             ->where('status', 'pending')->where('due_date', '<', $today);
 
-        // Pagáveis
+        // Recebíveis avulsos (manuais)
+        $manPendingQ  = UnitAccountReceivable::where('unit_id', $unitId)->where('status', 'pending');
+        $manReceivedQ = UnitAccountReceivable::where('unit_id', $unitId)
+            ->where('status', 'paid')->whereBetween('payment_date', [$start, $end]);
+        $manOverdueQ  = UnitAccountReceivable::where('unit_id', $unitId)
+            ->where('status', 'pending')->where('due_date', '<', $today);
+
+        // Pagáveis (unit_account_payables já inclui os manuais, origin='manual')
         $payPendingQ  = UnitAccountPayable::where('unit_id', $unitId)->where('status', 'pending');
         $payPaidQ     = UnitAccountPayable::where('unit_id', $unitId)
             ->where('status', 'paid')->whereBetween('payment_date', [$start, $end]);
         $payOverdueQ  = UnitAccountPayable::where('unit_id', $unitId)
             ->where('status', 'pending')->where('due_date', '<', $today);
 
-        $receivedMonth = (float) (clone $recReceivedQ)->sum('paid_value');
+        $receivedMonth = (float) (clone $recReceivedQ)->sum('paid_value')
+            + (float) (clone $manReceivedQ)->sum('paid_value');
         $paidMonth     = (float) (clone $payPaidQ)->sum('paid_value');
 
+        $receivablePending = (float) (clone $recPendingQ)->sum('value')
+            + (float) (clone $manPendingQ)->sum('value');
+        $overdueReceivable = (float) (clone $recOverdueQ)->sum('value')
+            + (float) (clone $manOverdueQ)->sum('value');
+
         return [
             'received_month'           => round($receivedMonth, 2),
-            'received_month_count'     => (clone $recReceivedQ)->count(),
+            'received_month_count'     => (clone $recReceivedQ)->count() + (clone $manReceivedQ)->count(),
             'paid_month'               => round($paidMonth, 2),
             'paid_month_count'         => (clone $payPaidQ)->count(),
             'result_month'             => round($receivedMonth - $paidMonth, 2),
 
-            'receivable_pending'       => round((float) (clone $recPendingQ)->sum('value'), 2),
-            'receivable_pending_count' => (clone $recPendingQ)->count(),
+            'receivable_pending'       => round($receivablePending, 2),
+            'receivable_pending_count' => (clone $recPendingQ)->count() + (clone $manPendingQ)->count(),
             'payable_pending'          => round((float) (clone $payPendingQ)->sum('value'), 2),
             'payable_pending_count'    => (clone $payPendingQ)->count(),
 
-            'overdue_receivable'       => round((float) (clone $recOverdueQ)->sum('value'), 2),
-            'overdue_receivable_count' => (clone $recOverdueQ)->count(),
+            'overdue_receivable'       => round($overdueReceivable, 2),
+            'overdue_receivable_count' => (clone $recOverdueQ)->count() + (clone $manOverdueQ)->count(),
             'overdue_payable'          => round((float) (clone $payOverdueQ)->sum('value'), 2),
             'overdue_payable_count'    => (clone $payOverdueQ)->count(),
         ];