<?php
include("session.php");
             include("conexion.php");
                                             
$query="SELECT `orden_pedido`, id_item FROM `importado_items`";
    $resultadoc=$conexion->query($query);
   while($row1=$resultadoc->fetch_assoc())
   {

$pedido=$row1['orden_pedido'];
$id=$row1['id_item'];

$queryi="SELECT `id_pedido` FROM `importado` where id_pedido='$pedido'";
    $resultadoi=$conexion->query($queryi);
$rowi=$resultadoi->fetch_assoc();

if ($pedido!=$rowi['id_pedido']) {
 
 $querye="DELETE FROM `importado_items` WHERE `id_item`='$id'";
 $resultadoce=$conexion->query($querye);
}


}

?>
<!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">Buscar Orden</button>
                                   <?php 
                                            }
                                             ?>   
                                </div>

                                 <form action="importado.php" method="GET" name="buscaryear" id="buscaryear">
                                <div class="row">
                                  <div class="col-md-4">
                                <h5>Real Importado 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">
                                    <table class="table table-striped table-hover dataTables-example">
                                        <thead style="color: #1f51a3">
                                        <tr>

                                            <th>Cod. Int.</th>
                                            <th>Orden #</th>
                                            <th>Fecha Pedido Real</th>
                                            <th>Cliente</th>
                                            <th>Ciudad</th>
                                            <th>Telefono</th>
                                            <th>Forma de Pago</th>
                                            <th>Fecha Final</th>
                                            <th>Detalles</th>
                                            <th>Comercial Invoice</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 
                                
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 `importado` 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><?php echo strtoupper($rowc['ciudad']);?></td>
                                            <td><?php echo strtoupper($rowc['telefono']);?></td>

                                            <td><?php echo $rowc['forma_pago'];?></td>
                                            <td><?php echo $rowc['fecha_final'];?></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 Real Importado</h4>
                                  </div>
                                  <div class="modal-body">

                                         

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


                                                      ?>
                                                      <input type="hidden" name="numpedido_<?php echo $pedido ?>" id="numpedido_<?php echo $pedido ?>" value="<?php echo $pedido ?>">
                                                      <select onchange="buscar_det_item_real(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="" selected>Select</option>
                                                         <?php
                                    $query_items="SELECT id_item, nombre, descripcion, supplier FROM `importado_items` inner join clientes on id=supplier where orden_pedido ='$pedido' order by nombre asc";

$supliern="";
                                    $resultadoc_items=$conexion->query($query_items);
 while ($rowc_items=$resultadoc_items->fetch_assoc()) {                                       

$i=0;

$Total=0;
$pedidox=$rowc_items['id_item'];
$consulta_color="SELECT `id_pedidos_color`, `id_pedidos_items`, `color`, importado_items_color.`quantity` quantity, nombre_ref, descripcion, q_real FROM `importado_items` left join importado_items_color on id_item=id_pedidos_items inner join referencias on descripcion=id_ref where id_pedidos_items ='$pedidox'  order by color asc";

$resultado_color=$conexion-> query ($consulta_color);

$supliern2=$rowc_items['nombre'];



$consult_total="SELECT sum(importado_items_color.quantity) quantity FROM `importado_items` left join importado_items_color 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 "
<optgroup label='".$rowc_items['nombre']."'>";

}
     while ($row_color=$resultado_color->fetch_assoc()){
      $i=$i+1;

     echo "<option value=".$row_color['id_pedidos_color'].">".$i." ".$row_color['nombre_ref']." Color: ".$row_color['color'];
     }


if ($supliern2!=$supliern)
{
 echo "</optgroup>";

}
$supliern=$rowc_items['nombre'];
           }
               
                        ?>
                                                      </select><?php  //echo $consulta_color;?>
                                                    </div>


                                     </div>
                                
                                                  
                                               
                                                  <div class="row">
                                                    <div class="col-md-12">
                                                      <label style="margin-top: 20px;">Color Details <?php // echo $supliern2; ?></label>
                                                      </div>
                                                       </div>
                                                                                             <div class="row">
                                                    <div class="col-md-3">
                                                      <label>Color</label>
                                                      <input type="text" class="form-control" name="color_det_<?php echo $pedido ?>" id="color_det_<?php echo $pedido ?>">
                                                      <input type="hidden" class="form-control" name="id_pedidos_color_<?php echo $pedido ?>" id="id_pedidos_color_<?php echo $pedido ?>" >

                                                 </div>
                                                 <div class="col-md-3">
                                                      <label>Quantity Color</label>
                                                      <input type="text" class="form-control" name="qty_det_<?php echo $pedido ?>" id="qty_det_<?php echo $pedido ?>" readonly>

                                                 </div>
                                                 <div class="col-md-3">
                                                          <label>Quantity Real</label>
                                                          <input  type="text"  autocomplete="off" placeholder="" onkeyup="cambiar_<?php echo $pedido ?>()"  class="form-control" name="q_real_<?php echo $pedido ?>" id="q_real_<?php echo $pedido ?>">

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

