<?php		
session_start();
require_once ("../../config/Cado.php");
require_once ("../../config/Datos.php");
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once("../producto/cProductomarcacodigo.php");
$oProductomarcacodigo = new cProductomarcacodigo();
require_once ("../formatos/formato.php");
if(isset($_SESSION['consumopagar_catid'])){
	$num_rows=count($_SESSION['consumopagar_catid']);
	if($num_rows==0)$num_rows="";
}else{
	$num_rows="";
}
?>
<style>
	#tabla_consumopagar_car td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_consumopagar_car td.editable,#tabla_consumopagar_car td.editable *{
		cursor: text !important;
		height: 100%; 
		border-bottom-color: #ccc;
		border-bottom-style: dashed; 
		border-bottom-width: 0.5px;
	}
	#tabla_consumopagar_car td.editando{
		background-color: rgba(18, 180, 243, 0.05);
		outline: 2px solid #35A4F3;
	}
	#tabla_consumopagar_car td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_consumopagar_car td input:focus{
		background: transparent;
		height: 100%;
		width: 100%;
		outline: none;
		border: none;
	}
	#tabla_consumopagar_car td input[readonly]{
		background: transparent;
	}
</style> 
			
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:0px;" >
	
	<div class="row" >
	
	<div class="col-lg-8 col-md-12 col-sm-12 col-xs-12" style="padding:0px;" >
	<div class="table-responsive">
	<input name="hdd_ven_numite" id="hdd_ven_numite" type="hidden" value="<?php echo $num_rows?>">
	
	<table id="tabla_consumopagar_car" class="table table-bordered table-striped">
		<thead>
			<tr>
				<th>ARTÍCULO</th>
				<th>MARCA</th>
				<th align="right">CAN</th>
				<th align="right" >V. UNI.</th>		
				<th align="right" >IGV UNI.</th>
				<th align="right" style="background-color:#00ACC1;color:#fff" >PRE. UNI.</th>			
				<th align="right" >VAL. TOTAL</th>
				<th align="right" >IGV TOTAL</th>
				<th align="right" style="background-color:#00ACC1;color:#fff" >PRE. TOTAL</th>
				<th width="25">&nbsp;</th>
			</tr>
		</thead>		 
		<tbody>
		
			<?php 
			$total = 0;
			if($num_rows>0){
				$banrojo = true;
				foreach($_SESSION['consumopagar_catid'] as $indice=>$cantidad) {
				
			$dts1=$oCatalogo->mostrarUno($_SESSION['consumopagar_catid'][$indice],$_SESSION['consumopagar_marid'][$indice]);
			$dt1 = mysqli_fetch_array($dts1);
			$arttip_nom =  $dt1['tb_articulotipo_nom'];
			$emp_fac =  $dt1['tb_empaque_fac'];
			$preven =  $dt1['tb_productomarca_preven'];
			$prevenf =  $dt1['tb_productomarca_prevenf'];
			
			$cantidad=$_SESSION['consumopagar_can'][$indice];
			$can=$_SESSION['consumopagar_can'][$indice];
			
			
			//echo $can.'-' ; 
			
			list($cane,$canf) = explode('F',$can);
		
			//echo $cane.'-' ; 
			
			
			if($cane!=="" && $canf==""){ 
				
				$can = $cane;
			}else if($cane=="" && $canf!==""){ 
				
				$can = $canf;
			}
			else if($cane!=="" && $canf!==""){ 
				$cane = $cane*$emp_fac;
				$can = $cane + $canf;
			}
			
			$des = $_SESSION['consumopagar_des'][$indice] ;
			$valor_uni=$_SESSION['consumopagar_preven'][$indice];
			
			$igv_uni = $valor_uni * $app_igv_igv;
			$pre_uni=$valor_uni * $app_igv_sub;
			$pre_uni=formato_money($pre_uni);
			
			//echo $can;
			
	
			
			$pre_tot = $pre_uni * $can;
			
			$val_tot = $pre_tot/$app_igv_sub;
			$igv_tot = $val_tot*$app_igv_igv;
			
			if($pre_tot<=0){
				$banrojo = false;
			}
					
					
					?>
					<tr style="font-weight:bold;" >
						<td nowrap style="font-size:10pt; font-weight:bold"><?php echo $dt1['tb_producto_nom']?></td>
						<td style="font-weight:bold;" ><?php echo $dt1['tb_marca_nom']?></td>
			
			<td align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold">
				<input type="text" value="<?php echo $cantidad ?>" data-cantidad="<?php echo $can ?>" data-tip = "can" 
				data-preven="<?php echo $valor_uni ?>"
				data-original="<?php echo $cantidad ?>" readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>"
				data-tipdes="<?php echo $tipdes ?>" data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;font-weight:bold;">
			</td>
			
			<td style="font-weight:bold;" align="right"><?php echo formato_money($valor_uni)?></td>
				
			<td style="font-weight:bold;" align="right"><?php echo formato_money($igv_uni)?></td>
				
			<td align="right" width="80" class="editable" style="font-size:14pt; font-weight:bold;background-color:#fff9c4;">
				<input type="text" value="<?php echo formato_money($pre_uni) ?>" 
				data-preven="<?php echo formato_money($pre_uni) ?>" data-tip = "preuni" 
				data-original="<?php echo formato_money($pre_uni) ?>" 
				data-tipdes="<?php echo $tipdes ?>" 
				data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" readonly 
				data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" style="text-align: right;font-weight:bold;">
			</td>
				
			
			<td  align="right" width="100" class="editable" style="font-size:10pt; font-weight:bold" >
			
		
			<input type="text" class="text-red" value="<?php echo formato_money($val_tot) ; ?>" 
							data-original="<?php echo formato_money($val_tot) ?>" 
							data-cantidad="<?php echo $can ?>" data-tip = "valtot" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-des="<?php echo formato_money($des) ?>"
							readonly 
							style="text-align: right;font-weight:bold;">
			
			
			</td>
			<td align="right" width="70" style="font-size: 9pt; font-weight: bold;"> 
			<strong class="text-red"> <?php echo formato_money($igv_tot)?> </strong> 
			</td>
			
			<td align="right" width="100" class="editable" style="font-size:15pt; font-weight:bold;background-color:#fff9c4;">
							<input type="text" class="text-red" value="<?php echo formato_money($pre_tot) ; ?>" 
							data-original="<?php echo formato_money($pre_tot) ?>" 
							data-cantidad="<?php echo $can ?>" data-tip = "pretot" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-des="<?php echo formato_money($des) ?>"
							readonly 
							style="text-align: right;font-weight:bold;">
			</td>
			
						
						
						<td align="center" nowrap="nowrap"> 
						<!--	<span  title="Editar" class="btn btn-primary btn-sm btn_editar" ><i class="fa fa-pencil"></i></span>
							-->
							<span data-indice ="<?php echo $indice; ?>" class="btn btn-danger btn-sm btn_eliminar" ><i class="fa fa-times"></i></span> 		
						<!--	<span class="btn btn-primary btn-sm" id="demo-ajax_<?php echo $dt1['tb_catalogo_id'] ?>" ><i class="fa fa-info"></i></span> 	
						-->
						</td>				
					</tr>
					
					<?php //if ($dt1['tb_producto_des']!=""){?>
					<!--
					<tr style="background-color:#fff;">
					<td colspan="8" >
					<span class="text-red" style="font-weight:bold;">ESPECIFICACIONES: Estructura / Accesorios y Equipamiento.</span>
					<div class="col-lg-12 col-sm-12 col-xs-12" style="word-wrap: break-word;word-break: break-all; font-weight:bold;" >
					<?php echo nl2br($dt1['tb_producto_des']); ?>
					</div>
					
					</td>
					</tr>
					-->
					<?php
					//}
					mysqli_free_result($dts1);
					$total1	+=$pre_tot;
				}
				
				$total_valorventa =$total1/1.18;
			$total_igv	=$total_valorventa*0.18;
			$total	=$total1;
				
			}
			?>
		</tbody>
	</table>
	
	
	<br>
	<?php 
	if($num_rows=="" or $num_rows==0)
		echo 'Ningún producto agregado. <input type="hidden" id="evaluar_cantidad_items_venta" value="'.$cantidad_productos.'">';
	if($num_rows==1)
		echo $num_rows.' producto agregado. <input type="hidden" id="evaluar_cantidad_items_venta" value="'.$cantidad_productos.'">';
	if($num_rows>=2)
		echo $num_rows.' productos agregados. <input type="hidden" id="evaluar_cantidad_items_venta" value="'.$cantidad_productos.'">';
	
	

	?>

		
	</div>		
	</div>		
					
		<div class="col-lg-4 col-sm-12 col-12">
							<div class="row">

							<div class="col-lg-4 col-sm-6 col-4">
								<label>SubTotal:</label>
								<input name="txt_conpag_valven" type="text"class="form-control form-control-lg " id="txt_conpag_valven" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total_valorventa)?>"  > 							
							</div>
						
							<div class="col-lg-4 col-sm-6  col-4">
								<label>IGV:</label>
								<input name="txt_conpag_igv" type="text" class="form-control form-control-lg " id="txt_conpag_igv" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total_igv)?>"   > 
							</div>					
							<div class="col-lg-4 col-sm-6  col-4">
								<label>TOTAL:</label>
								<input name="txt_conpag_totsdes" type="text" class="form-control form-control-lg " id="txt_conpag_totsdes" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total)?>" > 
							</div>
							
							</div>
		</div>
		
		<div class="col-lg-12 col-sm-12 col-12   ">
			<div class="box-footer  div_botones_venta float-right ">	
				<button type="button" data-dismiss="modal" class="btn btn-lg btn-default restablecer">RESTABLECER</button>
				<button class="btn btn-danger btn-lg" id="save_form">GUARDAR</button>
				<div class="col-lg-12 col-md-12 col-12" style="padding:5px;"></div> 
			</div>
		</div>
		
		
		</div>
		
		
		
		</div>
	
	
