<?php
include_once("../../../php/sessionStart.php"); 
include_once("../../../php/db/dbExec.php");
include_once('../../../php/functions.php');
include_once("../../../php/fpdf/functions.php");
include_once('../../../php/maxExecTime.php');

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=utf8_decode($_SESSION["empNom"]);

$selDepto=$_GET["selDepto"];
$selsubDepto=$_GET["selSubDepto"];
$selBodega=$_GET["selBodega"];
$BodegaNom=$_GET["BodegaNom"];
$crInclue=$_GET["crInclue"];
$dtFec=$_GET["dtFec"];
$crTse=$_GET["crTse"];
$txtGrupo=$_GET["txtGrupo"];
if ($crTse=="T") {
   $crTsenom="Todos los articulos";
} else {
   $crTsenom="Solo articulos con Existencia";
}   

$lineWidth=17;
$lineHeight=0.8;

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineHeight, $lineWidth, $dtFec,$crInclue, $BodegaNom, $crTsenom;
        $this->SetMargins(1, 1, 2);
        $this->Cell(0, 0, "", 0, 1);

        $this->SetFont("Arial","B",17);
        $this->Cell($lineWidth,.7,$empNom,0,0,"L");

        $this->SetFont("Arial","I",9);
        $this->SetY(.7);
        $this->Cell(0,1,utf8_decode("PÁGINA ").$this->PageNo(),0,1,"R");

        $this->SetFont("","",9);
        $this->MultiAlignCell($lineWidth, .4, utf8_decode("Listado de Articulos para toma fisica al ".strftime("%d/%m/%Y", strtotime($dtFec))),0,1,"L");
        //$this->Ln();
        $this->MultiAlignCell($lineWidth, .4, utf8_decode($BodegaNom),0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode($crTsenom),0,1,"L");
        $this->SetFont("","",8);
        if ($crInclue=='S') {
            $header = array("Codigo","Descripcion","Medida","Fecha/Vence","Sistema","Fisico");
            $this->SetFont("","B");
            $this->SetWidths(array(3,7,2.5,2.2,2.5,2.5));
            $this->SetAligns(array("C","C","C","C","C","C"));
            $this->Row($header, $lineHeight);
            $this->SetFont("");
            $this->SetAligns(array("L","L","C","C","R","C"));
        } else {
            $header = array("Codigo","Descripcion","Medida","Fecha/Vence","Fisico");
            $this->SetFont("","B");
            $this->SetWidths(array(2.5,7,2.5,2.2,2.5));
            $this->SetAligns(array("C","C","C","C","C"));
            $this->Row($header, $lineHeight);
            $this->SetFont("");
            $this->SetAligns(array("L","L","C","C","C"));
        }
    }
    
    function Footer() {
        $func = new functions();

        $this->SetY(-1.5);
        $this->SetFont("Arial","I",6);
        $this->Cell(0,1,strftime("%d/%m/%Y %T"),0,0,"L");
    }
}

