<?php
	include_once(__DIR__."/../../core.php");

			$module_name="Fatture di vendita";

	include_once($docroot."/lib/permissions_check.php");

	if( isset($_GET['idintervento']) ){
		$idintervento = $html->form('idintervento');
	} else {
		$idintervento = $id_record;
	}


	$query = "SELECT * FROM in_righe_interventi WHERE idintervento='".$idintervento."' ".$additional_where['Magazzino']." ORDER BY data_movimento , id ASC";
	$rs2 = $dbo->fetchArray($query);

	if( sizeof($rs2)>0 ){
		echo "	<table class='table table-striped table-condensed table-hover table-bordered'>\n";
		
		
		
		echo "	<tr><th>\n";
		echo "		Descrizione\n";
		echo "	</th>\n";
		
		echo "	<th class='text-center' width='8%'>\n";
		echo "		Data\n";
		echo "	</th>\n";
		

		echo "	<th class='text-center' width='8%'>\n";
		echo "		Q.t&agrave;\n";
		echo "	</th>\n";

		echo "	<th class='text-center' width='15%'>\n";
		echo "		Spesa\n";
		echo "	</th>\n";

		echo "	<th class='text-center' width='15%'>\n";
		echo "		Subtotale\n";
		echo "	</th>\n";
	
		echo "	<th class='text-center' width='5%'>\n";
			echo "		Rif\n";
		echo "	</th>\n";
	
		echo "	<th width='80'>Funzioni</th></tr>\n";



		for( $i=0; $i<sizeof($rs2); $i++ ){
			echo "	<tr><td>\n";
			echo "		<input type='hidden' name='id' value='".$rs2[$i]['id']."'>\n";
				echo "		".nl2br($rs2[$i]['descrizione'])."\n";
			echo "	</td>\n";

			//data
			echo "	<td class='text-center'>\n";
				echo "		".date( "d/m/Y", strtotime($rs2[$i]['data_movimento']))."\n";
			echo "	</td>\n";
			

			//Quantità
			echo "	<td class='text-right'>\n";
				echo "		".number_format($rs2[$i]['qta'], get_var("Cifre decimali"), ",", "." )." ".$rs2[$i]['um']."\n";
			echo "	</td>\n";

			//Prezzo unitario
			$netto = $rs2[$i]['prezzo_vendita'] - $rs2[$i]['sconto'];

			echo "	<td class='text-right'>\n";
				echo "		".number_format( $rs2[$i]['prezzo_vendita'], get_var("Cifre decimali"), ",", "" )." &euro;\n";
			echo "	</td>\n";

			//Prezzo di vendita
			echo "	<td class='text-right'>\n";
				echo "		<span class='prezzo_vendita'>".number_format( $netto*$rs2[$i]['qta'], get_var("Cifre decimali"), ",", "" )."</span> &euro;\n";
			echo "	</td>\n";
			
			//Riferimento
			echo "	<td class='text-right'>\n";
			echo "		".$rs2[$i]['idriga_pianifica']."\n";
			echo "	</td>\n";
			
			echo "	<td class='text-center'>\n";
			echo "		<button type='button' class='btn btn-danger btn-xs' data-toggle='tooltip' title='Elimina...' onclick=\"if( confirm( 'Eliminare questa spesa?' ) ){ elimina_riga( '".$rs2[$i]['id']."' ); }\"><i class='fa fa-trash-o'></i></button>\n";
			echo "	</td>\n";
			
			
		
			echo "	</tr>\n";
		}

		echo "</table>\n";
		echo "<table class='table table condensed table-striped table-hover table-bordered'>\n";
						echo "	<tr>\n";
						echo "		<th></th>\n";						
						echo "		<th width='150'>IMPORTO</th>\n";						
						echo "	</tr>\n";

						

						//Altre spese
						$rsr = $dbo->fetchArray("SELECT
													SUM(prezzo_acquisto*qta) AS altrespese_costo,
													SUM(prezzo_vendita*qta) AS altrespese_addebito,
													SUM(prezzo_vendita*qta - sconto*qta) AS altrespese_scontato

													FROM in_righe_interventi GROUP BY idintervento HAVING idintervento=\"".$idintervento."\"");

						$altrespese_addebito	= $rsr[0]['altrespese_addebito'];						
						
						echo "	<tr>\n";
						echo "		<th>TOTALE ALTRE SPESE</th>\n";
						echo "		<td class='text-right'>".number_format( $altrespese_addebito, 2, ",", "." )." &euro;</td>\n";
						echo "	</tr>\n";
				
	echo "</table>\n";
	}
?>
	<script type="text/javascript">
		function elimina_riga( id ){
			$.post('<?php echo $rootdir ?>/modules/documenti/actions.php', { op: 'delriga', idriga: id }, function(data, result){
				if( result=='success' ){
					//ricarico l'elenco delle righe
					$('#righe').load( '<?php echo $rootdir ?>/modules/documenti/ajax_righe.php?idintervento=<?php echo $idintervento ?>');
				}
			});
		}
		
		
		
	</script>