startOfMonth()->toDateString(); $end = Carbon::now()->endOfMonth()->toDateString(); $today = Carbon::today()->toDateString(); // Recebíveis = parcelas de aluno $recPendingQ = StudentContractInstallment::where('unit_id', $unitId)->where('status', 'pending'); $recReceivedQ = StudentContractInstallment::where('unit_id', $unitId) ->where('status', 'paid')->whereBetween('payment_date', [$start, $end]); $recOverdueQ = StudentContractInstallment::where('unit_id', $unitId) ->where('status', 'pending')->where('due_date', '<', $today); // 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); // Contas que a franqueadora vinculou à unidade (origin=manual_unit) — mesma // fonte que entra no faturamento do cálculo de TBR (TbrCalculationService). // Conta independente de ter ou não cobrança Asaas gerada: o cliente reclama // se faltar aqui, mesmo sem integração. $linkedPendingQ = FranchiseeAccountReceive::where('unit_id', $unitId) ->where('origin', 'manual_unit')->where('status', 'pending'); $linkedReceivedQ = FranchiseeAccountReceive::where('unit_id', $unitId) ->where('origin', 'manual_unit') ->where('status', 'paid')->whereBetween('payment_date', [$start, $end]); $linkedOverdueQ = FranchiseeAccountReceive::where('unit_id', $unitId) ->where('origin', 'manual_unit') ->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') + (float) (clone $manReceivedQ)->sum('paid_value') + (float) (clone $linkedReceivedQ)->sum('paid_value'); $paidMonth = (float) (clone $payPaidQ)->sum('paid_value'); $receivablePending = (float) (clone $recPendingQ)->sum('value') + (float) (clone $manPendingQ)->sum('value') + (float) (clone $linkedPendingQ)->sum('value'); $overdueReceivable = (float) (clone $recOverdueQ)->sum('value') + (float) (clone $manOverdueQ)->sum('value') + (float) (clone $linkedOverdueQ)->sum('value'); return [ 'received_month' => round($receivedMonth, 2), 'received_month_count' => (clone $recReceivedQ)->count() + (clone $manReceivedQ)->count() + (clone $linkedReceivedQ)->count(), 'paid_month' => round($paidMonth, 2), 'paid_month_count' => (clone $payPaidQ)->count(), 'result_month' => round($receivedMonth - $paidMonth, 2), 'receivable_pending' => round($receivablePending, 2), 'receivable_pending_count' => (clone $recPendingQ)->count() + (clone $manPendingQ)->count() + (clone $linkedPendingQ)->count(), 'payable_pending' => round((float) (clone $payPendingQ)->sum('value'), 2), 'payable_pending_count' => (clone $payPendingQ)->count(), 'overdue_receivable' => round($overdueReceivable, 2), 'overdue_receivable_count' => (clone $recOverdueQ)->count() + (clone $manOverdueQ)->count() + (clone $linkedOverdueQ)->count(), 'overdue_payable' => round((float) (clone $payOverdueQ)->sum('value'), 2), 'overdue_payable_count' => (clone $payOverdueQ)->count(), ]; } }