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


$id=$_POST['id'];
$pedido2=$_POST['id_pedido'];



 $query="DELETE FROM `pedidos_items_color` WHERE `id_pedidos_color`='$id'";
    $resultadoc=$conexion->query($query);


 echo "<table width='100%' border='1' style='border-collapse: collapse;' class='table table-bordered table-condensed table-hover'>";
      

                                                    
                                    $query_items="SELECT id_item, nombre, descripcion, supplier, um FROM `pedidos_items` inner join clientes on id=supplier where orden_pedido ='$pedido2' 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'";
$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 href='imprimir_detalle.php?id_supplier=".$rowc_items['supplier']."&id=".$pedido2."' target='_blank'><i class='fa fa-file-pdf-o'></i></a> Purchase Order to ".$rowc_items['nombre']."  - <a class='pull-right' href='imprimir_detalle_etiquetas.php?id_supplier=".$rowc_items['supplier']."&id=".$pedido2."' target='_blank'><i class='fa fa-file-file-o'></i> Inst. Empaques</a> </td>
</tr>
<tr>
<td 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;'><i class='fa fa-trash'></i></td>
</tr>
";
$i=0;
 
}

$supliern=$rowc_items['nombre'];
$supliern2="";
$refn="";


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

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

    $supliern=$rowc_items['nombre'];
   $i=$i+1;


 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><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><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'];

     }

     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><tr>";

}

$supliern=$rowc_items['nombre'];



     
                                                           }
echo "</table>";

?>