Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdministratorsInvoicesSync
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 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     * The FacturasService instance.
27     *
28     * @var FacturasService
29     */
30    protected $facturasService;
31
32    /**
33     * Create a new command instance.
34     *
35     * @return void
36     */
37    public function __construct(FacturasService $facturasService)
38    {
39        parent::__construct();
40        $this->facturasService = $facturasService;
41    }
42
43    /**
44     * Execute the console command.
45     *
46     * @return int
47     */
48    public function handle()
49    {
50        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Comunidad Valenciana', 'Madrid'];
51
52        try {
53            foreach ($regions as $region) {
54                $resultSet = $this->facturasService->setAllMonthAdministratorsInvoices($region);
55
56                if ($resultSet['success'] === false) {
57                    $this->warn("   Skipping sending for {$region} due to import error: ".($resultSet['error'] ?? 'Unknown error'));
58
59                    continue;
60                }
61
62                $resultSend = $this->facturasService->sendAdministratorsInvoices($region);
63
64                if ($resultSend['success'] === true) {
65                    $this->info("   ✅ Envío completado para {$region}.");
66                } else {
67                    $this->error("   ❌ Error al enviar recordatorios en {$region}".($resultSend['error'] ?? $resultSend['message'] ?? 'Unknown error'));
68                }
69            }
70
71            return Command::SUCCESS;
72        } catch (\Exception $e) {
73            Log::channel('g3w_invoices')->error('Error al sincronizar facturas: '.$e->getMessage());
74            $this->error('Error: '.$e->getMessage());
75
76            return Command::FAILURE;
77        }
78    }
79}