<?php
session_start();
header('Content-Type: text/html; charset=UTF-8');
require('fpdf186/fpdf.php');

date_default_timezone_set("America/La_Paz");
include "core/autoload.php";

include "core/app/model/ChanceData.php";
include "core/app/model/PersonData.php";
include "core/app/model/ProductData.php";
include "core/app/model/ProductStockData.php";
include "core/app/model/ProposalData.php";
include "core/app/model/ProposalDetailData.php";
include "core/app/model/UserData.php";

class PDF extends FPDF
{
    protected $widths;
    protected $aligns;

    function SetWidths($w)
    {
        // Set the array of column widths
        $this->widths = $w;
    }

    function SetAligns($a)
    {
        // Set the array of column alignments
        $this->aligns = $a;
    }

    function Row($data)
    {
        // Calculate the height of the row
        $nb = 0;
        for ($i = 0; $i < count($data); $i++)
            $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
        $h = 5 * $nb;
        // Issue a page break first if needed
        $this->CheckPageBreak($h);
        // Draw the cells of the row
        for ($i = 0; $i < count($data); $i++) {
            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            // Save the current position
            $x = $this->GetX();
            $y = $this->GetY();
            // Draw the border
            //$this->Rect($x, $y, $w, $h);
            // Print the text
            $this->MultiCell($w, 5, $data[$i], 0, $a);
            // Put the position to the right of the cell
            $this->SetXY($x + $w, $y);
        }
        // Go to the next line
        $this->Ln($h);
    }

    function CheckPageBreak($h)
    {
        // If the height h would cause an overflow, add a new page immediately
        if ($this->GetY() + $h > $this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);
    }

    function NbLines($w, $txt)
    {
        // Compute the number of lines a MultiCell of width w will take
        if (!isset($this->CurrentFont))
            $this->Error('No font has been set');
        $cw = $this->CurrentFont['cw'];
        if ($w == 0)
            $w = $this->w - $this->rMargin - $this->x;
        $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
        $s = str_replace("\r", '', (string)$txt);
        $nb = strlen($s);
        if ($nb > 0 && $s[$nb - 1] == "\n")
            $nb--;
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while ($i < $nb) {
            $c = $s[$i];
            if ($c == "\n") {
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
                continue;
            }
            if ($c == ' ')
                $sep = $i;
            $l += $cw[$c];
            if ($l > $wmax) {
                if ($sep == -1) {
                    if ($i == $j)
                        $i++;
                } else
                    $i = $sep + 1;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
            } else
                $i++;
        }
        return $nl;
    }
    function Header()
    {

        //Logo
        $this->SetFillColor(0, 0, 0);
        $this->Image('logo_total.jpg', 130, 10, 60);
        $this->Cell(189, 20, '', 0, 1); //end of line
        //Arial bold 15
        $this->SetFont('Arial', 'B', 8);
        //Movernos a la derecha
        $this->Cell(30);
        //T�tulo
        //$this->Cell(21, 5, '', 0, 0, 'C');
        //$this->Cell(100);
        $this->SetFont('Arial', 'B', 25);
        $this->Cell(5, 5, 'PROFORMA', 0, 1, 'C');
        //$this->Cell(21, 5, 'Nro.' . $GLOBALS['bill'], 0, 1, 'C');
        $this->Cell(189, 15, '', 0, 1); //
        $this->SetFont('Arial', 'B', 16);
        $this->Cell(5, 4, '', 0, 0);
        $this->Cell(5, 8, 'FECHA', 0, 0,);
        $this->Cell(50);
        $this->Cell(5, 8, 'NRO PROFORMA', 0, 0);
        $this->Cell(60);
        $this->SetFont('Arial', '', 12);
        $this->Cell(60, 8, 'Av. Felipe Bestres, esquina', 0, 1);
        $this->Cell(15, 4, '', 0, 0);
        $this->Cell(5, 4, $GLOBALS['dia'] . '/' . $GLOBALS['mes'] . '/' . $GLOBALS['anio'], 0, 0);
        $this->Cell(50);
        $this->Cell(5, 4, $GLOBALS['bill'], 0, 0);
        $this->Cell(50);
        $this->Cell(60, 6, 'Av. Satelite, Ciudad Satelite - ', 0, 1);
        $this->Cell(125);
        $this->Cell(60, 6, 'El Alto, La Paz ', 0, 1);
        $this->Cell(125);
        $this->Cell(60, 6, '76222236-63167327', 0, 1);
        $this->Cell(125);
        $this->Cell(60, 6, 'ventas@vanaeph.com', 0, 1);
        //Salto de l�nea
        $this->Ln(5);
        //$this->SetAlpha(0.1);
        //$this->Image('fondo9.png', 16, 60, $fpdf->w, $fpdf->h);
        //$this->SetAlpha(1);
    }

    function Footer()
    {

        $this->SetY(-20);
        $this->SetFont('Arial', 'B', 8);

        $this->Cell(60, 5, 'Vanaeph', 1, 0, 'C');
        $this->Cell(60, 5, 'Codigo: FRM-CMZ-001.00', 1, 0, 'C');
        $this->Cell(60, 5, 'V: 01/11/2022', 1, 1, 'C');
    }

    protected $B = 0;
    protected $I = 0;
    protected $U = 0;
    protected $HREF = '';

    function WriteHTML($html)
    {
        // HTML parser
        $html = str_replace("\n", ' ', $html);
        $a = preg_split('/<(.*)>/U', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
        foreach ($a as $i => $e) {
            if ($i % 2 == 0) {
                // Text
                if ($this->HREF)
                    $this->PutLink($this->HREF, $e);
                else
                    $this->Write(5, $e);
            } else {
                // Tag
                if ($e[0] == '/')
                    $this->CloseTag(strtoupper(substr($e, 1)));
                else {
                    // Extract attributes
                    $a2 = explode(' ', $e);
                    $tag = strtoupper(array_shift($a2));
                    $attr = array();
                    foreach ($a2 as $v) {
                        if (preg_match('/([^=]*)=["\']?([^"\']*)/', $v, $a3))
                            $attr[strtoupper($a3[1])] = $a3[2];
                    }
                    $this->OpenTag($tag, $attr);
                }
            }
        }
    }

    function OpenTag($tag, $attr)
    {
        // Opening tag
        if ($tag == 'B' || $tag == 'I' || $tag == 'U')
            $this->SetStyle($tag, true);
        if ($tag == 'A')
            $this->HREF = $attr['HREF'];
        if ($tag == 'BR')
            $this->Ln(5);
    }

    function CloseTag($tag)
    {
        // Closing tag
        if ($tag == 'B' || $tag == 'I' || $tag == 'U')
            $this->SetStyle($tag, false);
        if ($tag == 'A')
            $this->HREF = '';
    }

    function SetStyle($tag, $enable)
    {
        // Modify style and select corresponding font
        $this->$tag += ($enable ? 1 : -1);
        $style = '';
        foreach (array('B', 'I', 'U') as $s) {
            if ($this->$s > 0)
                $style .= $s;
        }
        $this->SetFont('', $style);
    }

    function PutLink($URL, $txt)
    {
        // Put a hyperlink
        $this->SetTextColor(0, 0, 255);
        $this->SetStyle('U', true);
        $this->Write(5, $txt, $URL);
        $this->SetStyle('U', false);
        $this->SetTextColor(0);
    }
}

$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, '¡Hola, Mundo!');
$pdf->Output();
