<?php
session_start();
if($_SESSION["sistema"]!= "1"){ header("location: ../login/index.php"); exit();}
require_once ("../../config/Cado.php");
require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();

require_once("../formatos/formato.php");
	$dts = $oVenta->mostrarUno($_POST['ven_id']);
	$dt = mysqli_fetch_array($dts);
	$reg = mostrarFechaHora($dt['tb_venta_reg']);
	$fec = mostrarFecha($dt['tb_venta_fec']);
	$doc_id	= $dt['tb_documento_id'];
	$numdoc	= $dt['tb_venta_numdoc'];
	$cli_id	= $dt['tb_cliente_id'];
	$cli_nom = $dt['tb_cliente_nom'];
	$cli_doc = $dt['tb_cliente_doc'];
	
	$can = strlen($cli_doc);
	
	$cli_dir = $dt['tb_cliente_dir'];
	$cli_tip = $dt['tb_cliente_tip'];
	$cli_pla = $dt['tb_cliente_pla'];
	$mon_id = $dt['tb_moneda_id'];
	$tipcam = $dt['tb_venta_tipcam'];
	$valven	= $dt['tb_venta_valven'];
	$igv	= $dt['tb_venta_igv'];
	$tot	= $dt['tb_venta_tot'];
	$est	= $dt['tb_venta_est'];
	$emp_nom	= $dt['tb_empresa_nomcom'];
	$piso_nom	= $dt['tb_piso_nom'];
	$almacen_nom	= $dt['tb_almacen_nom'];
	$alm_id	= $dt['tb_almacen_id'];
	$ven_id	= $dt['tb_vendedor_id'];
	$lab1	= $dt['tb_venta_lab1'];
	$empvg	= $dt['empven'];
	mysqli_free_result($dts);
	
	
	
	$dts1=$oVenta->mostrar_venta_detalle($_POST['ven_id']);
	$num_rows= mysqli_num_rows($dts1);
	
	$ban = 0;
	
	while($dt1 = mysqli_fetch_array($dts1))
    {	
		$catid = $dt1['tb_catalogo_id'];
		$marid = $dt1['tb_marca_id'];
	
		$dts1=$oCatalogo->mostrarUno($catid,$marid);
		$dt1 = mysqli_fetch_array($dts1);
		$arttip_id = $dt1['tb_articulotipo_id'];
		
		if($arttip_id==3){
			$ban +=1;
		}
	
	}
	
if($ban>0){
	
	$dts12=$oVenta->mostrar_venta_detalle($_POST['ven_id']);
	$num_rows2= mysqli_num_rows($dts12);
	
	//  echo $num_rows2;
	
	?>
	<div class="modal-header">		
		<h4 class="modal-title " id="titlemodal">Facturar <strong class="text-red"><?php echo $numdoc?></strong></h4>
	</div>
	<div class="modal-header">				
		<strong>Hay productos internos en este ticket que no se pueden FACTURAR NI BOLETEAR.<br/>
		Por favor elimina este ticket, vueve a emitir la factura con los productos correctos <br/>
		y otro ticket con los productos internos correspondientes.</strong>
	</div>
	
<div class="table-responsive">
	<table id="" class="table table-bordered table-striped">
		<thead>
			
		</thead>
		<tbody>		
	<?php 
	while($dt12 = mysqli_fetch_array($dts12))
    {	
		$catid = $dt12['tb_catalogo_id'];
		$marid = $dt12['tb_marca_id'];
	
		$dts122=$oCatalogo->mostrarUno($catid,$marid);
		$dt122 = mysqli_fetch_array($dts122);
		$pro_nom = $dt122['tb_producto_nom'];
		$mar_nom = $dt122['tb_marca_nom'];
		$arttip_id = $dt122['tb_articulotipo_id'];
		$arttip_nom = $dt122['tb_articulotipo_nom'];
	?>	
	
	
		 <tr>
		  <td nowrap ><strong class="<?php if($arttip_id==3) echo 'text-danger'; else echo 'text-primary'; ?>" ><?php echo $dt122['tb_articulotipo_nom']?></strong></td>
          <td><?php echo $dt12['tb_catalogo_cod'];?></td>
          <td nowrap ><?php echo '<strong>'.$dt12['tb_ventadetalle_nom'].'</strong>';?></td>
          <td nowrap ><?php echo $dt122['tb_categoria_nom'].' | '.$dt122['tb_marca_nom'];?></td>
          <td nowrap ><?php echo $dt122['tb_unidad_nom']?></td>
         
        </tr>
		
		
		
	
	<?php 
	}
	
	?>
		</tbody>
	</table>
	</div> 
	
	<div class="modal-footer float-right">	 
			<button type="button" class="btn btn-default " id="" data-dismiss="modal">Cancelar</button>
	</div> 
	
	<?php 
	
	
}else{	
	
?>



<script type="text/javascript">
	
	function cmb_docgen_id(idf,doc){
 				
		
				$.ajax({
 					type: "POST",
 					url: "../documento/cmb_doc_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({
 						mod_id: '3',
 						def : '1',
 						doc_id: idf,
						doc : doc,
						tot:'<?php echo $tot; ?>',
 						vista:	'ven'
 					}),
 					beforeSend: function() {
 						$('#cmb_docgen_id').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_docgen_id').html(html);
 					},
 					complete: function(){
 						
 						}
 					});
 			
				
			}
			
	
	
