<?php
$key = 'EixMKU90GHKJGhDVN78A';
session_start();

require_once ("../../config/Cado.php");
require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once ("../notacredito/cNotacredito.php");
$oNotacredito = new cNotacredito();

require_once ("../notadebito/cNotadebito.php");
$oNotadebito = new cNotadebito();

require_once("../guiaremision/cGuiaremision.php");
$oGuiaremision = new cGuiaremision();


require_once ("../../app/empresa/cEmpresa.php");
$oEmpresa = new cEmpresa();
require_once ("../cliente/cCliente.php");
$oCliente = new cCliente();
require_once ("../usuario/cUsuario.php");
$oUsuario = new cUsuario();

require_once("../formatos/formato.php");
require_once ("../../config/Datos.php");


if($_POST['cli_id']>0){
	$dts=$oCliente->mostrarUno($_POST['cli_id']);
	$dt = mysqli_fetch_array($dts);
	
		$cli_nom=$dt['tb_cliente_nom'];
		$cli_doc=$dt['tb_cliente_doc'];
		$cli_con=$dt['tb_cliente_con'];
		$cli_tel=$dt['tb_cliente_tel'];
		$cli_ema=$dt['tb_cliente_ema'];


	mysqli_free_result($dts);
}


$dts = $oVenta->mostrarUno_ticket($_POST['venta_id']);
	while($dt = mysqli_fetch_array($dts)){
		$ven_docAbr=$dt['tb_documento_abr'];
		$ven_numDoc	=$dt['numdoc'];
		$empid=$dt['tb_empresa_id'];
}



$dts=$oEmpresa->mostrarUno($empid);
$dt = mysqli_fetch_array($dts);
	$emp_ruc=$dt['tb_empresa_ruc'];
	$emp_nomcom=$dt['tb_empresa_nomcom'];
	$emp_razsoc=$dt['tb_empresa_razsoc'];
	$emp_dir=$dt['tb_empresa_dir'];
	$emp_dir2=$dt['tb_empresa_dir2'];
	$emp_tel=$dt['tb_empresa_tel'];
	$emp_ema=$dt['tb_empresa_ema'];
	$emp_fir=$dt['tb_empresa_fir'];		
mysqli_free_result($dts);
	

if($ven_docAbr=="F"){
	$tip_com="01";
	$tipdoc = 'FACTURA ELECTRONICA';
}else if($ven_docAbr=="B"){
	$tip_com="03";
	$tipdoc = 'BOLETA ELECTRONICA';
}

$xml = $emp_ruc."-".$tip_com."-".$ven_numDoc;