variable = document.getElementById("q_real_<?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("q_real_<?php echo $pedido ?>").value=variable;

}


</script>
                                                        </div>
                                                   <div class="col-md-3">
                                                     <button type="button" style="margin-top: 18px" class="btn btn-white" onclick="regdetalles_color_real(<?php echo $pedido ?>)">Actualizar</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 `importado_items` inner join clientes on id=supplier where orden_pedido ='$pedido' order by nombre asc";
$supliern="";
                                    $resultadoc_items=$conexion->query($query_items);
 while ($rowc_items=$resultadoc_items->fetch_assoc()) {
                                                              

$i=0;

$Total=0;
$pedido=$rowc_items['id_item'];
$consulta_color="SELECT `id_pedidos_color`, `id_pedidos_items`, `color`, importado_items_color.`quantity` quantity, nombre_ref, descripcion, q_real FROM `importado_items_color` inner join importado_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(importado_items_color.quantity) quantity FROM `importado_items_color` inner join importado_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'];



//////real
$consult_total="SELECT sum(importado_items_color.q_real) quantity FROM `importado_items_color` inner join importado_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_real=$row_total['quantity'];






if ($supliern2!=$supliern)
{
 echo "
<tr>
<td colspan=6 > ".$rowc_items['nombre']." (".number_format($Total,2,".",",")." ". $rowc_items['um'].") Real: (".$Total_real." ". $rowc_items['um'].")</td>
</tr>
<tr>
<td align=center width=5%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>#</td>
<td align=center width=25%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>Ref</td>
<td align=center width=10%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>Color.</td>
<td align=center width=25%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>Qty</td>
<td align=center width=25%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>Qty Real</td>
<td align=center width=10%  style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'><i class='fa fa-trash'></i></td>
</tr>
";
 
}

$supliern=$rowc_items['nombre'];



     while ($row_color=$resultado_color->fetch_assoc()){
      $i=$i+1;

      $a=$row_color['quantity'];
$b=$row_color['q_real'];

$validar= ($b*100)/$a;
  $style="black";
  $icon="";
if (($validar>=110 || $validar<=90) && $validar!=0 ) {
  $style="green";
  $icon="<i class='fa fa-info'></i><sup>".number_format($validar,0,",",".")."%</sup>";

}
else
{
  if ($validar!=0) {
     $style="red";
  $icon="<i class='fa fa-info'></i><sup>".number_format($validar,0,",",".")."%</sup>";
 }
 else
 {
    $style="black";
 }

}

     echo "<tr>

<td align=center height=25px>".$i."</td>
<td align=center height=25px >".$row_color['nombre_ref']."</td>
<td align=center height=25px >".$row_color['color']."</td>
<td align=center height=25px>".number_format($row_color['quantity'],0,"",",")."</td>
<td align=center height=25px style='color:".$style."'>".number_format($row_color['q_real'],0,"",",")." " .$icon."</td>
<td align=center height=25px><a onclick=elminar_det_color_real(".$row_color['id_pedidos_color'].",".$pedido2.") ><i class='fa fa-trash'></i></a></td>
</tr>";


     }




     
                                                           }
                                                           ?>
</table>


                                             

                                                     </div>

                                                   </div>
                                                 </div>


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

                                




                                            <div class="modal-footer">
                                               <a href="importado.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>

                                              <a href="#" data-toggle="modal" data-target="#myModal_inv_<?php echo $rowc['id_pedido'] ?>"><i class="fa fa-file-pdf-o"></i></a>            

