Skip to content

Commit de41a03

Browse files
committed
fix: riferca automatica riferimenti ddt in import fe
1 parent d0e0e3b commit de41a03

3 files changed

Lines changed: 58 additions & 41 deletions

File tree

plugins/importFE/actions.php

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -440,48 +440,50 @@
440440
}
441441
}
442442

443-
// Se nella fattura elettronica è indicato un DDT cerco quel documento specifico
444-
$ddt = $dati_ddt[$numero_linea];
445-
$query = "SELECT
446-
`dt_righe_ddt`.`id`,
447-
`dt_righe_ddt`.`idddt` AS id_documento,
448-
`dt_righe_ddt`.`is_descrizione`,
449-
`dt_righe_ddt`.`idarticolo`,
450-
`dt_righe_ddt`.`is_sconto`, 'ddt' AS ref,
451-
CONCAT('DDT num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', `dt_statiddt_lang`.`title`, ']') AS opzione
452-
FROM
453-
`dt_righe_ddt`
454-
INNER JOIN `dt_ddt` ON `dt_ddt`.`id` = `dt_righe_ddt`.`idddt`
455-
INNER JOIN `dt_statiddt` ON `dt_statiddt`.`id` = `dt_ddt`.`idstatoddt`
456-
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).'
457-
WHERE
458-
`dt_ddt`.`numero_esterno` = '.prepare($ddt['numero']).' AND
459-
YEAR(`dt_ddt`.`data`) = '.prepare($ddt['anno']).' AND
460-
`dt_ddt`.`idanagrafica` = '.prepare($anagrafica->id).' AND
461-
`dt_righe_ddt`.`qta` > `dt_righe_ddt`.`qta_evasa` AND
462-
|where|';
463-
464-
// Ricerca di righe DDT con stesso Articolo
465-
if (!empty($id_articolo)) {
466-
$query_articolo = replace($query, [
467-
'|where|' => '`dt_righe_ddt`.`idarticolo` = '.prepare($id_articolo),
468-
]);
443+
// Se nella fattura elettronica è indicato un DDT cerco SOLO quel documento specifico
444+
if (isset($dati_ddt[$numero_linea])) {
445+
$ddt = $dati_ddt[$numero_linea];
446+
$query = "SELECT
447+
`dt_righe_ddt`.`id`,
448+
`dt_righe_ddt`.`idddt` AS id_documento,
449+
`dt_righe_ddt`.`is_descrizione`,
450+
`dt_righe_ddt`.`idarticolo`,
451+
`dt_righe_ddt`.`is_sconto`, 'ddt' AS ref,
452+
CONCAT('DDT num. ', IF(`numero_esterno` != '', `numero_esterno`, `numero`), ' del ', DATE_FORMAT(`data`, '%d/%m/%Y'), ' [', `dt_statiddt_lang`.`title`, ']') AS opzione
453+
FROM
454+
`dt_righe_ddt`
455+
INNER JOIN `dt_ddt` ON `dt_ddt`.`id` = `dt_righe_ddt`.`idddt`
456+
INNER JOIN `dt_statiddt` ON `dt_statiddt`.`id` = `dt_ddt`.`idstatoddt`
457+
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).'
458+
WHERE
459+
`dt_ddt`.`numero_esterno` = '.prepare($ddt['numero']).' AND
460+
YEAR(`dt_ddt`.`data`) = '.prepare($ddt['anno']).' AND
461+
`dt_ddt`.`idanagrafica` = '.prepare($anagrafica->id).' AND
462+
`dt_righe_ddt`.`qta` > `dt_righe_ddt`.`qta_evasa` AND
463+
|where|';
469464

470-
$collegamento = $database->fetchOne($query_articolo);
471-
}
465+
// Ricerca di righe DDT con stesso Articolo
466+
if (!empty($id_articolo)) {
467+
$query_articolo = replace($query, [
468+
'|where|' => '`dt_righe_ddt`.`idarticolo` = '.prepare($id_articolo),
469+
]);
472470

473-
// Ricerca di righe DDT per stessa descrizione
474-
if (empty($collegamento)) {
475-
$query_descrizione = replace($query, [
476-
'|where|' => '`dt_righe_ddt`.`descrizione` = '.prepare($riga['Descrizione']),
477-
]);
471+
$collegamento = $database->fetchOne($query_articolo);
472+
}
473+
474+
// Ricerca di righe DDT per stessa descrizione
475+
if (empty($collegamento)) {
476+
$query_descrizione = replace($query, [
477+
'|where|' => '`dt_righe_ddt`.`descrizione` = '.prepare($riga['Descrizione']),
478+
]);
478479

479-
$collegamento = $database->fetchOne($query_descrizione);
480+
$collegamento = $database->fetchOne($query_descrizione);
481+
}
480482
}
481483

482-
// Se nella fattura elettronica NON è indicato un DDT ed è indicato anche un ordine
483-
// cerco per quell'ordine
484-
if (empty($collegamento)) {
484+
// Se nella fattura elettronica NON è indicato un DDT ed è indicato un ordine
485+
// cerco SOLO per quell'ordine specifico
486+
if (empty($collegamento) && isset($dati_ordini[$numero_linea])) {
485487
$ordine = $dati_ordini[$numero_linea];
486488
$query = "SELECT
487489
`or_righe_ordini`.`id`,
@@ -523,10 +525,10 @@
523525

524526
/*
525527
* TENTATIVO 2: ricerca solo per articolo o descrizione su documenti
526-
* non referenziati nella fattura elettronica
528+
* SOLO se nella fattura elettronica NON sono specificati riferimenti per questa riga
527529
*/
528-
// Se non ci sono Ordini o DDT cerco per contenuto
529-
if (empty($collegamento)) {
530+
// Se non ci sono riferimenti specifici nell'XML E non ho trovato collegamenti, cerco per contenuto
531+
if (empty($collegamento) && !isset($dati_ddt[$numero_linea]) && !isset($dati_ordini[$numero_linea])) {
530532
$match_documento_da_fe = false;
531533
$query = "SELECT
532534
`dt_righe_ddt`.`id`,

plugins/importFE/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/generate.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,11 @@ function copia() {
799799
}
800800
});
801801
802+
// Flag globale per indicare se siamo in modalità ricerca automatica
803+
var isAutomaticReferenceSearch = false;
804+
802805
$("select[name^=selezione_riferimento]").change(function() {
803-
if (!$(this).hasClass("already-loaded")) {
806+
if (!$(this).hasClass("already-loaded") && !isAutomaticReferenceSearch) {
804807
let $this = $(this);
805808
let data = $this.selectData();
806809

0 commit comments

Comments
 (0)