<?php
//ini_set('display_error',1);
date_default_timezone_set('America/Lima');
session_start();
if($_SESSION["sistema"]!= "1"){ header("location: ../login/index.php"); exit();}


require_once ("../../config/Cado.php");
require_once ("../formatos/formato.php");
require_once ("../puntoventa/cPuntoventa.php");
$oPuntoventa = new cPuntoventa();
require_once ("../impresora/cImpresora.php");
$oImpresora = new cImpresora();

require_once ("../serviciopago/cServiciopago.php");
$oServiciopago = new cServiciopago();

require_once ("../configuracion/cConfiguracion.php");
$oConfiguracion = new cConfiguracion();

$y=date('Y');
$m=date('m');
$d=date('d');

$hora = 17; // date('G');

//$dia = 15;
// $d = 1;

$dtsc=$oConfiguracion->mostrarUno('1');
$dtcon = mysqli_fetch_array($dtsc);
$diaven=$dtcon['tb_configuracion_diaven'];
mysqli_free_result($dtsc);

$fecha_hoy= $diaven.'-'.$m.'-'.$y;
list($day,$mon,$year) = explode('-',$fecha_hoy);
$fecha2dias=date('d-m-Y',mktime(0,0,0,$mon,$day+2,$year));
	
list($day,$mon,$year) = explode('-',$fecha_hoy);
$d2=date('d',mktime(0,0,0,$mon,$day+5,$year));

if($d>=$diaven && $d<=$d2){

$dtspagos = $oServiciopago->versi_pago(fecha_mysql($fecha_hoy),fecha_mysql($fecha2dias));
$rowspago = mysqli_num_rows($dtspagos);

if($rowspago>0){	
$dias = 'info';	
}else
{
$fecha1=mktime(0,0,0,$m,$d,$y);
$fecha2=mktime(0,0,0,$m,$diaven,$y);

$diferencia=$fecha2-$fecha1;
$dias=$diferencia/(60*60*24);

if($d == $diaven){
	$dias='0';
}

}
}else{
	$dias = 'info';	
}



if(isset($_SESSION['puntoventa_id'])){
	$dts=$oPuntoventa->mostrarUno($_SESSION['puntoventa_id']);
	$dt = mysqli_fetch_array($dts);
	$punven_nom=$dt['tb_puntoventa_nom'];
	$alm_id=$dt['tb_almacen_id'];
	$caj_id=$dt['tb_caja_id'];
	$imp_id=$dt['tb_impresora_id'];
	mysqli_free_result($dts);
}

if($imp_id>0)
{
	$dts=$oImpresora->mostrarUno($imp_id);
	$dt = mysqli_fetch_array($dts);
	$imp_nom =$dt['tb_impresora_nom'];
	$imp_nomloc =$dt['tb_impresora_nomloc'];
	$imp_ser =$dt['tb_impresora_ser'];
	$imp_url =$dt['tb_impresora_url'];
	$imp_ip =$dt['tb_impresora_ip'];
	mysqli_free_result($dts);
}

?>
<style type="text/css">
select{
	cursor:pointer;
}

</style>

<div class="content-header" >
      <div class="container-fluid">
        <div class="row mb-2">
		 <div class="col-sm-6">
			<h1>
			Ventas
			<small>modulo</small>
			</h1>
		</div>	
		
		<div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
				<li class="breadcrumb-item" >Inicio</li>
				<li class="breadcrumb-item" >Operaciónes</li>
				<li class="breadcrumb-item active">Ventas</li>
            </ol>
          </div>
		</div>
	</div>
</div>

