<?php
/**
 * phpaga
 *
 * PDF template: show an invoice
 *
 * This file contains the necessary routines to manage expenses.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: invoice.tpl.php 888 2007-11-05 17:01:35Z florian $
 *
 * Copyright (c) 2003, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

$type = $data['type'];
$info = $data['info'];
$recipient = $data['recipient'];
$letterhead = $data['letterhead'];
$watermark = '';


$this->ezSetMargins(140,
                    70,
                    PHPAGA_PDF_BORDER_LEFT,
                    PHPAGA_PDF_BORDER_RIGHT);

if ($letterhead)
{
    if (!isset($lethead) || !strlen($lethead))
        include(PHPAGA_BASEPATH.'pdftemplates/letterhead.tpl.php');
    $this->addObject($lethead,'all');
}

if ($type == PHPAGA_TYPE_INVOICE)
{
    $details = $info['bill_details'];
    $header = array(array('label' => sprintf("%s:", utf8_decode(_('Invoice'))),
                          'text' => $info['bill_number']),

                    array('label' => sprintf("%s:",_('Date')),
                          'text' => date(PHPAGA_DATEFORMAT_PRINT,
                                         strtotime($info['bill_date']))));

    if (isset($info['bill_date_due']) && strlen($info['bill_date_due']))
        $header[] =  array('label' => sprintf("%s:",utf8_decode(_('Date due'))),
                           'text' => date(PHPAGA_DATEFORMAT_PRINT,
                                         strtotime($info['bill_date_due'])));

    if (isset($info['bill_payterm']) && strlen($info['bill_payterm']))
        $header[] =  array('label' => sprintf("%s:",utf8_decode(_('Term of payment'))),
                           'text' => $info['bill_payterm']);

    $watermark = sprintf(_("%s %s / %s"),
                         utf8_decode(_('Invoice')), $info['bill_number'],
                         date(PHPAGA_DATEFORMAT_PRINT,
                              strtotime($info['bill_date'])));

}
else
{
    $details = $info['quot_details'];
    $header = array(array('label' => sprintf("%s:",utf8_decode(_('Quotation'))),
                          'text' => $info['quot_number']),

                    array('label' => sprintf("%s:",utf8_decode(_('Date'))),
                          'text' => date(PHPAGA_DATEFORMAT_PRINT,
                                         strtotime($info['quot_date']))));

    if (isset($info['quot_payterm']) && strlen($info['quot_payterm']))
        $header[] =  array('label' => sprintf("%s:",utf8_decode(_('Term of payment'))),
                           'text' => $info['quot_payterm']);

    $watermark = sprintf(_("%s %s / %s"),
                         utf8_decode(_('Quotation')), $info['quot_number'],
                         date(PHPAGA_DATEFORMAT_PRINT,
                              strtotime($info['quot_date'])));
}

$this->setColor(0.9, 0.9, 0.9, 1);

$this->addText(PHPAGA_PDF_BORDER_LEFT,
               PHPAGA_PDF_POS_Y_RECIPIENT + 20,
               35,
               $watermark);

$this->setColor(0, 0, 0, 1);

if (isset($recipient['cpn_taxnr']) && strlen($recipient['cpn_taxnr'])) {
    $header[] = array('label' => sprintf("%s:",utf8_decode(_('VAT number'))),
                      'text' => $recipient['cpn_taxnr']);
}

if ($recipient != null) {
    include_once(PHPAGA_BASEPATH.'pdftemplates/recipient.tpl.php');
}

$this->ezSetY(PHPAGA_PDF_POS_Y_RECIPIENT);

$this->ezTable($header,
               array('label' => '',
                     'text' => ''),
               '',
               array('xPos' => PHPAGA_PDF_BORDER_LEFT + 10,
                     'xOrientation' => 'right',
                     'showHeadings' => 0,
                     'shaded' => 0,
                     'fontSize' => PHPAGA_PDF_FONT_SIZE_SMALL,
                     'maxWidth' => PHPAGA_PDF_DOCWIDTH -
                     (2 * PHPAGA_PDF_BORDER_LEFT),
                     'showLines' => 0,
                     'cols' =>
                     array('label' =>
                           array('justification' =>
                                 'right'),
                           'text' =>
                           array('justification' =>
                                 'left')),
                     'protectRows' => 5));

$setnum = $this->ezStartPageNumbers(PHPAGA_PDF_DOCWIDTH - PHPAGA_PDF_BORDER_LEFT,
                                    PHPAGA_PDF_SEPARATOR_BOTTOM_HEIGHT - PHPAGA_PDF_HEADER_NEXTLINE,
                                    PHPAGA_PDF_FONT_SIZE_SMALL,
                                    'left',
                                    _('Page').' {PAGENUM} / {TOTALPAGENUM}',
                                    1);

$this->ezSetDY(-PHPAGA_PDF_BILL_NEXTLINE * 2);

/* line items */

if (!isset($info['lineitems'])) {
    $info['lineitems'] = array();

}

