<?php
	//carica intervento
	$idcontratto = save($_GET['idcontratto']);
	
	$show_costi = get_var("Stampa i prezzi sui contratti");
	
	//Lettura dati contratto e interventi
	
	$q = "SELECT * from co_contratti WHERE id='".$idcontratto."'";
	
	$rscontrattii = $dbo->fetchArray( $q );
	$idcliente = $rscontrattii[0]['idanagrafica'];
	
	
	// cerco importo
	$id_tipocontratto=$rscontrattii[0]['id_tipocanone'];
	$descrizione=$rscontrattii[0]['nome'];
	$rs = $dbo->fetchArray("SELECT costo_annuale FROM co_contratti_tipi WHERE id='".$id_tipocontratto."'");
	$subtot=$rs[0]['costo_annuale'];
	
	
	//carica report html
	$report = file_get_contents ($docroot."/templates/contratti/contratto.html");
	$body = file_get_contents ($docroot."/templates/contratti/contratto_body.html");
	
	include_once( "pdfgen_variables.php" );

	$totrows = sizeof($rscontrattii);
	$totale_km = 0;
	$totale_ore = 0;
	$totale = 0;
	$contratti = array();
	$ore = array();
	$km = array();
	$ntecnici = array();
	$tecnici = array();
	$costi_orari = array();
	$costi_km = array();
	$idinterventi = array('-1');

	
	
	//Sostituisco i valori tra | | con il valore del campo del db
	$body .= preg_replace( "/|(.+?)|/", $rscontrattii[0]['${1}'], $body );
	
	//Lettura nome referenti collegati all'anagrafica
	$query = "SELECT * FROM an_referenti WHERE id = \"".$rscontrattii[0]['idreferente']."\"";
	$rs = $dbo->fetchArray( $query );
	$nome_referente = $rs[0]['nome'];
	
		
	
	
	
	// Tabella intestazione
	$body .= "<table  class='table_values' border='0' cellpadding='0'>\n";
	
	$body .= "<tr><td align=\"left\" width='356' style='border:0px;'>\n";
	$body .= " \n";
	$body .= "</td>\n";
	$body .= "<td align=\"left\" width='356' >\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";
	
	
	$body .= "<tr><td align=\"left\" colspan=\"2\" style='border:0px;'>\n";
	$body .= "<big><b>".$f_citta.", ".date("d/m/Y", strtotime( $rscontrattii[0]['data_bozza']) )."</b></big>\n";
	$body .= "</td>\n";
	$body .= "</tr>\n";
	
	
	$body .= "<tr><td align=\"left\" colspan=\"2\" style='border:0px;'>\n";
	$body .= "<big><b>OGGETTO:  ".$rscontrattii[0]['nome']." N<sup>o</sup> ".$rscontrattii[0]['numero']." DEL ".date("d/m/Y", strtotime( $rscontrattii[0]['data_bozza']) )."</b><br></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 .= "</table>\n";
	
	
	
	
	
		
	/*
		TABELLA COSTI
	*/
	$body .= "<table  class='table_values' border='0' cellpadding='0'>\n";
	$body .= "<col width='525'><col width='50'><col width='40'><col width='90'><col width='78'>\n";
	$body .= "<thead>\n";
	$body .= "<tr><th width='525'>Descrizione</th>\n";
	$body .= "<th width='50' align='center'>Q.t&agrave;</th>\n";

	
	$body .= "<th width='75' align='center'>Imponibile</th></tr>\n";
	$body .= "</thead>\n";

	$body .= "<tbody>\n";
	
	$rs = $dbo->fetchArray("SELECT * FROM co_righe2_contratti WHERE idcontratto=\"".$idcontratto."\"");
	$totale = 0;


		//Descrizione
		$body .= "<tr><td valign='top'>Importo Annuale</td>\n";

		//Q.tà
		$body .= "<td align='center' valign='top'>1</td>\n";

	
		//Subtotale
		$body .= "<td align='center' valign='top'>".number_format( $subtot, 2, ",", "" )."  &euro;</td>\n";
		$body .= "</tr>\n";
		
		$totale += $rs[$i]['subtotale'];

	
	
	
	//Totale complessivo intervento
	$body .= "<tr><td align=\"right\" colspan=\"2\">\n";
	$body .= "<big><b>QUOTAZIONE TOTALE:</b></big>\n";
	$body .= "</td>\n";

	$body .= "<td align=\"right\" bgcolor=\"#cccccc\">\n";
	$body .= "<big><b>".number_format( $subtot , 2, ",", ".")." &euro;</b></big>\n";
	$body .= "</td></tr>\n";
	$body .= "</tbody>\n";
	$body .= "</table><br/><br/>\n";
	
	
	
	

	//CONDIZIONI GENERALI DI FORNITURA
	
	//Lettura pagamenti
	if ($rscontrattii[0]['idpagamento']!=""){
		$query = "SELECT * FROM co_pagamenti WHERE id = ".$rscontrattii[0]['idpagamento'];
		$rs = $dbo->fetchArray( $query );
		$pagamento = $rs[0]['descrizione'];
	}else{
		
		$pagamento = "N.D.";
		
	}

	
	$rscontrattii[0]['idpagamento'];
	
	$body .= "<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>CONDIZIONI GENERALI DI FORNITURA</b></span><br/>\n";
	$body .= "</td></tr>\n";
		
	//PAGAMENTI
	$body .= "<tr><td>\n";
	$body .= "<big><b><u>PAGAMENTI:</u></b></big>\n";
	$body .= "</td>\n";
	
	$body .= "<td>\n";
	$body .= "<span><b>".$pagamento."</b></span>\n";
	$body .= "</td></tr>\n";
	
	//VALIDITA' OFFERTA
	$body .= "<tr><td>\n";
	$body .= "<big><b><u>VALIDIT&Agrave; OFFERTA:</u></b></big>\n";
	$body .= "</td>\n";
	
	$body .= "<td>\n";
	$body .= "<span><b>".$rscontrattii[0]['validita']." giorni</b></span>\n";
	$body .= "</td></tr>\n";
		
	//VALIDITA' CONTRATTO
	$body .= "<tr><td>\n";
	$body .= "<big><b><u>VALIDIT&Agrave; CONTRATTO:</u></b></big>\n";
	$body .= "</td>\n";
	
	$body .= "<td>\n";
	$body .= "<span><b>dal ".date( "d/m/Y", strtotime($rscontrattii[0]['data_accettazione']) )." al ".date( "d/m/Y", strtotime($rscontrattii[0]['data_conclusione']) )."</b></span>\n";
	$body .= "</td></tr>\n";
	

	//Descrizione
	$body .= "<tr><td>\n";
	$body .= "<big><b><u>DESCRIZIONE:</u></b></big>\n";
	$body .= "</td>\n";
	
	$body .= "<td>\n";
	$body .= "<span><b>".$rscontrattii[0]['descrizione']." giorni</b></span>\n";
	$body .= "</td></tr>\n";

	
	//ESCLUSIONI
	$body .= "<tr><td>\n";
	$body .= "<big><b><u>ESCLUSIONI:</u></b></big>\n";
	$body .= "</td>\n";
	
	$body .= "<td>\n";
	$body .= "<span><b>".$rscontrattii[0]['esclusioni']."</b></span>\n";
	$body .= "</td></tr>\n";
	
	$body .= "</table>\n";
	$body .= "<br/><br/>\n";	
	
	
	$body .= "<span>In attesa di un Vostro Cortese riscontro scritto, l&rsquo;occasione mi &egrave; gradita per porgere Cordiali Saluti.</span>\n";
	
	$report_name = "contratto_".$idcontratto.".pdf";
?>