2017-09-07 16:51:14 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2018-07-17 12:05:21 +02:00
include_once Modules :: filepath ( 'Fatture di vendita' , 'modutil.php' );
2018-06-22 17:52:51 +02:00
2017-09-07 16:51:14 +02:00
$report_name = 'fattura_' . $numero . '.pdf' ;
2017-09-08 17:03:47 +02:00
$autofill = [
'count' => 0 , // Conteggio delle righe
'words' => 70 , // Numero di parolo 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-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-07-04 12:57:53 +02:00
if ( ! empty ( $records [ 0 ][ 'ref_documento' ])) {
$data = $dbo -> fetchArray ( " SELECT IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM co_documenti WHERE id = " . prepare ( $records [ 0 ][ 'ref_documento' ]));
$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 ;
}
}
2018-06-22 17:52:51 +02:00
$ref = doc_references ( $r , $records [ 0 ][ 'dir' ], [ 'iddocumento' ]);
2018-02-15 16:44:16 +01:00
2018-06-22 17:52:51 +02:00
if ( ! empty ( $ref )) {
2018-02-15 16:44:16 +01:00
echo '
2018-06-22 17:52:51 +02:00
< br >< small > '.$ref[' description '].' </ small > ' ;
2018-07-04 12:57:53 +02:00
2018-02-15 16:44:16 +01:00
if ( $count <= 1 ) {
$count += 0.4 ;
}
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 '
2018-07-30 15:28:54 +02:00
'.(empty($r[' qta ']) || empty($r[' subtotale ']) ? ' ' : Translator::numberToLocale($r[' subtotale '] / $r[' qta '])).' & euro ; ' ;
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' ]),
'_TYPE_' => ( $r [ 'tipo_sconto' ] == 'PRC' ? '%' : '€' ),
]) . '</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 '
2018-07-30 15:28:54 +02:00
'.(empty($r[' subtotale ']) ? ' ' : Translator::numberToLocale($r[' subtotale '] - $r[' sconto '])).' & euro ; ' ;
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' ]),
'_TYPE_' => '€' ,
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 '
'.Translator::numberToLocale($r[' perc_iva ' ]);
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-12-29 09:34:46 +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' ],
]);
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 '
< tr > ' ;
2018-06-22 17:52:51 +02:00
if ( abs ( $records [ 0 ][ 'bollo' ]) > 0 ) {
2017-09-07 16:51:14 +02:00
echo '
2018-03-22 19:10:29 +01:00
< td width = " 85% " > ' ;
2018-06-22 17:52:51 +02:00
} else {
2018-03-22 19:10:29 +01:00
echo '
< td width = " 100% " > ' ;
}
if ( ! empty ( $records [ 0 ][ 'note' ])) {
echo '
< p class = " small-bold " > '.tr(' Note ', [], [' upper ' => true]).' :</ p >
< p > '.nl2br($records[0][' note ']).' </ p > ' ;
}
echo '
</ td > ' ;
2018-06-22 17:52:51 +02:00
if ( abs ( $records [ 0 ][ 'bollo' ]) > 0 ) {
2018-03-22 19:10:29 +01:00
echo '
< td width = " 15% " align = " right " > ' ;
2017-09-07 16:51:14 +02:00
}
2017-12-20 14:05:14 +01:00
if ( abs ( $records [ 0 ][ 'bollo' ]) > 0 ) {
echo '
2018-03-22 19:10:29 +01:00
< table style = " width: 20mm; font-size: 50%; text-align: center " class = " table-bordered " >
< tr >
< td style = " height: 20mm; " >
< br >< br >
'.tr(' Spazio per applicazione marca da bollo ', [], [' upper ' => true]).'
</ td >
</ tr >
</ table > ' ;
}
2018-06-22 17:52:51 +02:00
if ( abs ( $records [ 0 ][ 'bollo' ]) > 0 ) {
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 );
$totale_iva = sum ( $iva , $records [ 0 ][ 'iva_rivalsainps' ]);
$totale = sum ([
$imponibile_scontato ,
$records [ 0 ][ 'rivalsainps' ],
$totale_iva ,
]);
$netto_a_pagare = sum ([
$totale ,
$records [ 0 ][ 'bollo' ],
- $records [ 0 ][ 'ritenutaacconto' ],
]);
$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 );