<?php

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

class PDF extends FPDF {

    //Cabecera de Página
    function Header() {
        //Logotipo
        include("DYIM_VER.php");
        //Arial Bold (14)
        $this->SetFont('Arial', 'B', 12);
        //Espacio
        //Título
        if ($_POST['setipven'] != 'T') {
            $dt_tipven = mysqli_fetch_array(mysqli_query($con ,"select * from dgtipven where tip_ven='" . $_POST['setipven'] . "'"));
            $tipoventa = $dt_tipven["des_tipven"];
        } else {
            $tipoventa = "TODAS";
        }

        if ($_POST['setipvent'] != 'T') {
            $dt_tipven = mysqli_fetch_array(mysqli_query($con ,"select * from ptventa where nparte1='" . $_POST['setipvent'] . "'"));
            $pventa = $dt_tipven["np_desc1"];
        } else {
            $pventa = "TODOS";
        }


        if ($_POST['secodven'] != 'T') {
            $dt_caj = mysqli_fetch_array(mysqli_query($con ,"select CONCAT(dgcatven.nom_ven,' ',dgcatven.ap_pat_ven,' ',dgcatven.ap_mat_ven) as nomVen from dgcatven where cod_ven='" . $_POST['secodven'] . "'"));
            $tipcaj = $dt_caj["nomVen"];
        } else {
            $tipcaj = "TODOS";
        }
        $this->Cell(190, 3, 'REPORTE DE CAJERO', 0, 1, 'C');
        $this->SetFont('Arial', 'B', 8);
        $this->Cell(190, 3, '', 0, 1, 'L');
        $this->Cell(190, 3, 'PERIODO DEL ' . $_POST['fecini'] . ' AL ' . $_POST['fecfin'] . '', 0, 1, 'C');
        $this->Cell(190, 3, 'PUNTO DE VENTA:' . $pventa . '', 0, 1, 'C');
        $this->Cell(190, 3, 'CAJERO:' . $tipcaj . '', 0, 1, 'C');

        //Salto de línea
        $this->Ln(3);
        $this->SetFont('ARIAL', 'B', 8);
        $this->SetFillColor(230, 230, 230);
        $this->Cell(22, 3, 'Folio Vta.', 1, 0, 'C', 1);
        $this->Cell(10, 3, 'Cant.', 1, 0, 'C', 1);
        $this->Cell(22, 3, 'Alimentos', 1, 0, 'C', 1);
        $this->Cell(20, 3, 'Bebidas', 1, 0, 'C', 1);
        $this->Cell(20, 3, 'Misc', 1, 0, 'C', 1);
        $this->Cell(25, 3, 'Subtotal', 1, 0, 'C', 1);
        $this->Cell(20, 3, 'Descuento', 1, 0, 'C', 1);
        $this->Cell(19, 3, 'T.i.p', 1, 0, 'C', 1);
        $this->Cell(20, 3, 'T.i.p. (S)', 1, 0, 'C', 1);
        $this->Cell(20, 3, 'Total', 1, 1, 'C', 1);
    }

