<?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'];
$vendedores=$_GET['vendedores'];
$estado=$_GET['estado'];


$pdf=new PDF_MC_Table('L','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->AliasNbPages();


$pdf->Cell(0,10,'DESDE '.$desde. ' HASTA ' .$hasta,0,1);

$pdf->SetFillColor(235,225,215);
$pdf->SetFont('Arial','B',6);
$pdf->Cell(15,4,'REF',1,0,'C',1);
$pdf->Cell(80,4,'NOMBRE',1,0,'C',1);
$pdf->Cell(25,4,'PROVEEDOR',1,0,'C',1);
$pdf->Cell(15,4,'CANT',1,0,'C',1);
$pdf->Cell(15,4,'UNI',1,0,'C',1);
$pdf->Cell(15,4,'STOCK',1,0,'C',1);
$pdf->Cell(15,4,'FALTANTES',1,0,'C',1);
$pdf->Cell(20,4,'COSTO FALTANTES',1,0,'C',1);
$pdf->Cell(20,4,'COSTO U',1,0,'C',1);
$pdf->Cell(20,4,'COSTO T',1,0,'C',1);
$pdf->Cell(20,4,'VENTA U',1,0,'C',1);
$pdf->Cell(20,4,'VENTA T',1,0,'C',1);
$pdf->Ln();    


$global_costo=0;
$global_venta=0;
$global_faltante=0;

$suma_articulos=0;

$sql1p=mysqli_query($con, "SELECT * from proveedor where empresa='$empresa' order by pro_nombre asc");
while ($cosp=mysqli_fetch_array($sql1p))
{
    $pro_id=$cosp['pro_id'];
    $pro_nombre=$cosp['pro_nombre'];

    $costo_t=0;
    $venta_t=0;
    $faltante_t=0;
   
    $sql=mysqli_query($con, "SELECT *, sum(cd.cotd_cantidad) as can, AVG(cd.cotd_precio) as pv from cotizacion c, cotizacion_detalle cd,articulos a WHERE c.cot_fecha BETWEEN '$desde' AND '$hasta' and c.sucursal='$sucursal' and c.empresa='$empresa' and c.cot_id=cd.cotizacion_cot_id  and a.proveedor_pro_id='$pro_id' and a.art_id=cd.cotd_art_id and cot_estado='$estado' and c.vendedor IN('".$vendedores."') group by cd.art_id_bod, cd.cotd_precio, cd.cotd_unidad order by a.art_nombre asc ");

    while ($row=mysqli_fetch_array($sql))
    {
        $cotd_art_id=$row['cotd_art_id'];
        $art_id_bod=$row['art_id_bod'];
        $cantidad=$row['can'];
        $venta=$row['pv'];
        $venta_item=$cantidad*$venta;
        $art_nombre=$row['cotd_nombre'];
        $unidad=$row['cotd_unidad'];

        $venta_t+=$venta_item;

        $sql1=mysqli_query($con, "SELECT * from articulos where art_id='$cotd_art_id' and empresa='$empresa' ");
        $cos=mysqli_fetch_array($sql1);
        $art_referencia=$cos['art_referencia'];
        $art_costo=$cos['art_precio_costo'];
        $total_costo=$art_costo*$cantidad;
        $costo_t+=$total_costo;

        $artb=mysqli_query($con, "SELECT * from articulos_has_bodegas where art_id='$art_id_bod' ");
        $artbo=mysqli_fetch_array($artb);
        $art_cantidad=$artbo['art_cantidad'];

        $suma_articulos+=$cantidad;

        if($art_cantidad>$cantidad)
        {
            $faltante=0;
        } else 
        {
            $faltante=$cantidad-$art_cantidad;
        }

        $costo_faltante=$faltante*$art_costo;
        $faltante_t+=$costo_faltante;

        $pdf->SetWidths(array(15,80,25,15,15,15,15,20,20,20,20,20));
        srand(time()*1000000);
        $pdf->Row(array($art_referencia,utf8_decode($art_nombre),utf8_decode($pro_nombre),$cantidad,$unidad,$art_cantidad,$faltante,number_format($costo_faltante,0), number_format($art_costo,0),number_format($total_costo,0),number_format($venta,0),number_format($venta_item,0)));
    }
    if( $venta_t<>0)
    {
        $pdf->SetFillColor(135,245,115);
        $pdf->SetFont('Arial','B',7);
        $pdf->Cell(120,4,'TOTAL',1,0,'C',1);
        $pdf->Cell(15,4,'',1,0,'C',1);
        $pdf->Cell(15,4,'',1,0,'C',1);
        $pdf->Cell(15,4,'',1,0,'C',1);
        $pdf->Cell(15,4,'',1,0,'C',1);
        $pdf->Cell(20,4,number_format($faltante_t,0),1,0,'R',1);
        $pdf->Cell(20,4,'',1,0,'C',1);
        $pdf->Cell(20,4,number_format($costo_t,0),1,0,'R',1);
        $pdf->Cell(20,4,'',1,0,'C',1);
        $pdf->Cell(20,4,number_format($venta_t,0),1,0,'R',1);
        $pdf->Ln(); 
    }
$global_faltante+=$faltante_t;
$global_costo+=$costo_t;
$global_venta+=$venta_t;
}  

$pdf->SetFillColor(255,255,0);
$pdf->SetFont('Arial','B',7);
$pdf->Cell(120,4,'TOTAL GENERAL',1,0,'C',1);
$pdf->Cell(15,4,$suma_articulos,1,0,'C',1);
$pdf->Cell(15,4,'',1,0,'C',1);
$pdf->Cell(15,4,'',1,0,'C',1);
$pdf->Cell(15,4,'',1,0,'C',1);
$pdf->Cell(20,4,number_format($global_faltante,0),1,0,'R',1);
$pdf->Cell(20,4,'',1,0,'C',1);
$pdf->Cell(20,4,number_format($global_costo,0),1,0,'R',1);
$pdf->Cell(20,4,'',1,0,'C',1);
$pdf->Cell(20,4,number_format($global_venta,0),1,0,'R',1);
$pdf->Ln();     
$pdf->Output();
?>
