<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once ("../venta/cVentapago.php");
$oVentapago = new cVentapago();
require_once ("../ingreso/cIngreso.php");
$oIngreso = new cIngreso();
require_once ("../formatos/formato.php");
$fecha1 = $_POST['txt_fil_ven_fec1']; 
$fecha2 = $_POST['txt_fil_ven_fec2']; 
$tipmot = $_POST['tipmot'];

$dts2=$oVenta->mostrarUno($_POST['venid']);
$dtv = mysqli_fetch_array($dts2);
$docven = $dtv['tb_documento_abr'];
// echo $tipmot;
if($docven=='B'){
	$docid_not = 30;
}else if($docven=='F')
{
	$docid_not = 13;
}

	
	
?>

<?php if($tipmot==1 || $tipmot==4 || $tipmot==6 ){
	
	$dts1=$oVenta->mostrarUno($_POST['venid']);
	$num_rows= mysqli_num_rows($dts1);
	
	
?>

<div class="tab-content col-lg-12 col-md-12 col-sm-12 col-xs-12" id="">
				<div class="row">
					<div class="table-responsive">
    <table id="tabla_venta" class="table table-bordered table-striped">
		   
    <thead>
    <tr>
      <th>FECHA</th>
      <th>DOCUMENTO</th>
      <th>CLIENTE</th>
      <th>RUC/DNI</th>
      <th>VENDEDOR</th>
      <th align="right">TOTAL</th>
      <th align="right">PAGOS</th>
      <th align="right">SALDO</th>
      <th ></th>
      
    </tr>
  </thead> 
            <tbody>
    <?php
    if($num_rows>0){
    while($dt1 = mysqli_fetch_array($dts1))
    {
     
	 $mon_id = $dt1['tb_moneda_id'];
	 $punven_id = $dt1['tb_puntoventa_id'];
	 
      $total_valven+=$dt1['tb_venta_valven'];
      $total_igv+=$dt1['tb_venta_igv'];
      $total_ventas +=$dt1['tb_venta_tot'];
	  $reg = mostrarFechaHora($dt1['tb_venta_reg']);
 
      $total_pagar += $dt1['tb_venta_tot'];
	  
	  
	  $dtvp = $oVentapago->filtrar($dt1['tb_venta_id']);
      $dtv = mysqli_fetch_array($dtvp); 
        $monto = $dtv['tb_ventapago_mon']; 
        $vping = $dtv['tb_ventapagoingresos_id']; 
        
		//echo '--'.$vping;
		
		$abonos=0;
        $mod_id=3;
        $est1=1;
        $est2=2;
       $dts3 = $oIngreso->mostrar_por_modulo($mod_id,$vping,$est1,$est2);
        while($dt3 = mysqli_fetch_array($dts3))
        {
          $abonos+= $dt3['tb_ingreso_imp'];
        } 
        mysqli_free_result($dts3);
        $saldo = ($monto - $abonos);
        $total_abono+=$abonos;
        $total_saldo+=$saldo; 
    ?>
    <tr >
      <td title="FECHA DE REGISTRO : <?php echo "$reg";  ?>" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_venta_fec'])?></td>
      <td nowrap="nowrap"><?php echo $dt1['tb_documento_abr'].' '.$dt1['tb_venta_numdoc']?></td>
      <td><?php echo $dt1['tb_cliente_nom'];?></td>
      <td><?php echo $dt1['tb_cliente_doc'];?></td>
      <td align="right"><?php echo $dt1['tb_usuario_nom'].' '.$dt1['tb_usuario_ape']?></td>
      <td align="right"><?php echo formato_money($dt1['tb_venta_tot'])?></td>
      <td align="right"><?php echo formato_money($abonos);?></td>
      <td align="right"><?php echo formato_money($saldo);?></td>
      <td align="right">
	   <?php
		if($dt1['tb_venta_est']==1){
			?>
			<small class="badge badge-success" style="cursor:pointer;" title="SE EMITIRA UNA NOTA DE CREDITO CON ESTE DOCUMENTO">DISPONIBLE</small>
			<?php 
		}else{
			?>
			<small class="badge badge-danger" style="cursor:pointer;" title="YA ESTA ANULADA" >ANULADA</small>
			<?php 
		}
	  ?>
	  </td>
     
    </tr>
    <?php
  }
  mysqli_free_result($dts1);
  }
  ?> 
			<tr class="even">
			<td colspan="">TOTAL</td>
			<td colspan=""></td>
			<td colspan=""></td>
			<td colspan=""></td>
			<td colspan=""></td>
			<td align="right"><strong><?php echo formato_money($total_pagar)?></strong></td>
			<td align="right"><strong><?php echo formato_money($total_abono)?></strong></td>
			<td align="right"><strong><?php echo formato_money($total_saldo)?></strong></td>
			
			</tr>
		</tbody>
        </table>
		
		</div>
	</div>
</div>
		
		<div class="tab-content col-lg-6 col-md-4 col-sm-4 col-xs-12" id="">
				<div class="row">
					<div class="table-responsive">
						<table class="table">
							<tbody>
							<tr>
								<th>Monto:</th>
								<td>
									<input type="hidden" name="txt_docrel_mon" id="txt_docrel_mon" value="<?php echo moneda_mysql(formato_money($total_pagar));?>" />
									<input name="txt_notcre_mon" class="form-control" type="text" id="txt_notcre_mon" style="text-align:right;font-weight: bold;" autocomplete="off" value="0" > 
								</td>
							</tr>							
						</tbody>
						</table>
					</div>
				</div>
			</div>
			
	<?php }else if($tipmot==5 || $tipmot==7 ){ 
	
	
		require_once ("../producto/cCatalogo.php");
		$oCatalogo = new cCatalogo();

		//ini_set('display_errors',1);
	
	
		if(isset($_SESSION['notacredito_catid'])){
			$num_rows=count($_SESSION['notacredito_catid']);
			if($num_rows==0)$num_rows="";
		}else{
			$num_rows="";
		}

	
	//echo $num_rows;
	
	
	?>
	
	
	
<style>
	#tabla_notacredito_car td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_notacredito_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_notacredito_car td.editando{
		background-color: rgba(18, 180, 243, 0.05);
		outline: 2px solid #35A4F3;
	}
	#tabla_notacredito_car td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_notacredito_car td input:focus{
		background: transparent;
		height: 100%;
		width: 100%;
		outline: none;
		border: none;
	}
	#tabla_notacredito_car td input[readonly]{
		background: transparent;
	}
