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('ttTeamHelper');
 
  32 import('DateAndTime');
 
  33 import('ttExpenseHelper');
 
  36 if (!ttAccessCheck(right_data_entry)) {
 
  37   header('Location: access_denied.php');
 
  41 $cl_id = $request->getParameter('id');
 
  43 // Get the expense item we are editing.
 
  44 $expense_item = ttExpenseHelper::getItem($cl_id, $user->getActiveUser());
 
  46 // Prohibit editing invoiced items.
 
  47 if ($expense_item['invoice_id']) die($i18n->getKey('error.sys'));
 
  49 $item_date = new DateAndTime(DB_DATEFORMAT, $expense_item['date']);
 
  51 // Initialize variables.
 
  52 $cl_date = $cl_client = $cl_project = $cl_item_name = $cl_cost = null;
 
  53 if ($request->getMethod() == 'POST') {
 
  54   $cl_date = trim($request->getParameter('date'));
 
  55   $cl_client = $request->getParameter('client');
 
  56   $cl_project = $request->getParameter('project');
 
  57   $cl_item_name = trim($request->getParameter('item_name'));
 
  58   $cl_cost = trim($request->getParameter('cost'));
 
  60   $cl_date = $item_date->toString($user->date_format);
 
  61   $cl_client = $expense_item['client_id'];
 
  62   $cl_project = $expense_item['project_id'];
 
  63   $cl_item_name = $expense_item['name'];
 
  64   $cl_cost = $expense_item['cost'];
 
  67 // Initialize elements of 'expenseItemForm'.
 
  68 $form = new Form('expenseItemForm');
 
  70 // Dropdown for clients in MODE_TIME. Use all active clients.
 
  71 if (MODE_TIME == $user->tracking_mode && in_array('cl', explode(',', $user->plugins))) {
 
  72     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
  73     $form->addInput(array('type'=>'combobox',
 
  74       'onchange'=>'fillProjectDropdown(this.value);',
 
  76       'style'=>'width: 250px;',
 
  78       'data'=>$active_clients,
 
  79       'datakeys'=>array('id', 'name'),
 
  80       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
  82   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
  85 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
  86   // Dropdown for projects assigned to user.
 
  87   $project_list = $user->getAssignedProjects();
 
  88   $form->addInput(array('type'=>'combobox',
 
  90     'style'=>'width: 250px;',
 
  92     'data'=>$project_list,
 
  93     'datakeys'=>array('id','name'),
 
  94     'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
  97   // Dropdown for clients if the clients plugin is enabled.
 
  98   if (in_array('cl', explode(',', $user->plugins))) {
 
  99     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 100     // We need an array of assigned project ids to do some trimming. 
 
 101     foreach($project_list as $project)
 
 102       $projects_assigned_to_user[] = $project['id'];
 
 104     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 105     // Also trim their associated project list to only assigned projects (to user).
 
 106     foreach($active_clients as $client) {
 
 107           $projects_assigned_to_client = explode(',', $client['projects']);
 
 108           $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 110         $client['projects'] = implode(',', $intersection);
 
 111             $client_list[] = $client;
 
 114     $form->addInput(array('type'=>'combobox',
 
 115       'onchange'=>'fillProjectDropdown(this.value);',
 
 117       'style'=>'width: 250px;',
 
 119       'data'=>$client_list,
 
 120       'datakeys'=>array('id', 'name'),
 
 121       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 125 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
 
 126 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
 
 127 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
 
 128 // Hidden control for record id.
 
 129 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
 
 130 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
 
 131 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.save')));
 
 132 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.copy')));
 
 133 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete')));
 
 135 if ($request->getMethod() == 'POST') {
 
 136   // Validate user input.
 
 137   if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
 
 138     $errors->add($i18n->getKey('error.client'));
 
 139   if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 140     if (!$cl_project) $errors->add($i18n->getKey('error.project'));
 
 142   if (!ttValidString($cl_item_name)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
 
 143   if (!ttValidFloat($cl_cost)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
 
 144   if (!ttValidDate($cl_date)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
 
 146   // Determine lock date.
 
 147   $lock_interval = $user->lock_interval;
 
 149   if ($lock_interval > 0) {
 
 150     $lockdate = new DateAndTime();
 
 151     $lockdate->decDay($lock_interval);
 
 154   // This is a new date for the expense item.
 
 155   $new_date = new DateAndTime($user->date_format, $cl_date);
 
 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 ($new_date->after($browser_today))
 
 161       $errors->add($i18n->getKey('error.future_date'));
 
 165   if ($request->getParameter('btn_save')) {
 
 167     // 1) Prohibit updating locked entries (that are in locked interval).
 
 168     // 2) Prohibit saving unlocked entries into locked interval.
 
 170     // Now, step by step.
 
 171     // 1) Prohibit updating locked entries.
 
 172     if($lockdate && $item_date->before($lockdate))
 
 173       $errors->add($i18n->getKey('error.period_locked'));        
 
 174     // 2) Prohibit saving completed unlocked entries into locked interval.
 
 175     if($errors->isEmpty() && $lockdate && $new_date->before($lockdate))
 
 176       $errors->add($i18n->getKey('error.period_locked'));        
 
 179     if ($errors->isEmpty()) {
 
 180       if (ttExpenseHelper::update(array('id'=>$cl_id,'date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getActiveUser(),
 
 181           'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost))) {
 
 182         header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
 
 188   // Save as new record.
 
 189   if ($request->getParameter('btn_copy')) {
 
 190     // We need to prohibit saving into locked interval.
 
 191     if($lockdate && $new_date->before($lockdate))
 
 192       $errors->add($i18n->getKey('error.period_locked'));
 
 194     // Now, a new insert.
 
 195     if ($errors->isEmpty()) {
 
 196       if (ttExpenseHelper::insert(array('date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getActiveUser(),
 
 197         'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
 
 198         header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
 
 201         $errors->add($i18n->getKey('error.db'));
 
 205   if ($request->getParameter('btn_delete')) {
 
 206     header("Location: expense_delete.php?id=$cl_id");
 
 209 } // End of if ($request->getMethod() == "POST")
 
 211 $smarty->assign('client_list', $client_list);
 
 212 $smarty->assign('project_list', $project_list);
 
 213 $smarty->assign('task_list', $task_list);
 
 214 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 215 $smarty->assign('title', $i18n->getKey('title.edit_expense'));
 
 216 $smarty->assign('content_page_name', 'expense_edit.tpl');
 
 217 $smarty->display('index.tpl');