<?php
// ini_Set('display_errors',1);
session_start();
require_once ("../../config/Cado.php");
require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once ("../venta/cVentapago.php");
$oVentapago = new cVentapago();
require_once ("../ingreso/cIngreso.php");
$oIngreso = new cIngreso();
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 ("../historial/cHistorial.php");
$oHistorial = new cHistorial();
require_once ("../formatos/formato.php");
$fecha1 = $_POST['txt_fil_ven_fec1']; 
$fecha2 = $_POST['txt_fil_ven_fec2']; 
//echo  $_POST['cmb_fil_ven_est'] ;
$dts1=$oVenta->mostrar_filtro(fecha_mysql($fecha1),fecha_mysql($fecha2),$_POST['cmb_fil_doc_id'],$_POST['txt_fil_docnum'],
$_POST['hdd_fil_cli_id'],$_POST['cmb_fil_mon_id'],$_POST['cmb_fil_ven_est'],$_POST['cmb_fil_puntoventa_id'],$_POST['cmb_fil_ven_estsun'],$_POST['cmb_fil_ven_lim']);
			
$num_rows= mysqli_num_rows($dts1);
	if($_POST['txt_fil_docnum']!="" || $fecha1 != $fecha2 || $_POST['cmb_fil_doc_id']!="" || $_POST['txt_fil_cli'] !=""){
		
		$user_agen = $_SERVER['HTTP_USER_AGENT'];
		$mod = 3;
		$mod_ide = 0;
		$det = 'MOSTRANDO VENTAS SEGUN ['.$fecha1.' | '.$fecha2.' | '.$_POST['cmb_fil_doc_id'].' | '.
		$_POST['txt_fil_docnum'].' | '.$_POST['txt_fil_cli'].']';
		$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
		);
		}
		
		
