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