Importazione sequenziale FE

This commit is contained in:
Thomas Zilio 2019-07-24 17:17:54 +02:00
parent 6ebea3510b
commit 5af5cf25e9
13 changed files with 244 additions and 80 deletions

View File

@ -140,7 +140,7 @@ if (empty($record['is_fiscale'])) {
<?php
if ($dir == 'entrata') {
?>
{[ "type": "select", "label": "<?php echo tr('Stato FE'); ?>", "name": "codice_stato_fe", "required": 0, "values": "query=SELECT codice as id, CONCAT_WS(' - ',codice,descrizione) as text FROM fe_stati_documento", "value": "$codice_stato_fe$", "disabled": <?php echo intval(API\Services::isEnabled()); ?>, "class": "unblockable", "help": "<?php echo (!empty($record['data_stato_fe'])) ? Translator::timestampToLocale($record['data_stato_fe']) : ''; ?>" ]}
{[ "type": "select", "label": "<?php echo tr('Stato FE'); ?>", "name": "codice_stato_fe", "required": 0, "values": "query=SELECT codice as id, CONCAT_WS(' - ',codice,descrizione) as text FROM fe_stati_documento", "value": "$codice_stato_fe$", "disabled": <?php echo intval(API\Services::isEnabled()); ?>, "class": "unblockable", "help": "<?php echo (!empty($record['data_stato_fe'])) ? Translator::timestampToLocale($record['data_stato_fe']) : ''; ?>", "disabled": "<?php echo intval($record['stato'] == 'Bozza'); ?>" ]}
<?php
}
?>

View File

@ -25,7 +25,7 @@ switch (filter('op')) {
break;
case 'send':
$result = Interaction::sendXML($id_record);
$result = Interaction::sendInvoice($id_record);
// Aggiornamento dello stato
if ($result['code'] == 200) {

View File

@ -11,7 +11,7 @@ use API\Services;
*/
class Interaction extends Services
{
public static function sendXML($id_record)
public static function sendInvoice($id_record)
{
try {
$fattura = new FatturaElettronica($id_record);

View File

@ -7,6 +7,13 @@ use Plugins\ImportFE\Interaction;
$file = null;
switch (filter('op')) {
case 'list':
$list = Interaction::getRemoteList();
echo json_encode($list);
break;
case 'save':
$content = file_get_contents($_FILES['blob']['tmp_name']);
$file = FatturaElettronica::store($_FILES['blob']['name'], $content);
@ -15,12 +22,12 @@ switch (filter('op')) {
case 'prepare':
if (!isset($file)) {
$name = filter('name');
$file = Interaction::getImportXML($name);
$file = Interaction::getInvoiceFile($name);
}
if (FatturaElettronica::isValid($file)) {
// Individuazione ID fisico
$files = Interaction::fileToImport();
$files = Interaction::getFileList();
foreach ($files as $key => $value) {
if ($value['name'] == $file) {
$index = $key;
@ -63,19 +70,19 @@ switch (filter('op')) {
];
$fattura_pa = FatturaElettronica::manage($filename);
$id_record = $fattura_pa->save($info);
$id_fattura = $fattura_pa->save($info);
ricalcola_costiagg_fattura($id_record);
elimina_scadenza($id_record);
elimina_movimento($id_record, 0);
aggiungi_scadenza($id_record);
aggiungi_movimento($id_record, 'uscita');
ricalcola_costiagg_fattura($id_fattura);
elimina_scadenza($id_fattura);
elimina_movimento($id_fattura, 0);
aggiungi_scadenza($id_fattura);
aggiungi_movimento($id_fattura, 'uscita');
$fattura_pa->delete();
// Processo il file ricevuto
if (Interaction::isEnabled()) {
$process_result = Interaction::processXML($filename);
$process_result = Interaction::processInvoice($filename);
if ($process_result != '') {
flash()->error($process_result);
redirect(ROOTDIR.'/controller.php?id_module='.$id_module);
@ -84,12 +91,17 @@ switch (filter('op')) {
}
}
redirect(ROOTDIR.'/editor.php?id_module='.$id_module.'&id_record='.$id_record);
break;
case 'list':
include __DIR__.'/rows.php';
$files = Interaction::getFileList();
$file = $files[$id_record - 1];
if (get('sequence') == null) {
redirect(ROOTDIR.'/editor.php?id_module='.$id_module.'&id_record='.$id_fattura);
} elseif (!empty($file)) {
redirect(ROOTDIR.'/editor.php?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record='.$id_record.'&sequence=1');
} else {
flash()->info(tr('Tutte le fatture salvate sono state importate!'));
redirect(ROOTDIR.'/controller.php?id_module='.$id_module);
}
break;
case 'process':
@ -97,7 +109,7 @@ switch (filter('op')) {
// Processo il file ricevuto
if (Interaction::isEnabled()) {
$process_result = Interaction::processXML($name);
$process_result = Interaction::processInvoice($name);
if (!empty($process_result)) {
flash()->error($process_result);
}

View File

@ -4,7 +4,7 @@ include_once __DIR__.'/../../core.php';
use Plugins\ImportFE\Interaction;
if (!empty($fattura_pa)) {
if (!empty($record)) {
include $structure->filepath('generate.php');
return;
@ -93,17 +93,23 @@ echo '
<div class="box-header with-border">
<h3 class="box-title">
'.tr('Fatture da importare').'</span>
</h3>';
</h3>
<div class="pull-right">
<button type="button" class="btn btn-warning" onclick="importAll(this)">
<i class="fa fa-download"></i> '.tr('Importa in sequenza').'
</button>';
// Ricerca automatica
if (Interaction::isEnabled()) {
echo '
<button type="button" class="btn btn-primary pull-right" onclick="search(this)">
<i class="fa fa-refresh"></i> '.tr('Ricerca fatture di acquisto').'
</button>';
<button type="button" class="btn btn-primary" onclick="search(this)">
<i class="fa fa-refresh"></i> '.tr('Ricerca fatture di acquisto').'
</button>';
}
echo '
</div>
</div>
<div class="box-body" id="list">';
@ -125,13 +131,91 @@ if (Interaction::isEnabled()) {
$(document).ready(function() {
$($.fn.dataTable.tables(true)).DataTable().columns.adjust();
$($.fn.dataTable.tables(true)).DataTable().scroller.measure();
});
});';
function search(button) {
var restore = buttonLoading(button);
if (Interaction::isEnabled()) {
echo '
function importAll(btn) {
swal({
title: "'.tr('Importare tutte le fatture?').'",
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?').'",
showCancelButton: true,
confirmButtonText: "'.tr('Procedi').'",
type: "info",
}).then(function (result) {
var restore = buttonLoading(btn);
$("#main_loading").show();
$("#list").load("'.$structure->fileurl('list.php').'?id_module='.$id_module.'&id_plugin='.$id_plugin.'", function() {
buttonRestore(button, restore);
$.ajax({
url: globals.rootdir + "/actions.php",
data: {
op: "list",
id_module: "'.$id_module.'",
id_plugin: "'.$id_plugin.'",
},
type: "post",
success: function(data){
data = JSON.parse(data);
count = data.length;
counter = 0;
data.forEach(function(element) {
$.ajax({
url: globals.rootdir + "/actions.php",
type: "get",
data: {
id_module: "'.$id_module.'",
id_plugin: "'.$id_plugin.'",
op: "prepare",
name: element.name,
},
success: function(data) {
counter ++;
importComplete(count, counter, btn, restore);
},
error: function(data) {
counter ++;
importComplete(count, counter, btn, restore);
}
});
});
importComplete(count, counter, btn, restore);
},
error: function(data) {
alert("'.tr('Errore').': " + data);
$("#main_loading").fadeOut();
buttonRestore(btn, restore);
}
});
});
}
function importComplete(count, counter, btn, restore) {
if(counter == count){
$("#main_loading").fadeOut();
buttonRestore(btn, restore);
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=1&sequence=1");
}
}';
} else {
echo '
function importAll(btn) {
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=1&sequence=1");
}';
}
echo '
function search(btn) {
var restore = buttonLoading(btn);
$("#list").load("'.$structure->fileurl('list.php').'?id_module='.$id_module.'&id_plugin='.$id_plugin.'", function() {
buttonRestore(btn, restore);
});
}
</script>';

View File

@ -2,6 +2,80 @@
include_once __DIR__.'/../../core.php';
echo '
<script>
$(document).ready(function() {
$("#save").hide();
});
</script>';
$skip_link = $has_next ? ROOTDIR.'/editor.php?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record='.($id_record + 1).'&sequence='.get('sequence') : ROOTDIR.'/editor.php?id_module='.$id_module;
if (empty($fattura_pa)) {
if (!empty($error)) {
echo '
<p>'.tr("Errore durante l'apertura della fattura elettronica _NAME_", [
'_NAME_' => $record['name'],
]).'.</p>';
} elseif (!empty($imported)) {
echo '
<p>'.tr('La fattura elettrnica _NAME_ è già stata importata in passato', [
'_NAME_' => $record['name'],
]).'.</p>';
}
echo '
<div class="row">
<div class="col-md-12 text-right">';
if (!empty($imported)) {
echo '
<button type="button" class="btn btn-danger" onclick="cleanup()">
<i class="fa fa-trash-o"></i> '.tr('Processa e rimuovi').'
</button>';
}
echo '
<button type="button" class="btn btn-warning" onclick="skip()">
<i class="fa fa-ban "></i> '.tr('Salta fattura').'
</button>
</div>
</div>
<script>
function skip() {
redirect("'.$skip_link.'");
}
function cleanup(){
$.ajax({
url: globals.rootdir + "/actions.php",
type: "get",
data: {
id_module: "'.$id_module.'",
id_plugin: "'.$id_plugin.'",
op: "delete",
name: "'.$record['name'].'",
}
});
$.ajax({
url: globals.rootdir + "/actions.php",
type: "get",
data: {
id_module: "'.$id_module.'",
id_plugin: "'.$id_plugin.'",
op: "process",
name: "'.$record['name'].'",
}
});
skip();
}
</script>';
return;
}
// Fornitore
$fornitore = $fattura_pa->getAnagrafe();
@ -27,13 +101,8 @@ $metodi = isset($metodi[0]) ? $metodi : [$metodi];
$codice_modalita_pagamento = $metodi[0]['ModalitaPagamento'];
echo '
<form action="'.$rootdir.'/actions.php" method="post">
<input type="hidden" name="id_module" value="'.$id_module.'">
<input type="hidden" name="id_plugin" value="'.$id_plugin.'">
<input type="hidden" name="filename" value="'.$filename.'">
<input type="hidden" name="id_segment" value="'.get('id_segment').'">
<input type="hidden" name="id" value="'.get('id').'">
<input type="hidden" name="backto" value="record-edit">
<form action="" method="post">
<input type="hidden" name="filename" value="'.$record['name'].'">
<input type="hidden" name="op" value="generate">
<div class="row">
@ -55,7 +124,7 @@ echo '
<h4>
'.$dati_generali['Numero'].'
<a href="'.$structure->fileurl('view.php').'?filename='.$filename.'" class="btn btn-info btn-xs" target="_blank" >
<a href="'.$structure->fileurl('view.php').'?filename='.$record['name'].'" class="btn btn-info btn-xs" target="_blank" >
<i class="fa fa-eye"></i> '.tr('Visualizza').'
</a>
@ -116,13 +185,13 @@ echo '
// Sezionale
echo '
<div class="col-md-3">
{[ "type": "select", "label": "'.tr('Sezionale').'", "name": "id_segment", "required": 1, "values": "query=SELECT id, name AS descrizione FROM zz_segments WHERE id_module='.$id_module.' ORDER BY name", "value": "'.$_SESSION['module_'.$id_module]['id_segment'].'" ]}
{[ "type": "select", "label": "'.tr('Sezionale').'", "name": "id_segment", "required": 1, "values": "query=SELECT id, name AS descrizione FROM zz_segments WHERE is_fiscale = 1 AND id_module='.$id_module.' ORDER BY name", "value": "'.$_SESSION['module_'.$id_module]['id_segment'].'" ]}
</div>';
// Data di registrazione
echo '
<div class="col-md-3">
{[ "type": "date", "label": "'.tr('Data di registrazione').'", "name": "data_registrazione", "required": 1, "value": "'.get('data_registrazione').'", "max-date": "-now-", "min-date": "'.$dati_generali['Data'].'", "readonly": "'.(intval(get('data_registrazione') != null)).'" ]}
{[ "type": "date", "label": "'.tr('Data di registrazione').'", "name": "data_registrazione", "required": 1, "value": "'.(get('data_registrazione') ?: $dati_generali['Data']).'", "max-date": "-now-", "min-date": "'.$dati_generali['Data'].'", "readonly": "'.(intval(get('data_registrazione') != null)).'" ]}
</div>';
if (!empty($anagrafica)) {
@ -304,18 +373,13 @@ if (!empty($righe)) {
echo '
<div class="row">
<div class="col-md-12 text-right">
<a href="'.$skip_link.'" class="btn btn-warning">
<i class="fa fa-ban "></i> '.tr('Salta fattura').'
</a>
<button type="submit" class="btn btn-primary">
<i class="fa fa-arrow-right"></i> '.tr('Continua').'...
</button>
</div>
</div>
</form>';
echo '
<script>
$(document).ready(function() {
$("#save").hide();
});
</script>
<script src="'.$rootdir.'/lib/init.js"></script>';

View File

@ -6,16 +6,17 @@ use Plugins\ImportFE\FatturaElettronica;
use Plugins\ImportFE\Interaction;
if (isset($id_record)) {
$files = Interaction::fileToImport();
$file = $files[$id_record - 1];
$files = Interaction::getFileList();
$record = $files[$id_record - 1];
$filename = $file['name'];
$has_next = isset($files[$id_record]);
try {
$fattura_pa = FatturaElettronica::manage($filename);
$fattura_pa = FatturaElettronica::manage($record['name']);
$anagrafica = $fattura_pa->findAnagrafica();
$record = $file;
} catch (UnexpectedValueException $e) {
$imported = true;
} catch (Exception $e) {
$error = true;
}
}

View File

@ -3,12 +3,8 @@
include_once __DIR__.'/../../core.php';
use Plugins\ImportFE\Interaction;
use Plugins\ImportFE\InvoiceHook;
$list = Interaction::listToImport();
// Aggiornamento cache hook
InvoiceHook::update($list);
$list = Interaction::getInvoiceList();
$directory = Plugins\ImportFE\FatturaElettronica::getImportDirectory();
@ -124,7 +120,7 @@ function download(button, file, data_registrazione) {
data = JSON.parse(data);
if (!data.already) {
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=" + data.id);
redirect(globals.rootdir + "/editor.php?id_module=" + globals.id_module + "&id_plugin=" + '.$id_plugin.' + "&id_record=" + data.id + "&data_registrazione=" + data_registrazione);
} else {
swal({
title: "'.tr('Fattura già importata.').'",

View File

@ -11,11 +11,24 @@ use API\Services;
*/
class Interaction extends Services
{
public static function listToImport()
public static function getInvoiceList()
{
$list = [];
$names = [];
$list = self::getRemoteList();
// Ricerca fisica
$names = array_column($list, 'name');
$files = self::getFileList($names);
$list = array_merge($list, $files);
// Aggiornamento cache hook
InvoiceHook::update($list);
return $list;
}
public static function getRemoteList()
{
// Ricerca da remoto
if (self::isEnabled()) {
$response = static::request('POST', 'fatture_da_importare');
@ -23,20 +36,13 @@ class Interaction extends Services
if ($body['status'] == '200') {
$list = $body['results'];
$names = array_column($list);
}
}
// Ricerca fisica
$files = self::fileToImport($names);
$list = array_merge($list, $files);
return $list;
return $list ?: [];
}
public static function fileToImport($names = [])
public static function getFileList($names = [])
{
$list = [];
@ -57,7 +63,7 @@ class Interaction extends Services
return $list;
}
public static function getImportXML($name)
public static function getInvoiceFile($name)
{
$directory = FatturaElettronica::getImportDirectory();
$file = $directory.'/'.$name;
@ -74,7 +80,7 @@ class Interaction extends Services
return $name;
}
public static function processXML($filename)
public static function processInvoice($filename)
{
$response = static::request('POST', 'fattura_xml_salvata', [
'filename' => $filename,

View File

@ -9,7 +9,7 @@ class InvoiceHook extends HookManager
{
public function manage()
{
$list = Interaction::listToImport();
$list = Interaction::getInvoiceList();
return $list;
}

View File

@ -3,14 +3,10 @@
include_once __DIR__.'/../../core.php';
use Plugins\ReceiptFE\Interaction;
use Plugins\ReceiptFE\ReceiptHook;
use Plugins\ReceiptFE\Ricevuta;
$list = Interaction::getReceiptList();
// Aggiornamento cache hook
ReceiptHook::update($list);
$directory = Ricevuta::getImportDirectory();
if (!empty($list)) {

View File

@ -36,7 +36,12 @@ class Interaction extends Services
}
}
return array_clean($list);
$list = array_clean($list);
// Aggiornamento cache hook
ReceiptHook::update($list);
return $list;
}
public static function getReceipt($name)

View File

@ -152,7 +152,7 @@ INSERT INTO `zz_settings` (`id`, `nome`, `valore`, `tipo`, `editable`, `sezione`
DELETE FROM `zz_settings` WHERE `nome` = 'apilayer API key for Email';
-- Fatture pro-forma di acquisto
INSERT INTO `zz_segments` (`id`, `id_module`, `name`, `clause`, `position`, `pattern`, `is_fiscale`) VALUES (NULL, '15', 'Fatture pro-forma', '1=1', 'WHR', 'PRO-###', '1');
INSERT INTO `zz_segments` (`id`, `id_module`, `name`, `clause`, `position`, `pattern`, `is_fiscale`) VALUES (NULL, '15', 'Fatture pro-forma', '1=1', 'WHR', 'PRO-###', '0');
-- Aggiunto codice cig e codice cup per ddt
ALTER TABLE `dt_ddt` ADD `codice_cig` VARCHAR(15), ADD `codice_cup` VARCHAR(15) AFTER `codice_cig`, ADD `id_documento_fe` VARCHAR(20) AFTER `codice_cup`,ADD `num_item` VARCHAR(15) AFTER `id_documento_fe`;