?>
<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 $_POST['cmb_fil_mon_id'];
/* 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>&nbsp;</th>
	  <th nowrap class="">ESTADO SUNAT</th>
	  <th nowrap class="">REFERENCIAS</th>
	  <th nowrap >DOCUMENTO</th>
      <th nowrap >FECHA E.</th>
      <th nowrap >FECHA V.</th>     
      <th nowrap >CLIENTE</th>
      <th class="">RUC/DNI</th>      
      <th align="right">TOTAL</th>
      <th class="" align="right">PAGOS</th>
      <th class="" align="right">SALDO</th>
      
    </tr>
  </thead> 
            <tbody>
    <?php
    if($num_rows>0){
    while($dt1 = mysqli_fetch_array($dts1))
    {
       $ven_est = $dt1['tb_venta_est']; 
       	 
      $total_valven+=$dt1['tb_venta_valven'];
      $total_igv+=$dt1['tb_venta_igv'];
      $total_ventas +=$dt1['tb_venta_tot'];
	  $reg = mostrarFechaHora($dt1['tb_venta_reg']);
 
      $total_pagar += $dt1['tb_venta_tot'];
      $dts2=$oVentapago->filtrar($dt1['tb_venta_id']);
      while($dt2 = mysqli_fetch_array($dts2))
      {
        $monto = $dt2['tb_ventapago_mon']; 
      
        $abonos=0;
        $mod_id=3;
        $est1=1;
        $est2=2;
        $dts3 = $oIngreso->mostrar_por_modulo($mod_id,$dt2['tb_ventapagoingresos_id'],$est1,$est2);
        while($dt3 = mysqli_fetch_array($dts3))
        {
          $abonos+= $dt3['tb_ingreso_imp'];
        } 
        mysqli_free_result($dts3);
        $saldo = ($monto - $abonos);
        $total_abono+=$abonos;
        $total_saldo+=$saldo;
      }
    
  /*   $filas=0;
    $rw1=$oVenta->mostrar_venta_detalle($dt1['tb_venta_id']);
    $filas= mysqli_num_rows($rw1);
    $detalle="";
    
    if(!$filas>0)
    {
      $detalle="Vacío";
    } */
	
	
	
	
	$numdocguia = '';
	$dtsguia = $oGuiaremision->verexistencia($dt1['tb_venta_id']);
	$numrowsguia = mysqli_num_rows($dtsguia);
	$guia = '';
	if($numrowsguia>0){
		
		while($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> <br /> ';
		} 
	
	}
	
	$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 .= '<a href="../../files/notacredito/notacredito_impcpe_doc.php?notid='.$idnc.'" target="_blank" class="badge"
		style="background-color:#00897b;color:#fff;" >'.$numdocnc.'</a> <br />';
	} 	
	}
	
	$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 .= '<a href="../../files/notadebito/notadebito_impcpe_doc.php?notid='.$idnd.'" target="_blank" class="badge"
		style="background-color:#1c2a48;color:#fff;" >'.$numdocnd.'</a> <br />';
	} 
	
	}
	
	
	
	 if($dt1['tb_documento_abr']=='B'){
		 
		if($ven_est==2){
		$col="bg-red";
		$title = ' title="Boleta anulada ::: NOTA DE CREDITO EMITIDA"';
	}else{
		$col="";
		$title ="";
	}
		 
	 }else if($dt1['tb_documento_abr']=='F'){
		 if($ven_est==2){
		$col="bg-red";
		$title = ' title="Factura anulada ::: NOTA DE CREDITO EMITIDA"';
	}else{
		$col="";
		$title ="";
	}
	 }else if($dt1['tb_documento_abr']=='T'){
		 if($ven_est==2){
		$col="bg-red";
		$title = ' title="Ticket anulado ::: DOCUMENTO INTERNO "';
	}else{
		$col="";
		$title ="";
	}
	 }
	/* $numdocguia = '';
	$dtsguia = $oGuiaremision->verexistencia($dt1['tb_venta_id']);
	$numrowsguia = mysqli_num_rows($dtsguia);
	
	if($numrowsguia>0){
		
		$dtg = mysqli_fetch_array($dtsguia);
		$numdocguia = $dtg['tb_guiaremision_numdoc'];
		
	}  */
	
	
	
	
	
    ?>
	
	
	
    <tr class="<?php echo $col; ?>" <?php echo $title;  ?> style="cursor:pointer;" >
      
	  <td align="left" nowrap="nowrap">
	  
		<span class="btn btn-info btn-sm" title="VER INFORMACION DEL DOCUMENTO : <?php echo $dt1['tb_venta_numdoc'];?>" data-toggle="tooltip" data-placement="top"  onClick="venta_form_vista('editar','<?php echo $dt1['tb_venta_id']?>')"><i class="fas fa-edit"></i></span> 
    
		<?php 
        if($dt1['tb_venta_est']!=2){?>
       <!-- <button  title="Anular" class="btn btn-primary" style="padding:2px 5px;margin:0px 2px" onClick="venta_anular('<?php echo $dt1['tb_venta_id']?>','<?php echo $dt1['tb_documento_abr'].' '.$dt1['tb_venta_numdoc']?>')"><i class="fa fa-trash"></i></button> 
		
		
		 <a href="javascript:void(0)" class="btn btn-danger btn-sm glyphicon glyphicon-trash" onclick="venta_anular('<?php echo $dt1['tb_venta_id'] ?>','<?php echo $dt1['tb_venta_numdoc'];?>')" target="new" title="CLICK PARA ANULAR LA VENTA"></a>		
		-->	
		  
		<?php }?>
		
		
		<?php 
		
        if($dt1['tb_cliente_id']!='1'){}
		
		if($dt1['tb_venta_est']!=2){
			
        if($dt1['tb_documento_abr']!='T'){
			
			
			 	
			if($dt1['tb_venta_estsun']==1){
				
				 if($dt1['tb_venta_est']!=2)
				 {
				?>
				<a class="btn btn-danger btn-sm" href="?app=notacredito" target="_blank" title="ANULAR DOCUMENTO CON UNA NOTA DE CREDITO : <?php echo $dt1['tb_venta_numdoc'];?>" data-toggle="tooltip" data-placement="top"  ><i class="fas fa-trash"></i></a>
			
				<!--
				<a href="?app=notacredito" class="btn btn-warning btn-sm glyphicon glyphicon-trash" target="_blank" title="Debera emitir una nota de credito para anular la venta.  CLICK PARA EMITIR NOTA DE CREDITO"></a>
				-->
				<?php 							
				} 
				?>
				<a title="VER DESCARGAR Y/O IMPRIMIR PDF" data-toggle="tooltip" data-placement="top"    href="../../files/venta/venta_impcpe_doc.php?venid=<?php echo $dt1['tb_venta_id']; ?>" target="_blank" class="btn btn-sm" style="background-color:#00897b;color:#fff;" title="DESCARGAR E IMPRIMIR PDF"><i class="fas fa-file-pdf"></i></a>		 
				<span title="ENVIAR CORREO" data-toggle="tooltip" data-placement="top"  class="btn btn-info btn-sm" onclick="enviarCorreo('<?php echo $dt1['tb_venta_id']; ?>','<?php echo $dt1['tb_cliente_id']; ?>');" style="" title="ENVIAR CORREO"><i class="fas fa-envelope-square "></i></span>
				
				<span title="GENERAR GUIA DE REMISION ELECTRONICA" data-toggle="tooltip" data-placement="top"  class="btn btn-sm" style="background-color:#3F729B;color:#fff;" onclick="ventaguia_form('insertar','<?php echo $dt1['tb_venta_id'] ?>')" ><i class="fas fa-book-open"></i></span>
			
			
			<?php 
			
			if($numrowsguia>0){
			?>
			<?php	
			}else{
			?>
			<?php 
			}
			
			
			}else if($dt1['tb_venta_estsun']==0){				
				?>
						
				<a title="VER DESCARGAR Y/O IMPRIMIR PDF" data-toggle="tooltip" data-placement="top" href="../../files/venta/venta_impcpe_doc.php?venid=<?php echo $dt1['tb_venta_id']; ?>" target="_blank" class="btn btn-sm" style="background-color:#00897b;color:#fff;" title="DESCARGAR E IMPRIMIR PDF"><i class="fas fa-file-pdf"></i></a>		  
				 <a title="EMITIR DOCUMENTO :: ENVIAR A SUNAT :: IMPRIMIR" data-toggle="tooltip" data-placement="top" href="javascript:void(0)" class="btn btn-success btn-sm" onClick="venta_ticket_datos('<?php echo $dt1['tb_venta_id'] ?>');" ><i class="fas fa-angle-double-right"></i></a>		 
				
						<?php if($_SESSION['usuariogrupo_id']==1){?>
				<span title="ACTUALIZAR ESTADO DE SUNAT" data-toggle="tooltip" data-placement="top" class="btn btn-sm btn-primary" onClick="actualizarestado('<?php echo $dt1['tb_venta_id']; ?>','venta');" ><i class="fas fa-retweet"></i></span>	
		
				 <?php 
				}
				}else if($dt1['tb_venta_estsun']==2){		
				
				?>
				<a title="VER DESCARGAR Y/O IMPRIMIR PDF" data-toggle="tooltip" data-placement="top" href="../../files/venta/venta_impcpe_doc.php?venid=<?php echo $dt1['tb_venta_id']; ?>" target="_blank" class="btn btn-sm" style="background-color:#00897b;color:#fff;" title="DESCARGAR E IMPRIMIR PDF"><i class="fas fa-file-pdf"></i></a>		  
				
				<?php
				
				
				}
				
				
			}else{
			if($dt1['tb_venta_est']!=2){
		?>
			<!--
			<a href="javascript:void(0)" class="btn btn-warning btn-sm glyphicon glyphicon-trash" onclick="venta_anular('<?php echo $dt1['tb_venta_id'] ?>','<?php echo $dt1['tb_venta_numdoc'];?>')" target="new" title="CLICK PARA ANULAR EL TICKET INTERNO"></a>
			 -->
			
			 <?php if($dt1['tb_venta_estsun']==0){
			 if($dt1['tb_venta_rel']>0){
											
			 }else{
			 ?>
			 <span class="btn btn-success btn-sm" title="CANJEAR TICKET" data-toggle="tooltip" data-placement="top" onclick="ventafact_form('<?php echo $dt1['tb_venta_id'] ?>')" ><i class="fas fa-random"></i></span>
			<?php 
			 }
			 }
			 
			 }else{
				 ?>
				
				 <?php 
			 }
			?>
		<a href="../../files/venta/venta_impcpe_doc.php?venid=<?php echo $dt1['tb_venta_id']; ?>" target="_blank" class="btn btn-sm" style="background-color:#00897b;color:#fff;" title="VER DESCARGAR Y/O IMPRIMIR PDF" data-toggle="tooltip" data-placement="top" ><i class="fas fa-file-pdf"></i></a>		 
		<?php
			 
		}
		}else{
			
			
			 if($dt1['tb_documento_abr']!='T'){	
			 if($dt1['tb_venta_estsun']==1){
				?>
						<a href="../../files/venta/venta_impcpe_doc.php?venid=<?php echo $dt1['tb_venta_id']; ?>" target="_blank" class="btn btn-sm" style="background-color:#00897b;color:#fff;" title="VER DESCARGAR Y/O IMPRIMIR PDF" data-toggle="tooltip" data-placement="top" ><i class="fas fa-file-pdf"></i></a>		 
		
				<?php 
			 }	}		
		}
				
		?>
		
		
		<?php  if($dt1['tb_documento_abr']!='T'){	
		?>
			
		<span class="btn btn-sm btn-warning" title="IMPRIMIR FORMATO TICKET" data-toggle="tooltip" data-placement="top" onclick="traer_datos_doc('<?php echo $dt1['tb_venta_id']?>','<?php echo $dt1['tb_venta_xmlurl'];?>')"><i class="fa fa-print"></i></span>
	  
		<?php 
		}else{
			if($dt1['tb_venta_est']==1){
		?>
		<span class="btn btn-danger btn-sm " title="ANULAR DOCUMENTO INTERNO" data-toggle="tooltip" data-placement="top" onclick="venta_anular('<?php echo $dt1['tb_venta_id'] ?>','<?php echo $dt1['tb_venta_numdoc'];?>')" style="cursor:pointer" ><i class="fas fa-trash"></i></span>		
		<?php 			
			}else{
			?>
			<?php			
			}
		?>		
		<span class="btn btn-sm btn-warning" title="IMPRIMIR FORMATO TICKET" data-toggle="tooltip" data-placement="top" onclick="venta_ticket_datos_tabla('<?php echo $dt1['tb_venta_id']?>')"><i class="fas fa-print"></i></span>
	  
		<?php 
		}?>	
		
	  
      </td>	  
	  <td>
	  <?php
	   if($dt1['tb_documento_abr']!='T'){	
		if($dt1['tb_venta_estsun']==0){
			echo '<badge class="badge badge-warning">NO CDR : ENVIAR DE NUEVO</badge>';
		}else if($dt1['tb_venta_estsun']==2){
			echo '<badge class="badge" style="color:#fff;background-color:#F93154">RECHAZADA POR SUNAT</badge>';
		}else{
				echo '<badge class="badge" style="color:#fff;background-color:#0d47a1">ACEPTADA</badge>';
		 }		 
		 }else{
			 if($dt1['tb_venta_rel']>0){
			echo '<badge class="badge" style="color:#fff;background-color:#007E33">CANJEADA</badge>';
			}	
		 }
		
	//echo ' <br /> <badge class="badge" style="color:#fff;background-color:#FF8800">'.$numdocguia.'</badge>';
		 
	  ?>
	  </td>
	  <td>
	  <?php 
	  
		echo $guia;
		echo $nc;
		echo $nd;
		 
	  ?>
	  </td>
	  <td nowrap="nowrap" align="left" ><?php echo $dt1['tb_documento_abr'].' '.$dt1['tb_venta_numdoc']?></td>
      <td class="" title="FECHA DE REGISTRO : <?php echo "$reg";  ?> " data-toggle="tooltip" data-placement="top" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_venta_fec'])?></td>
      <td class="" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_venta_ven'])?></td>
	  <td nowrap align="left" ><?php echo $dt1['tb_cliente_nom'];?></td>
      <td class="" align="right" ><?php echo $dt1['tb_cliente_doc'];?></td>
      <td align="right"><?php echo formato_money($dt1['tb_venta_tot'])?></td>
      <td class="" align="right"><?php echo formato_money($abonos);?></td>
      <td class="" align="right"><?php echo formato_money($saldo);?></td>
	</tr>
    <?php
  }
  mysqli_free_result($dts1);
  }
  ?> 
			
