<?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"]);

$dtFeci=$_GET["dtFeci"];
$dtFecf=$_GET["dtFecf"];
$crTmone=$_GET["crTmone"];
if ($crTmone == 'L') {
    $tipomon=$empMoneda;
} else {
    $tipomon='DOLARES';
}   

//$func = new functions();

$lineWidth=17;
$lineHeight=0.5;

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineHeight, $lineWidth, $dtFeci, $dtFecf, $tipomon;
        $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 CARGO DEL ".strftime("%d/%m/%Y", strtotime($dtFeci))." AL ".strftime("%d/%m/%Y", strtotime($dtFecf))),0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("DOCUMENTOS REGISTRADOS EN ").$tipomon,0,1,"L");

        $this->Ln();
        $this->SetFont("","",8);
        $header = array("SERIE","NUMERO","FEC/DOC","FEC/VENCE","PROVEEDOR","CONCEPTO","TC","VALOR");
        $this->SetFont("","B");
        $this->SetWidths(array(1.7,2.3,1.8,2,6,7,1.6,2.2));
        $this->SetAligns(array("C","C","C","C","C","C","C","C"));
        $this->Row($header, $lineHeight);

        $this->SetFont("");
        $this->SetAligns(array("L","L","C","C","L","L","R","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 ($crTmone=='L') {
    $sDoctosc = "SELECT *, TRIM(dc_prov) AS dc_prov, TRIM(dc_codp) AS dc_codp, TRIM(dc_nit) AS dc_nit, (dc_tot-(dc_abo+dc_ret+dc_reti)) AS dc_saldod, TRIM(pv.pro_clas) AS dc_clas, TRIM(cla.cla_nom) AS cla_nom FROM doctosc dc 
        LEFT JOIN provcp pv ON dc.dc_emp=pv.pro_emp and dc.dc_codp=pv.pro_cod 
        LEFT JOIN clasi cla ON pv.pro_clas=cla.cla_cod
        WHERE dc_emp='$empCod' and dc_fec>='$dtFeci' and dc_fec<='$dtFecf' and dc_tcam=1 and dc_tot>0 ORDER BY dc_fec,dc_num";
} else {
    $sDoctosc = "SELECT *, TRIM(dc_prov) AS dc_prov, TRIM(dc_codp) AS dc_codp, TRIM(dc_nit) AS dc_nit, (dc_tot-(dc_abo+dc_ret+dc_reti)) AS dc_saldod, TRIM(pv.pro_clas) AS dc_clas, TRIM(cla.cla_nom) AS cla_nom FROM doctosc dc 
        LEFT JOIN provcp pv ON dc.dc_emp=pv.pro_emp and dc.dc_codp=pv.pro_cod 
        LEFT JOIN clasi cla ON pv.pro_clas=cla.cla_cod
        WHERE dc_emp='$empCod' and dc_fec>='$dtFeci' and dc_fec<='$dtFecf' and dc_tcam>1 and dc_tot>0 ORDER BY dc_fec,dc_num";
}    
# EJECUCION DE QUERYS
$db = new dbExec();
$lineHeight=0.45;
$select["doctosc"] = $db->query($sDoctosc);

# GENERAR PDF
$pdf = new generatePDF("L","cm", array(22, 28));
$pdf->AddPage();
$func = new functions();

$ti_clasi="XXXZZZ999";
$tottot=0;
$t_lini=1;
$codp="";
$ti_codp="XXZZZ999";
$nomp="";
$clas="";
$clasnom="";

$arrayProv = array();

while ($rec = $db->fetchAssoc($select["doctosc"])) {
    $codp=$rec["dc_codp"];
    $seried=$rec["dc_serie"];
    $numd=$rec["dc_num"];
    $Prov=$rec["dc_prov"];    
    $fec=$rec["dc_fec"];
    $fecp=$rec["dc_fecp"];
    $tcam=$rec["dc_tcam"];
    $tot=$rec["dc_tot"];

    $tottot += $tot;

    $body = array(
        $rec["dc_serie"],
        trim($rec["dc_num"]),
        strftime("%d/%m/%Y", strtotime($fec)),
        strftime("%d/%m/%Y", strtotime($fecp)),
        trim($rec["dc_prov"]),
        trim($rec["dc_con"]),
        ($tcam != 0) ? number_format($tcam, 6, '.', ',') : "",        
        (round($tot, 2) != 0) ? number_format($tot, 2, '.', ',') : "",        
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
}

$pdf->line($pdf->GetX()+0, $pdf->GetY(), 26.2, $pdf->GetY());
$body = array(
    "",
    "",
    "",
    "",
    "",
    "          TOTAL GENERAL",
    "",
    number_format($tottot, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 26.2, $pdf->GetY());

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

$db->close();

?>