<?php
include("session.php");
?>
<!DOCTYPE html>
<html>
<?php
include("head.php");
?>

<body>
<div id="wrapper">
<?php
include ("menu.php");
?>

<div id="page-wrapper" class="gray-bg dashbard-1">
<div class="row border-bottom">
<nav class="navbar navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<a class="navbar-minimalize minimalize-styl-2 btn btn-primary " href="#"><i class="fa fa-bars"></i> </a>

</div>
<ul class="nav navbar-top-links navbar-right">

<li>
<a href="index.php">

<i class="fa fa-sign-out"></i> Salir
</a>
</li>

</ul>

</nav>
</div>

<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">

<div class="ibox-title">
  <div class="pull-right">
       <?php
               if ($_SESSION["roles"]==1 || $_SESSION["roles"]==2) {
               ?>
  <button class="btn btn-primary" data-toggle="modal" data-target="#myModal"><i class="fas fa-plus"></i> Nueva Orden</button>
     <?php
              }
               ?>
  </div>
  <form action="pedidos.php" method="GET" name="buscaryear" id="buscaryear">
  <div class="row">
    <div class="col-md-4">
  <h5>Ordenes de Pedido Año:  <input autocomplete="off" type="text" size="4" style="border: none; background-color: white; display: inline; font-size: 18px" name="ayo" ondragenter="javascript:document.getElementById('buscaryear').submit()" value="<?php if (isset($_GET['ayo'])) {
    echo $_GET['ayo'];
  }  else echo date("Y")?>" ></h5>

 </div>
  </div>
</form>

</div>
<div class="ibox-content">

  <div class="table-responsive">
    Quitar/Mostrar Columnas:
              <a class="toggle-vis btn btn-xs btn-white" data-column="0" >Cod. Int.</a>
              <a class="toggle-vis btn btn-xs btn-white" data-column="1" >Orden #</a>
              <a class="toggle-vis btn btn-xs btn-white" data-column="2" >Fecha Pedido</a>
              <a class="toggle-vis btn btn-xs btn-white" data-column="3" >Cliente</a>
              <a class="toggle-vis btn btn-xs btn-white" data-column="8" >Detalles</a>
              <a class="toggle-vis btn btn-xs btn-white" data-column="9" >Comentarios</a>
              <a class="toggle-vis btn btn-xs btn-white" data-column="10" >Proforma</a>

      <table class="table table-striped table-hover dataTables-example">
          <thead>
          <tr style="background-color:#000000;color: #ffffff">

              <th>Cod. Int.</th>
              <th>Orden #</th>
              <th>Fecha Pedido</th>
              <th>Cliente</th>
              <th>Detalles</th>
              <th style="display: none;">Ciudad</th>
              <th style="display: none;">Telefono</th>
              <th style="display: none;">Forma de Pago</th>
              <th style="display: none;">Fecha Final de Producción</th>


              <th>Proforma</th>



               <?php
               if ($_SESSION["roles"]==1 || $_SESSION["roles"]==2) {
               ?>
              <th>Editar</th>

              <?php if ($_SESSION["roles"]==1) {
                ?>
              <th>Eliminar</th>

              <?php
            }
            }
            ?>
              </tr>
          </thead>
          <tbody style="font-weight: bold;">
               <?php
               include("conexion.php");
               $referencias  = [];
               $query_referencias="SELECT cap_ref,um_ref,peso_ref,ancho_ref,nombre_ref FROM `referencias`";
               $resultadoc_referencias=$conexion->query($query_referencias);
               while ($rowc_items=$resultadoc_referencias->fetch_assoc()) {
                 $referencias[$rowc_items["nombre_ref"]]  =    $rowc_items["cap_ref"];
               }

if (isset($_GET['ayo'])) {
$ayo=$_GET['ayo'];


}
else
{
$ayo=date("Y");
}