<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-12" style="padding:0px;">
		    <div class="card">
		        <div class="card-header">
		          <button id="btn_agregar" onClick="venta_form('insertar')" class="btn btn-primary">Agregar</button>
		          <button id="btn_actualizar" class="btn btn-default">Actualizar</button>	


					<div class="col-lg-2 col-md-2 col-sm-2 col-5 float-right" style="display: inline-block; white-space: nowrap;padding:0px;"  >
						<input id="chkopcionmodulo1" class="float-right" type="checkbox" checked data-toggle="toggle" data-on="<i class='fa fa-align-left'></i> REGISTROS" 
						data-off="<i class='fa fa-chart-area'></i> GRAFICAS" data-onstyle="danger" data-offstyle="warning" >	  
					</div>

				  
		        </div>
		    </div>
		</div>
	</div>
	</div>
</section>


	<div id="div_venta_filtro" class=""></div>

	
	
	
	
	
	<section class="content">
	<div class="container-fluid">
		<div class="row">		
			<div class="card">
													 
					<div id="div_venta_tabla" class=""></div>
					<div id="div_venta_grafica" style="display:none"></div>		
			</div>		
		</div>
	</div>
</section>
	
	
	
	
	
	
	
	
	
	
	
		
	<!-- formulario de vista -->
			<div class="modal fade" id="div_venta_form_dialog_vista" role="dialog" data-keyboard="true" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-xl " role="document" >

			<div class="modal-content">

			<div class="modal-body div_venta_form_vista" >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>

			<!-- formulario de registro -->
			<div class="modal fade" id="div_venta_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px; overflow-y:scroll;">
			<div class="modal-dialog modal-xl" role="document" >

			<div class="modal-content">

			<div class="modal-body div_venta_form" style="padding:0%;" >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<!-- formulario de registro -->
			<div class="modal fade" id="div_guia_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px; overflow-y:scroll;">
			<div class="modal-dialog modal-xl "  role="document" >

			<div class="modal-content">

			<div class="modal-body div_guia_form" style="padding:0%;" >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<!-- formulario de nuevo cliente -->
			<div class="modal fade" id="div_ventafac_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px; overflow-y:scroll;">
			<div class="modal-dialog modal-xs"  role="document" >

			<div class="modal-content">

			<div class="modal-body div_ventafac_form"  >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<!--MODAL PARA CORREO ELECTRONICOS-->
			
			
			<div class="modal fade" id="div_venta_correo_form" role="dialog" data-keyboard="false" data-backdrop="static" style="padding:0px;overflow-y:scroll; ">
			<div class="modal-dialog modal-lg"  >

			<div class="modal-content">

			<div class="modal-body div_venta_correo_conten" style="padding:0px;"   >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<!-- formulario de nuevo cliente -->
			<div class="modal fade" id="div_cliente_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px; overflow-y:scroll;">
			<div class="modal-dialog modal-lg"  role="document" >

			<div class="modal-content">

			<div class="modal-body div_cliente_form"  >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<div class="modal fade" id="div_clientedireccion_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >

			<div class="modal-content">

			<div class="modal-body div_clientedireccion_form" >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<div class="modal fade" id="div_clientevehiculos_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >

			<div class="modal-content">

			<div class="modal-body div_clientevehiculos_form" >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
				<!-- formulario de registro -->
			<div class="modal fade" id="div_pago_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;overflow-y:scroll;">
			<div class="modal-dialog modal-lg" style=""   role="document" >

			<div class="modal-content">

			<div class="modal-body div_pago_form"  >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
					<!-- formulario de registro -->
			<div class="modal fade" id="div_productomarca_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;overflow-y:scroll;">
			<div class="modal-dialog modal-xl" style=""   role="document" >

			<div class="modal-content">

			<div class="modal-body div_productomarca_form" >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
		
		
			<div class="modal " id="div_mensaje_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px; overflow-y:scroll;">
			<div class="modal-dialog modal-md"  role="document" >

			<div class="modal-content" style="padding:0px;">

			<div class="modal-body div_mensaje"  >

			
		
			
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
		<!-- formulario de registro -->
			<div class="modal fade" id="div_servicioval_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;overflow-y:scroll;">
			<div class="modal-dialog modal-lg" style=""   role="document" >

			<div class="modal-content">

			<div class="modal-body div_servicioval_form" >

			</div>
			<br />
			<br />
			</div>
			</div>
			</div>   
				
	<div id="div_venta_impresion"></div>

