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

	$module_name = 'Interventi';

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

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

	$query = "SELECT *, (SELECT codice FROM mg_articoli WHERE id=mg_articoli_interventi.idarticolo) AS codice, mg_articoli_interventi.id AS idriga, (SELECT prc_guadagno FROM mg_listini 
	WHERE id=(SELECT idlistino FROM an_anagrafiche WHERE idanagrafica=(SELECT idanagrafica FROM in_interventi WHERE idintervento=mg_articoli_interventi.idintervento) ) ) AS prc_guadagno 
	FROM mg_articoli_interventi WHERE idintervento='".$idintervento."' order by data_movimento asc ".$additional_where['Magazzino'];



	$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 "		Articolo\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 "		Prezzo di vendita\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 "		<a href='".$rootdir."/editor.php?id_module=".$modules_info['Articoli']['id']."&id_record=".$rs2[$i]['idarticolo']."' target='_blank'>".nl2br($rs2[$i]['descrizione'])."</a>\n";

			
			echo "	</td>\n";

			//data movimento
			echo "	<td class='text-center'>\n";
				echo "		".date( "d/m/Y", strtotime($rs2[$i]['data_movimento']))."\n";
			echo "	</td>\n";
			
			//Quantità
			$qta = $rs2[$i]['qta'];
			echo "	<td class='text-right'>\n";
			echo "		".number_format( $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_articolo'>".number_format( $netto*$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 questo articolo?' ) ){ ritorna_al_magazzino( '".$rs2[$i]['id']."' ); }\"><i class='fa fa-trash-o'></i></button>\n";
			echo "	</td></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";

						//Ricambi
						$rsr = $dbo->fetchArray("SELECT
													SUM(prezzo_acquisto*qta) AS ricambi_costo,
													SUM(prezzo_vendita*qta) AS ricambi_addebito,
													SUM(prezzo_vendita*qta - sconto*qta) AS ricambi_scontato
													FROM mg_articoli_interventi GROUP BY idintervento HAVING idintervento=\"".$idintervento."\"");

						$ricambi_addebito	= $rsr[0]['ricambi_addebito'];

						echo "	<tr>\n";
						echo "		<th>TOTALE MATERIALE UTILIZZATO</th>\n";
						echo "		<td class='text-right'>".number_format( $ricambi_addebito, 2, ",", "." )." &euro;</td>\n";
						echo "	</tr>\n";
						
						
		echo "</table>\n";
		
		
		
	}
?>
	<script type="text/javascript">
		function ritorna_al_magazzino( id ){
			$.post('<?php echo $rootdir ?>/modules/documenti/actions.php', { op: 'delrigaart', idriga: id }, function(data, result){			
			if( result == 'success' ){
					//ricarico l'elenco degli articoli
					$('#articoli').load( '<?php echo $rootdir ?>/modules/documenti/ajax_articoli.php?idintervento=<?php echo $idintervento ?>');
				}
			});
		}
		
	</script>
