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('form.ActionForm');
 
  32 import('ttReportHelper');
 
  33 import('ttGroupHelper');
 
  36 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports')  || ttAccessAllowed('view_client_reports'))) {
 
  37   header('Location: access_denied.php');
 
  41 if ($user->isPluginEnabled('ap')) {
 
  42   $cl_mark_approved_select_option = $request->getParameter('mark_approved_select_options', ($request->isPost() ? null : @$_SESSION['mark_approved_select_option']));
 
  43   $_SESSION['mark_approved_select_option'] = $cl_mark_approved_select_option;
 
  44   $cl_mark_approved_action_option = $request->getParameter('mark_approved_action_options', ($request->isPost() ? null : @$_SESSION['mark_approved_action_option']));
 
  45   $_SESSION['mark_aproved_action_option'] = $cl_mark_approved_action_option;
 
  47 if ($user->isPluginEnabled('ps')) {
 
  48   $cl_mark_paid_select_option = $request->getParameter('mark_paid_select_options', ($request->isPost() ? null : @$_SESSION['mark_paid_select_option']));
 
  49   $_SESSION['mark_paid_select_option'] = $cl_mark_paid_select_option;
 
  50   $cl_mark_paid_action_option = $request->getParameter('mark_paid_action_options', ($request->isPost() ? null : @$_SESSION['mark_paid_action_option']));
 
  51   $_SESSION['mark_paid_action_option'] = $cl_mark_paid_action_option;
 
  53 if ($user->isPluginEnabled('iv')) {
 
  54   $cl_assign_invoice_select_option = $request->getParameter('assign_invoice_select_options', ($request->isPost() ? null : @$_SESSION['assign_invoice_select_option']));
 
  55   $_SESSION['assign_invoice_select_option'] = $cl_assign_invoice_select_option;
 
  56   $cl_recent_invoice_option = $request->getParameter('recent_invoice', ($request->isPost() ? null : @$_SESSION['recent_invoice_option']));
 
  57   $_SESSION['recent_invoice_option'] = $cl_recent_invoice_option;
 
  60 // Use custom fields plugin if it is enabled.
 
  61 if ($user->isPluginEnabled('cf')) {
 
  62   require_once('plugins/CustomFields.class.php');
 
  63   $custom_fields = new CustomFields();
 
  64   $smarty->assign('custom_fields', $custom_fields);
 
  67 $form = new Form('reportViewForm');
 
  69 // Report settings are stored in session bean before we get here from reports.php.
 
  70 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
 
  71 // If we are in post, load the bean from session, as the constructor does it only in get.
 
  72 if ($request->isPost()) $bean->loadBean();
 
  74 $client_id = $bean->getAttribute('client');
 
  76 // Do we need to show checkboxes? We show them if we allow setting approved or paid status,
 
  77 // and also when we can assign / deassign records to invoces.
 
  78 if ($bean->getAttribute('chapproved') && ($user->can('approve_reports') || $user->can('approve_all_eports')))
 
  79   $showForApproved = true;
 
  80 if ($bean->getAttribute('chpaid') ||
 
  81    ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by1')) && !$user->isClient())) {
 
  82   if ($user->can('manage_invoices'))
 
  83     $showForInvoicesOrPaid = true;
 
  85 $use_checkboxes = $showForApproved || $showForInvoicesOrPaid;
 
  87   $smarty->assign('use_checkboxes', true);
 
  89 // Controls for "Mark approved" block.
 
  90 if ($showForApproved) {
 
  91   $mark_approved_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
 
  92   $form->addInput(array('type'=>'combobox',
 
  93     'name'=>'mark_approved_select_options',
 
  94     'data'=>$mark_approved_select_options,
 
  95     'value'=>$cl_mark_approved_select_option));
 
  96   $mark_approved_action_options = array('1'=>$i18n->get('dropdown.approved'),'2'=>$i18n->get('dropdown.not_approved'));
 
  97   $form->addInput(array('type'=>'combobox',
 
  98     'name'=>'mark_approved_action_options',
 
  99     'data'=>$mark_approved_action_options,
 
 100     'value'=>$cl_mark_approved_action_option));
 
 101   $form->addInput(array('type'=>'submit','name'=>'btn_mark_approved','value'=>$i18n->get('button.submit')));
 
 102   $smarty->assign('use_mark_approved', true);
 
 105 // Controls for "Mark paid" block.
 
 106 if ($user->can('manage_invoices') && $bean->getAttribute('chpaid')) {
 
 107   $mark_paid_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
 
 108   $form->addInput(array('type'=>'combobox',
 
 109     'name'=>'mark_paid_select_options',
 
 110     'data'=>$mark_paid_select_options,
 
 111     'value'=>$cl_mark_paid_select_option));
 
 112   $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
 
 113   $form->addInput(array('type'=>'combobox',
 
 114     'name'=>'mark_paid_action_options',
 
 115     'data'=>$mark_paid_action_options,
 
 116     'value'=>$cl_mark_paid_action_option));
 
 117   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
 
 118   $smarty->assign('use_mark_paid', true);
 
 121 // Controls for "Assign to invoice" block.
 
 122 if ($user->can('manage_invoices') &&
 
 123   ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by1')) && !$user->isClient())) {
 
 124   // Client is selected and we are displaying the invoice column.
 
 125   $recent_invoices = ttGroupHelper::getRecentInvoices($client_id);
 
 126   if ($recent_invoices) {
 
 127     $assign_invoice_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
 
 128     $form->addInput(array('type'=>'combobox',
 
 129       'name'=>'assign_invoice_select_options',
 
 130       'data'=>$assign_invoice_select_options,
 
 131       'value'=>$cl_assign_invoice_select_option));
 
 132     $form->addInput(array('type'=>'combobox',
 
 133       'name'=>'recent_invoice',
 
 134       'data'=>$recent_invoices,
 
 135       'datakeys'=>array('id','name'),
 
 136       'value'=>$cl_recent_invoice_option,
 
 137       'empty'=>array(''=>$i18n->get('dropdown.select_invoice'))));
 
 138     $form->addInput(array('type'=>'submit','name'=>'btn_assign','value'=>$i18n->get('button.submit')));
 
 139     $smarty->assign('use_assign_to_invoice', true);
 
 143 if ($request->isPost()) {
 
 145   // Validate parameters and at the same time build arrays of record ids.
 
 146   if (($request->getParameter('btn_mark_approved') && 2 == $request->getParameter('mark_approved_select_options'))
 
 147        || ($request->getParameter('btn_mark_paid') && 2 == $request->getParameter('mark_paid_select_options'))
 
 148        || ($request->getParameter('btn_assign') && 2 == $request->getParameter('assign_invoice_select_options'))) {
 
 149     // We act on selected records. Are there any?
 
 150     foreach($_POST as $key => $val) {
 
 151       if ('log_id_' == substr($key, 0, 7))
 
 152         $time_log_ids[] = substr($key, 7);
 
 153       if ('item_id_' == substr($key, 0, 8))
 
 154         $expense_item_ids[] = substr($key, 8);
 
 156     if (!$time_log_ids && !$expense_item_ids) $err->Add($i18n->get('error.record')); // There are no selected records.
 
 157     // Validation of parameteres ended here.
 
 159     // We are assigning all report items. Get the arrays from session.
 
 160     // Note: getting from session assures we act only on previously displayed records.
 
 161     // Rebuilding from $bean may get us a different set.
 
 162     $item_ids = ttReportHelper::getFromSession();
 
 163     $time_log_ids = $item_ids['report_item_ids'];
 
 164     $expense_item_ids = $item_ids['report_item_expense_ids'];
 
 165     // The above code is here beacues the arrays are used in both "Mark paid" and "Assign to invoice" handlers below.
 
 169     if ($request->getParameter('btn_mark_approved')) {
 
 170       // User clicked the "Mark approved" button to mark some or all items either approved or not approved.
 
 172       // Determine user action.
 
 173       $mark_approved = $request->getParameter('mark_approved_action_options') == 1 ? true : false;
 
 175       // Mark as requested.
 
 176       if ($time_log_ids || $expense_item_ids) {
 
 177         ttReportHelper::markApproved($time_log_ids, $expense_item_ids, $mark_approved);
 
 180       // Re-display this form.
 
 181       header('Location: report.php');
 
 185     if ($request->getParameter('btn_mark_paid')) {
 
 186       // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
 
 188       // Determine user action.
 
 189       $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
 
 191       // Mark as requested.
 
 192       if ($time_log_ids || $expense_item_ids) {
 
 193         ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
 
 196       // Re-display this form.
 
 197       header('Location: report.php');
 
 201     if ($request->getParameter('btn_assign')) {
 
 202       // User clicked the Submit button to assign all or some items to a recent invoice.
 
 204       // Determine invoice id.
 
 205       $invoice_id = $request->getParameter('recent_invoice');
 
 207       // Assign as requested.
 
 208       if ($time_log_ids || $expense_item_ids) {
 
 209         ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
 
 211       // Re-display this form.
 
 212       header('Location: report.php');
 
 218 $options = ttReportHelper::getReportOptions($bean);
 
 220 $report_items = ttReportHelper::getItems($options);
 
 221 // Store record ids in session in case user wants to act on records such as marking them all paid.
 
 222 if ($request->isGet() && $use_checkboxes)
 
 223   ttReportHelper::putInSession($report_items);
 
 225 if (ttReportHelper::grouping($options)) {
 
 226   $subtotals = ttReportHelper::getSubtotals($options);
 
 227   $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
 
 229 $totals = ttReportHelper::getTotals($options);
 
 231 // TODO: Determine if we can create a timesheet out of this report.
 
 232 // There must be only one user, and nothing assigned to existing timesheets.
 
 233 $canCreateTimesheet = false;
 
 234 if ($user->isPluginEnabled('ts') && count($report_items) > 0 &&
 
 235   ($user->can('manage_own_timesheets') || $user->can('manage_timesheets'))) {
 
 237   $canCreateTimesheet = true; // Start with true and reset if we can't.
 
 238   $first_user_id = null;
 
 239   foreach ($report_items as $report_item) {
 
 242       $first_user_id = $report_item['user_id'];
 
 244       if ($report_item['user_id'] != $first_user_id) {
 
 245         // We have items for multiple users.
 
 246         $canCreateTimesheet = false;
 
 250     // Check timesheet id.
 
 251     if ($report_item['timesheet_id']) {
 
 252       // We have an item already assigned to a timesheet.
 
 253       $canCreateTimesheet = false;
 
 257   // Save user_id in session.
 
 258   $bean->saveDetachedAttribute('timesheet_user_id', $first_user_id);
 
 260   // TODO: Improve this for "view_all_reports" situation.
 
 261   // We may need to add "manage_all_timesheets" right.
 
 264 // Assign variables that are used to print subtotals.
 
 266   $smarty->assign('print_subtotals', true);
 
 267   $smarty->assign('first_pass', true);
 
 268   $smarty->assign('prev_grouped_by', '');
 
 269   $smarty->assign('cur_grouped_by', '');
 
 272 // Assign variables that are used to alternate color of rows for different dates.
 
 273 $smarty->assign('prev_date', '');
 
 274 $smarty->assign('cur_date', '');
 
 275 $smarty->assign('report_row_class', 'rowReportItem');
 
 276 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 277 $smarty->assign('report_items', $report_items);
 
 278 $smarty->assign('subtotals', $subtotals);
 
 279 $smarty->assign('totals', $totals);
 
 280 $smarty->assign('can_create_timesheet', $canCreateTimesheet);
 
 281 $smarty->assign('bean', $bean);
 
 282 $smarty->assign('title', $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
 
 283 $smarty->assign('content_page_name', 'report.tpl');
 
 284 $smarty->display('index.tpl');