<?php
session_start();
require('../fpdf/fpdf.php');
require('plantilla_general.php');
require '../../../conexion.php';
require_once('../../../include/sesiones.php');
$desde=$_GET['desde'];
$hasta=$_GET['hasta'];
$art_proveedor=$_GET['proveedor'];

$total_compras=0;
$iva_global=0;
$cargo_global=0;
$base=0;

if($art_proveedor<>'0')
{
    $registros=mysqli_query($con,"SELECT * FROM compras where proveedor_pro_id='$art_proveedor' and com_fecha BETWEEN '$desde' AND '$hasta' and sucursal='$sucursal' and empresa='$empresa' order by com_id desc") or die(mysqli_error($con));
}
else 
{
    $registros=mysqli_query($con,"SELECT * FROM compras where sucursal='$sucursal' and com_fecha BETWEEN '$desde' AND '$hasta' and empresa='$empresa'   order by com_id desc") or die(mysqli_error($con));
}

$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->AliasNbPages();
//Table with 20 rows and 4 columns

$pdf->Cell(0,10,'COMPRAS DESDE '.$desde. ' HASTA ' .$hasta,0,1);
    
$pdf->SetFillColor(235,225,215);
$pdf->SetFont('Arial','B',8);
$pdf->Cell(20,4,'N',1,0,'C',1);
$pdf->Cell(20,4,'FECHA',1,0,'C',1);
$pdf->Cell(80,4,'PROVEEDOR',1,0,'C',1);
$pdf->Cell(20,4,'BASE',1,0,'C',1);
$pdf->Cell(15,4,'IMP',1,0,'C',1);
$pdf->Cell(15,4,'CARGO',1,0,'C',1);
$pdf->Cell(20,4,'TOTAL',1,0,'C',1);
$pdf->Ln(); 


while ($reg=mysqli_fetch_array($registros))
{
    $com_id=$reg['com_id'];
    $sumador_total=0;
    $neto=0;
    $total_retencion=0;
    $total_iva=0;
    $total_cargo_adicional=0;
    $sql=mysqli_query($con, "SELECT * from compra_detalle where compras_com_id='$com_id' and empresa='$empresa'");
    while ($rew=mysqli_fetch_array($sql))
    {
        $comd_cantidad=$rew['comd_cantidad'];
        $comd_precio=$rew['comd_precio'];
        $comd_iva=$rew['comd_iva'];
        $precio_total=$comd_precio*$comd_cantidad;
        $iva=($precio_total*$comd_iva)/100;
        $neto+=$precio_total;//Sumador
        $comd_flete=$rew['comd_flete'];
        $cargo_adicional=$comd_flete*$comd_cantidad;
        $sumador_total+=$precio_total;//Sumador
        $total_iva+=$iva;//Sumador
        $total_cargo_adicional+=$cargo_adicional;
    }
    $sql=mysqli_query($con, "SELECT sum(coab_abono) as total from compra_abonos where compras_com_id='$com_id' and empresa='$empresa'");
    $sql1=mysqli_fetch_array($sql);
    $total=$sql1['total'];
    $saldo=$sumador_total-$total;

    $ret=mysqli_query($con,"SELECT * FROM retencion_compra where empresa='$empresa' and compras_com_id='$com_id'") or die(mysqli_error($con)); 
    if($rets=mysqli_fetch_array($ret)){
        $total_retencion=($ret_porcentaje=$rets['ret_porcentaje']/100)*$neto;
    }

    
    $pdf->SetFillColor(255,255,255);
    $pdf->SetFont('Arial','',8);
    $pdf->Cell(20,4,$reg['com_n_compra'],1,0,'C',1);
    $pdf->Cell(20,4,$reg['com_fecha'],1,0,'C',1);
    $pdf->Cell(80,4,utf8_decode($reg['proveedor_nombre']),1,0,'C',1);
    $pdf->Cell(20,4,number_format($neto,0),1,0,'R',1);
    $pdf->Cell(15,4,number_format($total_iva,0),1,0,'R',1);
    $pdf->Cell(15,4,number_format($total_cargo_adicional,0),1,0,'R',1);
    $pdf->Cell(20,4,number_format($sumador_total+$total_cargo_adicional-$total_retencion+$total_iva,0),1,0,'R',1);
    $pdf->Ln();

    $total_compras+=$sumador_total-$total_retencion;
    $iva_global+=$total_iva;
    $cargo_global+=$total_cargo_adicional;
    $base+=$neto;

}
$pdf->SetFillColor(239,215,025);
$pdf->SetFont('Arial','B',8);
$pdf->Cell(105,4,'',1,0,'C',1);
$pdf->Cell(15,4,'TOTAL',1,0,'C',1);
$pdf->Cell(20,4,number_format($base,0),1,0,'R',1);
$pdf->Cell(15,4,number_format($iva_global,0),1,0,'R',1);
$pdf->Cell(15,4,number_format($cargo_global,0),1,0,'R',1);
$pdf->Cell(20,4,number_format($base+$cargo_global+$iva_global,0),1,0,'R',1);
$pdf->Ln();
           
$pdf->Output();
?>