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

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

$selStatus=$_GET["selStatus"];
$selBodega=$_GET["selBodega"];
$selBodegan=$_GET["selBodegan"];
$dtFeci=$_GET["dtFeci"];
$dtFecf=$_GET["dtFecf"];
if ($selStatus=="000") {
    $statusnom="TODAS LAS TOMAS FISICAS";
}
if ($selStatus=="001") {
    $statusnom="TOMAS FISICAS QUE NO HAN SIDO PROCESADAS";
}
if ($selStatus=="002") {
    $statusnom="TOMAS FISICAS QUE YA FUERON PROCESADAS";
}

//$func = new functions();

$lineWidth=17;
$lineHeight=0.5;

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineHeight, $lineWidth, $dtFeci, $dtFecf, $statusnom, $selBodegan, $selBodega;
        $this->SetMargins(1.5, 1, 2);
        $this->Cell(0, 0, "", 0, 1);

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

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

        $this->SetFont("","",9);
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("LISTADO DE DOCUMENTOS DE ENTRADA DEL ".strftime("%d/%m/%Y", strtotime($dtFeci))." AL ".strftime("%d/%m/%Y", strtotime($dtFecf))),0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, $selBodegan."       ".$statusnom,0,1,"L");

        $this->Ln();
        $this->SetFont("","",8);
        $this->SetFont("","B");
        $this->SetWidths(array(3.5,8,2,2,3.5));
        $this->SetAligns(array("C","C","C","C","C"));

        $this->SetFont("");
        $this->SetAligns(array("L","L","C","C","R"));
    }
    
    function Footer() {
        // Posición: a 1,5 cm del final
        //$this->SetY(-2.5);
        //$this->SetFont("Arial","I",8);
        //$this->Cell(0,1,"PAGINA ".$this->PageNo(),0,0,"C");
    }
}

# QUERYS
if ($selStatus=='000') {
    $sInvefis = "SELECT *, TRIM(inf.if_obs) AS if_obs
       FROM invefdet ifd LEFT JOIN invefis inf ON ifd.infd_emp=inf.if_emp and ifd.infd_tipo=inf.if_tipo and ifd.infd_num=inf.if_num 
       LEFT JOIN bodegas bod ON  inf.if_bod=bod.bod_cod 
       WHERE infd_emp='$empCod' and inf.if_bod='$selBodega' and infd_fec>='$dtFeci' and infd_fec<='$dtFecf' ORDER BY infd_fec,infd_tipo,infd_num";
}
if ($selStatus=='001') {
    $sInvefis = "SELECT *, TRIM(inf.if_obs) AS if_obs
       FROM invefdet ifd LEFT JOIN invefis inf ON ifd.infd_emp=inf.if_emp and ifd.infd_tipo=inf.if_tipo and ifd.infd_num=inf.if_num 
       LEFT JOIN bodegas bod ON  inf.if_bod=bod.bod_cod 
       WHERE infd_emp='$empCod' and inf.if_bod='$selBodega' and infd_fec>='$dtFeci' and infd_fec<='$dtFecf' AND if_proc!='S' ORDER BY infd_fec,infd_tipo,infd_num";
}
if ($selStatus=='002') {
    $sInvefis = "SELECT *, TRIM(inf.if_obs) AS if_obs
       FROM invefdet ifd LEFT JOIN invefis inf ON ifd.infd_emp=inf.if_emp and ifd.infd_tipo=inf.if_tipo and ifd.infd_num=inf.if_num 
       LEFT JOIN bodegas bod ON  inf.if_bod=bod.bod_cod 
       WHERE infd_emp='$empCod' and inf.if_bod='$selBodega' and infd_fec>='$dtFeci' and infd_fec<='$dtFecf' AND if_proc='S' ORDER BY infd_fec,infd_tipo,infd_num";
}

# EJECUCION DE QUERYS
$db = new dbExec();
$lineHeight=0.40;
$select["invefisdet"] = $db->query($sInvefis);

