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('ttTimeHelper');
 
  35 import('ttExpenseHelper');
 
  38 if (!(ttAccessAllowed('track_own_expenses') || ttAccessAllowed('track_expenses'))) {
 
  39   header('Location: access_denied.php');
 
  42 if (!$user->isPluginEnabled('ex')) {
 
  43   header('Location: feature_disabled.php');
 
  46 if (!$user->exists()) {
 
  47   header('Location: access_denied.php'); // Nobody to enter expenses for.
 
  50 if ($user->behalf_id && (!$user->can('track_expenses') || !$user->checkBehalfId())) {
 
  51   header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
 
  54 if (!$user->behalf_id && !$user->can('track_own_expenses') && !$user->adjustBehalfId()) {
 
  55   header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
 
  58 if ($request->isPost() && $request->getParameter('user')) {
 
  59   if (!$user->isUserValid($request->getParameter('user'))) {
 
  60     header('Location: access_denied.php'); // Wrong user id on post.
 
  64 // End of access checks.
 
  66 // Determine user for which we display this page.
 
  67 $userChanged = $request->getParameter('user_changed');
 
  68 if ($request->isPost() && $userChanged) {
 
  69   $user_id = $request->getParameter('user');
 
  70   $user->setOnBehalfUser($user_id);
 
  72   $user_id = $user->getUser();
 
  75 // Initialize and store date in session.
 
  76 $cl_date = $request->getParameter('date', @$_SESSION['date']);
 
  77 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
 
  78 if($selected_date->isError())
 
  79   $selected_date = new DateAndTime(DB_DATEFORMAT);
 
  81   $cl_date = $selected_date->toString(DB_DATEFORMAT);
 
  82 $_SESSION['date'] = $cl_date;
 
  84 // Determine previous and next dates for simple navigation.
 
  85 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
 
  86 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
 
  88 $tracking_mode = $user->getTrackingMode();
 
  89 $show_project = MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode;
 
  91 // Initialize variables.
 
  92 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
 
  93 $_SESSION['client'] = $cl_client;
 
  94 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
 
  95 $_SESSION['project'] = $cl_project;
 
  96 $cl_item_name = $request->getParameter('item_name');
 
  97 $cl_cost = $request->getParameter('cost');
 
  99 // Elements of expensesForm.
 
 100 $form = new Form('expensesForm');
 
 102 if ($user->can('track_expenses')) {
 
 103   $rank = $user->getMaxRankForGroup($user->getGroup());
 
 104   if ($user->can('track_own_expenses'))
 
 105     $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
 
 107     $options = array('status'=>ACTIVE,'max_rank'=>$rank);
 
 108   $user_list = $user->getUsers($options);
 
 109   if (count($user_list) >= 1) {
 
 110     $form->addInput(array('type'=>'combobox',
 
 111       'onchange'=>'this.form.user_changed.value=1;this.form.submit();',
 
 113       'style'=>'width: 250px;',
 
 116       'datakeys'=>array('id','name')));
 
 117     $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
 
 118     $smarty->assign('user_dropdown', 1);
 
 122 // Dropdown for clients in MODE_TIME. Use all active clients.
 
 123 if (MODE_TIME == $tracking_mode && $user->isPluginEnabled('cl')) {
 
 124     $active_clients = ttGroupHelper::getActiveClients(true);
 
 125     $form->addInput(array('type'=>'combobox',
 
 126       'onchange'=>'fillProjectDropdown(this.value);',
 
 128       'style'=>'width: 250px;',
 
 130       'data'=>$active_clients,
 
 131       'datakeys'=>array('id', 'name'),
 
 132       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 133   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 137   // Dropdown for projects assigned to user.
 
 138   $project_list = $user->getAssignedProjects();
 
 139   $form->addInput(array('type'=>'combobox',
 
 140     // 'onchange'=>'fillTaskDropdown(this.value);',
 
 142     'style'=>'width: 250px;',
 
 143     'value'=>$cl_project,
 
 144     'data'=>$project_list,
 
 145     'datakeys'=>array('id','name'),
 
 146     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 148   // Dropdown for clients if the clients plugin is enabled.
 
 149   if ($user->isPluginEnabled('cl')) {
 
 150     $active_clients = ttGroupHelper::getActiveClients(true);
 
 151     // We need an array of assigned project ids to do some trimming. 
 
 152     foreach($project_list as $project)
 
 153       $projects_assigned_to_user[] = $project['id'];
 
 155     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 156     // Also trim their associated project list to only assigned projects (to user).
 
 157     foreach($active_clients as $client) {
 
 158       $projects_assigned_to_client = explode(',', $client['projects']);
 
 159       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 161         $client['projects'] = implode(',', $intersection);
 
 162         $client_list[] = $client;
 
 165     $form->addInput(array('type'=>'combobox',
 
 166       'onchange'=>'fillProjectDropdown(this.value);',
 
 168       'style'=>'width: 250px;',
 
 170       'data'=>$client_list,
 
 171       'datakeys'=>array('id', 'name'),
 
 172       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 175 // If predefined expenses are configured, add controls to select an expense and quantity.
 
 176 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
 
 177 if ($predefined_expenses) {
 
 178   $form->addInput(array('type'=>'combobox',
 
 179     'onchange'=>'recalculateCost();',
 
 180     'name'=>'predefined_expense',
 
 181     'style'=>'width: 250px;',
 
 182     'value'=>$cl_predefined_expense,
 
 183     'data'=>$predefined_expenses,
 
 184     'datakeys'=>array('id', 'name'),
 
 185     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 186   $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
 
 188 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
 
 189 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
 
 190 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
 
 191 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
 
 192 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
 
 195 if ($request->isPost()) {
 
 196   if ($request->getParameter('btn_submit')) {
 
 197     // Validate user input.
 
 198     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
 
 199       $err->add($i18n->get('error.client'));
 
 200     if ($show_project && !$cl_project)
 
 201       $err->add($i18n->get('error.project'));
 
 202     if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
 
 203     if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
 
 205     // Prohibit creating entries in future.
 
 206     if (!$user->getConfigOption('future_entries')) {
 
 207       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 208       if ($selected_date->after($browser_today))
 
 209         $err->add($i18n->get('error.future_date'));
 
 211     if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
 
 212     // Finished validating input data.
 
 214     // Prohibit creating entries in locked range.
 
 215     if ($user->isDateLocked($selected_date))
 
 216       $err->add($i18n->get('error.range_locked'));
 
 220       if (ttExpenseHelper::insert(array('date'=>$cl_date,'client_id'=>$cl_client,
 
 221           'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
 
 222         header('Location: expenses.php');
 
 225         $err->add($i18n->get('error.db'));
 
 227   } elseif ($request->getParameter('onBehalfUser')) {
 
 228     if($user->can('track_expenses')) {
 
 229       unset($_SESSION['behalf_id']);
 
 230       unset($_SESSION['behalf_name']);
 
 232       if($on_behalf_id != $user->id) {
 
 233         $_SESSION['behalf_id'] = $on_behalf_id;
 
 234         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
 
 236       header('Location: expenses.php');
 
 242 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 243 $smarty->assign('show_project', $show_project);
 
 244 $smarty->assign('next_date', $next_date);
 
 245 $smarty->assign('prev_date', $prev_date);
 
 246 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date));
 
 247 $smarty->assign('expense_items', ttExpenseHelper::getItems($cl_date));
 
 248 $smarty->assign('predefined_expenses', $predefined_expenses);
 
 249 $smarty->assign('client_list', $client_list);
 
 250 $smarty->assign('project_list', $project_list);
 
 251 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
 
 252 $smarty->assign('title', $i18n->get('title.expenses'));
 
 253 $smarty->assign('content_page_name', 'mobile/expenses.tpl');
 
 254 $smarty->display('mobile/index.tpl');