<?php 
session_start();
if($_SESSION["sistema"]!= "1"){ header("location: ../login/index.php"); exit();}
?>
<style type="text/css">
select{
	cursor:pointer;
}
</style>





<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:0px;" >
		    <div class="card">
		        <div class="card-header">
		    
				 <div id="tabventa" class="nav-tabs-custom">
 								<nav class="nav nav-tabs">
 									<a href="#tab_registrar" style="color:#000;" class="nav-item nav-link active"  data-toggle="tab" aria-expanded="false">Registrar Consumo</a>
 									<a href="#tab_registros" style="color:#000;" class="nav-item nav-link "  id="formpagotitle" data-toggle="tab" aria-expanded="true">Registros de Consumos</a>
 								</nav>
 								
								<div class="tab-content">
										<div class=" active tab-pane " id="tab_registrar">
											<div class="row"> 
											<div class="col-lg-12 col-md-12 col-12" style="padding:0px;">
													<div id="div_consumopagar_form" class="no-padding"></div>	
											</div>
											</div>
 										</div>
 										
 										<div class="tab-pane " id="tab_registros">
 											
											<div class="row"> 
													<div id="div_consumopagar_filtro" class=""></div>
													<div class="col-lg-12 col-md-12 col-12 no-padding">
													<div id="div_consumopagar_tabla" class=""></div>
													</div>
											</div>

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


