Skip to content

Commit 8633f35

Browse files
committed
fix: importazione sequenziale fatture di acquisto
1 parent b710cae commit 8633f35

4 files changed

Lines changed: 226 additions & 117 deletions

File tree

plugins/importFE/actions.php

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141

4242
break;
4343

44+
case 'get_local_files':
45+
// Ottiene solo i file locali per l'importazione in sequenza
46+
$list = Interaction::getFileList();
47+
48+
echo json_encode($list);
49+
50+
break;
51+
4452
case 'save':
4553
$temp_name = $_FILES['blob']['tmp_name'];
4654
$name = $_FILES['blob']['name'];
@@ -189,24 +197,60 @@
189197
$process_result = Interaction::processInvoice($filename);
190198
if ($process_result != '') {
191199
flash()->error($process_result);
192-
redirect(base_path().'/controller.php?id_module='.$id_module);
200+
201+
// Se siamo in modalità sequenza, continua con il prossimo file
202+
if (get('sequence') == 1) {
203+
// Usa la lista appropriata in base alle API
204+
if (Interaction::isEnabled()) {
205+
$files = Interaction::getInvoiceList();
206+
} else {
207+
$files = Interaction::getFileList();
208+
}
209+
$next_record = $id_record + 1;
210+
$next_file = $files[$next_record - 1] ?? null;
211+
212+
if (!empty($next_file)) {
213+
redirect(base_path().'/editor.php?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record='.$next_record.'&sequence=1');
214+
} else {
215+
flash()->info(tr('Tutte le fatture salvate sono state importate!'));
216+
redirect(base_path().'/controller.php?id_module='.$id_module);
217+
}
218+
} else {
219+
redirect(base_path().'/controller.php?id_module='.$id_module);
220+
}
193221

194222
return;
195223
}
196224
}
197225

198-
$files = Interaction::getFileList();
199-
$file = $files[$id_record - 1];
226+
// Usa la lista appropriata in base alle API
227+
if (Interaction::isEnabled()) {
228+
$files = Interaction::getInvoiceList();
229+
} else {
230+
$files = Interaction::getFileList();
231+
}
232+
$file = $files[$id_record - 1] ?? null;
233+
234+
200235