<script>

	$(function() {
		
		
		
			<?php 
	if($num_rows==0){
	?>
		$("#save_form").hide(100);
	<?php
	}else{
		if($banrojo==true){
	?>
		$("#save_form").show(100);
	<?php 
		}else if($banrojo==false){
	?>
		$("#save_form").hide(100);
	<?php 
		}
	}
	?>
		
		
	<?php if($num_rows > 0){ ?>
		$('#cmb_mone_id').attr('disabled','disabled');
		$('#cmb_puntoconsumopagar_id').attr('disabled','disabled');
		<?php }else{
		?>
		$('#cmb_mone_id').removeAttr("disabled");
		$('#cmb_puntoconsumopagar_id').removeAttr("disabled");
		<?php	
		}?>
	
  var nFilas = $("#hdd_ven_numite").val();
  if(nFilas>0){
  	$('#tabcliente .nav-tabs a:last').tab('show'); 
  }
  $("#txt_importe_cliente").attr('placeholder',$("#txt_conpag_tot").val())
	/* 	$("#txt_importe_cliente").keyup(function() {		
			calcular_vuelto();
		}); */
		
	// $("#txt_conpag_des").keyup(function(e){
				// des = $("#txt_conpag_des").val();
				// /* $("#txt_conpag_des").val(new NumberFormat(des).toFormatted()); */
				// totsindes = <?php echo $total ?>;				
				
								
				// totcondes = totsindes - des;
				// igv = (totcondes*0.18)/1.18;
				// valven = totcondes/1.18;
				
				// $("#txt_conpag_tot").val(new NumberFormat(totcondes).toFormatted());
				// $("#txt_conpag_igv").val(new NumberFormat(igv).toFormatted());
				// $("#txt_conpag_valven").val(new NumberFormat(valven).toFormatted());
				// $("#txt_conpag_totsdes").val(new NumberFormat(totsindes).toFormatted());
				// /* venpag_aut(); */
				
		// });
	// $("#txt_conpag_des").keydown(function(e){
			// if (e.which == 13){
				// totsindes = <?php echo $total  ?>;				
				// des = $("#txt_conpag_des").val();
				
				// totcondes = totsindes - des;
				
				// $("#txt_conpag_tot").val(totcondes.toFixed(2));
				// $("#txt_conpag_totsdes").val(totsindes.toFixed(2));
				// /* venpag_aut(); */
				// return false;
				// }
		// });
	/* 	$('#tabla_consumopagar_car td input[data-cantidad]').autoNumeric({
			aSep: '',
			vMin: '0',
			vMax: '9999999'
		});
		$('#tabla_consumopagar_car td input[data-preven]').autoNumeric({
			aSep: ',',
			vMin: '0',
			vMax: '9999999.9999'
		}); */
		$('#tabla_consumopagar_car td input').keydown(function(ev) {
			if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			
			var cat_id = $(this).attr('data-cat_id');
			var mar_id = $(this).attr('data-mar_id');
			var cantidad = $(this).parent().parent().find('input[data-cantidad]').val();
			var preven = $(this).parent().parent().find('input[data-preven]').val();
			var des = $(this).parent().parent().find('input[data-des]').val();
			var tipdes = $(this).attr('data-tipdes');
			var tip = $(this).attr('data-tip');
			var valor = $(this).val();
			
			if(tip=="des"){
					
				preven =  $(this).attr('data-preven') * 1.18;		
				agregar_producto('agregar', cat_id, cantidad, preven, mar_id,des);
			
			}else if(tip=="pretot"){
					
				preuni = valor.toString().replace(',','')/cantidad;	
				valuni = preuni.toString().replace(',','');
				
				agregar_producto('agregar', cat_id, cantidad, valuni, mar_id,des);
			
			}else if(tip=="preuni"){
					
				valuni = valor.toString().replace(',','');
				
				agregar_producto('agregar', cat_id, cantidad, valuni, mar_id,des);
			
			}else if(tip=="valtot"){
				
				valuni =  valor.toString().replace(',','')/cantidad;
				valuni = valuni * 1.18;
			
				agregar_producto('agregar', cat_id, cantidad, valuni, mar_id,des);
			
			}else if(tip=="can"){
				
				preven =  $(this).attr('data-preven') * 1.18;		
				agregar_producto('agregar', cat_id, cantidad, preven, mar_id,des);
				
			}
				
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
				return false;
			}
		});
		$('#tabla_consumopagar_car td input').parent().click(function(ev) {
			var $input = $(this).find('input');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		});
		$('#tabla_consumopagar_car td input').blur(function(event) {
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		});
		$('#tabla_consumopagar_car td input').keydown(function(ev) {
			if(ev.keyCode==27){
				$(this).val($(this).attr('data-original'));
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
			}
		});
	/* 	$( ".btn_editar" ).click(function() { 
			var $input = $(this).parent().parent().find('input[data-cantidad]');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		}); */
		$( ".btn_eliminar" ).click(function() { 
			
			var indice = $(this).attr('data-indice');
				
			if(confirm('¿Seguro de quitar el item?')){
				agregar_producto('quitar', indice);
			}
		});
		
		
		$( ".restablecer" ).click(function() { 
			consumopagar_form('insertar');
		});
		
		
		
	}); 
</script>