<?php
$r="../";
$idcliente=$_GET['cliente'];
require_once($r."inc/funciones.php");
validNotSession($r);
$mysqli = connectDB($r);

$row1 = $mysqli->query("SELECT * FROM z_pedidos WHERE idcliente = '$idcliente' AND pago < 2 AND estado IN (1,2,3,6)");

$arraycodigopedidos = array();
$arrayadicional= array();
$arrayabono= array();
$arraycliente=array();
while($row2 = $row1->fetch_assoc()) {
    array_push($arraycodigopedidos,$row2['codigopedido']);
    array_push($arrayadicional,$row2['adicional']);
    array_push($arrayabono,$row2['abono']);
    array_push($arraycliente,$row2['idcliente']);
}

?>
<!DOCTYPE html>
<html lang="en">
  <?php include $r."inc/head.php"; ?>
  <style>
      .container-fluid{padding-right:5px;padding-left:5px;}
  </style>
<body class="fixed-nav sticky-footer sidenav-toggled bg-dark" id="page-top">
  <?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 POR COBRAR </div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" width="100%" cellspacing="0">
              
              <thead>
                <tr>
                  <th>Codigo de pedido</th>
                  <th>Cliente</th>
                  <th>Abono</th>
                  <th>Adicional</th>
                  <th>Valor</th>
                  <th>Total a Pagar</th>
                  <th id="select" width="150px"><input id="checkall" type="checkbox" class="form-control" onclick="checkall()" value="0"></th>
                </tr>
              </thead>
              <tfoot>
                <tr>
                  <th colspan="5" rowspan="2"><textarea form="formulario" class="form-control" name="nota" placeholder="Nota adicional"></textarea></th>
                  <th>Valor Abonado</th>
                  <th class="text-right">
                      <input form="formulario" type="number" id="abono" name="abono" class="form-control" onkeyup="sumar()" required>
                  </th>
                </tr>
                <tr>
                    <th>Medio de Pago</th>
                    <th>
                        <select form="formulario" class="form-control" name="mpago" type="text" placeholder="mpago" required>
                            <option seledted disabled>Seleccione</option>
                            <? $sql1 = $mysqli->query("SELECT * FROM `z_pagos_medios`"); while ($row1 = $sql1->fetch_assoc()){ ?><option value="<?= $row1['id']; ?>"><?= $row1['medio']; ?></option><? } ?>
                        </select>
                    </th>
                </tr>
                <tr>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>Suma Selecionados</th>
                  <th class="text-right">$ <span id="TOTAL">0</span></th>
                </tr>
                <tr>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>&nbsp;</th>
                  <th>Saldo por Cobrar</th>
                  <th id="tt2" class="text-right">$ <span id="TOTAL2">0</span></th>
                </tr>
              </tfoot>
              <tbody>
                  <form id="formulario" action="acciones.php" method="POST">
                      <input type="hidden" name="pagos" value="si">
                <?
                  foreach($arraycodigopedidos as $key => $item){
                    $pedido = $mysqli->query("SELECT * FROM z_pedido WHERE codigopedido='$item'");
                    while($pedid = $pedido->fetch_assoc()){
                        $cantidad=$pedid['cantidad'];
                        //$devolucion=$pedid['devolucion'];
                        $precio=$pedid['precio'];
                        $descuento=100-$pedid['descuento'];
                        $cantidad=$cantidad-$devolucion;
                        $subtotal=$precio*$cantidad/100*$descuento;
                        $total=$total+$subtotal;
                    } ?><tr>
                        <td><a id="verpedido" class="text-primary" data-toggle="modal" data-target="#<?php echo $item ?>"><?echo $item;?></a></td>
                        <td><? $cliente=sclien($arraycliente[$key]);?><? echo $cliente['nombre'];?></td>
                        <td><a id="verpedido" class="text-primary" data-toggle="modal" data-target="#a<?php echo $item ?>"><?= moneda($arrayabono[$key]); $abono = $arrayabono[$key]; ?></a></td>
                        <td><? $adi=$arrayadicional[$key];echo moneda($arrayadicional[$key]); ?></td>
                        <td class="text-danger h6 text-right"><?= moneda($total); $t = (float)$total + (float)$adi - (float)$abono; ?></td>
                        <td class="text-danger h6 text-right"><?= moneda($t); ?><? $total2=$total2+$t; $total=0; ?></td>
                        <td><input type="checkbox" name="to[<?= $item ?>]" value="<?= $t; ?>" onclick="sumar()"/></td>
                    </tr>
                    
                 
                <?}?>
              </tbody>
                <tfoot>
                    <tr>
                  <th colspan="4"></th>
                    <td class="text-right h6"><b>SUMA TOTAL</b></td>
                    <td class="text-danger h6 text-right"><b>$ <? echo number_format($total2); ?> COP</b></td>
                  <th></th>
                    </tr>
                </tfoot>
            </table>
          </div>
        </div>
        <span id="abo"></span>
                </form>
      </div>
              <div class="text-center">
                  <a href="javascript:history.back(1)" class="btn btn-primary">ATRAS</a>
                    <button form="formulario" type="submit" class="btn btn-success">COBRAR</button>
                </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">Detalle del Pedido<span id="code"> <?php echo $item ?></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_abonos WHERE codigopedido=$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()){$users=suser($ped['iduser']);
        $rmodal.='<tr><td>'.$users['nombre'].'</a><br/></td>';
        $rmodal.='<td>'.$ped['abono'].'</a><br/></td>';
        $rmodal.='<td>'.$ped['fecha'].'<br/></td></tr>';
    }
        $rmodal.='<tr><td><br/></td>';
        $rmodal.='<td>Direccion<br/></td>';
        $rmodal.='<td class="text-danger"><b>'.$cliente['direccion'].'</b><br/></td></tr>';
    $rmodal.='</tbody>
        </table>';  echo $rmodal;$rmodal=""; ?>
      </div>
    </div>
  </div>