$query="SELECT `id_pedido`, `orden`, `fecha_pedido`, `fecha_final`, `cliente`, `forma_pago`, `embalaje`, nombre, ciudad, telefono FROM `pedidos`  inner join  clientes on id=cliente where fecha_pedido like '%$ayo%' or fecha_final like '%$ayo%'";
$resultadoc=$conexion->query($query);
while ($rowc=$resultadoc->fetch_assoc()) {


                  ?>
          <tr style="font-size: 10px">


              <td><?php echo $rowc['id_pedido']; ?></td>
              <td><?php echo $rowc['orden']; ?></td>
              <td><?php echo $rowc['fecha_pedido'];?></td>

              <td><?php echo strtoupper($rowc['nombre']);?></td>

                                        <td><a href="#" data-toggle="modal" data-target="#myModal_<?php echo $rowc['id_pedido'] ?>"><i class="fa fa-tasks"></i></a>

<!-- Modal-->
     <div class="modal inmodal" id="myModal_<?php echo $rowc['id_pedido'] ?>" tabindex="-1" role="dialog" aria-hidden="true">
     <div class="modal-dialog">
     <div class="modal-content animated bounceInRight">
    <div class="modal-header" style="text-align: left">
      <h4 class="modal-title"><img alt="Logo" src="img/logo.png" height="10%" width="10%">Detalles Orden de Producción</h4>
    </div>
    <div class="modal-body">



               <div class="form-group">
                              <div class="row">
              <div class="col-md-12">
                              <label>Nombre del Proveedor</label>
                               <?php
            $pedido=$rowc['id_pedido'];
            $pedido2=$rowc['id_pedido'];


      $query2="SELECT distinct(`supplier`) supplier FROM `pedidos_items` where orden_pedido ='$pedido'";
      $resultadoc2=$conexion->query($query2);
    //echo $query2;
$supliers="";
      ?>
<input type="hidden" name="numpedido_<?php echo $pedido ?>" id="numpedido_<?php echo $pedido ?>" value="<?php echo $pedido ?>">
<select class="form-control m-b chosen-select" name="cliente" id="cliente" onchange="buscar_ref_det(this.value,<?php echo $pedido ?>)">
<option selected value="">Seleccione</option>
      <?php
while ($rowc2=$resultadoc2->fetch_assoc()) {

            ?>



                             <?php
           $querycl="SELECT * FROM `clientes` where `tipo_tercero`='1'  or `tipo_tercero`='3'  order by nombre asc";
          $resultadocl=$conexion->query($querycl);
          while ($rowcl=$resultadocl->fetch_assoc()) {
              if ($rowcl['id']==$rowc2['supplier']) {
                 $supliers .="<option value='".$rowcl['id']."'>".$rowcl['nombre'] ."</option>";
              }

          };
          ?>

     <?php
}

echo $supliers;
          ?>
    </select> </div>
</div>
<div class="row">
                  <div class="col-md-3">
                        <label>Referencia</label>
                        <select onchange="buscar_det_item(this.value,<?php echo $pedido ?>)" class="form-control" style="width: 220px" name="referencia_det_<?php echo $pedido ?>" id="referencia_det_<?php echo $pedido ?>">
                          <option value="">Select</option>
                        </select>
                      </div>


       </div>

                    <div class="row">
                      <div class="col-md-3">
                        <label>Qty</label>
                        <input type="text" style="width: 120px; margin-right: 5px" class="form-control" name="qty_ref_<?php echo $pedido ?>" id="qty_ref_<?php echo $pedido ?>">
                   </div>
                   <div class="col-md-3">
                        <label>UM</label>
                        <input type="text" class="form-control" style="width: 120px; margin-right: 5px" name="um_det_ref_<?php echo $pedido ?>" id="um_det_ref_<?php echo $pedido ?>">
                   </div>
                    <div class="col-md-3">
                        <label>Width</label>
                        <input type="text" class="form-control" style="width: 120px; margin-right: 5px" name="width_det_ref_<?php echo $pedido ?>" id="width_det_ref_<?php echo $pedido ?>">
                   </div>
                   <div class="col-md-3">
                        <label>Weight</label>
                        <input type="text" class="form-control" style="width: 120px; margin-right: 5px" name="weight_det_ref_<?php echo $pedido ?>" id="weight_det_ref_<?php echo $pedido ?>">
                   </div>
                 </div>
                    <div class="row">
                   <div class="col-md-3">
                        <label>Meter Per Kilo</label>
                        <input type="text" style="width: 120px; margin-right: 5px"  class="form-control"  name="meter_det_ref_<?php echo $pedido ?>" id="meter_det_ref_<?php echo $pedido ?>">
                   </div>
                   <div class="col-md-3">
                        <label>Price</label>
                        <input type="text" style="width: 120px; margin-right: 5px" class="form-control"  name="price_det_ref_<?php echo $pedido ?>" id="price_det_ref_<?php echo $pedido ?>">
                   </div>
                    <div class="col-md-3">
                        <label>Total USD</label>
                        <input type="text" style="width: 120px; margin-right: 5px" class="form-control"  name="total_det_ref_<?php echo $pedido ?>" id="total_det_ref_<?php echo $pedido ?>">
                   </div>
                  </div>

                    <div class="row">
                      <div class="col-md-12">
                        <label style="margin-top: 20px;"><h3>Color Details</h3></label>
                        </div>
                         </div>
                                                               <div class="row">
                      <div class="col-md-4">
                        <label>Color</label>
                        <input type="text" style="width: 140px; margin-right: 5px"  class="form-control" name="color_det_<?php echo $pedido ?>" id="color_det_<?php echo $pedido ?>">

                   </div>
                   <div class="col-md-4">
                        <label>Quantity Color</label>
                        <input type="text"   style="width: 140px; margin-right: 5px"  class="form-control" onkeyup="cambiar_<?php echo $pedido ?>()" name="qty_det_<?php echo $pedido ?>" id="qty_det_<?php echo $pedido ?>">

<script type="text/javascript">
function cambiar_<?php echo $pedido ?>()
{

variable = document.getElementById("qty_det_<?php echo $pedido ?>").value;
variable= variable.replace(/\D/g, "");
variable= variable.replace(/([0-9])([0-9]{0})$/, '$1');
variable= variable.replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",");
document.getElementById("qty_det_<?php echo $pedido ?>").value=variable;

}


</script>
                   </div>
                     <div class="col-md-4">
                       <button type="button" style="margin-top: 18px" class="btn btn-white" onclick="regdetalles_color(<?php echo $pedido ?>)">Agregar</button>

                   </div>

                  </div>

                            <div class="row">

                         <div class="col-md-12">

<div id="resultado_detalles_<?php echo $pedido ?>" style="margin-top: 20px">
<table width='100%' border='1' style='border-collapse: collapse;' class='table table-bordered table-condensed table-hover'>
                             <?php




      $query_items="SELECT id_item, nombre, descripcion, supplier, um FROM `pedidos_items` inner join clientes on id=supplier where orden_pedido ='$pedido' order by nombre asc";
