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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 require_once('../initialize.php');
 
  31 import('ttUserHelper');
 
  32 import('ttGroupHelper');
 
  33 import('DateAndTime');
 
  34 import('ttExpenseHelper');
 
  37 if (!(ttAccessAllowed('track_own_expenses') || ttAccessAllowed('track_expenses'))) {
 
  38   header('Location: access_denied.php');
 
  41 if (!$user->isPluginEnabled('ex')) {
 
  42   header('Location: feature_disabled.php');
 
  45 if (!$user->exists()) {
 
  46   header('Location: access_denied.php'); // Nobody to enter expenses for.
 
  49 if ($user->behalf_id && (!$user->can('track_expenses') || !$user->checkBehalfId())) {
 
  50   header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
 
  53 if (!$user->behalf_id && !$user->can('track_own_expenses') && !$user->adjustBehalfId()) {
 
  54   header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
 
  57 if ($request->isPost() && $request->getParameter('user')) {
 
  58   if (!$user->isUserValid($request->getParameter('user'))) {
 
  59     header('Location: access_denied.php'); // Wrong user id on post.
 
  63 // End of access checks.
 
  65 // Determine user for which we display this page.
 
  66 $userChanged = $request->getParameter('user_changed');
 
  67 if ($request->isPost() && $userChanged) {
 
  68   $user_id = $request->getParameter('user');
 
  69   $user->setOnBehalfUser($user_id);
 
  71   $user_id = $user->getUser();
 
  74 // Initialize and store date in session.
 
  75 $cl_date = $request->getParameter('date', @$_SESSION['date']);
 
  76 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
 
  77 if($selected_date->isError())
 
  78   $selected_date = new DateAndTime(DB_DATEFORMAT);
 
  80   $cl_date = $selected_date->toString(DB_DATEFORMAT);
 
  81 $_SESSION['date'] = $cl_date;
 
  83 // Determine previous and next dates for simple navigation.
 
  84 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
 
  85 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
 
  87 $tracking_mode = $user->getTrackingMode();
 
  88 $show_project = MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode;
 
  90 // Initialize variables.
 
  91 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
 
  92 $_SESSION['client'] = $cl_client;
 
  93 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
 
  94 $_SESSION['project'] = $cl_project;
 
  95 $cl_item_name = $request->getParameter('item_name');
 
  96 $cl_cost = $request->getParameter('cost');
 
  98 // Elements of expensesForm.
 
  99 $form = new Form('expensesForm');
 
 101 if ($user->can('track_expenses')) {
 
 102   $rank = $user->getMaxRankForGroup($user->getGroup());
 
 103   if ($user->can('track_own_expenses'))
 
 104     $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
 
 106     $options = array('status'=>ACTIVE,'max_rank'=>$rank);
 
 107   $user_list = $user->getUsers($options);
 
 108   if (count($user_list) >= 1) {
 
 109     $form->addInput(array('type'=>'combobox',
 
 110       'onchange'=>'this.form.user_changed.value=1;this.form.submit();',
 
 112       'style'=>'width: 250px;',
 
 115       'datakeys'=>array('id','name')));
 
 116     $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
 
 117     $smarty->assign('user_dropdown', 1);
 
 121 // Dropdown for clients in MODE_TIME. Use all active clients.
 
 122 if (MODE_TIME == $tracking_mode && $user->isPluginEnabled('cl')) {
 
 123     $active_clients = ttGroupHelper::getActiveClients(true);
 
 124     $form->addInput(array('type'=>'combobox',
 
 125       'onchange'=>'fillProjectDropdown(this.value);',
 
 127       'style'=>'width: 250px;',
 
 129       'data'=>$active_clients,
 
 130       'datakeys'=>array('id', 'name'),
 
 131       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 132   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 136   // Dropdown for projects assigned to user.
 
 137   $project_list = $user->getAssignedProjects();
 
 138   $form->addInput(array('type'=>'combobox',
 
 139     // 'onchange'=>'fillTaskDropdown(this.value);',
 
 141     'style'=>'width: 250px;',
 
 142     'value'=>$cl_project,
 
 143     'data'=>$project_list,
 
 144     'datakeys'=>array('id','name'),
 
 145     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 147   // Dropdown for clients if the clients plugin is enabled.
 
 148   if ($user->isPluginEnabled('cl')) {
 
 149     $active_clients = ttGroupHelper::getActiveClients(true);
 
 150     // We need an array of assigned project ids to do some trimming. 
 
 151     foreach($project_list as $project)
 
 152       $projects_assigned_to_user[] = $project['id'];
 
 154     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 155     // Also trim their associated project list to only assigned projects (to user).
 
 156     foreach($active_clients as $client) {
 
 157       $projects_assigned_to_client = explode(',', $client['projects']);
 
 158       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 160         $client['projects'] = implode(',', $intersection);
 
 161         $client_list[] = $client;
 
 164     $form->addInput(array('type'=>'combobox',
 
 165       'onchange'=>'fillProjectDropdown(this.value);',
 
 167       'style'=>'width: 250px;',
 
 169       'data'=>$client_list,
 
 170       'datakeys'=>array('id', 'name'),
 
 171       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 174 // If predefined expenses are configured, add controls to select an expense and quantity.
 
 175 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
 
 176 if ($predefined_expenses) {
 
 177   $form->addInput(array('type'=>'combobox',
 
 178     'onchange'=>'recalculateCost();',
 
 179     'name'=>'predefined_expense',
 
 180     'style'=>'width: 250px;',
 
 181     'value'=>$cl_predefined_expense,
 
 182     'data'=>$predefined_expenses,
 
 183     'datakeys'=>array('id', 'name'),
 
 184     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 185   $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
 
 187 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
 
 188 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
 
 189 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
 
 190 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
 
 191 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
 
 194 if ($request->isPost()) {
 
 195   if ($request->getParameter('btn_submit')) {
 
 196     // Validate user input.
 
 197     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
 
 198       $err->add($i18n->get('error.client'));
 
 199     if ($show_project && !$cl_project)
 
 200       $err->add($i18n->get('error.project'));
 
 201     if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
 
 202     if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
 
 204     // Prohibit creating entries in future.
 
 205     if (!$user->getConfigOption('future_entries')) {
 
 206       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 207       if ($selected_date->after($browser_today))
 
 208         $err->add($i18n->get('error.future_date'));
 
 210     // Finished validating input data.
 
 212     // Prohibit creating entries in locked range.
 
 213     if ($user->isDateLocked($selected_date))
 
 214       $err->add($i18n->get('error.range_locked'));
 
 218       if (ttExpenseHelper::insert(array('date'=>$cl_date,'client_id'=>$cl_client,
 
 219           'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
 
 220         header('Location: expenses.php');
 
 223         $err->add($i18n->get('error.db'));
 
 225   } elseif ($request->getParameter('onBehalfUser')) {
 
 226     if($user->can('track_expenses')) {
 
 227       unset($_SESSION['behalf_id']);
 
 228       unset($_SESSION['behalf_name']);
 
 230       if($on_behalf_id != $user->id) {
 
 231         $_SESSION['behalf_id'] = $on_behalf_id;
 
 232         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
 
 234       header('Location: expenses.php');
 
 240 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 241 $smarty->assign('show_project', $show_project);
 
 242 $smarty->assign('next_date', $next_date);
 
 243 $smarty->assign('prev_date', $prev_date);
 
 244 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date));
 
 245 $smarty->assign('expense_items', ttExpenseHelper::getItems($cl_date));
 
 246 $smarty->assign('predefined_expenses', $predefined_expenses);
 
 247 $smarty->assign('client_list', $client_list);
 
 248 $smarty->assign('project_list', $project_list);
 
 249 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
 
 250 $smarty->assign('title', $i18n->get('title.expenses'));
 
 251 $smarty->assign('content_page_name', 'mobile/expenses.tpl');
 
 252 $smarty->display('mobile/index.tpl');