Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.25% covered (danger)
6.25%
1 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdministratorsInvoicesSync
6.25% covered (danger)
6.25%
1 / 16
50.00% covered (danger)
50.00%
1 / 2
47.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\FacturasService;
6use Illuminate\Console\Command;
7use Illuminate\Support\Facades\Log;
8
9class AdministratorsInvoicesSync extends Command
10{
11    /**
12     * The name and signature of the console command.
13     *
14     * @var string
15     */
16    protected $signature = 'administrators-invoices:sync {region?}';
17
18    /**
19     * The console command description.
20     *
21     * @var string
22     */
23    protected $description = 'Automatic sending of administrators invoice reminders';
24
25    /**
26     * Create a new command instance.
27     */
28    public function __construct(protected FacturasService $facturasService)
29    {
30        parent::__construct();
31    }
32
33    /**
34     * Execute the console command.
35     */
36    public function handle(): int
37    {
38        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Comunidad Valenciana', 'Madrid'];
39
40        try {
41            foreach ($regions as $region) {
42                $resultSet = $this->facturasService->setAllMonthAdministratorsInvoices($region);
43
44                if ($resultSet['success'] === false) {
45                    $this->warn("   Skipping sending for {$region} due to import error: ".($resultSet['error'] ?? 'Unknown error'));
46
47                    continue;
48                }
49
50                $resultSend = $this->facturasService->sendAdministratorsInvoices($region);
51
52                if ($resultSend['success'] === true) {
53                    $this->info("   ✅ Envío completado para {$region}.");
54                } else {
55                    $this->error("   ❌ Error al enviar recordatorios en {$region}".($resultSend['error'] ?? $resultSend['message'] ?? 'Unknown error'));
56                }
57            }
58
59            return Command::SUCCESS;
60        } catch (\Exception $e) {
61            Log::channel('g3w_invoices')->error('Error al sincronizar facturas: '.$e->getMessage());
62            $this->error('Error: '.$e->getMessage());
63
64            return Command::FAILURE;
65        }
66    }
67}