<?php
/**
 * This file provides the correspondence class
 * 
 * @author Camil Staps
 * 
 * BusinessAdmin: administrative software for small companies
 * Copyright (C) 2015 Camil Staps (ViviSoft)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('fpdf.php');

/**
 * An extension of FPDF to generate personalized correspondence PDFs
 */
class correspondence extends FPDF {
    /**
     * @var $contact                Holds the contact this correspondence will be sent to
     * @var $language               Holds the language this correspondence should be sent in
     */
    var $contact, $language;
    
    /**
     * If you'd want, you could change these constants to encode an RGB colour for headings in your PDFs
     */
    const HEAD_RED = 0;
    const HEAD_GREEN = 0;
    const HEAD_BLUE = 0;

    /**
     * Array holding the translations
     * 
     * @see _()                     A function to translate
     */
    protected static $translations = array(
        'adres'         => array(
                        'en' => 'Address',
                        'nl' => 'Adres'), 
        'amount'        => array(
                        'en' => 'Amount',
                        'nl' => 'Subtotaal'), 
        'amount-due'    => array(
                        'en' => 'Total amount due',
                        'nl' => 'Te voldoen'), 
        'biccode'       => array(
                        'en' => 'The BIC code of our bank is %%.',
                        'nl' => 'De BIC-code van de bank is %%.'), 
        'btwnr'         => array(
                        'en' => 'VAT nr',
                        'nl' => 'BTW nr'), 
        'description'   => array(
                        'en' => 'Description',
                        'nl' => 'Omschrijving'), 
        'due-date'      => array(
                        'en' => 'Due date',
                        'nl' => 'Vervaldatum'), 
        'email'         => array(
                        'en' => 'Email',
                        'nl' => 'Email'),
        'iban'          => array(
                        'en' => 'IBAN',
                        'nl' => 'IBAN'),
        'invoice'       => array(
                        'en' => 'Invoice',
                        'nl' => 'Factuur'), 
        'invoice-date'  => array(
                        'en' => 'Invoice date',
                        'nl' => 'Factuurdatum'), 
        'invoice-nr'    => array(
                        'en' => 'Invoice number',
                        'nl' => 'Factuurnummer'), 
        'price-excl'    => array(
                        'en' => 'Price excl.',
                        'nl' => 'Prijs excl.'), 
        'price-incl'    => array(
                        'en' => 'Price incl.',
                        'nl' => 'Prijs incl.'), 
        'request'       =>  array(
                        'en' => 'You are kindly requested to transfer the total amount before the due date to the provided IBAN nr.',
                        'nl' => 'U wordt vriendelijk verzocht het te voldoen bedrag voor de vervaldatum van de factuur over te maken naar opgegeven IBAN-nummer.'), 
        'tel-nr'        => array(
                        'en' => 'Tel.',
                        'nl' => 'Tel.'),
        'total'         => array(
                        'en' => 'Total',
                        'nl' => 'Totaal'), 
        'vat'           => array(
                        'en' => 'VAT',
                        'nl' => 'BTW'), 
    );

    /**
     * Translate a string
     * 
     * @see $translations           The array holding the translations
     * 
     * @param string $key           The string to translate
     * @param string $lang          The language to translate to (two-letter code)
     * 
     * @return string               The translated string
     */
    public function _($key) {
        if (!array_key_exists($key, self::$translations)) 
            return $key;
        return self::$translations[$key][$this->language];
    }
    
    /**
     * Create a new PDF
     * 
     * @param string $orientation   See the FPDF class specs
     * @param string $unit          See the FPDF class specs
     * @param string $size          See the FPDF class specs
     */
    function __construct($orientation='P',$unit='mm',$size='A4') {
        $this->FPDF($orientation,$unit,$size);
        
        $this->SetMargins(30,20,20);
        $this->SetAuthor(constants::invoice_name);
        $this->SetCreator('BusinessAdmin Correspondence Generator');
        $this->SetDisplayMode('fullpage','continuous');
    }

    /**
     * Set the language of the correspondence (used to translate stuff)
     * 
     * @param string $lang          The language, two letter code ('en', 'nl', ...)
     */
    function SetLangauge($lang) {
        $supported = array('nl', 'en');
        if (!in_array($lang, $supported)) {
            throw new Exception("Language not supported");
        } else {
            $this->language = $lang;
        }
    }
    
