<?php $r="../";
require_once($r."inc/funciones.php");
validNotSession($r);

$spe=$_SESSION['perfil'];
if($spe!=$pad && $spe!=$psu && $spe!=$pbo){ echo '<div class="alert alert-danger  alert-dismissible text-center"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><h6>NO TIENES PERMISO PARA ESTA SECCION</h6></div>';exit();}
$mysqli = connectDB($r);
$arraycodigopedidos = array();
$row1 = $mysqli->query("SELECT * FROM z_dproveedor WHERE estado!=2");
?>
<!DOCTYPE html>
<html lang="es">
  <?php include $r."inc/head.php"; ?>
<body class="fixed-nav sidenav-toggled sticky-footer bg-dark" id="page-top">
  <div class="container">
    <div id="mensaje" style="position:fixed;width:90%;padding-left:10%;top:20px;left:0px;margin:0 auto;z-index:9999;"></div>
  </div>
  <?php include $r."inc/nav.php"; ?>
  
  <div class="content-wrapper">
    <div class="container-fluid">
      <div class="card mb-3">
        <div class="h3 card-header">
          <i class="fa fa-table"></i>  PEDIDOS PENDIENTES POR COBRAR</div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>Codigo Factura</th>
                  <th>Cliente</th>
                  <th>fecha</th>
                  <th>Abonos</th>
                  <th>Valor Inversion</th>
                  <th>Saldo Inversion</th>
                  <th></th>
                </tr>
              </thead>
              <tbody>
                    <?
                  while($row2 = $row1->fetch_assoc()) {
                    $item=$row2['id'];
                    $codigo=$row2['codigofactura'];
                    array_push($arraycodigopedidos,$item);  
                    $fecha=$row2['fecha'];
    $total=$row2['valor'];
    $abono=$row2['abono'];
    $proveedor=$row2['idrpoveedor']; ?><tr>
                        <td><?echo $codigo;?></td>
                        <td><? $proveedor=sprove($row2[idproveedor]);?><a id="verpedido" class="text-primary" data-toggle="modal" data-target="#<?php echo $item ?>"><? echo $proveedor['nombre'];?></a></td>
                        
                        <td><?echo $fecha;?></td>
                        <td><a id="verpedido" class="text-primary" data-toggle="modal" data-target="#a<?php echo $item ?>"><? echo $abono;?></a></td>
                        <td class="text-danger h6 text-right">$ <? echo number_format($total); ?> COP</td>
                        <td class="text-danger h6 text-right">$ <? echo number_format($total-$abono);$t=$total-$abono ?> COP<? $total2=$total2+$t; $total=0; ?></td>
                        <td width="30px"><a title="clic aquí para cobrar el pedido" class="btn_default pagado h3 p-0 m-0" href="acciones.php?pago=<? echo $t; ?>&code=<?php echo $row2["id"] ?>"><i class="fa fa-money text-success"></i></a></td>
                        
                    </tr>
                    <? } ?>
                    
              <tfoot>
                <tr>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th>Total Por Pagar</th>
                  <th class="text-danger h6 text-right"><b>$ <?php echo number_format($total2); ?> COP</b></th>
                  <th></th>
                </tr>
              </tfoot>
              <tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <?  foreach($arraycodigopedidos as $key => $item){?>
   <!-- Modal -->
<div class="modal fade" id="a<?php echo $item ?>" tabindex="-1" role="dialog" aria-labelledby="verpedidoLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="verpedidoLabel">Historico abonos <span id="code"></span></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div id="verpedido">
          
          <?  $pedidos = $mysqli->query("SELECT * FROM z_aproveedores WHERE idcodigo=$item");
    $rmodal.='<table style="width:100%" class="table">
          <thead>
            <tr>
              <th>USUARIO</th>
              <th>ABONO</th>
              <th>FECHA</th>
            </tr>
          </thead>
          <tbody>';
    while ($ped = $pedidos->fetch_assoc()){ $usuario=suser($ped[iduser]);
        $rmodal.='<tr><td>'.$usuario['nombre'].'</a><br/></td>';
        $rmodal.='<td>'.$ped['abono'].'</a><br/></td>';
        $rmodal.='<td>'.$ped['fecha'].'<br/></td></tr>';
    }
        
    $rmodal.='</tbody>
        </table>';  echo $rmodal;$rmodal=""; ?>
      </div>
    </div>
  </div>
</div><? } ?>
  <?php include $r."inc/foot.php"; ?>
</body>
<script>
$('#dataTable').DataTable( {
        "order": [[ 2, "desc" ]],
        "lengthMenu": [[25, 50, 100, 500, -1], [25, 50, 100, 500, "All"]]
    } );
  </script>
</html>    