# GENERAR PDF
$pdf = new generatePDF("P","cm", "letter");
$pdf->AddPage();
$func = new functions();

$ti_tipo="XXXZZZ999";
$ti_num=9999;
$subtot=0;
$tottot=0;
$subcant=0;
$totcant=0;
$t_lini=1;
$codp="";
$nomp="";
$clas="";
$clasnom="";

$arrayProv = array();

while ($rec = $db->fetchAssoc($select["invefisdet"])) {
    if ($t_lini == 1) {
        //$pdf->SetFont("","B",8);        
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TOMA FISICA "."         NUMERO:  ".$rec["infd_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["infd_fec"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("BODEGA:  ".trim($rec["bod_nom"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("OBSERVACIONES: ".$rec["if_obs"]),0,1,"L");
        $pdf->SetFont("","",8);   
        $header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"MEDIDA","F/VENCE","FISICO");
        $widths = array(3.5,8,2,2,3.5);
        $pdf->SetAligns(array("C","C","C","C","C"));
        $pdf->Row($header, $lineHeight);
        $pdf->SetAligns(array("L","L","C","C","R"));
    }
    if ($ti_tipo!=$rec["infd_tipo"]  || $ti_num!=$rec["infd_num"]) {
        if ($t_lini > 1) {
            $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
            $pdf->Cell(.04,.4,"                  TOTAL ESTE DOCUMENTO",0,0,"L");
            $body = array(
                "",
                "",
                "",
                "",
                number_format($subcant, 2, '.', ','),
            ); 
            $pdf->RowBorderHidden($body, $lineHeight);
            $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
            $pdf->ln();
            $pdf->MultiAlignCell($lineWidth, $lineHeight, "TOMA FISICA "."         NUMERO:  ".$rec["infd_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["infd_fec"])),0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("BODEGA:  ".trim($rec["bod_nom"])),0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("OBSERVACIONES: ".$rec["if_obs"]),0,1,"L");
            $pdf->SetFont("","",8);   
            $header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"MEDIDA","F/VENCE","FISICO");
            $widths = array(3.5,8,2,2,3.5);
            $pdf->SetAligns(array("C","C","C","C","C"));
            $pdf->Row($header, $lineHeight);
            $pdf->SetAligns(array("L","L","C","C","R"));
        }             
        $subtot=0;
        $subcant=0;
        $ti_tipo=$rec["infd_tipo"];
        $ti_num=$rec["infd_num"];

        $t_lini=2;
    } 
    $subcant += $rec["infd_cant"];
    $totcant += $rec["infd_cant"];

    $body = array(
        $rec["infd_cod"],
        $rec["infd_desc"],
        $rec["infd_unimed"],
        date('d-m-Y', strtotime($rec["infd_fvence"])),
        number_format($rec["infd_cant"],2),
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
    if (($pdf->GetY()+$lineHeight) > 25) {
        $pdf->AddPage();
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TOMA FISICA "."         NUMERO:  ".$rec["infd_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["infd_fec"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("BODEGA:  ".trim($rec["bod_nom"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("OBSERVACIONES: ".$rec["if_obs"]),0,1,"L");
        $pdf->SetFont("","",8);   
        $header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"MEDIDA","F/VENCE","FISICO");
        $pdf->SetWidths(array(3.5,8,2,2,3.5));
        $pdf->SetAligns(array("C","C","C","C","C"));
        $pdf->Row($header, $lineHeight);
        $pdf->SetAligns(array("L","L","C","C","R"));
    }    
}

$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$pdf->Cell(.04,.4,"                  TOTAL ESTE DOCUMENTO",0,0,"L");
$body = array(
    "",
    "",
    "",
    "",
    number_format($subcant, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$pdf->ln();

$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$pdf->Cell(.04,.4,"                  TOTAL GENERAL",0,0,"L");
$body = array(
    "",
    "",
    "",
    "",
    number_format($totcant, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());

$pdf->Output(utf8_decode("Lista Tomas Fisicas" ).".pdf","I");

$db->close();

?>