<?php defined('_JEXECGS') or die ('Acceso restringido'); ?>
<script type="text/javascript">
	(function(a){a.fn.validaCampo=function(b){a(this).on({keypress:function(a){var c=a.which,d=a.keyCode,e=String.fromCharCode(c).toLowerCase(),f=b;(-1!=f.indexOf(e)||9==d||37!=c&&37==d||39==d&&39!=c||8==d||46==d&&46!=c)&&161!=c||a.preventDefault()}})}})(jQuery);
	$(function(){
        $('#txtNueva1').validaCampo('abcdefghijklmnñopqrstuvwxyzáéiou1234567890');
		$('#txtNueva2').validaCampo('abcdefghijklmnñopqrstuvwxyzáéiou1234567890');
		$('#txtActual').validaCampo('abcdefghijklmnñopqrstuvwxyzáéiou1234567890');
    });
	function send(){
		var ruta = 'controller/ctrlPedido.php?opt=grabar';	
		document.frm.action = ruta;
		document.frm.submit();
	}
	function nuevo(){
		var ruta = 'controller/ctrlPedido.php?opt=nuevo';	
		document.frm.action = ruta;
		document.frm.submit();
	}
	function MM_openBrWindow(theURL,winName,features) { //v2.0
	  window.open(theURL,winName,features);
	}
	function MET_consulta(){
		MM_openBrWindow('/shine/form/consultaProducto.php','winConsSolicitudes','status=yes,scrollbars=yes,resizable=yes,width=700,height=450');
	}
	
	function MET_imprimir(id){
		MM_openBrWindow('/shine/form/rptPedido.php?id='+id,'winConsSolicitudes','status=yes,scrollbars=yes,resizable=yes,width=700,height=450');
	}
	
	function MET_quitar(){
        try {
			var cnt_filas = parseInt(document.getElementById('txtNumItems').value);
            var tblDetalle = document.getElementById('tblDetDetConKardex');
            var rowCount = tblDetalle.rows.length;
            for(var i=0; i<rowCount; i++) {
                var row = tblDetalle.rows[i];
                var chkbox = row.cells[5].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    tblDetalle.deleteRow(i);
                    rowCount--;
                    cnt_filas=cnt_filas-1;
                    i--;
                }
            }
			document.getElementById('txtNumItems').value = cnt_filas;
        }catch(e) {
            alert(e);
        }
		
        met_total();
    }
</script>
<script type="text/javascript">

          function addDetalle(tableID, valor1, valor2, valor3, valor4, valor5) {
			  var cont_item = parseInt(document.getElementById('txtNumItems').value);
			  cont_item++;
			  //valor1-->codigo
			  //valor2-->nombre
			  //valor3-->cantidad
			  //valor4-->precio
			  //valor5-->total
			  
              if(valor2.length>0){
               var table = document.getElementById(tableID);
               var rowCount = table.rows.length;
               var row = table.insertRow(rowCount);
               var cell1 = row.insertCell(0);
			   
			   
			   var element1 = document.createElement("input");
               element1.type = "text";
			   element1.name = "txt_idArticulo"+cont_item;
			   element1.id = "txt_idArticulo"+cont_item;
               if(valor1.length>0){
                   element1.setAttribute("value",valor1);
               }
               element1.setAttribute("class","map-ctrl-input");
               element1.setAttribute("size","12");
               element1.setAttribute("readonly","readonly");
               cell1.appendChild(element1);
              

               var cell2 = row.insertCell(1);
                
               var element2   = document.createElement("input");;
               element2.type = "text";
			   element2.name = "txt_NomArticulo"+cont_item;
			   element2.id = "txt_NomArticulo"+cont_item;
               if(valor2.length>0){
                   element2.setAttribute("value",valor2);
               }
               element2.setAttribute("class","map-ctrl-input");
               element2.setAttribute("size","46");
               element2.setAttribute("readonly","readonly");
               cell2.appendChild(element2);
               
               
               var cell3 = row.insertCell(2);

               var element3    = document.createElement("input");
                
               element3.type = "text";
			   element3.name = "txt_idCantidad"+cont_item;
			   element3.id = "txt_idCantidad"+cont_item;
               if(valor3>0){
                    element3.setAttribute("value",valor3);
               }
               element3.setAttribute("class","map-ctrl-input");
               element3.setAttribute("size","12");
              // element3.setAttribute("readonly","readonly");
               cell3.appendChild(element3);
               
               var cell4 = row.insertCell(3);
                
               var element4   = document.createElement("input");;
               element4.type = "text";
			   element4.name = "txt_idPrecio"+cont_item;
			   element4.id = "txt_idPrecio"+cont_item;
                if(valor4>0){
                   element4.setAttribute("value",valor4);
               }
                element4.setAttribute("class","map-ctrl-input");
                element4.setAttribute("size","12");
                element4.setAttribute("readonly","readonly");
                cell4.appendChild(element4);  

		var cell5 = row.insertCell(4);
                
               var element5   = document.createElement("input");;
               element5.type = "text";
			   element5.name = "txt_idTotal"+cont_item;
			   element5.id = "txt_idTotal"+cont_item;
                if(valor5>0){
                   element5.setAttribute("value",valor5);
               }
                element5.setAttribute("class","map-ctrl-input");
                element5.setAttribute("size","12");
                element5.setAttribute("readonly","readonly");
                cell5.appendChild(element5);  

				document.getElementById('txtNumItems').value = cont_item;
				
				var cell6 = row.insertCell(5);
				var element6 = document.createElement("input");
				element6.type = "checkbox";
				element6.setAttribute("class","map-ctrl-input");
				cell6.appendChild(element6);

              }
          }