</style> 




	
	<div class="tab-content col-lg-12 col-md-12 col-sm-12 col-xs-12" id="">
		<div class="row">
			<div class="table-responsive">
	
				
	<table id="tabla_notacredito_car" class="table table-bordered table-striped">
		<thead>
			<tr>
				<th>COD</th>
				<th nowrap >ARTÍCULO</th>
				<th nowrap >MARCA</th>
				<th align="right">CAN</th>
				<th align="right" >MONTO</th>
				<th align="right" ></th>
			</tr>
		</thead>		 

		<tbody>
			<?php 
			
			$total = 0;
			if($num_rows>0){
				$c=0;
			foreach($_SESSION['notacredito_catid'] as $indice=>$cantidad) {
            
            $c++;
			
			$imei = $_SESSION['notacredito_imei'][$indice];		
			$imei_id = $_SESSION['notacredito_imei_id'][$indice];				
			$can=$_SESSION['notacredito_can'][$indice];		
			$pre_uni=$_SESSION['notacredito_preven'][$indice];
			$nom=$_SESSION['notacredito_pronom'][$indice];
			$esp_id=$_SESSION['notacredito_espid'][$indice];
				
			$pretot = $precio_uni * $can;
				
				$dts1=$oCatalogo->mostrarUno($_SESSION['notacredito_catid'][$indice],$_SESSION['notacredito_marid'][$indice]);
				$dt1 = mysqli_fetch_array($dts1);
			
		
			?>
			
			<tr>
						<td >
							<span style="font-size:12px;"  ><?php echo $c; ?></span>
						</td>
						
				
						<td  style="white-space: nowrap; font-size: 9pt; font-weight: bold;"><strong> <?php echo $nom?> </strong>
									
			</td>
			
			<td nowrap ><?php echo $dt1['tb_marca_nom']?></td>
			<td nowrap ><?php echo $can ?></td>

			
						
			<td align="right" width="80"  class="editable" style="font-size:9pt; font-weight:bold">
				
				<input type="text" class="" value="<?php echo formato_money($pre_uni) ?>" 
				data-preven="<?php echo formato_money($pre_uni) ?>"  data-tip = "pre"
				data-original="<?php echo formato_money($pre_uni) ?>" 
				data-nom="<?php echo $nom ?>" 
				data-can="<?php echo $can ?>" 
				data-imei_id ="<?php echo $imei_id ?>"
				data-imei ="<?php echo $imei ?>"
				data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" readonly 
				data-esp_id ="<?php echo $esp_id ?>"
				data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" style="text-align: right;">
						
			</td>
			<td align="center" nowrap="nowrap"> 						
							<span data-indice ="<?php echo $indice; ?>"  style="cursor:pointer" class="btn btn-danger btn-sm btn_eliminar" ><i class="fa fa-times"></i></span> 					
			</td>				
		</tr>
			
			
			<?php
					
					$total_pagar += $pre_uni;
					
				}
				
				
			}
			?>
		</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>
	
	
	
	
	<div class="tab-content col-lg-6 col-md-4 col-sm-4 col-xs-12" id="">
				<div class="row">
					<div class="table-responsive">
						<table class="table">
							<tbody>
							<tr>
								<th>Monto:</th>
								<td>
									<input type="hidden" name="txt_docrel_mon" id="txt_docrel_mon" value="<?php echo moneda_mysql(formato_money($total_pagar));?>" />
									<input name="txt_notcre_mon" class="form-control" type="text" id="txt_notcre_mon" style="text-align:right;font-weight: bold;" autocomplete="off" value="0" > 
								</td>
							</tr>							
						</tbody>
						</table>
					</div>
				</div>
			</div>
			
	
	
	<?php
	}?>		
			
			<div class="tab-content col-lg-12 col-md-12 col-sm-12 col-xs-12 d-none" id="">
				
						<div class="row">
												
																			<div class="col-lg-3 col-sm-4 col-xs-12 form-group ">
																				
																				<label for="cmb_forpag_id">Forma:</label>
																				<select name="cmb_forpag_id" id="cmb_forpag_id"  class="form-control input-sm">
																						<option value="1" selected="selected">CONTADO</option>
																				</select> 
																		
																			</div>
																			
																			<div id="div_modo" class="col-lg-3 col-sm-4 col-xs-12">
																					<label for="cmb_modpag_id">Modo:</label> 
																					<select name="cmb_modpag_id" id="cmb_modpag_id" class="form-control input-sm">
																					<option value="1">EFECTIVO</option>
																					<option value="7">YAPE</option>
																					<option value="8">PLIN</option>
																					<option value="2">DEPOSITO</option>
																					<option value="4">TRANSFERENCIA</option>
																					<option value="5">CHEQUE</option>
																					<option value="6">TARJETA</option>
																					</select> 
																				</div>

																			<div id="div_operacion" style="display:none" class="col-lg-3 col-sm-4 col-xs-12"> 
																				<label class="lbl_venpag_numope">N° Operación:</label> 
																				<input type="text" name="txt_venpag_numope" autocomplete="off"  id="txt_venpag_numope" class="form-control input-sm" >
																			</div>
										
																			<div class="col-lg-3 col-sm-3 col-xs-6" id="div_caj" >
																				<label for="cmb_caj_id">Caja:</label>
																				<select name="cmb_caj_id" id="cmb_caj_id" class="form-control"></select>
																			</div>
																			
																			<div class="col-lg-3 col-sm-2 col-xs-12">
																				<label for="txt_venpag_mon">Monto:</label> 
																				
																				<div class="input-group"> 
																					<input name="txt_venpag_mon" type="text" class="form-control input-sm" id="txt_venpag_mon" style="text-align:right;" maxlength="10" value="<?php echo moneda_mysql(formato_money($total_pagar));?>" >
																				
																					<span id="div_pago_agregar" style="display:none"  class="input-group-btn">
																						<a id="btn_venpag_agregar" class="btn btn-primary btn-flat"  href="javascript:void(0)" onClick="pago_venta_agregar('agregar')">Agregar</a> 
																					</span>
																			   </div>
																			
																			</div>

						</div>
					
			</div>
			
		