<script type="text/javascript">



 function venta_ticket_datos(id_ven,imprimir){	
		
		 //alert(imprimir); 
		
		$.ajax({
			type: "POST",
			url: "../venta/venta_ticket_datos.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			venid: id_ven
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				 //console.log(data); 
				
				if(data!=""){					
					if(data.ven_docabr=="F" /* && data.cli_nom !='CLIENTE' */ ){
					generarFactura_xml(data,data.url_genfac,data.emp_ruc,data.ven_numdoc,id_ven,data.cli_id,imprimir);
					}else if(data.ven_docabr=="B" /* && data.cli_nom !='CLIENTE' */ ){
					generarBoleta_xml(data,data.url_genbol,data.emp_ruc,data.ven_numdoc,id_ven,data.cli_id,imprimir);
					}else if(data.ven_docabr=="T"){
						if(imprimir=="1"){
							venta_ticket_impresion(data,data.imp_doc,imprimir);
						}		
					}
				}else{
					
				}
			}
		});
	
}


 function venta_ticket_datos_tabla(id_ven){	
		
		/* alert(id_ven); */
		
		$.ajax({
			type: "POST",
			url: "../venta/venta_ticket_datos.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			venid: id_ven
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				// console.log(data); 
				
				if(data!=""){					
					if(data.ven_docabr=="T"){
					venta_ticket_impresion(data,data.imp_doc);
					}		
				}else{
					
				}
			}
		});
	
}



function venta_ticket_impresion(data,url){	

	$.ajax({
		type: "POST",
		url: url,
		async:true,
		dataType: "html",                      
		data: data,
		beforeSend: function()
		{
			//console.log(data);	
		},
		success: function(html){
				
			
		}
	});
}

function generarFactura_xml(data,url,emp_ruc,ven_numdoc,id_ven,id_cli,imprimir){
		/* alert(url); */
		$.ajax({
		type: "POST",
		url: url,
		async:true,
		dataType: "json",                      
		data: data,
		beforeSend: function()
		{
			
		},
		success: function(data){
				/* alert(html); */
				//tip_com = '01';
		/* crear_xml(html,emp_ruc,ven_numdoc,tip_com,id_ven,id_cli); */
		firmar(data.url,data.arc,emp_ruc,ven_numdoc,data.tipcom,id_ven,id_cli,imprimir);
		}
	});

}

function generarBoleta_xml(data,url,emp_ruc,ven_numdoc,id_ven,id_cli,imprimir){
		/* alert(url); */
		$.ajax({
		type: "POST",
		url: url,
		async:true,
		dataType: "json",                      
		data: data,
		beforeSend: function()
		{
			
		},
		success: function(data){
			
 firmar(data.url,data.arc,emp_ruc,ven_numdoc,data.tipcom,id_ven,id_cli,imprimir);			
			/* alert(html); */
		//tip_com = '03';
		//crear_xml(html,emp_ruc,ven_numdoc,tip_com,id_ven,id_cli);
		}
	});

}

// function crear_xml(xml,emp_ruc,ven_numdoc,tip_com,id_ven,id_cli){
		// /* alert(url); */
		// $.ajax({
		// type: "POST",
		// url: '../../docgeneracion/crearxml.php',
		// async:true,
		// dataType: "json",                      
		// data: ({
			// xml : xml,
			// emp_ruc : emp_ruc,
			// ven_numdoc : ven_numdoc,
			// tip_com : tip_com,
		// }),
		// beforeSend: function()
		// {
			
		// },
		// success: function(data){
		
			// firmar(data.url,data.arc,emp_ruc,ven_numdoc,data.tipcom,id_ven,id_cli);
				
		// }
	// });

