<table cellspacing="0" style="width: 100%; padding-top: 5px;">
    <tr style="background-color:<?php echo $color; ?>">
        <td style="width: 10%;color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">REF</strong>
        </td>
        <td style="width: 5%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">CANT</strong>
        </td>
        <td style="width: 5%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">UM</strong>
        </td>
        <td style="width: 5%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">GTIA</strong>
        </td>
        <td style="width: 47%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">DESCRIPCIÓN</strong>
        </td>
        <td style="width: 8%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">PRECIO U</strong>
        </td>
        <td style="width: 6%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">IMP %</strong>
        </td>
        <td style="width: 5%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">DESC </strong>
        </td>
        <td style="width:9%; color:white;padding:2px;text-align:center"> 
            <strong style="font-size:10px;color: #000">TOTAL</strong>
        </td>
    </tr>

<?php
    $suma=0;
    $sql=mysqli_query($con,"
    SELECT 
        vd.vend_id AS id,
        vd.vend_referencia AS referencia,
        vd.vend_nombre AS nombre,
        vd.vend_unidad AS unidad,
        vd.vend_cantidad AS cantidad,
        vd.vend_precio AS precio,
        vd.vend_iva AS iva,
        vd.vend_desc AS descuento,
        'ART' AS tipo,
        vd.vend_art_id AS art_id
    FROM venta_detalle vd 
    WHERE vd.ventas_ven_id='$ven_id'

    UNION ALL

    SELECT 
        vdc.combo_id AS id,
        vdc.combo_referencia AS referencia,
        vdc.combo_nombre AS nombre,
        'COMB' AS unidad,
        vdc.combo_cantidad AS cantidad,
        vdc.combo_precio AS precio,
        vdc.combo_iva AS iva,
        vdc.combo_desc AS descuento,
        'COMBO' AS tipo,
        NULL AS art_id
    FROM venta_detalle_combo vdc 
    WHERE vdc.ven_id='$ven_id'
    
    LIMIT $limit_inf, $limit_sup
");

while ($row = mysqli_fetch_array($sql)) {
    $id = $row['id'];
    $art_id = $row['art_id'];
    $referencia = $row['referencia'];
    $nombre = $row['nombre'];
    $unidad = $row['unidad'];
    $cantidad = $row['cantidad'];
    $precio = $row['precio'];
    $iva = $row['iva'] > 0 ? $row['iva'] : 0;
    $descuento = $row['descuento'] > 0 ? $row['descuento'] : 0;
    $tipo = $row['tipo'];
    $suma+=$row['cantidad'];

    $iva0 = $iva / 100;
    $iva1 = 1 + $iva0;

    $precio_unit_sin_iva=$precio/$iva1;
    $precio_total_sin_iva=$precio_unit_sin_iva*$cantidad;
    
    $garantia = '';
    if ($tipo == 'ART') {
        $gar = mysqli_query($con, "SELECT * FROM articulos WHERE art_id='$art_id' AND empresa='$empresa'");
        $gar = mysqli_fetch_array($gar);
        $garantia = $gar['art_garantia'];
        $ref2 = $gar['art_referencia2'];
        // Añadir referencia secundaria si existe
        if ($ref2 != 'null') {
            $nombre .= ' - ' . $ref2;
        }
        // Sabores
        $sabores = mysqli_query($con, "SELECT sab_nombre FROM venta_detalle_sabores_productos WHERE vend_id='$id'");
        if (mysqli_num_rows($sabores) > 0) {
            $nombre .= ' (';
            while ($sab = mysqli_fetch_array($sabores)) {
                $nombre .= $sab['sab_nombre'] . '-';
            }
            $nombre = rtrim($nombre, '-') . ')';
        }
        // Seriales
        $seriales = mysqli_query($con, "SELECT ser_codigo FROM seriales WHERE venta_detalle_vend_id='$id'");
        if (mysqli_num_rows($seriales) > 0) {
            $nombre .= ' (- ';
            while ($ser = mysqli_fetch_array($seriales)) {
                $nombre .= $ser['ser_codigo'] . ' - ';
            }
            $nombre = rtrim($nombre, ' - ') . ')';
        }
    }?>
    <tr>
      <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo $referencia; ?></td>
      <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo $cantidad; ?></td>
      <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo $unidad; ?></td>
      <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo $garantia; ?></td>
      <td style="padding:2px;text-align:left;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo $nombre; ?></td>
      <td style="padding:2px;text-align:right;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo number_format($precio_unit_sin_iva); ?></td>
      <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo $iva; ?></td>
      <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo $descuento; ?></td>
      <td style="padding:2px;text-align:right;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"><?php echo number_format($precio_total_sin_iva); ?></td>
    </tr>
  
<?php   }   ?>
    <tr>
        <td style="padding:2px;text-align:right;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;font-size:8px">Total Unidades</td>
        <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;font-size:8px"><?php echo $suma; ?></td>
        <td colspan="2" style="padding:2px;text-align:right;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;font-size:8px"></td>
        <td style="padding:2px;text-align:center;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;font-size:8px">Total Items: <?php echo $total_items; ?></td>
        <td colspan="4" style="padding:2px;text-align:le;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;font-size:8px"></td>
    </tr>
</table>