<script type="text/javascript">


function validar_campos(modid){
	
	var mon_id = '<?php echo $mon_id; ?>';
	var punven_id = '<?php echo $punven_id; ?>';
	
	
	
	
	
	
	if(modid==""){

		
		$("#div_operacion").hide(100);
	
	}else if(modid==1){
	

		$(".lbl_venpag_numope").text('Nº Operacion:');
		$("#div_operacion").hide(100);
		cmb_caja_id('1',punven_id,mon_id);
		
	}else if(modid==2){
		
		$(".lbl_venpag_numope").text('Nº Operacion:');
		$("#div_operacion").show(100);
		cmb_caja_id2(punven_id,mon_id,'2');
		
	}else if(modid==3){
		
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);

	}else if(modid==4){
		
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id2(punven_id,mon_id,'2');

	}else if(modid==5){
		
		$(".lbl_venpag_numope").text('Nº Cheque:');
			$("#div_operacion").show(100);
			cmb_caja_id('',punven_id,mon_id);

	}
	else if(modid==6){
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id2(punven_id,mon_id,'2');
	}else if(modid==7){
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id3(punven_id,mon_id,'3','YAPE');
	}
	else if(modid==8){
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id3(punven_id,mon_id,'3','PLIN');
	}
}



function cmb_caja_id3(punven_id,moneid,tip,ban_nom)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_cajaxpuntoventa_id3.php",
			async:true,
			dataType: "html",                      
			data: ({
				punven_id: punven_id,
				mon_id: moneid,
				tip: tip,
				bannom : ban_nom,
				vista : 'venta'
			}),
			beforeSend: function(){
				$('#cmb_caj_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id').html(html);
			}
		});
	}

 