// }

function firmar(url,arc,emp_ruc,ven_numdoc,tipcom,id_ven,id_cli,imprimir){
		/* alert(url); */
		$.ajax({
		type: "POST",
		url: url,
		async:true,
		dataType: "json",                      
		data: ({
			arc : arc,
			emp_ruc : emp_ruc,
			ven_numdoc:ven_numdoc,
			tipcom:tipcom,
			venid:id_ven
		}),
		beforeSend: function()
		{
			
		},
		success: function(data){			
			generar_pdf(id_ven);
			//generarPDF417(data);
			traer_datos_doc(id_ven,data.fil_name_xml,imprimir);
			enviarSunat(data.xml_nom,data.fil_name_zip,data.fil_name_xml,data.filenom,id_ven,id_cli,tipcom);
			
				
			
		}
	});

}


function enviarSunat(nomxml,fil_name_zip,fil_name_xml,filenom,id_ven,id_cli,tipcom){
		$.ajax({
		type: "POST",
		url: '../../docgeneracion/enviosunat.php',
		async:true,
		dataType: "json",                      
		data: ({
			nomxml:nomxml,
			name_zip:fil_name_zip,
			nomfile:filenom,
			id_ven:id_ven,
			xmlarchivo:fil_name_xml,
			tipcom:tipcom
		}),
		beforeSend: function()
		{
			toastr["error"]('Esperando respuesta de sunat');
		},
		success: function(data){
			
			toastr["success"](data.msj);
			
			if(data.est==1){
				//enviarCorreo(id_ven,id_cli);
				
				//abrirEnPestana('="../../../../files/venta/venta_impcpe_doc.php?venid='+id_ven);
				venta_tabla();
			}else{
				toastr["info"]('El documento no se ha emitido.');
			}
			
		}
	});

}


 function generar_pdf(id_ven){	
		
//alert(id_ven); 
		
		$.ajax({
			type: "GET",
			url: "../venta/venta_generar_doc.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			venid: id_ven
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				toastr["info"]('Pdf generado');
				}
		});
	
}

function enviarCorreo(venid,cliid){
	
	$('#div_venta_correo_form').modal();
	//$('body').css('overflow-y','hidden');
				
	$.ajax({
			type: "POST",
			url: "../venta/venta_correo_form.php",
			async:true,
			dataType: "html",
			data: ({
				venta_id:venid,
				cli_id:cliid	
			}),
			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_venta_correo_conten').html(div);
	       


		   },
			success: function(html){
				$('.div_venta_correo_conten').html(html);				
			}
		});
	
}

