<?php
  include("conexion.php");


$num_orden=$_POST['num_orden'];
$referencia=$_POST['referencia'];
$supplier=$_POST['supplier'];
$um_ref=$_POST['um_ref'];
$ancho_ref=$_POST['ancho_ref'];
$peso_ref=$_POST['peso_ref'];
$rendi_ref=$_POST['rendi_ref'];
$cap_ref=$_POST['cap_ref'];
$porc_ocup=$_POST['porc_ocup'];
$quantity=$_POST['quantity'];
$tela_etiqueta=$_POST['tela_etiqueta'];
$tenido=$_POST['tenido'];


$quantity=str_replace(",","",$quantity);



$com_colim=$_POST['com_colim'];
$bon_fab=$_POST['bon_fab'];
$remark=$_POST['remark'];
$prec_venta=$_POST['prec_venta'];
$prec_comp=$_POST['prec_comp'];
$fob=$_POST['fob'];
$fecha_final=$_POST['fecha_final'];


$prec_venta=str_replace(",","",$prec_venta);

$prec_comp=str_replace(",","",$prec_comp);
//Pendientes meter, `seis_ocho`, `cent`, `cent_usd`, `total_bon_fab`, `total_ganac`,, `total_com`


 $query="INSERT INTO `pedidos_items`(`orden_pedido`, `descripcion`, `supplier`, `um`, `width`, `weight`, `quantity`, `precio_compra`, `price_usd`, `fob`, `cap_hq`, `porc_ocup`, `com_colim`, `bon_fab`, `fecha_prod`, `remark`, `meter`, `tela_etiqueta`, `tenido`) VALUES ('$num_orden','$referencia','$supplier','$um_ref','$ancho_ref','$peso_ref','$quantity','$prec_comp','$prec_venta','$fob','$cap_ref','$porc_ocup','$com_colim','$bon_fab','$fecha_final','$remark','$rendi_ref','$tela_etiqueta','$tenido')";
    $resultadoc=$conexion->query($query);

$costo_t=0;
$servicios="";
$i=0;
$pedido=$_POST['num_orden'];


$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='6' 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`,
                  `cap_ref`,
                  `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']);


      $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=center height=25px>".number_format(($row1['quantity'] * 100) / $row1['cap_ref'] , 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;
$costo_Porcentaje=$costo_Porcentaje+($row1['quantity'] * 100) / $row1['cap_ref'];

$costo_Porcentajetotal=$costo_Porcentajetotal+($row1['quantity'] * 100) / $row1['cap_ref'];
     }
 	 $servicios.="<tr>
<td colspan=5 align=center height=30px style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5'>TOTAL PROVEEDOR</td>
<td align=right style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5' >". number_format($costo_P, 2, ',', '.')."</td>
<td align=right></td>
</tr>
<tr>
<td colspan=5 align=center height=30px style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5'>TOTAL % OCUPACION</td>
<td align=right style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5' >". number_format($costo_Porcentaje, 2, ',', '.')."</td>
<td align=right></td>
</tr>";
$costo_P=0;
     $i=0;
     $costo_Porcentaje=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=10% 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 >
<td colspan=3 align=center height=30px style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5'>TOTAL % OCUPACION</td>
<td align=right style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5'>". number_format($costo_Porcentajetotal, 2, ',', '.')."</td>
<td align=center style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5'>TOTAL</td>
<td align=right style='background: rgba(226, 226, 226, 0.15); font-weight: bold;background-color:#333;color:#e2e4e5'>". number_format($costo_t, 2, ',', '.')."</td>
</tr>

</table>";
 // header("Location: cotizaciones.php")



?>
