Cosmetic formatting fixes
[timetracker.git] / expenses.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('form.Form');
31 import('ttUserHelper');
32 import('ttTeamHelper');
33 import('DateAndTime');
34 import('ttExpenseHelper');
35
36 // Access check.
37 if (!ttAccessCheck(right_data_entry)) {
38   header('Location: access_denied.php');
39   exit();
40 }
41
42 // Initialize and store date in session.
43 $cl_date = $request->getParameter('date', @$_SESSION['date']);
44 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
45 if($selected_date->isError())
46   $selected_date = new DateAndTime(DB_DATEFORMAT);
47 if(!$cl_date)
48   $cl_date = $selected_date->toString(DB_DATEFORMAT);
49 $_SESSION['date'] = $cl_date;
50
51 // Initialize variables.
52 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id']) ? $_SESSION['behalf_id'] : $user->id));
53 $cl_client = $request->getParameter('client', ($request->getMethod()=='POST' ? null : @$_SESSION['client']));
54 $_SESSION['client'] = $cl_client;
55 $cl_project = $request->getParameter('project', ($request->getMethod()=='POST' ? null : @$_SESSION['project']));
56 $_SESSION['project'] = $cl_project;
57 $cl_item_name = $request->getParameter('item_name');
58 $cl_cost = $request->getParameter('cost');
59
60 // Elements of expensesForm.
61 $form = new Form('expensesForm');
62
63 if ($user->canManageTeam()) {
64   $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
65   if (count($user_list) > 1) {
66     $form->addInput(array('type'=>'combobox',
67       'onchange'=>'this.form.submit();',
68       'name'=>'onBehalfUser',
69       'style'=>'width: 250px;',
70       'value'=>$on_behalf_id,
71       'data'=>$user_list,
72       'datakeys'=>array('id','name')));
73     $smarty->assign('on_behalf_control', 1);
74   }
75 }
76
77 // Dropdown for clients in MODE_TIME. Use all active clients.
78 if (MODE_TIME == $user->tracking_mode && in_array('cl', explode(',', $user->plugins))) {
79     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
80     $form->addInput(array('type'=>'combobox',
81       'onchange'=>'fillProjectDropdown(this.value);',
82       'name'=>'client',
83       'style'=>'width: 250px;',
84       'value'=>$cl_client,
85       'data'=>$active_clients,
86       'datakeys'=>array('id', 'name'),
87       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
88   // Note: in other modes the client list is filtered to relevant clients only. See below.
89 }
90
91 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
92   // Dropdown for projects assigned to user.
93   $project_list = $user->getAssignedProjects();
94   $form->addInput(array('type'=>'combobox',
95     // 'onchange'=>'fillTaskDropdown(this.value);',
96     'name'=>'project',
97     'style'=>'width: 250px;',
98     'value'=>$cl_project,
99     'data'=>$project_list,
100     'datakeys'=>array('id','name'),
101     'empty'=>array(''=>$i18n->getKey('dropdown.select'))
102   ));
103
104   // Dropdown for clients if the clients plugin is enabled.
105   if (in_array('cl', explode(',', $user->plugins))) {
106     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
107     // We need an array of assigned project ids to do some trimming. 
108     foreach($project_list as $project)
109       $projects_assigned_to_user[] = $project['id'];
110
111     // Build a client list out of active clients. Use only clients that are relevant to user.
112     // Also trim their associated project list to only assigned projects (to user).
113     foreach($active_clients as $client) {
114       $projects_assigned_to_client = explode(',', $client['projects']);
115       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
116       if ($intersection) {
117         $client['projects'] = implode(',', $intersection);
118         $client_list[] = $client;
119       }
120     }
121     $form->addInput(array('type'=>'combobox',
122       'onchange'=>'fillProjectDropdown(this.value);',
123       'name'=>'client',
124       'style'=>'width: 250px;',
125       'value'=>$cl_client,
126       'data'=>$client_list,
127       'datakeys'=>array('id', 'name'),
128       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
129   }
130 }
131 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
132 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
133 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
134 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
135 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
136
137 // Determine lock date. Time entries earlier than lock date cannot be created or modified. 
138 $lock_interval = $user->lock_interval;
139 $lockdate = 0;
140 if ($lock_interval > 0) {
141   $lockdate = new DateAndTime();
142   $lockdate->decDay($lock_interval);
143 }
144
145 // Submit.
146 if ($request->getMethod() == 'POST') {
147   if ($request->getParameter('btn_submit')) {
148     // Validate user input.
149     if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
150       $errors->add($i18n->getKey('error.client'));
151     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
152       if (!$cl_project) $errors->add($i18n->getKey('error.project'));
153     }
154     if (!ttValidString($cl_item_name)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
155     if (!ttValidFloat($cl_cost)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
156
157     // Prohibit creating entries in future.
158     if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
159       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
160       if ($selected_date->after($browser_today))
161         $errors->add($i18n->getKey('error.future_date'));
162     }
163     // Finished validating input data.
164
165     // Prohibit creating time entries in locked interval.
166     if($lockdate && $selected_date->before($lockdate))
167       $errors->add($i18n->getKey('error.period_locked'));
168
169     // Insert record.
170     if ($errors->isEmpty()) {
171       if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
172         'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
173         header('Location: expenses.php');
174         exit();
175       } else
176         $errors->add($i18n->getKey('error.db'));
177     }
178   }
179   else if ($request->getParameter('onBehalfUser')) {
180     if($user->canManageTeam()) {
181       unset($_SESSION['behalf_id']);
182       unset($_SESSION['behalf_name']);
183
184       if($on_behalf_id != $user->id) {
185         $_SESSION['behalf_id'] = $on_behalf_id;
186         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
187       }
188       header('Location: expenses.php');
189       exit();
190     }
191   }
192 }
193
194 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
195 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
196 $smarty->assign('client_list', $client_list);
197 $smarty->assign('project_list', $project_list);
198 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
199 $smarty->assign('timestring', $selected_date->toString($user->date_format));
200 $smarty->assign('title', $i18n->getKey('title.expenses'));
201 $smarty->assign('content_page_name', 'expenses.tpl');
202 $smarty->display('index.tpl');