$supliern="";
$i=0;
$resultadoc_items=$conexion->query($query_items);
while ($rowc_items=$resultadoc_items->fetch_assoc()) {




$Total=0;
$Total2=0;
$pedido=$rowc_items['id_item'];
$consulta_color="SELECT `id_pedidos_color`, `id_pedidos_items`, `color`, pedidos_items_color.`quantity` quantity, nombre_ref, descripcion FROM `pedidos_items_color` inner join pedidos_items on id_item=id_pedidos_items inner join referencias on descripcion=id_ref where id_pedidos_items ='$pedido' order by color asc";
$resultado_color=$conexion-> query ($consulta_color);



$supliern2=$rowc_items['nombre'];


$consult_total="SELECT sum(pedidos_items_color.quantity) quantity FROM `pedidos_items_color` inner join pedidos_items on id_item=id_pedidos_items inner join clientes on id=supplier where nombre='$supliern2' and orden_pedido ='$pedido2' ";
$resultado_total=$conexion-> query ($consult_total);
$row_total=$resultado_total->fetch_assoc();

$Total=$row_total['quantity'];





if ($supliern2!=$supliern)
{


echo "

<tr>
<td colspan=7 ><a data-toggle='modal' data-target='#myModal_purchase_".$pedido ."' ><i class='fa fa-file-pdf-o'></i></a> Purchase Order to ".$rowc_items['nombre']."- <a href='#' class='pull-right'  data-toggle='modal' data-target='#myModal_emp_".$pedido ."'> <i class='fa fa-file-file-o'></i> Inst. Empaques</a>  </td>
</tr>
<tr>
<td style='bacbground-color:#ffffff';align=center width=5%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>#</td>
<td align=center width=10%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;' colspan=2>Referencia</td>
<td align=center width=10%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;' colspan=2>Color.</td>
<td align=center width=15%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>Qty</td>
<td align=center width=15%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>Cap. 40HQ</td>
<td align=center width=15%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>%</td>
<td align=center width=15%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'><i class='fa fa-trash'></i></td>
</tr>
";
$i=0;


?>


<!-- Viejo Modal-->
     <div class="modal inmodal" id="myModal_emp_<?php echo $pedido ?>" tabindex="-1" role="dialog" aria-hidden="true" >
     <div class="modal-dialog modal-xs" style="width: 550px">
     <div class="modal-content animated bounceInRight">
    <div class="modal-header" style="text-align: left">
      <h4 class="modal-title"><img alt="Logo" src="img/logo.png" height="10%" width="10%"> Generar Instrucciones </h4>
    </div>



<form action="imprimir_detalle_etiquetas.php" method="get" target="_blank">
<div class="modal-body">
<input type="hidden" name="id_supplier" value="<?php echo $rowc_items['supplier'] ?>">
<input type="hidden" name="id" value="<?php echo $pedido2 ?>">

               <div class="form-group">

          <div class="row">
                  
                      <div class="col-md-3">
                        <label>1. TAPE</label><br>
                      </div>
                      <div class="col-md-6">
                       <select class="form-control" name="tape" required="">
                         <option value="">Seleccione</option>
                         <option value="We need white tape for each roll">We need white tape for each roll</option>
                         <option value="We need company name tape for each roll">We need company name tape for each roll</option>
                       </select>
                      </div>

         
            </div>

            <div class="row">
                  <div class="col-md-12">
                        <label>PACKING DETAILS</label><br>


                    <div class="row">
                      <div class="col-md-3">
                        <label>2. packing details</label>
                      </div>
                      <div class="col-md-6">
                        <select class="form-control" name="packing_det1" style="font-size: 12px">
                          <option value="">Selecc</option>
                          <option value="a single transparent poly bag">a single transparent poly bag</option>
                          <option value="a single transparent poly bag + braid bag(编织带) ">a single transparent poly bag + braid bag(编织带)</option>
                        </select>
                      </div>
                    </div>



                    <div class="row">
                      <div class="col-md-3">
                        <label>3. packing</label>
                      </div>
                      <div class="col-md-6">
                        <select class="form-control" name="packing_det2" style="font-size: 12px">
                          <option value="" >Selecc</option>
                          <option value="Loose Packing">Loose Packing</option>
                          <option value="Bale Packing">Bale Packing</option>
                        </select>
                      </div>
                    </div>



                    <div class="row">
                      <div class="col-md-3">
                      <label>4. mix or not suppliers</label>
                    </div>
                    <div class="col-md-6">
                   <select class="form-control" name="mix_det2">
                          <option value="">Selecc</option>
                          <option value="This cargo will be ship together with other goods that are under production now">This cargo will be ship together with other goods that are under production now</option>
                          <option value="This cargo will be produced only by one supplier">This cargo will be produced only by one supplier</option>
                        </select>
                    </div>
                    </div>


                        <div class="row">
                      <div class="col-md-3">
                       <label>5. container type</label>
                    </div>
                    <div class="col-md-6">
                       <select class="form-control" name="container2">
                          <option value="">Selecc</option>
                          <option value="40”HQ">40”HQ</option>
                          <option value="40”GP">40”GP</option>
                          <option value="20”">20”</option>
                        </select>
                    </div>
                    </div>
                    
                    </div>

                  </div>


                  <div class="row" style="display: none">
                  <div class="col-md-12">
                        <label>SHIPMENT DETAILS</label><br>
                        <div class="row">
                      <div class="col-md-3">
                        <label>1. packing type</label></div>
                      <div class="col-md-6"><input type="tex" class="form-control" name="packing"></div>
                    </div>
                    <div class="row">
                      <div class="col-md-3">
                      <label>2. mix or not suppliers</label>
                    </div>
                    <div class="col-md-6">
                      <input type="tex" class="form-control" name="mix">
                    </div>
                    </div>
                        <div class="row">
                      <div class="col-md-3">
                       <label>3. container type</label>
                    </div>
                    <div class="col-md-6">
                       <input type="tex" class="form-control" name="container">
                    </div>
                    </div>

                  </div>
                   </div>
                   <div class="row">
                    <div class="col-md-3">

                        <label>6. Shiping Samples Details</label><br>
                    </div>
                    <div class="col-md-6">
                       <textarea class="form-control" name="samples"></textarea>
                    </div>

                </div>


                  <div class="row">
                      <div class="col-md-3">
                        <label>7. Lenght per rool</label>
                      </div>
                        <div class="col-md-6">
                       <input type="tex" class="form-control" name="lenght_per">
                    </div>

                      </div>



                   

                
                   </div>



                </div>
                <div class="modal-footer">
                 <button data-toggle="modal" data-target="#myModal_emp_<?php echo $pedido ?>" type="button" class="btn btn-white">Cerrar</button>
                  <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#myModal_emp_<?php echo $pedido ?>">Generar</button>

              </div>

    </div><!-- div body-->






              


</form>

              </div>
                </div>
                </div>
<!-- Modal-->







<!-- Modal purchase-->
     <div class="modal inmodal" id="myModal_purchase_<?php echo $pedido ?>" tabindex="-1" role="dialog" aria-hidden="true" >
     <div class="modal-dialog modal-xs" style="width: 550px">
     <div class="modal-content animated bounceInRight">
    <div class="modal-header" style="text-align: left">
      <h4 class="modal-title"><img alt="Logo" src="img/logo.png" height="10%" width="10%"> Generar Purchase Order</h4>
    </div>




<form action="imprimir_detalle.php" method="get" target="_blank">
<div class="modal-body">
<input type="hidden" name="id_supplier" value="<?php echo $rowc_items['supplier'] ?>">
<input type="hidden" name="id" value="<?php echo $pedido2 ?>">

               <div class="form-group">

          <div class="row">
                  <div class="col-md-12">
                        <label>Date</label><br>
                        <input type="date" class="form-control" name="date">

                  </div>
                  </div>


                </div>
    </div><!-- div body-->






              <div class="modal-footer">
                 <button data-toggle="modal" data-target="#myModal_purchase_<?php echo $pedido ?>" type="button" class="btn btn-white">Cerrar</button>
                  <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#myModal_purchase_<?php echo $pedido ?>">Generar</button>

              </div>


</form>

              </div>
                </div>
                </div>
<!-- Modal purchase-->
<?php

}



$refn="";
$supliern2="";

while ($row_color=$resultado_color->fetch_assoc()){

$refn2=$row_color['nombre_ref'];
if ($refn2!=$refn)
{


$supliern=$rowc_items['nombre'];
$i=$i+1;
//$referencias[$rowc_items["nombre_ref"]]
echo "<tr>
<td align=center height=25px>".$i."</td>
<td align=center height=25px align=center colspan=2> ".$row_color['nombre_ref']."</td>
<td align=center height=25px colspan=2 >".$row_color['color']."</td>
<td align=center height=25px>".number_format($row_color['quantity'],0,".",","). " " .$rowc_items['um']."</td>
<td align=center height=25px>".number_format($referencias[$row_color['nombre_ref']],0,".",",")."</td>
<td align=center height=25px>".number_format(($row_color['quantity'] * 100 )/$referencias[$row_color['nombre_ref']],0,".",",")."</td>
<td align=center height=25px><a onclick=elminar_det_color(".$row_color['id_pedidos_color'].",".$pedido2.") ><i class='fa fa-trash'></i></a></td><tr>";


}




else
{
echo "<tr>
<td align=center height=25px style='border:none'></td>
<td align=center height=25px style='border:none' colspan=2 ></td>
<td align=center height=25px colspan=2 >".$row_color['color']."</td>
<td align=center height=25px>".number_format($row_color['quantity'],0,".",","). " " .$rowc_items['um']."</td>
<td align=center height=25px>".number_format($referencias[$row_color['nombre_ref']],0,".",",")."</td>
<td align=center height=25px>".number_format(($row_color['quantity'] * 100 )/$referencias[$row_color['nombre_ref']],0,".",",")."</td>
<td align=center height=25px><a onclick=elminar_det_color(".$row_color['id_pedidos_color'].",".$pedido2.") ><i class='fa fa-trash'></i></a></td>
</tr>";

}

$refn=$row_color['nombre_ref'];



$Total2= $Total2 + $row_color['quantity'];
$Total3= $Total3 + $referencias[$row_color['nombre_ref']];
$Total4= $Total4 + ($row_color['quantity'] * 100 )/$referencias[$row_color['nombre_ref']];
}


if ($supliern2!=$supliern)
{



echo "<tr style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>
<td align=center height=25px style='border:none'></td>
<td align=center height=25px style='border:none' align=center colspan=2></td>
<td align=center height=25px colspan=2 >TOTAL</td>
<td align=center height=25px>".number_format($Total2,0,".",",")." " .$rowc_items['um']."</td>
<td align=center height=25px>".number_format($Total3,0,".",",")."</td>
<td align=center height=25px>".number_format($Total4,0,".",",")."</td>
<td align=center height=25px>
<tr>";

}

$supliern=$rowc_items['nombre'];






                             }

                             ?>
