<?php

session_start();
require_once ("../../config/Cado.php");
require_once("cNotacredito.php");
$oNotacredito = new cNotacredito();

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

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

require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
$coddoc = '07';

require_once ("../historial/cHistorial.php");
$oHistorial = new cHistorial();


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

$fecha1 = $_POST['txt_fil_notcre_fec1']; 
$fecha2 = $_POST['txt_fil_notcre_fec2']; 

$dts1=$oNotacredito->filtrar(fecha_mysql($fecha1),fecha_mysql($fecha2),$_POST['cmb_fil_puntoventa_id'],$_POST['txt_fil_docnum'],
$_POST['cmb_fil_mon_id'],$_POST['hdd_fil_cli_id'],$_POST['cmb_fil_nc_estsun'],$_POST['cmb_fil_nc_lim']);


if($fecha1!=$fecha2 || $_POST['txt_fil_docnum'] != ""){
	
		$user_agen = $_SERVER['HTTP_USER_AGENT'];
		$mod = 7;
		$mod_ide = 0;
		$det = 'MOSTRANDO NOTAS DE CREDITO SEGUN ['.$fecha1.' | '.$fecha2.' | '.$_POST['txt_fil_docnum'].']';
		$fec = date('Y-m-d');
		$doc_id = '';
		$numdoc = '';
		$ip = mostrar_ip();
	 	
		$oHistorial->insertar(
		fecha_mysql($fec),
		$det,
		$ip,
		intval($_SESSION['usuario_id']),
		intval($mod),
		intval($mod_ide),
		intval($doc_id),
		$numdoc
		);

	
	
}

$num_rows= mysqli_num_rows($dts1);
?>

<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
		    <div class="card">
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:5px;">
					<div class="table-responsive">
	
	
          <table id="tabla_venta" class="table table-bordered table-striped">
		  
