<?php

// ini_set('display_errors',1);

session_start();
require_once ("../../config/Cado.php");
require_once("cNotadebito.php");
$oNotadebito = new cNotadebito();

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

require_once ("../notacredito/cNotacredito.php");
$oNotacredito = new cNotacredito();

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

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

$fecha1 = $_POST['txt_fil_notdeb_fec1']; 
$fecha2 = $_POST['txt_fil_notdeb_fec2']; 

$dts1=$oNotadebito->filtrar(fecha_mysql($fecha1),fecha_mysql($fecha2),$_POST['cmb_fil_empresa_id'],$_POST['txt_fil_docnum'],
$_POST['cmb_fil_mon_id'],$_POST['hdd_fil_cli_id']);

$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 class="" >FECHA</th>
      <th class="" >EMPRESA</th>
      <th class="" >CLIENTE</th>
      <th nowrap >N° DOCUMENTO</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="label"
		style="background-color:#4B515D;color:#fff;" >'.$numdocguia.'</a> ';
	} 
	
	$numdocnc = '';
	$dtsnc = $oNotacredito->validarVenta($dt1['tb_venta_id']);
	$numrowsnc = mysqli_num_rows($dtsnc);
	$nc = '';
	if($numrowsnc>0){
		
		while($dtnc = mysqli_fetch_array($dtsnc)){
		
		$idnc = $dtnc['tb_notacredito_id'];
		$numdocnc = 'NC '.$dtnc['tb_notacredito_numdoc'];
		$nc .= ' <br /> <a href="../../files/notacredito/notacredito_impcpe_doc.php?notid='.$idnc.'" target="_blank" class="label"
		style="background-color:#00897b;color:#fff;" >'.$numdocnc.'</a>';
		} 
	
	}
	
	
	
	$doc = '<a href="../../files/venta/venta_impcpe_doc.php?venid='.$dt1['tb_venta_id'].'" target="_blank" class="label"
		style="background-color:#3F729B;color:#fff;" >'.$dt1['tb_venta_numdoc'].'</a> ';
	 
	 
    ?>
    <tr>
	    <td nowrap >
	  <a href="../../files/notadebito/notadebito_impcpe_doc.php?notid=<?php echo $dt1['tb_notadebito_id']; ?>" target="_blank" class="btn btn-sm" style="background-color:#00897b;color:#fff;" title="DESCARGAR E IMPRIMIR PDF"><i class="fa fa-file-pdf-o"></i></a>		 
	  
	  <?php
	   if ($_SESSION['usuariogrupo_id']=='4'){}
	  else{
		if($dt1['tb_notadebito_estsun']==1){
			?>		
			<span class="btn btn-sm btn-warning" onclick="enviarCorreo('<?php echo $dt1['tb_notadebito_id']; ?>','<?php echo $dt1['tb_cliente_id']; ?>');" title="ENVIAR CORREO"><i class="fa fa-envelope-o"></i></span>		 				  
	
			<?php 
		}else{
			?>
			<a href="javascript:void(0)" class="btn btn-success btn-sm" title="EMITIR DOCUMENTO :: ENVIAR A SUNAT :: IMPRIMIR" 
			onClick="notadebito_ticket_datos('<?php echo $dt1['tb_notadebito_id']; ?>','<?php echo $dt1['tb_venta_id'] ?>');" ><i class="fa fa-sign-out"></i></a>		 
				
			<?php 
	  }
	  }
	  ?>
	  
	  
	  </td>
		<td>
		 <?php
		if($dt1['tb_notadebito_estsun']==1){
			?>		
			<small class="label" style="color:#fff;background-color:#0d47a1" style="cursor:pointer;" >ACEPTADA</small>		
			<?php 
		}else{
			?>
			<small class="label label-danger" style="cursor:pointer;" >RECHAZADA</small>	
			<?php 
		}
	  ?>
		</td>
		<td>
		<!--
		<table>
			<tr>
			<td>
		<span onclick="enviar_documentos('<?php echo $dt1['tb_venta_id']; ?>','<?php echo $idnc ?>','<?php echo $idguia; ?>','<?php echo $dt1['tb_cliente_id']; ?>','<?php echo $dt1['tb_notadebito_id']; ?>')" class="btn btn-xs btn-warning glyphicon glyphicon-envelope"></span>
			</td>
			</tr>
		</table>		
		-->
		<?php 
		echo $doc;
		echo $guia;
		echo $nc;
		
		?>		
		</td>
  
      <td class="" title="FECHA DE REGISTRO : <?php echo $dt1['tb_notadebito_reg'];  ?>" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_notadebito_fec'])?></td>
      <td  nowrap="nowrap"><?php echo $dt1['tb_empresa_nomcom']?></td>
      <td nowrap="nowrap"><?php echo $dt1['tb_cliente_nom']?></td>
      <td align="right" nowrap="nowrap"><?php echo $dt1['tb_notadebito_numdoc']?></td>
      <td nowrap class="" ><?php echo $dt1['tb_notadebito_motivo'];?></td>
      <td align="right" nowrap ><?php echo $dt1['tb_venta_numdoc'];
       if($dt1['tb_notadebito_estsun']==1){
			?>
			<small class="label label-danger" style="cursor:pointer;" >ANULADA</small>
			<?php 
		}else{
			?>
			<small class="label label-success" style="cursor:pointer;" >DISPONIBLE</small>
			<?php 
		}
      ?></td>
      <td  align="right" nowrap ><?php echo formato_money($dt1['tb_notadebito_tot']);?></td>
    
	<script type="text/javascript">
		// generar_pdf_notcredito(<?php echo $dt1['tb_notadebito_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>  