<?php
	//carica movimento
	$idmovimento = save($_GET['iddocumento']);
	$iddocumento = save($_GET['iddocumento']);
	

	$show_costi =get_var_tipodoc2($iddocumento,'stampa_prezzi');
	//Lettura dati preventivo e interventi
	$q = "SELECT *, date_format(data, '%d/%m/%Y') AS data FROM co_documenti WHERE co_documenti.id='".$idmovimento."'";
	
	
	$q = "SELECT *, (SELECT descrizione FROM co_statidocumento WHERE id=idstatodocumento) AS stato_doc, (SELECT txt_suffisso FROM co_tipidocumento WHERE id=idtipodocumento) AS suffisso, (SELECT descrizione FROM co_tipidocumento WHERE id=idtipodocumento) AS tipo_doc, (SELECT descrizione FROM co_pagamenti WHERE id=idpagamento) AS tipo_pagamento, (SELECT dir FROM co_tipidocumento WHERE id=idtipodocumento) AS dir FROM co_documenti WHERE id=\"".$idmovimento."\" ".$additional_where[$nome_modulo];		
	
	
	
	
	$rsmovimento = $dbo->fetchArray( $q );
	$idcliente = $rsmovimento[0]['idanagrafica'];
	$tipo_doc = $rsmovimento[0]['tipo_doc'];
	
	//carica report html
	$report = file_get_contents ($docroot."/templates/movimento/movimento.html");
	$body = file_get_contents ($docroot."/templates/movimento/movimento_body.html");
	
	include_once( "pdfgen_variables.php" );

	
	//Lettura nome referenti collegati all'anagrafica
	$query = "SELECT * FROM an_referenti WHERE id = ".$rsmovimento[0]['idreferente'];
	$rs = $dbo->fetchArray( $query );
	$nome_referente = $rs[0]['nome'];
	
		
	
	
	
	// Tabella intestazione
	$body .= "<table border='0' cellspacing='10' cellpadding='10'>\n";
	
	$body .= "<tr><td align=\"left\" width='356' valign='top'>\n";


	$body .= "<big><b>$tipo_doc N<sup>o</sup> ".$rsmovimento[0]['numero']." </b></big><br/>\n";
	$body .= "<big><b>del ".date( "d/m/Y", strtotime($rsmovimento[0]['data']) )."</b></big>\n";

	$body .= "</td>\n";
	$body .= "<td align=\"left\" width='356' valign='top'>\n";
	
	if ($c_cap!=""){ $c_cap = $c_cap." "; }
	if ($c_provincia!=""){ $c_provincia = " (".$c_provincia.")"; }
		
	$body .= "<big style=\"line-height:5mm\" >Spettabile<br/><b>".$c_ragionesociale."<br>".$c_indirizzo."<br>".$c_cap.$c_citta.$c_provincia."<br> P.Iva: ".$c_piva."</b></big>\n";
	$body .= "</td>\n";
	$body .= "</tr>\n";
	
		
	if ($nome_referente!=""){
		$body .= "<tr><td align=\"left\" colspan=\"2\" >\n";
		$body .= "<b><u>C.A.</u></b> ".$nome_referente."\n";
		$body .= "</td>\n";
		$body .= "</tr>\n";
	}
	
	
	$body .= "<tr><td align=\"left\" colspan=\"2\" >\n";
	$body .= "<span>".str_replace( "\n", "<br/>", $rsmovimento[0]['cdescrizione'] )."</span><br/><br/>\n";
	$body .= "</td>\n";
	$body .= "</tr>\n";
	
	$body .= "</table>\n";
	
	
	/*
		TABELLA COSTI
	*/
	

	
	
	$body .= "<table class='table_values' style='table-layout:fixed;' border='0' cellpadding='0'>\n";
	$body .= "<col width='340'><col width='50'><col width='90'><col width='75'><col width='90'>\n";
	$body .= "<thead>\n";
	$body .= "<tr><th width='340'>Descrizione</th>\n";
	$body .= "<th width='50' align='center'>Q.t&agrave;</th>\n";
	$body .= "<th width='90' align='center'>Costo U.</th>\n";
	$body .= "<th width='75' align='center'>Iva</th>\n";
	$body .= "<th width='90' align='center'>Imponibile</th></tr>\n";
	$body .= "</thead>\n";

	$body .= "<tbody>\n";
	
	
	$cifredecimali = get_var("Cifre decimali");
	
	//ARTICOLI
	$q_art = "SELECT *, IFNULL((SELECT codice FROM mg_articoli WHERE id=idarticolo),'') AS codice, (SELECT descrizione FROM co_iva WHERE id=idiva) AS desc_iva FROM `co_righe_documenti` WHERE iddocumento='$idmovimento' ORDER BY id ASC";
	$rs_art = $dbo->fetchArray( $q_art );
	$tot_art = sizeof($rs_art);

	$imponibile_articoli = 0.0;
	$totale_iva = 0.0;
	$totale_sconto = 0.0;

	for( $i=0; $i<$tot_art; $i++ ){
			
		//descrizione
		$body .= "<tr><td>\n";

		if( $rs_art[$i]['codice']!='' )	$body .= $rs_art[$i]['codice']." - ";
		$body .= nl2br($rs_art[$i]['descrizione']);

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

		//q.tà
		$body .= "<td class='table_cell' align=\"right\" valign=\"top\">\n";
		$qta = $rs_art[$i]['qta'];
		$body .= number_format($rs_art[$i]['qta'], $cifredecimali, ",", "")."\n";
		if( $rs_art[$i]['um']!='' )
				$body .= "<br/>\n<small style='color:#555;'>".$rs_art[$i]['um']."</small>\n";
		$body .= "</td>\n";
		
		
		//Costo unitario
		$body .= "<td class='table_cell' align=\"right\" valign=\"top\">\n";
		if( $show_costi == 1){
			$body .= number_format( $rs_art[$i]['subtotale']/$rs_art[$i]['qta'], $cifredecimali, ",", "" )." &euro;\n";
			if( $rs_art[$i]['sconto']>0 )
					$body .= "<br/>\n<small style='color:#555;'>- sconto ".number_format( $rs_art[$i]['sconto'], 2, ",", "." )." &euro;</small>\n";
		} else {
			$body .= "-";
		}
	//	$totale_sconto += ($rs_art[$i]['sconto']*$qta);
		$totale_sconto += $rs_art[$i]['sconto'];
		$body .= "</td>\n";
		

		$body .= "<td class='table_cell' align=\"right\" valign=\"top\">\n";
		$iva  = $rs_art[$i]['iva'];
		$body .= number_format( $iva, 2, ",", "" )." &euro;<br/><small style='color:#777;'>".$rs_art[$i]['desc_iva']."</small>\n";
		$body .= "</td>\n";
		
		
		//Imponibile
		$body .= "<td class='table_cell' align=\"right\" valign=\"top\">\n";
		if( $show_costi == 1 ){
			$body .= number_format( $rs_art[$i]['subtotale']-($qta*$rs_art[$i]['sconto']) , $cifredecimali, ",", "." )." &euro;\n";
		} else {
			$body .= "-";
		}
		$body .= "</td></tr>\n";
	
		$imponibile_articoli += $rs_art[$i]['subtotale'];
		$totale_iva += $iva;
	}
	
	
	//SCONTO
	if( abs($totale_sconto) > 0 ){

		$body .= "<tr><td align='right' colspan='4'>\n";
		$body .= "	<b>SCONTO:</b>\n";
		$body .= "</td>\n";
		$body .= "<td align=\"right\" bgcolor=\"#cccccc\">\n";
		$body .= "	<big><b>-	".number_format( $totale_sconto, get_var("Cifre decimali"), ",", "." )." &euro;</b></big>\n"; 
		$body .= "</td></tr>\n";
	}

	
	//Totale iva
	$body .= "<tr><td align=\"right\" colspan=\"4\">\n";
	$body .= "	<b>TOTALE IMPONIBILE:</b>\n";
	$body .= "</td>\n";
	$body .= "<td align=\"right\" bgcolor=\"#cccccc\">\n";
	$body .= "	<big><b>".number_format( ($imponibile_articoli-$totale_sconto), $cifredecimali, ",", ".")." &euro;</b></big>\n";
	$body .= "</td></tr>\n";


	//Totale iva
	$body .= "<tr><td align=\"right\" colspan=\"4\">\n";
	$body .= "	<b>TOTALE IVA:</b>\n";
	$body .= "</td>\n";
	$body .= "<td align=\"right\" bgcolor=\"#cccccc\">\n";
	$body .= "<big><b>".number_format( $totale_iva , $cifredecimali, ",", ".")." &euro;</b></big>\n";
	$body .= "</td></tr>\n";
	

	//Totale complessivo movimento
	$body .= "<tr><td align=\"right\" colspan=\"4\">\n";
	$body .= "<b>TOTALE MOVIMENTATO:</b>	\n";
	$body .= "</td>\n";
	$body .= "<td align=\"right\" bgcolor=\"#cccccc\">\n";
	$body .= "<big><b>".number_format( ($imponibile_articoli-$totale_sconto)+$totale_iva , $cifredecimali, ",", ".")." &euro;</b></big>\n";
	$body .= "</td></tr>\n";

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

	
	
	
	$rsmovimento[0]['idpagamento'];
	
	$body .= "<nobreak><table border=\"0\" cellpadding='0' cellspacing='10'>\n";
	$body .= "<col width='200'><col width='510'>\n";
	$body .= "<tr><td align=\"center\" valign=\"middle\" style=\"height:5mm;font-size:14pt;\" bgcolor=\"#dddddd\" colspan=\"2\">\n";
	$body .= "<span><b>Note fisse movimento</b></span>\n";
	$body .= "</td></tr>\n";

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


	$body .= "<tr>\n";
	$body .= "	<td colspan='2'>\n";
	$body .= "		<br/><span>$dicitura_fissa_fattura</span>\n";
	$body .= "	</td>\n";	
	$body .= "</tr>\n";

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

	$report_name = "movimento".$idmovimento.".pdf";
?>
