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');
44 $cl_invoice_id = (int)$request->getParameter('id');
45 $invoice = ttInvoiceHelper::getInvoice($cl_invoice_id);
47 header('Location: access_denied.php');
50 // End of access checks.
52 $invoice_date = new DateAndTime(DB_DATEFORMAT, $invoice['date']);
53 $client = ttClientHelper::getClient($invoice['client_id'], true);
54 if (!$client) // In case client was deleted.
55 $client = ttClientHelper::getDeletedClient($invoice['client_id']);
57 $invoice_items = ttInvoiceHelper::getInvoiceItems($cl_id);
58 $tax_percent = $client['tax'];
62 foreach($invoice_items as $item)
63 $subtotal += $item['cost'];
65 $tax_expenses = $user->isPluginEnabled('et');
66 foreach($invoice_items as $item) {
67 if ($item['type'] == 2 && !$tax_expenses)
69 $tax += round($item['cost'] * $tax_percent / 100, 2);
72 $total = $subtotal + $tax;
74 $smarty->assign('subtotal', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($subtotal, 2))));
75 if ($tax) $smarty->assign('tax', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($tax, 2))));
76 $smarty->assign('total', $user->currency.' '.str_replace('.', $user->decimal_mark, sprintf('%8.2f', round($total, 2))));
78 if ('.' != $user->decimal_mark) {
79 foreach ($invoice_items as &$item)
80 $item['cost'] = str_replace('.', $user->decimal_mark, $item['cost']);
83 // Calculate colspan for invoice summary.
85 if (MODE_PROJECTS == $user->tracking_mode)
87 elseif (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
90 $form = new Form('invoiceForm');
91 // Hidden control for invoice id.
92 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
93 // invoiceForm only contains controls for "Mark paid" block below invoice table.
94 if ($user->isPluginEnabled('ps')) {
95 $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
96 $form->addInput(array('type'=>'combobox',
97 'name'=>'mark_paid_action_options',
98 'data'=>$mark_paid_action_options,
99 'value'=>$cl_mark_paid_action_option));
100 $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
103 if ($request->isPost()) {
104 if ($request->getParameter('btn_mark_paid')) {
105 // User clicked the "Mark paid" button to mark all invoice items either paid or not paid.
107 // Determine user action.
108 $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
109 ttInvoiceHelper::markPaid($cl_id, $mark_paid);
111 // Re-display this form.
112 header('Location: invoice_view.php?id='.$cl_id);
117 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
118 $smarty->assign('invoice_id', $cl_id);
119 $smarty->assign('invoice_name', $invoice['name']);
120 $smarty->assign('invoice_date', $invoice_date->toString($user->date_format));
121 $smarty->assign('client_name', $client['name']);
122 $smarty->assign('client_address', $client['address']);
123 $smarty->assign('invoice_items', $invoice_items);
124 $smarty->assign('colspan', $colspan);
125 $smarty->assign('title', $i18n->get('title.view_invoice'));
126 $smarty->assign('content_page_name', 'invoice_view.tpl');
127 $smarty->display('index.tpl');