<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("cDevolucionproveedor.php");
$oDevolucionproveedor = new cDevolucionproveedor();
require_once ("../compra/cComprapago.php");
$oComprapago = new cComprapago();
require_once ("../egreso/cEgreso.php");
$oEgreso = new cEgreso();
require_once ("../formatos/formato.php");
$dts1=$oDevolucionproveedor->mostrar_filtro2($_POST['cmb_dev_emp_id'],$_POST['hdd_fil_pro_id'],$_POST['txt_dev_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_compra" class="table">
            <thead>
              <tr>
                <th class="" ></th>
                <th class="" >FECHA</th>
                <th class="" >PERIODO</th>
                <th>DOCUMENTO</th>                    
                <th>PROVEEDOR</th>
                <th class="" >EMPRESA</th>
                <th NOWRAP class="" align="right">TOTAL</th>
                <th NOWRAP class="" align="right">PAGOS</th>
                <th NOWRAP class="" align="right">SALDO</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;
       $saldoporcompra = 0;
	   $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;
				  
			}
				
			
		$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="badge badge-primary" style="cursor:pointer;" >PARA CANJE</small>';
			 }else if($dt1['tb_compra_tippro']=='2'){
				 $lbltippro = '<small class="badge badge-danger" style="cursor:pointer;" >PAGO NORMAL</small>';
			 }
					
$reg = mostrarFechaHora($dt1['tb_compra_reg']);					
	  
      ?>
    <tr title="FECHA DE REGISTRO : <?php echo "$reg";  ?>" >
        <td class="" nowrap="nowrap"> 
		<span class="btn btn-primary" style="cursor:pointer" onclick="ver_detalles(<?php echo $dt1['tb_compra_id']?>);" ><i class="fas fa-list"></i></span>
		</td>
        <td class="" 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 NOWRAP class="" ><?php echo $dt1['tb_empresa_nomcom']?></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($abonos);?></td>
        <td NOWRAP class="" align="right"><?php echo $monsim.' '.formato_money($saldoporcompra);?></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() {  
  
  agregar_producto('restablecer');
  
    $("#tabla_compra").DataTable( {
      paging: true,
	  searching: false,
	  ordering:false,
	  "columnDefs": [
        { "orderable": true, "targets": [0] }
      ],
    });
  }); 
</script>  