<?php $r="../";
$code=$_GET['code'];
require_once($r."inc/funciones.php");
$titlepage = 'Reasignar Domiciliario';
validNotSession($r);
$mysqli = connectDB($r);
$spe=$_SESSION['perfil'];
roleaccess(1500);
$new=$mysqli->query("SELECT * FROM z_pedidos WHERE codigopedido='$code' AND estado<'1'");
$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'");
?>

  <?php include $r."inc/head.php"; ?>
<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. <? echo $code; ?></h3>
      <form id="formulario" class="p-3" method="POST" action="acciones.php">
        <input type="hidden" name="reasignar" value="<? echo $code; ?>">
        <div class="form-group row border-bottom">
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><span class="font-weight-bold">Codigo</span></label>
          <label class="col-4 col-sm-4 col-md-4 col-lg-4 col-xl-4 col-form-label"><span class="font-weight-bold">Poducto</span></label>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><span class="font-weight-bold">Cantidad</span></label>
          <? if($spe!=$pbo){ ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><span class="font-weight-bold">Bono</span></label>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><span class="font-weight-bold">Descuento</span></label>
          <? } ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><span class="font-weight-bold">Precio UND</span></label>
          <label class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 col-form-label"><span class="font-weight-bold">Total</span></label>
        </div>
        <?php while($row2 = $row1->fetch_assoc()) { $abono=$row2['abono'];$adicional=$row2['adicional']; $pago=$row2['pago']; ?>
        <div class="form-group row border-bottom">
        <?php $precio=$mysqli->query("SELECT * FROM z_distribuidores WHERE idcliente='".$row2['idcliente']."' AND idproducto='".$row2["idproducto"]."'");
          $pre = $precio->fetch_assoc();
          
            $cliente=sclien($row2['idcliente']);
            $row3=sprod($row2["idproducto"]);
          if($pre['precio']!=""){$precios=$pre['precio'];}
          else{ 
            if($cliente['tipocliente']==1){$precios=$row3['salsamentaria'];}
            if($cliente['tipocliente']==2){$precios=$row3['distribuidor'];}
            if($cliente['tipocliente']>2){$precios=$row3['precio'];}
          } ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><? echo $row3['codigo'] ?></label>
          <label class="col-4 col-sm-4 col-md-4 col-lg-4 col-xl-4 col-form-label"><? echo $row3['producto'] ?></label>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><? echo $row2["cantidad"] ?></label>
          <? if($spe!=$pbo){ ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><? echo $row2['bono'].' Unds' ?></label>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><? echo $row2["descuento"].' %' ?></label>
          <? } ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label">
              <input type="hidden" name="pre<? echo $row2["idproducto"];?>" value="<? echo $precios;?>">
              $ <span id="por<?php echo $row2["idproducto"]; ?>"><? echo $precios; ?></span> COP</label>
          <label class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 col-form-label">$ <span id="2por<? echo $row2["idproducto"]; ?>"><? $total=$precios*$row2['cantidad']; echo $total; ?></span> COP</label>
        
        </div>
       <? $cliente=$row2['idcliente']; $iduser=$row2['iduser']; $codigo=$row2['codigopedido'];$cantidad=$cantidad+$row2['cantidad'];$total2=$total2+$total;?>
        <?php } ?>
        
        <div class="form-group row">
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"></label>
          <label class="col-4 col-sm-4 col-md-4 col-lg-4 col-xl-4 col-form-label text-right"><b>TOTAL UND</b></label>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label"><?php echo '<b class="text-danger"> '.$cantidad.'</b>'; ?></label>
          <? if($spe!=$pbo){ ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label" id="bono"></label>
          <? } ?>
          <label class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 col-form-label text-right"><b>TOTAL A PAGAR</b></label>
          <label class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 col-form-label"><?php echo '<b class="text-danger">$<span id="total3"> '.$total2.'</span> COP</b>'; ?></label>
        </div>
        <div class="form-group row m-0">
          <label class="col-6 col-sm-6 col-md-6 col-lg-6 col-xl-6 col-form-label"></label>
          
          <? if($spe!=$pbo){ ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label" ></label>
          <? } ?>
          <label class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 col-form-label text-right"><b>Valor Adicional</b></label>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label text-right"><b>$ <? echo $adicional;?></b></label>
        </div>
        <div class="form-group row m-0">
          <label class="col-6 col-sm-6 col-md-6 col-lg-6 col-xl-6 col-form-label"></label>
          
          <? if($spe!=$pbo){ ?>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label" ></label>
          <? } ?>
          <label class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 col-form-label text-right"><b>Abono</b></label>
          <label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label text-right"><b>$ <? echo $abono;?></b></label>
        </div>
        <div class="form-group row">
          <label for="rutero" class="col-sm-2 col-form-label text-right"><span class="text-danger h5">*</span>Domiciliario asignado</label>
            <div class="col-sm-4">
            <? $row4 = $mysqli->query("SELECT * FROM z_ruteros WHERE eliminado=0 AND estado=0"); ?>
            <select name="rutero" class="form-control" required>
                <?php /*$ruteros=$mysqli->query("SELECT * FROM z_ruteros WHERE zona='$zona'");$rutero = $ruteros->fetch_assoc(); */?>
                <option value="<? echo $zona; ?>"><? if($rutero!=""){ echo $rutero['nombre'];}else{echo 'Selecciones un Rutero';} ?></option>
              <?  while($row5 = $row4->fetch_assoc()) {?><option value="<? echo $row5['id']; ?>"><? echo $row5['nombre']; ?></option><? } ?>
            </select>
          </div>
          <label for="pago" class="col-sm-2 col-form-label text-right"><span class="text-danger h5">*</span>Estado actual pedido</label>
            <div class="col-sm-4">
            <? $row4 = $mysqli->query("SELECT * FROM z_ruteros WHERE eliminado=0 AND estado=0"); ?>
            <select name="pago" class="form-control" required>
                <? if($pago==0){ ?><option value="0">Por cobrar</option><? } ?>
                <? if($pago==1){ ?><option value="1">Bono</option><? } ?>
                <? if($pago==2){ ?><option value="2">Cancelado</option><? } ?>
            </select>
          </div>
        </div>
        <div class="form-group row">
          <? $cliente=sclien($cliente);$user=suser($iduser);   ?>
          <label for="nombre" class="col-sm-2 col-form-label text-right"><span class="font-weight-bold">Nombres y apellidos cliente :  </span></label>
          <label for="nombre" class="col-sm-2 form-control col-form-label"><? echo  $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"><? echo  $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"><? echo  $codigo; ?></label>
        </div>
        
        <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" form="formulario"><i class="fa fa-check"></i> Reasignar Domiciliario</button>
        <!--<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 desc(field,precio,cantidad) {
      var preciototal=precio*cantidad;
      var descuentotal = -precio*field.value/100+precio;
      var descuento =descuentotal*cantidad;
      $('#'+field.name).html(descuentotal);
      $('#2'+field.name).html(descuento);
      var cont=0;
      var suma="0";
      while (cont<19){
          var v=$("#2por"+cont).text();
          if(v!=""){suma=parseInt(suma)+parseInt(v); }
         cont++;
      }$('#total3').html(suma);
      
  }
  function adic(field) {
       adicional=field.value;
      var cont=0;
      var suma="0";
       while (cont<19){
          var v=$("#2por"+cont).text();
          if(v!=""){suma=parseInt(suma)+parseInt(v);}
         cont++;}
         
          if(adicional!=""){suma=parseInt(adicional)+parseInt(suma);}
         $('#total3').html(suma); 
  }
 
  function abonar(field) {
      abonof=field.value;
      var cont=0;
      var suma="0";
       while (cont<19){
          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>    
    
 