<?php
	$pdf->SetFillColor(215,235,2415);
	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(20,4,$roww['ven_prefijo'].$roww['ven_n_venta'],1,0,'C',1);
	$pdf->Cell(95,4,utf8_decode($roww['ven_nombre']),1,0,'C',1);
	$pdf->Cell(50,4,'Doc: '.$roww['ven_cc'],1,0,'C',1);
	$pdf->Cell(25,4,$roww['ven_forma_de_pago'],1,0,'C',1);
	$pdf->Ln();
	$pdf->SetFillColor(235,225,215);
	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(20,4,'REF',1,0,'C',1);
	$pdf->Cell(85,4,'NOMBRE',1,0,'C',1);
	$pdf->Cell(10,4,'UM',1,0,'C',1);
	$pdf->Cell(15,4,'CANT',1,0,'C',1);
	$pdf->Cell(20,4,'VALOR U',1,0,'C',1);
	$pdf->Cell(15,4,'DESC %',1,0,'C',1);
	$pdf->Cell(25,4,'TOTAL',1,0,'C',1);
	$pdf->Ln(); 

	$total_retencion=0;
    $ret=mysqli_query($con,"SELECT SUM(ret_importe) as ret_importe FROM retencion_venta where empresa='$empresa' and ventas_ven_id='$ven_id'") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
    $total_retencion=$rets['ret_importe'];


	$subtotal_art=0;
	$descuento_art=0;
	$iva_art=0;

	$sql=mysqli_query($con, "SELECT * from venta_detalle where ventas_ven_id='$ven_id'");
	while ($row=mysqli_fetch_array($sql))
	{
	    $vend_referencia=$row['vend_referencia'];
	    $vend_nombre=$row['vend_nombre'];
	    $vend_unidad=$row['vend_unidad'];
	    $vend_cantidad=$row['vend_cantidad'];
	    $vend_precio=$row['vend_precio'];
	    if ($row['vend_iva']>0){ $vend_iva=$row['vend_iva']; } else { $vend_iva='0';}
        if ($row['vend_desc']>0){ $vend_descuento=$row['vend_desc']; } else { $vend_descuento='0';}
        $iva0=$vend_iva/100; //0.19
    	$iva1=1+$iva0; //1.19

    	$precio_total=$vend_precio*$vend_cantidad;
	    $precio_total_sin_iva_und=($vend_precio/$iva1); //precio sin iva  840.336 unidad

	    $precio_total_sin_iva_item=($vend_precio/$iva1)*$vend_cantidad; //precio sin iva  840.336
	    $descuento_item=($vend_descuento/100)*$precio_total_sin_iva_item; //descuento_item 84.033
	    $total_iva_item=($precio_total_sin_iva_item-$descuento_item)*$iva0; // total iva  159.336

		$subtotal_art+=$precio_total_sin_iva_item;
		$descuento_art+=$descuento_item;
		$iva_art+=$total_iva_item;

	    $pdf->SetWidths(array(20,85,10,15,20,15,25));
	    srand(time()*1000000);
	    $pdf->Row(array($vend_referencia,utf8_decode($vend_nombre),utf8_decode($vend_unidad),$vend_cantidad,number_format($vend_precio,0),$vend_descuento,number_format($precio_total,0)));
	}

	$subtotal_com=0;
	$descuento_com=0;
	$iva_com=0;
	$sql=mysqli_query($con, "SELECT * from venta_detalle_combo where ven_id='$ven_id'");
	while ($row1=mysqli_fetch_array($sql))
	{
	    $combo_referencia=$row1["combo_referencia"];
	    $combo_nombre=$row1['combo_nombre'];       
	    $combo_cantidad=$row1["combo_cantidad"];
	    $combo_precio=$row1['combo_precio'];
	    if ($row1['combo_iva']>0){ $combo_iva=$row1['combo_iva']; } else { $combo_iva='0';}
        if ($row1['combo_desc']>0){ $combo_desc=$row1['combo_desc']; } else { $combo_desc='0';}
        $com_iva0=$combo_iva/100; //0.19
    	$com_iva1=1+$com_iva0; //1.19

    	$precio_total_sin_iva_und_com=($combo_precio/$com_iva1); //precio sin iva  840.336 unidad

	    $precio_total_sin_iva_item_com=($combo_precio/$com_iva1)*$combo_cantidad; //precio sin iva  840.336
	    $descuento_item_com=($combo_desc/100)*$precio_total_sin_iva_item_com; //descuento_item 84.033
	    $total_iva_item_com=($precio_total_sin_iva_item_com-$descuento_item_com)*$com_iva0; // total iva  159.336

		$subtotal_com+=$precio_total_sin_iva_item_com;
		$descuento_com+=$descuento_item_com;
		$iva_com+=$total_iva_item_com;


	    $pdf->SetWidths(array(20,85,10,15,20,15,25));
	    srand(time()*1000000);
	    $pdf->Row(array($combo_referencia,utf8_decode($combo_nombre),'Comb',$combo_cantidad,number_format($precio_total_sin_iva_und_com,0),$combo_desc,number_format($precio_total_sin_iva_item_com,0)));
	}

	$subtotal_venta=$subtotal_art+$subtotal_com;
	$descuento_venta=$descuento_art+$descuento_com;
	$iva_venta=$iva_art+$iva_com;
	$total_venta=($subtotal_venta-$descuento_venta-$total_retencion)+$iva_venta;

	$pdf->SetFillColor(255,255,255);
	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(165,4,'Subtotal',1,0,'R',1);
	$pdf->Cell(25,4,number_format($subtotal_venta,0),1,0,'C',1);
	$pdf->Ln();
	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(165,4,'Descuento',1,0,'R',1);
	$pdf->Cell(25,4,number_format($descuento_venta,0),1,0,'C',1);
	$pdf->Ln();
	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(165,4,'Retencion',1,0,'R',1);
	$pdf->Cell(25,4,number_format($total_retencion,0),1,0,'C',1);
	$pdf->Ln();
	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(165,4,'Impuesto',1,0,'R',1);
	$pdf->Cell(25,4,number_format($iva_venta,0),1,0,'C',1);
	$pdf->Ln();
	$pdf->SetFillColor(135,245,115);
	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(150,4, $roww['ven_fecha'] .$roww['ven_hora'],1,0,'C',1);
	$pdf->Cell(15,4,'TOTAL',1,0,'C',1);
	$pdf->Cell(25,4,number_format($total_venta,0),1,0,'C',1);
	$pdf->Ln();
	$total+=($total_venta);
?>