<?php


require_once ("../../config/Cado.php");

require_once ("../consumopagar/cConsumopagar.php");
$oConsumopagar = new cConsumopagar();

require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once ("../formatos/formato.php");

$dts = $oConsumopagar->mostrarUno($_POST['conpagid']);
$dt = mysqli_fetch_array($dts);
  $fec	=mostrarFecha($dt['tb_consumopagar_fec']);
  $doc_id	=$dt['tb_documento_id'];
  $numdoc	=$dt['tb_consumopagar_numdoc'];
  $cli_id	=$dt['tb_cliente_id'];
 
  $des	=$dt['tb_consumopagar_des'];
 
mysqli_free_result($dts);
$dtsc=$oConsumopagar->mostrarconsumopagardetalle($_POST['conpagid']);
$num_rows= mysqli_num_rows($dtsc);
?>
 
<style>
	#tabla_venta_car td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_venta_car td.editable,#tabla_venta_car td.editable *{
		cursor: text !important;
		height: 100%; 
		border-bottom-color: #ccc;
		border-bottom-style: dashed; 
		border-bottom-width: 0.5px;
	}
	#tabla_venta_car td.editando{
		background-color: rgba(18, 180, 243, 0.05);
		outline: 2px solid #35A4F3;
	}
	#tabla_venta_car td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_venta_car td input:focus{
		background: transparent;
		height: 100%;
		width: 100%;
		outline: none;
		border: none;
	}
	#tabla_venta_car td input[readonly]{
		background: transparent;
	}
</style> 
	
 <div class="col-lg-12 col-md-12 col-xs-12 " >
 	<div class="table-responsive">
 
<table cellspacing="1" id="tabla_venta_car" class="table table-bordered table-striped"> 
 
    <thead>
        <tr>
				<th>ARTÍCULO</th>
				<th>MARCA</th>
				<th>UNIDAD</th>
				<th align="right">CAN</th>
				<th align="right" >VAL. UNI.</th>	
			
				<th align="right" >IGV UNI.</th>
				<th align="right" style="background-color:#fff9c4;" >P. UNI.</th>	
				<th align="right" >VAL. TOTAL</th>		
				<th align="right" >IGV TOTAL.</th>
				<th align="right" style="background-color:#fff9c4;" >PRECIO TOTAL.</th>
				
			</tr>
    </thead>
    <tbody>
    <?php
		while($dtc = mysqli_fetch_array($dtsc))
    {
		
		$dts1=$oCatalogo->mostrarUno($dtc['tb_catalogo_id'],$dtc['tb_marca_id']);
		$dt1 = mysqli_fetch_array($dts1);
		
			$can=$dtc['tb_consumopagardetalle_can']*1;		
			$valor_uni=$dtc['tb_consumopagardetalle_valuni'];
			$des_uni = $dtc['tb_consumopagardetalle_des'];
		
			$igv_uni = $valor_uni *0.18;
			$pre_uni= $valor_uni * 1.18;
			
			$val_tot = $valor_uni * $can;		
			$igv_tot = ($valor_uni *0.18) * $can;
			$pre_tot = ($valor_uni * 1.18) * $can;
			
			$total_valorventa +=$val_tot;
			$total_igv	+=$igv_tot;
			$total	+=$pre_tot;
			
			$cod = $dtc['tb_catalogo_cod'];
		$des_tot = $des_uni * $can;
		
	$pre_tot_cdes = $pre_tot - $des_tot;
		
		//if($_POST['estgenven']==1){
		?>
		
		 <tr>
          <td style="font-weight:bold;" ><?php  echo '<strong>'.$dtc['tb_consumopagardetalle_nom'].'</strong>';?></td>
          <td style="font-weight:bold;" ><?php echo $dt1['tb_marca_nom'];?></td>
          <td style="font-weight:bold;" ><?php echo $dt1['tb_unidad_nom']?></td>
          <td style="font-weight:bold;" align="right"><?php echo $can ;?></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 style="font-weight:bold;" align="right"><?php echo formato_money($pre_uni) ;?></td>
          <td style="font-weight:bold;" align="right"><?php echo formato_money($val_tot) ;?></td>
          <td style="font-weight:bold;" align="right"><?php echo formato_money($igv_tot) ;?></td>
          <td style="font-weight:bold;" align="right"><?php echo formato_money($pre_tot) ;?></td>                
		</tr>
		
		
		
		<?php 
		//}
		$tot	+=$pre_tot_cdes;
		
		$valven +=$tot / 1.18;
		$igv	+=$valven * 0.18;
		
		
		}
    mysqli_free_result($dts1);
   
   ?>
    </tbody>