</table>




                       </div>

                     </div>
                   </div>


</div>
    </div><!-- div body-->






              <div class="modal-footer">
                 <a href="pedidos.php"> <button type="button" class="btn btn-white">Cerrar</button></a>
                  <button style="display: none" type="submit" class="btn btn-primary">Guardar cambios</button>

              </div>




              </div>
                </div>
                </div>
<!-- Modal-->


</td>

              <td style="display: none;"><?php echo strtoupper($rowc['ciudad']);?></td>
              <td style="display: none;"><?php echo strtoupper($rowc['telefono']);?></td>

              <td style="display: none;"><?php echo $rowc['forma_pago'];?></td>
              <td style="display: none;"><?php echo $rowc['fecha_final'];?></td>







               <td align="center"><a target="_blank" data-toggle='modal' data-target='#myModal_prof_<?php echo $rowc['id_pedido'] ?>' ><i class="fa fa-file-pdf-o"></i></a></td>
               <?php
               if ($_SESSION["roles"]==1 || $_SESSION["roles"]==2) {
               ?>
             <td align="center"><a href="pedidos.php?id=<?php echo $rowc['id_pedido'] ?>"><i class="fas fa-pencil-alt"></i></a></td>

              <?php if ($_SESSION["roles"]==1) {
                ?>
              <td align="center"><a href="eliminar_pedido.php?id=<?php echo $rowc['id_pedido'] ?>"><i class="fa fa-trash"></i></a></td>

              <?php
            }
            }
            ?>


          </tr>


          <!-- Modal-->
     <div class="modal inmodal" id="myModal_prof_<?php echo $rowc['id_pedido'] ?>" tabindex="-1" role="dialog" aria-hidden="true" >
     <div class="modal-dialog modal-xs" style="width: 550px">
     <div class="modal-content animated bounceInRight">
    <div class="modal-header" style="text-align: left">
      <h4 class="modal-title"><img alt="Logo" src="img/logo.png" height="10%" width="10%"> Generar Proforma </h4>
    </div>



