Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreditDaysOfferedSync
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 2
420
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 / 72
0.00% covered (danger)
0.00%
0 / 1
380
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\GestionaService;
6use App\Models\TblCreditDaysOffered;
7use Illuminate\Console\Command;
8use Illuminate\Http\Request;
9use Illuminate\Support\Facades\Log;
10use Illuminate\Support\Facades\File;
11use Carbon\Carbon;
12
13class CreditDaysOfferedSync extends Command
14{
15    /**
16     * The name and signature of the console command.
17     *
18     * @var string
19     */
20    protected $signature = 'sync:credit-days-offered {syncAll?}';
21
22    /**
23     * The console command description.
24     *
25     * @var string
26     */
27    protected $description = 'Command description';
28
29    /**
30     * Create a new command instance.
31     *
32     * @return void
33     */
34    public function __construct(GestionaService $gestionaService)
35    {
36        parent::__construct();
37        $this->gestionaService = $gestionaService;
38    }
39
40    /**
41     * Execute the console command.
42     *
43     * @return int
44     */
45    public function handle()
46    {
47        try {
48
49            $isSyncAll = $this->argument('syncAll') ?? null;
50
51            $regions = array(
52                'Catalunya',
53                'Madrid',                
54                'Valencia',
55                'Almeria',                
56            );
57
58            $years = [date('Y')];
59
60            if($isSyncAll == 1){                
61                $years = array(
62                    2023,
63                    2024,
64                    2025,
65                    2026
66                );
67            }
68
69            foreach ($regions as $region) {    
70
71                foreach ($years as $year) {      
72                                        
73                    $date = $year . date('-m-d', strtotime('-1 day'));
74                    if($isSyncAll == 1){
75                        $date = $year . date('-m-d');
76                    }
77
78                    $clients = $this->gestionaService->request('get', 'cliente/nuevosconunafactura/'. $date, $region, []);
79
80                    if (!empty($clients)) {
81                        foreach ($clients as $client) {
82                            $id = $client['ID'];
83
84                            $clientInvoiceResponse = $this->gestionaService->request('get', 'factura/cliente/'. $id, $region, []);
85                            $invoices = $clientInvoiceResponse['facturas'] ?? [];
86
87                            if (count($invoices) >= 2) {
88                                continue;
89                            }
90
91                            $clientData = $this->gestionaService->request('get', 'cliente/'. $id, $region, []);
92
93                            if (!empty($clientData) && !empty($clientData['cliente'])) { 
94                                $clientData = $clientData['cliente'];
95
96                                if (count($invoices) === 1) {
97                                    $invoiceResponse = $this->gestionaService->request('get', 'factura/'. $invoices[0]['numero'], $region, []);
98                                    if (!empty($invoiceResponse) && !empty($invoiceResponse['factura'])) { 
99                                        $invoiceData = $invoiceResponse['factura'];
100                                        $clientValue = $invoiceData['base_imponible_factura'] ?? 0;
101
102                                        if (! empty($invoiceData['fecha_creacion']) && ! empty($invoiceData['fecha_emision'])) {
103                                            $creation = Carbon::parse($invoiceData['fecha_creacion']);
104                                            $send = $invoiceData['vencimientos'][0]['fecha_vencimiento'] ?? $invoiceData['fecha_emision'];
105                                            $creditDays = $creation->diffInDays($send);
106                                        }
107                                    }
108                                }
109
110                                $highDate = ($clientData['fecha_alta'] === '0000-00-00' || empty($clientData['fecha_alta']))
111                                    ? Carbon::now()
112                                    : Carbon::parse($clientData['fecha_alta']);
113
114                                TblCreditDaysOffered::upsert(
115                                    [
116                                        [
117                                            'cif' => $clientData['cliente_cif'],
118                                            'client_code' => $clientData['cod_cliente'],
119                                            'client_name' => trim($clientData['empresa']),
120                                            'credit_days' => $creditDays,
121                                            'taxable_invoice_base' => $clientValue,
122                                            'payment_days' => $clientData['forma_pago'] ?? 0,
123                                            'has_invoice' => count($invoices) === 1 ? 1 : 0,
124                                            'region' => $region,
125                                            'high_date' => $highDate,
126                                            'updated_at' => now(),
127                                        ]
128                                    ],
129                                    ['client_code'],
130                                    [
131                                        'cif',
132                                        'client_name',
133                                        'taxable_invoice_base',
134                                        'payment_days',
135                                        'has_invoice',
136                                        'region',
137                                        'high_date',
138                                        'updated_at'
139                                    ]
140                                );
141                            }                            
142                        }
143                    }
144                }
145            }
146
147        } catch (\Exception $e) {
148            Log::channel('cron_credit_days_offered_sync')->error($e->getMessage());
149        }
150    }
151}