function cliente_cargar_datos(idf){	
	$.ajax({
		type: "POST",
		url: "../cliente/cliente_reg.php",
		async:true,
		dataType: "json",                      
		data: ({
			action: "obtener_datos",
			cli_id:	idf
		}),
		beforeSend: function() {
		
		},
		success: function(data){
			$('#hdd_ven_cli_id').val(idf);	
			$('#txt_ven_cli_nom').val(data.nombre);	
			$('#txt_ven_cli_doc').val(data.documento);						
			$('#txt_ven_cli_dir').val(data.direccion);
			$("#hdd_ven_cli_tip").val(data.tipo);
			cargar_direcciones(idf);
			
		can = 	$('#txt_ven_cli_doc').val().length;
		
		if(can == '8'){
				cmb_docgen_id('4',data.documento);
		}else{
				cmb_docgen_id('5',data.documento);
		}		
			//cargar_vehiculos(idf);
			
		}
	});		
}
	
	
function cliente_form(act,idf){
	$('#div_cliente_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/cliente_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			action: act,
			cli_id:	idf,
			vista:	'venta'
		}),
		beforeSend: function() {
			
			var div = "<center><div class='overlay col-lg-12 col-sm-12 col-xs-12' style='background-color:#fff;padding:0%;font-size:3rem;' >  <i class='fa fa-refresh fa-spin ' ></i> </div>  </center>";	
			$('.div_cliente_form').html(div); 
		},
		success: function(html){ 	
			$('.div_cliente_form').html(html); 				
		},
		complete: function(){
				
				
		}
	});
}
	function cargar_direcciones(idcli){
	
			$.ajax({
 					type: "POST",
 					url: "../cliente/cmb_clidir_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						cli_id: idcli,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_ven_cli_dir').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_ven_cli_dir').html(html);
 					},
 					complete: function(){
 						
 						}
 					});
}
	
	
	