<form action="imprimir_proforma.php" method="get" target="_blank">
<div class="modal-body">
<input type="hidden" name="id" value="<?php echo $rowc['id_pedido'] ?>">

               <div class="form-group">


                   <div class="row">
                  <div class="col-md-12">

                        <div class="row">
                      <div class="col-md-4">
                        <label>FINAL DESTINATION</label></div>
                      <div class="col-md-6">
                        <select class="form-control m-b chosen-select" name="final" id="final">

          <option value="">Seleccione</option>
                             <?php
           $queryc_destino="SELECT `id_destino`, `nombre_destino` FROM `destino`";
          $resultadoc_destino=$conexion->query($queryc_destino);
          while ($rowc_destino=$resultadoc_destino->fetch_assoc()) {

         echo "<option  value='".$rowc_destino['nombre_destino']."'>".$rowc_destino['nombre_destino'] ."</option>";

          };
          ?>
      </select>

                      </div>
                    </div>

                    <div class="row">
                      <div class="col-md-4">
                        <label>COUNTRY OF ORIGIN</label></div>
                      <div class="col-md-6">
                        <select class="form-control m-b chosen-select" name="origen" id="origen">

          <option value="">Seleccione</option>
                             <?php
           $queryc_origen="SELECT `id_origen`, `nombre_origen` FROM `origen`";
          $resultadoc_origen=$conexion->query($queryc_origen);
          while ($rowc_origen=$resultadoc_origen->fetch_assoc()) {

         echo "<option  value='".$rowc_origen['nombre_origen']."'>".$rowc_origen['nombre_origen'] ."</option>";

          };
          ?>
      </select>

                      </div>
                    </div>

<div class="row">
                      <div class="col-md-4">
                        <label>DELIVERY</label></div>
                      <div class="col-md-6">
                        <select class="form-control m-b chosen-select" name="entrega" id="entrega">

          <option value="">Seleccione</option>
                             <?php
           $queryc_entrega="SELECT `id_entrega`, `nombre_entrega` FROM `entrega`";
          $resultadoc_entrega=$conexion->query($queryc_entrega);
          while ($rowc_entrega=$resultadoc_entrega->fetch_assoc()) {

         echo "<option  value='".$rowc_entrega['nombre_entrega']."'>".$rowc_entrega['nombre_entrega'] ."</option>";

          };
          ?>
      </select>

                      </div>
                    </div>


                     <div class="row">
                      <div class="col-md-4">
                        <label>BANK ACCOUNT</label></div>
                      <div class="col-md-6">
                        <select class="form-control m-b chosen-select" name="bancos" id="bancos">

          <option value="">Seleccione</option>
                             <?php
           $queryc_bancos="SELECT `id_bancos`, `nombre_bancos` FROM `bancos`";
          $resultadoc_bancos=$conexion->query($queryc_bancos);
          while ($rowc_bancos=$resultadoc_bancos->fetch_assoc()) {

         echo "<option  value='".$rowc_bancos['id_bancos']."'>".$rowc_bancos['nombre_bancos'] ."</option>";

          };
          ?>
      </select>

                      </div>
                    </div>


                    <div class="row">
                      <div class="col-md-4">
                      <label>PAYMENT</label>
                    </div>
                    <div class="col-md-6">
                      <select class="form-control m-b chosen-select" required name="payment">
<option value="">Seleccione</option>
<option value="100% BEFORE SHIPMENT">100% BEFORE SHIPMENT</option>
<option value="100% WITHIN 30 DAYS AFTER SHIPMENT">100% WITHIN 30 DAYS AFTER SHIPMENT</option>
<option value="ADVANCE PAYMENT 20% BALANCE PAYMENT 80% WITHIN 30 DAYS AFTER SHIPMENT">ADVANCE PAYMENT 20% BALANCE PAYMENT 80% WITHIN 30 DAYS AFTER SHIPMENT</option>
<option value="ADVANCE PAYMENT 20% BALANCE PAYMENT 80% AGAINST DOCUMENTS">ADVANCE PAYMENT 20% BALANCE PAYMENT 80% AGAINST DOCUMENTS</option>

                      </select>
                    </div>
                    </div>
                        <div class="row">
                      <div class="col-md-4">
                       <label>PACKING</label>
                    </div>
                    <div class="col-md-6">
                      <select class="form-control m-b chosen-select" required name="packing">
<option value="">Seleccione</option>
<option value="LOOSE PACKING">LOOSE PACKING</option>
<option value="BALE PACKING">BALE PACKING</option>

                      </select>
                    </div>
                    </div>

                  </div>
                   </div>
                   <div class="row">
                  <div class="col-md-12" style="display: none;">
                        <label>Shiping Samples Details</label><br>
                       <textarea class="form-control" name="samples"></textarea>

                  </div>
                </div>



                </div>
    </div><!-- div body-->






              <div class="modal-footer">
                 <button data-toggle="modal" data-target="#myModal_prof_<?php echo $rowc['id_pedido'] ?>" type="button" class="btn btn-white">Cerrar</button>
                  <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#myModal_prof_<?php echo $rowc['id_pedido'] ?>">Generar</button>

              </div>


</form>

              </div>
                </div>
                </div>
<!-- Modal-->

           <?php
}
?>
          </tbody>
      </table>
  </div>

</div>
</div>

</div>
</div>



<?php include("footer.php"); ?>
</div>


<!-- MODAL --->

