<?php $r="../";
$code=$_GET['code'];
require_once($r."inc/funciones.php");
validNotSession($r);
$titlepage="Aprobar Pedido";
$mysqli = connectDB($r);
$spe=$_SESSION['perfil'];
roleaccess(1410);
$new=$mysqli->query("SELECT * FROM z_pedidos WHERE codigopedido='$code' AND estado='0'");
$off = $new->num_rows;
if($off=0){exit(header("Location:aprobar-pedido.php?error=Este Pedido ya Fue Aprobado"));}
$row1 = $mysqli->query("SELECT * FROM z_pedido INNER JOIN z_pedidos ON z_pedido.codigopedido='$code' AND z_pedidos.codigopedido='$code'");
?>
<!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">
      <h3 class="text-center">APROBACION DEL PEDIDO No. <?= $code; ?></h3><hr/>
      <form id="formulario" class="p-3" method="post" action="acciones.php">
        <input type="hidden" name="aprobar" value="<?= $code; ?>">
        
        <div class="card-body">
          <div class="table-responsive">
            <table  class="table table-bordered" id="dataTables" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th width="50px">Codigo</th>
                  <th>Poducto</th>
                  <th width="150px">Cantidad</th>
                  <th width="150px">Precio UND</th>
                  <th width="200px">Total</th>
                </tr>
             </thead>
        <?php $i=1; while($row2 = $row1->fetch_assoc()) { $iva=$row2['iva']; $fuente=$row2['fuente']; $ica=$row2['ica']; ?>
        
        <!--<div class="form-group row border-bottom">-->
        <?php 
          
            $cliente=sclien($row2['idcliente']);
            $row3=sprod($row2['idproducto']);
            $precios=$row2["precio"];?>
            
            <tr>
               <td><?= $row3['codigo'] ?></td>
               <td><?= $row3['producto'] ?></td>
               <td><?= numodec($row2['cantidad']); $infopresent =sinfo('z_presentaciones_productos',$row2["idpresentacion"]); echo ' '.$infopresent['presentacion'];   ?></td>
               <td class="text-right"><input type="hidden" name="pre<?= $row2["idproducto"];?>" value="<?= $precios;?>"><span id="por<?php echo $row2["idproducto"]; ?>"><?= moneda($precios); ?></span></td>
               <td class="text-right"><span id="2por<?= $i; $i++ ?>"><? $total=$precios*$row2['cantidad']; echo moneda($total); ?></span> <?php $total2=$total2+$total; $iva=$total*$row3['iva']/100;  $totaliva=$totaliva+$iva; ?></td>
           </tr>
        </div>
       <? $cliente=$row2['idcliente'];$tipopedido=$row2['tipopedido'];$nota=$row2['nota'];$adicional=$row2['adicional']; $iduser=$row2['iduser']; $codigo=$row2['codigopedido'];$fecha=$row2['fechaentrega'];$cantidad=$cantidad+$row2['cantidad'];?>
        <?php } ?>
                <tr>
                  <th></th>
                  <th class="text-right"><b>TOTAL UND</b></th>
                  <th><?php echo '<b class="text-danger"> '.$cantidad.'</b>'; ?></th>
                  <th class="text-right"><b>BASE IVA</b></th>
                  <th class="text-right"><?php echo '<b class="text-primary"><span id="total3"> '.moneda(round($total2)).'</span></b>'; ?></th>
                </tr>
              
                <tr>
                  <th colspan="3">NOTA: <span class="text-warning"><?echo $nota; ?></span></th>
                  <th class="text-right"><b>IVA</b></th>
                  <th class="text-right"><?php  if($tipopedido=='R'){$totaliva=0; }echo moneda(round($totaliva));?></th>
                </tr>
                <tr>
                  <th colspan="3"></th>
                  <th class="text-right"><b>ADICIONALES</b></th>
                  <th class="text-right"><?= moneda(round($adicional)); ?></th>
                </tr>
                <tr>
                  <th colspan="3"></th>
                  <th class="text-right"><b>RETENCION</b></th>
                  <th class="text-right"><?= moneda(round($fuente,0,PHP_ROUND_HALF_UP)); ?></th>
                </tr>
                <tr>
                  <th colspan="3"></th>
                  <th class="text-right"><b>RTE ICA</b></th>
                  <th class="text-right">$ <?= number_format(round($ica,0,PHP_ROUND_HALF_UP)); ?> COP</th>
                </tr>
              <tfoot>
                <tr>
                  <th colspan="3"></th>
                  <th class="text-right"><b>TOTAL A PAGAR</b></th>
                  <th class="text-danger text-right">$ <?  echo number_format(round($total2+$totaliva+$adicional-$fuente-$ica)); ?> COP</th>
                </tr>
              </tfoot>
        </table>
            </div>
        </div>
        
        <div class="form-group row">
          <label for="rutero" class="col-sm-2 col-form-label text-right font-weight-bold">Domiciliario <span class="text-danger h5">*</span></label>
            <div class="col-sm-2">
            <? $row4 = $mysqli->query("SELECT * FROM z_ruteros WHERE eliminado=0 AND estado=0"); ?>
            <select name="rutero" class="form-control" required>
                <option disabled>Selecciones un Rutero</option>
                <?php $cliente=sclien($cliente);$user=suser($iduser); $idruta=$cliente['ruta'];
                $rutas=$mysqli->query("SELECT * FROM z_ruta WHERE id='$idruta' AND eliminado=0");
                $ruta = $rutas->fetch_assoc();
                $idrutero=$ruta['idrutero']; 
                ?>
              <?  while($row5 = $row4->fetch_assoc()) {?><option value="<?= $row5['id']; ?>" <? if($row5['id']==$idrutero){echo 'selected="selected"'; } ?>><?= $row5['nombre']; ?></option><? } ?>
            </select>
          </div>
          <label for="vendedor" class="col-sm-2 col-form-label text-right font-weight-bold">Vendedor <span class="text-danger h5">*</span></label>
            <div class="col-sm-2">
            <? $sqlvendedores = $mysqli->query("SELECT * FROM z_usuarios_vendedores WHERE eliminado=0"); ?>
            <select name="vendedor" class="form-control" required>
              <?  while($rowvendedores = $sqlvendedores->fetch_assoc()) {?><option value="<?= $rowvendedores['id']; ?>"><?= $rowvendedores['nombre']; ?></option><? } ?>
            </select>
          </div>
          <label for="pago" class="col-sm-2 col-form-label text-right font-weight-bold">Por Remesar <span class="text-danger h5">*</span></label>
            <div class="col-sm-2">
            <select name="remesa" class="form-control" required>
                <option>NO</option>
                <option>SI</option>
            </select>
          </div>
        </div>
        
        
        
        <div class="form-group row">
          <label for="nombre" class="col-sm-2 col-form-label text-right"><span class="font-weight-bold">Cliente :  </span></label>
          <label for="nombre" class="col-sm-2 form-control col-form-label"><?=  $cliente['nombre'];$zona=$cliente['zona']; ?></label>
          <label for="nombre" class="col-sm-2 col-form-label text-right"><span class="font-weight-bold">Ingresado Por :</span></label>
          <label for="nombre" class="col-sm-2 form-control col-form-label"><?=  $user['nombre']; ?></label>
          <label for="nombre" class="col-sm-2 col-form-label text-right"><span class="font-weight-bold">No. Pedido :  </span></label>
          <label for="nombre" class="col-sm-2 form-control col-form-label"><?=  $codigo; ?></label>
        </div>
        
        <div class="form-group row">
          <label for="pago" class="col-sm-2 col-form-label text-right font-weight-bold">Estado del Pedido <span class="text-danger h5">*</span></label>
            <div class="col-sm-4">
            <? $row4 = $mysqli->query("SELECT * FROM z_ruteros WHERE eliminado=0 AND estado=0"); ?>
            <select onchange="viewmpagos(this.value)" name="pago" class="form-control" required>
                <?  $sqlpago=$mysqli->query("SELECT * FROM z_epagos");
                while($rowpago = $sqlpago->fetch_assoc()) {?><option value="<?= $rowpago['id']; ?>" <? if($rowpago['predefinido']==1){echo 'selected="selected"'; } ?>><?= $rowpago['estado']; ?></option><? } ?>
            </select>
          </div>
          <label for="mpagos" class="col-sm-2 col-form-label text-right font-weight-bold">Medios de Pago <span class="text-danger h5">*</span></label>
            <div class="col-sm-4">
            <select name="mpagos" class="form-control" disabled>
                <?  $sqlpago=$mysqli->query("SELECT * FROM z_pagos_medios");
                while($rowpago = $sqlpago->fetch_assoc()) {?><option value="<?= $rowpago['id']; ?>"><?= $rowpago['medio']; ?></option><? } ?>
            </select>
          </div>
        </div>
      <h3 class="text-danger text-right">FECHA LIMITE DE PAGO: <span class="text-dark"><?= $fecha; ?></span></h3>
        <input type="button" class="btn btn-primary float-left" value="Atras" name="Back2" onclick="history.back()" />
        <button type="submit" class="btn btn-success float-right m-1"><i class="fa fa-check"></i> Enviar a Bodega</button>
        <a href="corregir.php?code=<?= $code; ?>" class="btn btn-warning float-right m-1"><i class="fa fa-pencil-square-o"></i> CORREGIR</a>
        <!--<input type='button' class="btn btn-info float-right m-1" onclick='window.print();' value='Facturar y enviar a Bodega' />-->
      </form>
    </div>   
  </div>
  <?php include $r."inc/foot.php"; ?>
  <script> 
  var adicional="0";
  var abonof="0";
  var totalinicial=$("#total3").text();
  function adic(field) {
       adicional=field.value;
      var cont=0;
      var suma="0";
       while (cont<20){
          var v=$("#2por"+cont).text();
          if(v!=""){suma=parseInt(suma)+parseInt(v);}
         cont++;}
         
          if(adicional!=""){suma=parseInt(adicional)+parseInt(suma);}
         $('#total3').html(suma); 
  }
  function viewmpagos(valor){
      if(valor == 2){
          $('select[name=mpagos]').prop('disabled', false);
      }else{
           $('select[name=mpagos]').prop('disabled', true);
      }
  }
  function abonar(field) {
      abonof=field.value;
      var cont=0;
      var suma="0";
       while (cont<20){
          var v=$("#2por"+cont).text();
          if(v!=""){suma=parseInt(suma)+parseInt(v);}
         cont++;}
         
          if(abonof!=""){suma=parseInt(suma)+parseInt(adicional)-parseInt(abonof);}
         $('#total3').html(suma); 
  }
      
      
  </script> 
  
</body>
</html>    
    
 