<?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/timezone.php');
include_once('../../../php/maxExecTime.php');
//include_once('../ws/COFIDI/wsExec.php');

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=utf8_decode($_SESSION["empNom"]);
$empNomc=utf8_decode($_SESSION["empNomc"]);
//$empDir=utf8_decode($_SESSION["empDir"]);
$empNit=$_SESSION["empNit"];
$usuNom = utf8_decode($_SESSION['usuNom']);

$jsonLista = (isset($_GET['tblLista'])) ? json_decode($_GET['tblLista']) : json_decode($_SESSION['tblLista']);

$arrayError=array();

# QUERYS
function sDoctose($tipo,$numero) {
    global $empCod;
    return "SELECT *, 
        TRIM(de.de_obs) AS de_obs,
        TRIM(de.de_codp) AS de_codp,
        TRIM(cp.pro_nom) AS pro_nom,
        TRIM(bod.bod_nom) AS bod_nom,
        TRIM(bods.bod_nom) AS bod_noms,
        TRIM(td.td_nom) AS td_nom
        FROM doctose de 
        LEFT JOIN bodegas bod ON de.de_emp=bod.bod_emp AND de.de_bodent=bod.bod_cod 
        LEFT JOIN bodegas bods ON de.de_emp=bods.bod_emp AND de.de_bodsal=bods.bod_cod 
        LEFT JOIN provcp cp ON de.de_emp=cp.pro_emp AND de.de_codp=cp.pro_cod 
        LEFT JOIN tipodoci td ON  de.de_tipo=td.td_cod 
        WHERE de_emp='$empCod' AND de_tipo='$tipo' AND de_num='$numero'";
}

function sDoce_det($tipo,$numero) {
    global $empCod;
    return "SELECT * FROM doce_det WHERE ded_emp='$empCod' AND ded_tipo='$tipo' AND ded_num='$numero'";
}
$lineWidth=19.5;
$lineHeight=0.4;

class generatePDF extends PDF_MC_Table {
    function Header() {
    }
    
    function Footer() { 
        // Posición: a 1,5 cm del final
        $func = new functions();
        $this->SetY(-2);
        $this->SetFont("Arial","I",7);
        $this->Cell(0,1,strftime("%d/%m/%Y %T"),0,0,"L");
    }
}

$db = new dbExec();

$func = new functions();

# GENERAR PDF
$pdf = new generatePDF("P", "cm", "Letter");

# EJECUCION DE QUERYS
$pdf->AddPage();
$corr=1;
foreach ($jsonLista as $obj) {
    if ($corr>1) {
        $pdf->AddPage();
    }
    $tipo =  "0190";
    $numero =  $obj->numero;
    $select["doctose"] = $db->query(sDoctose($tipo,$numero));
    $select["doce_det"] = $db->query(sDoce_det($tipo,$numero));
    $rec = $db->fetchAssoc($select["doctose"]);

    $pdf->SetFont("Arial","B",14);
    $pdf->SetTextColor(0,0,0);

    $lineHeight7 = .6;
    /*$img = '../img/logo2.jpg';
    $pdf->Image($img,1,1,2.2);
    $pdf->SetMargins(1, 1, 1);
    $pdf->SetY(1.75);
    $pdf->Cell(0, 0, "", 0, 1);*/
    //$pdf->Cell($lineWidth/4, $lineHeight7, "");
    $pdf->Cell($lineWidth/1, $lineHeight7, $empNom,0,1,"L");
    $pdf->Cell($lineWidth/3, $lineHeight7, $rec["td_nom"],0,0,"L");
    $pdf->SetFont("Arial","B",14);
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, ("No.  ".$rec["de_num"]),0,1,"R");

    $pdf->SetFont("Arial","B",8);
    // Derecha
    $y = 1.3;
    $x = 9;

    $pdf->SetY($pdf->GetY()+.25);
    $pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+1, $pdf->GetY());
    $pdf->SetY($pdf->GetY()+.25);

    $pdf->SetFont("","B");
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("FECHA:"),0,0,"L");
    $pdf->SetFont("","");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, strftime("%d/%m/%Y", strtotime($rec["de_fec"])),0,1,"L");
    $lineHeight = .42;
    $pdf->SetFont("","B");
