More fixes to invoice view.
[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 import('form.Form');
34
35 // Access checks.
36 if (!(ttAccessAllowed('manage_invoices') || ttAccessAllowed('view_own_invoices'))) {
37   header('Location: access_denied.php');
38   exit();
39 }
40 if (!$user->isPluginEnabled('iv')) {
41   header('Location: feature_disabled.php');
42   exit();
43 }
44 $cl_invoice_id = (int)$request->getParameter('id');
45 $invoice = ttInvoiceHelper::getInvoice($cl_invoice_id);
46 if (!$invoice) {
47   header('Location: access_denied.php');
48   exit();
49 }
50 // End of access checks.
51
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']);
56
57 $invoice_items = ttInvoiceHelper::getInvoiceItems($cl_invoice_id);
58 $tax_percent = $client['tax'];
59
60 $subtotal = 0;
61 $tax = 0;
62 foreach($invoice_items as $item)
63   $subtotal += $item['cost'];
64 if ($tax_percent) {
65   $tax_expenses = $user->isPluginEnabled('et');
66   foreach($invoice_items as $item) {
67     if ($item['type'] == 2 && !$tax_expenses)
68       continue;
69     $tax += round($item['cost'] * $tax_percent / 100, 2);
70   }
71 }
72 $total = $subtotal + $tax; 
73
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))));
77
78 if ('.' != $user->decimal_mark) {
79   foreach ($invoice_items as &$item)
80     $item['cost'] = str_replace('.', $user->decimal_mark, $item['cost']);
81 }
82
83 // Calculate colspan for invoice summary.
84 $colspan = 4;
85 if (MODE_PROJECTS == $user->tracking_mode)
86   $colspan++;
87 elseif (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
88   $colspan += 2;
89
90 $form = new Form('invoiceForm');
91 // Hidden control for invoice id.
92 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_invoice_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')));
101 }
102
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.
106
107     // Determine user action.
108     $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
109     ttInvoiceHelper::markPaid($cl_invoice_id, $mark_paid);
110
111     // Re-display this form.
112     header('Location: invoice_view.php?id='.$cl_invoice_id);
113     exit();
114   }
115 }
116
117 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
118 $smarty->assign('invoice_id', $cl_invoice_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');