function MET_anadir(){//valor1-->codigo
			  //valor2-->nombre
			  //valor3-->cantidad
			  //valor4-->precio
			  //valor5-->total
	var codigo = document.getElementById('txtCodItem').value;
	var nombre = document.getElementById('txtNomItem').value;
	var cantidad = parseFloat(document.getElementById('txtCantidad').value);
	var precio = parseFloat(document.getElementById('txtPrecio').value);
	var total = (parseFloat(precio) * parseFloat(cantidad));
	if(total>0){
		addDetalle("tblDetDetConKardex", codigo, nombre, cantidad, precio, total);
	}else{
		alert("Debe seleccionar un producto");
	}
	document.getElementById('txtCodItem').value="";
	document.getElementById('txtNomItem').value="";
	document.getElementById('txtCantidad').value="";
	document.getElementById('txtPrecio').value="";
	met_total();
}


 function consultaProducto(codigo, nombre, precio, stock){
	 document.getElementById('txtCodItem').value = codigo.toString();
	 document.getElementById('txtNomItem').value = nombre.toString();
	 document.getElementById('txtPrecio').value = precio.toString();
	 document.getElementById('txtCantidad').value = stock.toString();
 }
 
 function met_total(){
	 var cnt_filas = parseInt(document.getElementById('txtNumItems').value);
	 var val_total = 0;
	 var control="";
	 if(cnt_filas > 0){
		 for(var i=1; i<=cnt_filas; i++){
			 control = "txt_idTotal"+i;
			 val_total =  val_total + parseFloat(document.getElementById(control).value);
		 }
	 }else{
	 }
	 document.getElementById('txtTotal').value = val_total;
 }
		  
</script>
<?php
if (isset($_GET['id'])){
	if ($_GET['id'] > 0){
		include_once($_SERVER['DOCUMENT_ROOT'].'/shine/model/clsPedido.php');
		$objPedido = new clsPedido();
		$cod_cia = $_SESSION['num_identificacion'];
		$reg = $objPedido->MET_consultaC($_GET['id'], $cod_cia) ;
		$registros = $objPedido->MET_consultaD($_GET['id'], $cod_cia) ;
	//	print_r($reg);
	}
}
?>
<form id="frm" name="frm" method="post">
<h2>Registro de pedidos</h2>
<table>
<tr>
<td>
<p>Codigo</p>
</td>
<td>
<input name="txtCodigo" type="text" class="txt_textbox" disabled id="txtCodigo" size="25" title="No usar espacios en blanco" value="<?php echo (isset($reg))? $reg['cod_pedido']:''; ?>" />
</td>
</tr>

<tr>
<td>
<p>Fecha</p>
</td>
<td>
<input name="txtFecha" type="text" class="txt_textbox" disabled id="txtFecha" size="25" title="No usar espacios en blanco" value="<?php echo (isset($reg))? $reg['fec_pedido']:''; ?>" />
</td>
</tr>