201236
if (get('sequence') == null) {
202237
redirect(base_path().'/editor.php?id_module='.$id_module.'&id_record='.$id_fattura);
203238
} elseif (!empty($file)) {
204-
redirect(base_path().'/editor.php?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record='.$id_record.'&sequence=1');
205-
flash()->info(tr('La fattura numero _NUM_ del _DATA_ (_ANAGRAFICA_) è stata importata correttamente', [
206-
'_NUM_' => $fattura->numero,
207-
'_DATA_' => dateFormat($fattura->data),
208-
'_ANAGRAFICA_' => $fattura->anagrafica->ragione_sociale,
209-
]));
239+
// Calcola il prossimo record per l'importazione in sequenza
240+
$next_record = $id_record + 1;
241+
$next_file = $files[$next_record - 1] ?? null;
242+
243+
if (!empty($next_file)) {
244+
redirect(base_path().'/editor.php?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record='.$next_record.'&sequence=1');
245+
flash()->info(tr('La fattura numero _NUM_ del _DATA_ (_ANAGRAFICA_) è stata importata correttamente', [
246+
'_NUM_' => $fattura->numero,
247+
'_DATA_' => dateFormat($fattura->data),
248+
'_ANAGRAFICA_' => $fattura->anagrafica->ragione_sociale,
249+
]));
250+
} else {
251+
flash()->info(tr('Tutte le fatture salvate sono state importate!'));
252+
redirect(base_path().'/controller.php?id_module='.$id_module);
253+
}
210254
} else {
211255
flash()->info(tr('Tutte le fatture salvate sono state importate!'));
212256
redirect(base_path().'/controller.php?id_module='.$id_module);
@@ -441,14 +485,14 @@
441485

442486
// Se nella fattura elettronica è indicato un DDT cerco quel documento specifico
443487
$ddt = $dati_ddt[$numero_linea];
444-
$query = "SELECT
445-
`dt_righe_ddt`.`id`,
446-
`dt_righe_ddt`.`idddt` AS id_documento,
447-
`dt_righe_ddt`.`is_descrizione`,
448-
`dt_righe_ddt`.`idarticolo`,
488+
$query = "SELECT
489+
`dt_righe_ddt`.`id`,
490+
`dt_righe_ddt`.`idddt` AS id_documento,
491+
`dt_righe_ddt`.`is_descrizione`,
492+
`dt_righe_ddt`.`idarticolo`,
449493
`dt_righe_ddt`.`is_sconto`, 'ddt' AS ref,
450494
CONCAT('DDT num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', `dt_statiddt_lang`.`title`, ']') AS opzione
451-
FROM
495+
FROM
452496
`dt_righe_ddt`
453497
INNER JOIN `dt_ddt` ON `dt_ddt`.`id` = `dt_righe_ddt`.`idddt`
454498
INNER JOIN `dt_statiddt` ON `dt_statiddt`.`id` = `dt_ddt`.`idstatoddt`
@@ -482,12 +526,12 @@
482526
// cerco per quell'ordine
483527
if (empty($collegamento)) {
484528
$ordine = $dati_ordini[$numero_linea];
485-
$query = "SELECT
486-
`or_righe_ordini`.`id`,
487-
`or_righe_ordini`.`idordine` AS id_documento,
488-
`or_righe_ordini`.`is_descrizione`,
489-
`or_righe_ordini`.`idarticolo`,
490-
`or_righe_ordini`.`is_sconto`,
529+
$query = "SELECT
530+
`or_righe_ordini`.`id`,
531+
`or_righe_ordini`.`idordine` AS id_documento,
532+
`or_righe_ordini`.`is_descrizione`,
533+
`or_righe_ordini`.`idarticolo`,
534+
`or_righe_ordini`.`is_sconto`,
491535
'ordine' AS ref,
492536
CONCAT('Ordine num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', `or_statiordine_lang`.`title` , ']') AS opzione
493537
FROM `or_righe_ordini`
@@ -527,45 +571,45 @@
527571
// Se non ci sono Ordini o DDT cerco per contenuto
528572
if (empty($collegamento)) {
529573
$match_documento_da_fe = false;
530-
$query = "SELECT
531-
`dt_righe_ddt`.`id`,
532-
`dt_righe_ddt`.`idddt` AS id_documento,
533-
`dt_righe_ddt`.`is_descrizione`,
534-
`dt_righe_ddt`.`idarticolo`,
535-
`dt_righe_ddt`.`is_sconto`,
574+
$query = "SELECT
575+
`dt_righe_ddt`.`id`,
576+
`dt_righe_ddt`.`idddt` AS id_documento,
577+
`dt_righe_ddt`.`is_descrizione`,
578+
`dt_righe_ddt`.`idarticolo`,
579+
`dt_righe_ddt`.`is_sconto`,
536580
'ddt' AS ref,
537581
CONCAT('DDT num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', `dt_statiddt_lang`.`title`, ']') AS opzione
538-
FROM
582+
FROM
539583
`dt_righe_ddt`
540584
INNER JOIN `dt_ddt` ON `dt_ddt`.`id` = `dt_righe_ddt`.`idddt`
541585
INNER JOIN `dt_statiddt` ON `dt_statiddt`.`id` = `dt_ddt`.`idstatoddt`
542586
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).')
543587
INNER JOIN `dt_tipiddt` ON `dt_ddt`.`idtipoddt` = `dt_tipiddt`.`id`
544-
WHERE
545-
`dt_ddt`.`idanagrafica` = '.prepare($anagrafica->id)." AND
546-
|where_ddt| AND
547-
`dt_righe_ddt`.`qta` > `dt_righe_ddt`.`qta_evasa` AND
588+
WHERE
589+
`dt_ddt`.`idanagrafica` = '.prepare($anagrafica->id)." AND
590+
|where_ddt| AND
591+
`dt_righe_ddt`.`qta` > `dt_righe_ddt`.`qta_evasa` AND
548592
`dt_statiddt_lang`.`title` != 'Fatturato' AND
549593
`dt_tipiddt`.`dir` = 'uscita'
550-
UNION
551-
SELECT
594+
UNION
595+
SELECT
552596
`or_righe_ordini`.`id`,
553597
`or_righe_ordini`.`idordine` AS id_documento,
554-
`or_righe_ordini`.`is_descrizione`,
555-
`or_righe_ordini`.`idarticolo`,
556-
`or_righe_ordini`.`is_sconto`,
598+
`or_righe_ordini`.`is_descrizione`,
599+
`or_righe_ordini`.`idarticolo`,
600+
`or_righe_ordini`.`is_sconto`,
557601
'ordine' AS ref,
558602
CONCAT('Ordine num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', (SELECT `descrizione` FROM `or_statiordine` WHERE `id` = `idstatoordine`) , ']') AS opzione
559-
FROM
603+
FROM
560604
`or_righe_ordini`
561605
INNER JOIN `or_ordini` ON `or_ordini`.`id` = `or_righe_ordini`.`idordine`
562606
INNER JOIN `or_statiordine` ON `or_statiordine`.`id` = `or_ordini`.`idstatoordine`
563607
LEFT JOIN `or_statiordine_lang` ON (`or_statiordine_lang`.`id_record` = `or_statiordine`.`id` AND `or_statiordine_lang`.`id_lang` = ".prepare(Models\Locale::getDefault()->id).')
564608
INNER JOIN `or_tipiordine` ON `or_ordini`.`idtipoordine` = `or_tipiordine`.`id`
565-
WHERE
566-
`or_ordini`.`idanagrafica` = '.prepare($anagrafica->id)." AND
567-
|where_ordini| AND
568-
`or_righe_ordini`.`qta` > `or_righe_ordini`.`qta_evasa` AND
609+
WHERE
610+
`or_ordini`.`idanagrafica` = '.prepare($anagrafica->id)." AND
611+
|where_ordini| AND
612+
`or_righe_ordini`.`qta` > `or_righe_ordini`.`qta_evasa` AND
569613
`or_statiordine_lang`.`title` != 'Fatturato' AND
570614
`or_tipiordine`.`dir` ='uscita'";
571615

plugins/importFE/edit.php

Lines changed: 115 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -177,90 +177,132 @@ function upload(btn) {
177177
echo '
178178
function importAll(btn) {
179179
swal({
180-
title: "'.tr('Importare tutte le fatture?').'",
181-
html: "'.tr('Verranno scaricate tutte le fatture da importare, e non sarà più possibile visualizzare altre informazioni oltre al nome per le fatture che non verranno importate completamente. Continuare?').'",
180+
title: "'.tr('Importare tutte le fatture in sequenza?').'",
181+
html: "'.tr('Verranno importate manualmente tutte le fatture presenti. Continuare?').'",
182182
showCancelButton: true,
183183
confirmButtonText: "'.tr('Procedi').'",
184184
type: "info",
185185
}).then(function (result) {
186-
var restore = buttonLoading(btn);
187-
188-
// Mostra l\'animazione di caricamento
189-
$("#main_loading").show();
190-
191-
// Mostra un messaggio di caricamento
192-
$("#list").html("<div class=\"text-center py-5\"><i class=\"fa fa-refresh fa-spin fa-3x fa-fw text-primary\"></i><div class=\"mt-3\">'.tr('Importazione fatture in corso...').'</div></div>");
193-
194-
$.ajax({
195-
url: globals.rootdir + "/actions.php",
196-
data: {
197-
op: "list",
198-
id_module: "'.$id_module.'",
199-
id_plugin: "'.$id_plugin.'",
200-
},
201-
type: "post",
202-
success: function(data){
203-
data = JSON.parse(data);
204-
205-
count = data.length;
206-
counter = 0;
207-
data.forEach(function(element) {
208-
$.ajax({
209-
url: globals.rootdir + "/actions.php",
210-
type: "get",
211-
data: {
212-
id_module: "'.$id_module.'",
213-
id_plugin: "'.$id_plugin.'",
214-
op: "prepare",
215-
name: element.name,
216-
},
217-
success: function(data) {
218-
counter ++;
219-
220-
importComplete(count, counter, btn, restore);
221-
},
222-
error: function(data) {
223-
counter ++;
224-
225-
importComplete(count, counter, btn, restore);
226-
}
227-
});
228-
});
229-
230-
importComplete(count, counter, btn, restore);
231-
},
232-
error: function(data) {
233-
alert("'.tr('Errore').': " + data);
186+
if (result) {
187+
var restore = buttonLoading(btn);
188+
$("#main_loading").show();
189+
190+
// Ottieni la lista delle fatture per trovare la prima da importare
191+
$.ajax({
192+
url: globals.rootdir + "/actions.php",
193+
data: {
194+
op: "list",
195+
id_module: "'.$id_module.'",
196+
id_plugin: "'.$id_plugin.'",
197+
},
198+
type: "post",
199+
success: function(data){
200+
try {
201+
data = JSON.parse(data);
234202
235-
$("#main_loading").fadeOut();
236-
buttonRestore(btn, restore);
237-
}
238-
});
239-
});
240-
}
203+
if (data.length > 0) {
204+
// Avvia l\'importazione in sequenza dalla prima fattura
205+
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=1&sequence=1");
206+
} else {
207+
$("#main_loading").hide();
208+
buttonRestore(btn, restore);
241209
242-
function importComplete(count, counter, btn, restore) {
243-
// Se tutte le fatture sono state elaborate, reindirizza
244-
if(counter == count){
245-
$("#main_loading").fadeOut();
246-
buttonRestore(btn, restore);
210+
swal({
211+
title: "'.tr('Nessuna fattura da importare').'",
212+
html: "'.tr('Non sono presenti fatture da importare.').'",
213+
type: "warning",
214+
});
215+
}
216+
} catch (e) {
217+
$("#main_loading").hide();
218+
buttonRestore(btn, restore);
247219
248-
// Mostra un messaggio di successo prima del reindirizzamento
249-
swal({
250-
title: "'.tr('Importazione completata!').'",
251-
text: "'.tr('Tutte le fatture sono state importate con successo.').'",
252-
type: "success",
253-
timer: 2000,
254-
showConfirmButton: false
255-
}).then(function() {
256-
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=1&sequence=1");
257-
});
258-
}
220+
swal({
221+
title: "'.tr('Errore').'",
222+
html: "'.tr('Errore durante il caricamento delle fatture.').'",
223+
type: "error",
224+
});
225+
}
226+
},
227+
error: function(data) {
228+
$("#main_loading").hide();
229+
buttonRestore(btn, restore);
230+
231+
swal({
232+
title: "'.tr('Errore').'",
233+
html: "'.tr('Errore durante il caricamento delle fatture.').'",
234+
type: "error",
235+
});
236+
}
237+
});
238+
}
239+
});
259240
}';
260241
} else {
261242
echo '
262243
function importAll(btn) {
263-
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=1&sequence=1");
244+
swal({
245+
title: "'.tr('Importare tutte le fatture in sequenza?').'",
246+
html: "'.tr('Verranno importate manualmente tutte le fatture presenti nella cartella di importazione. Continuare?').'",
247+
showCancelButton: true,
248+
confirmButtonText: "'.tr('Procedi').'",
249+
type: "info",
250+
}).then(function (result) {
251+
if (result) {
252+
var restore = buttonLoading(btn);
253+
$("#main_loading").show();
254+
255+
// Carica la lista delle fatture locali per trovare la prima da importare
256+
$.ajax({
257+
url: globals.rootdir + "/actions.php",
258+
data: {
259+
op: "get_local_files",
260+
id_module: "'.$id_module.'",
261+
id_plugin: "'.$id_plugin.'",
262+
},
263+
type: "post",
264+
success: function(data){
265+
try {
266+
data = JSON.parse(data);
267+
268+
if (data.length > 0) {
269+
// Avvia l\'importazione in sequenza dal primo file (record 1)
270+
// Il record ID corrisponde alla posizione nell\'array + 1
271+
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=1&sequence=1");
272+
} else {
273+
$("#main_loading").hide();
274+
buttonRestore(btn, restore);
275+
276+
swal({
277+
title: "'.tr('Nessuna fattura da importare').'",
278+
html: "'.tr('Non sono presenti fatture nella cartella di importazione.').'",
279+
type: "warning",
280+
});
281+
}
282+
} catch (e) {
283+
$("#main_loading").hide();
284+
buttonRestore(btn, restore);
285+
286+
swal({
287+
title: "'.tr('Errore').'",
288+
html: "'.tr('Errore durante il caricamento delle fatture.').'",
289+
type: "error",
290+
});
291+
}
292+
},
293+
error: function(data) {
294+
$("#main_loading").hide();
295+
buttonRestore(btn, restore);
296+
297+
swal({
298+
title: "'.tr('Errore').'",
299+
html: "'.tr('Errore durante il caricamento delle fatture.').'",
300+
type: "error",
301+
});
302+
}
303+
});
304+
}
305+
});
264306
}';
265307
}
266308
echo '

0 commit comments

Comments
 (0)