'janeiro', 2 => 'fevereiro', 3 => 'março', 4 => 'abril', 5 => 'maio', 6 => 'junho', 7 => 'julho', 8 => 'agosto', 9 => 'setembro', 10 => 'outubro', 11 => 'novembro', 12 => 'dezembro', ]; public function handle(TbrCalculationService $service, NotificationService $notifications): int { $reference = Carbon::now()->subMonthNoOverflow(); $year = (int) ($this->option('year') ?: $reference->year); $month = (int) ($this->option('month') ?: $reference->month); $this->info("Gerando TBR automático para {$month}/{$year}..."); $result = $service->generateBatch($year, $month, null); $this->info(sprintf( '%d gerado(s), %d pulado(s), %d erro(s).', $result['generated_count'], $result['skipped_count'], $result['error_count'], )); if ($result['generated_count'] > 0) { $this->notifyMonthClosed($notifications, $year, $month, $result['generated']); } return self::SUCCESS; } /** * Notifica a matriz (contas a receber) e cada unidade (conta a pagar). */ private function notifyMonthClosed( NotificationService $notifications, int $year, int $month, array $generated, ): void { $monthLabel = (self::MONTHS[$month] ?? $month) . "/{$year}"; $count = count($generated); // Matriz: visão consolidada no Contas a Receber. $notifications->dispatch([ 'origin_table' => 'tbr_calculations', 'origin_id' => $year * 100 + $month, 'type' => NotificationType::TBR_MONTH_CLOSED->value, 'title' => 'Fechamento do mês', 'message' => "As cobranças de {$monthLabel} foram geradas no Contas a Receber " . "({$count} unidade(s)).", 'url' => '/financial/accounts-receivable', 'action_text' => 'Ver Contas a Receber', 'priority' => 'normal', 'recipient_user_ids' => $notifications->matrizUserIds(), ]); // Cada unidade: sua conta a pagar (TBR) do mês. foreach ($generated as $item) { $unitId = $item['unit_id'] ?? null; if (!$unitId) { continue; } $notifications->dispatch([ 'origin_table' => 'unit_account_payables', 'origin_id' => $item['receivable_id'] ?? ($year * 100 + $month), 'unit_id' => $unitId, 'type' => NotificationType::TBR_MONTH_CLOSED->value, 'title' => 'Conta a pagar gerada', 'message' => "Sua cobrança (TBR) referente a {$monthLabel} foi gerada em Contas a Pagar.", 'url' => '/financial/accounts-payable', 'action_text' => 'Ver Contas a Pagar', 'priority' => 'normal', 'recipient_user_ids' => $notifications->unitUserIds($unitId), ]); } } }