//    $pdf->MultiAlignCell($lineWidth/4.5, $lineHeight, utf8_decode("SERIE ORDEN DE COMPRA:"),0,0,"L");
//    $pdf->SetFont("","");
//    $pdf->MultiAlignCell($lineWidth/5, $lineHeight, utf8_decode($SerieoNom),0,0,"L");
    $pdf->SetFont("","B");
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("BODEGA ENTRO:"),0,0,"L");
    $pdf->SetFont("","");
    $pdf->MultiAlignCell($lineWidth/3.92, $lineHeight, utf8_decode($rec["bod_nom"]),0,0,"L");
    $pdf->SetFont("","B");
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("BODEGA SALIO:"),0,0,"L");
    $pdf->SetFont("","");
    $pdf->MultiAlignCell($lineWidth/3.92, $lineHeight, utf8_decode($rec["bod_noms"]),0,1,"L");
    $pdf->SetFont("","B");
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("OBSERVACIONES:"),0,0,"L");
    $pdf->SetFont("","");
    $pdf->MultiAlignCell($lineWidth/1.92, $lineHeight, utf8_decode($rec["de_obs"]),0,1,"L");
    $pdf->Ln();
    //$pdf->Ln();

    $lineHeight=0.4;
    $pdf->SetY($pdf->GetY()+.25);

    $header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"MEDIDA","F/VENCE","CANTIDAD","UNITARIO","TOTAL","BONIFICA","EXEN");
    $widths = array(2.3,5.5,1.8,1.5,1.7,2,2,1.6,1);
    $pdf->SetWidths($widths);
    $pdf->SetAligns(array("C","C","C","C","C","C","C","C","C"));
    $pdf->Row($header, $lineHeight);

    $pdf->SetAligns(array("L","L","C","C","R","R","R","R","C"));

    // Lineas separadoras del detalle
    $detX = 0;
    /*$detY = $pdf->GetY()+14.7;
    $pdf->line($pdf->GetX(), $pdf->GetY(), $pdf->GetX(), $detY);
    *foreach ($widths as $x) {
        $detX+=$x;
        $pdf->line($pdf->GetX()+$detX, $pdf->GetY(), $pdf->GetX()+$detX, $detY);
    } */
    $pdf->SetFont("Arial","",7);
    $sumcant=0;
    while ($recDet = $db->fetchAssoc($select["doce_det"])) {
        $body = array(
            $recDet["ded_cod"],
            $recDet["ded_desc"],
            $recDet["ded_unimed"],
            strftime("%d/%m/%Y", strtotime($recDet["ded_fvence"])),
            ($recDet["ded_cant"] > 0) ? number_format($recDet["ded_cant"],2) : "",
            ($recDet["ded_cosu"] > 0) ? number_format($recDet["ded_cosu"],5) : "",
            ($recDet["ded_cost"] > 0) ? number_format($recDet["ded_cost"],5) : "",
            ($recDet["ded_boni"] > 0) ? number_format($recDet["ded_boni"],2) : "",
             $recDet["ded_exesn"],
        );
        $pdf->RowBorderHidden($body, $lineHeight);
        $sumcant+=$recDet["ded_cant"];

    }
    $pdf->SetY($pdf->GetY()+.1);
    $pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.9, $pdf->GetY());
    $pdf->SetY($pdf->GetY()+.1);

    $body = array(
        "",
        "             TOTAL DEL DOCUMENTO: ",
        "",
        "",
        ($sumcant > 0) ? number_format($sumcant,2) : "",
        "",
        number_format($rec["de_total"],5),
        "",
        "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $pdf->SetY($pdf->GetY()+.1);
    $pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.9, $pdf->GetY());
    $pdf->SetY($pdf->GetY()+.1);

    $pdf->Ln($lineHeight*4);
    $pdf->Cell($lineWidth/6);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "Hecho por", "T", 0, "C");
    $pdf->Cell($lineWidth/6);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "Revisado por", "T", 0, "C");

    $corr+=1;
    //$pdf->Cell($lineWidth, .1, "",1,1);
    /*$pdf->Cell($lineWidth, $lineHeight, "DATOS FISCALES",1,1,"C");
    $pdf->SetFont("Arial","B");
    $pdf->Cell($lineWidth, $lineHeight, utf8_decode("Fecha de certificación: ").$sxe->xpath('//dte:FechaHoraCertificacion')[0],1,1);
    $pdf->Cell($lineWidth, $lineHeight, utf8_decode("CERTIFICADOR: ").$sxe->xpath('//dte:NombreCertificador')[0],1,1); */
}

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

//$emailAttachment = $pdf->Output('','I');
$db->close();
?>