function traer_datos_doc(venid,urlxml,imprimir){
	
	//alert(imprimir)
	
	$.ajax({
			type: "POST",
			url: "../venta/venta_doc_datos.php",
			async:true,
			dataType: "json",                      
			data:({ 
			venid: venid,
			urlxml :urlxml
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				/* console.log(data); */
			
			//generarPDF417(data);
			
			if(imprimir=="1"){
				venta_doc_impresion(data,data.imp_doc);
			}
			/* if(data.cli_ema==""){
				
			}else{
				act ="enviar";
				enviarCorreo(data);
			}	 */	
			
			}
		});
	
}

function generarPDF417(data){
	
	console.log(data);
	
	$.ajax({
			type: "POST",
			url: "../venta/generarPdf417.php",
			async:true,
			dataType: "json",                      
			data:data,
			beforeSend: function() {
				
			},
			success: function(data){			

			}
		});	 
		
}

function venta_doc_impresion(data,url){
	$.ajax({
			type: "POST",
			url: url,
			async:true,
			dataType: "json",                      
			data: data ,
			beforeSend: function() {
				//console.log(data);
			},
			success: function(data){
				
				
			}
		});
}



	

	function venta_filtro()
	{
		$.ajax({
			type: "POST",
			url: "../venta/venta_filtro.php",
			async:true,
			dataType: "html",                      
			data: ({
				
			}),
			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_venta_filtro').html(div);
			},
			success: function(html){
				$('#div_venta_filtro').html(html);
			},
			complete: function(){
					$('#chkopcionmodulo1').bootstrapToggle('on');
					$('#div_venta_grafica').hide('fade');
					$('#div_venta_tabla').show('fade');
			}
		});
	}

	function venta_tabla()
	{
		 $('#div_mensaje_dialog').modal('show');
		$.ajax({
			type: "POST",
			url: "../venta/venta_tabla.php",
			async:true,
			dataType: "html",                      
			data: $("#for_fil_ven").serialize(),
			beforeSend: function(){
				var divmen = "<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(divmen);
				$('#div_venta_tabla').addClass("ui-state-disabled");
			},
			success: function(html){
				$('#div_venta_tabla').html(html);
			},
			complete: function(){			
				$('#div_venta_tabla').removeClass("ui-state-disabled");
				$('#div_mensaje_dialog').modal('hide');
			}
		});     
	}
	
	
	function venta_grafica()
		{
			$('#div_mensaje_dialog').modal('show');

			$.ajax({
				type: "POST",
				url: "../venta/venta_grafica.php",
				async:true,
				dataType: "html",                      
				data: $("#for_fil_vengra").serialize(), 
				beforeSend: function() {
						var divmen = "<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(divmen);
					$('#div_venta_grafica').addClass("ui-state-disabled");
				},
				success: function(html){
					$('#div_venta_grafica').html(html);
				},
				complete: function(){			
					$('#div_venta_grafica').removeClass("ui-state-disabled");
					
					$('#div_mensaje_dialog').modal('hide');

			}
			});     
		}
		
		
	
function actualizarestado(venid,vis)
	{
		$('#div_servicioval_form_dialog').modal();
		$.ajax({
			type: "POST",
			url: "../servicio/servicio_formvaldoc.php",
			async:true,
			dataType: "html",                      
			data: ({
				modide:	venid,
				vista:	vis
			}),
			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_servicioval_form').html(div);
			},
			success: function(html){
				$('.div_servicioval_form').html(html);				
			}
		});
	}

	
function ventaguia_form(act,idf){
		$('#div_guia_form_dialog').modal();
		$('body').css('overflow-y','hidden');
		$.ajax({
			type: "POST",
			url: "../guiaremision/guiaremision_form.php",
			async:true,
			dataType: "html",   
			position:'top',                   
			data: ({
				action: act,
				ven_id:	idf,
				vista : 'venta'
			}),
			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_guia_form').html(div);
			},
			success: function(html){
				$('.div_guia_form').html(html);				
			},
			complete: function(){

			}
		});
	}
	
	function venta_tabla2()
	{
		
		$.ajax({
			type: "POST",
			url: "../venta/venta_tabla.php",
			async:true,
			dataType: "html",                      
			data: $("#for_fil_ven").serialize(),
			beforeSend: function(){
				
				$('#div_venta_tabla').addClass("ui-state-disabled");
			},
			success: function(html){
				$('#div_venta_tabla').html(html);
			},
			complete: function(){			
				$('#div_venta_tabla').removeClass("ui-state-disabled");
				$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');	
				
				
				$('#chkopcionmodulo1').bootstrapToggle('on');
					$('#div_venta_grafica').hide('fade');
					$('#div_venta_tabla').show('fade');
					
			}
		});     
	}

	function venta_form_vista(act,idf){
		$('#div_venta_form_dialog_vista').modal();
		$('body').css('overflow-y','hidden');
		$.ajax({
			type: "POST",
			url: "../venta/venta_form_vista.php",
			async:true,
			dataType: "html",                      
			data: ({
				action: act,
				ven_id:	idf
			}),
			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_venta_form_vista').html(div); 
			},
			success: function(html){
				$('.div_venta_form_vista').html(html);	
			},
			complete: function(){
			}
		});
	}

