Skip to content

Commit 47c53c6

Browse files
committed
fix: allineamento con importFE
1 parent 37e576b commit 47c53c6

5 files changed

Lines changed: 152 additions & 53 deletions

File tree

plugins/importFE_ZIP/actions.php

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,14 @@
510510
// Riorganizzazione dati ordini per numero di riga
511511
$dati_ordini = [];
512512
foreach ($DatiOrdini as $dato) {
513+
if (!isset($dato['RiferimentoNumeroLinea']) || !$dato['RiferimentoNumeroLinea']) {
514+
$dati_ordini_documento[] = [
515+
'numero' => $dato['IdDocumento'],
516+
'data' => (new Carbon($dato['Data']))->format('d/m/Y'),
517+
];
518+
continue;
519+
}
520+
513521
if (is_array($dato['RiferimentoNumeroLinea'])) {
514522
foreach ($dato['RiferimentoNumeroLinea'] as $dati => $linea) {
515523
foreach ($replaces as $replace) {
@@ -594,6 +602,28 @@
594602
}
595603
}
596604

605+
// Aggiungi gli ordini senza riferimento a riga specifica
606+
if (isset($dati_ordini_documento)) {
607+
foreach ($dati_ordini_documento as $ordine) {
608+
$riferimento_fe[] = tr('Ordine _NUMERO_ del _DATA_',
609+
[
610+
'_NUMERO_' => $ordine['numero'],
611+
'_DATA_' => $ordine['data'],
612+
]);
613+
}
614+
}
615+
616+
// Aggiungi i DDT senza riferimento a riga specifica
617+
if (isset($dati_ddt_documento)) {
618+
foreach ($dati_ddt_documento as $ddt) {
619+
$riferimento_fe[] = tr('DDT _NUMERO_ del _DATA_',
620+
[
621+
'_NUMERO_' => $ddt['numero'],
622+
'_DATA_' => $ddt['data'],
623+
]);
624+
}
625+
}
626+
597627
// Iterazione sulle singole righe
598628
$righe = $fattura->getRighe();
599629
foreach ($righe as $key => $riga) {
@@ -644,48 +674,50 @@
644674
}
645675
}
646676

647-
// Se nella fattura elettronica è indicato un DDT cerco quel documento specifico
648-
$ddt = $dati_ddt[$numero_linea];
649-
$query = "SELECT
650-
`dt_righe_ddt`.`id`,
651-
`dt_righe_ddt`.`idddt` AS id_documento,
652-
`dt_righe_ddt`.`is_descrizione`,
653-
`dt_righe_ddt`.`idarticolo`,
654-
`dt_righe_ddt`.`is_sconto`, 'ddt' AS ref,
655-
CONCAT('DDT num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', `dt_statiddt_lang`.`title`, ']') AS opzione
656-
FROM
657-
`dt_righe_ddt`
658-
INNER JOIN `dt_ddt` ON `dt_ddt`.`id` = `dt_righe_ddt`.`idddt`
659-
INNER JOIN `dt_statiddt` ON `dt_statiddt`.`id` = `dt_ddt`.`idstatoddt`
660-
LEFT JOIN `dt_statiddt_lang` ON `dt_statiddt_lang`.`id_record` = `dt_statiddt`.`id` AND `dt_statiddt_lang`.`id_lang` = ".prepare(Models\Locale::getDefault()->id).'
661-
WHERE
662-
`dt_ddt`.`numero_esterno` = '.prepare($ddt['numero']).' AND
663-
YEAR(`dt_ddt`.`data`) = '.prepare($ddt['anno']).' AND
664-
`dt_ddt`.`idanagrafica` = '.prepare($anagrafica->id).' AND
665-
`dt_righe_ddt`.`qta` > `dt_righe_ddt`.`qta_evasa` AND
666-
|where|';
667-
668-
// Ricerca di righe DDT con stesso Articolo
669-
if (!empty($id_articolo)) {
670-
$query_articolo = replace($query, [
671-
'|where|' => '`dt_righe_ddt`.`idarticolo` = '.prepare($id_articolo),
672-
]);
677+
// Se nella fattura elettronica è indicato un DDT cerco SOLO quel documento specifico
678+
if (isset($dati_ddt[$numero_linea])) {
679+
$ddt = $dati_ddt[$numero_linea];
680+
$query = "SELECT
681+
`dt_righe_ddt`.`id`,
682+
`dt_righe_ddt`.`idddt` AS id_documento,
683+
`dt_righe_ddt`.`is_descrizione`,
684+
`dt_righe_ddt`.`idarticolo`,
685+
`dt_righe_ddt`.`is_sconto`, 'ddt' AS ref,
686+
CONCAT('DDT num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', `dt_statiddt_lang`.`title`, ']') AS opzione
687+
FROM
688+
`dt_righe_ddt`
689+
INNER JOIN `dt_ddt` ON `dt_ddt`.`id` = `dt_righe_ddt`.`idddt`
690+
INNER JOIN `dt_statiddt` ON `dt_statiddt`.`id` = `dt_ddt`.`idstatoddt`
691+
LEFT JOIN `dt_statiddt_lang` ON `dt_statiddt_lang`.`id_record` = `dt_statiddt`.`id` AND `dt_statiddt_lang`.`id_lang` = ".prepare(Models\Locale::getDefault()->id).'
692+
WHERE
693+
`dt_ddt`.`numero_esterno` = '.prepare($ddt['numero']).' AND
694+
YEAR(`dt_ddt`.`data`) = '.prepare($ddt['anno']).' AND
695+
`dt_ddt`.`idanagrafica` = '.prepare($anagrafica->id).' AND
696+
`dt_righe_ddt`.`qta` > `dt_righe_ddt`.`qta_evasa` AND
697+
|where|';
673698

674-
$collegamento = $database->fetchOne($query_articolo);
675-
}
699+
// Ricerca di righe DDT con stesso Articolo
700+
if (!empty($id_articolo)) {
701+
$query_articolo = replace($query, [
702+
'|where|' => '`dt_righe_ddt`.`idarticolo` = '.prepare($id_articolo),
703+
]);
676704

677-
// Ricerca di righe DDT per stessa descrizione
678-
if (empty($collegamento)) {
679-
$query_descrizione = replace($query, [
680-
'|where|' => '`dt_righe_ddt`.`descrizione` = '.prepare($riga['Descrizione']),
681-
]);
705+
$collegamento = $database->fetchOne($query_articolo);
706+
}
682707

683-
$collegamento = $database->fetchOne($query_descrizione);
708+
// Ricerca di righe DDT per stessa descrizione
709+
if (empty($collegamento)) {
710+
$query_descrizione = replace($query, [
711+
'|where|' => '`dt_righe_ddt`.`descrizione` = '.prepare($riga['Descrizione']),
712+
]);
713+
714+
$collegamento = $database->fetchOne($query_descrizione);
715+
}
684716
}
685717

686-
// Se nella fattura elettronica NON è indicato un DDT ed è indicato anche un ordine
687-
// cerco per quell'ordine
688-
if (empty($collegamento)) {
718+
// Se nella fattura elettronica NON è indicato un DDT ed è indicato un ordine
719+
// cerco SOLO per quell'ordine specifico
720+
if (empty($collegamento) && isset($dati_ordini[$numero_linea])) {
689721
$ordine = $dati_ordini[$numero_linea];
690722
$query = "SELECT
691723
`or_righe_ordini`.`id`,
@@ -727,10 +759,10 @@
727759

728760
/*
729761
* TENTATIVO 2: ricerca solo per articolo o descrizione su documenti
730-
* non referenziati nella fattura elettronica
762+
* SOLO se nella fattura elettronica NON sono specificati riferimenti per questa riga
731763
*/
732-
// Se non ci sono Ordini o DDT cerco per contenuto
733-
if (empty($collegamento)) {
764+
// Se non ci sono riferimenti specifici nell'XML E non ho trovato collegamenti, cerco per contenuto
765+
if (empty($collegamento) && !isset($dati_ddt[$numero_linea]) && !isset($dati_ordini[$numero_linea])) {
734766
$match_documento_da_fe = false;
735767
$query = "SELECT
736768
`dt_righe_ddt`.`id`,

plugins/importFE_ZIP/buttons.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ function compilaRiferimenti(btn) {
105105
return;
106106
}
107107
108+
// Imposta il flag per indicare che siamo in modalità ricerca automatica
109+
if (typeof isAutomaticReferenceSearch !== "undefined") {
110+
isAutomaticReferenceSearch = true;
111+
}
112+
108113
for (id_riga in response) {
109114
data = response[id_riga];
110115
@@ -114,6 +119,13 @@ function compilaRiferimenti(btn) {
114119
// Impostazione del riferimento
115120
impostaRiferimento(id_riga, data.documento, data.riga);
116121
}
122+
123+
// Reimposta il flag dopo aver completato la ricerca automatica (con un piccolo delay per gestire operazioni asincrone)
124+
setTimeout(function() {
125+
if (typeof isAutomaticReferenceSearch !== "undefined") {
126+
isAutomaticReferenceSearch = false;
127+
}
128+
}, 100);
117129
},
118130
error: function(data) {
119131
swal("'.tr('Errore').'", "'.tr('La ricerca automatica dei riferimenti per le righe non è andata a buon fine').'.", "error");

plugins/importFE_ZIP/generate.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function cleanup(){
189189
</h3>
190190
</div>
191191
<div class="card-body">
192-
<h4>'.$ragione_sociale.' '.(empty($anagrafica) ? '<span class="badge badge-warning">'.tr('Nuova anagrafica').'</span>' : '<small>'.Modules::link('Anagrafiche', $anagrafica->id, '', null, '').'</small>').'</h4>
192+
<h4>'.$ragione_sociale.' '.(empty($anagrafica) ? '<span class="badge badge-warning">'.tr('Nuova anagrafica').'</span>' : '<small>'.Modules::link('Anagrafiche', $anagrafica->id, '', '', '').'</small>').'</h4>
193193
<hr>
194194
<small>
195195
'.(!empty($codice_fiscale) ? ('<strong>'.tr('Codice Fiscale').':</strong> '.$codice_fiscale.'<br>') : '').'
@@ -286,28 +286,37 @@ function cleanup(){
286286
$query = $query_tipo;
287287
}
288288

289-
$id_tipodocumento = $database->fetchOne($query_tipo)['id'];
289+
$tipo_doc = $database->fetchOne($query_tipo);
290+
$id_tipodocumento = $tipo_doc['id'] ?? null;
290291

291292
echo '
292293
<div class="row">
293294
<div class="col-md-3">
294-
{[ "type": "select", "label": "'.tr('Tipo fattura').'", "name": "id_tipo", "required": 1, "values": "query='.$query.'", "value": "'.($numero_tipo != 1 ? $id_tipodocumento : '').'" ]}
295+
{[ "type": "select", "label": "'.tr('Tipo fattura').'", "name": "id_tipo", "required": 1, "values": "query='.$query.'", "value": "'.($numero_tipo != 1 && !empty($id_tipodocumento) ? $id_tipodocumento : '').'" ]}
295296
</div>';
296297

297298
// Sezionale
298-
$id_segment = $database->table('co_tipidocumento')->where('id', '=', $id_tipodocumento)->value('id_segment');
299+
$id_segment = null;
300+
if (!empty($id_tipodocumento)) {
301+
$id_segment = $database->table('co_tipidocumento')->where('id', '=', $id_tipodocumento)->value('id_segment');
302+
}
299303

300304
echo '
301305
<div class="col-md-3">
302-
{[ "type": "select", "label": "'.tr('Sezionale').'", "name": "id_segment", "required": 1, "ajax-source": "segmenti", "select-options": '.json_encode(['id_module' => $id_module, 'is_fiscale' => 1, 'is_sezionale' => 1, 'for_fe' => 1]).', "value": "'.$id_segment.'" ]}
306+
{[ "type": "select", "label": "'.tr('Sezionale').'", "name": "id_segment", "required": 1, "ajax-source": "segmenti", "select-options": '.json_encode(['id_module' => $id_module, 'is_fiscale' => 1, 'is_sezionale' => 1, 'for_fe' => 1]).', "value": "'.($id_segment ?: '').'" ]}
303307
</div>';
304308

305309
// Data di registrazione
306310
$data_registrazione = get('data_registrazione');
307-
$data_registrazione = new Carbon($data_registrazione);
311+
if (!empty($data_registrazione)) {
312+
$data_registrazione = new Carbon($data_registrazione);
313+
} else {
314+
$data_registrazione = null;
315+
}
316+
$data_registrazione_value = !empty($data_registrazione) ? $data_registrazione->format('Y-m-d') : $dati_generali['Data'];
308317
echo '
309318
<div class="col-md-3">
310-
{[ "type": "date", "label": "'.tr('Data di registrazione').'", "name": "data_registrazione", "required": 1, "value": "'.($data_registrazione ?: $dati_generali['Data']).'", "max-date": "-now-", "min-date": "'.$dati_generali['Data'].'" ]}
319+
{[ "type": "date", "label": "'.tr('Data di registrazione').'", "name": "data_registrazione", "required": 1, "value": "'.$data_registrazione_value.'", "max-date": "-now-", "min-date": "'.$dati_generali['Data'].'" ]}
311320
</div>';
312321

313322
if (!empty($anagrafica)) {
@@ -374,6 +383,9 @@ function cleanup(){
374383

375384
// Movimentazioni
376385
echo '
386+
<div class="col-md-3">
387+
{[ "type": "checkbox", "label": "'.tr('Movimenta gli articoli').'", "name": "movimentazione", "value": "'.setting('Movimenta magazzino da fatture di vendita').'" ]}
388+
</div>
377389
378390
<div class="col-md-3">
379391
{[ "type": "checkbox", "label": "'.tr('Creazione automatica articoli').'", "name": "flag_crea_articoli", "value": 0, "help": "'.tr('Nel caso di righe con almeno un nodo \'CodiceArticolo\', il gestionale procede alla creazione dell\'articolo se la riga non risulta assegnata manualmente').'." ]}
@@ -434,24 +446,40 @@ function cleanup(){
434446

435447
// Riorganizzazione dati ordini per numero di riga
436448
$dati_ordini = [];
449+
$dati_ordini_documento = [];
437450
foreach ($DatiOrdini as $dato) {
438451
foreach ($dato['RiferimentoNumeroLinea'] as $dati => $linea) {
439452
$dati_ordini[(int) $linea] = [
440453
'numero' => $dato['IdDocumento'],
441454
'data' => (new Carbon($dato['Data']))->format('d/m/Y'),
442455
];
443456
}
457+
458+
if (!$dato['RiferimentoNumeroLinea']) {
459+
$dati_ordini_documento[] = [
460+
'numero' => $dato['IdDocumento'],
461+
'data' => (new Carbon($dato['Data']))->format('d/m/Y'),
462+
];
463+
}
444464
}
445465

446-
// Riorganizzazione dati ordini per numero di riga
466+
// Riorganizzazione dati ddt per numero di riga
447467
$dati_ddt = [];
468+
$dati_ddt_documento = [];
448469
foreach ($DatiDDT as $dato) {
449470
foreach ($dato['RiferimentoNumeroLinea'] as $dati => $linea) {
450471
$dati_ddt[(int) $linea] = [
451472
'numero' => $dato['NumeroDDT'],
452473
'data' => (new Carbon($dato['DataDDT']))->format('d/m/Y'),
453474
];
454475
}
476+
477+
if (!$dato['RiferimentoNumeroLinea']) {
478+
$dati_ddt_documento[] = [
479+
'numero' => $dato['NumeroDDT'],
480+
'data' => (new Carbon($dato['DataDDT']))->format('d/m/Y'),
481+
];
482+
}
455483
}
456484

457485
foreach ($righe as $key => $riga) {
@@ -490,21 +518,32 @@ function cleanup(){
490518
// Prendo il codice articolo dal primo nodo CodiceValore che trovo
491519
$codice_principale = $codici[0]['CodiceValore'];
492520
if (!empty($codice_principale)) {
521+
if (!empty($anagrafica) && empty($id_articolo)) {
522+
$result = $database->fetchOne('SELECT `id_articolo` AS id FROM `mg_fornitore_articolo` WHERE `codice_fornitore` = '.prepare($codice_principale).' AND `id_fornitore` = '.prepare($anagrafica->id));
523+
$id_articolo = $result['id'] ?? null;
524+
if (empty($id_articolo)) {
525+
$result = $database->fetchOne('SELECT `id_articolo` AS id FROM `mg_fornitore_articolo` WHERE REPLACE(`codice_fornitore`, " ", "") = '.prepare($codice_principale).' AND `id_fornitore` = '.prepare($anagrafica->id));
526+
$id_articolo = $result['id'] ?? null;
527+
}
528+
}
529+
493530
if (empty($id_articolo)) {
494-
$id_articolo = $database->fetchOne('SELECT `id` FROM `mg_articoli` WHERE `codice` = '.prepare($codice_principale))['id'];
531+
$result = $database->fetchOne('SELECT `id` FROM `mg_articoli` WHERE `codice` = '.prepare($codice_principale));
532+
$id_articolo = $result['id'] ?? null;
495533
if (empty($id_articolo)) {
496-
$id_articolo = $database->fetchOne('SELECT `id` FROM `mg_articoli` WHERE REPLACE(`codice`, " ", "") = '.prepare($codice_principale))['id'];
534+
$result = $database->fetchOne('SELECT `id` FROM `mg_articoli` WHERE REPLACE(`codice`, " ", "") = '.prepare($codice_principale));
535+
$id_articolo = $result['id'] ?? null;
497536
}
498537
}
499538

500539
$idconto_vendita = $database->fetchOne('SELECT `idconto_vendita` FROM `mg_articoli` WHERE `id` = '.prepare($id_articolo))['idconto_vendita'];
501540
}
502541

503542
$idconto_vendita = $is_autofattura ? setting('Conto per autofattura') : $idconto_vendita;
504-
$qta = $riga['Quantita'];
543+
$qta = (empty($riga['Quantita']) && (!empty($riga['PrezzoUnitario']) || !empty($riga['Importo'])) ? 1 : $riga['Quantita']);
505544
$um = $riga['UnitaMisura'];
506545
$prezzo_unitario = $riga['PrezzoUnitario'] ?: $riga['Importo'];
507-
$is_descrizione = empty((float) $riga['Quantita']) && empty((float) $prezzo_unitario);
546+
$is_descrizione = empty((float) $qta);
508547

509548
$sconto_unitario = 0;
510549
$sconti = $riga['ScontoMaggiorazione'] ?: 0;

plugins/importFE_ZIP/init.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,20 @@
4646

4747
redirect_url(base_path_osm().'/controller.php?id_module='.$id_module);
4848
}
49+
50+
// Se la fattura è già stata importata e siamo in modalità sequenza, passa alla successiva
51+
if (!empty($imported) && get('sequence') == 1) {
52+
$next_record = $id_record + 1;
53+
$next_file = $files[$next_record - 1] ?? null;
54+
55+
if (!empty($next_file)) {
56+
flash()->info(tr('La fattura _NAME_ è già stata importata, passaggio alla successiva...', [
57+
'_NAME_' => $record['name'],
58+
]));
59+
redirect_url(base_path_osm().'/editor.php?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record='.$next_record.'&sequence=1');
60+
} else {
61+
flash()->info(tr('Tutte le fatture salvate sono state importate!'));
62+
redirect_url(base_path_osm().'/controller.php?id_module='.$id_module);
63+
}
64+
}
4965
}

plugins/importFE_ZIP/list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Carbon\Carbon;
2424
use Plugins\ImportFE\Interaction;
2525

26-
$list = Interaction::getFileList($list = [], 'Fatture di vendita', 'Importazione FE');
26+
$list = Interaction::getFileList([], 'Fatture di vendita', 'Importazione FE');
2727

2828
$directory = Plugins\ImportFE\FatturaElettronica::getImportDirectory('Fatture di vendita', 'Importazione FE');
2929
if (!empty($list)) {

0 commit comments

Comments
 (0)