2019-04-19 03:18:05 +02: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 />.
*/
2019-04-19 03:18:05 +02:00
namespace Plugins\ImportFE ;
2021-07-28 11:50:02 +02:00
use Common\Components\Component ;
2021-02-18 18:48:44 +01:00
use Modules\Anagrafiche\Anagrafica ;
2019-04-19 03:18:05 +02:00
use Modules\Articoli\Articolo as ArticoloOriginale ;
2019-10-18 16:40:15 +02:00
use Modules\Articoli\Categoria ;
2019-04-19 03:18:05 +02:00
use Modules\Fatture\Components\Articolo ;
2021-11-30 11:09:16 +01:00
use Modules\Fatture\Components\Descrizione ;
2019-04-19 03:18:05 +02:00
use Modules\Fatture\Components\Riga ;
use Modules\Fatture\Fattura ;
2021-07-28 12:06:29 +02:00
use Plugins\ListinoClienti\DettaglioPrezzo ;
2019-04-19 03:18:05 +02:00
use Util\XML ;
/**
* Classe per la gestione della fatturazione elettronica in XML .
*
* @ since 2.4 . 2
*/
class FatturaOrdinaria extends FatturaElettronica
{
public function __construct ( $name )
{
parent :: __construct ( $name );
if ( $this -> getHeader ()[ 'DatiTrasmissione' ][ 'FormatoTrasmissione' ] == 'FSM10' ) {
2024-01-15 15:30:45 +01:00
throw new \UnexpectedValueException ();
2019-04-19 03:18:05 +02:00
}
}
public function getAnagrafe ()
{
$dati = $this -> getHeader ()[ 'CedentePrestatore' ];
$anagrafe = $dati [ 'DatiAnagrafici' ];
$rea = $dati [ 'IscrizioneREA' ];
$sede = $dati [ 'Sede' ];
$contatti = $dati [ 'Contatti' ];
$info = [
'partita_iva' => $anagrafe [ 'IdFiscaleIVA' ][ 'IdCodice' ],
'codice_fiscale' => $anagrafe [ 'CodiceFiscale' ],
'ragione_sociale' => $anagrafe [ 'Anagrafica' ][ 'Denominazione' ],
'nome' => $anagrafe [ 'Anagrafica' ][ 'Nome' ],
'cognome' => $anagrafe [ 'Anagrafica' ][ 'Cognome' ],
'rea' => [
2022-12-02 19:26:32 +01:00
'codice' => ( ! empty ( $dati [ 'IscrizioneREA' ]) ? $rea [ 'Ufficio' ] . '-' . $rea [ 'NumeroREA' ] : '' ),
2019-04-19 03:18:05 +02:00
'capitale_sociale' => $rea [ 'CapitaleSociale' ],
],
'sede' => [
2020-06-30 13:33:42 +02:00
'indirizzo' => $sede [ 'Indirizzo' ] . ' ' . $sede [ 'NumeroCivico' ],
2019-04-19 03:18:05 +02:00
'cap' => $sede [ 'CAP' ],
'citta' => $sede [ 'Comune' ],
'provincia' => $sede [ 'Provincia' ],
'nazione' => $sede [ 'Nazione' ],
],
'contatti' => [
'telefono' => $contatti [ 'Telefono' ],
'fax' => $contatti [ 'Fax' ],
'email' => $contatti [ 'email' ],
],
];
return $info ;
}
public function getRighe ()
{
2023-07-31 17:28:46 +02:00
$linee = $this -> getBody ()[ 'DatiBeniServizi' ][ 'DettaglioLinee' ];
$linee = $this -> forceArray ( $linee );
$cassa_previdenziale = $this -> getBody ()[ 'DatiGenerali' ][ 'DatiGeneraliDocumento' ][ 'DatiCassaPrevidenziale' ];
2023-06-30 17:35:56 +02:00
2023-07-31 17:28:46 +02:00
$imponibile = [];
$totale_imposta = [];
2023-12-18 12:26:44 +01:00
$importo = 0 ;
2023-08-04 14:54:28 +02:00
foreach ( $linee as $linea ) {
2023-12-18 12:26:44 +01:00
$importo = $linea [ 'PrezzoUnitario' ] * $linea [ 'Quantita' ];
if ( $linea [ 'ScontoMaggiorazione' ]) {
$linea [ 'ScontoMaggiorazione' ] = $this -> forceArray ( $linea [ 'ScontoMaggiorazione' ]);
2024-01-04 14:47:06 +01:00
foreach ( $linea [ 'ScontoMaggiorazione' ] as $sm ) {
if ( isset ( $sm [ 'Percentuale' ])) {
2023-12-18 12:26:44 +01:00
$sconto = ( $importo * $sm [ 'Percentuale' ] / 100 );
if ( $sm [ 'Tipo' ] == 'SC' ) {
$importo -= $sconto ;
} else {
$importo += $sconto ;
}
2024-01-04 14:47:06 +01:00
} elseif ( isset ( $sm [ 'Importo' ])) {
2023-12-18 12:26:44 +01:00
if ( $sm [ 'Tipo' ] == 'SC' ) {
$importo -= $sm [ 'Importo' ];
} else {
$importo += $sm [ 'Importo' ];
}
}
}
}
2024-01-04 14:47:06 +01:00
2023-12-18 12:26:44 +01:00
if ( ! $linea [ 'Quantita' ]) {
2024-01-04 14:47:06 +01:00
$importo = $linea [ 'PrezzoUnitario' ];
}
2023-12-18 12:26:44 +01:00
$imponibile [ $linea [ 'AliquotaIVA' ]] = ( $imponibile [ $linea [ 'AliquotaIVA' ]] ? ? 0 ) + round ( $importo , 2 );
2023-12-15 17:29:06 +01:00
}
// Aggiunta degli arrotondamenti IVA come righe indipendenti
$riepiloghi = $this -> getBody ()[ 'DatiBeniServizi' ][ 'DatiRiepilogo' ];
$riepiloghi = $this -> forceArray ( $riepiloghi );
foreach ( $riepiloghi as $riepilogo ) {
$aliquota_iva = $riepilogo [ 'AliquotaIVA' ];
2024-01-04 14:47:06 +01:00
$imponibile [ $aliquota_iva ] += ( float ) $riepilogo [ 'Arrotondamento' ];
2023-06-30 13:08:21 +02:00
}
2023-07-31 17:28:46 +02:00
foreach ( $imponibile as $aliquota_iva => $importo ) {
2023-08-04 14:54:28 +02:00
$totale_imposta [ $aliquota_iva ] = $imponibile [ $aliquota_iva ] * $aliquota_iva / 100 ;
if ( $cassa_previdenziale [ 'AliquotaIVA' ] == $aliquota_iva ) {
$totale_imposta [ $aliquota_iva ] += $cassa_previdenziale [ 'ImportoContributoCassa' ] * $cassa_previdenziale [ 'AliquotaIVA' ] / 100 ;
2023-07-31 17:28:46 +02:00
}
}
2023-11-16 11:42:07 +01:00
$riepiloghi_raggruppati = [];
2021-01-08 14:35:53 +01:00
foreach ( $riepiloghi as $riepilogo ) {
2023-11-16 11:42:07 +01:00
$aliquota_iva = $riepilogo [ 'AliquotaIVA' ];
2023-11-17 17:08:32 +01:00
2023-11-16 11:42:07 +01:00
if ( array_key_exists ( $aliquota_iva , $riepiloghi_raggruppati )) {
$riepiloghi_raggruppati [ $aliquota_iva ][ 'ImponibileImporto' ] += $riepilogo [ 'ImponibileImporto' ];
$riepiloghi_raggruppati [ $aliquota_iva ][ 'Imposta' ] += $riepilogo [ 'Imposta' ];
} else {
$riepiloghi_raggruppati [ $aliquota_iva ] = $riepilogo ;
}
}
foreach ( $riepiloghi_raggruppati as $riepilogo ) {
2023-07-31 17:28:46 +02:00
$valore = 0 ;
2024-01-04 14:47:06 +01:00
$diff_iva = round (( float ) $riepilogo [ 'Imposta' ] - $totale_imposta [ $riepilogo [ 'AliquotaIVA' ]], 2 );
2023-12-15 17:29:06 +01:00
if ( $diff_iva ) {
$valore = $diff_iva * 100 / $riepilogo [ 'AliquotaIVA' ];
2024-01-04 14:47:06 +01:00
}
2023-08-04 14:54:28 +02:00
2023-07-31 17:28:46 +02:00
if ( $valore != 0 ) {
2019-05-04 00:32:28 +02:00
$descrizione = tr ( 'Arrotondamento IVA _VALUE_' , [
'_VALUE_' => empty ( $riepilogo [ 'Natura' ]) ? numberFormat ( $riepilogo [ 'AliquotaIVA' ]) . '%' : $riepilogo [ 'Natura' ],
]);
2023-07-31 17:28:46 +02:00
$linee [] = [
2019-05-04 00:32:28 +02:00
'Descrizione' => $descrizione ,
2020-12-04 17:40:25 +01:00
'PrezzoUnitario' => $valore ,
2019-05-04 00:32:28 +02:00
'Quantita' => 1 ,
'AliquotaIVA' => $riepilogo [ 'AliquotaIVA' ],
'Natura' => $riepilogo [ 'Natura' ],
];
}
}
2023-07-31 17:28:46 +02:00
return $this -> forceArray ( $linee );
2019-04-19 03:18:05 +02:00
}
2023-09-28 12:31:03 +02:00
public function saveRighe ( $articoli , $iva , $conto , $movimentazione = true , $crea_articoli = [], $tipi_riferimenti = [], $id_riferimenti = [], $tipi_riferimenti_vendita = [], $id_riferimenti_vendita = [], $update_info = [], $serials = [])
2019-04-19 03:18:05 +02:00
{
2019-07-23 18:29:40 +02:00
$info = $this -> getRitenutaRivalsa ();
2019-07-24 10:59:40 +02:00
$righe = $this -> getRighe ();
$fattura = $this -> getFattura ();
2021-09-28 15:51:11 +02:00
$anagrafica = Anagrafica :: find ( $fattura -> idanagrafica );
$direzione = 'uscita' ;
2019-07-23 18:29:40 +02:00
$id_ritenuta_acconto = $info [ 'id_ritenuta_acconto' ];
$id_rivalsa = $info [ 'id_rivalsa' ];
$calcolo_ritenuta_acconto = $info [ 'rivalsa_in_ritenuta' ] ? 'IMP+RIV' : 'IMP' ;
2019-07-24 10:59:40 +02:00
$ritenuta_contributi = ! empty ( $fattura -> id_ritenuta_contributi );
2021-11-30 11:09:16 +01:00
$conto_arrotondamenti = null ;
2019-04-19 03:18:05 +02:00
2023-12-15 17:29:06 +01:00
$tot_arr = 0 ;
foreach ( $righe as $riga ) {
if ( strpos ( $riga [ 'Descrizione' ], 'Arrotondamento' ) !== false ) {
$tot_arr += $riga [ 'PrezzoUnitario' ];
}
}
2022-03-04 11:50:57 +01:00
// Disattivo temporaneamente l'impostazione per evadere solo le quantità previste
$original_setting_evasione = setting ( 'Permetti il superamento della soglia quantità dei documenti di origine' );
\Settings :: setValue ( 'Permetti il superamento della soglia quantità dei documenti di origine' , 1 );
2019-04-19 03:18:05 +02:00
foreach ( $righe as $key => $riga ) {
$articolo = ArticoloOriginale :: find ( $articoli [ $key ]);
$riga [ 'PrezzoUnitario' ] = floatval ( $riga [ 'PrezzoUnitario' ]);
$riga [ 'Quantita' ] = floatval ( $riga [ 'Quantita' ]);
2021-11-30 11:09:16 +01:00
$is_descrizione = empty ( $riga [ 'Quantita' ]) && empty ( $riga [ 'PrezzoUnitario' ]);
2019-10-18 16:40:15 +02:00
$codici = $riga [ 'CodiceArticolo' ] ? : [];
$codici = ! empty ( $codici ) && ! isset ( $codici [ 0 ]) ? [ $codici ] : $codici ;
// Creazione articolo relativo
2022-05-16 11:14:17 +02:00
if ( ! empty ( $codici ) && ! empty ( $crea_articoli [ $key ]) && empty ( $articolo )) {
2019-10-18 16:40:15 +02:00
$codice = $codici [ 0 ][ 'CodiceValore' ];
2019-10-18 16:43:28 +02:00
$articolo = ArticoloOriginale :: where ( 'codice' , $codice ) -> first ();
if ( empty ( $articolo )) {
$nome_categoria = 'Importazione automatica' ;
2024-03-28 16:38:03 +01:00
$categoria = Categoria :: find (( new Categoria ()) -> getByField ( 'name' , strtolower ( $nome_categoria )));
2019-10-18 16:43:28 +02:00
if ( empty ( $categoria )) {
2024-03-27 10:37:57 +01:00
$categoria = Categoria :: build ();
$categoria -> setTranslation ( 'nome' , $nome_categoria );
$categoria -> save ();
2019-10-18 16:43:28 +02:00
}
2024-03-19 18:18:11 +01:00
$articolo = ArticoloOriginale :: build ( $codice , $categoria );
$articolo -> setTranslation ( 'descrizione' , $riga [ 'Descrizione' ]);
2021-05-28 17:51:07 +02:00
$articolo -> um = $riga [ 'UnitaMisura' ];
2021-07-07 07:57:10 +02:00
$articolo -> idconto_acquisto = $conto [ $key ];
2023-05-29 10:53:56 +02:00
$articolo -> abilita_serial = setting ( 'Serial number abilitato di default' );
2019-10-18 16:43:28 +02:00
$articolo -> save ();
}
2019-10-18 16:40:15 +02:00
}
2019-04-19 03:18:05 +02:00
if ( ! empty ( $articolo )) {
2021-09-23 13:03:01 +02:00
$articolo -> idconto_acquisto = $conto [ $key ];
$articolo -> save ();
2021-10-18 10:23:39 +02:00
2019-04-19 03:18:05 +02:00
$obj = Articolo :: build ( $fattura , $articolo );
$obj -> movimentazione ( $movimentazione );
2021-02-18 17:46:38 +01:00
2021-09-13 09:47:40 +02:00
$target_type = Articolo :: class ;
2023-08-04 14:54:28 +02:00
} elseif ( $is_descrizione ) {
2021-11-30 11:09:16 +01:00
$obj = Descrizione :: build ( $fattura );
$target_type = Descrizione :: class ;
2019-04-19 03:18:05 +02:00
} else {
$obj = Riga :: build ( $fattura );
2021-02-18 17:46:38 +01:00
2021-09-13 09:47:40 +02:00
$target_type = Riga :: class ;
2019-04-19 03:18:05 +02:00
}
2020-09-16 09:14:00 +02:00
$obj -> descrizione = $riga [ 'Descrizione' ];
2023-11-10 16:38:48 +01:00
$obj -> save ();
2020-09-16 09:14:00 +02:00
2020-02-24 18:30:05 +01:00
// Collegamento al documento di riferimento
2023-11-10 16:38:48 +01:00
$has_serial_riferimento = false ;
2022-03-04 11:50:57 +01:00
if ( ! empty ( $tipi_riferimenti [ $key ]) && is_subclass_of ( $tipi_riferimenti [ $key ], Component :: class ) && ! empty ( $id_riferimenti [ $key ])) {
2021-07-28 11:50:02 +02:00
$riga_origine = ( $tipi_riferimenti [ $key ]) :: find ( $id_riferimenti [ $key ]);
2022-04-04 12:50:37 +02:00
list ( $riferimento_precedente , $nuovo_riferimento ) = $obj -> impostaOrigine ( $riga_origine );
2020-02-27 17:37:59 +01:00
2020-09-16 09:14:00 +02:00
// Correzione della descrizione
$obj -> descrizione = str_replace ( $riferimento_precedente , '' , $obj -> descrizione );
$obj -> descrizione .= $nuovo_riferimento ;
2020-02-24 18:30:05 +01:00
2023-11-10 16:38:48 +01:00
$serials_rif = $riga_origine -> serials ;
if ( $serials_rif && $obj -> abilita_serial ) {
$obj -> serials = $serials_rif ;
$has_serial_riferimento = true ;
}
}
2021-02-18 17:46:38 +01:00
if ( ! empty ( $tipi_riferimenti_vendita [ $key ])) {
database () -> insert ( 'co_riferimenti_righe' , [
'source_type' => $tipi_riferimenti_vendita [ $key ],
'source_id' => $id_riferimenti_vendita [ $key ],
'target_type' => $target_type ,
'target_id' => $obj -> id ,
]);
}
2021-11-30 11:09:16 +01:00
if ( ! $is_descrizione ) {
$obj -> id_iva = $iva [ $key ];
$obj -> idconto = $conto [ $key ];
2019-04-19 03:18:05 +02:00
2023-08-04 14:54:28 +02:00
if ( empty ( $conto_arrotondamenti ) && ! empty ( $conto [ $key ])) {
2021-11-30 11:09:16 +01:00
$conto_arrotondamenti = $conto [ $key ];
}
2019-07-23 18:29:40 +02:00
2021-11-30 11:09:16 +01:00
$obj -> ritenuta_contributi = $ritenuta_contributi ;
2019-07-23 18:04:01 +02:00
2021-12-03 14:39:34 +01:00
// Inserisco la ritenuta se è specificata nella riga o se non è specificata nella riga ma è presente in Dati ritenuta (quindi comprende tutte le righe)
2024-02-06 17:25:50 +01:00
if ( ! empty ( $riga [ 'Ritenuta' ]) || $info [ 'ritenuta_norighe' ] == true || $info [ 'rivalsa_norighe' ] == true ) {
2021-11-30 11:09:16 +01:00
$obj -> id_ritenuta_acconto = $id_ritenuta_acconto ;
$obj -> calcolo_ritenuta_acconto = $calcolo_ritenuta_acconto ;
2023-11-15 15:53:13 +01:00
$obj -> id_rivalsa_inps = $id_rivalsa ;
2021-11-30 11:09:16 +01:00
}
2019-04-19 03:18:05 +02:00
2022-11-15 18:00:53 +01:00
// Totale documento
2023-07-24 10:01:51 +02:00
$totale_righe = 0 ;
2023-07-25 10:06:13 +02:00
$totale_arrotondamento = 0 ;
2023-07-31 17:28:46 +02:00
$totale_imp = 0 ;
2022-11-15 18:00:53 +01:00
$dati_riepilogo = $this -> getBody ()[ 'DatiBeniServizi' ][ 'DatiRiepilogo' ];
if ( ! empty ( $dati_riepilogo [ 'ImponibileImporto' ])) {
2023-07-25 10:06:13 +02:00
$totale_arrotondamento = $dati_riepilogo [ 'Arrotondamento' ];
2023-07-31 17:28:46 +02:00
$totale_imp = sum ( $dati_riepilogo [ 'ImponibileImporto' ], null , 2 );
2022-11-15 18:00:53 +01:00
} elseif ( is_array ( $dati_riepilogo )) {
foreach ( $dati_riepilogo as $dato ) {
2023-07-25 10:14:47 +02:00
$totale_arrotondamento += $dato [ 'Arrotondamento' ];
2023-07-31 17:28:46 +02:00
$totale_imp += $dato [ 'ImponibileImporto' ];
2023-08-04 14:54:28 +02:00
}
}
2023-07-28 17:50:25 +02:00
$totali_righe = array_column ( $righe , 'PrezzoTotale' );
2023-12-15 17:29:06 +01:00
$tot_righe = sum ( $totali_righe , null , 2 );
$totale_righe = round ( $tot_righe + $tot_arr + $totale_arrotondamento , 2 );
2022-11-15 18:00:53 +01:00
2021-11-30 11:09:16 +01:00
// Nel caso il prezzo sia negativo viene gestito attraverso l'inversione della quantità (come per le note di credito)
// TODO: per migliorare la visualizzazione, sarebbe da lasciare negativo il prezzo e invertire gli sconti.
2023-08-04 14:54:28 +02:00
if ( ! empty ( $articolo -> um ) && ! empty ( $articolo -> um_secondaria ) && ! empty (( float ) $articolo -> fattore_um_secondaria ) && $riga [ 'UnitaMisura' ] == $articolo -> um_secondaria ) {
2023-05-03 14:09:08 +02:00
$qta = (( $riga [ 'Quantita' ] ? : 1 ) / $articolo -> fattore_um_secondaria );
2023-08-04 14:54:28 +02:00
$prezzo = $totale_righe > 0 ? $totale_righe / $qta : - ( $totale_righe / $qta );
2023-05-03 14:09:08 +02:00
} else {
$qta = ( $riga [ 'Quantita' ] ? : 1 );
$prezzo = $totale_righe > 0 ? $riga [ 'PrezzoUnitario' ] : - $riga [ 'PrezzoUnitario' ];
}
2019-04-19 03:18:05 +02:00
2021-11-30 11:09:16 +01:00
// Prezzo e quantità
$obj -> prezzo_unitario = $prezzo ;
$obj -> qta = $qta ;
2019-04-19 03:18:05 +02:00
2021-11-30 11:09:16 +01:00
if ( ! empty ( $riga [ 'UnitaMisura' ])) {
2023-08-04 14:54:28 +02:00
if ( ! empty ( $articolo -> um ) && ! empty ( $articolo -> um_secondaria ) && ! empty (( float ) $articolo -> fattore_um_secondaria ) && $riga [ 'UnitaMisura' ] == $articolo -> um_secondaria ) {
2023-05-02 14:55:21 +02:00
$obj -> um = $articolo -> um ;
} else {
$obj -> um = $riga [ 'UnitaMisura' ];
}
2021-09-17 17:01:00 +02:00
}
2021-11-30 11:09:16 +01:00
// Sconti e maggiorazioni
$sconti = $riga [ 'ScontoMaggiorazione' ];
if ( ! empty ( $sconti )) {
2022-08-02 11:21:59 +02:00
$tot_sconto_calcolato = 0 ;
2021-11-30 11:09:16 +01:00
$sconto_unitario = 0 ;
$sconti = $sconti [ 0 ] ? $sconti : [ $sconti ];
// Determina il tipo di sconto in caso di sconti misti UNT e PRC
foreach ( $sconti as $sconto ) {
$tipo_sconto = ! empty ( $sconto [ 'Importo' ]) ? 'UNT' : 'PRC' ;
if ( ! empty ( $tipo ) && $tipo_sconto != $tipo ) {
$tipo = 'UNT' ;
} else {
$tipo = $tipo_sconto ;
}
}
2021-10-18 10:23:39 +02:00
2021-11-30 11:09:16 +01:00
foreach ( $sconti as $sconto ) {
$unitario = $sconto [ 'Importo' ] ? : $sconto [ 'Percentuale' ];
// Sconto o Maggiorazione
$sconto_riga = ( $sconto [ 'Tipo' ] == 'SC' ) ? $unitario : - $unitario ;
$tipo_sconto = ! empty ( $sconto [ 'Importo' ]) ? 'UNT' : 'PRC' ;
if ( $tipo_sconto == 'PRC' ) {
$sconto_calcolato = calcola_sconto ([
'sconto' => $sconto_riga ,
2022-08-02 11:21:59 +02:00
'prezzo' => $sconto_unitario ? $obj -> prezzo_unitario - ( $tot_sconto_calcolato / $obj -> qta ) : $obj -> prezzo_unitario ,
2021-11-30 11:09:16 +01:00
'tipo' => 'PRC' ,
'qta' => $obj -> qta ,
]);
if ( $tipo == 'PRC' ) {
$tot_sconto = $sconto_calcolato * 100 / $obj -> imponibile ;
} else {
$tot_sconto = $sconto_calcolato ;
}
2021-09-17 17:01:00 +02:00
} else {
2021-11-30 11:09:16 +01:00
$tot_sconto = $sconto_riga ;
2021-09-17 17:01:00 +02:00
}
2021-11-30 11:09:16 +01:00
2023-08-04 14:54:28 +02:00
$tot_sconto_calcolato += $sconto_calcolato ;
2021-11-30 11:09:16 +01:00
$sconto_unitario += $tot_sconto ;
2021-09-17 17:01:00 +02:00
}
2019-04-19 03:18:05 +02:00
2021-11-30 11:09:16 +01:00
$obj -> setSconto ( $sconto_unitario , $tipo );
2019-04-19 03:18:05 +02:00
}
2021-11-30 11:09:16 +01:00
// Aggiornamento prezzo di acquisto e fornitore predefinito in base alle impostazioni
if ( ! empty ( $articolo )) {
if ( $update_info [ $key ] == 'update_price' || $update_info [ $key ] == 'update_all' ) {
$dettaglio_predefinito = DettaglioPrezzo :: dettaglioPredefinito ( $articolo -> id , $anagrafica -> idanagrafica , $direzione )
-> first ();
2021-09-28 15:51:11 +02:00
2021-11-30 11:09:16 +01:00
// Aggiungo associazione fornitore-articolo se non presente
if ( empty ( $dettaglio_predefinito )) {
$dettaglio_predefinito = DettaglioPrezzo :: build ( $articolo , $anagrafica , $direzione );
}
2021-10-18 10:23:39 +02:00
2021-11-30 11:09:16 +01:00
// Imposto lo sconto nel listino solo se è una percentuale, se è un importo lo sottraggo dal prezzo
if ( $tipo == 'PRC' ) {
$dettaglio_predefinito -> sconto_percentuale = $sconto_unitario ;
$prezzo_unitario = $obj -> prezzo_unitario ;
$prezzo_acquisto = $obj -> prezzo_unitario - ( $obj -> prezzo_unitario * $sconto_unitario / 100 );
} else {
$prezzo_unitario = $obj -> prezzo_unitario - $sconto_unitario ;
$prezzo_acquisto = $prezzo_unitario ;
}
2021-10-15 10:47:45 +02:00
2021-11-30 11:09:16 +01:00
// Aggiornamento listino
$dettaglio_predefinito -> setPrezzoUnitario ( $prezzo_unitario );
$dettaglio_predefinito -> save ();
2021-09-28 15:51:11 +02:00
2021-11-30 11:09:16 +01:00
// Aggiornamento fornitore predefinito
if ( $update_info [ $key ] == 'update_all' ) {
// Aggiornamento prezzo di acquisto e fornitore predefinito
$articolo -> prezzo_acquisto = $prezzo_acquisto ;
$articolo -> id_fornitore = $anagrafica -> idanagrafica ;
$articolo -> save ();
}
2021-09-28 15:51:11 +02:00
}
2023-09-28 12:31:03 +02:00
// Gestione seriali
2023-11-10 16:38:48 +01:00
if ( $serials [ $key ] && ! $has_serial_riferimento ) {
2023-09-28 12:31:03 +02:00
$obj -> serials = $serials [ $key ];
}
2021-09-28 15:51:11 +02:00
}
2021-11-30 11:09:16 +01:00
$tipo = null ;
$sconto_unitario = null ;
2019-04-19 03:18:05 +02:00
}
2021-10-18 10:23:39 +02:00
2019-04-19 03:18:05 +02:00
$obj -> save ();
}
2022-03-04 11:50:57 +01:00
// Ripristino l'impostazione iniziale di evasione quantità
\Settings :: setValue ( 'Permetti il superamento della soglia quantità dei documenti di origine' , $original_setting_evasione );
2019-05-04 00:32:28 +02:00
// Ricaricamento della fattura
$fattura -> refresh ();
2019-04-19 03:18:05 +02:00
// Arrotondamenti differenti nella fattura XML
2023-12-15 17:29:06 +01:00
$riepiloghi = $this -> getBody ()[ 'DatiBeniServizi' ][ 'DatiRiepilogo' ];
$riepiloghi = $this -> forceArray ( $riepiloghi );
2023-10-05 10:26:57 +02:00
2023-12-15 17:29:06 +01:00
$riep_imp = 0 ;
foreach ( $riepiloghi as $riepilogo ) {
$riep_imp += $riepilogo [ 'Imposta' ];
}
2023-12-18 12:26:44 +01:00
2023-12-15 17:29:06 +01:00
$diff_iva = round ( $riep_imp - $fattura -> iva , 2 );
2024-01-04 14:47:06 +01:00
$diff = round ( abs ( $fattura -> totale_imponibile ) - abs ( $totale_righe + $tot_arr ), 2 );
2023-12-15 17:29:06 +01:00
$diff_tot = round ( $fattura -> totale_imponibile + $fattura -> rivalsa_inps - $totale_imp + $tot_arr , 2 );
2024-01-04 14:47:06 +01:00
2024-02-14 16:46:45 +01:00
$iva_arrotondamento = database () -> fetchOne ( 'SELECT * FROM `co_iva` WHERE `percentuale`= 0 AND `deleted_at` IS NULL LIMIT 1' );
2024-01-04 14:47:06 +01:00
2024-01-15 15:30:45 +01:00
if (( $diff != 0 && $diff != $diff_tot ) || (( $diff_tot != $diff ) && ! $diff_iva ) || $diff_iva ) {
2023-12-15 17:29:06 +01:00
if ( $diff != 0 && $diff != $diff_tot ) {
2023-12-18 12:26:44 +01:00
$diff *= 100 / ( 100 + $iva_arrotondamento [ 'percentuale' ]);
2023-12-15 17:29:06 +01:00
} elseif (( $diff == $diff_tot ) && ! $diff_iva ) {
$diff = $totale_righe - $fattura -> totale_imponibile ;
2023-12-18 12:26:44 +01:00
} elseif (( $diff == $diff_tot ) && $diff_iva ) {
2023-12-15 17:29:06 +01:00
$diff = $diff_iva ;
2023-07-31 17:28:46 +02:00
} else {
2023-12-15 17:29:06 +01:00
$diff = - ( $diff_tot * 100 ) / ( 100 + $iva_arrotondamento [ 'percentuale' ]);
2023-07-31 17:28:46 +02:00
}
2024-01-04 14:47:06 +01:00
2019-04-19 03:18:05 +02:00
$obj = Riga :: build ( $fattura );
$obj -> descrizione = tr ( 'Arrotondamento calcolato in automatico' );
2021-05-05 18:01:09 +02:00
$obj -> id_iva = $iva_arrotondamento [ 'id' ];
2021-11-30 11:09:16 +01:00
$obj -> idconto = $conto_arrotondamenti ;
2020-02-14 17:43:39 +01:00
$obj -> prezzo_unitario = round ( $diff , 4 );
2019-04-19 03:18:05 +02:00
$obj -> qta = 1 ;
$obj -> save ();
}
}
2019-07-23 18:04:01 +02:00
2022-01-11 11:54:50 +01:00
protected function prepareFattura ( $id_tipo , $data , $data_registrazione , $id_sezionale , $ref_fattura )
2019-07-23 18:04:01 +02:00
{
2022-01-11 11:54:50 +01:00
$fattura = parent :: prepareFattura ( $id_tipo , $data , $data_registrazione , $id_sezionale , $ref_fattura );
2019-07-23 18:04:01 +02:00
$database = database ();
$righe = $this -> getRighe ();
2019-07-24 10:59:40 +02:00
$totali = array_column ( $righe , 'PrezzoTotale' );
$totale = sum ( $totali );
2019-07-23 18:04:01 +02:00
foreach ( $righe as $riga ) {
2019-07-24 10:59:40 +02:00
$dati = $riga [ 'AltriDatiGestionali' ];
if ( ! empty ( $dati )) {
$dati = isset ( $dati [ 0 ]) ? $dati : [ $dati ];
foreach ( $dati as $dato ) {
if ( $dato [ 'TipoDato' ] == 'CASSA-PREV' ) {
$descrizione = $dato [ 'RiferimentoTesto' ];
$importo = floatval ( $dato [ 'RiferimentoNumero' ]);
preg_match ( '/^(.+?) - (.+?) \((.+?)%\)$/' , trim ( $descrizione ), $m );
$nome = ucwords ( strtolower ( $m [ 2 ]));
$percentuale = $m [ 3 ];
$totale_previsto = round ( $importo / $percentuale * 100 , 2 );
$percentuale_importo = round ( $totale_previsto / $totale * 100 , 2 );
$ritenuta_contributi = $database -> fetchOne ( 'SELECT * FROM`co_ritenuta_contributi` WHERE `percentuale` = ' . prepare ( $percentuale ) . ' AND `percentuale_imponibile` = ' . prepare ( $percentuale_importo ));
if ( empty ( $ritenuta_contributi )) {
$database -> query ( 'INSERT INTO `co_ritenuta_contributi` (`descrizione`, `percentuale`, `percentuale_imponibile`) VALUES (' . prepare ( $nome ) . ', ' . prepare ( $percentuale ) . ', ' . prepare ( $percentuale_importo ) . ')' );
}
$ritenuta_contributi = $database -> fetchOne ( 'SELECT * FROM`co_ritenuta_contributi` WHERE `percentuale` = ' . prepare ( $percentuale ) . ' AND `percentuale_imponibile` = ' . prepare ( $percentuale_importo ));
$fattura -> id_ritenuta_contributi = $ritenuta_contributi [ 'id' ];
}
}
2019-07-23 18:04:01 +02:00
}
}
2019-07-24 10:59:40 +02:00
return $fattura ;
}
protected function getRitenutaRivalsa ()
{
$database = database ();
$dati_generali = $this -> getBody ()[ 'DatiGenerali' ][ 'DatiGeneraliDocumento' ];
// Righe
$righe = $this -> getRighe ();
2019-07-23 18:04:01 +02:00
2019-07-23 18:29:40 +02:00
$rivalsa_in_ritenuta = false ;
// Rivalsa
$casse = $dati_generali [ 'DatiCassaPrevidenziale' ];
if ( ! empty ( $casse )) {
2022-06-24 18:06:10 +02:00
$totale = 0 ;
2024-02-06 10:23:55 +01:00
$rivalsa_norighe = true ;
$totale_norighe = 0 ;
2022-06-24 18:06:10 +02:00
foreach ( $righe as $riga ) {
2023-11-15 15:53:13 +01:00
if ( $riga [ 'Ritenuta' ]) {
$totale += $riga [ 'PrezzoTotale' ];
2024-02-06 10:23:55 +01:00
$rivalsa_norighe = false ;
} else {
$totale_norighe += $riga [ 'PrezzoTotale' ];
2023-11-15 15:53:13 +01:00
}
2022-06-24 18:06:10 +02:00
}
2024-02-06 10:23:55 +01:00
2019-07-23 18:29:40 +02:00
$casse = isset ( $casse [ 0 ]) ? $casse : [ $casse ];
$importi = [];
foreach ( $casse as $cassa ) {
$importi [] = floatval ( $cassa [ 'ImportoContributoCassa' ]);
if ( $cassa [ 'Ritenuta' ]) {
$rivalsa_in_ritenuta = true ;
}
}
$importo = sum ( $importi );
2019-07-23 18:04:01 +02:00
2024-02-06 10:23:55 +01:00
$percentuale = round ( $importo / ( $rivalsa_norighe ? $totale_norighe : $totale ) * 100 , 2 );
2019-07-23 18:04:01 +02:00
2019-07-23 18:29:40 +02:00
$rivalsa = $database -> fetchOne ( 'SELECT * FROM`co_rivalse` WHERE `percentuale` = ' . prepare ( $percentuale ));
if ( empty ( $rivalsa )) {
$descrizione = tr ( 'Rivalsa _PRC_%' , [
'_PRC_' => numberFormat ( $percentuale ),
]);
$database -> query ( 'INSERT INTO `co_rivalse` (`descrizione`, `percentuale`) VALUES (' . prepare ( $descrizione ) . ', ' . prepare ( $percentuale ) . ')' );
}
$rivalsa = $database -> fetchOne ( 'SELECT * FROM`co_rivalse` WHERE `percentuale` = ' . prepare ( $percentuale ));
$id_rivalsa = $rivalsa [ 'id' ];
2019-07-23 18:04:01 +02:00
}
2023-11-15 15:35:22 +01:00
$percentuale = 0 ;
$importo = 0 ;
2019-07-23 18:29:40 +02:00
// Ritenuta d'Acconto
$ritenuta = $dati_generali [ 'DatiRitenuta' ];
if ( ! empty ( $ritenuta )) {
2019-07-24 10:59:40 +02:00
$totali = [];
2021-12-03 14:39:34 +01:00
$ritenuta_norighe = true ;
2024-01-04 14:47:06 +01:00
2019-07-24 10:59:40 +02:00
foreach ( $righe as $riga ) {
if ( ! empty ( $riga [ 'Ritenuta' ])) {
$totali [] = $riga [ 'PrezzoTotale' ];
2021-12-03 14:39:34 +01:00
$ritenuta_norighe = false ;
2019-07-24 10:59:40 +02:00
}
}
2024-01-04 14:47:06 +01:00
2023-12-18 16:10:45 +01:00
if ( ! empty ( $ritenuta )) {
$ritenuta = $this -> forceArray ( $ritenuta );
foreach ( $ritenuta as $rit ) {
$percentuale += floatval ( $rit [ 'AliquotaRitenuta' ]);
$importo += floatval ( $rit [ 'ImportoRitenuta' ]);
}
}
2024-01-04 14:47:06 +01:00
2023-05-26 09:35:13 +02:00
// Calcolo la ritenuta su tutte le righe se non è specificata su nessuna riga
if ( empty ( $totali )) {
$totale = array_sum ( array_column ( $righe , 'PrezzoTotale' ));
} else {
$totale = sum ( $totali );
}
2024-01-04 14:47:06 +01:00
2023-12-18 16:10:45 +01:00
$totale_previsto = round ( $importo * 100 / $percentuale , 2 );
2019-07-23 18:29:40 +02:00
$percentuale_importo = round ( $totale_previsto / $totale * 100 , 2 );
2020-03-06 17:17:09 +01:00
$percentuale_importo = min ( $percentuale_importo , 100 ); // Nota: Fix per la percentuale che superava il 100% nel caso di importi con Rivalsa compresa
2024-01-04 14:47:06 +01:00
$ritenuta_acconto = $database -> fetchOne ( 'SELECT * FROM `co_ritenutaacconto` WHERE `percentuale` = ' . prepare ( $percentuale ) . ' AND `percentuale_imponibile` = ' . prepare ( $percentuale_importo ));
2019-07-23 18:29:40 +02:00
if ( empty ( $ritenuta_acconto )) {
$descrizione = tr ( 'Ritenuta _PRC_% sul _TOT_%' , [
'_PRC_' => numberFormat ( $percentuale ),
'_TOT_' => numberFormat ( $percentuale_importo ),
]);
2024-01-04 14:47:06 +01:00
$database -> query ( 'INSERT INTO `co_ritenutaacconto` (`descrizione`, `percentuale`, `percentuale_imponibile`) VALUES (' . prepare ( $descrizione ) . ', ' . prepare ( $percentuale ) . ', ' . prepare ( $percentuale_importo ) . ')' );
$ritenuta_acconto = $database -> fetchOne ( 'SELECT * FROM `co_ritenutaacconto` WHERE `percentuale` = ' . prepare ( $percentuale ) . ' AND `percentuale_imponibile` = ' . prepare ( $percentuale_importo ));
2019-07-23 18:29:40 +02:00
}
2024-01-04 14:47:06 +01:00
2019-07-23 18:29:40 +02:00
$id_ritenuta_acconto = $ritenuta_acconto [ 'id' ];
}
2024-01-04 14:47:06 +01:00
2019-07-23 18:29:40 +02:00
return [
'id_ritenuta_acconto' => $id_ritenuta_acconto ,
'id_rivalsa' => $id_rivalsa ,
2024-02-06 10:23:55 +01:00
'rivalsa_norighe' => $rivalsa_norighe ,
2019-07-23 18:29:40 +02:00
'rivalsa_in_ritenuta' => $rivalsa_in_ritenuta ,
2021-12-03 14:39:34 +01:00
'ritenuta_norighe' => $ritenuta_norighe ,
2019-07-23 18:29:40 +02:00
];
2019-07-23 18:04:01 +02:00
}
2019-04-19 03:18:05 +02:00
}