Starting work on timesheet view.
[timetracker.git] / timesheet_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('ttTimesheetHelper');
31
32 // Access checks.
33 if (!(ttAccessAllowed('view_own_timesheets') || ttAccessAllowed('view_timesheets') || ttAccessAllowed('view_all_timesheets') || ttAccessAllowed('view_client_timesheets'))) {
34   header('Location: access_denied.php');
35   exit();
36 }
37 if (!$user->isPluginEnabled('ts')) {
38   header('Location: feature_disabled.php');
39   exit();
40 }
41 $timesheet_id = (int)$request->getParameter('id');
42 $timesheet = ttTimesheetHelper::getTimesheet($timesheet_id);
43 if (!$timesheet) {
44   header('Location: access_denied.php');
45   exit();
46 }
47 // TODO: add other checks here for timesheet being appropriate for user role.
48 // End of access checks.
49
50 $invoice_date = new DateAndTime(DB_DATEFORMAT, $invoice['date']);
51 $client = ttClientHelper::getClient($invoice['client_id'], true);
52 if (!$client) // In case client was deleted.
53   $client = ttClientHelper::getDeletedClient($invoice['client_id']);
54
55 $invoice_items = ttInvoiceHelper::getInvoiceItems($cl_invoice_id);
56 $tax_percent = $client['tax'];
57
58 $subtotal = 0;
59 $tax = 0;
60 foreach($invoice_items as $item)
61   $subtotal += $item['cost'];
62 if ($tax_percent > 0) {
63   $tax_expenses = $user->isPluginEnabled('et');
64   foreach($invoice_items as $item) {
65     if ($item['type'] == 2 && !$tax_expenses)
66       continue;
67     $tax += round($item['cost'] * $tax_percent / 100, 2);
68   }
69 }
70 $total = $subtotal + $tax;
71
72 $currency = $user->getCurrency();
73 $decimalMark = $user->getDecimalMark();
74
75 $smarty->assign('subtotal', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($subtotal, 2))));
76 if ($tax) $smarty->assign('tax', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($tax, 2))));
77 $smarty->assign('total', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($total, 2))));
78
79 if ('.' != $decimalMark) {
80   foreach ($invoice_items as &$item)
81     $item['cost'] = str_replace('.', $decimalMark, $item['cost']);
82 }
83
84 // Calculate colspan for invoice summary.
85 $colspan = 4;
86 $trackingMode = $user->getTrackingMode();
87 if (MODE_PROJECTS == $trackingMode)
88   $colspan++;
89 elseif (MODE_PROJECTS_AND_TASKS == $trackingMode)
90   $colspan += 2;
91
92 $form = new Form('invoiceForm');
93 // Hidden control for invoice id.
94 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_invoice_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')));
103 }
104
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.
108
109     // Determine user action.
110     $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
111     ttInvoiceHelper::markPaid($cl_invoice_id, $mark_paid);
112
113     // Re-display this form.
114     header('Location: invoice_view.php?id='.$cl_invoice_id);
115     exit();
116   }
117 }
118
119 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
120 $smarty->assign('invoice_id', $cl_invoice_id);
121 $smarty->assign('timesheet_name', $timesheet['name']);
122 $smarty->assign('invoice_date', $invoice_date->toString($user->getDateFormat()));
123 $smarty->assign('client_name', $client['name']);
124 $smarty->assign('client_address', $client['address']);
125 $smarty->assign('show_project', MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode);
126 $smarty->assign('show_task', MODE_PROJECTS_AND_TASKS == $trackingMode);
127 $smarty->assign('invoice_items', $invoice_items);
128 $smarty->assign('colspan', $colspan);
129 $smarty->assign('title', $i18n->get('title.timesheet'));
130 $smarty->assign('content_page_name', 'timesheet_view.tpl');
131 $smarty->display('index.tpl');