<?php 
/* echo fecha_mysql($fecha1).'-'.fecha_mysql($fecha2).'-'.$_POST['cmb_fil_doc_id'].'-'.$_POST['hdd_fil_cli_id'].'-'.$_POST['cmb_fil_ven_est'].'-'.$_SESSION['usuario_id'].'-p'. */
/* $_SESSION['puntoventa_id'].'-'.$_POST['chk_fil_ven_may'].'-'.$_POST['cmb_fil_moz_id']; */
/* echo $_POST['txt_fil_docnum']; */
?>
		  
           <thead>
    <tr>
	<th></th>
	<th>ESTADO SUNAT</th>
	<th>REFERENCIAS</th>
	   <th nowrap >N° DOCUMENTO</th>
      <th class="" >FECHA</th>
      <th class="" >EMPRESA</th>
      <th class="" >CLIENTE</th>
   
      <th class="" >MOTIVO</th>
      <th nowrap >DOC. OPERACION</th>
      <th nowrap >TOTAL OPERACION</th>
      
    </tr>
  </thead> 
            <tbody>
    <?php
    if($num_rows>0){
    while($dt1 = mysqli_fetch_array($dts1))
    {
     
	
	$numdocguia = '';
	$dtsguia = $oGuiaremision->verexistencia($dt1['tb_venta_id']);
	$numrowsguia = mysqli_num_rows($dtsguia);
	$guia = '';
	if($numrowsguia>0){
		
		$dtg = mysqli_fetch_array($dtsguia);
		$idguia = $dtg['tb_guiaremision_id'];
		$numdocguia = 'GUIA '.$dtg['tb_guiaremision_numdoc'];
		$guia = '<a href="../../files/guiaremision/guiaremision_impcpe_doc.php?guiaid='.$idguia.'" target="_blank" class="badge"
		style="background-color:#4B515D;color:#fff;" >'.$numdocguia.'</a> ';
	} 
	
	
	$numdocnd = '';
	$dtsnd = $oNotadebito->validarVenta($dt1['tb_venta_id']);
	$numrowsnd = mysqli_num_rows($dtsnd);
	$nd = '';
	if($numrowsnd>0){
		
		while($dtnd = mysqli_fetch_array($dtsnd)){
		$idnd = $dtnd['tb_notadebito_id'];
		$numdocnd = 'ND '.$dtnd['tb_notadebito_numdoc'];
		$nd .= ' <br /> <a href="../../files/notadebito/notadebito_impcpe_doc.php?notid='.$idnd.'" target="_blank" class="badge"
		style="background-color:#1c2a48;color:#fff;" >'.$numdocnd.'</a>';
	} 
	}
	
	$doc = '<a href="../../files/venta/venta_impcpe_doc.php?venid='.$dt1['tb_venta_id'].'" target="_blank" class="badge"
		style="background-color:#3F729B;color:#fff;" >'.$dt1['tb_venta_numdoc'].'</a> ';
	 
	 
    ?>
    <tr>
	    <td nowrap >
		
			<span class="btn btn-info btn-sm" title="VER INFORMACION DEL DOCUMENTO : <?php echo $dt1['tb_notacredito_numdoc'];?>" 
			data-toggle="tooltip" data-placement="top"  onClick="notacredito_form_vista('editar','<?php echo $dt1['tb_notacredito_id']?>')"><i class="fas fa-edit"></i></span> 
   
   
   
	  <a href="../../files/notacredito/notacredito_impcpe_doc.php?notid=<?php echo $dt1['tb_notacredito_id']; ?>" target="_blank" class="btn btn-sm"  style="background-color:#00897b;color:#fff;" title="DESCARGAR E IMPRIMIR PDF"><i class="fa fa-file-pdf"></i></a>		 
	  <?php
		if($dt1['tb_notacredito_estsun']==1){
			?>		
			<span class="btn btn-sm btn-primary" style="cursor:pointer" onclick="enviarCorreo('<?php echo $dt1['tb_notacredito_id']; ?>','<?php echo $dt1['tb_cliente_id']; ?>');" 
			title="ENVIAR CORREO"><i class="fas fa-envelope-square"></i></span>		 				  
			<span style="cursor:pointer" class="btn btn-sm btn-warning" onclick="notacredito_ticket_datos('<?php echo $dt1['tb_notacredito_id']?>','<?php echo $dt1['tb_venta_id'] ?>','<?php echo $dt1['tb_notacredito_estsun']?>')"><i class="fas fa-print"></i></span>
	  
			<?php 
		}else{
			?>
			<a style="cursor:pointer" href="javascript:void(0)" class="btn btn-success btn-sm" title="EMITIR DOCUMENTO :: ENVIAR A SUNAT :: IMPRIMIR" 
			onClick="notacredito_ticket_datos('<?php echo $dt1['tb_notacredito_id']; ?>','<?php echo $dt1['tb_venta_id'] ?>');" ><i class="fas fa-angle-double-right"></i></a>		 
				<?php if($_SESSION['usuariogrupo_id']==1){?>
			<span style="cursor:pointer" class="btn btn-sm btn-primary" title="ACTUALIZAR ESTADO DE SUNAT" 
				onClick="actualizarestado('<?php echo $dt1['tb_notacredito_id']; ?>','nc');" ><i class="fas fa-retweet"></i></span>	
			<?php 
	  }
			
		}
	  ?>
	    <a href="../../docfirmado/<?php echo $dt1['tb_empresa_ruc'].'-'.$coddoc.'-'.$dt1['tb_notacredito_numdoc'].'.xml'?>" class="btn btn-danger btn-sm" 
				download="<?php echo $dt1['tb_empresa_ruc'].'-'.$coddoc.'-'.$dt1['tb_notacredito_numdoc'].'.xml'; ?> " data-toggle="tooltip" data-placement="top"  title="Descargar xml firmado"  ><i class="fas fa-file-code"></i></a>
				
				
				
	<a href="../../doccdr/R-<?php echo $dt1['tb_empresa_ruc'].'-'.$coddoc.'-'.$dt1['tb_notacredito_numdoc'].'.zip';?>" class="btn btn-dark btn-sm" 
				download="<?php echo 'R-'.$dt1['tb_empresa_ruc'].'-'.$coddoc.'-'.$dt1['tb_notacredito_numdoc'].'.zip'; ?>" data-toggle="tooltip" data-placement="top"  title="Descargar CDR"  ><i class="fas fa-file-archive"></i></a>
				
				
	  
	  </td>
		<td>
		<?php
		if($dt1['tb_notacredito_estsun']==1){
			?>		
			<small class="badge" style="color:#fff;background-color:#0d47a1" style="cursor:pointer;" >ACEPTADA</small>		
			<?php 
		}else if($dt1['tb_notacredito_estsun']==2){
			?>
			<small class="badge badge-danger" style="cursor:pointer;" >RECHAZADA POR SUNAT</small>	
			<?php 
		}else if($dt1['tb_notacredito_estsun']==0){
			?>
			<small class="badge badge-warning" style="cursor:pointer;" >VOLVER A REENVIAR</small>	
			<?php 
		}else if($dt1['tb_notacredito_estsun']==3){
			?>
			<small class="badge badge-info" style="cursor:pointer;" >ANULADA POR EL SISTEMA</small>	
			<?php 
		}
	  ?>
		</td>
		<td>
		<!--
		<table>
			<tr>
			<td>
		<span onclick="enviar_documentos('<?php echo $dt1['tb_venta_id']; ?>','<?php echo $dt1['tb_notacredito_id'] ?>','<?php echo $idguia; ?>','<?php echo $dt1['tb_cliente_id']; ?>','<?php echo $idnd; ?>')" class="btn btn-xs btn-warning glyphicon glyphicon-envelope"></span>
			</td>
			</tr>
		</table>
		-->
		<?php 
		echo $doc;
		echo $guia;
		echo $nd;
		
		?>		
		</td>
        <td align="right" nowrap="nowrap"><?php echo $dt1['tb_notacredito_numdoc']?></td>
      <td class="" title="FECHA DE REGISTRO : <?php echo $dt1['tb_notacredito_reg'];  ?>" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_notacredito_fec'])?></td>
      <td  nowrap="nowrap"><?php echo $dt1['tb_empresa_nomcom']?></td>
      <td nowrap="nowrap"><?php echo $dt1['tb_cliente_nom']?></td>

      <td class="" ><?php echo $dt1['tb_notacredito_motivo'];?></td>
      <td align="right" nowrap ><?php echo $dt1['tb_venta_numdoc'];
       if($dt1['tb_notacredito_estsun']==1){
			?>
			<small class="badge badge-danger" style="cursor:pointer;" >ANULADA</small>
			<?php 
		}else{
			?>
			<small class="badge badge-success" style="cursor:pointer;" >DISPONIBLE</small>
			<?php 
		}
      ?></td>
      <td  align="right" nowrap ><?php echo formato_money($dt1['tb_notacredito_tot']);?></td>
    
	<script type="text/javascript">
		generar_pdf_notcredito(<?php echo $dt1['tb_notacredito_id']; ?>);
	</script>
											
	</tr>
	
	
    <?php
  }
  mysqli_free_result($dts1);
  }
  ?> 
		
</tbody> 

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


 <script type="text/javascript">
  $(function() {  
    $("#tabla_venta").DataTable({
      "columnDefs": [
      { "orderable": false, "targets": [3,4] }
      ],
      "searching": false,
      "order":[[0,"asc"]]
    });
  }); 
</script>  