openstamanager/plugins/receiptFE/edit.php

295 lines
10 KiB
PHP
Raw Normal View History

2018-12-14 12:50:44 +01:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-12-14 12:50:44 +01:00
include_once __DIR__.'/../../core.php';
use Carbon\Carbon;
use Modules\Fatture\Fattura;
2018-12-14 12:50:44 +01:00
use Plugins\ReceiptFE\Interaction;
use Util\XML;
2018-12-14 12:50:44 +01:00
echo '
<p>'.tr('Le ricevute delle Fatture Elettroniche permettono di individuare se una determinata fattura tramessa è stata accettata dal Sistema Di Interscambio').'.</p>';
if (Interaction::isEnabled()) {
2019-11-15 15:11:20 +01:00
echo '
<p>'.tr('Tramite il pulsante _BTN_ è possibile procedere al recupero delle ricevute, aggiornando automaticamente lo stato delle relative fatture e allegandole ad esse', [
'_BTN_' => '<i class="fa fa-refresh"></i> <b>'.tr('Ricerca ricevute').'</b>',
]).'.</p>';
}
// Messaggio informativo su fatture con stato di errore
$fatture_generate_errore = Fattura::vendita()
->whereIn('codice_stato_fe', ['NS', 'ERR', 'EC02'])
->where('data_stato_fe', '>=', $_SESSION['period_start'])
->orderBy('data_stato_fe')
->get();
if (!empty($fatture_generate_errore->count())) {
echo '
<div class="alert alert-warning alert-dismissible" role="alert">
<button class="close" type="button" data-dismiss="alert" aria-hidden="true"><span aria-hidden="true">×</span><span class="sr-only">'.tr('Chiudi').'</span></button>
<p><i class="fa fa-warning"></i> '.tr('Attenzione: le seguenti fatture hanno ricevuto uno scarto o presentano errori in fase di trasmissione').'.</p>
<ul>';
foreach ($fatture_generate_errore as $fattura_generata) {
$descrizione = $fattura_generata['codice_stato_fe'];
$ricevuta_principale = $fattura_generata->getRicevutaPrincipale();
if (!empty($ricevuta_principale)) {
$contenuto_ricevuta = XML::readFile($ricevuta_principale->filepath);
// Informazioni aggiuntive per EC02
if (!empty($contenuto_ricevuta['EsitoCommittente'])) {
$descrizione .= ': '.htmlentities($contenuto_ricevuta['EsitoCommittente']['Descrizione']);
}
// Informazioni aggiuntive per NS
$lista_errori = $contenuto_ricevuta['ListaErrori'];
if ($lista_errori) {
$lista_errori = $lista_errori[0] ? $lista_errori : [$lista_errori];
$errore = $lista_errori[0]['Errore'];
$descrizione .= ': '.$errore['Codice'].' - '.htmlentities($errore['Descrizione']);
}
}
echo '<li>'.reference($fattura_generata, $fattura_generata->getReference()).' ['.$descrizione.'] ['.timestampFormat($fattura_generata['data_stato_fe']).']</li>';
}
echo '
</ul>
</div>';
}
// Controllo se ci sono fatture in elaborazione da più di 7 giorni per le quali non ho ancora una ricevuta
2021-09-17 16:11:54 +02:00
$data_limite = (new Carbon())->subDays(7);
$fatture_generate = Fattura::vendita()
->where('codice_stato_fe', 'WAIT')
->where('data_stato_fe', '>=', $_SESSION['period_start'])
->where('data_stato_fe', '<', $data_limite)
->orderBy('data_stato_fe')
->get();
if (!empty($fatture_generate->count())) {
echo '
2020-11-10 16:40:05 +01:00
<div class="alert alert-info info-dismissible" role="alert"><button class="close" type="button" data-dismiss="alert" aria-hidden="true"><span aria-hidden="true">×</span><span class="sr-only">'.tr('Chiudi').'</span></button>
<p><i class="fa fa-info"></i> '.tr('Informazione: le seguenti fatture sono in attesa di una ricevuta da più di 7 giorni').'.</p>
<ul>';
foreach ($fatture_generate as $fattura_generata) {
echo '<li>'.reference($fattura_generata, $fattura_generata->getReference()).' ['.timestampFormat($fattura_generata['data_stato_fe']).']</li>';
}
echo '
</ul>
</div>';
}
echo '
2019-07-10 15:02:09 +02:00
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title">
'.tr('Carica un XML').'
2019-07-10 15:02:09 +02:00
<span class="tip" title="'.tr('Formati supportati: XML e P7M').'.">
<i class="fa fa-question-circle-o"></i>
</span>
</h3>
</div>
<div class="box-body" id="upload">
<div class="row">
<div class="col-md-9">
{[ "type": "file", "name": "blob", "required": 1 ]}
</div>
<div class="col-md-3">
<button type="button" class="btn btn-primary pull-right" onclick="upload(this)">
<i class="fa fa-upload"></i> '.tr('Carica ricevuta').'
</button>
</div>
</div>
</div>
</div>';
2018-12-14 12:50:44 +01:00
echo '
2019-07-10 15:02:09 +02:00
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">
'.tr('Ricevute da importare').'</span>
</h3>';
// Ricerca automatica
if (Interaction::isEnabled()) {
echo '
<div class="pull-right">
<button type="button" class="btn btn-warning" onclick="importAll(this)">
2019-07-24 17:39:11 +02:00
<i class="fa fa-cloud-download"></i> '.tr('Importa tutte le ricevute').'
2019-07-10 15:02:09 +02:00
</button>
2019-07-10 15:02:09 +02:00
<button type="button" class="btn btn-primary" onclick="search(this)">
<i class="fa fa-refresh"></i> '.tr('Ricerca ricevute').'
</button>
</div>';
}
echo '
</div>
<div class="box-body" id="list">';
if (Interaction::isEnabled()) {
echo '
<p>'.tr('Per vedere le ricevute da importare utilizza il pulsante _BUTTON_', [
'_BUTTON_' => '<i class="fa fa-refresh"></i> <b>'.tr('Ricerca ricevute').'</b>',
2019-07-10 15:02:09 +02:00
]).'.</p>';
} else {
include $structure->filepath('list.php');
}
2018-12-14 12:50:44 +01:00
2019-07-10 15:02:09 +02:00
echo '
</div>
2018-12-14 12:50:44 +01:00
</div>';
echo '
<script>
2019-07-10 15:02:09 +02:00
function search(button) {
var restore = buttonLoading(button);
$("#list").load("'.$structure->fileurl('list.php').'?id_module='.$id_module.'&id_plugin='.$id_plugin.'", function() {
buttonRestore(button, restore);
});
}
function upload(btn) {
if ($("#blob").val()) {
2018-12-14 12:50:44 +01:00
var restore = buttonLoading(btn);
2019-07-10 15:02:09 +02:00
$("#upload").ajaxSubmit({
2018-12-14 12:50:44 +01:00
url: globals.rootdir + "/actions.php",
data: {
2019-07-10 15:02:09 +02:00
op: "save",
2018-12-14 12:50:44 +01:00
id_module: "'.$id_module.'",
id_plugin: "'.$id_plugin.'",
},
type: "POST",
2018-12-14 12:50:44 +01:00
success: function(data){
2019-07-10 15:02:09 +02:00
importMessage(data);
2018-12-14 12:50:44 +01:00
buttonRestore(btn, restore);
},
2019-07-10 15:02:09 +02:00
error: function(xhr) {
alert("'.tr('Errore').': " + xhr.responseJSON.error.message);
2018-12-14 12:50:44 +01:00
buttonRestore(btn, restore);
}
});
2019-07-10 15:02:09 +02:00
} else {
swal({
title: "'.tr('Selezionare un file!').'",
type: "error",
})
}
}
function importMessage(data) {
data = JSON.parse(data);
var ricevuta = "<br>'.tr('Ricevuta').': " + data.file;
2019-07-10 15:02:09 +02:00
if(data.fattura) {
swal({
title: "'.tr('Importazione completata!').'",
html: "'.tr('Fattura aggiornata correttamente').':" + data.fattura + ricevuta,
type: "success",
});
} else {
swal({
title: "'.tr('Importazione fallita!').'",
html: "<i>'.tr('Fattura relativa alla ricevuta non rilevata. Controlla che esista una fattura di vendita corrispondente caricata a gestionale.').'</i>" + ricevuta,
type: "error",
});
2018-12-14 12:50:44 +01:00
}
2019-07-10 15:02:09 +02:00
}
2018-12-14 12:50:44 +01:00
2019-07-10 15:02:09 +02:00
function importAll(btn) {
swal({
title: "'.tr('Importare tutte le ricevute?').'",
html: "'.tr('Importando le ricevute, verranno aggiornati gli stati di invio delle fatture elettroniche. Continuare?').'",
showCancelButton: true,
confirmButtonText: "'.tr('Procedi').'",
type: "info",
}).then(function (result) {
2018-12-14 12:50:44 +01:00
var restore = buttonLoading(btn);
$("#main_loading").show();
2018-12-14 12:50:44 +01:00
$.ajax({
url: globals.rootdir + "/actions.php",
data: {
op: "import",
id_module: "'.$id_module.'",
id_plugin: "'.$id_plugin.'",
},
type: "POST",
2018-12-14 12:50:44 +01:00
success: function(data){
data = JSON.parse(data);
2019-07-24 17:54:50 +02:00
if(data.length == 0){
var html = "'.tr('Non sono state trovate ricevute da importare').'.";
} else {
var html = "'.tr('Sono state elaborate le seguenti ricevute:').'";
2019-07-24 17:54:50 +02:00
data.forEach(function(element) {
var text = "";
if(element.fattura) {
text += element.fattura;
} else {
text += "<i>'.tr('Fattura relativa alla ricevuta non rilevata. Controlla che esista una fattura di vendita corrispondente caricata a gestionale.').'</i>";
}
2019-07-24 17:54:50 +02:00
text += " (" + element.file + ")";
2019-07-24 17:54:50 +02:00
html += "<small><li>" + text + "</li></small>";
});
2019-07-24 17:54:50 +02:00
html += "<br><small>'.tr("Se si sono verificati degli errori durante la procedura e il problema continua a verificarsi, contatta l'assistenza ufficiale").'</small>";
}
$("#list").load("'.$structure->fileurl('list.php').'?id_module='.$id_module.'&id_plugin='.$id_plugin.'", function() {
swal({
title: "'.tr('Operazione completata!').'",
html: html,
type: "info",
});
2020-08-11 13:04:36 +02:00
buttonRestore(btn, restore);
$("#main_loading").fadeOut();
});
2018-12-14 12:50:44 +01:00
},
error: function(data) {
alert("'.tr('Errore').': " + data);
2018-12-14 12:50:44 +01:00
buttonRestore(btn, restore);
}
});
2019-07-10 15:02:09 +02:00
});
}
2018-12-14 12:50:44 +01:00
</script>';