<?php





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

require_once ("../cotizacion/cCotizacion.php");

$oCotizacion = new cCotizacion();



require_once ("../producto/cCatalogo.php");

$oCatalogo = new cCatalogo();



require_once ("../formatos/formato.php");



$dts = $oCotizacion->mostrarUno($_POST['cotid']);

$dt = mysqli_fetch_array($dts);

  $fec	=mostrarFecha($dt['tb_cotizacion_fec']);

  $doc_id	=$dt['tb_documento_id'];

  $numdoc	=$dt['tb_cotizacion_numdoc'];

  $cli_id	=$dt['tb_cliente_id'];



 

  $des	=$dt['tb_cotizacion_des'];

 

mysqli_free_result($dts);



$dtsc=$oCotizacion->mostrarCotizaciondetalle($_POST['cotid']);

$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 " >

<table cellspacing="1" id="tabla_venta_car" class="table table-bordered table-striped"> 

 

    <thead>

        <tr>

				<th>CÓDIGO</th>

				<th>ARTÍCULO</th>

				<th>MARCA</th>

				<th>UNIDAD</th>

				<th align="right">CANT</th>

				<th align="right" >V. UNI.</th>	

				<th align="right" style="background-color:#a5d6a7;"  >DSCT. 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" style="background-color:#a5d6a7;"  >DSC. TOT.</th>		

				

				<th align="right" >IGV TOTAL.</th>

				<th align="right" style="background-color:#fff9c4;" >PRECIO TOTAL.</th>

				<th align="right" style="background-color:#ef9a9a;" >C/DSCT.</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_cotizaciondetalle_can']*1;		

			$valor_uni=$dtc['tb_cotizaciondetalle_valuni'];

			$des_uni = $dtc['tb_cotizaciondetalle_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><?php echo $cod ;?></td>

          <td><?php  echo '<strong>'.$dtc['tb_cotizaciondetalle_nom'].'</strong>';?></td>

          <td><?php echo $dt1['tb_marca_nom'];?></td>

          <td><?php echo $dt1['tb_unidad_nom']?></td>

          <td align="right"><?php echo $can ;?></td>

          <td align="right"><?php echo formato_money($valor_uni) ;?></td>

          <td align="right"><?php echo formato_money($des_uni) ;?></td>

          <td align="right"><?php echo formato_money($igv_uni) ;?></td>

          <td align="right"><?php echo formato_money($pre_uni) ;?></td>

          <td align="right"><?php echo formato_money($val_tot) ;?></td>

          <td align="right"><?php echo formato_money($des_tot) ;?></td>

          <td align="right"><?php echo formato_money($igv_tot) ;?></td>

          <td align="right"><?php echo formato_money($pre_tot) ;?></td>         

          <td align="right"><?php echo formato_money($pre_tot_cdes) ;?></td>         

		</tr>

		

		

		

		<?php 

		}else{		

		?>

        <tr>

          <td><?php echo $cod ;?></td>

          <td><?php  echo '<strong>'.$dtc['tb_cotizaciondetalle_nom'].'</strong>';?></td>

          <td><?php echo $dt1['tb_marca_nom'];?></td>

          <td><?php echo $dt1['tb_unidad_nom']?></td>

        

		<td align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold">

				<input type="text" value="<?php echo $can ?>" data-cantidad="<?php echo $can ?>" data-tip = "can"

				data-preven="<?php echo $valor_uni ?>"

				data-original="<?php echo $can ?>" readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>"

				data-detid="<?php echo $dtc['tb_cotizaciondetalle_id']; ?>" data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">

		</td>

        

		<td align="right"><?php echo formato_money($valor_uni) ;?></td>



        <td 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-detid="<?php echo $dtc['tb_cotizaciondetalle_id']; ?>" 

				data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" readonly 

				data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" style="text-align: right;">

		</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_uni) ?>"

							data-detid="<?php echo $dtc['tb_cotizaciondetalle_id']; ?>" 

							readonly 

							style="text-align: right;">		

			</td>



		  <td align="right"><?php echo formato_money($igv_tot) ;?></td>

         

		  <td align="right" 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_uni) ?>"

							data-detid="<?php echo $dtc['tb_cotizaciondetalle_id']; ?>" 

							readonly 

							style="text-align: right;">

			</td>

         

		<td>

				<span data-indice ="<?php echo $dtc['tb_cotizaciondetalle_id']; ?> " class="btn btn-danger btn-sm btn_eliminar" ><i class="fa fa-remove"></i></span> 			

		</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 class="tab-content col-lg-4 col-md-4 col-sm-4 col-xs-12" id="">

				<div class="row">

					<div class="table-responsive">

						<table class="table" style="font-size:2rem;border:solid 1px #fbc02d;" >

							<tbody>

							<tr>

								<th style="width:50%">VALOR VENTA:</th>

								<td>

									<input name="txt_ven_valven_vis" type="text" id="txt_ven_valven_vis" 

									style="text-align:right;font-weight: bold;border:0px" value="<?php echo formato_money($valven)?>"  readonly> 

								</td>

							</tr>

						</tbody></table>

						

					</div>

				</div>

