| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Console\Commands;
- use App\Enums\NotificationType;
- use App\Services\NotificationService;
- use App\Services\TbrCalculationService;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- /**
- * Geração automática do TBR do mês fechado (roda no schedule mensal).
- *
- * Sem opções, gera o TBR do MÊS ANTERIOR (já fechado) — ex.: rodando em 01/ago,
- * gera as contas referentes a julho. Idempotente: pula o que já foi gerado.
- */
- class GenerateMonthlyTbrCommand extends Command
- {
- protected $signature = 'tbr:generate-monthly {--year=} {--month=}';
- protected $description = 'Gera automaticamente as cobranças do TBR do mês fechado';
- private const MONTHS = [
- 1 => '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),
- ]);
- }
- }
- }
|