<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("cCompra.php");
$oCompra = new cCompra();
require_once ("../compra/cComprapago.php");
$oComprapago = new cComprapago();
require_once ("../egreso/cEgreso.php");
$oEgreso = new cEgreso();


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

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

$fecha1 = $_POST['txt_fil_com_fec1']; 
$fecha2 = $_POST['txt_fil_com_fec2'];

$dts1=$oCompra->mostrar_filtro(fecha_mysql($fecha1),fecha_mysql($fecha2),$_POST['cmb_fil_mon_id'],$_POST['hdd_fil_pro_id'],$_POST['cmb_fil_com_est'],
$_POST['cmb_fil_empresa_id'],$_POST['cmb_fil_punven_id'],$_POST['cmb_fil_piso_id'],$_POST['cmb_fil_per_mes'],$_POST['cmb_fil_per_anio'],
$_POST['cmb_campo_order'],$_POST['cmb_opc_order'],$_POST['txt_fil_numdoc'],$_POST['cmb_fil_tipdoc_id']);
$num_rows= mysqli_num_rows($dts1);




if($_POST['txt_fil_numdoc']!="" || $fecha1 != $fecha2 || $_POST['cmb_fil_doc_id']!="" || $_POST['hdd_fil_pro_id'] !="" || $_POST['cmb_fil_per_mes'] !="" || $_POST['cmb_fil_per_anio'] != "" ){
		
		$user_agen = $_SERVER['HTTP_USER_AGENT'];;
		$mod = 4;
		$mod_ide = 0;
		$det = 'MOSTRANDO COMPRAS SEGUN ['.$fecha1.' | '.$fecha2.' | '.$_POST['cmb_fil_doc_id'].' | '.
		$_POST['txt_fil_numdoc'].' | '.$_POST['txt_fil_pro'].']';
		$fec = date('Y-m-d');
		$doc_id = '';
		$numdoc = '';
		$ip = mostrar_ip();
	 	
		$oHistorial->insertar(
		fecha_mysql($fec),
		$det,
		$ip,
		$_SESSION['usuario_id'],
		$mod,
		$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_compra" class="table table-bordered table-striped" >
            <thead>
              <tr>
                <th class="" >FECHA</th>
                <th class="" >PERIODO</th>
                <th>DOCUMENTO</th>                    
                <th>PROVEEDOR</th>
              <!--  <th class="" >RUC/DNI</th>
                -->
                <th class="" >EMPRESA</th>
               <!-- <th class="" >TIPO PROCESO</th>
                -->
                <th NOWRAP class="" align="right">TOTAL</th>
                <th NOWRAP class="" align="right">PAGOS</th>
                <th NOWRAP class="" align="right">SALDO</th>
                <th>&nbsp;</th>
              </tr>
            </thead>
            <tbody>
      <?php
       if($num_rows>0){
      while($dt1 = mysqli_fetch_array($dts1))
      {

       $total_valven+=$dt1['tb_compra_valven'];
       $total_igv+=$dt1['tb_compra_igv'];
       $total_per+=$dt1['tb_compra_per'];
       $total_compras+=$dt1['tb_compra_tot'];

	   
			if($dt1['tb_moneda_id']==1){
				$monsim = '<span class="text-primary">'.$dt1['tb_moneda_sim'].'</span>';
				$total_pagar_sol += $dt1['tb_compra_tot'];
	   
			}else if($dt1['tb_moneda_id']==2){
				$monsim = '<span class="text-green">'.$dt1['tb_moneda_sim'].'</span>';
				$total_pagar_dol += $dt1['tb_compra_tot'];
			}

	   $saldo=0;
	   $ver = 0;
       $abonoporcompra = 0;
       $saldoporcompra = 0;
	   $canje = 1;
	   
	   $dts2=$oComprapago->filtrar($dt1['tb_compra_id']);
       while($dt2 = mysqli_fetch_array($dts2))
       {
		 		   
		if($dt2['tb_comprapago_est']=='1' || $dt2['tb_comprapago_est']=='3'){
			//$ver = 1;
		}
		
        $monto = $dt2['tb_comprapago_mon']; 
        $abonos=0;
		
        $mod_id=4;
        $est=1;
        $dts3 = $oEgreso->mostrar_por_modulo($mod_id,$dt2['tb_comprapago_id'],$est);
        while($dt3 = mysqli_fetch_array($dts3))
        {
          $abonos+= $dt3['tb_egreso_imp'];		
		} 
        mysqli_free_result($dts3);
		
        $saldo = ($monto - $abonos);

			if($dt1['tb_moneda_id']==1){
				 $total_abono_sol+=$abonos;
				 $total_saldo_sol+=$saldo;
				 
				
	   
			}else if($dt1['tb_moneda_id']==2){
				 $total_abono_dol+=$abonos;
                 $total_saldo_dol+=$saldo;
				  
			}
				
			
		$abonoporcompra += $abonos;	
		$saldoporcompra += $saldo;
      }
	  
		
      $i = $dt1['tb_compra_permes'];
    	                                                if($i=='01'){
															$lblmes = 'Enero';
														}else if($i=='02'){
															$lblmes = 'Febrero';
														}else if($i=='03'){
															$lblmes = 'Marzo';
														}else if($i=='04'){
															$lblmes = 'Abril';
														}else if($i=='05'){
															$lblmes = 'Mayo';
														}else if($i=='06'){
															$lblmes = 'Junio';
														}else if($i=='07'){
															$lblmes = 'Julio';
														}else if($i=='08'){
															$lblmes = 'Agosto';
														}else if($i=='09'){
															$lblmes = 'Septiembre';
														}else if($i=='10'){
															$lblmes = 'Octubre';
														}else if($i=='11'){
															$lblmes = 'Noviembre';
														}else if($i=='12'){
															$lblmes = 'Diciembre';
														}
														
			 if($dt1['tb_compra_tippro']=='1'){
				 $lbltippro = '<small class="label label-primary" style="cursor:pointer;" >PARA CANJE</small>';
			 }else if($dt1['tb_compra_tippro']=='2'){
				 $lbltippro = '<small class="label label-danger" style="cursor:pointer;" >PAGO NORMAL</small>';
			 }
					
$reg = mostrarFechaHora($dt1['tb_compra_reg']);					
	  
      ?>
      <tr style="font-weight:bold" >
        <td class="" data-toggle="tooltip" data-placement="top" title="FECHA DE REGISTRO : <?php echo "$reg";  ?>" style="cursor:pointer;" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_compra_fec'])?></td>
        <td class="" nowrap="nowrap"><?php echo $dt1['tb_compra_peranio'].' | '. $lblmes ?></td>
        <td><?php echo $dt1['tb_documento_abr'].' - '.$dt1['tb_compra_numdoc']?></td>
        <td NOWRAP ><?php echo $dt1['tb_proveedor_nom']?></td>
       <!--
        <td class="" ><?php echo $dt1['tb_proveedor_doc']?></td>
       -->
        <td NOWRAP class="" ><?php echo $dt1['tb_empresa_nomcom']?></td>
       <!-- <td class="" ><?php echo $lbltippro; ?></td>
        -->
        <td NOWRAP align="right"><?php echo $monsim.' '. formato_money($dt1['tb_compra_tot'])?></td>
        <td NOWRAP class="" align="right"><?php echo $monsim.' '.formato_money($abonoporcompra);?></td>
        <td NOWRAP class="" align="right"><?php echo $monsim.' '.formato_money($saldoporcompra);?></td>
        <td align="left" nowrap="nowrap">
	<!--	
		 <span class="btn btn-sm btn-warning" style="cursor:pointer" onClick="compra_anular('<?php echo $dt1['tb_compra_id']?>')"><i class="fa fa-trash"></i></span>
	 -->
		
        <?php if($dt1['tb_compra_est']!=0){
		
		if($dt1['tb_documento_abr']!='L'){
		?>
		  
		  
        <button class="btn btn-sm btn-info" title="VER INFORMACION DE LA COMPRA"  data-toggle="tooltip" data-placement="top" onClick="compra_form('editar','<?php echo $dt1['tb_compra_id']?>','<?php echo $dt1['saldo'] ?>')"><i class="fas fa-edit"></i></button>
    
<?php 
		}
if ($_SESSION['usuariogrupo_id']=='2' || $_SESSION['usuariogrupo_id']=='1' ){
if($ver==0){
	?>
	   <button class="btn btn-sm btn-danger" style="cursor:pointer" title="ANULAR REGISTRO DE COMPRA - RESTABLECER KARDEX STOCKS Y EGRESOS"  data-toggle="tooltip" data-placement="top" onClick="compra_anular('<?php echo $dt1['tb_compra_id']?>')"><i class="fa fa-trash"></i></button>
	<?php 
}else{?>	
		
<?php
}
}


if ($_SESSION['usuariogrupo_id']=='2' || $_SESSION['usuariogrupo_id']=='1' ){
?>
  <button class="btn btn-sm btn-primary" title="SUBIR Y VER DOCUMENTOS RELACIONADOS" data-toggle="tooltip" data-placement="top" onClick="ver_archivos('<?php echo $dt1['tb_compra_id']?>','<?php echo $dt1['tb_compra_numdoc'] ?>')"><i class="far fa-bookmark"></i></button>
	
<?php 	  	  
}else {

}

}?>
        </td>
      </tr>
      <?php
    }
    mysqli_free_result($dts1);
    }
    ?>
  </tbody>
  <tr class="even ">
  <td colspan="5">TOTAL SOLES</td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_pagar_sol)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_abono_sol)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_saldo_sol)?></strong></td>
  <td>&nbsp;</td>
</tr>

 <tr class="even ">
  <td colspan="5">TOTAL DOLARES</td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-green">US$</span>'.' '.formato_money($total_pagar_dol)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-green">US$</span>'.' '.formato_money($total_abono_dol)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-green">US$</span>'.' '.formato_money($total_saldo_dol)?></strong></td>
  <td>&nbsp;</td>
</tr>


            </table>
    </div>
       
 </div>
      </div>
    </div>
  </div>
</div>
</section>
  
   <script type="text/javascript">
  $(function() {  

	 $('#tabla_compra').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>  