if($_SESSION['usuario_id']>0)
{
	$dts=$oUsuario->mostrarUno($_SESSION['usuario_id']);
	$dt = mysqli_fetch_array($dts);
		$usugru		=$dt['tb_usuariogrupo_id'];
		$usugru_nom	=$dt['tb_usuariogrupo_nom'];
		$usu_nom	=$dt['tb_usuario_nom'];
		$apepat		=$dt['tb_usuario_apepat'];
		$apemat		=$dt['tb_usuario_apemat'];
		$ema		=$dt['tb_usuario_ema'];
	
	mysqli_free_result($dts);
	
	$usuario_reg="$usu_nom $apepat $apemat";
}

    $empresa=$_SESSION['empresa_nombre'];

	$asu=$emp_razsoc;
	$destinatario='Estimado(a) cliente:<br>'.$cli_nom.'<br>';
	$titulo= $emp_razsoc;





	$numdocguia = '';
	$dtsguia = $oGuiaremision->verexistencia($_POST['venta_id']);
	$numrowsguia = mysqli_num_rows($dtsguia);
	$guia = '';
	if($numrowsguia>0){
		
		while($dtg = mysqli_fetch_array($dtsguia)){
		$idguia = $dtg['tb_guiaremision_id'];
		$numdocguia = ''.$dtg['tb_guiaremision_numdoc'];
	    
		}
		
		$xmlguia = $emp_ruc."-09-".$numdocguia;
	
	}
	
	$numdocnc = '';
	$dtsnc = $oNotacredito->validarVenta($_POST['venta_id']);
	$numrowsnc = mysqli_num_rows($dtsnc);
	$nc = '';
	if($numrowsnc>0){
		
		while($dtnc = mysqli_fetch_array($dtsnc)){
		$idnc = $dtnc['tb_notacredito_id'];
		$numdocnc = ''.$dtnc['tb_notacredito_numdoc'];
	
	} 
	    $xmlnc = $emp_ruc."-07-".$numdocnc;
	}
	
	$numdocnd = '';
	$dtsnd = $oNotadebito->validarVenta($_POST['venta_id']);
	$numrowsnd = mysqli_num_rows($dtsnd);
	$nd = '';
	if($numrowsnd>0){
		
		while($dtnd = mysqli_fetch_array($dtsnd)){
		$idnd = $dtnd['tb_notadebito_id'];
		$numdocnd = ''.$dtnd['tb_notadebito_numdoc'];
		
	} 
    	$xmlnd = $emp_ruc."-08-".$numdocnd;
	}
	
	



	$mentip='<p>Por la presente le comunicamos '.$emp_razsoc.' emisor electronico de comprobantes de pago, ha emitido el siguiente documento: </p>
	
    <table style="width:80%;" >
    
    ';
    
    $mentip.='
	 <tr style="border:1px solid #010101">
	 <td style="background-color:#263238;color:#fff;" > <strong>TIPO DE DOCUMENTO</strong></td>
	 <td align="right"><strong>'.$tipdoc.'</strong></td>
	 </tr> 
	 
	 <tr style="border:1px solid #010101" >
	 <td style="background-color:#263238;color:#fff;" ><strong >NUMERO DE DOCUMENTO</strong></td>
	 <td align="right"> <strong>'.$ven_numDoc.'</strong></td>
	 </tr>
	 
	 <tr style="border:1px solid #010101" >
	 <td style="" ></td>
	 <td align="right"></td>
	 </tr>
	 
	 ';
	 
	 
	 
	 if($idguia>0){
	     
	  $mentip.='
	 <tr style="border:1px solid #010101">
	 <td style="background-color:#FFC107;color:#010101;" > <strong>TIPO DE DOCUMENTO</strong></td>
	 <td align="right"><strong>GUIA DE REMISION</strong></td>
	 </tr> 
	 
	 <tr style="border:1px solid #010101" >
	 <td style="background-color:#FFC107;color:#010101;" ><strong >NUMERO DE DOCUMENTO</strong></td>
	 <td align="right"> <strong>'.$numdocguia.'</strong></td>
	 </tr>
	 
	 <tr style="border:1px solid #010101" >
	 <td style="" ></td>
	 <td align="right"></td>
	 </tr>
	 
	 ';
	     
	 }
	 
	 
	  if($idnc>0){
	     
	  $mentip.='
	 <tr style="border:1px solid #010101">
	 <td style="background-color:#EC407A;color:#010101;" > <strong>TIPO DE DOCUMENTO</strong></td>
	 <td align="right"><strong>NOTA DE CREDITO</strong></td>
	 </tr> 
	 
	 <tr style="border:1px solid #010101" >
	 <td style="background-color:#EC407A;color:#010101;" ><strong >NUMERO DE DOCUMENTO</strong></td>
	 <td align="right"> <strong>'.$numdocnc.'</strong></td>
	 </tr>
	 
	 <tr style="border:1px solid #010101" >
	 <td style="" ></td>
	 <td align="right"></td>
	 </tr> 
	 
	 ';
	     
	 }
	 
	 
	 
	  if($idnd>0){
	     
	  $mentip.='
	 <tr style="border:1px solid #010101">
	 <td style="background-color:#0277BD;color:#fff;" > <strong>TIPO DE DOCUMENTO</strong></td>
	 <td align="right"><strong>NOTA DE DEBITO</strong></td>
	 </tr> 
	 
	 <tr style="border:1px solid #010101" >
	 <td style="background-color:#0277BD;color:#fff;" ><strong >NUMERO DE DOCUMENTO</strong></td>
	 <td align="right"> <strong>'.$numdocnd.'</strong></td>
	 </tr> 
	 ';
	     
	 }
	 
	 
	 
	 
	 $mentip.='
	 </table>
	 
	 Adjuntamos archivos con extensiones <strong>XML</strong> y <strong>PDF</strong> para su respectiva descarga.
	';
	
	
	
	
	
	

	$tabla.='';


	$emisor="<p>Atentamente, <strong>".$emp_razsoc."</strong></p>";

		$firma='
	<table align="left" border="0" cellpadding="1" cellspacing="1" style="width:50%">
		<tbody>
		
			    <tr>
				<td style="width:25%">
				<img width="100%" src="'.$d_sc_urllogo.'" /> 
				</td>
				<td style="width:25%">
				<img width="100%" src="'.$d_sc_urllogoempresa.'" /> 
				</td>
			    </tr>
	
		</tbody>
	</table>
	';

	$men=$destinatario.''.$mentip.''.$tabla.''.$emisor.''.$firma;

?>


<script type="text/javascript">









