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

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

$dtFecI=$_SESSION["print"]["dtFecI"];
$dtFecF=$_SESSION["print"]["dtFecF"];
$selBodega=$_SESSION["print"]["selBodega"];
$BodegaNom=$_SESSION["print"]["BodegaNom"];
$txtCodd=$_SESSION["print"]["txtCodd"];
$txtDescd=$_SESSION["print"]["txtDescd"];

$lineWidth=19.3;
$lineHeight=0.4;

# QUERYS
$sMoviie0 = "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='$txtCodd' AND mov_bodent='$selBodega' AND mov_fec>='$dtFecI' GROUP BY '$empCod','$txtCodd'";

$sMoviis0 = "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='$txtCodd' AND mov_bodsal='$selBodega' AND mov_fec>='$dtFecI' GROUP BY '$empCod','$txtCodd'";

$sMovii = "SELECT *,
            TRIM(mov_cod) AS mov_cod
            FROM movii_peps LEFT JOIN tipodoci ON movii_peps.mov_tipo=tipodoci.td_cod 
            LEFT JOIN facturas ON movii_peps.mov_emp=facturas.fact_emp AND movii_peps.mov_num=facturas.fact_num AND movii_peps.mov_bodsal=facturas.fact_bodsal AND movii_peps.mov_tipo>='0101' AND movii_peps.mov_tipo<='01104'
            WHERE mov_emp='$empCod' AND mov_cod='$txtCodd' AND (mov_bodent='$selBodega' OR mov_bodsal='$selBodega') AND mov_fec>='$dtFecI' AND mov_fec<='$dtFecF' ORDER BY mov_fec,td_eos,mov_tipo,mov_num";
    
$sExiart = "SELECT * FROM exiart WHERE ea_emp='$empCod' AND ea_cod='$txtCodd' AND ea_bod=$selBodega";

$sArtic = "SELECT *,
            TRIM(art_cod) AS art_cod,
            TRIM(art_desc) AS art_desc 
            FROM artic LEFT JOIN unimed ON artic.art_medi=unimed.um_cod WHERE art_emp='$empCod' AND art_cod='$txtCodd'";

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

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

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

$select["artic"]=$db->query($sArtic);
$recart = $db->fetchAssoc($select['artic']);
$artNom=trim($recart["art_desc"]);
$artmedida=rtrim($recart["um_nom"]);

$select["exiart"]=$db->query($sExiart);
$recea = $db->fetchAssoc($select['exiart']);
$Saldo=$recea["ea_exist"];

$salIni=($Saldo-$sEntramov)+$sSalemov;
$Saldo=0;

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineHeight, $lineWidth,$dtFecI, $dtFecF, $txtCodd,$salIni,$Saldo,$artNom,$artmedida,$BodegaNom;
        $func = new functions();
        $this->SetMargins(.9, 1, 1.35);
        $this->Cell(0, 0, "", 0, 1);

        $this->SetFont("Arial","B",10);
        $this->MultiAlignCell($lineWidth, $lineHeight, $empNom,0,1,"L");
        $this->SetFont("","I",8);
        $this->SetY(.6);
        $this->Cell(0,1,utf8_decode("PÁGINA ").$this->PageNo(),0,1,"R");

        $this->SetFont("","B",9);
        $this->Cell($lineWidth, $lineHeight, utf8_decode("KARDEX DEL ".strftime("%d/%m/%Y", strtotime($dtFecI))." AL ".strftime("%d/%m/%Y", strtotime($dtFecF))),0,1,"L");
        $this->Cell($lineWidth, $lineHeight, utf8_decode("ARTICULO: ".$txtCodd.' - '.$artNom),0,1,"L");
        $this->Cell($lineWidth, $lineHeight, utf8_decode("UNIDAD DE MEDIDA: ".$artmedida),0,1,"L");
        $this->Cell($lineWidth, $lineHeight, utf8_decode("BODEGA: ".$BodegaNom),0,1,"L");
        $this->SetFont("","B",9);
        $this->Cell($lineWidth/1.2, $lineHeight, "Saldo Inicial: ",0,0,"R");
        $this->Cell($lineWidth/9, $lineHeight, number_format($salIni, 2, '.', ','),0,1,"R");

        $this->SetFont("","B",9);        
        $header = array("Tipo de Documento","Numero","Proveed","Factura","ID interno","Fec/Doc","F/Vencim","Entradas","Salidas","Saldo");
        $this->SetFont("","B");
        $this->SetWidths(array(3.5,1.9,1.6,1.9,2.1,1.8,1.8,1.8,1.8,1.8));
        $this->SetAligns(array("C","C","C","C","C","C","C","C","C","C"));
        $this->Row($header, $lineHeight);
        $this->SetFont("", "", 8);
        $this->SetAligns(array("L","L","L","L","L","C","R","R","R","R"));

    }
    
    function Footer() {
        // Posición: a 1,5 cm del final
        $func = new functions();

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

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

$sentra = 0;
$ssale = 0;
$Saldo = $salIni;

$select["movii"] = $db->query($sMovii);
while ($rec = $db->fetchAssoc($select['movii'])) {
    $entra = $rec["mov_entra"];
    $sale = $rec["mov_sale"];
    if (TRIM($rec["mov_bodent"])!=TRIM($selBodega)) {
        $entra=0;
    }
    if (TRIM($rec["mov_bodsal"])!=trim($selBodega)) {
        $sale=0;
    }
    $sentra += $entra;
    $ssale += $sale;

    $Saldo = ($Saldo+$entra)-$sale;

    $body = array(
        trim($rec["td_nom"]),
        $rec["mov_num"],
        $rec["mov_codp"],
        ($rec["mov_fnum"]>0) ? $rec["mov_fnum"] : "",
        $rec["fact_idinterno"],
        strftime("%d/%m/%Y", strtotime($rec["mov_fec"])),
        strftime("%d/%m/%Y", strtotime($rec["mov_fvence"])),
        (round($entra,2) != 0) ? number_format($entra, 2, '.', ',') : "",
        (round($sale,2) != 0) ? number_format($sale, 2, '.', ',') : "",
        number_format($Saldo, 2, '.', ','),
        );
     $pdf->RowBorderHidden($body, $lineHeight);
}

$pdf->line($pdf->GetX()+7, $pdf->GetY(), 20.8, $pdf->GetY());

$pdf->SetAligns(array("","","","","","","R","R","R"));
$body = array("","","","","","Totales: ","",
    number_format($sentra,2, '.', ','),
    number_format($ssale,2, '.', ','),
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+7, $pdf->GetY(), 20.8, $pdf->GetY());
$pdf->line($pdf->GetX()+7, $pdf->GetY()+.075, 20.8, $pdf->GetY()+.075);

$db->close();

$pdf->Output(utf8_decode("Kardex").".pdf","I");
?>