</div><? } ?>
<?  foreach($arraycodigopedidos as $key => $item){?>
   <!-- Modal -->
<div class="modal fade" id="<?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">Detalle del Pedido<span id="code"> <?php echo $item ?></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_pedido WHERE codigopedido=$item");
    $rmodal.='<table style="width:100%" class="table">
          <thead>
            <tr>
              <th>Nombre Producto</th>
              <th>Cantidad</th>
            </tr>
          </thead>
          <tbody>';
    while ($ped = $pedidos->fetch_assoc()){
        $producto=sprod($ped['idproducto']);
        
        $rmodal.='<tr><td>'.$producto['producto'].'</a><br/></td>';
        $rmodal.='<td>'.$ped['cantidad'].'<br/></td></tr>';
    }
        $rmodal.='<tr><td>Direccion<br/></td>';
        $rmodal.='<td class="text-danger"><b>'.$cliente['direccion'].'</b><br/></td></tr>';
    $rmodal.='</tbody>
        </table>';  echo $rmodal;$rmodal=""; ?>
      </div>
    </div>
  </div>
</div><? } ?>
  <?php include $r."inc/foot.php"; ?>
  <script type="text/javascript">
function sumar(){
        var abono = $("#abono").val();
        if(abono==''){}
        var total=0;
        var total2=0;
        $("input:checkbox:checked").each(function() {
             var valor=$(this).val();
             total+=parseInt(valor);
             total2= total-parseInt(abono);
        });
        $('#TOTAL').html(total).slideDown(400);
        $('#TOTAL2').html(total2).slideDown(400);
        $('#abo').html('<input form="formulario" type="hidden" name="abonos" value="'+total2+'"/>').slideDown(400);
}

function checkall(){
    var option = $('#checkall').is(':checked');
    $("input[type=checkbox]").each(function() {
         $(this).prop('checked',option);
    });
    sumar();
}
</script>
</body>
</html>    
    
 