<div class="modal inmodal" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">

  <form class="form-horizontal" action="<?php
    if (isset($_GET['id'])) {
                      echo "act_pedido.php";
               }
               else
               {
                  echo "reg_pedido.php";
               }
                ?>" method="POST" name="formulario">
      <div class="modal-dialog">
      <div class="modal-content animated bounceInRight">
              <div class="modal-header" style="text-align: left">
                  <a href="pedidos.php"><button type="button" class="close" ><span aria-hidden="true">×</span><span class="sr-only">Close</span></button></a>

                  <h4 class="modal-title"><img alt="Logo" src="img/logo.png" height="10%" width="10%">
                    <?php
                    if (isset($_GET['id'])) {
                echo " Editar";

               }
               else
               {
            echo " Nueva ";
               } ?> Orden</h4>
                  <!-- <small class="font-bold">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</small> -->
              </div>



<div class="modal-body">
                  <!--
               SELECT `id_cotizacion`, `fecha`, `nit_cliente`, `dirigido_a`, `cargo`, `tipo_carroceria`, `chasis`, `largo_int`, `ancho_int`, `alto_int`, `concesionario`, `largo_ext`, `ancho_ext`, `alto_ext`, `plataforma`, `perfileria`, `forro`, `marco`, `incluye`, `tiempo`, `garantia`, `pago`, `referencia`, `nombre_referencia`, `cantidad`, `precio_unit`, `total`, `observaciones`, `asesor_interno`, `asesor_concesionario`, `estado`, `num_orden_p` FROM `cotizaciones`
                  -->
                      <?php
$id_pedido=0;
                  if (isset($_GET['id'])) {
                      $id_pedido=$_GET['id'];

               }





                    $query="SELECT `id_pedido`, `orden`, `fecha_pedido`, `fecha_final`, `cliente`, `observaciones`, `forma_pago`, `embalaje` FROM `pedidos` WHERE id_pedido='$id_pedido'";
$resultadoc=$conexion->query($query);
$rowx=$resultadoc->fetch_assoc();

?>

<div class="form-group">
<div class="row">
<div class="col-md-4">
<label>Fecha de Pedido</label>
<input style="background-color: #e3e4e5" type="date" name="fecha_pedido" placeholder="" required class="form-control col-md-4" value="<?php if($rowx["fecha_pedido"]!="") {echo $rowx["fecha_pedido"];} else { echo date("Y-m-d"); }?>" >
</div>

<div class="col-md-2">
<label>Orden #</label>
<input style="background-color: #e3e4e5" type="text" class="form-control" name="orden" value='<?php echo $rowx["orden"]; ?>'>

</div>

<div class="col-md-6">
<label>Nombre de Cliente</label>
<input  style="background-color: #e3e4e5" type="hidden" name="id_pedido" id="id_pedido" value='<?php echo $id_pedido; ?>'>
<select style="background-color: #e3e4e5" class="form-control m-b chosen-select" name="cliente" id="cliente" onchange="buscar_nit(this.value)">

   <option style="background-color: #e3e4e5" value="">Seleccione</option>

       <?php
         $queryc="SELECT * FROM `clientes` where `tipo_tercero`='2'  or `tipo_tercero`='3'  order by nombre asc";
          $resultadoc=$conexion->query($queryc);
          while ($rowc=$resultadoc->fetch_assoc()) {
              if ($rowc['id']==$rowx['cliente']) {
                echo "<option selected value='".$rowc['id']."'>".$rowc['nombre'] ."</option>";
              }
              else
              {
         echo "<option  value='".$rowc['id']."'>".$rowc['nombre'] ."</option>";
        }
          };
          ?>
      </select>