function venta_form(act,idf){
		$('#div_venta_form_dialog').modal();
		$('body').css('overflow-y','hidden');
		$.ajax({
			type: "POST",
			url: "../venta/venta_form.php",
			async:true,
			dataType: "html",   
			position:'top',                   
			data: ({
				action: act,
				ven_id:	idf
			}),
			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_venta_form').html(div);
			},
			success: function(html){
				$('.div_venta_form').html(html);				
			},
			complete: function(){

			}
		});
	}

	
	function ventafact_form(idf){
		$('#div_ventafac_form_dialog').modal();
		$('body').css('overflow-y','hidden');
		$.ajax({
			type: "POST",
			url: "../venta/venta_form_genfac.php",
			async:true,
			dataType: "html",                      
			data: ({
				ven_id:	idf
			}),
			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_ventafac_form').html(div); 
			},
			success: function(html){
				$('.div_ventafac_form').html(html);	
			},
			complete: function(){
			}
		});
	}
	
	function venta_anular(id,texto){      
		if(confirm("Realmente desea anular venta "+texto+", se actualizará el stock. ASEGURESE QUE LAS CANTIDADES DE PRODUCTO SE PUEDAN REPONER CORRECTAMENTE.  Continuar?")){
			
			 $('#div_mensaje_dialog').modal('show');
			$.ajax({
				type: "POST",
				url: "../venta/venta_anular.php",
				async:true,
				dataType: "json",
				data: ({
					action: "anular",
					ven_id:		id
				}),
				beforeSend: function() {

				var divmen = "<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(divmen);
					
				},
				success: function(data){
					
					toastr["success"](data.msj);
					 $('#div_mensaje_dialog').modal('hide');
					 
				},
				complete: function(){
					
					venta_tabla();
				}
			});
		}
	}

	function venta_reporte(url)
	{	
		$("#for_fil_ven").attr("action", url);
		$("#for_fil_ven").submit();
	}

	function venta_impresion(idf)
	{
	/*$.ajax({
		type: "POST",
		url: "../venta/venta_preimpresion.php",
		async:true,
		dataType: "html",                      
		data: ({
			ven_id:	idf
		}),
		beforeSend: function() {
			$('#div_venta_impresion').dialog("open");
			$('#div_venta_impresion').html('Cargando....');
        },
		success: function(html){
			$('#div_venta_impresion').html(html);				
		}
	});*/
	//if(confirm("Desea imprimir éste documento?")){
		venta_imppos_datos(idf);
	//}
}

function venta_imppos_datos(id_ven){	
	if(confirm("Desea imprimir éste documento?")){
		$.ajax({
			type: "POST",
			url: "../venta/venta_imppos_datos.php",
			async:true,
			dataType: "json",                      
			data: ({ ven_id: id_ven}),
			beforeSend: function() {
				//$('#msj_venta').html("Imprimiendo...");
				//$('#msj_venta').show(100);
			},
			success: function(data){
				//venta_imppos_comprobante(data,data.imp_doc);
			}
		});
	}
}

function venta_imppos_comprobante(datos,url_doc){	
	$.ajax({
		type: "POST",
		url: url_doc,
		async:true,
		dataType: "html",                      
		data: datos,
		beforeSend: function()
		{
			console.log(datos);
		
		},
		success: function(html){						
			$('#msj_venta_imp').html(html);		
		}
	});
}

function venta_imppos_venta_datos(){	
	if(confirm("Confirma la impresión del documento?"))
	{	
		$.ajax({
			type: "POST",
			url: "../venta/venta_imppos_venta_datos.php",
			async:true,
			dataType: "json",                      
			data: $("#for_fil_ven").serialize(),
			beforeSend: function() {
					//alert($("#for_fil_ven").serialize());
					//$('#msj_venta').html("Imprimiendo...");
					//$('#msj_venta').show(100);
				},
				success: function(data){
					venta_imppos_venta(data);	
				}
			});
	}
}

