<?php
include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
require_once("DF80000.php");
$a=0;

class PDF extends FPDF{
//Cabecera de página
	function Header(){
	    //Logo
	         include("DYIM_VER.php");
	    //Arial bold 14
		$this->SetFont('Arial','B',13);
		//Título
		$this->Cell(205,5,'RESUMEN DE SALIDAS POR DEPARTAMENTOS',0,1,'C',0);
		//Salto de línea
		$this->SetFont('Arial','I',12);
		$this->Cell(55,5,'Periodo: '.$_POST['fec'].' al '.$_POST['fec2'].'',0,0,'C',0);
		
		$this->Ln(8);
		$this->SetFont('Arial','B',8);//establece tamaño de letra
		$this->SetFillColor(192,192,192);
		$this->Cell(1,5,'',0,0,'C',0);
		$this->Cell(50,5,'Departamento',1,0,'C',0);
		$this->Cell(20,5,'Alim',1,0,'C',0);
		$this->Cell(20,5,'Bebidas',1,0,'C',0);
		$this->Cell(18,5,'Sumin',1,0,'C',0);
		$this->cell(18,5,'directos',1,0,'C',0);
		$this->cell(18,5,'Sum Hdpds',1,0,'C',0);
		$this->cell(18,5,'SubTo',1,0,'C',0);
		$this->cell(18,5,'Iva',1,0,'C',0);
		$this->Cell(18,5,'Total',1,1,'C',0);
			
	}	
	//Pie de página
	function Footer()
	{
	//Posición: a 1,5 cm del final
	$this->SetY(-15);
	//Arial italic 8
	$this->SetFont('Arial','I',8);
	//Número de página
	$this->Cell(0,10, $this->PageNo(),0,0,'C');
	}
}

$pdf=new PDF('P','mm','Letter');
$pdf->AddPage();

$pdf->SetFillColor(192,192,192);

$fec = $_POST['fec'];
    include('DF20000.php');
    $fecha_inicial = $f_m;
    $fec = $_POST['fec2'];
    include('DF20000.php');
    $fecha_final = $f_m;
