GenerateMonthlyTbrCommand.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\TbrCalculationService;
  4. use Carbon\Carbon;
  5. use Illuminate\Console\Command;
  6. /**
  7. * Geração automática do TBR do mês fechado (roda no schedule mensal).
  8. *
  9. * Sem opções, gera o TBR do MÊS ANTERIOR (já fechado) — ex.: rodando em 01/ago,
  10. * gera as contas referentes a julho. Idempotente: pula o que já foi gerado.
  11. */
  12. class GenerateMonthlyTbrCommand extends Command
  13. {
  14. protected $signature = 'tbr:generate-monthly {--year=} {--month=}';
  15. protected $description = 'Gera automaticamente as cobranças do TBR do mês fechado';
  16. public function handle(TbrCalculationService $service): int
  17. {
  18. $reference = Carbon::now()->subMonthNoOverflow();
  19. $year = (int) ($this->option('year') ?: $reference->year);
  20. $month = (int) ($this->option('month') ?: $reference->month);
  21. $this->info("Gerando TBR automático para {$month}/{$year}...");
  22. $result = $service->generateBatch($year, $month, null);
  23. $this->info(sprintf(
  24. '%d gerado(s), %d pulado(s), %d erro(s).',
  25. $result['generated_count'],
  26. $result['skipped_count'],
  27. $result['error_count'],
  28. ));
  29. return self::SUCCESS;
  30. }
  31. }