mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-02-02 17:07:01 +01:00
Aggiunta ricerca automatica dei riferimenti per FE
This commit is contained in:
parent
fa1c9b9b83
commit
c2a51c326a
@ -45,7 +45,6 @@ switch (filter('op')) {
|
||||
|
||||
$fornitore->codice_fornitore = post('codice_fornitore');
|
||||
$fornitore->descrizione = post('descrizione');
|
||||
$fornitore->prezzo_acquisto = post('prezzo_acquisto');
|
||||
$fornitore->qta_minima = post('qta_minima');
|
||||
$fornitore->giorni_consegna = post('giorni_consegna');
|
||||
|
||||
|
@ -64,15 +64,11 @@ echo '
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{[ "type": "number", "label": "'.tr('Prezzo acquisto').'", "name": "prezzo_acquisto", "required": 1, "value": "'.$fornitore['prezzo_acquisto'].'", "icon-after": "€" ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<div class="col-md-6">
|
||||
{[ "type": "number", "label": "'.tr('Qta minima ordinabile').'", "name": "qta_minima", "required": 0, "value": "'.$fornitore['qta_minima'].'", "icon-after": "'.$articolo->um.'" ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<div class="col-md-6">
|
||||
{[ "type": "text", "label": "'.tr('Tempi di consegna').'", "name": "giorni_consegna", "class": "text-right", "required": 0, "value": "'.$fornitore['giorni_consegna'].'", "icon-after": "'.tr('gg').'" ]}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
include_once __DIR__.'/../../core.php';
|
||||
|
||||
use Modules\DDT\DDT;
|
||||
use Modules\Ordini\Ordine;
|
||||
use Plugins\ImportFE\FatturaElettronica;
|
||||
use Plugins\ImportFE\Interaction;
|
||||
|
||||
@ -141,12 +143,12 @@ switch (filter('op')) {
|
||||
$fattura_pa->delete();
|
||||
|
||||
// Aggiorno la tipologia di anagrafica fornitore
|
||||
$anagrafica = $dbo->fetchOne('SELECT idanagrafica FROM co_documenti WHERE co_documenti.id='.prepare($id_fattura));
|
||||
$rs_t = $dbo->fetchOne("SELECT * FROM an_tipianagrafiche_anagrafiche WHERE idtipoanagrafica=(SELECT an_tipianagrafiche.idtipoanagrafica FROM an_tipianagrafiche WHERE an_tipianagrafiche.descrizione='Fornitore') AND idanagrafica=".prepare($anagrafica['idanagrafica']));
|
||||
$anagrafica = $database->fetchOne('SELECT idanagrafica FROM co_documenti WHERE co_documenti.id='.prepare($id_fattura));
|
||||
$rs_t = $database->fetchOne("SELECT * FROM an_tipianagrafiche_anagrafiche WHERE idtipoanagrafica=(SELECT an_tipianagrafiche.idtipoanagrafica FROM an_tipianagrafiche WHERE an_tipianagrafiche.descrizione='Fornitore') AND idanagrafica=".prepare($anagrafica['idanagrafica']));
|
||||
|
||||
// Se non trovo corrispondenza aggiungo all'anagrafica la tipologia fornitore
|
||||
if (empty($rs_t)) {
|
||||
$dbo->query("INSERT INTO an_tipianagrafiche_anagrafiche (idtipoanagrafica, idanagrafica) VALUES ((SELECT an_tipianagrafiche.idtipoanagrafica FROM an_tipianagrafiche WHERE an_tipianagrafiche.descrizione='Fornitore'), ".prepare($anagrafica['idanagrafica']).')');
|
||||
$database->query("INSERT INTO an_tipianagrafiche_anagrafiche (idtipoanagrafica, idanagrafica) VALUES ((SELECT an_tipianagrafiche.idtipoanagrafica FROM an_tipianagrafiche WHERE an_tipianagrafiche.descrizione='Fornitore'), ".prepare($anagrafica['idanagrafica']).')');
|
||||
}
|
||||
|
||||
// Processo il file ricevuto
|
||||
@ -230,7 +232,7 @@ switch (filter('op')) {
|
||||
return $item->count();
|
||||
});
|
||||
$id_conto = $conti->sort()->keys()->last();
|
||||
$conto = $dbo->fetchOne('SELECT * FROM co_pianodeiconti3 WHERE id = '.prepare($id_conto));
|
||||
$conto = $database->fetchOne('SELECT * FROM co_pianodeiconti3 WHERE id = '.prepare($id_conto));
|
||||
|
||||
// Ricerca dell'IVA più utilizzata secondo percentuali
|
||||
$iva = [];
|
||||
@ -265,4 +267,126 @@ switch (filter('op')) {
|
||||
'iva' => $iva,
|
||||
]);
|
||||
break;
|
||||
|
||||
case 'riferimenti-automatici':
|
||||
if (empty($anagrafica)) {
|
||||
echo json_encode([]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$results = [];
|
||||
|
||||
// Iterazione sulle singole righe
|
||||
$righe = $fattura_pa->getRighe();
|
||||
foreach ($righe as $key => $riga) {
|
||||
$collegamento = null;
|
||||
|
||||
// Visualizzazione codici articoli
|
||||
$codici = $riga['CodiceArticolo'] ?: [];
|
||||
$codici = !empty($codici) && !isset($codici[0]) ? [$codici] : $codici;
|
||||
|
||||
// Ricerca dell'articolo collegato al codice
|
||||
$id_articolo = null;
|
||||
foreach ($codici as $codice) {
|
||||
if (!empty($anagrafica) && empty($id_articolo)) {
|
||||
$id_articolo = $database->fetchOne('SELECT id_articolo AS id FROM mg_fornitore_articolo WHERE codice_fornitore = '.prepare($codice['CodiceValore']).' AND id_fornitore = '.prepare($anagrafica->id))['id'];
|
||||
}
|
||||
|
||||
if (empty($id_articolo)) {
|
||||
$id_articolo = $database->fetchOne('SELECT id FROM mg_articoli WHERE codice = '.prepare($codice['CodiceValore']))['id'];
|
||||
}
|
||||
|
||||
if (!empty($id_articolo)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$query = "SELECT dt_righe_ddt.id, dt_righe_ddt.idddt AS id_documento, dt_righe_ddt.is_descrizione, dt_righe_ddt.idarticolo, dt_righe_ddt.is_sconto, 'ddt' AS ref,
|
||||
CONCAT('DDT num. ', IF(numero_esterno != '', numero_esterno, numero), ' del ', DATE_FORMAT(data, '%d/%m/%Y'), ' [', (SELECT descrizione FROM dt_statiddt WHERE id = idstatoddt) , ']') AS opzione
|
||||
FROM dt_righe_ddt
|
||||
INNER JOIN dt_ddt ON dt_ddt.id = dt_righe_ddt.idddt
|
||||
WHERE dt_ddt.idanagrafica = ".prepare($anagrafica->id)." AND |where_ddt|
|
||||
|
||||
UNION SELECT or_righe_ordini.id, or_righe_ordini.idordine AS id_documento, or_righe_ordini.is_descrizione, or_righe_ordini.idarticolo, or_righe_ordini.is_sconto, 'ordine' AS ref,
|
||||
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
|
||||
FROM or_righe_ordini
|
||||
INNER JOIN or_ordini ON or_ordini.id = or_righe_ordini.idordine
|
||||
WHERE or_ordini.idanagrafica = ".prepare($anagrafica->id).' AND |where_ordini|';
|
||||
|
||||
// Ricerca di righe DDT/Ordine con stesso Articolo
|
||||
if (!empty($id_articolo)) {
|
||||
$query_articolo = replace($query, [
|
||||
'|where_ddt|' => 'dt_righe_ddt.idarticolo = '.prepare($id_articolo),
|
||||
'|where_ordini|' => 'or_righe_ordini.idarticolo = '.prepare($id_articolo),
|
||||
]);
|
||||
|
||||
$collegamento = $database->fetchOne($query_articolo);
|
||||
}
|
||||
|
||||
// Ricerca di righe DDT/Ordine per stessa descrizione
|
||||
if (empty($collegamento)) {
|
||||
$query_descrizione = replace($query, [
|
||||
'|where_ddt|' => 'dt_righe_ddt.descrizione = '.prepare($riga['Descrizione']),
|
||||
'|where_ordini|' => 'or_righe_ordini.descrizione = '.prepare($riga['Descrizione']),
|
||||
]);
|
||||
|
||||
$collegamento = $database->fetchOne($query_descrizione);
|
||||
}
|
||||
|
||||
// Ricerca di righe DDT/Ordine per stesso importo
|
||||
if (empty($collegamento)) {
|
||||
$query_descrizione = replace($query, [
|
||||
'|where_ddt|' => 'dt_righe_ddt.prezzo_unitario = '.prepare($riga['PrezzoUnitario']),
|
||||
'|where_ordini|' => 'or_righe_ordini.prezzo_unitario = '.prepare($riga['PrezzoUnitario']),
|
||||
]);
|
||||
|
||||
$collegamento = $database->fetchOne($query_descrizione);
|
||||
}
|
||||
|
||||
if (!empty($collegamento)) {
|
||||
// Individuazione del documento
|
||||
$documento = $collegamento['ref'] == 'ddt' ? DDT::find($collegamento['id_documento']) : Ordine::find($collegamento['id_documento']);
|
||||
|
||||
// Individuazione della classe di gestione per la riga
|
||||
$namespace = $collegamento['ref'] == 'ddt' ? 'Modules\\DDT\\Components\\' : 'Modules\\Ordini\\Components\\';
|
||||
if (!empty($collegamento['idarticolo'])) {
|
||||
$type = 'Articolo';
|
||||
} elseif (!empty($collegamento['is_sconto'])) {
|
||||
$type = 'Sconto';
|
||||
} elseif (!empty($collegamento['is_descrizione'])) {
|
||||
$type = 'Descrizione';
|
||||
} else {
|
||||
$type = 'Riga';
|
||||
}
|
||||
|
||||
// Ricerca della riga
|
||||
$riga = $documento->getRiga($namespace.$type, $collegamento['id']);
|
||||
$riga_origine = $riga->getOriginal();
|
||||
|
||||
// Compilazione dei dati
|
||||
$results[$key] = [
|
||||
'documento' => [
|
||||
'tipo' => $collegamento['ref'],
|
||||
'id' => $collegamento['id_documento'],
|
||||
'descrizione' => reference($documento, tr('Origine')),
|
||||
'opzione' => $collegamento['opzione'],
|
||||
],
|
||||
'riga' => [
|
||||
'tipo' => get_class($riga),
|
||||
'id' => $riga->id,
|
||||
'descrizione' => $riga->descrizione,
|
||||
'qta' => $riga->qta,
|
||||
'um' => $riga->um,
|
||||
'prezzo_unitario' => $riga->prezzo_unitario ?: $riga_origine->prezzo_unitario,
|
||||
'id_iva' => $riga->id_iva,
|
||||
'iva_percentuale' => $riga->aliquota->percentuale,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($results);
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -18,9 +18,17 @@
|
||||
*/
|
||||
|
||||
echo '
|
||||
<button type="button" class="btn btn-primary tip" '.(!empty($anagrafica) ? '' : 'disabled').' id="compilazione_automatica" onclick="compile(this)" title="'.tr('Tenta la compilazione automatica delle informazioni delle fattura elettronica sulla base delle precedenti fatture del Fornitore').'.">
|
||||
<div class="tip" data-toggle="tooltip" title="'.tr('Tenta la compilazione automatica delle informazioni delle fattura elettronica sulla base delle precedenti fatture del Fornitore').'.">
|
||||
<button type="button" class="btn btn-primary" '.(!empty($anagrafica) ? '' : 'disabled').' id="compilazione_automatica" onclick="compile(this)" >
|
||||
<i class="fa fa-address-book"></i> '.tr('Compila automaticamente').'
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="tip" data-toggle="tooltip" title="'.tr('Tenta il completamento automatico dei riferimenti per le righe delle fattura elettronica sulla base di Ordini e DDT registrati nel gestionale per il Fornitore').'.">
|
||||
<button type="button" class="btn btn-primary" '.(!empty($anagrafica) ? '' : 'disabled').' onclick="compilaRiferimenti(this)" >
|
||||
<i class="fa fa-list"></i> '.tr('Cerca riferimenti').'
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
@ -32,12 +40,13 @@ $(document).ready(function() {
|
||||
});
|
||||
|
||||
function compile(btn) {
|
||||
var restore = buttonLoading(btn);
|
||||
let restore = buttonLoading(btn);
|
||||
|
||||
$.ajax({
|
||||
url: globals.rootdir + "/actions.php",
|
||||
cache: false,
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
data: {
|
||||
id_module: "'.$id_module.'",
|
||||
id_plugin: "'.$id_plugin.'",
|
||||
@ -45,27 +54,24 @@ function compile(btn) {
|
||||
op: "compile",
|
||||
},
|
||||
success: function(response) {
|
||||
var data = JSON.parse(response);
|
||||
if (data.length == 0){
|
||||
buttonRestore(btn, restore);
|
||||
if (response.length === 0){
|
||||
return;
|
||||
}
|
||||
|
||||
$("#id_tipo").selectSet(data.id_tipo);
|
||||
$("#pagamento").selectSetNew(data.pagamento.id, data.pagamento.descrizione);
|
||||
$("#id_tipo").selectSet(response.id_tipo);
|
||||
$("#pagamento").selectSetNew(response.pagamento.id, response.pagamento.descrizione);
|
||||
|
||||
$("select[name^=iva]").each(function(){
|
||||
var aliquota = $(this).closest("tr").find("[id^=aliquota]").text();
|
||||
if (data.iva[aliquota] !== undefined){
|
||||
$(this).selectSet(data.iva[aliquota].id);
|
||||
if (response.iva[aliquota] !== undefined){
|
||||
$(this).selectSet(response.iva[aliquota].id);
|
||||
}
|
||||
});
|
||||
|
||||
$("select[name^=conto]").each(function(){
|
||||
$(this).selectSetNew(data.conto.id, data.conto.descrizione);
|
||||
$(this).selectSetNew(response.conto.id, response.conto.descrizione);
|
||||
});
|
||||
|
||||
buttonRestore(btn, restore);
|
||||
},
|
||||
error: function(data) {
|
||||
swal("'.tr('Errore').'", "'.tr('La compilazione automatica dei campi non è andata a buon fine').'.", "error");
|
||||
@ -74,4 +80,40 @@ function compile(btn) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function compilaRiferimenti(btn) {
|
||||
let restore = buttonLoading(btn);
|
||||
|
||||
$.ajax({
|
||||
url: globals.rootdir + "/actions.php",
|
||||
cache: false,
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
data: {
|
||||
id_module: "'.$id_module.'",
|
||||
id_plugin: "'.$id_plugin.'",
|
||||
id_record: "'.$id_record.'",
|
||||
op: "riferimenti-automatici",
|
||||
},
|
||||
success: function(response) {
|
||||
buttonRestore(btn, restore);
|
||||
if (response.length === 0){
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [id_riga, data] of response.entries()) {
|
||||
// Selezione dinamica
|
||||
$("#selezione_riferimento" + id_riga).selectSetNew(data.documento.id, data.documento.opzione);
|
||||
|
||||
// Impostazione del riferiment
|
||||
impostaRiferimento(id_riga, data.documento, data.riga);
|
||||
}
|
||||
},
|
||||
error: function(data) {
|
||||
swal("'.tr('Errore').'", "'.tr('La ricerca automatica dei riferimenti per le righe non è andata a buon fine').'.", "error");
|
||||
|
||||
buttonRestore(btn, restore);
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>';
|
||||
|
@ -17,6 +17,8 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
include_once __DIR__.'/../../core.php';
|
||||
|
||||
echo '
|
||||
@ -207,7 +209,7 @@ echo '
|
||||
|
||||
// Data di registrazione
|
||||
$data_registrazione = get('data_registrazione');
|
||||
$data_registrazione = new \Carbon\Carbon($data_registrazione);
|
||||
$data_registrazione = new Carbon($data_registrazione);
|
||||
echo '
|
||||
<div class="col-md-3">
|
||||
{[ "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'].'" ]}
|
||||
@ -268,7 +270,6 @@ echo '
|
||||
|
||||
// Righe
|
||||
$righe = $fattura_pa->getRighe();
|
||||
|
||||
if (!empty($righe)) {
|
||||
echo '
|
||||
<h4>
|
||||
@ -291,7 +292,7 @@ if (!empty($righe)) {
|
||||
<tbody>';
|
||||
|
||||
foreach ($righe as $key => $riga) {
|
||||
$query = 'SELECT id, IF(codice IS NULL, descrizione, CONCAT(codice, " - ", descrizione)) AS descrizione FROM co_iva WHERE percentuale = '.prepare($riga['AliquotaIVA']);
|
||||
$query = "SELECT id, IF(codice IS NULL, descrizione, CONCAT(codice, ' - ', descrizione)) AS descrizione FROM co_iva WHERE percentuale = ".prepare($riga['AliquotaIVA']);
|
||||
|
||||
if (!empty($riga['Natura'])) {
|
||||
$query .= ' AND codice_natura_fe = '.prepare($riga['Natura']);
|
||||
@ -312,8 +313,14 @@ if (!empty($righe)) {
|
||||
$id_articolo = null;
|
||||
$codice_principale = $codici[0]['CodiceValore'];
|
||||
if (!empty($codice_principale)) {
|
||||
if (!empty($anagrafica) && empty($id_articolo)) {
|
||||
$id_articolo = $database->fetchOne('SELECT id_articolo AS id FROM mg_fornitore_articolo WHERE codice_fornitore = '.prepare($codice_principale).' AND id_fornitore = '.prepare($anagrafica->id))['id'];
|
||||
}
|
||||
|
||||
if (empty($id_articolo)) {
|
||||
$id_articolo = $database->fetchOne('SELECT id FROM mg_articoli WHERE codice = '.prepare($codice_principale))['id'];
|
||||
}
|
||||
}
|
||||
|
||||
$qta = $riga['Quantita'];
|
||||
$um = $riga['UnitaMisura'];
|
||||
|
Loading…
x
Reference in New Issue
Block a user