From c69b6844e5bb6e9f32dc51340b315e6e062e87ea Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Mon, 22 Jul 2019 11:35:44 +0200 Subject: [PATCH] Riferimento fatture per note FE --- modules/fatture/row-list.php | 2 +- plugins/importFE/actions.php | 12 ++++---- plugins/importFE/rows.php | 33 +++++++++++++++++++-- plugins/importFE/src/FatturaElettronica.php | 21 +++++++++++-- plugins/importFE/src/Interaction.php | 4 +-- src/HTMLBuilder/Handler/SelectHandler.php | 12 +++++--- 6 files changed, 66 insertions(+), 18 deletions(-) diff --git a/modules/fatture/row-list.php b/modules/fatture/row-list.php index 5d9c1ec59..8c9c77ddc 100644 --- a/modules/fatture/row-list.php +++ b/modules/fatture/row-list.php @@ -152,7 +152,7 @@ foreach ($righe as $row) { ]); echo ' -
'.Modules::link('Fatture di vendita', $record['ref_documento'], $text, $text); +
'.Modules::link($id_module, $record['ref_documento'], $text, $text); } $ref = doc_references($riga, $dir, ['iddocumento']); diff --git a/plugins/importFE/actions.php b/plugins/importFE/actions.php index e16b6cc66..aa980ed8c 100644 --- a/plugins/importFE/actions.php +++ b/plugins/importFE/actions.php @@ -10,12 +10,12 @@ switch (filter('op')) { $content = file_get_contents($_FILES['blob']['tmp_name']); $file = FatturaElettronica::store($_FILES['blob']['name'], $content); - $name = $file; - // no break case 'prepare': - $name = $name ?: get('name'); - $file = Interaction::getImportXML($name); + if (!isset($file)){ + $name = get('name'); + $file = Interaction::getImportXML($name); + } if (FatturaElettronica::isValid($file)) { echo json_encode([ @@ -43,6 +43,7 @@ switch (filter('op')) { 'id_pagamento' => post('pagamento'), 'id_segment' => post('id_segment'), 'id_tipo' => post('id_tipo'), + 'ref_fattura' => post('ref_fattura'), 'data_registrazione' => post('data_registrazione'), 'articoli' => post('articoli'), 'iva' => post('iva'), @@ -67,7 +68,8 @@ switch (filter('op')) { if ($process_result != '') { flash()->error($process_result); redirect(ROOTDIR.'/controller.php?id_module='.$id_module); - exit; + + return; } } diff --git a/plugins/importFE/rows.php b/plugins/importFE/rows.php index 76e213fca..81e25e61d 100644 --- a/plugins/importFE/rows.php +++ b/plugins/importFE/rows.php @@ -65,12 +65,12 @@ echo ' '; // Tipo del documento -$query = 'SELECT id, CONCAT (descrizione, IF((codice_tipo_documento_fe IS NULL), \'\', CONCAT( \' (\', codice_tipo_documento_fe, \')\' ) )) as descrizione FROM co_tipidocumento WHERE dir = \'uscita\''; +$query = 'SELECT id, CONCAT (descrizione, IF((codice_tipo_documento_fe IS NULL), \'\', CONCAT( \' (\', codice_tipo_documento_fe, \')\' ) )) AS descrizione FROM co_tipidocumento WHERE dir = \'uscita\''; if (database()->fetchNum('SELECT id FROM co_tipidocumento WHERE codice_tipo_documento_fe = '.prepare($dati_generali['TipoDocumento']))) { $query .= ' AND codice_tipo_documento_fe = '.prepare($dati_generali['TipoDocumento']); } echo ' -
+
{[ "type": "select", "label": "'.tr('Tipo fattura').'", "name": "id_tipo", "required": 1, "values": "query='.$query.'" ]}
'; @@ -87,7 +87,34 @@ echo '
{[ "type": "date", "label": "'.tr('Data ricezione').'", "name": "data_registrazione", "required": 0, "value": "" ]} -
+
'; + +// Riferimenti ad altre fatture +if (in_array($dati_generali['TipoDocumento'], ['TD04', 'TD05'])){ + $anagrafica = $fattura_pa->saveAnagrafica(); + $query = "SELECT + co_documenti.id, + CONCAT('Fattura num. ', co_documenti.numero_esterno, ' del ', DATE_FORMAT(co_documenti.data, '%d/%m/%Y')) AS descrizione + FROM + co_documenti INNER JOIN co_tipidocumento ON co_tipidocumento.id = co_documenti.idtipodocumento + WHERE + co_tipidocumento.dir = 'uscita' AND + co_documenti.idanagrafica = ".prepare($anagrafica->id); + + echo ' +
+ {[ "type": "select", "label": "'.tr('Fattura collegata').'", "name": "ref_fattura", "required": 1, "values": "query='.$query.'" ]} +
'; +}elseif ($dati_generali['TipoDocumento'] == 'TD06'){ + $anagrafica = $fattura_pa->saveAnagrafica(); + + echo ' +
+ {[ "type": "select", "label": "'.tr('Fattura collegata').'", "name": "ref_fattura", "values": "query='.$query.'" ]} +
'; +} + +echo '
'; // Blocco DatiPagamento è valorizzato (opzionale) diff --git a/plugins/importFE/src/FatturaElettronica.php b/plugins/importFE/src/FatturaElettronica.php index 0dc1dd593..c1328f165 100644 --- a/plugins/importFE/src/FatturaElettronica.php +++ b/plugins/importFE/src/FatturaElettronica.php @@ -178,12 +178,18 @@ class FatturaElettronica } // Registrazione XML come allegato - $filename = Uploads::upload($this->file, array_merge($info, [ + Uploads::upload($this->file, array_merge($info, [ 'name' => tr('Fattura Elettronica'), 'original' => basename($this->file), ])); } + /** + * Restituisce l'anagrafica collegata alla fattura, eventualmente generandola con i dati forniti. + * + * @param string $type + * @return Anagrafica + */ public function saveAnagrafica($type = 'Fornitore') { $info = $this->getAnagrafe(); @@ -267,9 +273,15 @@ class FatturaElettronica /** * Registra la fattura elettronica come fattura del gestionale. * + * @param int $id_pagamento + * @param int $id_sezionale + * @param int $id_tipo + * @param string $data_registrazione + * @param int $ref_fattura + * * @return Fattura */ - public function saveFattura($id_pagamento, $id_sezionale, $id_tipo, $data_registrazione) + public function saveFattura($id_pagamento, $id_sezionale, $id_tipo, $data_registrazione, $ref_fattura = null) { $anagrafica = $this->saveAnagrafica(); @@ -288,6 +300,9 @@ class FatturaElettronica $fattura->numero_esterno = $numero_esterno; $fattura->idpagamento = $id_pagamento; + // Riferimento per nota di credito e debito + $fattura->ref_documento = $ref_fattura ?: null; + // Per il destinatario, la data di ricezione della fattura assume grande rilievo ai fini IVA, poiché determina la decorrenza dei termini per poter esercitare il diritto alla detrazione. // La data di ricezione della fattura è contenuta all’interno della “ricevuta di consegna” visibile al trasmittente della stessa. if (empty($data_registrazione)) { @@ -336,7 +351,7 @@ class FatturaElettronica public function save($info = []) { - $this->saveFattura($info['id_pagamento'], $info['id_segment'], $info['id_tipo'], $info['data_registrazione']); + $this->saveFattura($info['id_pagamento'], $info['id_segment'], $info['id_tipo'], $info['data_registrazione'], $info['ref_fattura']); $this->saveRighe($info['articoli'], $info['iva'], $info['conto'], $info['movimentazione']); diff --git a/plugins/importFE/src/Interaction.php b/plugins/importFE/src/Interaction.php index 5ce48fdca..b745ab500 100644 --- a/plugins/importFE/src/Interaction.php +++ b/plugins/importFE/src/Interaction.php @@ -13,8 +13,6 @@ class Interaction extends Services { public static function listToImport() { - $directory = FatturaElettronica::getImportDirectory(); - $list = []; $names = []; @@ -31,6 +29,8 @@ class Interaction extends Services } // Ricerca fisica + $directory = FatturaElettronica::getImportDirectory(); + $files = glob($directory.'/*.xml*'); foreach ($files as $file) { $name = basename($file); diff --git a/src/HTMLBuilder/Handler/SelectHandler.php b/src/HTMLBuilder/Handler/SelectHandler.php index d0b3c7dfb..8f38ebcce 100644 --- a/src/HTMLBuilder/Handler/SelectHandler.php +++ b/src/HTMLBuilder/Handler/SelectHandler.php @@ -52,13 +52,17 @@ class SelectHandler implements HandlerInterface } // Gestione del select da query specifica (se il campo "values" è impostato a "query=SQL") - elseif (preg_match_all('/^query=(.+?)$/', $values['values'], $matches)) { - $result .= $this->selectQuery($matches[1][0], $values['value']); + elseif (starts_with($values['values'], 'query=')) { + $query = substr($values['values'], strlen('query=')); + + $result .= $this->selectQuery($query, $values['value']); } // Gestione del select dal formato JSON parziale (valori singoli) - elseif (preg_match_all('/^list=(.+?)$/', $values['values'], $matches)) { - $result .= $this->selectList(json_decode('{'.$matches[1][0].'}', true), $values); + elseif (starts_with($values['values'], 'list=')) { + $list = substr($values['values'], strlen('list=')); + + $result .= $this->selectList(json_decode('{'.$list.'}', true), $values); } }