<?php
session_start();
if($_SESSION["sistema"]!= "1"){ header("location: ../login/index.php"); exit();}
?>
<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>
			Guia remision
			<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">Guia remision</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-xs-12 "   style="padding:0px;" >
		    <div class="card">
		        <div class="card-header">
		          <button id="btn_agregar" onClick="guiaremision_form('insertar')" class="btn btn-primary">Agregar</button>
		          <button id="btn_actualizar" class="btn btn-default">Actualizar</button>		        
		        </div>
		    </div>
		</div>
	</div>
	</div>
</section>
	<!-- busqueda de registro -->
	<div id="div_venta_filtro" class="contenido_tabla"></div>
	 
	<!-- LISTADO DE REGISTROS -->
	<div id="div_venta_tabla" class="contenido_tabla"></div>
	
	
		<div id="div_venta_impresion"></div>
		
	<!-- 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" style="width: 95% !important;" role="document" >
			<div class="modal-content">
			<div class="modal-body div_venta_form" style="padding:0%;" >
			</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>
			
			
			<!--MODAL PARA CORREO ELECTRONICOS-->
			
			<div class="modal fade" id="div_guia_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_guia_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 nuevo coductor -->
			<div class="modal fade" id="div_conductor_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_conductor_form"  >
			</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>   
				
						<!-- 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" role="document" >
			<div class="modal-content">
			<div class="modal-body div_productomarca_form" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
<script type="text/javascript">
	function guiaremision_filtro()
	{
		$.ajax({
			type: "POST",
			url: "../guiaremision/guiaremision_filtro.php",
			async:true,
			dataType: "html",                      
			data: ({
				
			}),
			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_venta_filtro').html(div);
			},
			success: function(html){
				$('#div_venta_filtro').html(html);
			},
			complete: function(){
				
			}
		});
	}
	
	
	
function actualizarestado(guiaid,vis)
	{
		$('#div_servicioval_form_dialog').modal();
		$.ajax({
			type: "POST",
			url: "../servicio/servicio_formvaldoc.php",
			async:true,
			dataType: "html",                      
			data: ({
				modide:	guiaid,
				vista:	vis
			}),
			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_servicioval_form').html(div);
			},
			success: function(html){
				$('.div_servicioval_form').html(html);				
			}
		});
	}
	function guiaremision_tabla()
	{
		$.ajax({
			type: "POST",
			url: "../guiaremision/guiaremision_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");
			
			}
		});     
	}
	
function guiaremision_form(act,idf){
		$('#div_venta_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 : 'guia'
			}),
			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_venta_form').html(div);
			},
			success: function(html){
				$('.div_venta_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?")){
			$.ajax({
				type: "POST",
				url: "../venta/venta_anular.php",
				async:true,
				dataType: "json",
				data: ({
					action: "anular",
					ven_id:		id
				}),
				beforeSend: function() {					
				},
				success: function(data){
					if(data.act!='correcto')
					{
						venta_anular(id);
					}
					toastr["success"](data.msj);	
				},
				complete: function(){
					
					venta_tabla();
				}
			});
		}
		else
		{			
			venta_tabla();	
		}
	}
	
	
	
	
	
	///// 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); 
		firmar(data.url,data.arc,emp_ruc,guia_numdoc,data.tipcom,guia_id,id_cli);
		}
	});
}
	
	
	
	
function firmar(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){
			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["error"]('El documento no se ha emitido.');
			}
			toastr["info"](data.msj);
			guiaremision_tabla();
		}
	});
}	
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 enviarCorreo(guiaid){
	
	$('#div_guia_correo_form').modal();
				
	$.ajax({
			type: "POST",
			url: "../guiaremision/guiaremision_correo_form.php",
			async:true,
			dataType: "html",
			data: ({
				guiaid:guiaid	
			}),
			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_guia_correo_conten').html(div);
	        },
			success: function(html){
				$('.div_guia_correo_conten').html(html);				
			}
		});
	
}
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() {
	guiaremision_filtro();		
	$("#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');
			});
	
	
	$('#btn_actualizar').click(function() {
		$("#contenido").load("../guiaremision/guiaremision_vista.php");
	});
	
	setTitle("MODULO DE GUIA DE REMISION - "+nombreEmpresa);	
	
}); 
</script>