$(function(){
		
	$("#save_form_enviar").click(function(){
		
		if(	$("#txt_cli_ema").val()==""){
		    
		}else{
		generarventa_pdf('<?php echo $_POST['venta_id']; ?>');
		
		<?php if($idguia>0){
		?>
	    	generarguia_pdf('<?php echo $idguia; ?>');
		<?php 
		}?>
		
		
		<?php if($idnc>0){
		?>
	    	generarnc_pdf('<?php echo $idnc; ?>');
		<?php 
		}?>
		
		<?php if($idnd>0){
		?>
	    	generarnd_pdf('<?php echo $idnd; ?>');
		<?php 
		}?>
		
		
		
		setTimeout(function(){ 
						 
		}, 2000);
        setTimeout(function(){ 
           $('#for_ven_corr').submit();
        }, 2000);
		
		
		
		}	
		
		
		
	});	
	
	
	
	
	
	
		
	$('#for_ven_corr').bootstrapValidator({
	    
	      message: 'Valor no Valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: { 
	   	
	      	txt_cli_ema: {
	      		group: '.col-lg-10',
	      		validators: {
	      			notEmpty: {
	      				message: 'Ingrese un correo*'
	      			}
	      		}
	      	},
	      }
	  }).on('success.form.bv', function(e) {
	  	
	  	$.ajax({
	  		type: "POST",
	  		url: "../venta/venta_correoreg.php",
	  		async:false,
	  		dataType: "json",
	  		data: $("#for_ven_corr").serialize(),
	  		beforeSend: function(){
					
				},
				success: function(data)
				{
					if(data.ven_corr_est==1){
						$('.modal').modal('hide');
						$('.in').removeClass('modal-backdrop');		
						toastr["info"](data.ven_corr_msj);		
					}else{
						toastr["error"](data.ven_corr_msj);		
					}
					
							
				},
				complete: function(){
					venta_tabla();
				}
			});	
	  });
	  
		
		
		
		
		
});

</script>



	    <div class="col-lg-12 col-sm-12 col-xs-12" >
	   
		
		
			<h4>ENVIAR CORREO:</h4>
		
		
		<form id="for_ven_corr">
			<input type="hidden" id="action_correo" name="action_correo" value="<?php echo $_POST['action']?>">
			<input type="hidden" id="hdd_rep_emp_id" name="hdd_rep_emp_id" value="<?php echo $_SESSION['empresa_id']?>">
			<input type="hidden" id="hdd_cli_id" name="hdd_cli_id" value="<?php echo $_POST['cli_id']?>" />
			<input type="hidden" id="hdd_ven_id" name="hdd_ven_id" value="<?php echo $_POST['venta_id']?>" />
			<input type="hidden" id="hdd_cli_nom" name="hdd_cli_nom" value="<?php echo $cli_nom?>" />

			<input name="cli_numdoc" class="" type="hidden" id="hdd_numdoc" value="<?php echo $cli_doc?>" >
			<input name="emp_numdoc" class="" type="hidden" id="hdd_numdoc" value="<?php echo $emp_ruc?>" >
			<input name="hdd_numdoc" class="" type="hidden" id="hdd_numdoc" value="<?php echo $ven_numDoc?>" >
			<input name="hdd_xmlzip" class="" type="hidden" id="hdd_xmlzip" value="<?php echo $xml?>" >
			
			
			<input name="hdd_numdocguia" class="" type="hidden" id="hdd_numdocguia" value="<?php echo $numdocguia?>" >
			<input name="hdd_numdocnc" class="" type="hidden" id="hdd_numdocnc" value="<?php echo $numdocnc?>" >
			<input name="hdd_numdocnd" class="" type="hidden" id="hdd_numdocnd" value="<?php echo $numdocnd?>" >
			
			<input name="hdd_xmlguiazip" class="" type="hidden" id="hdd_xmlguiazip" value="<?php echo $xmlguia?>" >
			<input name="hdd_xmlnczip" class="" type="hidden" id="hdd_xmlnczip" value="<?php echo $xmlnc?>" >
			<input name="hdd_xmlndzip" class="" type="hidden" id="hdd_xmlndzip" value="<?php echo $xmlnd?>" >
			
			<div class="row">
		
			<div class="col-lg-6 col-md-6 col-12">
		    	<label for="txt_cli_ema">Correo:</label>
			    <input name="txt_cli_ema" class="form-control" type="text" id="txt_cli_ema" value="<?php echo $cli_ema?>">
			</div>
		
			
			<div class="col-lg-6 col-md-6 col-2">
		    	<label for="">Ruc:</label>
			    <input name="txt_cli_doc" class="form-control" type="text" id="txt_cli_doc" value="<?php echo $cli_doc?>" readonly="true" >
			</div>
			
			<div class="col-lg-6 col-md-6 col-12">
			<label for="">Razon Soc.:</label>
			<input name="txt_cli_nom" class="form-control" type="text" id="txt_cli_nom" value="<?php echo $cli_nom?>"  readonly="true" >
			</div>			
					
		
			<div class="col-lg-6 col-md-6 col-12">
				<label for="txt_cor_asu">Asusto:</label>
				<input name="txt_cor_asu" class="form-control" type="text" id="txt_cor_asu" value="<?php echo $asu?>" readonly="true" >
			</div>
	
			<div class="col-lg-12 col-md-12 col-12">
					<label for="txt_cor_men">Mensaje:</label>	
					<textarea style="display:none;" name="txt_cor_men" id="txt_cor_men"><?php echo $men?></textarea>
					<div style="width:100%;height:300px;overflow-y:scroll;"><?php echo $men?></div>	
			</div>
			
			</div>
				
			</form>


		<div class="box-footer float-right">
	    	<button class="btn btn-primary" id="save_form_enviar">ENVIAR</button>
		    <button type="button" data-dismiss="modal"  title="Canelar correo" class="btn btn-default">Cancelar</button>		
		</div>
			
		</div>
		  