function cmb_caja_id(tip,punven_id,moneid)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_cajaxpuntoventa_id.php",
			async:true,
			dataType: "html",
			data: ({
				tip : tip,
				punven_id: punven_id,
				mon_id: moneid
			}),
			beforeSend: function(){
				$('#cmb_caj_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id').html(html);
			}
		});
	}
	
function cmb_caja_id2(punven_id,moneid,tip)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_cajaxpuntoventa_id2.php",
			async:true,
			dataType: "html",                      
			data: ({
				punven_id: punven_id,
				mon_id: moneid,
				tip: tip // 1 : bancos // 2 : detracciones
			}),
			beforeSend: function(){
				$('#cmb_caj_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id').html(html);
			}
		});
	}


$(function(){



	validar_campos($("#cmb_modpag_id").val());

	$("#cmb_modpag_id").change(function(){		
			validar_campos($("#cmb_modpag_id").val());
	});
	
	cmb_doc_id(<?php echo $docid_not; ?>);
			
	if($("#txt_notcre_tip").val()==1 || $("#txt_notcre_tip").val()==6){
			$("#txt_notcre_mon").val($("#txt_docrel_mon").val());
			$("#txt_notcre_mon").attr('readonly','readonly');
	}else if($("#txt_notcre_tip").val()==5 || $("#txt_notcre_tip").val()==7){
			$("#txt_notcre_mon").val($("#txt_docrel_mon").val());
			$("#txt_notcre_mon").attr('readonly','readonly');
	}

	$("#txt_notcre_mon").keydown(function(e){
		if(e.which==13)
		{
				
			return false;
		}
	});




	
	$('#tabla_notacredito_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).attr('data-can')
			var preven = $(this).val();			
			var esp_id = $(this).attr('data-esp_id');
			var pronom = $(this).attr('data-nom');
			var imei_id = $(this).attr('data-imei_id');
			var imei = $(this).attr('data-imei');

			
			agregar_producto('agregar', cat_id, cantidad,preven,mar_id,esp_id,imei_id,imei,pronom);
			
				
				
				
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
				return false;
			}
		});

		$('#tabla_notacredito_car td input').parent().click(function(ev) {
			var $input = $(this).find('input');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		});

		$('#tabla_notacredito_car td input').blur(function(event) {
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		});

		$('#tabla_notacredito_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_eliminar" ).click(function() { 
			
			var indice = $(this).attr('data-indice');
				
			if(confirm('¿Seguro de quitar el item?')){
				agregar_producto('quitar', indice);
			}
		});



});
</script>		
	