<!-- Modal-->
                                   <div class="modal inmodal" id="myModal_inv_<?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="20%" width="20%"> Generar Invoice </h4>
                                  </div>
                               

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

                                             <div class="form-group">
                               
                                        <div class="row">
                                                <div class="col-md-12">
                                                      <label>Port of Loading</label><br>
                                                     <div class="col-md-12">
                                                      <select class="form-control m-b chosen-select" name="final" id="final">

                                        <option style="width: 220px; margin-right: 5px" value="">Seleccione</option>
                                                           <?php
                                         $queryc_port="SELECT `id_port`, `nombre_port` FROM `Port_of_Loading`";
                                        $resultadoc_port=$conexion->query($queryc_port);
                                        while ($rowc_port=$resultadoc_port->fetch_assoc()) {
                                           
                                       echo "<option  value='".$rowc_port['nombre_port']."'>".$rowc_port['nombre_port'] ."</option>";
                                     
                                        };
                                        ?>
                                    </select>

                                                    </div>
                                                </div>
                                                </div>
                                                 <div class="row">
                                                <div class="col-md-12">
                                                      <label>Final Destinatión</label><br>
                                                     <div class="col-md-12">
                                                      <select class="form-control m-b chosen-select" name="final" id="final">

                                        <option style="width: 220px; margin-right: 5px" 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-12">
                                                      <label>Term of negotiation</label><br>
                                                     <div class="col-md-12">
                                                      <select class="form-control m-b chosen-select" name="final" id="final">

                                        <option style="width: 220px; margin-right: 5px" value="">Seleccione</option>
                                                           <?php
                                         $queryc_toneg="SELECT `id_toneg`, `nombre_toneg` FROM `term_of_negotiation`";
                                        $resultadoc_toneg=$conexion->query($queryc_toneg);
                                        while ($rowc_toneg=$resultadoc_toneg->fetch_assoc()) {
                                           
                                       echo "<option  value='".$rowc_toneg['nombre_toneg']."'>".$rowc_toneg['nombre_toneg'] ."</option>";
                                     
                                        };
                                        ?>
                                    </select>

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

                                             

                                                <div class="row">
                                                <div class="col-md-12">
                                                  <label>Payment</label><br>
                                              <select style="width: 220px; margin-right: 5px" class="form-control" 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-12">
                                                   <label>Packing</label><br>
                                                <select style="width: 220px; margin-right: 5px" class="form-control" 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 body-->

                                




                                            <div class="modal-footer">
                                               <button data-toggle="modal" data-target="#myModal_inv_<?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_inv_<?php echo $rowc['id_pedido'] ?>">Generar</button>
                                            
                                            </div>


