<?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'];
$com_id=$_GET['com_id'];
$tot_costo=0;
$tot_venta=0;
$tot_utilidad=0;
$tot_can=0;

$registros=mysqli_query($con,"SELECT *, sum(d.combo_cantidad) as can, AVG(d.combo_precio) as pv from ventas v ,venta_detalle_combo d WHERE v.ven_fecha BETWEEN '$desde' AND '$hasta' and v.sucursal='$sucursal' and v.empresa='$empresa' and d.combo_id='$com_id' and d.ven_id=v.ven_id group by v.ven_id order by can desc") or die(mysqli_error($con));

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

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

$pdf->SetFillColor(235,150,200);
$pdf->SetFont('Arial','B',7);
$pdf->Cell(15,4,'CODIGO',1,0,'C',1);
$pdf->Cell(100,4,'NOMBRE',1,0,'C',1);
$pdf->Cell(15,4,'CANTIDAD',1,0,'C',1);
$pdf->Cell(20,4,'PRECIO COSTO',1,0,'C',1);
$pdf->Cell(20,4,'PRECIO VENTA',1,0,'C',1);
$pdf->Cell(20,4,'UTILIDAD',1,0,'C',1);
$pdf->Ln();    

$pdf->SetFont('Arial','',7);
$total_cantidad=0;
while ($row=mysqli_fetch_array($registros))
{
    $combo_id=$row['combo_id'];
    $combo_cantidad=$row['can'];
    $combo_costo=$row['combo_costo'];
    $combo_precio=$row['combo_precio']; 
    $tot_can+=$combo_cantidad;
  
    if ($combo_costo==null) 
    {
        $totalprecio_costo=0;
        $com=mysqli_query($con, "SELECT * from combos_articulos where combos_com_id='$combo_id'");
        while ($com1=mysqli_fetch_array($com))
        {
            $coma_art_id=$com1['coma_art_id'];
            $coma_art_cantidad=$com1['coma_art_cantidad'];
            $artc=mysqli_query($con, "SELECT * from articulos where art_id='$coma_art_id'");
            $row1=mysqli_fetch_array($artc);
            $totalprecio_costo+=$precio_costo=$row1['art_precio_costo']*$coma_art_cantidad;
        }    
    }
    else
    {
        $totalprecio_costo=$combo_costo;
    }     

    $costo_combo=$totalprecio_costo*$combo_cantidad;
    $venta=$combo_cantidad*$combo_precio;

    $uti=$venta-$costo_combo;
    $tot_costo+=$costo_combo;
    $tot_venta+=$venta; 
    $tot_utilidad+=$uti;
}

$com0=mysqli_query($con, "SELECT * from combos where com_id='$com_id'");
$com0=mysqli_fetch_array($com0);
$com_referencia=$com0['com_referencia'];
$com_nombre=$com0['com_nombre'];


$pdf->SetFillColor(255,255,255);
$pdf->Cell(15,4,$com_referencia,1,0,'C',1);
$pdf->Cell(100,4,$com_nombre,1,0,'C',1);
$pdf->Cell(15,4,number_format($tot_can,0),1,0,'C',1);
$pdf->Cell(20,4,number_format($tot_costo,0),1,0,'C',1);
$pdf->Cell(20,4,number_format($tot_venta,0),1,0,'C',1);
$pdf->Cell(20,4,number_format($tot_utilidad,0),1,0,'C',1);
$pdf->Ln();


$pdf->Output();
?>