# QUERYS
if (empty($txtGrupo)) {
    if ($selDepto=='0000') {
        if ($selsubDepto=='0000') {
            $sArtic = "SELECT *, TRIM(art_cod) AS art_cod,
                        TRIM(art_desc) AS art_desc,
                        TRIM(art_desd) AS art_desd,
                        TRIM(art_marca) AS art_marca,
                        TRIM(art_prov) AS art_prov,
                        TRIM(art_codf) AS art_codf,
                        TRIM(art_codbarr) AS art_codbarr
                        FROM artic art LEFT JOIN deptosi di ON art.art_dep=di.dep_cod 
                        LEFT JOIN subdeptosi sdi ON art.art_dep=sdi.sdep_dep AND art.art_subdep=sdi.sdep_cod
                        LEFT JOIN unimed um ON art.art_medi=um.um_cod
                        INNER JOIN exiart_peps epeps ON art.art_emp=epeps.ea_emp AND art.art_cod=epeps.ea_cod
                        WHERE art_emp='$empCod' AND art_bienser='B' GROUP BY art.art_emp,art.art_cod ORDER BY art_emp,art_dep,art_subdep,art_desc";
        } else {
            $sArtic = "SELECT *, TRIM(art_cod) AS art_cod,
                        TRIM(art_desc) AS art_desc,
                        TRIM(art_desd) AS art_desd,
                        TRIM(art_marca) AS art_marca,
                        TRIM(art_prov) AS art_prov,
                        TRIM(art_codf) AS art_codf,
                        TRIM(art_codbarr) AS art_codbarr
                        FROM artic art LEFT JOIN deptosi di ON art.art_dep=di.dep_cod 
                        LEFT JOIN  subdeptosi sdi ON art.art_dep=sdi.sdep_dep AND art.art_subdep=sdi.sdep_cod
                        LEFT JOIN unimed um ON art.art_medi=um.um_cod
                        INNER JOIN exiart_peps epeps ON art.art_emp=epeps.ea_emp AND art.art_cod=epeps.ea_cod
                        WHERE art_emp='$empCod' AND art_subdep='$selsubDepto' AND art_bienser='B' GROUP BY art.art_emp,art.art_cod ORDER BY art_emp,art_dep,art_subdep,art_desc";
        }
    } else {
        if ($selsubDepto=='0000') {
            $sArtic = "SELECT *, TRIM(art_cod) AS art_cod,
                        TRIM(art_desc) AS art_desc,
                        TRIM(art_desd) AS art_desd,
                        TRIM(art_marca) AS art_marca,
                        TRIM(art_prov) AS art_prov,
                        TRIM(art_codf) AS art_codf,
                        TRIM(art_codbarr) AS art_codbarr
                        FROM artic art LEFT JOIN deptosi di ON art.art_dep=di.dep_cod 
                        LEFT JOIN  subdeptosi sdi ON art.art_dep=sdi.sdep_dep AND art.art_subdep=sdi.sdep_cod
                        LEFT JOIN unimed um ON art.art_medi=um.um_cod
                        INNER JOIN exiart_peps epeps ON art.art_emp=epeps.ea_emp AND art.art_cod=epeps.ea_cod
                        WHERE art_emp='$empCod' AND art_dep='$selDepto' AND art_bienser='B' GROUP BY art.art_emp,art.art_cod ORDER BY art_emp,art_dep,art_subdep,art_desc";
        } else {
            $sArtic = "SELECT *, TRIM(art_cod) AS art_cod,
                        TRIM(art_desc) AS art_desc,
                        TRIM(art_desd) AS art_desd,
                        TRIM(art_marca) AS art_marca,
                        TRIM(art_prov) AS art_prov,
                        TRIM(art_codf) AS art_codf,
                        TRIM(art_codbarr) AS art_codbarr
                        FROM artic art LEFT JOIN deptosi di ON art.art_dep=di.dep_cod 
                        LEFT JOIN  subdeptosi sdi ON art.art_dep=sdi.sdep_dep AND art.art_subdep=sdi.sdep_cod
                        LEFT JOIN unimed um ON art.art_medi=um.um_cod
                        INNER JOIN exiart_peps epeps ON art.art_emp=epeps.ea_emp AND art.art_cod=epeps.ea_cod
                        WHERE art_emp='$empCod' AND art_dep='$selDepto' AND art_subdep='$selsubDepto' AND art_bienser='B' GROUP BY art.art_emp,art.art_cod ORDER BY art_emp,art_dep,art_subdep,art_desc";
        }
    }
} else {
    $sArtic = "SELECT *, TRIM(art_cod) AS art_cod,
                TRIM(art_desc) AS art_desc,
                TRIM(art_desd) AS art_desd,
                TRIM(art_marca) AS art_marca,
                TRIM(art_prov) AS art_prov,
                TRIM(art_codf) AS art_codf,
                TRIM(art_codbarr) AS art_codbarr
                FROM artic art LEFT JOIN deptosi di ON art.art_dep=di.dep_cod 
                LEFT JOIN subdeptosi sdi ON art.art_dep=sdi.sdep_dep AND art.art_subdep=sdi.sdep_cod
                LEFT JOIN unimed um ON art.art_medi=um.um_cod
                INNER JOIN exiart_peps epeps ON art.art_emp=epeps.ea_emp AND art.art_cod=epeps.ea_cod
                WHERE art_emp='$empCod' AND art_bienser='B' AND art_cod LIKE '%$txtGrupo%' GROUP BY art.art_emp,art.art_cod ORDER BY art_emp,art_dep,art_subdep,art_desc";
}