<tr>
<td>
<p>Observacion</p>
</td>
<td>
<input name="txtObservacion" type="text" class="txt_textbox" id="txtObservacion" size="50" title="No usar espacios en blanco" value="<?php echo (isset($reg))? $reg['obs_pedido']:''; ?>" />
</td>
</tr>

<tr>
<td>
<p>Subtotal</p>
</td>
<td>
<input name="txtSubTotal" type="text" class="txt_textbox" disabled id="txtSubTotal" size="25" title="No usar espacios en blanco" value="<?php echo (isset($reg))? $reg['val_subtotal']:''; ?>" />
</td>
</tr>

<tr>
<td>
<p>Descuento</p>
</td>
<td>
<input name="txtDescuento" type="text" class="txt_textbox" disabled id="txtDescuento" size="25" title="No usar espacios en blanco" value="<?php echo (isset($reg))? $reg['val_desto']:''; ?>" />
</td>
</tr>

<tr>
<td>
<p>Total</p>
</td>
<td>
<input name="txtTotal" type="text" class="txt_textbox" disabled id="txtTotal" size="25" title="No usar espacios en blanco" value="<?php echo (isset($reg))? $reg['val_total']:''; ?>" />
</td>
</tr>



<tr>
<td>
<p><h3>Adicionar productos</h3></p>
</td>
</tr>

<tr>
<td colspan="2">
<input name="txtCodItem" type="text" class="txt_textbox" disabled id="txtCodItem" size="10" title="No usar espacios en blanco" value="" />
<img width="16px" height="16px" src="/shine/images/search.png" onclick="MET_consulta();">
<input name="txtNomItem" type="text" class="txt_textbox" disabled id="txtNomItem" size="65" title="No usar espacios en blanco" value="" />
<input name="txtPrecio" type="text" class="txt_textbox" disabled id="txtPrecio" size="10" title="No usar espacios en blanco" value=""/>
<input name="txtCantidad" type="text" class="txt_textbox" id="txtCantidad" size="10" title="No usar espacios en blanco" value="" />
<img width="16px" height="16px" src="/shine/images/add.png" onclick="MET_anadir();">
<img width="16px" height="16px" src="/shine/images/remove.png" onclick="MET_quitar();">
</td>
</tr>


<table id="tblCabDetalle" height="20px"  border="0">
	<tr>
		<th bgcolor="#f0f0f0" width="100px">Codigo</th>
		<th bgcolor="#f0f0f0" width="300px">Nombre</th>
		<th bgcolor="#f0f0f0" width="100px">Cantidad</th>
		<th bgcolor="#f0f0f0" width="100px">Precio</th>
		<th bgcolor="#f0f0f0" width="100px">Total</th>
	</tr>
</table>
<table name="tblDetDetConKardex" id="tblDetDetConKardex" height="20px" width="50%" border="0"></table>

<input name="txtNumItems" type="hidden" class="txt_textbox" id="txtNumItems" size="10" value="0" />

<tr>
<td colspan="2" style="text-align:right;">
<input name="btn_ingresar" type="button" class="txt_textbox" id="btn_ingresar" value="Grabar" style="width:70px" onClick="send();" />
<input name="btn_imprimir" type="button" class="txt_textbox" id="btn_imprimir" value="Imprimir" style="width:70px" 
onClick="MET_imprimir(<?php if (isset($_GET['id'])){
if ($_GET['id'] > 0){ echo $_GET['id'];}} ?>);" />
<input name="btn_nuevo" type="button" class="txt_textbox" id="btn_nuevo" value="Nuevo" style="width:70px" onClick="nuevo();" />
</td>
</tr>
</table>
</form>
<?php 
if(isset($registros)){
	foreach($registros as $ind => $reg2){
		echo "<script type='text/javascript'>
		addDetalle(
			'tblDetDetConKardex',
			'".$reg2['cod_item']."',
			'".$reg2['nom_item']."',
			'".$reg2['num_cantidad']."',
			'".$reg2['val_precio']."',
			'".$reg2['val_totald']."'
		);	
		</script>";		
		} //end foreach
}	
?>