</tbody> 
<tr class="even">
			<td colspan="">TOTAL</td>
			<td class="" colspan=""></td>
			<td colspan=""></td>
			<td class="" colspan=""></td>
			<td class="" colspan=""></td>
			<td class="" colspan=""></td>
			<td class="" colspan=""></td>
			<td class="" colspan=""></td>
			<td align="right"><strong><?php echo formato_money($total_pagar)?></strong></td>
			<td class="" align="right"><strong><?php echo formato_money($total_abono)?></strong></td>
			<td class="" align="right"><strong><?php echo formato_money($total_saldo)?></strong></td>
			
			</tr>
			
        </table>
		 
		
		
			
      </div>
      </div>
      </div>
    </div>
  </div>
</div>
</section>

 <script type="text/javascript">
  $(function() {  
	 $('#tabla_venta').DataTable({
 
        "language": {
                "lengthMenu": "Mostrar _MENU_ registros",
                "zeroRecords": "No se encontraron resultados",
                "info": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
                "infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
                "infoFiltered": "(filtrado de un total de _MAX_ registros)",
                "sSearch": "Buscar:",
                "oPaginate": {
                    "sFirst": "Primero",
                    "sLast":"Último",
                    "sNext":"Siguiente",
                    "sPrevious": "Anterior"
			     },
			     "sProcessing":"Procesando...",
            }
    });  
	
	$('.table-responsive').doubleScroll();
	
  }); 
</script>  