|
19 | 19 | */ |
20 | 20 |
|
21 | 21 | include_once __DIR__.'/../../core.php'; |
22 | | -include __DIR__.'/settings.php'; |
| 22 | +include_once __DIR__.'/init.php'; |
23 | 23 |
|
24 | 24 | use Carbon\Carbon; |
| 25 | +use Models\Module; |
25 | 26 | use Modules\Anagrafiche\Tipo; |
26 | 27 | use Modules\DDT\DDT; |
27 | 28 | use Modules\Fatture\Fattura; |
|
38 | 39 | $file = null; |
39 | 40 | switch (filter('op')) { |
40 | 41 | case 'list': |
41 | | - $list = Interaction::getRemoteList(); |
| 42 | + $list = Interaction::getInvoiceList(); |
42 | 43 |
|
43 | 44 | echo json_encode($list); |
44 | 45 |
|
45 | 46 | break; |
46 | 47 |
|
47 | | - case 'prepare': |
48 | | - if (!isset($file)) { |
49 | | - $name = filter('name'); |
50 | | - $file = Interaction::getInvoiceFile($name); |
51 | | - } |
52 | | - |
| 48 | + case 'prepare-all': |
| 49 | + // Ottimizzazione: prepara tutte le fatture in una sola chiamata |
53 | 50 | try { |
54 | | - if (!FatturaElettronica::isValid($file, 'Fatture di vendita', 'Importazione FE')) { |
| 51 | + $list = Interaction::getInvoiceList(); |
| 52 | + $total = count($list); |
| 53 | + |
| 54 | + if ($total === 0) { |
55 | 55 | echo json_encode([ |
56 | | - 'already' => 1, |
| 56 | + 'success' => false, |
| 57 | + 'total' => 0, |
| 58 | + 'message' => tr('Nessuna fattura da importare') |
57 | 59 | ]); |
58 | | - |
59 | | - return; |
| 60 | + break; |
60 | 61 | } |
61 | | - } catch (Exception) { |
62 | | - } |
63 | 62 |
|
64 | | - // Individuazione ID fisico |
65 | | - $files = Interaction::getFileList([], 'Fatture di vendita', 'Importazione FE'); |
66 | | - foreach ($files as $key => $value) { |
67 | | - if ($value['name'] == $file) { |
68 | | - $index = $key; |
| 63 | + // Pre-validazione delle fatture per evitare errori durante l'importazione sequenziale |
| 64 | + $valid_count = 0; |
| 65 | + $errors = []; |
69 | 66 |
|
70 | | - break; |
| 67 | + foreach ($list as $invoice) { |
| 68 | + try { |
| 69 | + $file = Interaction::getInvoiceFile($invoice['name']); |
| 70 | + if (!FatturaElettronica::isValid($file, 'Fatture di vendita', 'Importazione FE')) { |
| 71 | + $valid_count++; |
| 72 | + } |
| 73 | + } catch (Exception $e) { |
| 74 | + $errors[] = $invoice['name'] . ': ' . $e->getMessage(); |
| 75 | + } |
71 | 76 | } |
| 77 | + |
| 78 | + echo json_encode([ |
| 79 | + 'success' => true, |
| 80 | + 'total' => $total, |
| 81 | + 'valid_count' => $valid_count, |
| 82 | + 'errors' => $errors, |
| 83 | + 'message' => $valid_count > 0 ? tr('Fatture preparate per l\'importazione') : tr('Nessuna fattura valida trovata') |
| 84 | + ]); |
| 85 | + |
| 86 | + } catch (Exception $e) { |
| 87 | + echo json_encode([ |
| 88 | + 'success' => false, |
| 89 | + 'total' => 0, |
| 90 | + 'message' => tr('Errore durante la preparazione').': ' . $e->getMessage() |
| 91 | + ]); |
72 | 92 | } |
73 | 93 |
|
74 | | - echo json_encode([ |
75 | | - 'id' => $index + 1, |
76 | | - ]); |
| 94 | + break; |
77 | 95 |
|
78 | 96 | case 'save': |
79 | 97 | $directory = FatturaElettronica::getImportDirectory('Fatture di vendita', 'Importazione FE'); |
|
113 | 131 |
|
114 | 132 | $data = $file['FatturaElettronicaBody']['DatiGenerali']['DatiGeneraliDocumento']['Data']; |
115 | 133 |
|
116 | | - $fattura_pa = FatturaElettronica::manage($xml, 'Fattura di vendita', 'Importazione FE'); |
| 134 | + $fattura = FatturaElettronica::manage($xml, 'Fattura di vendita', 'Importazione FE'); |
117 | 135 |
|
118 | 136 | // Tipo documento |
119 | | - $fattura_body = $fattura_pa->getBody(); |
| 137 | + $fattura_body = $fattura->getBody(); |
120 | 138 | $dati_generali = $fattura_body['DatiGenerali']['DatiGeneraliDocumento']; |
121 | 139 | $id_tipo = $database->fetchOne('SELECT id FROM co_tipidocumento WHERE dir = "entrata" AND codice_tipo_documento_fe = '.prepare($dati_generali['TipoDocumento']))['id']; |
122 | 140 |
|
|
134 | 152 | if (!empty($pagamento)) { |
135 | 153 | $id_pagamento = $pagamento->id; |
136 | 154 | } else { |
137 | | - $id_pagamento = $settings['id_pagamento']; |
| 155 | + $id_pagamento = setting('Tipo di pagamento predefinito'); |
138 | 156 | } |
139 | 157 |
|
140 | | - $fattura_pa->saveFattura($id_pagamento, $settings['id_segment'], $id_tipo, $data, false, false, 'Cliente'); |
| 158 | + // Ottieni il sezionale predefinito per le fatture di vendita |
| 159 | + $id_module_fatture_vendita = Module::where('name', 'Fatture di vendita')->first()->id; |
| 160 | + $id_segment_predefinito = getSegmentPredefined($id_module_fatture_vendita); |
| 161 | + |
| 162 | + $fattura->saveFattura($id_pagamento, $id_segment_predefinito, $id_tipo, $data, false, false, 'Cliente'); |
141 | 163 |
|
142 | | - $righe = $fattura_pa->getRighe(); |
| 164 | + $righe = $fattura->getRighe(); |
143 | 165 |
|
144 | 166 | $articoli = []; |
145 | 167 | $iva = []; |
|
150 | 172 |
|
151 | 173 | $aliquota_iva = $riga['AliquotaIVA']; |
152 | 174 |
|
153 | | - $iva[$key] = $settings['iva'][$aliquota_iva]; |
| 175 | + $iva[$key] = setting('Iva predefinita'); |
154 | 176 |
|
155 | | - $conti[$key] = $settings['id_conto']; |
| 177 | + $conti[$key] = setting('Conto predefinito fatture di vendita'); |
156 | 178 | } |
157 | 179 |
|
158 | | - $fattura_pa->saveRighe($articoli, $iva, $conti); |
| 180 | + $fattura->saveRighe($articoli, $iva, $conti); |
159 | 181 |
|
160 | | - $fattura_pa->saveAllegati('Fatture di vendita'); |
| 182 | + $fattura->saveAllegati('Fatture di vendita'); |
161 | 183 |
|
162 | | - $id_record = $fattura_pa->getFattura()->id; |
| 184 | + $id_record = $fattura->getFattura()->id; |
163 | 185 | $fattura = Fattura::find($id_record); |
164 | 186 | $fattura->gestoreMovimenti = new GestoreMovimenti($fattura); |
165 | 187 |
|
|
217 | 239 |
|
218 | 240 | $directory = FatturaElettronica::getImportDirectory('Fatture di vendita'); |
219 | 241 | $files = Interaction::getFileList([], 'Fatture di vendita', 'Importazione FE'); |
| 242 | + |
220 | 243 | $file = $files[$file_id]; |
221 | 244 |
|
222 | 245 | if (!empty($file)) { |
|
248 | 271 | 'serial' => post('flag_crea_seriali') ? post('serial') : [], |
249 | 272 | ]; |
250 | 273 |
|
251 | | - $fattura_pa = FatturaElettronica::manage($filename, 'Fatture di vendita', 'Importazione FE'); |
252 | | - $id_fattura = $fattura_pa->save($info, 'Cliente'); |
253 | | - $fattura_pa->delete(); |
| 274 | + $fattura = FatturaElettronica::manage($filename, 'Fatture di vendita', 'Importazione FE'); |
| 275 | + $id_fattura = $fattura->save($info, 'Cliente'); |
| 276 | + $fattura->delete(); |
254 | 277 | $fattura = Fattura::find($id_fattura); |
255 | 278 | $id_autofattura = post('autofattura'); |
256 | 279 | $new_stato = Stato::where('name', 'Pagato')->first()->id; |
|
315 | 338 | flash()->info(tr('Tutte le fatture salvate sono state importate!')); |
316 | 339 | redirect(base_path().'/controller.php?id_module='.$id_module); |
317 | 340 | } |
318 | | - |
319 | 341 | $record = null; |
320 | 342 | break; |
321 | 343 |
|
|
340 | 362 | return; |
341 | 363 | } |
342 | 364 |
|
343 | | - $fatture = $anagrafica->fattureAcquisto() |
| 365 | + $fatture = $anagrafica->fattureVendita() |
344 | 366 | ->contabile() |
345 | 367 | ->orderBy('created_at', 'DESC') |
346 | 368 | ->take(10) |
|
365 | 387 | $id_tipo = $tipi->sort()->keys()->last(); |
366 | 388 |
|
367 | 389 | // Ricerca del conto più utilizzato |
368 | | - // Filtro le righe che hanno un conto valido (maggiore di 0) |
369 | | - $righe_con_conto = $righe->filter(fn ($item) => !empty($item->idconto) && $item->idconto > 0); |
370 | | - |
371 | | - $conto = null; |
372 | | - if ($righe_con_conto->isNotEmpty()) { |
373 | | - $conti = $righe_con_conto->groupBy(fn ($item, $key) => $item->idconto)->transform(fn ($item, $key) => $item->count()); |
374 | | - $id_conto = $conti->sort()->keys()->last(); |
375 | | - $conto = $database->fetchOne('SELECT * FROM co_pianodeiconti3 WHERE id = '.prepare($id_conto)); |
376 | | - } |
| 390 | + $conti = $righe->groupBy(fn ($item, $key) => $item->idconto)->transform(fn ($item, $key) => $item->count()); |
| 391 | + $id_conto = $conti->sort()->keys()->last(); |
| 392 | + $conto = $database->fetchOne('SELECT * FROM co_pianodeiconti3 WHERE id = '.prepare($id_conto)); |
377 | 393 |
|
378 | 394 | // Ricerca dell'IVA più utilizzata secondo percentuali |
379 | 395 | $iva = []; |
380 | | - // Filtro le righe che hanno un'aliquota IVA valida |
381 | | - $righe_con_iva = $righe->filter(fn ($item) => !empty($item->idiva) && $item->idiva > 0 && !empty($item->aliquota)); |
382 | | - $percentuali_iva = $righe_con_iva->groupBy(fn ($item, $key) => $item->aliquota->percentuale); |
| 396 | + $percentuali_iva = $righe->groupBy(fn ($item, $key) => $item->aliquota->percentuale); |
383 | 397 | foreach ($percentuali_iva as $key => $values) { |
384 | 398 | $aliquote = $values->mapToGroups(fn ($item, $key) => [$item->aliquota->id => $item->aliquota]); |
385 | 399 | $id_aliquota = $aliquote->map(fn ($item, $key) => $item->count())->sort()->keys()->last(); |
|
391 | 405 | ]; |
392 | 406 | } |
393 | 407 |
|
394 | | - $response = [ |
| 408 | + echo json_encode([ |
395 | 409 | 'id_tipo' => $id_tipo, |
396 | | - 'iva' => $iva, |
397 | | - ]; |
398 | | - |
399 | | - // Aggiungo il conto solo se è stato trovato |
400 | | - if (!empty($conto)) { |
401 | | - $response['conto'] = [ |
| 410 | + 'conto' => [ |
402 | 411 | 'id' => $conto['id'], |
403 | 412 | 'descrizione' => $conto['descrizione'], |
404 | | - ]; |
405 | | - } |
406 | | - |
407 | | - echo json_encode($response); |
| 413 | + ], |
| 414 | + 'iva' => $iva, |
| 415 | + ]); |
408 | 416 | break; |
409 | 417 |
|
410 | 418 | case 'riferimenti-automatici': |
|
417 | 425 | $results = []; |
418 | 426 |
|
419 | 427 | // Dati ordini |
420 | | - $DatiOrdini = XML::forceArray($fattura_pa->getBody()['DatiGenerali']['DatiOrdineAcquisto']); |
421 | | - $DatiDDT = XML::forceArray($fattura_pa->getBody()['DatiGenerali']['DatiDDT']); |
| 428 | + $DatiOrdini = XML::forceArray($fattura->getBody()['DatiGenerali']['DatiOrdineAcquisto']); |
| 429 | + $DatiDDT = XML::forceArray($fattura->getBody()['DatiGenerali']['DatiDDT']); |
422 | 430 |
|
423 | 431 | $replaces = ['n ', 'N ', 'n. ', 'N. ', 'nr ', 'NR ', 'nr. ', 'NR. ', 'num ', 'NUM ', 'num. ', 'NUM. ', 'numero ', 'NUMERO ']; |
424 | 432 |
|
|
510 | 518 | } |
511 | 519 |
|
512 | 520 | // Iterazione sulle singole righe |
513 | | - $righe = $fattura_pa->getRighe(); |
| 521 | + $righe = $fattura->getRighe(); |
514 | 522 | foreach ($righe as $key => $riga) { |
515 | 523 | // Se la riga è descrittiva non la collego a documenti |
516 | 524 | if ($riga['PrezzoTotale'] == 0) { |
|
740 | 748 | $riga = $documento->getRiga($namespace.$type, $collegamento['id']); |
741 | 749 | $riga_origine = $riga->getOriginalComponent(); |
742 | 750 |
|
| 751 | + $desc_conto = ''; |
743 | 752 | if (!empty($riga->idarticolo)) { |
744 | | - $desc_conto = $dbo->fetchOne('SELECT CONCAT( co_pianodeiconti2.numero, ".", co_pianodeiconti3.numero, " ", co_pianodeiconti3.descrizione ) AS descrizione FROM co_pianodeiconti3 INNER JOIN co_pianodeiconti2 ON co_pianodeiconti3.idpianodeiconti2=co_pianodeiconti2.id WHERE co_pianodeiconti3.id = '.prepare($riga->articolo->idconto_acquisto))['descrizione']; |
| 753 | + $desc_conto = $dbo->fetchOne('SELECT CONCAT( co_pianodeiconti2.numero, ".", co_pianodeiconti3.numero, " ", co_pianodeiconti3.descrizione ) AS descrizione FROM co_pianodeiconti3 INNER JOIN co_pianodeiconti2 ON co_pianodeiconti3.idpianodeiconti2=co_pianodeiconti2.id WHERE co_pianodeiconti3.id = '.prepare($riga->articolo->idconto_vendita))['descrizione']; |
745 | 754 | } |
746 | 755 |
|
747 | 756 | // Compilazione dei dati |
|
764 | 773 | 'iva_percentuale' => $riga->aliquota->percentuale, |
765 | 774 | 'id_articolo' => $riga->idarticolo, |
766 | 775 | 'desc_articolo' => str_replace(' ', '_', $riga->articolo->codice.' - '.$riga->articolo->getTranslation('title')), |
767 | | - 'id_conto' => $riga->articolo->idconto_acquisto, |
768 | | - 'desc_conto' => str_replace(' ', '_', $desc_conto), |
| 776 | + 'id_conto' => $riga->articolo->idconto_vendita, |
| 777 | + 'desc_conto' => str_replace(' ', '_', $desc_conto ?: ''), |
769 | 778 | ], |
770 | 779 | ]; |
771 | 780 | } |
|
0 commit comments