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

$numdoc=$_GET["numdoc"];
$numNum=$_GET["numdoc"];
$TdociNom=$_GET["TdociNom"];

$arrayError=array();

# QUERYS
$sProrrat = "SELECT * FROM prorrat WHERE prt_emp='$empCod' AND prt_num='$numNum'";

$sPrt_det1 = "SELECT *, TRIM(art.art_ctacos) AS pd1_ctacos, TRIM(art.art_ctainv) AS pd1_ctainv 
             FROM prt_det1 pd1 LEFT JOIN artic art ON pd1.pd1_emp=art.art_emp AND pd1.pd1_codpro=art.art_cod 
             WHERE pd1_emp='$empCod' AND pd1_num='$numNum'";

$sPrt_detc = "SELECT * FROM prt_detc WHERE prdc_emp='$empCod' AND prdc_num='$numNum' AND prdc_vald>0";

$sPrt_detf = "SELECT * FROM prt_detf WHERE prdf_emp='$empCod' AND prdf_num='$numNum' AND prdf_valq>0";

$sPrt_partida = "SELECT * FROM prt_partida WHERE pold_emp='$empCod' AND pold_num='$numNum'";

$lineWidth=25.2;
$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("L", "cm", "Letter");

# EJECUCION DE QUERYS
$select["prorrat"] = $db->query($sProrrat);
$select["prt_det1"] = $db->query($sPrt_det1);
$select["prt_detc"] = $db->query($sPrt_detc);
$select["prt_detf"] = $db->query($sPrt_detf);
$select["prt_partida"] = $db->query($sPrt_partida);

$rec = $db->fetchAssoc($select["prorrat"]);
$pdf->AddPage();

// Paara anular factura
/*$pdf->SetFont('Arial','B',100);
$pdf->SetTextColor(255, 144, 144);
$pdf->SetY(12);
$pdf->Cell($lineWidth, 1, "ANULADO",0,0,"C");*/

$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, "PRORRATEO",0,0,"L");
$pdf->SetFont("Arial","B",14);
$pdf->MultiAlignCell($lineWidth/2, $lineHeight, ("No.  ".$numdoc),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/12, $lineHeight, utf8_decode("FECHA:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, date('d-m-Y', strtotime($rec["prt_fec"])),0,1,"L");
$lineHeight = .42;
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/7, $lineHeight, utf8_decode("TIPO DE DOCUMENTO:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/5, $lineHeight, utf8_decode($TdociNom),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("NUMERO DOCUMENTO:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode($rec["prt_numd"]),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("TASA DE CAMBIO:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, $rec["prt_tcam"],0,1,"L");
$pdf->SetFont("","B");
if ($rec["prt_stat"]=='P') {
  $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("YA PROCESADO"),0,1,"L");
} else {
  $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("PENDIENTE DE PROCESAR"),0,1,"L");
}   
//$pdf->Ln();
//$pdf->Ln();

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

$header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"CANTIDAD","UNIT/FOB Q","TOTAL/FOB Q","% DISTRIB.","OTROS/GASTOS","DAI","COSTO TOTAL Q","UNITARIO Q");
$widths = array(3,7,2,2,2,2,2,2,2,2);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C","C","C","C","C","C","C"));
$pdf->Row($header, $lineHeight);

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

// 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;
$sumtfobq=0;
$sumpord=0;
$sumgastq=0;
$sumdai=0;
$sumvtotal=0;
while ($recDet = $db->fetchAssoc($select["prt_det1"])) {
    $body = array(
        $recDet["pd1_codpro"],
        $recDet["pd1_desc"],
        ($recDet["pd1_cant"] > 0) ? number_format($recDet["pd1_cant"],2) : "",
        ($recDet["pd1_cosu"] > 0) ? number_format($recDet["pd1_cosu"],6) : "",
        ($recDet["pd1_tfobq"] > 0) ? number_format($recDet["pd1_tfobq"],6) : "",
        ($recDet["pd1_pord"] > 0) ? number_format($recDet["pd1_pord"],6) : "",
        ($recDet["pd1_gastq"] > 0) ? number_format($recDet["pd1_gastq"],6) : "",
        ($recDet["pd1_dai"] > 0) ? number_format($recDet["pd1_dai"],6) : "",
        ($recDet["pd1_vtotal"] > 0) ? number_format($recDet["pd1_vtotal"],6) : "",
        ($recDet["pd1_costuq"] > 0) ? number_format($recDet["pd1_costuq"],5) : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $sumcant+=$recDet["pd1_cant"];
    $sumtfobq+=$recDet["pd1_tfobq"];
    $sumpord+=$recDet["pd1_pord"];
    $sumgastq+=$recDet["pd1_gastq"];
    $sumdai+=$recDet["pd1_dai"];
    $sumvtotal+=$recDet["pd1_vtotal"];
}
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+1.8, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

$body = array(
    "",
    "             TOTALES DEL DOCUMENTO: ",
    ($sumcant > 0) ? number_format($sumcant,2) : "",
    "",
    ($sumtfobq > 0) ? number_format($sumtfobq,6) : "",
    ($sumpord > 0) ? number_format($sumpord,6) : "",
    ($sumgastq > 0) ? number_format($sumgastq,6) : "",
    ($sumdai > 0) ? number_format($sumdai,6) : "",
    ($sumvtotal > 0) ? number_format($sumvtotal,6) : "",
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+1.8, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

$pdf->Ln();
$pdf->MultiAlignCell($lineWidth/2.1, $lineHeight, utf8_decode("PARTIDA CONTABLE"),0,1,"L");
$header = array("CUENTA",utf8_decode("DESCRIPCIÓN"),"DEBE","HABER");
$widths = array(3,5,3,3);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C"));
$pdf->Row($header, $lineHeight);
$pdf->SetAligns(array("L","L","R","R"));
$pdf->SetFont("Arial","",7);
$sumdebe=0;
$sumhaber=0;
while ($recDetpart = $db->fetchAssoc($select["prt_partida"])) {
    $body = array(
        $recDetpart["pold_cta"],
        $recDetpart["pold_ctan"],
        ($recDetpart["pold_debe"] > 0) ? number_format($recDetpart["pold_debe"],2) : "",
        ($recDetpart["pold_haber"] > 0) ? number_format($recDetpart["pold_haber"],2) : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $sumdebe+=$recDetpart["pold_debe"];
    $sumhaber+=$recDetpart["pold_haber"];
}
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), 15, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

$body = array(
    "",
    "                TOTAL PARTIDA: ",
    ($sumdebe > 0) ? number_format($sumdebe,2) : "",
    ($sumhaber > 0) ? number_format($sumhaber,2) : "",
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), 15, $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");


//$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("Prorrateo").".pdf","I");

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