$this->ezTable($info['lineitems'],
               array('lit_prodcode' => sprintf("<b>%s</b>", utf8_decode(_('Product code'))),
                     'lit_desc' => sprintf("<b>%s</b>", utf8_decode(_('Description'))),
                     'lit_qty' => sprintf("<b>%s</b>", utf8_decode(_('Qty./Hours'))),
                     'lit_netprice_format' => sprintf("<b>%s</b>", utf8_decode(_('Net price')))),
               '',
               array('xPos' => PHPAGA_PDF_BORDER_LEFT + 5,
                     'xOrientation' => 'right',
                     'showHeadings' => 1,
                     'shaded' => 0,
                     'lineCol' => array(0.5, 0.5, 0.5),
                     'outerLineThickness' => 0.3,
                     'innerLineThickness' => 0.3,
                     'fontSize' => PHPAGA_PDF_FONT_SIZE_SMALL,
                     'width' => (PHPAGA_PDF_DOCWIDTH -
                                 PHPAGA_PDF_BORDER_LEFT -
                                 PHPAGA_PDF_BORDER_RIGHT),
                     'maxWidth' => (PHPAGA_PDF_DOCWIDTH -
                                    PHPAGA_PDF_BORDER_LEFT -
                                    PHPAGA_PDF_BORDER_RIGHT),
                     'showLines' => 2,
                     'cols' => array('lit_qty' => array('justification' => 'right'),
                                     'lit_netprice_format' => array('justification' => 'right'))));

$this->ezSetDY(-PHPAGA_PDF_BILL_NEXTLINE);


/* Expenses */

if (isset($info['expenses']) && array($info['expenses']) && count($info['expenses'])) {

    $expenses = array();

    foreach ($info['expenses'] as $e)
        $expenses[] = array('prj_title' => $e['prj_title'],
                            'exp_date' => date(PHPAGA_DATEFORMAT_PRINT, strtotime($e['exp_date'])),
                            'exp_desc' => $e['exp_desc'],
                            'exp_sum' => phpaga_formatamount($e['exp_sum']));

    $this->ezTable($expenses,
                   array('prj_title' => sprintf("<b>%s</b>", utf8_decode(_('Project'))),
                         'exp_date' => sprintf("<b>%s</b>", utf8_decode(_('Date'))),
                         'exp_desc' => sprintf("<b>%s</b>", utf8_decode(_('Description'))),
                         'exp_sum' => sprintf("<b>%s</b>", utf8_decode(_('Amount')))),
                   utf8_decode(_('Reimbursable expenses')),
                   array('titleFontSize' => PHPAGA_PDF_FONT_SIZE_SMALL,
                         'xPos' => PHPAGA_PDF_BORDER_LEFT + 5,
                         'xOrientation' => 'right',
                         'showHeadings' => 1,
                         'shaded' => 0,
                         'lineCol' => array(0.5, 0.5, 0.5),
                         'outerLineThickness' => 0.3,
                         'innerLineThickness' => 0.3,
                         'fontSize' => PHPAGA_PDF_FONT_SIZE_SMALL,
                         'width' => (PHPAGA_PDF_DOCWIDTH -
                                     PHPAGA_PDF_BORDER_LEFT -
                                     PHPAGA_PDF_BORDER_RIGHT),
                         'maxWidth' => (PHPAGA_PDF_DOCWIDTH -
                                        PHPAGA_PDF_BORDER_LEFT -
                                        PHPAGA_PDF_BORDER_RIGHT),
                         'showLines' => 2,
                         'cols' => array('exp_sum' => array('justification' => 'right'))));

    $this->ezSetDY(-PHPAGA_PDF_BILL_NEXTLINE);

}

/* Details */

$this->ezTable($details,
               array('text' => '',
                     'amount' => ''),
               '',
               array('xPos' => PHPAGA_PDF_BORDER_LEFT + 5,
                     'xOrientation' => 'right',
                     'showHeadings' => 0,
                     'shaded' => 0,
                     'fontSize' => PHPAGA_PDF_FONT_SIZE_SMALL,
                     'width' => (PHPAGA_PDF_DOCWIDTH -
                                 PHPAGA_PDF_BORDER_LEFT -
                                 PHPAGA_PDF_BORDER_RIGHT),
                     'maxWidth' => (PHPAGA_PDF_DOCWIDTH -
                                    PHPAGA_PDF_BORDER_LEFT -
                                    PHPAGA_PDF_BORDER_RIGHT),
                     'showLines' => 2,
                     'lineCol' => array(0.5, 0.5, 0.5),
                     'outerLineThickness' => 0.3,
                     'innerLineThickness' => 0.3,
                     'cols' =>
                     array('amount' =>
                           array('justification' =>
                                 'right')),
                     'protectRows' => 10));

$this->ezStopPageNumbers(1, 1, $setnum);

if (($letterhead) && isset($lethead))
{
    $this->stopObject($lethead);
    $this->ezSetMargins(30, 30, PHPAGA_PDF_BORDER_LEFT, PHPAGA_PDF_BORDER_RIGHT);

}

?>