$(function() {
cliente_cargar_datos(<?php echo $cli_id?>);
$( "#txt_ven_cli_doc" ).autocomplete({
		minLength: 1,
		source: "../cliente/cliente_complete_doc.php",
		select: function(event, ui){			
			$("#hdd_ven_cli_id").val(ui.item.id);
			$("#txt_ven_cli_nom").val(ui.item.nombre);
			$("#txt_ven_cli_dir").val(ui.item.direccion);
			$("#hdd_ven_cli_tip").val(ui.item.tipo);
				$("#txt_bus_pro_codbar").focus();
				cargar_direcciones(ui.item.id);
				
		can = $('#txt_ven_cli_doc').val().length;
		
		if(can == '8'){
				cmb_docgen_id('4',can);
		}else{
				cmb_docgen_id('5',can);
		}	
				
			}
		});
	$( "#txt_ven_cli_nom" ).autocomplete({
		minLength: 1,
		source: "../cliente/cliente_complete_nom.php",
		select: function(event, ui){			
			$("#hdd_ven_cli_id").val(ui.item.id);							
			$("#txt_ven_cli_doc").val(ui.item.documento);
			$("#txt_ven_cli_dir").val(ui.item.direccion);
			$("#hdd_ven_cli_tip").val(ui.item.tipo);
				$("#txt_bus_pro_codbar").focus();
				cargar_direcciones(ui.item.id);
				
		can = $('#txt_ven_cli_doc').val().length;
		
		if(can == '8'){
				cmb_docgen_id('4',can);
		}else{
				cmb_docgen_id('5',can);
		}	
		
			}
		});
		
	$('#txt_ven_cli_nom').keydown(function(e){
		if (e.which == 13){$("#txt_bus_pro_codbar").focus();return false;}
	});
			
<?php if($can==8){
?>
//cmb_docgen_id('4');
<?php 	
if($cliid==1){
?>
$("#div_ate").hide(100);
$("#div_lle").hide(100);
<?php 	
}
}else{
?>
//cmb_docgen_id('5');
<?php	
}?>
					
$("#btn_ven_guardar").click(function() {
	$("#for_venfac").submit();
});
	
$('#for_venfac').bootstrapValidator({ 
	      message: 'Valor no valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: {	      	
	      	
	      	cmb_docgen_id: {
	      		group: '.col-lg-6',
	      		validators: {
	      			notEmpty: {
	      				message: 'Requerido *'
	      			}
	      		}
	      	}
	      }
	  }).on('success.form.bv', function(e) { 
				$('#div_mensaje_dialog').modal();
	  	$.ajax({
	  		type: "POST",
	  		url: "../venta/venta_facturar.php",
	  		async:true,
	  		dataType: "json",
	  		data: $("#for_venfac").serialize(),
	  		beforeSend: function(){
				var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
				$('.div_mensaje').html(div);
	  			toastr["info"]('Generando el documento...');  
	  		},
	  		success: function(data){
	  			
				toastr["info"](data.ven_msj);
				
			if(data.ven_est==1){
			
			venta_ticket_datos(data.ven_id);
					
				$('#div_ventafac_form_dialog').modal('hide');
			
			}	
				
				
	  		},
	  		complete: function(){
	  			
	  		}
	  	});	
		
 
	});
			
			
			
			
		});
	</script>
	
	<style type="text/css">
	.ui-autocomplete
{
    z-index: 99999; 
}
	</style>
	
	<div class="modal-header">		
		<h4 class="modal-title " id="titlemodal">Facturar <strong class="text-red"><?php echo $numdoc?></strong></h4> 
	</div>
	<form  role="form" id="for_venfac" method="POST">
	
	<input type="hidden" name="hdd_venfac" id="hdd_venfac" value="<?php echo $_POST['ven_id'];?>" />
	<input type="hidden" name="hdd_empid" id="hdd_empid" value="<?php echo $empvg;?>" />
		
 										<div class="row"> 
 											<input type="hidden" id="hdd_ven_cli_id" name="hdd_ven_cli_id" value="<?php echo $cli_id ?>" />
 											
											<div class="col-lg-5 col-sm-4 col-xs-12">															
												<label for="txt_com_pro_doc">RUC/DNI:</label>
 												<div class="input-group">
														
													
														
														<span id="" style=""  class="input-group-btn">
																<span id="btn_compag_agregar" style="cursor:pointer" class="btn btn-info btn-flat" onClick="cliente_form('insertar')"><i class="fa fa-plus"></i></span> 
														</span>	
 														
 														<input name="txt_ven_cli_doc" type="text" id="txt_ven_cli_doc" value="<?php echo $cli_doc?>"   maxlength="11" class="form-control input-sm"> 
 													</div>
 												</div>
 												<div class="col-lg-7 col-sm-4 col-xs-12">
 													<div id="div_clientecuenta_detalle"></div>
 													<label for="txt_ven_cli_nom">Cliente:</label>
 													<input type="text" id="txt_ven_cli_nom" name="txt_ven_cli_nom"  value="<?php echo $cli_nom?>" class="form-control input-sm" />
 												</div> 
 											
 											
 												<div class="col-lg-12 col-sm-12 col-xs-12">
 													<div id="div_clientecuenta_detalle"></div>
 													<label for="cmb_ven_cli_dir">Dirección:</label>
 													
													<select name="cmb_ven_cli_dir" id="cmb_ven_cli_dir" class="form-control input-sm" >
		<option value="">-</option>
		</select>										   
 		</div>
											
		</div>
		
		<div class="row">
			<div class="col-lg-6 col-sm-6 col-xs-6">
					<label for="cmb_docgen_id">Documento:</label>
					<select name="cmb_docgen_id" id="cmb_docgen_id" class="form-control"></select>
			</div>
		</div>
		
	</form>
	
<div class="modal-footer float-right">	 
			<button type="button" class="btn btn-default " id="" data-dismiss="modal">Cancelar</button>
			<button type="button" class="btn btn-info " id="btn_ven_guardar">Guardar</button>
		</div> 
		
<?php } ?>