Switched to using shorter ActionErrors functions
[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   // Dropdown for clients if the clients plugin is enabled.
104   if (in_array('cl', explode(',', $user->plugins))) {
105     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
106     // We need an array of assigned project ids to do some trimming. 
107     foreach($project_list as $project)
108       $projects_assigned_to_user[] = $project['id'];
109
110     // Build a client list out of active clients. Use only clients that are relevant to user.
111     // Also trim their associated project list to only assigned projects (to user).
112     foreach($active_clients as $client) {
113       $projects_assigned_to_client = explode(',', $client['projects']);
114       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
115       if ($intersection) {
116         $client['projects'] = implode(',', $intersection);
117         $client_list[] = $client;
118       }
119     }
120     $form->addInput(array('type'=>'combobox',
121       'onchange'=>'fillProjectDropdown(this.value);',
122       'name'=>'client',
123       'style'=>'width: 250px;',
124       'value'=>$cl_client,
125       'data'=>$client_list,
126       'datakeys'=>array('id', 'name'),
127       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
128   }
129 }
130 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
131 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
132 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
133 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
134 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
135
136 // Determine lock date. Time entries earlier than lock date cannot be created or modified.
137 $lock_interval = $user->lock_interval;
138 $lockdate = 0;
139 if ($lock_interval > 0) {
140   $lockdate = new DateAndTime();
141   $lockdate->decDay($lock_interval);
142 }
143
144 // Submit.
145 if ($request->getMethod() == 'POST') {
146   if ($request->getParameter('btn_submit')) {
147     // Validate user input.
148     if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
149       $errors->add($i18n->getKey('error.client'));
150     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
151       if (!$cl_project) $errors->add($i18n->getKey('error.project'));
152     }
153     if (!ttValidString($cl_item_name)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
154     if (!ttValidFloat($cl_cost)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
155
156     // Prohibit creating entries in future.
157     if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
158       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
159       if ($selected_date->after($browser_today))
160         $errors->add($i18n->getKey('error.future_date'));
161     }
162     // Finished validating input data.
163
164     // Prohibit creating time entries in locked interval.
165     if($lockdate && $selected_date->before($lockdate))
166       $errors->add($i18n->getKey('error.period_locked'));
167
168     // Insert record.
169     if ($errors->no()) {
170       if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
171         'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
172         header('Location: expenses.php');
173         exit();
174       } else
175         $errors->add($i18n->getKey('error.db'));
176     }
177   }
178   else if ($request->getParameter('onBehalfUser')) {
179     if($user->canManageTeam()) {
180       unset($_SESSION['behalf_id']);
181       unset($_SESSION['behalf_name']);
182
183       if($on_behalf_id != $user->id) {
184         $_SESSION['behalf_id'] = $on_behalf_id;
185         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
186       }
187       header('Location: expenses.php');
188       exit();
189     }
190   }
191 }
192
193 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
194 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
195 $smarty->assign('client_list', $client_list);
196 $smarty->assign('project_list', $project_list);
197 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
198 $smarty->assign('timestring', $selected_date->toString($user->date_format));
199 $smarty->assign('title', $i18n->getKey('title.expenses'));
200 $smarty->assign('content_page_name', 'expenses.tpl');
201 $smarty->display('index.tpl');