function sExiart($codd) { 
    global $empCod, $usuNom, $selBodega;
    return "SELECT * FROM exiart_peps WHERE ea_emp='$empCod' AND ea_cod='$codd' AND ea_bod='$selBodega' ORDER BY ea_fvence";
}

function sMoviie0($codd,$fvenced) { 
    global $empCod, $usuNom, $selBodega, $dtFec;
    return "SELECT mov_emp,mov_cod,mov_bodent,mov_bodsal,
            SUM(mov_entra) AS mov_sentra
            FROM movii_peps WHERE mov_emp='$empCod' AND mov_cod='$codd' AND mov_bodent='$selBodega' AND mov_fec>'$dtFec' AND mov_fvence='$fvenced' GROUP BY '$empCod','$codd','$fvenced'";
}
function sMoviis0($codd,$fvenced) { 
    global $empCod, $usuNom, $selBodega, $dtFec;
    return "SELECT mov_emp,mov_cod,mov_bodent,mov_bodsal,
            SUM(mov_sale) AS mov_ssale
            FROM movii_peps WHERE mov_emp='$empCod' AND mov_cod='$codd' AND mov_bodsal='$selBodega' AND mov_fec>'$dtFec' AND mov_fvence='$fvenced' GROUP BY '$empCod','$codd','$fvenced'";
}

# EJECUCION DE QUERYS
$db = new dbExec();
$func = new functions();

$select["artic"] = $db->query($sArtic);

# GENERAR PDF
$pdf = new generatePDF("P","cm", "Letter");
$pdf->AddPage();
$ti_Depto="XXXX";
$ti_sDepto="XXXX";
while ($rec = $db->fetchAssoc($select["artic"])) {
/*    if ($ti_Depto.$ti_sDepto!=$rec["art_dep"].$rec["art_subdep"]) {
       $pdf->Cell($lineWidth,.7,trim($rec["dep_nom"]).", ".rtrim($rec["sdep_nom"]),0,0,"L");
       $ti_Depto=$rec["art_dep"];
       $ti_sDepto=$rec["art_subdep"];
       $pdf->ln();
    }  */
    $codd=$rec["art_cod"];

    // Para revisar las existencias peps
    $select["exiart_peps"] = $db->query(sExiart($codd));
    while ($recexist = $db->fetchAssoc($select["exiart_peps"])) {
        $Exist = $recexist["ea_exist"];
        $fvence = $recexist["ea_fvence"];

        $select["movii"] = $db->query(sMoviie0($codd,$fvence));
        $recme0 = $db->fetchAssoc($select['movii']);
        $sEntramov=$recme0["mov_sentra"];

        $select["movii"] = $db->query(sMoviis0($codd,$fvence));
        $recms0 = $db->fetchAssoc($select['movii']);
        $sSalemov=$recms0["mov_ssale"];

        $Exipas=($Exist-$sEntramov)+$sSalemov;

        if ($crTse=='E' && $Exipas<=0) {
            continue;
        }        
        if ($ti_Depto.$ti_sDepto!=$rec["art_dep"].$rec["art_subdep"]) {
           $pdf->SetFont("","B");
           $pdf->Cell($lineWidth,.7,trim($rec["dep_nom"]).", ".rtrim($rec["sdep_nom"]),0,0,"L");
           $ti_Depto=$rec["art_dep"];
           $ti_sDepto=$rec["art_subdep"];
           $pdf->SetFont("","");
           $pdf->ln();
        }             

        if ($crInclue=='S') {
            $body = array(
                $rec["art_cod"],
                trim($rec["art_desc"]),
                trim($rec["um_nom"]),
                strftime("%d/%m/%Y", strtotime($fvence)),
                number_format($Exipas, 2, '.', ','),
                trim("_____________"),
            );
        } else {
            $body = array(
                $rec["art_cod"],
                trim($rec["art_desc"]),
                trim($rec["um_nom"]),
                strftime("%d/%m/%Y", strtotime($fvence)),
                trim("_____________"),
            );
        }
        $pdf->RowBorderHidden($body, $lineHeight);
    }
}
$db->close();
$pdf->Output(utf8_decode("TomaFisica").".pdf","I");
?>