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
'words' => 70 , // Numero di parolo dopo cui contare una riga nuova
'rows' => 20 , // Numero di righe massimo presente nella pagina
'additional' => 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 >
< tbody > ' ;
// RIGHE FATTURA CON ORDINAMENTO UNICO
$righe = $dbo -> fetchArray ( " SELECT *, IFNULL((SELECT codice FROM mg_articoli WHERE id=idarticolo),'') AS codice_articolo, (SELECT percentuale FROM co_iva WHERE id=idiva) AS perc_iva FROM `co_righe_documenti` WHERE iddocumento= " . prepare ( $iddocumento ) . ' 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
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
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
}
// Aggiunta riferimento a ordine
2017-09-08 17:03:47 +02:00
if ( ! empty ( $r [ 'idordine' ])) {
$rso = $dbo -> fetchArray ( 'SELECT numero, numero_esterno, data FROM or_ordini WHERE id=' . prepare ( $r [ 'idordine' ]));
2017-09-07 16:51:14 +02:00
$numero = ! empty ( $rso [ 0 ][ 'numero_esterno' ]) ? $rso [ 0 ][ 'numero_esterno' ] : $rso [ 0 ][ 'numero' ];
2017-09-13 19:02:36 +02:00
if ( ! empty ( $rso )) {
2017-09-15 09:48:56 +02:00
$descrizione = tr ( 'Rif. ordine num. _NUM_ del _DATE_' , [
2017-09-13 19:02:36 +02:00
'_NUM_' => $numero ,
'_DATE_' => Translator :: dateToLocale ( $rso [ 0 ][ 'data' ]),
]);
}
2017-09-07 16:51:14 +02:00
}
// Aggiunta riferimento a ddt
2017-09-08 17:03:47 +02:00
elseif ( ! empty ( $r [ 'idddt' ])) {
$rso = $dbo -> fetchArray ( 'SELECT numero, numero_esterno, data FROM dt_ddt WHERE id=' . prepare ( $r [ 'idddt' ]));
2017-09-07 16:51:14 +02:00
$numero = ! empty ( $rso [ 0 ][ 'numero_esterno' ]) ? $rso [ 0 ][ 'numero_esterno' ] : $rso [ 0 ][ 'numero' ];
2017-09-13 19:02:36 +02:00
if ( ! empty ( $rso )) {
2017-09-15 09:48:56 +02:00
$descrizione = tr ( 'Rif. ddt num. _NUM_ del _DATE_' , [
2017-09-13 19:02:36 +02:00
'_NUM_' => $numero ,
'_DATE_' => Translator :: dateToLocale ( $rso [ 0 ][ 'data' ]),
]);
}
}
// Aggiunta riferimento al preventivo
elseif ( ! empty ( $r [ 'idpreventivo' ])) {
$rso = $dbo -> fetchArray ( 'SELECT numero, data_bozza FROM co_preventivi WHERE id=' . prepare ( $r [ 'idpreventivo' ]));
if ( ! empty ( $rso )) {
2017-09-15 09:48:56 +02:00
$descrizione = tr ( 'Rif. preventivo num. _NUM_ del _DATE_' , [
2017-09-13 19:02:36 +02:00
'_NUM_' => $rso [ 0 ][ 'numero' ],
'_DATE_' => Translator :: dateToLocale ( $rso [ 0 ][ 'data_bozza' ]),
]);
}
2017-09-07 16:51:14 +02:00
}
2017-09-13 11:15:31 +02:00
// Aumento del conteggio
2017-09-13 19:02:36 +02:00
if (( ! empty ( $r [ 'idordine' ]) || ! empty ( $r [ 'idddt' ]) || ! empty ( $r [ 'idpreventivo' ])) && $count <= 1 && ! empty ( $descrizione )) {
echo '<br><small>' . $descrizione . '</small>' ;
2017-09-13 11:15:31 +02:00
$count += 0.4 ;
}
echo '
2017-09-07 16:51:14 +02:00
</ td > ' ;
echo "
2017-09-08 17:03:47 +02:00
< td class = 'text-center' >
2017-09-21 10:01:07 +02:00
" .Translator::numberToLocale( $r['qta'] ).' '. $r['um'] .'
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 "
2017-09-08 17:03:47 +02:00
< td class = 'text-right' >
2017-09-21 10:01:07 +02:00
" .(empty( $r['qta'] ) || empty( $r['subtotale'] ) ? '' : Translator::numberToLocale( $r['subtotale'] / $r['qta'] )).' €';
if ( $r [ 'sconto' ] > 0 ) {
echo "
< br >< small class = 'text-muted' >- " .tr('sconto _TOT_ _TYPE_', [
'_TOT_' => Translator :: numberToLocale ( $r [ 'sconto_unitario' ]),
'_TYPE_' => ( $r [ 'tipo_sconto' ] == 'PRC' ? '%' : '€' ),
]) . '</small>' ;
if ( $count <= 1 ) {
$count += 0.4 ;
}
}
echo '
2017-09-07 16:51:14 +02:00
</ td > ' ;
// Imponibile
echo "
2017-09-08 17:03:47 +02:00
< td class = 'text-right' >
2017-09-21 10:01:07 +02:00
" .(empty( $r['subtotale'] ) ? '' : Translator::numberToLocale( $r['subtotale'] )).' €';
2017-09-07 16:51:14 +02:00
2017-09-08 17:03:47 +02:00
if ( $r [ 'sconto' ] > 0 ) {
2017-09-21 10:01:07 +02:00
echo "
< br >< small class = 'text-muted' >- " .tr('sconto _TOT_ _TYPE_', [
'_TOT_' => Translator :: numberToLocale ( $r [ 'sconto' ]),
'_TYPE_' => '€' ,
]) . '</small>' ;
2017-09-13 11:15:31 +02:00
if ( $count <= 1 ) {
$count += 0.4 ;
}
2017-09-07 16:51:14 +02:00
}
echo '
</ td > ' ;
// Iva
echo '
2017-09-12 11:10:16 +02:00
< td class = " text-center " >
2017-09-21 10:01:07 +02:00
'.Translator::numberToLocale($r[' perc_iva ']).'
2017-09-07 16:51:14 +02:00
</ td >
</ tr > ' ;
2017-09-13 11:15:31 +02:00
$autofill [ 'count' ] += $count ;
$imponibile [] = $r [ 'subtotale' ];
$iva [] = $r [ 'iva' ];
$sconto [] = $r [ 'sconto' ];
2017-09-07 16:51:14 +02:00
2017-09-08 17:03:47 +02:00
$v_iva [ $r [ 'desc_iva' ]] += $r [ 'iva' ];
$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
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
}
}
2017-09-12 16:17:11 +02:00
if ( ! empty ( $records [ 0 ][ 'note' ])) {
2017-09-07 16:51:14 +02:00
echo '
< br >
2017-09-10 14:35:41 +02:00
< p class = " small-bold " > '.tr(' Note ', [], [' upper ' => true]).' :</ p >
2017-09-12 16:17:11 +02:00
< p > '.nl2br($records[0][' note ']).' </ p > ' ;
2017-09-07 16:51:14 +02:00
}
2017-09-08 17:03:47 +02:00
// Info per il footer
2017-09-13 11:15:31 +02:00
$imponibile = sum ( $imponibile );
$iva = sum ( $iva );
$sconto = sum ( $sconto );
$totale = $imponibile + $iva - $sconto ;