</div>


     </div>
   </div>

   <div class="form-group">
                              <div class="row">



                                 <div class="col-md-4" style="display: none;">
                              <label>Fecha Final de Producción</label>
                              <input type="date" name="fecha_final" id="fecha_final" placeholder="" class="form-control col-md-4" value="<?php if($rowx["fecha_final"]!="") {echo $rowx["fecha_final"];} ?>" >
                                </div>

                                <div class="col-md-4" style="display: none;" >
                                  <label>Embalaje</label>
                                  <select class="form-control" name="embalaje">

                                    <option value="">Seleccione</option>
                             <?php
           $queryc="SELECT `id_embalaje`, `nombre_embalaje` FROM `embalaje` order by nombre_embalaje asc";
          $resultadoc=$conexion->query($queryc);
          while ($rowc=$resultadoc->fetch_assoc()) {
              if ($rowc['nombre_embalaje']==$rowx["embalaje"]) {
                echo "<option selected value='".$rowc['nombre_embalaje']."'>".$rowc['nombre_embalaje'] ."</option>";
              }
              else
              {
         echo "<option  value='".$rowc['nombre_embalaje']."'>".$rowc['nombre_embalaje'] ."</option>";
        }
          };
          ?>

                                  </select>
                                </div>




                      </div>
                    </div>

                      <div class="form-group">
                              <div class="row">

                                <div class="col-md-12" style="display: none;">
                              <label>Forma de Pago</label>
                              <select class="form-control" name="forma_pago">
                 <option value="">Seleccione</option>
                             <?php
           $queryc="SELECT `id_forma_de_pago`, `nombre_forma_de_pago` FROM `forma_de_pago` order by nombre_forma_de_pago asc";
          $resultadoc=$conexion->query($queryc);
          while ($rowc=$resultadoc->fetch_assoc()) {
              if ($rowc['nombre_forma_de_pago']==$rowx["forma_pago"]) {
                echo "<option selected value='".$rowc['nombre_forma_de_pago']."'>".$rowc['nombre_forma_de_pago'] ."</option>";
              }
              else
              {
         echo "<option  value='".$rowc['nombre_forma_de_pago']."'>".$rowc['nombre_forma_de_pago'] ."</option>";
        }
          };
          ?>



                                  </select>





                                </div>



                      </div>
                    </div>




                        <!-- compras -->





                   <div class="form-group">
                          <div class="row">
                            <div class="col-md-12" style="display: none;">
                           <label>Observaciones</label>
                           <textarea onkeyup="MaysPrimera(this);" name="observaciones" class="form-control"><?php echo $rowx['observaciones'] ?></textarea>

                      </div>
                    </div>
                  </div>
                        <div class="form-group">
                          <div class="row">
                           <div class="col-md-12" style="text-align: left; color: #1f51a3">
                           <label><h4>Detalle de Productos/Referencias</h4></label>
                           <hr style="margin:2% 0;border: 2px solid #d6e5ff;">
                          </div>
                      </div>
                  </div>
                 
                  <?php
                  $temporal=rand(5, 17958);
                  ?>



            <div class="form-group">
                  <div class="row">
                       <div class="col-md-12">
                            <input type="hidden" name="num_orden" id="num_orden" value='<?php  if (isset($_GET['id'])) {
                                 echo $_GET['id'];
                                 } else {echo $temporal; }?>'>
               
                            <label>Tela o Referencia</label>
                            <select  class="form-control m-b chosen-select" name="referencia" id="referencia" placeholder="Item" onchange="buscar_ref(this.value)">

                             <option value="">Seleccione</option>
                             <?php




                             $queryc="SELECT `id_ref`, `nombre_ref`, `ancho_ref`, `peso_ref`, `rendi_ref`, `um_ref`, `comp_ref`, `const_ref`, `cap_ref`, `prec_venta`, `prec_comp`, `posi_aranc`, `tejido_ref`, `ligamento_ref`, `acabdado_ref`, `pais_origen`, `cod_ref` FROM `referencias` order by nombre_ref asc";
                            $resultadoc=$conexion->query($queryc);
                            while ($rowc=$resultadoc->fetch_assoc()) {

                           echo "<option  value='".$rowc['id_ref']."'>".$rowc['nombre_ref'] ."</option>";

                            };
                            ?>
                            </select>

                          </div>

                          <div class="col-md-6">
                              <label>Remark</label>
                              <select class="form-control" name="remark" id="remark">
                              <option value="">Seleccione</option>

                                       <?php
                                                  $queryc="SELECT `id_remarks`, `nombre_remarks` FROM `remarks`  order by nombre_remarks asc";
                                                  $resultadoc=$conexion->query($queryc);
                                                  while ($rowc=$resultadoc->fetch_assoc()) {

                                                  echo "<option  value='".$rowc['nombre_remarks']."'>".$rowc['nombre_remarks'] ."</option>";

                                                  };
                                        ?>
                                  </select>
                           </div>


                          <div class="col-md-6">
                              <label>Teñido</label>

                              <select style="background-color: #e3e4e5" class="form-control m-b chosen-select" name="tenido" id="tenido">

                               <option style="background-color: #e3e4e5" value="">Seleccione</option>

                                   <?php
                                     $queryc="SELECT `id_teñido`, `nombre_teñido` FROM `teñido`  order by nombre_teñido asc";
                                      $resultadoc=$conexion->query($queryc);
                                      while ($rowc=$resultadoc->fetch_assoc()) {
                                          if ($rowc['id_teñido']==$rowx['nombre_teñido']) {
                                            echo "<option selected value='".$rowc['id_teñido']."'>".$rowc['nombre_teñido'] ."</option>";
                                          }
                                          else
                                          {
                                     echo "<option  value='".$rowc['id_teñido']."'>".$rowc['nombre_teñido'] ."</option>";
                                    }
                                      };
                                      ?>
                                  </select>

                           </div>



                            </div>
                          </div>



                  <div class="form-group">
                            <div class="row">
                               <div  class="col-md-6">
                                  <label>Nombre Tela (Etiqueta)</label>
                                  <input type="text" name="tela_etiqueta" id="tela_etiqueta" class="form-control">
                                      </div>
                                      <div class="col-md-6">
                              <label>Nombre del Proveedor</label>
                              <select class="form-control m-b chosen-select" name="supplier" id="supplier" onchange="buscar_nit(this.value)">

          <option value="">Seleccione</option>
                             <?php
           $queryc="SELECT * FROM `clientes` where `tipo_tercero`='1'  or `tipo_tercero`='3'  order by nombre asc";
          $resultadoc=$conexion->query($queryc);
          while ($rowc=$resultadoc->fetch_assoc()) {

         echo "<option  value='".$rowc['id']."'>".$rowc['nombre'] ."</option>";

          };
          ?>
      </select>

    </div>
                            </div>
                          </div>


                          <div class="form-group">
                            <div class="row">
                                <div class="col-md-4">
                            <label>Cantidad</label>
                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_porc()"  class="form-control" name="quantity" id="quantity">
                          </div>

                          <div class="col-md-4">
                            <label>Capacidad</label>
                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_porc()"  class="form-control" name="cap_ref" id="cap_ref">
                          </div>




                          <div class="col-md-4">
                            <label>Ocupacion %</label>
                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="porc_ocup" id="porc_ocup">
                          </div>
                            </div>
                          </div>





         <div class="form-group">
                            <div class="row">

                           <div class="col-md-3">
                            <label>Ancho</label>
                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="ancho_ref" id="ancho_ref">
                          </div>
                          <div class="col-md-3">
                            <label>Peso</label>

                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="peso_ref" id="peso_ref">
                          </div>
                           <div class="col-md-3">
                            <label>Rendimiento</label>
                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="rendi_ref" id="rendi_ref">
                          </div>

                                  <div class="col-md-3">
                            <label>U.M.</label>
                            <input  type="text" autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="um_ref" id="um_ref" value="">
                          </div>

                           </div>
                  </div>

                      <div class="form-group">
                            <div class="row">

                              <div class="col-md-2">
