UnitFinancialDashboardService.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Services;
  3. use App\Models\StudentContractInstallment;
  4. use App\Models\UnitAccountPayable;
  5. use App\Models\UnitAccountReceivable;
  6. use Carbon\Carbon;
  7. /**
  8. * Visão geral financeira da unidade: agrega Contas a Receber (parcelas de aluno
  9. * + recebíveis avulsos manuais) e Contas a Pagar (unit_account_payables, que já
  10. * inclui os lançamentos manuais) do mês corrente.
  11. */
  12. class UnitFinancialDashboardService
  13. {
  14. public function overview(int $unitId): array
  15. {
  16. $start = Carbon::now()->startOfMonth()->toDateString();
  17. $end = Carbon::now()->endOfMonth()->toDateString();
  18. $today = Carbon::today()->toDateString();
  19. // Recebíveis = parcelas de aluno
  20. $recPendingQ = StudentContractInstallment::where('unit_id', $unitId)->where('status', 'pending');
  21. $recReceivedQ = StudentContractInstallment::where('unit_id', $unitId)
  22. ->where('status', 'paid')->whereBetween('payment_date', [$start, $end]);
  23. $recOverdueQ = StudentContractInstallment::where('unit_id', $unitId)
  24. ->where('status', 'pending')->where('due_date', '<', $today);
  25. // Recebíveis avulsos (manuais)
  26. $manPendingQ = UnitAccountReceivable::where('unit_id', $unitId)->where('status', 'pending');
  27. $manReceivedQ = UnitAccountReceivable::where('unit_id', $unitId)
  28. ->where('status', 'paid')->whereBetween('payment_date', [$start, $end]);
  29. $manOverdueQ = UnitAccountReceivable::where('unit_id', $unitId)
  30. ->where('status', 'pending')->where('due_date', '<', $today);
  31. // Pagáveis (unit_account_payables já inclui os manuais, origin='manual')
  32. $payPendingQ = UnitAccountPayable::where('unit_id', $unitId)->where('status', 'pending');
  33. $payPaidQ = UnitAccountPayable::where('unit_id', $unitId)
  34. ->where('status', 'paid')->whereBetween('payment_date', [$start, $end]);
  35. $payOverdueQ = UnitAccountPayable::where('unit_id', $unitId)
  36. ->where('status', 'pending')->where('due_date', '<', $today);
  37. $receivedMonth = (float) (clone $recReceivedQ)->sum('paid_value')
  38. + (float) (clone $manReceivedQ)->sum('paid_value');
  39. $paidMonth = (float) (clone $payPaidQ)->sum('paid_value');
  40. $receivablePending = (float) (clone $recPendingQ)->sum('value')
  41. + (float) (clone $manPendingQ)->sum('value');
  42. $overdueReceivable = (float) (clone $recOverdueQ)->sum('value')
  43. + (float) (clone $manOverdueQ)->sum('value');
  44. return [
  45. 'received_month' => round($receivedMonth, 2),
  46. 'received_month_count' => (clone $recReceivedQ)->count() + (clone $manReceivedQ)->count(),
  47. 'paid_month' => round($paidMonth, 2),
  48. 'paid_month_count' => (clone $payPaidQ)->count(),
  49. 'result_month' => round($receivedMonth - $paidMonth, 2),
  50. 'receivable_pending' => round($receivablePending, 2),
  51. 'receivable_pending_count' => (clone $recPendingQ)->count() + (clone $manPendingQ)->count(),
  52. 'payable_pending' => round((float) (clone $payPendingQ)->sum('value'), 2),
  53. 'payable_pending_count' => (clone $payPendingQ)->count(),
  54. 'overdue_receivable' => round($overdueReceivable, 2),
  55. 'overdue_receivable_count' => (clone $recOverdueQ)->count() + (clone $manOverdueQ)->count(),
  56. 'overdue_payable' => round((float) (clone $payOverdueQ)->sum('value'), 2),
  57. 'overdue_payable_count' => (clone $payOverdueQ)->count(),
  58. ];
  59. }
  60. }