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