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;
7
8class AdministratorsInvoicesSync extends Command
9{
10    /**
11     * The name and signature of the console command.
12     *
13     * @var string
14     */
15    protected $signature = 'administrators-invoices:sync {region?}';
16
17    /**
18     * The console command description.
19     *
20     * @var string
21     */
22    protected $description = 'Automatic sending of administrators invoice reminders';
23
24    /**
25     * The FacturasService instance.
26     *
27     * @var FacturasService
28     */
29    protected $facturasService;
30
31    /**
32     * Create a new command instance.
33     *
34     * @return void
35     */
36    public function __construct(FacturasService $facturasService)
37    {
38        parent::__construct();
39        $this->facturasService = $facturasService;
40    }
41
42    /**
43     * Execute the console command.
44     *
45     * @return int
46     */
47    public function handle()
48    {
49        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Comunidad Valenciana', 'Madrid'];
50
51        try {
52            foreach ($regions as $region) {
53                $resultSet = $this->facturasService->setAllMonthAdministratorsInvoices($region);
54                
55                if ($resultSet['success'] === false) {
56                    $this->warn("   Skipping sending for {$region} due to import error: " . ($resultSet['error'] ?? 'Unknown error'));
57                    continue;
58                }
59
60                $resultSend = $this->facturasService->sendAdministratorsInvoices($region);
61                
62                if ($resultSend['success'] === true) {
63                    $this->info("   ✅ Envío completado para {$region}.");
64                } else {
65                    $this->error("   ❌ Error al enviar recordatorios en {$region}" . ($resultSend['error'] ?? $resultSend['message'] ?? 'Unknown error'));
66                }
67            }
68            return Command::SUCCESS;
69        } catch (\Exception $e) {
70            Log::channel('g3w_invoices')->error("Error al sincronizar facturas: " . $e->getMessage());
71            $this->error("Error: " . $e->getMessage());
72            return Command::FAILURE;
73        }
74    }
75}