<!-- formulario de mensaje -->
			<div class="modal" id="div_mensaje_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  
			style="padding:0px; overflow-y:scroll;z-index:9999;">
			<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_consumopagar_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="width: 95% !important;" role="document" >
			<div class="modal-content">
			<div class="modal-body div_consumopagar_form" style="padding:0%;" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<div class="modal fade" id="div_ventaconsumo_form_dialog" 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_ventaconsumo_form" style="padding:0px;"   >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			
			<div class="modal fade" id="div_consumopagar_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_consumopagar_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>
			
				<!-- 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="width: 90% !important;"   role="document" >
			<div class="modal-content">
			<div class="modal-body div_productomarca_form" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
	
	<script type="text/javascript">
		
		function consumopagar_form_vista(act,idf){
		$('#div_consumopagar_form_dialog').modal();
		//$('body').css('overflow-y','hidden');
		//$('body').css('padding-right','0px');
		$.ajax({
			type: "POST",
			url: "../consumopagar/consumopagar_form_vista.php",
			async:true,
			dataType: "html",                      
			data: ({
				action: act,
				conpagid:	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_consumopagar_form').html(div); 
			},
			success: function(html){
				$('.div_consumopagar_form').html(html);	
			},
			complete: function(){
			}
		});
	}
	
	function consumopagar_form(act,idf){
	
		$.ajax({
			type: "POST",
			url: "../consumopagar/consumopagar_form.php",
			async:true,
			dataType: "html",   
			position:'top',                   
			data: ({
				action: act,
				conpagid:	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_consumopagar_form').html(div);
			},
			success: function(html){
				$('#div_consumopagar_form').html(html);				
			},
			complete: function(){
			}
		});
	}
	
	
function enviarCorreo(conpagid,cliid){
	
	$('#div_consumopagar_correo_form').modal();
				
	$.ajax({
			type: "POST",
			url: "../consumopagar/consumopagar_correo_form.php",
			async:true,
			dataType: "html",
			data: ({
				conpagid:conpagid,
				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_consumopagar_correo_conten').html(div);
	        },
			success: function(html){
				$('.div_consumopagar_correo_conten').html(html);				
			}
		});
	
}
function enviar_documentos(conpagid,cliid){
	
	$('#div_consumopagar_correo_form').modal();
				
	$.ajax({
			type: "POST",
			url: "../consumopagar/documento_correo_form.php",
			async:true,
			dataType: "html",
			data: ({
				conpagid:conpagid,
				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_consumopagar_correo_conten').html(div);
	        },
			success: function(html){
				$('.div_consumopagar_correo_conten').html(html);				
			}
		});
	
}
	
	function generar_pdf(conpagid){	
		
		$.ajax({
			type: "GET",
			url: "../consumopagar/consumopagar_generar_doc.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			conpagid: conpagid
			}),
			beforeSend: function() {	
			},
			success: function(data){
			}
		});
}
	
	
	
	
		function consumopagar_filtro(){
			$.ajax({
				type: "POST",
				url: "../consumopagar/consumopagar_filtro.php",
				dataType: "html",
				beforeSend: function() {
					$('#div_consumopagar_filtro').addClass("ui-state-disabled");
				},
				success: function(html){
					$('#div_consumopagar_filtro').html(html);
				},
				complete: function(){			
					$('#div_consumopagar_filtro').removeClass("ui-state-disabled");
				}
			}); 
		}
		
		function consumopagar_tabla(){
		
			$.ajax({
				type: "POST",
				url: "../consumopagar/consumopagar_tabla.php",
				dataType: "html",
				data:  $("#for_fil_cot").serialize(),
				beforeSend: function() {
					
					$('#div_consumopagar_tabla').addClass("ui-state-disabled");
				},
				success: function(html){
					$('#div_consumopagar_tabla').html(html);
				},
				complete: function(){					
					$('#div_mensaje_dialog').modal('hide');				
					
					$('#div_consumopagar_tabla').removeClass("ui-state-disabled");
					
				}
			}); 
		}
		
		
		
		function generar_venta(conpagid,empid){
		
		
		$('#div_ventaconsumo_form_dialog').modal();
		//$('body').css('overflow-y','hidden');
		$.ajax({
			type: "POST",
			url: "../consumopagar/consumopagar_ventaform.php",
			async:true,
			dataType: "html",   
			position:'top',                   
			data: ({
				conpagid:	conpagid,
				empid:	empid
			}),
			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_ventaconsumo_form').html(div);
			},
			success: function(html){
				$('.div_ventaconsumo_form').html(html);				
			},
			complete: function(){
			}
		});
		
		
		
			/* $.ajax({
				type: "POST",
				url: "../consumopagar/consumopagar_venta.php",
				dataType: "json",
				data:  ({
					conpagid:conpagid
				}),
				beforeSend: function() {
					
				},
				success: function(data){
				toastr["info"](data.msj);		
				},
				complete: function(){			
					
				}
			});  */
		}
		
		
		function consumopagar_anular(id,texto){   
		if(confirm("Realmente desea anular la consumopagar Nº "+texto+", Continuar?")){
			$.ajax({
				type: "POST",
				url: "../consumopagar/consumopagar_reg.php",
				async:true,
				dataType: "json",
				data: ({
					action: "eliminar",
					conpagid: id
				}),
				beforeSend: function() {					
				},
				success: function(data){
					/* if(data.act!='correcto')
					{
						consumopagar_anular(id);
					} */
					toastr["info"](data.msj);	
				},
				complete: function(){
					
					consumopagar_tabla();
				}
			});
		}
		else
		{			
			consumopagar_tabla();	
		}
	}
	
		function actualizarestado(conpag_id,campo,valor,texto){   
		if(confirm("Realmente desea cambiar el estado del CONSUMO Nº "+texto+", Continuar?")){
			$.ajax({
				type: "POST",
				url: "../consumopagar/consumopagar_reg.php",
				async:true,
				dataType: "json",
				data: ({
					action: "actualizarestado",
					conpag_id: conpag_id,
					campo: campo,
					valor: valor,
					texto:texto
				}),
				beforeSend: function() {					
				},
				success: function(data){				
					toastr["success"](data.msj);	
				},
				complete: function(){
					
					consumopagar_tabla();
				}
			});
		}
		else
		{			
			consumopagar_tabla();	
		}
	}
	
	///FACTURACION ELECTRONICAA
	
	
	
	
	
	
	
	function venta_ticket_datos(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=="F" /* && data.cli_nom !='CLIENTE' */ ){
					generarFactura_xml(data,data.url_genfac,data.emp_ruc,data.ven_numdoc,id_ven,data.cli_id);
					}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);
					}else if(data.ven_docabr=="NV"){
						
					generar_pdf_documento(id_ven);
					//venta_ticket_impresion(data,data.imp_doc);
					
					}		
				}else{
					
				}
			}
		});
	
	}
	
