2017-09-07 16:51:14 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
$report_name = 'fattura_' . $numero . '.pdf' ;
2017-09-08 17:03:47 +02:00
$autofill = [
'count' => 0 , // Conteggio delle righe
2018-12-20 16:33:15 +01:00
'words' => 70 , // Numero di parole dopo cui contare una riga nuova
2017-09-21 16:42:27 +02:00
'rows' => $fattura_accompagnatoria ? 15 : 20 , // Numero di righe massimo presente nella pagina
'additional' => $fattura_accompagnatoria ? 10 : 15 , // Numero di righe massimo da aggiungere
2017-09-13 11:15:31 +02:00
'columns' => 5 , // Numero di colonne della tabella
2017-09-08 17:03:47 +02:00
];
2017-09-07 16:51:14 +02:00
$v_iva = [];
$v_totale = [];
2017-09-13 11:15:31 +02:00
$sconto = [];
$imponibile = [];
$iva = [];
2017-09-07 16:51:14 +02:00
// Intestazione tabella per righe
echo "
2017-09-12 16:45:18 +02:00
< table class = 'table table-striped table-bordered' id = 'contents' >
2017-09-07 16:51:14 +02:00
< thead >
< tr >
2017-09-10 14:35:41 +02:00
< th class = 'text-center' style = 'width:50%' > " .tr('Descrizione', [], ['upper' => true]). " </ th >
2017-09-13 11:15:31 +02:00
< th class = 'text-center' style = 'width:14%' > " .tr('Q.tà', [], ['upper' => true]). " </ th >
2017-09-12 16:45:18 +02:00
< th class = 'text-center' style = 'width:16%' > " .tr('Prezzo unitario', [], ['upper' => true]). " </ th >
2017-09-10 14:35:41 +02:00
< th class = 'text-center' style = 'width:20%' > " .tr('Importo', [], ['upper' => true]). " </ th >
2017-09-13 11:15:31 +02:00
< th class = 'text-center' style = 'width:10%' > " .tr('IVA', [], ['upper' => true]).' (%)</th>
2017-09-07 16:51:14 +02:00
</ tr >
</ thead >
2018-06-22 17:52:51 +02:00
2018-04-10 18:12:27 +02:00
< tfoot >
< tr >
< td style = " border-top:none; border-bottom:1px solid #aaa; " ></ td >
< td style = " border-top:none; border-bottom:1px solid #aaa; " ></ td >
< td style = " border-top:none; border-bottom:1px solid #aaa; " ></ td >
< td style = " border-top:none; border-bottom:1px solid #aaa; " ></ td >
< td style = " border-top:none; border-bottom:1px solid #aaa; " ></ td >
</ tr >
</ tfoot >
2017-09-07 16:51:14 +02:00
< tbody > ' ;
// RIGHE FATTURA CON ORDINAMENTO UNICO
2018-02-15 14:25:27 +01:00
$righe = $dbo -> fetchArray ( " SELECT *,
2018-07-30 15:28:54 +02:00
IFNULL (( SELECT `codice` FROM `mg_articoli` WHERE `id` = `co_righe_documenti` . `idarticolo` ), '' ) AS codice_articolo ,
2018-02-15 14:25:27 +01:00
( SELECT GROUP_CONCAT ( `serial` SEPARATOR ', ' ) FROM `mg_prodotti` WHERE `id_riga_documento` = `co_righe_documenti` . `id` ) AS seriali ,
( SELECT `percentuale` FROM `co_iva` WHERE `id` = `co_righe_documenti` . `idiva` ) AS perc_iva
2018-02-15 17:41:09 +01:00
FROM `co_righe_documenti` WHERE `iddocumento` = " .prepare( $id_record ).' ORDER BY `order`');
2017-09-08 17:03:47 +02:00
foreach ( $righe as $r ) {
2017-09-13 11:15:31 +02:00
$count = 0 ;
$count += ceil ( strlen ( $r [ 'descrizione' ]) / $autofill [ 'words' ]);
$count += substr_count ( $r [ 'descrizione' ], PHP_EOL );
2017-09-07 16:51:14 +02:00
2018-12-12 18:42:41 +01:00
$v_iva [ $r [ 'desc_iva' ]] = sum ( $v_iva [ $r [ 'desc_iva' ]], $r [ 'iva' ]);
$v_totale [ $r [ 'desc_iva' ]] = sum ( $v_totale [ $r [ 'desc_iva' ]], [
$r [ 'subtotale' ], - $r [ 'sconto' ],
]);
2018-07-03 17:28:02 +02:00
// Valori assoluti
$r [ 'qta' ] = abs ( $r [ 'qta' ]);
2018-08-11 15:49:46 +02:00
if ( empty ( $r [ 'sconto_globale' ])) {
$r [ 'subtotale' ] = abs ( $r [ 'subtotale' ]);
} else {
$r [ 'subtotale' ] = ( $r [ 'subtotale' ]);
}
2018-07-03 17:28:02 +02:00
$r [ 'sconto_unitario' ] = abs ( $r [ 'sconto_unitario' ]);
$r [ 'sconto' ] = abs ( $r [ 'sconto' ]);
2018-08-11 15:49:46 +02:00
if ( empty ( $r [ 'sconto_globale' ])) {
$r [ 'iva' ] = abs ( $r [ 'iva' ]);
} else {
$r [ 'iva' ] = ( $r [ 'iva' ]);
}
2018-07-03 17:28:02 +02:00
2017-09-08 17:03:47 +02:00
echo '
2017-09-08 13:24:48 +02:00
< tr >
2017-09-08 17:03:47 +02:00
< td >
'.nl2br($r[' descrizione ' ]);
2017-09-07 16:51:14 +02:00
2018-02-15 14:25:27 +01:00
// Codice articolo
2017-09-08 17:03:47 +02:00
if ( ! empty ( $r [ 'codice_articolo' ])) {
2017-09-07 16:51:14 +02:00
echo '
2017-09-10 14:35:41 +02:00
< br >< small > '.tr(' COD . _COD_ ' , [
'_COD_' => $r [ 'codice_articolo' ],
]) . '</small>' ;
2017-09-13 19:02:36 +02:00
if ( $count <= 1 ) {
$count += 0.4 ;
}
2017-09-07 16:51:14 +02:00
}
2018-02-15 14:25:27 +01:00
// Seriali
if ( ! empty ( $r [ 'seriali' ])) {
echo '
< br >< small > '.tr(' SN ').' : '.$r[' seriali '].' </ small > ' ;
if ( $count <= 1 ) {
$count += 0.4 ;
}
}
2018-02-15 16:44:16 +01:00
// Aggiunta dei riferimenti ai documenti
2018-09-27 16:19:25 +02:00
if ( ! empty ( $record [ 'ref_documento' ])) {
$data = $dbo -> fetchArray ( " SELECT IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM co_documenti WHERE id = " . prepare ( $record [ 'ref_documento' ]));
2018-07-04 12:57:53 +02:00
$text = tr ( 'Rif. fattura _NUM_ del _DATE_' , [
'_NUM_' => $data [ 0 ][ 'numero' ],
'_DATE_' => Translator :: dateToLocale ( $data [ 0 ][ 'data' ]),
]);
echo '
< br >< small > '.$text.' </ small > ' ;
if ( $count <= 1 ) {
$count += 0.4 ;
}
}
2019-04-19 01:39:58 +02:00
// Aggiunta dei riferimenti ai documenti
if ( setting ( 'Riferimento dei documenti nelle stampe' )) {
$ref = doc_references ( $r , $record [ 'dir' ], [ 'iddocumento' ]);
2018-02-15 16:44:16 +01:00
2019-04-19 01:39:58 +02:00
if ( ! empty ( $ref )) {
echo '
2018-06-22 17:52:51 +02:00
< br >< small > '.$ref[' description '].' </ small > ' ;
2018-07-04 12:57:53 +02:00
2019-04-19 01:39:58 +02:00
if ( $count <= 1 ) {
$count += 0.4 ;
}
2018-02-15 16:44:16 +01:00
}
2017-09-13 11:15:31 +02:00
}
echo '
2017-09-07 16:51:14 +02:00
</ td > ' ;
2018-01-18 19:03:06 +01:00
echo '
< td class = " text-center " > ' ;
2018-02-15 16:44:16 +01:00
if ( empty ( $r [ 'is_descrizione' ])) {
2018-02-15 17:41:09 +01:00
echo '
2018-06-29 13:35:39 +02:00
'.Translator::numberToLocale($r[' qta '], ' qta ').' '.$r[' um ' ];
2018-01-18 19:03:06 +01:00
}
echo '
2017-09-07 16:51:14 +02:00
</ td > ' ;
2017-09-12 16:45:18 +02:00
// Prezzo unitario
2017-09-07 16:51:14 +02:00
echo "
2018-01-18 19:03:06 +01:00
< td class = 'text-right' > " ;
2018-02-15 16:44:16 +01:00
if ( empty ( $r [ 'is_descrizione' ])) {
2018-08-11 15:49:46 +02:00
echo '
2019-04-13 02:56:39 +02:00
'.(empty($r[' qta ']) ? ' ' : moneyFormat($r[' subtotale '] / $r[' qta ' ]));
2018-01-18 19:03:06 +01:00
if ( $r [ 'sconto' ] > 0 ) {
echo "
2018-07-05 17:23:21 +02:00
< br >< small class = 'text-muted' > " .tr('sconto _TOT_ _TYPE_', [
2018-02-15 17:41:09 +01:00
'_TOT_' => Translator :: numberToLocale ( $r [ 'sconto_unitario' ]),
2019-04-13 00:09:48 +02:00
'_TYPE_' => ( $r [ 'tipo_sconto' ] == 'PRC' ? '%' : currency ()),
2018-02-15 17:41:09 +01:00
]) . '</small>' ;
2018-01-18 19:03:06 +01:00
if ( $count <= 1 ) {
$count += 0.4 ;
}
2017-09-21 10:01:07 +02:00
}
}
echo '
2017-09-07 16:51:14 +02:00
</ td > ' ;
// Imponibile
echo "
2018-01-18 19:03:06 +01:00
< td class = 'text-right' > " ;
2018-02-15 16:44:16 +01:00
if ( empty ( $r [ 'is_descrizione' ])) {
2018-08-11 15:49:46 +02:00
echo '
2019-04-13 02:56:39 +02:00
'.moneyFormat($r[' subtotale '] - $r[' sconto ' ]);
2018-02-15 16:44:16 +01:00
2018-01-18 19:03:06 +01:00
if ( $r [ 'sconto' ] > 0 ) {
2018-07-10 16:01:28 +02:00
/* echo "
2018-07-05 17:23:21 +02:00
< br >< small class = 'text-muted' > " .tr('sconto _TOT_ _TYPE_', [
2018-02-15 17:41:09 +01:00
'_TOT_' => Translator :: numberToLocale ( $r [ 'sconto' ]),
2019-04-13 00:09:48 +02:00
'_TYPE_' => currency (),
2018-07-10 16:01:28 +02:00
]) . '</small>' ; */
2018-01-18 19:03:06 +01:00
if ( $count <= 1 ) {
$count += 0.4 ;
}
2017-09-13 11:15:31 +02:00
}
2017-09-07 16:51:14 +02:00
}
echo '
</ td > ' ;
// Iva
echo '
2018-01-18 19:03:06 +01:00
< td class = " text-center " > ' ;
2018-09-25 11:55:52 +02:00
if ( empty ( $r [ 'is_descrizione' ]) && empty ( $r [ 'sconto_globale' ])) {
2018-02-15 17:41:09 +01:00
echo '
2019-05-02 10:03:57 +02:00
'.Translator::numberToLocale($r[' perc_iva ' ], 0 );
2018-01-18 19:03:06 +01:00
}
echo '
2017-09-07 16:51:14 +02:00
</ td >
</ tr > ' ;
2017-09-13 11:15:31 +02:00
$autofill [ 'count' ] += $count ;
2017-09-07 16:51:14 +02:00
}
2017-09-12 11:10:16 +02:00
echo '
| autofill |
</ tbody >
</ table > ' ;
2017-09-07 16:51:14 +02:00
2018-04-03 12:10:58 +02:00
// Aggiungo diciture particolari per l'anagrafica cliente
$dicitura = $dbo -> fetchArray ( 'SELECT diciturafissafattura FROM an_anagrafiche WHERE idanagrafica = ' . prepare ( $id_cliente ));
if ( ! empty ( $dicitura [ 0 ][ 'diciturafissafattura' ])) {
2018-06-22 17:52:51 +02:00
$testo = $dicitura [ 0 ][ 'diciturafissafattura' ];
2018-04-03 12:10:58 +02:00
2018-06-22 17:52:51 +02:00
echo "
2018-04-03 12:10:58 +02:00
< p class = 'text-center' >
< b > " .nl2br( $testo ).'</b>
</ p > ' ;
}
2017-09-12 11:10:16 +02:00
// Aggiungo diciture per condizioni iva particolari
foreach ( $v_iva as $key => $value ) {
$dicitura = $dbo -> fetchArray ( 'SELECT dicitura FROM co_iva WHERE descrizione = ' . prepare ( $key ));
2017-09-07 16:51:14 +02:00
2017-09-12 11:10:16 +02:00
if ( ! empty ( $dicitura [ 0 ][ 'dicitura' ])) {
$testo = $dicitura [ 0 ][ 'dicitura' ];
2017-09-07 16:51:14 +02:00
2017-09-12 11:10:16 +02:00
echo "
< p class = 'text-center' >
2017-09-13 11:15:31 +02:00
< b > " .nl2br( $testo ).'</b>
</ p > ' ;
2017-09-07 16:51:14 +02:00
}
}
2018-03-22 19:10:29 +01:00
echo '
< table class = " table " > ' ;
echo '
2019-04-19 17:31:52 +02:00
< tr >
2018-03-22 19:10:29 +01:00
< td width = " 100% " > ' ;
2019-04-19 17:31:52 +02:00
2018-09-27 16:19:25 +02:00
if ( ! empty ( $record [ 'note' ])) {
2018-03-22 19:10:29 +01:00
echo '
< p class = " small-bold " > '.tr(' Note ', [], [' upper ' => true]).' :</ p >
2018-09-27 16:19:25 +02:00
< p > '.nl2br($record[' note ']).' </ p > ' ;
2018-03-22 19:10:29 +01:00
}
echo '
</ td > ' ;
2017-12-20 14:05:14 +01:00
2018-03-22 19:10:29 +01:00
echo '
</ tr > ' ;
echo '
</ table > ' ;
2018-07-30 13:15:46 +02:00
// Calcoli
2018-07-30 15:28:54 +02:00
$imponibile = sum ( array_column ( $righe , 'subtotale' ));
$sconto = sum ( array_column ( $righe , 'sconto' ));
$iva = sum ( array_column ( $righe , 'iva' ));
2018-07-30 13:15:46 +02:00
$imponibile_scontato = sum ( $imponibile , - $sconto );
2018-09-27 16:19:25 +02:00
$totale_iva = sum ( $iva , $record [ 'iva_rivalsainps' ]);
2018-07-30 13:15:46 +02:00
$totale = sum ([
$imponibile_scontato ,
2018-09-27 16:19:25 +02:00
$record [ 'rivalsainps' ],
2018-07-30 13:15:46 +02:00
$totale_iva ,
]);
$netto_a_pagare = sum ([
$totale ,
2018-09-27 16:19:25 +02:00
- $record [ 'ritenutaacconto' ],
2018-07-30 13:15:46 +02:00
]);
$imponibile = abs ( $imponibile );
$sconto = abs ( $sconto );
$iva = abs ( $iva );
$imponibile_scontato = abs ( $imponibile_scontato );
$totale_iva = abs ( $totale_iva );
$totale = abs ( $totale );
$netto_a_pagare = abs ( $netto_a_pagare );