Prechádzať zdrojové kódy

feat(tbr): geração automática mensal do TBR do mês fechado via schedule

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ebagabee 4 týždňov pred
rodič
commit
3a4ce0288b

+ 41 - 0
app/Console/Commands/GenerateMonthlyTbrCommand.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Console\Commands;
+
+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';
+
+    public function handle(TbrCalculationService $service): 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'],
+        ));
+
+        return self::SUCCESS;
+    }
+}

+ 2 - 0
bootstrap/app.php

@@ -28,6 +28,8 @@
     })
     ->withSchedule(function (Schedule $schedule) {
         $schedule->call(new DeleteExpiredTokens)->everyMinute();
+        // Gera automaticamente o TBR do mês fechado, todo dia 1 às 03:00.
+        $schedule->command('tbr:generate-monthly')->monthlyOn(1, '03:00');
     })
     ->withExceptions(function (Exceptions $exceptions) {
         //