function venta_imppos_venta(datos){	
	$.ajax({
		type: "POST",
		url: '<?php echo $imp_url."venta/venta_imppos_venta.php";?>',
		async:true,
		dataType: "html",                      
		data: datos,
		beforeSend: function() {
			$('#msj_venta_imp').html("Imprimiendo...");
			$('#msj_venta_imp').show(100);
		},
		success: function(html){						
			$('#msj_venta_imp').html(html);		
		}
	});
}


		function pago_form(act,dias){
				
			$('#div_pago_form_dialog').modal();
			$('body').css('overflow-y','hidden');
			$.ajax({
				type: "POST",
				url: "../serviciopago/serviciopago_form.php",
				async:true,
				dataType: "html",                      
				data: ({
					action: act,
					dias:dias
				}),
				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_pago_form').html(div);
				},
				success: function(html){
					$('.div_pago_form').html(html);				
				}
			});
		}

		
		
		
		///// GUIA ELECTRONICAA 
	
	
	
	
function guia_ticket_datos(id_guia){	
		
		
		$.ajax({
			type: "POST",
			url: "../guiaremision/guia_ticket_datos.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			guiaid: id_guia
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				 console.log(data); 
				
				if(data!=""){					
				//alert(data.guia_docAbr);
					if(data.guia_docAbr=="T" /* && data.cli_nom !='CLIENTE' */ ){
					generarGuia_xml(data,data.url_genguia,data.emp_ruc,data.guia_numdoc,id_guia,data.cliId);
					}
				}else{
					
				}
			}
		});
	
}
	
	
function generarGuia_xml(data,url,emp_ruc,guia_numdoc,guia_id,id_cli){
		$.ajax({
		type: "POST",
		url: url,
		async:true,
		dataType: "json",                      
		data: data,
		beforeSend: function()
		{
			
		},
		success: function(data){
				// alert(data.arc+' - '+emp_ruc+' - '+guia_numdoc+' - '+data.tipcom); 
		firmarguia(data.url,data.arc,emp_ruc,guia_numdoc,data.tipcom,guia_id,id_cli);
		}
	});

}
	
	
	
	
function firmarguia(url,arc,emp_ruc,guia_numdoc,tipcom,guia_id,id_cli){
		
		$.ajax({
		type: "POST",
		url: url,
		async:true,
		dataType: "json",                      
		data: ({
			arc : arc,
			emp_ruc : emp_ruc,
			ven_numdoc:guia_numdoc,
			tipcom:tipcom,
			venid:guia_id
		}),
		beforeSend: function()
		{
			
		},
		success: function(data){
			
	enviarSunatguia(data.xml_nom,data.fil_name_zip,data.fil_name_xml,data.filenom,guia_id,id_cli,tipcom,emp_ruc);
				/* traer_datos_doc(id_ven,data.fil_name_xml); */
		}
	});

}


function enviarSunatguia(nomxml,fil_name_zip,fil_name_xml,filenom,guia_id,id_cli,tipcom,emp_ruc){
	
	//alert(filenom);
	
		$.ajax({
		type: "POST",
		url: '../../docgeneracion/enviosunatguia.php',
		async:true,
		dataType: "json",                      
		data: ({
			nomxml:nomxml,
			name_zip:fil_name_zip,
			nomfile:filenom,
			guia_id:guia_id,
			xmlarchivo:fil_name_xml,
			tipcom:tipcom,
			emp_ruc:emp_ruc
		}),
		beforeSend: function()
		{
			toastr["info"]('Esperando respuesta de sunat');
		},
		success: function(data){
			toastr["info"](data.msj);
			if(data.est==1){
				//enviarCorreo(id_ven,id_cli);
				traer_datos_docguia(guia_id,fil_name_xml);
				generar_pdf_guia(guia_id);
				//venta_tabla();
			}else{
				toastr["info"]('El documento no se ha emitido.');
			}
			
				
		}
	});

}	