</div>
                          <div class="col-md-4">
                            <label>$ Compra</label>
                            <input  type="text"  autocomplete="off" placeholder=""   class="form-control" name="prec_comp" id="prec_comp">
                          </div>



                           <div class="col-md-4">
                            <label>$ Venta</label>
                            <input  type="text"  autocomplete="off" placeholder="" class="form-control" name="prec_venta" id="prec_venta">
                          </div>
<div class="col-md-2">
</div>

</div>
                  </div>

                  <div class="form-group">
                            <div class="row">

                              <div class="col-md-2">
</div>

                           <div class="col-md-4" style="display: none;">
                            <label>$ FOB</label>
                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="fob" id="fob">
                          </div>

                           <div class="col-md-4">
                            <label>Com. Colimptex %</label>
                            <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="com_colim" id="com_colim">
                          </div>

                          <div class="col-md-4">

                                <label>Bon. Fabrica %</label>

                                     <input  type="text"  step=".0001" autocomplete="off" placeholder="" onkeyup="calc_rend()"  class="form-control" name="bon_fab" id="bon_fab">



                      </div>

                           <div class="col-md-2">
</div>
                  </div>
                </div>
                  <div class="form-group">
                            <div class="row">
                           <div class="col-md-12">
                     <button style="background-color:#38630f;color: #ffffff"; type="button" class="btn btn-white" onclick="regdetalles_pedidos()"><i class="fas fa-plus"></i> Agregar</button>
                          </div>
                         <div class="col-md-12">
                         <div id="resultado">
                             <?php
$servicios="";
                             if (isset($_GET['id'])) {
$costo_t=0;
$servicios="";
$i=0;
$pedido=$_GET['id'];
$consulta3="SELECT distinct `supplier`, nombre FROM `pedidos_items` inner join  clientes on id=supplier  where orden_pedido ='$pedido'";
$resultado3=$conexion-> query ($consulta3);
while ($row3=$resultado3->fetch_assoc()){
$provee1= $row3['supplier'];
$servicios.="<tr>

<td colspan='7' align=center height=25px>".$row3['nombre']."</td>
</tr>";

$consulta="SELECT `id_item`, `orden_pedido`, `descripcion`, `supplier`, `um`, `width`, `weight`, `meter`, `quantity`, `precio_compra`, `price_usd`, `fob`, `cap_hq`, `porc_ocup`, `seis_ocho`, `cent`, `cent_usd`, `com_colim`, `total_com`, `bon_fab`, `total_bon_fab`, `total_ganac`, `fecha_prod`, `remark`, nombre_ref, tela_etiqueta, tenido FROM `pedidos_items` inner join  referencias on id_ref=descripcion  where orden_pedido ='$pedido' and supplier='$provee1'";
$resultado2=$conexion-> query ($consulta);


while ($row1=$resultado2->fetch_assoc()){
$i=$i+1;



$costo= ($row1['quantity']  * $row1['price_usd']);
$porc_ocup=($row1['porc_ocup']);

$servicios.="<tr>
<td align=center height=25px>".$i."</td>
<td align=center height=25px >".$row1['nombre_ref']. "<br><span style='font-size:10px'>(".$row1['tela_etiqueta'].")</span>" ."</td>
<td align=center height=25px>".number_format($row1['quantity'], 2, ',', '.')."</td>
<td align=right height=25px>".number_format($porc_ocup, 2, ',', '.')."</td>
<td align=right height=25px>".number_format($row1['price_usd'], 2, ',', '.')."</td>
<td align=right height=25px>".number_format($costo, 2, ',', '.')."</td>
<td align=center height=25px><a onclick=elminar_det_pedido(".$row1['id_item'].") ><i class='fa fa-trash'></i></a></td>
</tr>";
$costo_t=@$costo_t+$costo;
$costo_P=@$costo_P+$costo;
$porc_ocup_t=@$porc_ocup_t+$porc_ocup;

}


$servicios.="<tr>
<td colspan=4 align=center height=30px style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5'>TOTAL PROVEEDOR</td>
<td colspan=3 align=left style='background-color:#333;color:#fff'>". number_format($costo_P, 2, ',', '.')."</td>



</tr>
<tr>
<td colspan=4 align=center height=30px style='background: rgba(226, 226, 226, 0.15); font-weight: bold; background-color:#333;color:#fff'>TOTAL % OCUPACION</td>
<td colspan=3 align=left style='background-color:#333;color:#fff'>". number_format($porc_ocup_t, 2, ',', '.')."</td>
</tr>";
$costo_P=0;
$i=0;


}
echo "<table width='100%' border='1' style='border-collapse: collapse;' class='table table-bordered table-condensed table-hover'>
<tr>
<td align=center width=5% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>#</td>
<td align=center width=45% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>REFERENCIA</td>
<td align=center width=10% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>CANT.</td>
<td align=center width=15% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>%/OCUP.</td>
<td align=center width=15% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>V/UNITARIO</td>
<td align=center width=15% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>FOB/TOTAL</td>
<td align=center width=15% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'><i class='fa fa-trash'></i></td>
</tr>
".$servicios."

<tr>
</tr>

<tr>

<td colspan=4 align=center height=30px style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#fff'>TOTAL ORDEN</td>
<td colspan=3 colspan=1 align=left style='background-color:#333;color:#fff'>". number_format($costo_t, 2, ',', '.')."</td>
</tr>
</table>";

}
                             ?>



                         </div> </div>
                      </div>
                  </div>


              </div>
              <div class="modal-footer">
                 <a href="pedidos.php"> <button type="button" class="btn btn-white">Cerrar</button></a>
                  <button type="submit" class="btn btn-primary">Guardar cambios</button>

              </div>

      </div>
    </div>
  </form>
</div>
<!-- MODAL -->
</div>
<?php include("script.php"); ?>

<script type="text/javascript">
<?php
if (isset($_GET['id'])) {
?>
$('#myModal').modal('show');
<?php                       }
?>



</script>
</body>
</html>