@$sql = mysqli_query($con ,"SELECT fdepartamento.nparte1, fdepartamento.np_desc1 AS departamento FROM dgrequie
INNER JOIN dgrequid ON dgrequie.fol_ven = dgrequid.cons_ven
INNER JOIN fdepartamento ON dgrequie.tip_ven = fdepartamento.nparte1
WHERE dgrequie.fec_ven between '$fecha_inicial' and '$fecha_final' AND dgrequie.cancelado = 0
GROUP BY dgrequie.tip_ven
ORDER BY dgrequie.tip_ven;");
$a = 0;
$enc = "";
$pdf->SetFont('Arial', '', 8);
$totIVA = 0;
$totTOTAL = 0;
$totSUB = 0;
$totIVA = 0;
$totTOT = 0;
$totSUBALI = 0;
$totSUBBEB = 0;
$totSUBSUM = 0;
$totSUBDIR = 0;
$totSUBSUMHDPS = 0;
$ptot = false;
while ($row = mysqli_fetch_array($sql)) {
    $a++;

    $pdf->SetFont('Arial', 'B', 8);
	if($a > 1)
		$pdf->Cell(195, 0.3, '', 0, 1, 'L', 1);
    $pdf->Cell(2, 5, '', 0, 0, 'C', 0);
    // $row['fol_ven'
    $fec1 = $row["fec_ven"];
    include('DF20000.php');
	
	$alimentos=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(dgrequid.subtotal) AS SUBTOTAL, SUM(dgrequid.iva_pre) AS IVA , SUM(dgrequid.total_cd) AS TOTAL FROM dgrequid
				INNER JOIN dgrequie ON dgrequid.cons_ven = dgrequie.fol_ven
				INNER JOIN fdepartamento ON dgrequie.tip_ven = fdepartamento.nparte1
				INNER JOIN farticulos on farticulos.nparte1=dgrequid.nparte1
				INNER JOIN falmacen ON farticulos.sub = falmacen.nparte1
				where dgrequie.fec_ven between '$fecha_inicial' and '$fecha_final' 
				AND (falmacen.np_desc1 = 'ABARROTES' OR falmacen.np_desc1 = 'COCINA' OR falmacen.np_desc1 = 'CARNES' OR falmacen.np_desc1 = 'PRODUCTOS LACTEOS')
				AND fdepartamento.nparte1 = ".$row["nparte1"]."
				GROUP BY fdepartamento.nparte1")); 
	$bebidas=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(dgrequid.subtotal) AS SUBTOTAL, SUM(dgrequid.iva_pre) AS IVA , SUM(dgrequid.total_cd) AS TOTAL FROM dgrequid
				INNER JOIN dgrequie ON dgrequid.cons_ven = dgrequie.fol_ven
				INNER JOIN fdepartamento ON dgrequie.tip_ven = fdepartamento.nparte1
				INNER JOIN farticulos on farticulos.nparte1=dgrequid.nparte1
				INNER JOIN falmacen ON farticulos.sub = falmacen.nparte1
				where dgrequie.fec_ven between '$fecha_inicial' and '$fecha_final' 
				AND (falmacen.np_desc1 = 'BEBIDAS' OR falmacen.np_desc1 = 'CERVEZAS' OR falmacen.np_desc1 = 'REFRESCOS' OR falmacen.np_desc1 = 'TABAQUERIA')
				AND fdepartamento.nparte1 = ".$row["nparte1"]."
				GROUP BY fdepartamento.nparte1")); 
				
	$suministros=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(dgrequid.subtotal) AS SUBTOTAL, SUM(dgrequid.iva_pre) AS IVA , SUM(dgrequid.total_cd) AS TOTAL FROM dgrequid
				INNER JOIN dgrequie ON dgrequid.cons_ven = dgrequie.fol_ven
				INNER JOIN fdepartamento ON dgrequie.tip_ven = fdepartamento.nparte1
				INNER JOIN farticulos on farticulos.nparte1=dgrequid.nparte1
				INNER JOIN falmacen ON farticulos.sub = falmacen.nparte1
				where dgrequie.fec_ven between '$fecha_inicial' and '$fecha_final' 
				AND (falmacen.np_desc1 = 'SUMINISTROS' OR falmacen.np_desc1 = 'ARTICULOS DE LIMPIEZA')
				AND fdepartamento.nparte1 = ".$row["nparte1"]."
				GROUP BY fdepartamento.nparte1")); 
	/*
	$directos=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(dgrequid.subtotal) AS SUBTOTAL, SUM(dgrequid.iva_pre) AS IVA , SUM(dgrequid.total_cd) AS TOTAL FROM dgrequie
				INNER JOIN fdepartamento ON dgrequie.tip_ven = fdepartamento.nparte1
				INNER JOIN dgrequid ON dgrequie.fol_ven = dgrequid.cons_ven
				INNER JOIN farticulos on farticulos.nparte1=dgrequid.nparte1
				INNER JOIN falmacen ON farticulos.sub = falmacen.nparte1
				where dgrequie.fec_ven between '$fecha_inicial' and '$fecha_final'
				AND (falmacen.np_desc1 = 'FRUTAS Y VERDURAS' OR falmacen.np_desc1 = 'TORTILLAS' OR falmacen.np_desc1 = 'AGUA PURIFICADA' OR falmacen.np_desc1 = 'HIELO')
				AND fdepartamento.nparte1 = ".$row["nparte1"]."
				GROUP BY fdepartamento.nparte1")); 
	*/
				
	$sumhpds=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(dgrequid.subtotal) AS SUBTOTAL, SUM(dgrequid.iva_pre) AS IVA , SUM(dgrequid.total_cd) AS TOTAL  FROM dgrequid
				INNER JOIN dgrequie ON dgrequid.cons_ven = dgrequie.fol_ven
				INNER JOIN fdepartamento ON dgrequie.tip_ven = fdepartamento.nparte1
				INNER JOIN farticulos on farticulos.nparte1=dgrequid.nparte1
				INNER JOIN falmacen ON farticulos.sub = falmacen.nparte1
				where dgrequie.fec_ven between '$fecha_inicial' and '$fecha_final' 
				AND (falmacen.np_desc1 = 'SUMINISTROS HUESPEDES' OR falmacen.np_desc1 = 'SUMINISTROS A Y B')
				AND fdepartamento.nparte1 = ".$row["nparte1"]."
				GROUP BY fdepartamento.nparte1"));  
				
	$subtotal = $alimentos["SUBTOTAL"]+$bebidas["SUBTOTAL"]+$suministros["SUBTOTAL"]+$sumhpds["SUBTOTAL"];
	$iva = $alimentos["IVA"]+$bebidas["IVA"]+$suministros["IVA"]+$sumhpds["IVA"];
	$total = $alimentos["TOTAL"]+$bebidas["TOTAL"]+$suministros["TOTAL"]+$sumhpds["TOTAL"];
	
    $pdf->Cell(50, 5, $row["departamento"], 10, 0, 'L', 0);
    $pdf->Cell(20, 5, '$ ' . number_format($alimentos["SUBTOTAL"], 2, '.', ','), 10, 0, 'L', 0);
	$pdf->Cell(20, 5, '$ ' . number_format($bebidas["SUBTOTAL"], 2, '.', ','), 10, 0, 'L', 0);
	$pdf->Cell(18, 5, '$ ' . number_format($suministros["SUBTOTAL"], 2, '.', ','), 10, 0, 'L', 0);
	$pdf->Cell(18, 5, '$ ' . number_format(0, 2, '.', ','), 10, 0, 'L', 0);
	$pdf->Cell(18, 5, '$ ' . number_format($sumhpds["SUBTOTAL"], 2, '.', ','), 10, 0, 'L', 0);
	$pdf->Cell(20, 5, '$ ' . number_format($subtotal, 2, '.', ','), 10, 0, 'L', 0);
	$pdf->Cell(18, 5, '$ ' . number_format($iva, 2, '.', ','), 10, 0, 'L', 0);
	$pdf->Cell(18, 5, '$ ' . number_format($total, 2, '.', ','), 10, 1, 'L', 0);
	$totSUBALI += $alimentos["SUBTOTAL"];
	$totSUBBEB += $bebidas["SUBTOTAL"];
	$totSUBSUM += $suministros["SUBTOTAL"];
	$totSUBSUMHDPS += $sumhpds["SUBTOTAL"];
	$totSUB += $subtotal;
	$totIVA += $iva;
	$totTOT += $total;
    if ($ptot) {
   
        $ptot = false;
    } 	 

}
//Directos 
$directos=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(dgrequid.subtotal) AS SUBTOTAL, SUM(dgrequid.iva_pre) AS IVA , SUM(dgrequid.total_cd) AS TOTAL FROM dgrequid  INNER JOIN farticulos on farticulos.nparte1=dgrequid.nparte1
		INNER JOIN falmacen ON farticulos.sub = falmacen.nparte1
INNER JOIN dgrequie ON dgrequid.cons_ven = dgrequie.fol_ven
		where dgrequie.fec_ven between '$fecha_inicial' and '$fecha_final' 
		AND (falmacen.np_desc1 = 'FRUTAS Y VERDURAS' OR falmacen.np_desc1 = 'TORTILLAS' OR falmacen.np_desc1 = 'AGUA PURIFICADA' OR falmacen.np_desc1 = 'HIELO' OR falmacen.np_desc1 = 'HELADOS')")); 
$pdf->Cell(195, 0.3, '', 0, 1, 'L', 1);
$pdf->Cell(2, 5, '', 0, 0, 'C', 0);
$pdf->Cell(50, 5, 'DIRECTOS', 10, 0, 'L', 0);
$pdf->Cell(20, 5, '$ ' . number_format(0, 2, '.', ','), 10, 0, 'L', 0);
$pdf->Cell(20, 5, '$ ' . number_format(0, 2, '.', ','), 10, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format(0, 2, '.', ','), 10, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($directos["SUBTOTAL"], 2, '.', ','), 10, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format(0, 2, '.', ','), 10, 0, 'L', 0);
$pdf->Cell(20, 5, '$ ' . number_format($directos["SUBTOTAL"], 2, '.', ','), 10, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($directos["IVA"], 2, '.', ','), 10, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($directos["TOTAL"], 2, '.', ','), 10, 1, 'L', 0);
$totSUBDIR += $directos["SUBTOTAL"];
$totSUB += $directos["SUBTOTAL"];
$totIVA += $directos["IVA"];
$totTOT += $directos["TOTAL"];
//*************************************************************************

$pdf->Cell(1, 5, '', 0, 1, 'R', 0);

$pdf->Cell(10, 5, '', 0, 0, 'R', 0);
$pdf->Cell(40, 5, 'Totales', 1, 0, 'R', 0);
$pdf->Cell(20, 5, '$ ' . number_format($totSUBALI, 2, '.', ','), 1, 0, 'L', 0);
$pdf->Cell(20, 5, '$ ' . number_format($totSUBBEB, 2, '.', ','), 1, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($totSUBSUM, 2, '.', ','), 1, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($totSUBDIR, 2, '.', ','), 1, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($totSUBSUMHDPS, 2, '.', ','), 1, 0, 'L', 0);
$pdf->Cell(20, 5, '$ ' . number_format($totSUB, 2, '.', ','), 1, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($totIVA, 2, '.', ','), 1, 0, 'L', 0);
$pdf->Cell(18, 5, '$ ' . number_format($totTOT, 2, '.', ','), 1, 1, 'L', 0);
$pdf->Output("Salidas_Directas.pdf","D");   