    //Pie de Página
    function Footer() {
        //Posición: 1,5 cm del Final
        $this->SetY(-15);
        //Arial Italic 8
        $this->SetFont('Arial', 'I', 8);
        //Número de página
        $this->Cell(0, 0, $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

}

$sq_pvta = false;
if ($_POST['tip_rep'] == "R" && $_POST['setipvent'] == 'T') {
    $sq_pvta = mysqli_query($con ,"select * from ptventa");
}
$datos['tipRep'] = $_POST['tip_rep'];
$pdf = new PDF('P', 'mm', 'Letter');
$pdf->AddPage();
$count = 0;
do {
    $suma = 0;
    $granDesVen = 0;
    $desCuento = 0;  //descuento
    $granCorVen = 0;
    $sumacr = 0;  //cortesia
    $granTotVen = 0;
    $granSalVen = 0;
    $sumtot = 0;  //total
    $numVen = 0;
    $comida=0;
    $bebida=0;
    $miscelaneo=0;
    $granPro=0;
    $granSer=0;
  
    $count++;
    if ($count > 1) {
        unset($arrBus);
        $arrBus = array();
    }
    $pdf->AliasNbPages();
    $binit = -1;
    $pdf->SetFillColor(240, 240, 240);
    $pdf->SetFont('Arial', '', 8);
    $fecIni = chfecha($_POST['fecini'], 'd/m/Y', 'Y-m-d');
    $fecFin = chfecha($_POST['fecfin'], 'd/m/Y', 'Y-m-d');
    $arrBus[] = "(dgpemema.fec_ven between '$fecIni' and '$fecFin')";

    if ($_POST['secodven'] != 'T') { 
        $arrBus[] = "dgger_cajerosj.vendedor='" . $_POST['secodven'] . "'";
    }
    if ($_POST['setipven'] != 'T') {
        $arrBus[] = "dgpemema.tip_ven='" . $_POST['setipven'] . "'";
    }
    if ($_POST['setipvent'] != 'T') {
        $arrBus[] = ' ptventa.nparte1=' . $_POST['setipvent'] . ' ';
    } else {
        if ($count > 1) {
            $pdf->SetFont('Arial', 'B', 8);
            $pdf->Ln(3);
            $arrBus[] = ' ptventa.nparte1=' . $dt_tippag['nparte1'] . ' ';
            $pdf->Cell(190, 3, 'PUNTO DE VENTA: ' . $dt_tippag["np_desc1"] . '', 0, 1, 'C');
        } else
            $arrBus[] = ' ptventa.nparte1!=0 ';
    }

    $WHERE = "WHERE " . implode(" AND ", $arrBus);
    
    @$sql = mysqli_query($con ,"
		SELECT 
			dgpemema.fol_ven,
			dgpemema.con_ven,
			dgpemema.fec_ven,
			dgpemema.cod_ven,
			dgpemema.imp_pag,
			ctmeser.iniciales as nomVen,
			dgpemema.por_des as descuento,
			ctmesas.np_desc1 as dmesa,
			ptventa.tip_ven as dptvta,
			fpropina.tip as propina,
			dgger_cajerosj.tip_mov as tipag,
			dgpemema.cancela as cancela,
			dgger_cajerosj.fol_unp as folp,
			dgpemema.servicio as servicio
		FROM 
			dgpemema
			LEFT JOIN fpos ON fpos.fol_ven=dgpemema.fol_ven
			LEFT JOIN ctmesas ON ctmesas.nparte1=fpos.mesa
			LEFT JOIN ctarea ON ctarea.nparte1=ctmesas.area
			LEFT JOIN ctmeser  ON ctmeser.nparte1=dgpemema.cod_ven
			LEFT JOIN ptventa ON ptventa.nparte1=ctmesas.descripcion
			LEFT JOIN dgger_cajerosj ON dgger_cajerosj.pedido=dgpemema.fol_ven		
			LEFT JOIN  fpropina ON fpropina.fol_ven=dgpemema.fol_ven	
			LEFT JOIN  dgtippag ON dgtippag.tip_pag=dgger_cajerosj.tip_mov								
			left JOIN dgcatven ON dgpemema.cod_ven=dgcatven.cod_ven
			
		$WHERE 
		group by dgpemema.fol_ven
		ORDER BY 
			dgpemema.con_ven");
    echo mysqli_error($con);
    while ($reg = mysqli_fetch_array($sql)) {
        if (($reg['folp'] <= 0) || ($reg['folp'] == '')) {
//------suma de productos
            @$s = mysqli_query($con ,"SELECT SUM(dgpememd.pre_bas*dgpememd.can_ref) as sumat, sum(dgpememd.imp_des) as desCuento  FROM dgpememd, dgger_cajerosj, dgpemema WHERE dgpememd.cons_ven='" . $reg['fol_ven'] . "' and dgger_cajerosj.pedido='" . $reg['fol_ven'] . "' and dgpememd.cons_ven=dgpemema.fol_ven and dgpemema.cancelado=0 and dgpemema.cancela=0 and dgger_cajerosj.cancelado=0 and dgpememd.cancela=0");
            echo mysqli_error($con);
            while ($r = mysqli_fetch_array($s)) {
                $suma = $r["sumat"];
                $desCuento = $r['desCuento'];
            }
////---------------------------------	
            @$sqal = mysqli_query($con ,"SELECT sum(can_ref*pre_bas) as totAli from dgpememd inner join dginventp on dginventp.nparte1=dgpememd.nparte1 left join dgpemema on dgpememd.cons_ven=dgpemema.fol_ven where dgpememd.cons_ven='" . $reg['fol_ven'] . "' and dgpememd.cancela=0 and dginventp.tipo='1' and dgpemema.cancela='0' and dgpemema.cancelado='0' group by dgpememd.nparte1");
            while ($com = mysqli_fetch_array($sqal)) {
                $comida += $com['totAli'];
            }
            @$sqal = mysqli_query($con ,"SELECT sum(can_ref*pre_bas) as totAli from dgpememd inner join dginventp on dginventp.nparte1=dgpememd.nparte1 left join dgpemema on dgpememd.cons_ven=dgpemema.fol_ven where dgpememd.cons_ven='" . $reg['fol_ven'] . "' and dgpememd.cancela=0 and dginventp.tipo='3' and dgpemema.cancela='0' and dgpemema.cancelado='0' group by dgpememd.nparte1");
            while ($com = mysqli_fetch_array($sqal)) {
                $miscelaneo += $com['totAli'];
            }
            @$sqbb = mysqli_query($con ,"SELECT sum(can_ref*pre_bas) as totBeb from dgpememd inner join dginventp on dginventp.nparte1=dgpememd.nparte1 left join dgpemema on dgpememd.cons_ven=dgpemema.fol_ven where dgpememd.cons_ven='" . $reg['fol_ven'] . "' and dgpememd.cancela=0 and dginventp.tipo='2' and dgpemema.cancela='0' and dgpemema.cancelado='0' group by dgpememd.nparte1");
            while ($beb = mysqli_fetch_array($sqbb)) {
                $bebida += $beb['totBeb'];
            }
//------suma de productos solo cortesia
            @$cr = mysqli_query($con ,"select dgger_cajerosd.importe as sumat from dgger_cajerosj left join dgger_cajerosd on (dgger_cajerosd.n_rec=dgger_cajerosj.n_rec) where dgger_cajerosj.pedido='" . $reg['fol_ven'] . "' and dgger_cajerosd.tip_pag=3");
            echo mysqli_error($con);
            $ccr = mysqli_fetch_array($cr);
            if ($ccr['sumat'] != 0) {
                $sumacr = $ccr["sumat"] + $reg['propina'];
            } else {
                $sumacr = $ccr["sumat"];
            }
////---------------------------------
            @$sq = mysqli_query($con ,"SELECT sum(can_ref) as can_ref,pre_bas,nparte1 from dgpememd where cons_ven='" . $reg['fol_ven'] . "' and cancela=0 ");
            echo mysqli_error($con);
            while ($rg = mysqli_fetch_array($sq)) {
                if (isset($datos['tipRep']) && $datos['tipRep'] == 'D') {
                    $pdf->SetFillColor(240, 240, 240);
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell(22, 3, $reg["con_ven"], 0, 0, 'C', 0);
                    $pdf->Cell(10, 3, '  ' . $rg["can_ref"], 0, 0, 'L', 0);

                    @$al = mysqli_query($con ,"SELECT * from dginventp where nparte1='" . $rg['nparte1'] . "' and tipo=1"); //tipo=1 alimento
                    $al1 = mysqli_fetch_array($al);
                    $qw = $al1['lim_vta1'] * $rg['can_ref'];

                    $pdf->Cell(40, 3, number_format($qw, 2, '.', ','), 0, 0, 'L', 0);

                    @$beb = mysqli_query($con ,"SELECT * from dginventp where nparte1='" . $rg['nparte1'] . "' and tipo=2"); //tipo=2 bebidas
                    $beb1 = mysqli_fetch_array($beb);

                    $pdf->Cell(20, 3, number_format(($beb1['lim_vta1'] * $rg["can_ref"]), 2, '.', ','), 0, 0, 'R', 0);

                    $pdf->Cell(10, 3, '', 0, 0, 'R', 0); //descuento
                    $pdf->Cell(20, 3, '', 0, 0, 'R', 0); //cortesia			
                    $pdf->Cell(20, 3, '', 0, 0, 'R', 0); //subt
                    $pdf->Cell(20, 3, '', 0, 0, 'R', 0); // iva 
                    $pdf->Cell(20, 3, '', 0, 1, 'R', 0); //total
                }
            }
            if (($reg['imp_pag'] == 0.00) && ($reg['cancela'] == 1)) {
                if ($datos['tipRep'] == 'D') {
                    $pdf->SetFont('ARIAL', 'B', 8);
                    $pdf->SetFillColor(230, 210, 230);
                    $pdf->Cell(22, 3, $reg["con_ven"], 0, 0, 'C', 1);
                    $pdf->Cell(15, 3, chfecha($reg['fec_ven'], 'Y-m-d', 'd/m/Y'), 0, 0, 'L', 1);
                    $pdf->Cell(25, 3, $reg['dptvta'] . ' | ' . $reg["dmesa"], 0, 0, 'R', 1);
                    $pdf->Cell(30, 3, $reg["nomVen"], 0, 0, 'L', 1);
                    $pdf->Cell(105, 3, 'CANCELADO', 0, 1, 'C', 1);
                }
            } else {
                if (isset($datos['tipRep']) && $datos['tipRep'] == 'D') {
                    $pdf->SetFont('ARIAL', 'B', 8);
                    $pdf->SetFillColor(230, 230, 230);
                    $pdf->Cell(22, 3, $reg["con_ven"], 0, 0, 'C', 1);
                    $pdf->Cell(15, 3, chfecha($reg['fec_ven'], 'Y-m-d', 'd/m/Y'), 0, 0, 'L', 1);
                    $pdf->Cell(25, 3, $reg['dptvta'] . ' | ' . $reg["dmesa"], 0, 0, 'R', 1);
                    $pdf->Cell(30, 3, $reg["nomVen"], 0, 0, 'L', 1);
                }
                if (isset($reg['imp_pag']) && $reg['imp_pag'] == 0) {
                    $pdf->Cell(100, 3, 'CUENTA ABIERTA', 0, 1, 'C', 1);  //ctas abiertas
                } else {
                    if ($datos['tipRep'] == 'D') {

                        $pdf->Cell(2, 3, '', 0, 0, 'R', 1);  //descuento
                        $pdf->Cell(25, 3, number_format($suma, 2, '.', ','), 0, 0, 'R', 1);   //cortesia	
                        $pdf->Cell(20, 3, number_format($desCuento, 2, '.', ','), 0, 0, 'R', 1);   //cortesia
                        $pdf->Cell(19, 3, number_format($reg["propina"], 2, '.', ','), 0, 0, 'R', 1);   //propina	
                        $pdf->Cell(20, 3, number_format($reg["servicio"], 2, '.', ','), 0, 0, 'R', 1);  //iva				
                        $pdf->Cell(20, 3, number_format($sumtot = ($suma + $reg['propina'] + $reg['servicio'] - $desCuento), 2, '.', ','), 0, 1, 'R', 1);
                    }
                    $sumtot = ($suma + $reg['propina'] + $reg['servicio'] - $desCuento);

                    //SUMA DE TOTALES
                    $granDesVen +=$desCuento;  //descuento
                    $granCorVen +=$sumacr;  //cortesia
                    $granTotVen +=$suma;  //subtotal
                    //$granPagVen +=$reg["iva_ven"];  //iva
                    if ($reg["imp_pag"] != 0) {
                        $granSalVen +=$sumtot;  //total
                    }
                    $granPro +=($reg["propina"]);  //propina		
                    $granSer +=($reg["servicio"]);  //servicio		
                    $numVen++;
                }
            }
        }
    }

    $pdf->SetFont('ARIAL', 'B', 8);
    $pdf->SetFillColor(230, 230, 230);
    $pdf->Cell(30, 3, 'TOTALES: ', 0, 0, 'L', 1);
    $pdf->Cell(22, 3, number_format($comida, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(20, 3, number_format($bebida, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(20, 3, number_format($miscelaneo, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(25, 3, number_format($granTotVen, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(20, 3, number_format($granDesVen, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(19, 3, number_format($granPro, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(20, 3, number_format($granSer, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(20, 3, number_format($granSalVen, 2, '.', ','), 0, 1, 'R', 1);
    $pdf->SetFont('ARIAL', 'B', 8);
    $pdf->SetFillColor(230, 230, 230);
    $pdf->Cell(30, 3, 'TOT S/IVA: ', 0, 0, 'L', 1);
    $pdf->Cell(22, 3, number_format($comida / 1.16, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(20, 3, number_format($bebida / 1.16, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(20, 3, number_format($miscelaneo / 1.16, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(25, 3, number_format($granTotVen / 1.16, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(25, 3, number_format($granTotVen * .16, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(15, 3, number_format($granDesVen, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(15, 3, number_format($granPro, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(15, 3, number_format($granSer, 2, '.', ','), 0, 0, 'R', 1);
    $pdf->Cell(15, 3, number_format($granSalVen, 2, '.', ','), 0, 1, 'R', 1);
} while ($dt_tippag = mysqli_fetch_array($sq_pvta));
$pdf->Output("Reporte_de_Cajero.pdf", "D");
?>


