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

	
	
	$idmodulo = $html->form('idmodule');
	$module_name=get_module_name_by_id($idmodulo);

	
	$iddocumento = $html->form('iddocumento');
	$idriga = $html->form('idriga');
	$dir = get_var_tipodoc2($iddocumento,"dir");	
	$mod_mag = get_var_tipodoc2($iddocumento,"mod_mag");
	//Info documento
	$q = "SELECT * FROM co_documenti WHERE id='".$iddocumento."'";
	$rs = $dbo->fetchArray($q);
	( $rs[0]['numero_esterno'] != '' ) ? $numero=$rs[0]['numero_esterno'] : $numero=$rs[0]['numero'];
	$idanagrafica = $rs[0]['idanagrafica'];
	$idconto_fattura = $rs[0]['idconto'];
	
	//Info riga inserita
	$q = "SELECT * FROM co_righe_documenti WHERE iddocumento='".$iddocumento."' AND id='".$idriga."'";
	$rsr = $dbo->fetchArray($q);
	$idconto_fattura = $rsr[0]['idconto'];
	$id_magazzino_e=$rsr[0]['id_magazzino_e'];
	
	$per_utile_agente=$rsr[0]['perc_agente'];
	
	// seleziono magazzino definito in impostazioni

		$rs_mag = $dbo->fetchArray("SELECT nome FROM mg_magazzini WHERE id='".$rsr[0]['id_magazzino_e']."'");
		$nome_magazzino=$rs_mag[0]['nome'];
		
	if( $rsr[0]['id_impianto'] != "0"  ){
		$rs_mag = $dbo->fetchArray("SELECT nome FROM my_impianti WHERE id='".$rsr[0]['id_impianto']."'");
		$nome_impianto=$rs_mag[0]['nome'];
	}
	else{	
		$nome_impianto="Nessuna Installazione";
	}

	/*
		Form di modifica riga documento
	*/
	echo "<label>Documento numero ".$numero."</label><br><br>\n";
	echo "<form id=\"link_form\" action=\"".$rootdir."/editor.php?id_module=".$modules_info[$module_name]['id']."&id_record=".$iddocumento."\" method=\"post\">\n";
	
	echo "	<input type='hidden' name='op' value='editriga'>\n";
	echo "	<input type='hidden' name='idriga' value='".$idriga."'>\n";
	echo "	<input type='hidden' name='backto' value='record-edit'>\n";
	echo "	<input type='hidden' name='dir' value='".$dir."'>\n";
	
	
	
	//Descrizione
	echo "	<div class='row'>\n";
	echo "		<div class='col-md-12'>\n";
	echo "			<label>Descrizione</label>\n";
	echo "			<textarea name=\"descrizione\" id=\"descrizione\" class='form-control' >".$rsr[0]['descrizione']."</textarea>\n";
	echo "		</div>\n";
	echo "	</div>\n";
			
	echo "<br>";

	echo "<div class='panel panel-primary'>";
		echo "<div class='panel-body'>";
			echo "	<div class='row'>\n";	
			//Qta	
				echo "		<div class='col-md-2'>\n";
				echo "			<div class='form-group'>\n";
				echo "				<label>Q.t&agrave;</label>\n";
				echo "				<div class='input-group'>\n";
				echo "					<input type='text' class='form-control inputmask-decimal' id='qta' maxlength='8' name='qta' value=\"".$rsr[0]['qta']."\">\n";
				echo "				</div>\n";
				echo "			</div>\n";
				echo "		</div>\n";		
			
			//Costo unitario
				echo "		<div class='col-md-3'>\n";
				echo "			<div class='form-group'>\n";
				echo "				<label>Costo unitario</label>\n";
				echo "				<div class='input-group'>\n";
				echo "					<input type='text' class='form-control inputmask-decimal' id='subtot' name='subtot' value=\"".number_format($rsr[0]['subtotale']/$rsr[0]['qta'], get_var("Cifre decimali"), ".", "")."\"><span class='input-group-addon'>&euro;</span>\n";
				echo "				</div>\n";
				echo "			</div>\n";
				echo "		</div>\n";
			//Unità di misura
				echo "		<div class='col-md-3'>\n";
				echo '			{[ "type": "select", "label": "'. _("Unità di misura").'",   "name": "um", "required": 0, "value": "'.$rsr[0]['um'].'", "ajax-source": "misure" ]}';
				echo "		</div>\n";
				if ($mod_mag != '0'	) {
				// Magazzino
					$query = "SELECT * FROM mg_magazzini";
					$rs = $dbo->fetchArray($query);	
					echo "		<div class='col-md-4'>\n";
					echo "			<label>Magazzino</label>\n";
						echo "			<select id='idmagazzino' class='superselect' name=\"idmagazzino\">\n";														
						echo "					<option value='".$id_magazzino_e."'>$nome_magazzino</option>\n";	
						for( $i=0; $i<sizeof($rs); $i++ ){				
							echo "	<option value='".$rs[$i]['id']."' $attr>".$rs[$i]['nome']."</option>\n";
						}	
						// echo "	<option value='0' $attr>Principale</option>\n";
						echo "			</select>\n";	
					echo "		</div>\n";	
				}
				
			echo "		</div>\n";	
		echo "		</div>\n";		
	echo "		</div>\n";		
	
	
	
	
	echo "<div class='panel panel-primary'>";
		echo "<div class='panel-body'>";
			echo "	<div class='row'>\n";		
			//Sconto
				echo "		<div class='col-md-3'>\n";
					echo "			<div class='form-group'>\n";
					echo "				<label>Sconto</label>\n";
					echo "				<div class='input-group'>\n";
					if($rsr[0]['tipo_sconto']=='2'){
						echo "					<input type='text'  class='form-control text-right'  id='sconto' name='sconto' value=\"".number_format($rsr[0]['sconto_primo'], get_var("Cifre decimali"), ".", "")."\"  />\n";
					}else{
						echo "					<input type='text'  class='form-control inputmask-decimal'  id='sconto' name='sconto' value=\"".number_format($rsr[0]['sconto']/$rsr[0]['qta'], get_var("Cifre decimali"), ".", "")."\"  />\n";
					}
					
					echo "				</div>\n";
					echo "			</div>\n";
				echo "		</div>\n";
			//Sconto 1
				echo "		<div class='col-md-3'>\n";
					echo "		<div class='form-group'>\n";
					echo "			<label>Extra</label>\n";
					echo "				<div class='input-group'>\n";
					if($rsr[0]['tipo_sconto']=='2'){
						echo "					<input type='text'  class='form-control inputmask-decimal'  id='sconto_extra' name='sconto_extra' value=\"".number_format($rsr[0]['sconto_extra'], get_var("Cifre decimali"), ".", "")."\"  />\n";
					}else{
						echo "					<input type='text'  class='form-control inputmask-decimal'  id='sconto_extra' name='sconto_extra' value=\"".number_format($rsr[0]['sconto_extra']/$rsr[0]['qta'], get_var("Cifre decimali"), ".", "")."\"  />\n";
					}
					echo "				</div>\n";
					echo "			</div>\n";
				echo "		</div>\n";
			//Sconto
				echo "		<div class='col-md-3'>\n";
				echo "			<div class='form-group'>\n";
				echo "				<label>Extra</label>\n";
				echo "				<div class='input-group'>\n";
				if($rsr[0]['tipo_sconto']=='2'){
					echo "					<input type='text'  class='form-control inputmask-decimal'  id='sconto_extra1' name='sconto_extra1' value=\"".number_format($rsr[0]['sconto_extra1'], get_var("Cifre decimali"), ".", "")."\"  />\n";
				}else{
					echo "					<input type='text'  class='form-control inputmask-decimal'  id='sconto_extra1' name='sconto_extra1' value=\"".number_format($rsr[0]['sconto_extra1']/$rsr[0]['qta'], get_var("Cifre decimali"), ".", "")."\"  />\n";
				}		
				echo "				</div>\n";
				echo "			</div>\n";
				echo "	</div>\n";				
				//Tipo Sconto
				$query = "SELECT * FROM co_tipisconto";
				$rs = $dbo->fetchArray($query);	
				echo "		<div class='col-md-3'>\n";
				echo "			<label>Tipologia Sconto</label>\n";
					echo "			<select id='idtiposconto' class='superselect' name=\"idtiposconto\">\n";														
					if($rsr[0]['tipo_sconto']=='2'){
						echo "					<option value='2'>Percentuale</option>\n";	
					}else{
						echo "					<option value='1'>Fisso</option>\n";	
						
					}	
					for( $i=0; $i<sizeof($rs); $i++ ){				
						echo "	<option value='".$rs[$i]['id']."' $attr>".$rs[$i]['descrizione']."</option>\n";
					}	
					echo "			</select>\n";	
				echo "		</div>\n";
			echo "		</div>\n";		
		echo "		</div>\n";	
	echo "	</div>\n";
	
	
	
	
	echo "<div class='panel panel-primary'>";
		echo "<div class='panel-body'>";
			echo "	<div class='row'>\n";			
			//Iva 
				echo "		<div class='col-md-6'>\n";
					echo "			<label>Iva</label>\n";
					echo "			<select id='idiva' class='superselect' name=\"idiva\">\n";
						echo "				<option value='0'>- Seleziona un valore -</option>\n";
							$query = "SELECT * FROM co_iva ORDER BY descrizione ASC";
							$rs = $dbo->fetchArray($query);						
							for( $i=0; $i<sizeof($rs); $i++ ){
								(  $rs[$i]['id'] == $rsr[0]['idiva'] ) ? $attr=' selected="selected"' : $attr='';
								echo "					<option value='".$rs[$i]['id']."' $attr>".$rs[$i]['descrizione']."</option>\n";
							}				
					echo "			</select>\n";	
				echo "			</div>\n";
				//Conto
				echo "		<div class='col-md-6'>\n";
					echo "			<div class='form-group'>\n";
						echo "				<label>Conto</label>\n";
						echo "				<select id='idconto_riga' class='superselect' name=\"idconto_riga\">\n";
						$query = "SELECT co_pianodeiconti3.id, CONCAT_WS( ' ', co_pianodeiconti3.numero, co_pianodeiconti3.descrizione ) AS descrizione FROM co_pianodeiconti3 INNER JOIN (co_pianodeiconti2 INNER JOIN co_pianodeiconti1 ON co_pianodeiconti2.idpianodeiconti1=co_pianodeiconti1.id) ON co_pianodeiconti3.idpianodeiconti2=co_pianodeiconti2.id  WHERE co_pianodeiconti1.descrizione='Economico' AND co_pianodeiconti3.dir = '".$dir."' ORDER BY co_pianodeiconti3.numero ASC";
						$rs = $dbo->fetchArray($query);
						
						echo "					<option value=''> - Seleziona un'opzione -</option>\n";
						
						for( $i=0; $i<sizeof($rs); $i++ ){
							(  $rs[$i]['id'] == $idconto_fattura ) ? $attr=' selected="selected"' : $attr='';
							echo "				<option value='".$rs[$i]['id']."' $attr>".$rs[$i]['descrizione']."</option>\n";
						}
						echo "				</select>\n";	
					echo "			</div>\n";	
				echo " 		</div>\n";								
			echo "		</div>\n";
		echo "		</div>\n";
	echo "		</div>\n";
	
	
	//Installazioni
	echo "<div class='panel panel-primary'>";
		echo "<div class='panel-body'>";
			echo "	<div class='row'>\n";
				//Tipo Sconto
				
				if( $id_contratto > "0" ) {
					$query="SELECT my_impianti.matricola as id , nome  FROM my_impianti INNER JOIN my_impianti_contratti ON my_impianti.matricola=my_impianti_contratti.matricola WHERE 
					idanagrafica='".$idanagrafica."' and idcontratto ='".$id_contratto."' ORDER BY nome"; 										
				}else{
					$query="SELECT my_impianti.matricola as id , nome  FROM my_impianti  WHERE idanagrafica='".$idanagrafica."'  ORDER BY nome"; 	
				}	
			
				
				$rs = $dbo->fetchArray($query);	
				echo "		<div class='col-md-4'>\n";
				echo "			<label>Installazione Impianti</label>\n";
					echo "			<select id='id_matricola' class='superselect' name=\"id_matricola\">\n";														
					echo "					<option value='".$rsr[0]['id_impianto']."'>$nome_impianto</option>\n";	
					
					if( $rsr[0]['id_impianto'] != "0"  )	echo "	<option value='0'>Nessuna Installazione</option>\n";	
					
					for( $i=0; $i<sizeof($rs); $i++ ){				
						echo "	<option value='".$rs[$i]['id']."' $attr>".$rs[$i]['nome']."</option>\n";
					}	
					echo "			</select>\n";	
				echo "		</div>\n";						
			echo "		</div>\n";
		echo "		</div>\n";
	echo "		</div>\n";	
	
	
	
	echo "<div class='panel panel-primary'>";
		echo "<div class='panel-body'>";		
			echo "	<div class='row'>\n";	
											
				
				if ($per_utile_agente != '0'	) {
					// Utile agente
					echo "		<div class='col-md-2'>\n";
					echo "			<div class='form-group'>\n";
					echo "				<label>% Agente</label>\n";
					echo "				<div class='input-group'>\n";
					echo "					<input type='text' class='form-control inputmask-decimal' id='per_agente' maxlength='8' name='per_agente' value=\"".$per_utile_agente."\"><span class='input-group-addon'>".$um."</span>\n";
					echo "				</div>\n";
					echo "			</div>\n";
					echo "		</div>\n";		
				}
				
				
				
				//Rivalsa INPS
				if( get_var("Percentuale rivalsa INPS") != ""  ){
					echo "		<div class='col-md-5'>\n";
					echo "			<label>Rivalsa INPS:</label>\n";
					echo "			<select id='idrivalsainps' class='superselect' name=\"idrivalsainps\">\n";
					echo "				<option value=''>-</option>\n";
				
					$query = "SELECT * FROM co_rivalsainps";
					$rs = $dbo->fetchArray($query);
					for( $i=0; $i<sizeof($rs); $i++ ){
						( $rs[$i]['id'] == $rsr[0]['idrivalsainps'] ) ? $attr='selected="true"' : $attr='';
						echo "				<option value='".$rs[$i]['id']."' ".$attr.">".$rs[$i]['descrizione']."</option>\n";
					}				
					echo "			</select>\n";
					echo "		</div>\n";
				}
	
				//Ritenuta d'acconto
				if( get_var("Percentuale ritenuta d'acconto") != ""  ){
					echo "		<div class='col-md-5'>\n";
					echo "			<label>Ritenuta d'acconto:</label>\n";
					echo "			<select id='idritenutaacconto' class='superselect' name=\"idritenutaacconto\">\n";
					echo "				<option value=''>-</option>\n";
					$query = "SELECT * FROM co_ritenutaacconto";
					$rs = $dbo->fetchArray($query);
					for( $i=0; $i<sizeof($rs); $i++ ){
						( $rs[$i]['id'] == $rsr[0]['idritenutaacconto'] ) ? $attr='selected="true"' : $attr='';
						echo "				<option value='".$rs[$i]['id']."' ".$attr.">".$rs[$i]['descrizione']."</option>\n";
					}
				
					echo "			</select>\n";	
					echo "		</div>\n";
				}
			echo "	</div>\n";
		echo "	</div>\n";
	echo "	</div>\n";
	
	
	
	
	
	
	echo "	<a onclick=\"addriga();\" class=\"btn btn-primary pull-right\"><i class=\"fa fa-edit\"></i> Modifica</a>\n";
	echo "</form>\n";
?>

	<script type="text/javascript">
	
		function addriga(){
			if( $('#descrizione').val()!='' && $('#idiva option:selected').val()!='' ){
				$("#link_form").submit();
			}
		}
		
		$(document).ready( function(){
		
			start_superselect();
			start_inputmask();
		
		});
	</script>

	<div class="clearfix"></div>

