<?php
session_start();
require_once('conexion.php');
if (isset($_SESSION['usu_nombre']))
{
    require_once('include/sesiones.php');
    $regis=mysqli_query($con,"SELECT * from permiso where modulo_mod_id='14' and usuario_usu_id='$usu_id'") or die(mysqli_error($con));
    $reg=mysqli_fetch_array($regis);
    if($reg['per_visualizar']=="Activo")
      {          
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include ('head.php'); ?>
</head>
<body>
<?php
  $inicio="";
  $M_inventario="";
  $M_contactos="active";
  $M_compras="";
  $M_ventas="";
  $M_pedidos="";
  $M_pos="";
  $R_reportes="";

  $proveedores="active";
  $clientes="";
  $m_usuarios="";
  $m_distribuidor="";

  include ('menu.php');
?> 
<div class="main">
     <div class="col-md-2">        
        <?php include ('include/menu_contactos.php');?>
    </div> <!-- /span3 -->
    <div class="col-md-10">   
        <div class="widget stacked">
            <div class="widget-content">
                <div class="widget stacked">
                    <div class="widget stacked widget-table action-table">
                        <div class="widget-header">
                            <i class="icon-user"></i>
                            <h3><?php  
                            $pro_id=$_GET['pro_id'];
                            $registros=mysqli_query($con,"SELECT * FROM proveedor where pro_id='$pro_id' and empresa='$empresa'") or die(mysqli_error($con));
                            $reg=mysqli_fetch_array($registros);
                            echo $pro_nombre= $reg['pro_nombre'];
                            ?></h3>
                        </div> <!-- /widget-header -->          
                        <div class="widget-content">
                           <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th width="3%">ESTADO</th>
                                        <th width="10%">FACTURA Nº</th>
                                        <th width="17%">Nº FACTURA PROVEEDOR</th>
                                        <th width="15%">FECHA</th>
                                        <th width="15%">TOTAL</th>
                                        <th width="15%">ABONO</th>
                                        <th width="15%">SALDO</th>
                                        <th width="1%"></th>
                                    </tr>
                                </thead>
                                <tbody  class="buscar">
                                <?php 
                                $registros=mysqli_query($con,"SELECT * FROM compras where empresa='$empresa' AND  sucursal='$sucursal' and proveedor_pro_id='$pro_id' order by com_n_compra desc") or die(mysqli_error($con));
                                while ($reg=mysqli_fetch_array($registros))
                                {              
                                    $com_n_compra= $reg['com_n_compra'];
                                    $com_facturaproveedor= $reg['com_facturaproveedor'];
                                    $com_fecha= $reg['com_fecha'];
                                    $com_id=$reg['com_id'];

                                    $sql=mysqli_query($con,"SELECT sum(comd_precio*comd_cantidad) as base from compra_detalle where compras_com_id='$com_id' and empresa='$empresa'");
                                    $sql=mysqli_fetch_array($sql);
                                    $base=$sql['base'];

                                    $sql=mysqli_query($con,"SELECT sum(coab_abono) as total from compra_abonos where compras_com_id='$com_id' and empresa='$empresa'");
                                    $sql1=mysqli_fetch_array($sql);
                                    $total=$sql1['total'];
                                    $saldo=$base-$total;
                                ?>
                              <tr>
                                <td><?php
                                    if($reg['com_estado']=="Pagado")
                                        {
                                        echo $estados='<span class="badgeazul" title="Pagada"><i class="btn-icon-only icon-ok"></i></span>';
                                        }
                                        else 
                                        {
                                        echo $estados='<span class="badgerojo" title="Sin Pagar"><i class="btn-icon-only icon-remove"></i></span>';
                                        }
                                    ?>
                                </td>
                                <td><?php echo $com_n_compra; ?></td>
                                <td><?php echo $com_facturaproveedor; ?></td>
                                <td><?php echo $com_fecha; ?></td>
                                <td><?php echo number_format($base,0); ?></td>
                                <td><?php if($reg['com_estado']=="Pagado"){echo'';} 
                                 else{ echo number_format($total,0); }?></td>
                                <td><?php if($reg['com_estado']=="Pagado"){echo'';}
                                else{ echo number_format($saldo,0); }?></td>
                                <td>
                               <?php 
                                  $files = glob("img_facturas/$com_id.pdf");
                                  if(count($files) > 0) 
                                  { 
                                    foreach($files as $file) 
                                    { 
                                      echo "<a href='img_facturas/$com_id.pdf' target='_blank'  class='btn btn-warning btn-xs'><span class='btn-icon-only icon-print'></span></a>"; 
                                    } 
                                  } 
                                ?>
                                </td>
                            </tr>
                            <?php }?>
                            </tbody>
                        </table>
                    </div> <!-- /widget-content -->
                </div> <!-- /widget -->
            </div> <!-- /span6 -->
            <?php include ('include/pie_de_pagina.php'); ?>
        </div> <!-- /row -->
    </div> <!-- /container -->
</div> <!-- /main -->
<?php
    include ('php_caja/caja_abrir.php');
    include ('script.php');
?>
</body>
</html>
<?php
}
else
{
   echo '<script>alert("No tienes permiso para ver esta pagina")</script>';
   echo '<script> setTimeout("history.back(1)", 0); </script>';
}
}
else
{
    echo '<script>location.href = "index.php";</script>';
}
?>