function traer_datos_docguia(guia_id,urlxml){
	
	$.ajax({
			type: "POST",
			url: "../guiaremision/guia_doc_datos.php",
			async:true,
			dataType: "json",                      
			data:({ 
			guia_id: guia_id,
			urlxml :urlxml
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				/* console.log(data); */
				
			guia_doc_impresion(data,data.imp_doc);

			/* if(data.cli_ema==""){
				
			}else{
				act ="enviar";
				enviarCorreo(data);
			}	 */	
			
			}
		});
	
}


function guia_doc_impresion(data,url){
	$.ajax({
			type: "POST",
			url: url,
			async:true,
			dataType: "json",                      
			data: data ,
			beforeSend: function() {
				console.log(data);
			},
			success: function(data){
				
				
			}
		});
}
	
	
	
		function generar_pdf_guia(guiaid){	
		
		
		$.ajax({
			type: "POST",
			url: "../guiaremision/guiaremision_generar_doc.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			guiaid: guiaid
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				
				}
		});
	
	}	
	
		
	
		
		
		
		
		
$(function() {
	
	
	
	$('#chkopcionmodulo1').bootstrapToggle();
		
		
	 $('#chkopcionmodulo1').change(function() {
      if($(this).prop('checked')==true){
		  
		   $('#div_venta_grafica').hide('fade');
		   $('#div_venta_tabla').show('fade');
		   
		   $('#filtrograficos').hide('fade');
		   $('#filtroregistros').show('fade');
		  
		  
	  }else if($(this).prop('checked')==false){
		  
		   $('#div_venta_grafica').show('fade');
		   $('#div_venta_tabla').hide('fade');
		   
		   $('#filtrograficos').show('fade');
		   $('#filtroregistros').hide('fade');
		      
	  }
    });
		
	
	
	
	<?php if($dias=='info'){
		
	}else if($dias=='0'){
	    if($hora>=17){
	?>
	
		pago_form('falta','<?php echo $dias?>');
	<?php 
	}
	} else{
	    if($hora>=17){
	?>	
		pago_form('falta','<?php echo $dias?>');
	<?php 
	    }
	}
	?>
	
	venta_filtro();		

	$('#chk_ven_anu').change( function(){
		venta_tabla();
	});

	$('#btn_actualizar').click(function() {
		$("#contenido").load("../venta/venta_vista.php");
	});
	
	$("#div_cliente_form_dialog").on('hidden.bs.modal', function () {
		   	$('body').css('overflow-y','hidden');
    });
	
	$("#div_venta_form_dialog").on('hidden.bs.modal', function () {
		   	$('body').css('overflow-y','scroll');
    });
	$("#div_venta_correo_form").on('hidden.bs.modal', function () {
		   	$('body').css('overflow-y','scroll');
    });
	
	$("#div_venta_form_dialog_vista").on('hidden.bs.modal', function () {
		   	$('body').css('overflow-y','scroll');
    });
	
	$("#div_ventafac_form_dialog").on('hidden.bs.modal', function () {
		   	$('body').css('overflow-y','scroll');
    });
	
	$("#div_guia_form_dialog").on('hidden.bs.modal', function () {
		   	$('body').css('overflow-y','scroll');
    });
	
	$("#div_pago_form_dialog").on('hidden.bs.modal', function () {
		   	$('body').css('overflow-y','scroll');
    });
	
	
			$("#div_mensaje_dialog").on('hidden.bs.modal', function () {
			$('body').css('padding-right','0px');
			});
	
	venta_form('insertar')
	
	$('#div_venta_form_dialog').on('shown.bs.modal', function () { $("#txt_bus_pro_codbar").focus(); }); 


	setTitle("MODULO DE VENTAS - "+nombreEmpresa);	
	
}); 

</script>