function generarFactura_xml(data,url,emp_ruc,ven_numdoc,id_ven,id_cli){
		$.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);
		}
	});
}
function generarBoleta_xml(data,url,emp_ruc,ven_numdoc,id_ven,id_cli){
		/* 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);			
			/* alert(html); */
		//tip_com = '03';
		//crear_xml(html,emp_ruc,ven_numdoc,tip_com,id_ven,id_cli);
		}
	});
}
	
	function consumopagar_ticket_datos(conpag_id){	
		
		/* alert(id_ven); */
		
		$.ajax({
			type: "POST",
			url: "../consumopagar/consumopagar_ticket_datos.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			conpagid: conpag_id
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				 console.log(data); 
				
				if(data!=""){						
					consumopagar_ticket_impresion(data,data.imp_doc);
				}else{
					
				}
			}
		});
	
}
function consumopagar_ticket_impresion(data,url){	
	$.ajax({
		type: "POST",
		url: url,
		async:true,
		dataType: "html",                      
		data: data,
		beforeSend: function()
		{
			console.log(data);	
		},
		success: function(html){
				
			
		}
	});
}
	
function firmar(url,arc,emp_ruc,ven_numdoc,tipcom,id_ven,id_cli){
		/* 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);
			traer_datos_doc(id_ven,data.fil_name_xml);
	enviarSunat(data.xml_nom,data.fil_name_zip,data.fil_name_xml,data.filenom,id_ven,id_cli,tipcom,emp_ruc);
				/* traer_datos_doc(id_ven,data.fil_name_xml); */
		}
	});
}
	
	
	
 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 enviarSunat(nomxml,fil_name_zip,fil_name_xml,filenom,id_ven,id_cli,tipcom,emp_ruc){
	
		$.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,
			emp_ruc:emp_ruc
		}),
		beforeSend: function()
		{
			toastr["error"]('Esperando respuesta de sunat');
		},
		success: function(data){
			if(data.est==1){
				//enviarCorreo(id_ven,id_cli);
				//traer_datos_doc(id_ven,fil_name_xml);
				//generar_pdf_documento(id_ven);
				//venta_tabla();
			}else{
				toastr["info"]('El documento no se ha emitido.');
			}
			
			toastr["info"](data.msj);
				
		}
	});
}	
function traer_datos_doc(venid,urlxml){
	
	$.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); */
				
			venta_doc_impresion(data,data.imp_doc);
			/* if(data.cli_ema==""){
				
			}else{
				act ="enviar";
				enviarCorreo(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){
				
				
			}
		});
}
	
	///// 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);
	//$('#div_mensaje_dialog').modal();
	
		$.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()
		{
		//	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> ESPERANDO SOLICITUD DE SUNAT...</div>  </center>";	
			//	$('.div_mensaje').html(div);
			toastr["error"]('Esperando respuesta de sunat');
		},
		success: function(data){
			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.');
			}
			toastr["info"](data.msj);
			
			//$('#div_mensaje_dialog').modal('hide');
		}
	});
}	
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_documento(venid)
	{	
	    //alert(venid);
		$.ajax({
			type: "GET",
			url: "../venta/venta_generar_doc.php",
			async:true,
			dataType: "json",                      
			data: ({
			venid: venid
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				
				}
		});
	
	}
	
	function generar_pdf_guia(guiaid){	
		$.ajax({
			type: "GET",
			url: "../guiaremision/guiaremision_generar_doc.php",
			async:true,
			dataType: "json",                      
			data: ({ 
			guiaid: guiaid
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				
				}
		});
	
	}	
	
$(function() {
			consumopagar_form('insertar');
			consumopagar_filtro();
			
			$('#btn_actualizar').click(function() {
				$("#contenido").load("../consumopagar/consumopagar_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_mensaje_dialog").on('hidden.bs.modal', function () {
			//$('body').css('padding-right','0px');
			});
	
	
			setTitle("CONSUMO POR PAGAR - "+nombreEmpresa);
		});
	</script>