<?php
	//carica report html
	$report = file_get_contents ($docroot."/templates/reg_iva/reg_iva.html");
	$body = file_get_contents ($docroot."/templates/reg_iva/reg_iva_body.html");
		
	
	$v_iva = [];
	$v_totale = [];

	$totale_iva = 0;
	$totale_subtotale = 0;
	
	$dir = $_GET['dir'];
	$periodo = $_GET['periodo'];
	
	$date_start = $_SESSION['period_start'];
	$date_end = $_SESSION['period_end'];
	
	
	include_once( "pdfgen_variables.php" );

	if ('entrata' == $dir) {
		$Testo="REGISTRO IVA ENTRATE";		
		$query = "SELECT *, SUM(subtotale-co_righe_documenti.sconto) AS subtotale, SUM(iva) AS iva, (SELECT ragione_sociale FROM an_anagrafiche WHERE an_anagrafiche.idanagrafica=co_documenti.idanagrafica) AS ragione_sociale FROM co_documenti INNER JOIN co_righe_documenti ON co_documenti.id=co_righe_documenti.iddocumento INNER JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id WHERE dir='entrata' AND co_documenti.data>='".$date_start."' AND co_documenti.data<='".$date_end."' and co_documenti.idstatodocumento='1' and co_tipidocumento.mod_riepilogo_iva='1' and co_righe_documenti.qta > 0 GROUP BY co_documenti.id, co_righe_documenti.idiva";
	} elseif ('uscita' == $dir) {
		$Testo="REGISTRO IVA USCITE";
		$query = "SELECT *, SUM(subtotale-co_righe_documenti.sconto) AS subtotale, SUM(iva) AS iva, (SELECT ragione_sociale FROM an_anagrafiche WHERE an_anagrafiche.idanagrafica=co_documenti.idanagrafica) AS ragione_sociale FROM co_documenti INNER JOIN co_righe_documenti ON co_documenti.id=co_righe_documenti.iddocumento INNER JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id WHERE dir='uscita' AND co_documenti.data>='".$date_start."' AND co_documenti.data<='".$date_end."' and co_documenti.idstatodocumento='1' and co_tipidocumento.mod_riepilogo_iva='1' and co_righe_documenti.qta > 0 GROUP BY co_documenti.id, co_righe_documenti.idiva";
	}
	
	$rs = $dbo->fetchArray($query);
	
	$totrows = sizeof($rs);
	
	$body .= "<h3>$Testo AL ".date("d/m/Y")."</h3>\n";
	
	$body .= "<table cellspacing='0' style='table-layout:fixed;'>\n";
	$body .= "<col width='125'><col width='100'><col width='450'><col width='100'><col width='100'><col width='100'>\n";
	
	$body .= "<tr>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell1 cell-padded'>N° Documento</th>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell cell-padded'>Data</th>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell cell-padded'>Causale<br>Ragione sociale</th>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell cell-padded'>Aliquota</th>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell cell-padded'>Imponibile</th>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell cell-padded'>Imposta</th>\n";
	

	$body .= "</tr>\n";

	for( $r=0; $r<sizeof($rs); $r++ ){
		$body .= "<tr>\n";
		$body .= "	<td class='first_cell cell-padded'>".$rs[$r]['numero_documento']."</td>\n";
		$body .= "	<td class='table_cell cell-padded'>".date('d/m/Y', strtotime($rs[$r]['data']))."</td>\n";
		 if ('entrata' == $dir) {
        $body .= "<td class='table_cell cell-padded'>
                    Fattura di vendita<br>
                    ".$rs[$r]['ragione_sociale'].'
                    </td>';
		} elseif ('uscita' == $dir) {
			$body .= "<td class='table_cell cell-padded'>
                    Fattura di acquisto<br>
                    ".$rs[$r]['ragione_sociale'].'
                    </td>';
		}
		$body .= "	<td class='table_cell text-right cell-padded'>". $rs[$r]['desc_iva']." &euro;</td>\n";
		$body .= "	<td class='table_cell text-right cell-padded'>".number_format( $rs[$r]['subtotale'], 2, ",", "." )."</td>\n";
		$body .= "	<td class='table_cell text-right cell-padded'>".number_format( $rs[$r]['iva'], 2, ",", "." )." &euro;</td>\n";
		$body .= "</tr>\n";
		
			$v_iva[$rs[$r]['desc_iva']] += $rs[$r]['iva'];
			$v_totale[$rs[$r]['desc_iva']] += $rs[$r]['subtotale'];

			$totale_iva += $rs[$r]['iva'];
			$totale_subtotale += $rs[$r]['subtotale'];
				
	}

	$body .= "</table>\n";


	$body .= "<h4>RIEPILOGO IVA</h4>\n";
	$body .= "<table cellspacing='0' style='table-layout:fixed;'>\n";
	$body .= "<col width='125'><col width='100'><col width='100'>\n";
	
	$body .= "<tr>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell1 cell-padded'>Cod. IVA</th>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell cell-padded'>Imponibile</th>\n";
	$body .= "<th bgcolor='#dddddd' class='full_cell cell-padded'>Imposta</th>\n";
	$body .= "</tr>\n";		
	
	foreach ($v_iva as $desc_iva => $tot_iva) {
    if ('' != $desc_iva) {
        $body .= "<tr>\n";
			$body .= "	<td class='first_cell cell-padded'>".$desc_iva."</td>\n";
			$body .= "	<td class='table_cell text-right cell-padded'>".number_format( $v_totale[$desc_iva], 2, ",", "." )."</td>\n";
			$body .= "	<td class='table_cell text-right cell-padded'>".number_format( $v_iva[$desc_iva], 2, ",", "." )."</td>\n";
	   $body .= "</tr>\n";
    }
}
	
	$body .= "</table>\n";
	$orientation = 'L';
	$report_name = 'registro_iva.pdf';
	
?>