<?php
/**
 * phpaga
 *
 * PDF template: show an invoice
 *
 * This file contains the necessary routines to manage expenses.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @author Michael Kimmick <support@nichewares.com>
 * @version $Id: invoice.tpl.php 966 2009-03-18 16:40:47Z mkimmick $
 *
 * 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 = '';
$additional_information = '';

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

$config = phpaga_config_get();

if ($letterhead)
{
    if (!isset($lethead) || !strlen($lethead))
        include(PHPAGA_PDFTEMPLATESPATH.'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']);

	if (isset($info['bill_additional_line']) && strlen($info['bill_additional_line'])) {
	  $additional_information = utf8_decode($info['bill_additional_line']);
	}

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

	if (isset($info['quot_additional_line']) && strlen($info['quot_additional_line'])) {
	  $additional_information = utf8_decode($info['quot_additional_line']);
	}

    $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_PDFTEMPLATESPATH.'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 * 3);

if (strlen($additional_information)) {
  $this->ezSetDY(-PHPAGA_PDF_BILL_NEXTLINE * 2);
  $this->ezText($additional_information, PHPAGA_PDF_FONT_SIZE_SMALL);
  $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')))),
               sprintf("<b>%s</b>", utf8_decode(_('Time and Materials'))),
               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('lit_qty' => array('justification' => 'center'),
                                     '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' => $config['MONETARY_SYMBOL'].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')))),
                   sprintf("<b>%s</b>", 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);

}

/* Payments */

if (isset($info['payments']) && isset($info['pmtsum']) && array($info['payments']) && count($info['payments']) && count($info['pmtsum'])) {
    $payments = array();

    foreach ($info['payments'] as $pmt)
	$payments[] = array('pmt_date' => date(PHPAGA_DATEFORMAT_PRINT, strtotime($pmt['pmt_date'])),
	    'pmt_note' => $pmt['pmt_note'],
	    'pmt_amt' => "-".$config['MONETARY_SYMBOL'].phpaga_formatamount($pmt['pmt_amt']),
	    'pmt_sum' => $config['MONETARY_SYMBOL'].phpaga_formatamount($info['pmtsum']['pmt_sum']));

	$this->ezTable($payments,
			array('pmt_date' => sprintf("<b>%s</b>", utf8_decode(_('Payment Date'))),
				'pmt_note' => sprintf("<b>%s</b>", utf8_decode(_('Payment Note'))),
				'pmt_amt' => sprintf("<b>%s</b>", utf8_decode(_('Amount')))),
			sprintf("<b>%s</b>", utf8_decode(_('Payments'))),
			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('pmt_amt' => array('justification' => 'right'))));

	$this->ezSetDY(-PHPAGA_PDF_BILL_NEXTLINE);
}

/* Late Fees */

if (isset($info['latefees']) && isset($info['feesum']) && array($info['latefees']) && count($info['latefees']) && count($info['feesum']) ) {
    $latefees = array();

    foreach ($info['latefees'] as $fee)
	$latefees[] = array('fee_date' => date(PHPAGA_DATEFORMAT_PRINT, strtotime($fee['fee_date'])),
	    'fee_amt' => $config['MONETARY_SYMBOL'].phpaga_formatamount($fee['fee_amt']),
	    'fee_sum' => $info['feesum']['fee_sum']);

	$this->ezTable($latefees,
			array('fee_date' => sprintf("<b>%s</b>", utf8_decode(_('Date'))),
			      'fee_amt' => sprintf("<b>%s</b>", utf8_decode(_('Amount')))),
			sprintf("<b>%s</b>", utf8_decode(_('Late Fees'))),
			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('fee_amt' => array('justification' => 'right'))));

	$this->ezSetDY(-PHPAGA_PDF_BILL_NEXTLINE);
}

/* Details */

$this->ezTable($details,
		array('text' => '',
		      'amount' => sprintf("<b>%s</b>", utf8_decode(_('Amount')))),
		sprintf("<b>%s</b>", utf8_decode(_('Summary'))),
		array('titleFontSize' => PHPAGA_PDF_FONT_SIZE_SMALL,
		      'xPos' => PHPAGA_PDF_BORDER_LEFT + 5,
		      'xOrientation' => 'right',
		      'showHeadings' => 1,
		      '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);

}

?>