</table>
<?php 
  if($num_rows=="" or $num_rows==0)echo $num_rows.' Ningún registro';
  if($num_rows==1)echo $num_rows.' registro';
  if($num_rows>=2)echo $num_rows.' registros';
?>
<br/>
</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="" type="text"class="form-control form-control-lg " id="" 
								style="text-align:right;font-weight: bold;" readonly value="<?php echo formato_money($valven)?>"  > 							
							</div>
						
							<div class="col-lg-4 col-sm-6  col-4">
								<label>IGV:</label>
								<input name="" type="text" class="form-control form-control-lg " id="" 
								style="text-align:right;font-weight: bold;" readonly  value="<?php echo formato_money($igv)?>"   > 
							</div>					
							<div class="col-lg-4 col-sm-6  col-4">
								<label>TOTAL:</label>
								<input name="" type="text" class="form-control form-control-lg " id="" 
								style="text-align:right;font-weight: bold;" readonly value="<?php echo formato_money($tot)?>" > 
							</div>
							
							</div>
		</div>
		
<script>

	$(function() {

  
		
	$("#txt_ven_des").keydown(function(e){
			if (e.which == 13){
				totsindes = <?php echo $total  ?>;				
				des = $("#txt_ven_des").val();
				
				totcondes = totsindes - des;
				
				$("#txt_ven_tot").val(totcondes.toFixed(2));
				$("#txt_ven_totsdes").val(totsindes.toFixed(2));
				/* venpag_aut(); */
				return false;
				}
		});
	/* 	$('#tabla_venta_car td input[data-cantidad]').autoNumeric({
			aSep: '',
			vMin: '0',
			vMax: '9999999'
		});
		$('#tabla_venta_car td input[data-preven]').autoNumeric({
			aSep: ',',
			vMin: '0',
			vMax: '9999999.9999'
		}); */
		$('#tabla_venta_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 detid = $(this).attr('data-detid');
			var tip = $(this).attr('data-tip');
			var valor = $(this).val();
			
			if(tip=="pretot"){
					
				preuni = valor.toString().replace(',','')/cantidad;	
				valuni = preuni.toString().replace(',','');
				
				agregar_producto('agregaritem', cat_id, cantidad, valuni, mar_id,des,detid);
			
			}else if(tip=="preuni"){
					
				valuni = valor.toString().replace(',','');
				
				//alert(valuni);
				
				agregar_producto('agregaritem', cat_id, cantidad, valuni, mar_id,des,detid);
			
			}else if(tip=="valtot"){
			
				valuni =  valor.toString().replace(',','')/cantidad;
				valuni = valuni * 1.18;
				
				agregar_producto('agregaritem', cat_id, cantidad, valuni, mar_id,des,detid);
			
			}else if(tip=="can"){
				preven =  $(this).attr('data-preven') * 1.18;
			
				agregar_producto('agregaritem', cat_id, cantidad, preven, mar_id,des,detid);
				
			}	
				
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
				return false;
			}
		});
		$('#tabla_venta_car td input').parent().click(function(ev) {
			var $input = $(this).find('input');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		});
		$('#tabla_venta_car td input').blur(function(event) {
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		});
		$('#tabla_venta_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 detid = $(this).attr('data-indice');
				
			if(confirm('¿Seguro de quitar el item?')){
				agregar_producto('quitar','','','','','',detid,'');
			}
		});
	}); 
</script>