2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 require_once('initialize.php');
 
  30 import('DateAndTime');
 
  31 import('ttInvoiceHelper');
 
  32 import('ttClientHelper');
 
  36 if (!(ttAccessAllowed('manage_invoices') || ttAccessAllowed('view_own_invoices'))) {
 
  37   header('Location: access_denied.php');
 
  40 if (!$user->isPluginEnabled('iv')) {
 
  41   header('Location: feature_disabled.php');
 
  45 $cl_id = (int)$request->getParameter('id');
 
  46 $invoice = ttInvoiceHelper::getInvoice($cl_id);
 
  47 // Temporary fix for invalid invoice id. TODO: implement properly and review security of other pages,
 
  48 // where item id is passed (or posted) as parameter.
 
  50   header('Location: access_denied.php');
 
  54 $invoice_date = new DateAndTime(DB_DATEFORMAT, $invoice['date']);
 
  55 $client = ttClientHelper::getClient($invoice['client_id'], true);
 
  56 if (!$client) // In case client was deleted.
 
  57   $client = ttClientHelper::getDeletedClient($invoice['client_id']);
 
  59 $invoice_items = ttInvoiceHelper::getInvoiceItems($cl_id);
 
  60 $tax_percent = $client['tax'];
 
  64 foreach($invoice_items as $item)
 
  65   $subtotal += $item['cost'];
 
  67   $tax_expenses = $user->isPluginEnabled('et');
 
  68   foreach($invoice_items as $item) {
 
  69     if ($item['type'] == 2 && !$tax_expenses)
 
  71     $tax += round($item['cost'] * $tax_percent / 100, 2);
 
  74 $total = $subtotal + $tax; 
 
  76 $smarty->assign('subtotal', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($subtotal, 2))));
 
  77 if ($tax) $smarty->assign('tax', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($tax, 2))));
 
  78 $smarty->assign('total', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($total, 2))));
 
  80 if ('.' != $user->decimal_mark) {
 
  81   foreach ($invoice_items as &$item)
 
  82     $item['cost'] = str_replace('.', $user->decimal_mark, $item['cost']);
 
  85 // Calculate colspan for invoice summary.
 
  87 if (MODE_PROJECTS == $user->tracking_mode)
 
  89 elseif (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
 
  92 $form = new Form('invoiceForm');
 
  93 // Hidden control for invoice id.
 
  94 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
 
  95 // invoiceForm only contains controls for "Mark paid" block below invoice table.
 
  96 if ($user->isPluginEnabled('ps')) {
 
  97   $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
 
  98   $form->addInput(array('type'=>'combobox',
 
  99     'name'=>'mark_paid_action_options',
 
 100     'data'=>$mark_paid_action_options,
 
 101     'value'=>$cl_mark_paid_action_option));
 
 102   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
 
 105 if ($request->isPost()) {
 
 106   if ($request->getParameter('btn_mark_paid')) {
 
 107     // User clicked the "Mark paid" button to mark all invoice items either paid or not paid.
 
 109     // Determine user action.
 
 110     $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
 
 111     ttInvoiceHelper::markPaid($cl_id, $mark_paid);
 
 113     // Re-display this form.
 
 114     header('Location: invoice_view.php?id='.$cl_id);
 
 119 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 120 $smarty->assign('invoice_id', $cl_id);
 
 121 $smarty->assign('invoice_name', $invoice['name']);
 
 122 $smarty->assign('invoice_date', $invoice_date->toString($user->date_format));
 
 123 $smarty->assign('client_name', $client['name']);
 
 124 $smarty->assign('client_address', $client['address']);
 
 125 $smarty->assign('invoice_items', $invoice_items);
 
 126 $smarty->assign('colspan', $colspan);
 
 127 $smarty->assign('title', $i18n->get('title.view_invoice'));
 
 128 $smarty->assign('content_page_name', 'invoice_view.tpl');
 
 129 $smarty->display('index.tpl');