</form>

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






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

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

                                            <?php
                                          }
                                          }
                                          ?>
                                            
                                            
                                        </tr>

                                         <?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_importado.php";
                                             }
                                             else
                                             {
                                                echo "reg_importado.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="importado.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%"> Registro de Real Importado</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_importado=0;
                                                if (isset($_GET['id'])) {
                                                    $id_importado=$_GET['id'];
                                                   
                                             }  

                                             



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

?>                                          

                                                                                             <div class="form-group">
                                                            <div class="row">
                                                               <div class="col-md-6">
                                                                <label>Fecha de Pedido Real</label>
                                                                <input type="date" name="fecha_pedido" id="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-6">
                                                            <label>Orden #</label>

                                                            <select class="form-control chosen-select" name="orden" id="orden" onchange="buscar_orden(this.value)">
                                        <option value="">Seleccione</option>

                                                              <?php
                                                            


                                              if (isset($_GET['id'])) {

                                                     $query_orden="SELECT importado.orden `orden` FROM `importado` order by  orden asc";
                                             }  
                                             else
                                             {
                                                $query_orden="SELECT pedidos.orden `orden` FROM `pedidos` WHERE NOT EXISTS (SELECT NULL FROM importado where importado.orden = pedidos.orden) order by orden asc" ;
                                             }
                                                                
      $resultado_orden=$conexion->query($query_orden);
                                                                      while ($row_orden=$resultado_orden->fetch_assoc()) {
                                                                      if ($row_orden['orden']==$rowx['orden']) {
                                                                          echo "<option selected value='".$row_orden['orden']."'>".$row_orden['orden'] ."</option>";
                                                                          }
                                                                          else
                                                                          {
                                                                            echo "<option value='".$row_orden['orden']."'>".$row_orden['orden'] ."</option>";
                                                                          }
                                                                      }
                                                              ?>
                                                            </select>
                                                             </div>
                                                                <div class="col-md-12">
                                                            <label>Nombre de Cliente</label>
                                                            <input type="hidden" name="id_pedido" id="id_pedido" value='<?php echo $id_pedido; ?>'>
                                                            <select class="form-control m-b chosen-select" name="cliente" id="cliente" onchange="buscar_nit(this.value)">

                                        <option 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 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" id="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" id="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" id="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: center; color: #1f51a3">
                                                         <label><h4>Detalle Real Importado</h4></label>
                                                        </div>
                                                    </div>
                                                </div>



                                                <?php 
                                                $temporal=rand(5, 17958);
                                                ?>
                                      <div class="form-group">
                                                          <div class="row">
                                                         <div style="display: block" class="col-md-6">

                                                            
                                                            <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="<?php if (isset($_GET['id'])) { echo "buscar_ref_pedido_2(this.value)"; } else { echo "buscar_ref_pedido(this.value)";} ?>">

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

                                                            if (isset($_GET['id'])) {
                                                              $ordenped=$_GET['id'];
 
                                         $queryc="SELECT `id_item`, `orden_pedido`, `supplier`, nombre_ref FROM `importado_items` inner join referencias on descripcion=id_ref where orden_pedido='$ordenped' order by id_item asc";
                                        $resultadoc=$conexion->query($queryc);
$i=0;
                                        while ($rowc=$resultadoc->fetch_assoc()) {
                                            $i=$i+1;
                                      echo "<option value='".$rowc['id_item']."'>".$i." - ".$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-12">
                                                            <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_rend()"  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="" onkeyup="calc_rend()"  class="form-control" name="prec_comp" id="prec_comp">
                                                        </div>
                                                         <div class="col-md-4">
                                                          <label>$ Venta</label>
                                                          <input  type="text"  autocomplete="off" placeholder="" onkeyup="calc_rend()"  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-3" 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="number"  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="number"  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 type="button" class="btn btn-white" onclick="<?php if (isset($_GET['id'])) { echo "actdetalles_pedidos_real2()"; } else { echo "actdetalles_pedidos_real()";} ?>">Actualizar</button>
                                                        </div>
                                                       <div class="col-md-12">
                                                       <div id="resultado">
                                                           <?php

                                                           if (isset($_GET['id'])) {
                                                              
$costo_t=0;
$servicios="";
$i=0;
$pedido=$_GET['id'];
$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 FROM `importado_items` inner join  referencias on id_ref=descripcion where orden_pedido ='$pedido' order by id_item asc";
$resultado2=$conexion-> query ($consulta);


     while ($row1=$resultado2->fetch_assoc()){

$id_item=$row1['id_item'];


///buscando cantidad en item color
$consulta_color="SELECT sum(q_real) q_real FROM `importado_items_color` where id_pedidos_items='$id_item'";
$resultado_color=$conexion-> query ($consulta_color);
$row_color=$resultado_color->fetch_assoc();
$total_qty_real=$row_color['q_real'];

if (($row_color['q_real']>0 && $row_color['q_real']!="") && ($row_color['q_real']>$row1['quantity'] || $row_color['q_real']<$row1['quantity'] )) {

$row1['quantity']=$row_color['q_real']." *";

}
////buscando


      $i=$i+1;
      
      $provee1= $row1['supplier'];

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

    
      $servicios.="<tr>

<td align=center height=25px>".$i."</td>
<td align=center height=25px >".$row1['nombre_ref']."</td>
<td align=center height=25px>".number_format($row1['quantity'], 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_real_2(".$row1['id_item'].") ><i class='fa fa-trash'></i></a></td>
</tr>";
$costo_t=$costo_t+$costo;


     }
     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=35% height=30px style='background: rgba(145, 140, 142, 0.15); font-weight: bold;'>REFERENCIA</td>
<td align=center width=20% 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;'>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>
<td colspan=4 rowspan=3 align=left valign=top ></td>
<td align=center height=30px style='background: rgba(226, 226, 226, 0.15); font-weight: bold;'>TOTAL</td>
<td colspan=2 align=right>". number_format($costo_t, 2, ',', '.')."</td>
</tr>

</table>";
                                                           }


                                                           ?>



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

                                            </div>
                                            <div class="modal-footer">
                                               <a href="importado.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>