    /**
     * Set the contact to whom this correspondence will be sent (used for the address)
     * 
     * @param contact $contact      The contact
     */
    function SetContact($contact) {
        $this->contact = $contact;

        $this->language = $contact->getLanguage();
    }

    /**
     * Makes a header with your details and the address of the contact
     */
    function CorrespondenceHeader() {
        if (file_exists(constants::files_folder . 'logo-correspondence.png')) {
            $this->Image(constants::files_folder . 'logo-correspondence.png',30,20,50);
        }
        
        $this->SetFont('Helvetica','',7);
        $this->Cell(110,20);
        $this->Cell(12,1,' ','T');
        $this->Cell(2,1);
        $this->Cell(35,1,' ','T');
        $this->Ln();
        
        $this->Cell(110,3.5);
        $this->SetFont('','B');
        $this->Cell(12,3.5,$this->_('adres'),'',0,'R');
        $this->Cell(2,3.5);
        $this->SetFont('','');
        $this->Cell(35,3.5, constants::invoice_name);
        $this->Ln();
        
        $this->Cell(124,3.5);
        $this->Cell(35,3.5, constants::invoice_address_1);
        $this->Ln();
        $this->Cell(124,3.5);
        $this->Cell(35,3.5, constants::invoice_address_2);
        $this->Ln();
        $this->Cell(124,3.5);
        $this->Cell(35,3.5, constants::invoice_address_3);
        $this->Ln();
        $this->Cell(160,1.5);
        $this->Ln();
        $this->Cell(110,3.5);
        $this->SetFont('','B');
        $this->Cell(12,3.5,$this->_('btwnr'),'',0,'R');
        $this->Cell(2,3.5);
        $this->SetFont('','');
        $this->Cell(35,3.5, constants::invoice_tax_nr);
        $this->Ln();
        $this->Cell(160,1.5);
        $this->Ln();
        $this->Cell(110,3.5);
        $this->SetFont('','B');
        $this->Cell(12,3.5, $this->_('iban'),'',0,'R');
        $this->Cell(2,3.5);
        $this->SetFont('','');
        $this->Cell(35,3.5, constants::invoice_iban);
        $this->Ln();
        $this->Cell(160,1.5);
        $this->Ln();
        $this->Cell(110,3.5);
        $this->SetFont('','B');
        $this->Cell(12,3.5, $this->_('tel-nr'),'',0,'R');
        $this->Cell(2,3.5);
        $this->SetFont('','',7);
        $this->Cell(35,3.5, constants::invoice_tel_nr);
        $this->Ln();
        $this->Cell(110,3.5);
        $this->SetFont('','B');
        $this->Cell(12,3.5, $this->_('email'),'',0,'R');
        $this->Cell(2,3.5);
        $this->SetFont('','');
        $this->Cell(35,3.5, constants::invoice_email);
        $this->Ln();
        $this->Cell(110,1);
        $this->Cell(12,1,' ','B');
        $this->Cell(2,1);
        $this->Cell(35,1,' ','B');
        $this->Ln();
        $this->Cell(160,15);
        $this->Ln();
        
        $this->SetFont('','',11);
        $this->SetXY(30,56.5);
        $this->Cell(90,5.5,utf8_decode($this->contact->getName()));
        $this->Ln();
        $this->SetXY(30,61.5);
        $this->Cell(90,5.5,utf8_decode($this->contact->getAddress()));
        $this->Ln();
        $this->SetXY(30,66.5);
        $this->Cell(90,5.5,utf8_decode($this->contact->getPostalCode().' '.$this->contact->getCity()));
        $this->Ln();
        $this->SetXY(30,71.5);
        $this->Cell(90,5.5,utf8_decode($this->contact->getCountry()));
        $this->Ln();
    }
    
    /**
     * Put PDF information
     */
    function _putinfo()
    {
        $this->_out('/Producer '.$this->_textstring('BusinessAdmin Correspondence Generator'));
        if(!empty($this->title))
            $this->_out('/Title '.$this->_textstring($this->title));
        if(!empty($this->subject))
            $this->_out('/Subject '.$this->_textstring($this->subject));
        if(!empty($this->author))
            $this->_out('/Author '.$this->_textstring($this->author));
        if(!empty($this->keywords))
            $this->_out('/Keywords '.$this->_textstring($this->keywords));
        if(!empty($this->creator))
            $this->_out('/Creator '.$this->_textstring($this->creator));
        $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis')));
    }
}