Removed PHP closing tag in more files
[timetracker.git] / invoice_view.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('DateAndTime');
31 import('ttInvoiceHelper');
32 import('ttClientHelper');
33
34 // Access check.
35 if (!ttAccessCheck(right_view_invoices)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 $invoice_id = (int)$request->getParameter('id');
41 $invoice = ttInvoiceHelper::getInvoice($invoice_id);
42 $invoice_date = new DateAndTime(DB_DATEFORMAT, $invoice['date']);
43 $client = ttClientHelper::getClient($invoice['client_id'], true);
44 if (!$client) // In case client was deleted.
45   $client = ttClientHelper::getDeletedClient($invoice['client_id']);
46
47 $invoice_items = ttInvoiceHelper::getInvoiceItems($invoice_id);
48 $tax_percent = $client['tax'];
49
50 $subtotal = 0;
51 $tax = 0;
52 foreach($invoice_items as $item)
53   $subtotal += $item['cost'];
54 if ($tax_percent) {
55   $tax_expenses = in_array('et', explode(',', $user->plugins));
56   foreach($invoice_items as $item) {
57         if ($item['type'] == 2 && !$tax_expenses)
58           continue;
59     $tax += round($item['cost'] * $tax_percent / 100, 2);       
60   }
61 }
62 $total = $subtotal + $tax; 
63
64 $smarty->assign('subtotal', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($subtotal, 2))));
65 if ($tax) $smarty->assign('tax', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($tax, 2))));
66 $smarty->assign('total', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($total, 2))));
67
68 if ('.' != $user->decimal_mark) {
69   foreach ($invoice_items as &$item)
70         $item['cost'] = str_replace('.', $user->decimal_mark, $item['cost']);
71 }
72
73 // Calculate colspan for invoice summary.
74 $colspan = 4;
75 if (MODE_PROJECTS == $user->tracking_mode)
76   $colspan++;
77 else if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
78   $colspan += 2;
79
80 $smarty->assign('invoice_id', $invoice_id);
81 $smarty->assign('invoice_name', $invoice['name']);
82 $smarty->assign('invoice_date', $invoice_date->toString($user->date_format));
83 $smarty->assign('client_name', $client['name']);
84 $smarty->assign('client_address', $client['address']);
85 $smarty->assign('invoice_items', $invoice_items);
86 $smarty->assign('colspan', $colspan);
87 $smarty->assign('title', $i18n->getKey('title.view_invoice'));
88 $smarty->assign('content_page_name', 'invoice_view.tpl');
89 $smarty->display('index.tpl');