More work in progress on custom fields extension.
[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_client_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 > 0) {
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 $currency = $user->getCurrency();
75 $decimalMark = $user->getDecimalMark();
76
77 $smarty->assign('subtotal', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($subtotal, 2))));
78 if ($tax) $smarty->assign('tax', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($tax, 2))));
79 $smarty->assign('total', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($total, 2))));
80
81 if ('.' != $decimalMark) {
82   foreach ($invoice_items as &$item)
83     $item['cost'] = str_replace('.', $decimalMark, $item['cost']);
84 }
85
86 // Calculate colspan for invoice summary.
87 $colspan = 4;
88 $trackingMode = $user->getTrackingMode();
89 if (MODE_PROJECTS == $trackingMode)
90   $colspan++;
91 elseif (MODE_PROJECTS_AND_TASKS == $trackingMode)
92   $colspan += 2;
93
94 $form = new Form('invoiceForm');
95 // Hidden control for invoice id.
96 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_invoice_id));
97 // invoiceForm only contains controls for "Mark paid" block below invoice table.
98 if ($user->isPluginEnabled('ps')) {
99   $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
100   $form->addInput(array('type'=>'combobox',
101     'name'=>'mark_paid_action_options',
102     'data'=>$mark_paid_action_options,
103     'value'=>$cl_mark_paid_action_option));
104   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
105 }
106
107 if ($request->isPost()) {
108   if ($request->getParameter('btn_mark_paid')) {
109     // User clicked the "Mark paid" button to mark all invoice items either paid or not paid.
110
111     // Determine user action.
112     $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
113     ttInvoiceHelper::markPaid($cl_invoice_id, $mark_paid);
114
115     // Re-display this form.
116     header('Location: invoice_view.php?id='.$cl_invoice_id);
117     exit();
118   }
119 }
120
121 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
122 $smarty->assign('invoice_id', $cl_invoice_id);
123 $smarty->assign('invoice_name', $invoice['name']);
124 $smarty->assign('invoice_date', $invoice_date->toString($user->getDateFormat()));
125 $smarty->assign('client_name', $client['name']);
126 $smarty->assign('client_address', $client['address']);
127 $smarty->assign('show_project', MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode);
128 $smarty->assign('show_task', MODE_PROJECTS_AND_TASKS == $trackingMode);
129 $smarty->assign('invoice_items', $invoice_items);
130 $smarty->assign('colspan', $colspan);
131 $smarty->assign('title', $i18n->get('title.view_invoice'));
132 $smarty->assign('content_page_name', 'invoice_view.tpl');
133 $smarty->display('index.tpl');