Fix ProgressivoInvio

This commit is contained in:
Thomas Zilio 2018-11-16 10:41:21 +01:00
parent b7956fe664
commit 561045be04
2 changed files with 41 additions and 32 deletions

View File

@ -14,7 +14,11 @@ switch (filter('op')) {
flash()->info(tr('Fattura elettronica generata correttamente!')); flash()->info(tr('Fattura elettronica generata correttamente!'));
if (!$fattura_pa->isValid()) { if (!$fattura_pa->isValid()) {
flash()->warning(tr('La fattura elettronica potrebbe avere delle irregolarità!')); $errors = $fattura_pa->getErrors();
flash()->warning(tr('La fattura elettronica potrebbe avere delle irregolarità!').' '.tr('Controllare i seguenti campi: _LIST_', [
'_LIST_' => implode(', ', $errors),
]).'.');
} }
} else { } else {
flash()->error(tr('Impossibile generare la fattura elettronica')); flash()->error(tr('Impossibile generare la fattura elettronica'));

View File

@ -34,11 +34,12 @@ class FatturaElettronica
/** @var array Righe del documento */ /** @var array Righe del documento */
protected $righe = []; protected $righe = [];
/** @var array Stato di validazione interna dell'XML della fattura */
protected $is_valid = null;
/** @var array XML della fattura */ /** @var array XML della fattura */
protected $xml = null; protected $xml = null;
/** @var array Irregolarità nella fattura XML */
protected $errors = null;
public function __construct($id_documento) public function __construct($id_documento)
{ {
$database = database(); $database = database();
@ -148,11 +149,21 @@ class FatturaElettronica
*/ */
public function isValid() public function isValid()
{ {
if (empty($this->is_valid)) { return empty($this->getErrors());
}
/**
* Restituisce l'elenco delle irregolarità interne all'XML della fattura.
*
* @return bool
*/
public function getErrors()
{
if (!isset($this->errors)) {
$this->toXML(); $this->toXML();
} }
return $this->is_valid; return $this->errors;
} }
/** /**
@ -191,7 +202,7 @@ class FatturaElettronica
} }
// Inizializzazione PEC solo se anagrafica azienda e codice destinatario non compilato, per privato e PA la PEC non serve // Inizializzazione PEC solo se anagrafica azienda e codice destinatario non compilato, per privato e PA la PEC non serve
if (empty($cliente['codice_destinatario']) and $cliente['tipo'] == 'Azienda' ){ if (empty($cliente['codice_destinatario']) && $cliente['tipo'] == 'Azienda') {
$result['PECDestinatario'] = $cliente['pec']; $result['PECDestinatario'] = $cliente['pec'];
} }
@ -431,12 +442,12 @@ class FatturaElettronica
$result = []; $result = [];
foreach ($contratti as $contratto) { foreach ($contratti as $contratto) {
if (!empty($contratto['id_documento_fe'])) { if (!empty($contratto['id_documento_fe'])) {
$dati_contratto = [ $dati_contratto = [
'IdDocumento' => $contratto['id_documento_fe'], 'IdDocumento' => $contratto['id_documento_fe'],
]; ];
} }
if (!empty($contratto['codice_cig'])) { if (!empty($contratto['codice_cig'])) {
$dati_contratto['CodiceCIG'] = $contratto['codice_cig']; $dati_contratto['CodiceCIG'] = $contratto['codice_cig'];
} }
@ -468,7 +479,6 @@ class FatturaElettronica
// Controllo le le righe per la fatturazione di contratti // Controllo le le righe per la fatturazione di contratti
$dati_contratti = static::getDatiContratto($fattura); $dati_contratti = static::getDatiContratto($fattura);
if (!empty($dati_contratti)) { if (!empty($dati_contratti)) {
foreach ($dati_contratti as $dato) { foreach ($dati_contratti as $dato) {
if (!empty($dato)) { if (!empty($dato)) {
$result[] = [ $result[] = [
@ -796,16 +806,11 @@ class FatturaElettronica
if (!empty($validator)) { if (!empty($validator)) {
$validation = $validator->validate($output); $validation = $validator->validate($output);
$this->is_valid &= $validation; // Segnalazione dell'irregolarità
$errors = array();
if (!intval($validation)) { if (!intval($validation)) {
$_SESSION['warnings'][] = $key; $this->errors[] = $key;
} }
// Per debug
//flash()->warning($key.': '.intval($validation));
} }
} }
return $output; return $output;
@ -844,7 +849,7 @@ class FatturaElettronica
public function save($directory) public function save($directory)
{ {
// Generazione nome XML // Generazione nome XML
$filename = $this->getFilename(); $filename = $this->getFilename(true);
// Salvataggio del file // Salvataggio del file
$file = rtrim($directory, '/').'/'.$filename; $file = rtrim($directory, '/').'/'.$filename;
@ -884,12 +889,12 @@ class FatturaElettronica
* *
* @return string * @return string
*/ */
public function getFilename() public function getFilename($new = false)
{ {
$azienda = static::getAzienda(); $azienda = static::getAzienda();
$codice = 'IT'.(empty($azienda['piva']) ? $azienda['codice_fiscale'] : $azienda['piva']); $prefix = 'IT'.(empty($azienda['piva']) ? $azienda['codice_fiscale'] : $azienda['piva']);
if (empty($this->documento['codice_xml'])) { if (empty($this->documento['codice_xml']) || !empty($new)) {
$database = database(); $database = database();
do { do {
@ -901,7 +906,7 @@ class FatturaElettronica
$this->documento['codice_xml'] = $code; $this->documento['codice_xml'] = $code;
} }
return $codice.'_'.$this->documento['codice_xml'].'.xml'; return $prefix.'_'.$this->documento['codice_xml'].'.xml';
} }
/** /**
@ -912,7 +917,7 @@ class FatturaElettronica
public function toXML() public function toXML()
{ {
if (empty($this->xml)) { if (empty($this->xml)) {
$this->is_valid = true; $this->errors = [];
$cliente = $this->getCliente(); $cliente = $this->getCliente();