</div>

	

	<div class="tab-content col-lg-4 col-md-4 col-sm-4 col-xs-12" id="">

				<div class="row">

					<div class="table-responsive">

						<table class="table" style="font-size:2rem;border:solid 1px #fbc02d;" >

							<tbody>

							<tr>

								<th>IGV:</th>

								<td>

									<input name="txt_ven_igv_vis" type="text" id="txt_ven_igv_vis" style="text-align:right;font-weight: bold;border:0px" 

									value="<?php echo formato_money($igv)?>"   readonly> 

								</td>

							</tr>

						</tbody></table>

						

					</div>

				</div>

			</div>

			

			<div class="tab-content col-lg-4 col-md-4 col-sm-4 col-xs-12" id="">

				<div class="row">

					<div class="table-responsive">

						<table class="table" style="font-size:2rem;border:solid 1px #fbc02d;" >

							<tbody>

							

							<tr>

								<th>TOTAL:</th>

								<td>

									<input name="txt_ven_tot_vis" type="text" id="txt_ven_tot_vis" style="text-align:right;font-weight: bold;border:0px" value="<?php echo formato_money($tot)?>" readonly> 

								</td>

							</tr> 

						</tbody></table>

						

					</div>

				</div>

			</div>

				

			



<script type="text/javascript" src="../cotizacion/NumberFormat.js"></script>



<script>

/* 

	function calcular_vuelto(){

		var importe = $("#txt_importe_cliente").val();

		var total = $('#txt_ven_tot').val();	

		

		if(importe==""){

		$('#lbl_ven_vuelto').val('');	

		}

		else{

		

		$.ajax({

			type: "POST",

			url: "../venta/calcular_vuelto.php",

			async:true,

			dataType: "html",                      

			data: ({

				importe: importe,				

				total:	total

			}),

			beforeSend: function(){				



			},

			success: function(vuelto){

				$('#lbl_ven_vuelto').val(vuelto);					

			}

		});	

	}

	} */



	$(function() {



	<?php if($num_rows > 0){ ?>

		$('#cmb_mone_id').attr('disabled','disabled');

		<?php }else{

		?>

		$('#cmb_mone_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_ven_tot").val())



	/* 	$("#txt_importe_cliente").keyup(function() {		

			calcular_vuelto();

		}); */



		

	$("#txt_ven_des").keyup(function(e){

				des = $("#txt_ven_des").val();

				/* $("#txt_ven_des").val(new NumberFormat(des).toFormatted()); */

				totsindes = <?php echo $total ?>;				

				

								

				totcondes = totsindes - des;

				igv = (totcondes*0.18)/1.18;

				valven = totcondes/1.18;

				

				$("#txt_ven_tot").val(new NumberFormat(totcondes).toFormatted());

				$("#txt_ven_igv").val(new NumberFormat(igv).toFormatted());

				$("#txt_ven_valven").val(new NumberFormat(valven).toFormatted());

				$("#txt_ven_totsdes").val(new NumberFormat(totsindes).toFormatted());

				/* venpag_aut(); */

				

		});





	$("#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>



