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');
 
  34 import('ttTimesheetHelper');
 
  37 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports')  || ttAccessAllowed('view_client_reports'))) {
 
  38   header('Location: access_denied.php');
 
  42 if ($user->isPluginEnabled('ap')) {
 
  43   $cl_mark_approved_select_option = $request->getParameter('mark_approved_select_options', ($request->isPost() ? null : @$_SESSION['mark_approved_select_option']));
 
  44   $_SESSION['mark_approved_select_option'] = $cl_mark_approved_select_option;
 
  45   $cl_mark_approved_action_option = $request->getParameter('mark_approved_action_options', ($request->isPost() ? null : @$_SESSION['mark_approved_action_option']));
 
  46   $_SESSION['mark_aproved_action_option'] = $cl_mark_approved_action_option;
 
  48 if ($user->isPluginEnabled('ps')) {
 
  49   $cl_mark_paid_select_option = $request->getParameter('mark_paid_select_options', ($request->isPost() ? null : @$_SESSION['mark_paid_select_option']));
 
  50   $_SESSION['mark_paid_select_option'] = $cl_mark_paid_select_option;
 
  51   $cl_mark_paid_action_option = $request->getParameter('mark_paid_action_options', ($request->isPost() ? null : @$_SESSION['mark_paid_action_option']));
 
  52   $_SESSION['mark_paid_action_option'] = $cl_mark_paid_action_option;
 
  54 if ($user->isPluginEnabled('iv')) {
 
  55   $cl_assign_invoice_select_option = $request->getParameter('assign_invoice_select_options', ($request->isPost() ? null : @$_SESSION['assign_invoice_select_option']));
 
  56   $_SESSION['assign_invoice_select_option'] = $cl_assign_invoice_select_option;
 
  57   $cl_recent_invoice_option = $request->getParameter('recent_invoice', ($request->isPost() ? null : @$_SESSION['recent_invoice_option']));
 
  58   $_SESSION['recent_invoice_option'] = $cl_recent_invoice_option;
 
  60 if ($user->isPluginEnabled('ts')) {
 
  61   $cl_assign_timesheet_select_option = $request->getParameter('assign_timesheet_select_options', ($request->isPost() ? null : @$_SESSION['assign_timesheet_select_option']));
 
  62   $_SESSION['assign_timesheet_select_option'] = $cl_assign_timesheet_select_option;
 
  63   $cl_timesheet_option = $request->getParameter('timesheet', ($request->isPost() ? null : @$_SESSION['timesheet_option']));
 
  64   $_SESSION['timesheet_option'] = $cl_timesheet_option;
 
  67 // Use custom fields plugin if it is enabled.
 
  68 if ($user->isPluginEnabled('cf')) {
 
  69   require_once('plugins/CustomFields.class.php');
 
  70   $custom_fields = new CustomFields();
 
  71   $smarty->assign('custom_fields', $custom_fields);
 
  74 $form = new Form('reportViewForm');
 
  76 // Report settings are stored in session bean before we get here from reports.php.
 
  77 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
 
  78 // If we are in post, load the bean from session, as the constructor does it only in get.
 
  79 if ($request->isPost()) $bean->loadBean();
 
  81 $client_id = $bean->getAttribute('client');
 
  82 $options = ttReportHelper::getReportOptions($bean);
 
  84 // Do we need to show checkboxes? We show them in the following 4 situations:
 
  85 // - We can approve items.
 
  86 // - We can mark items as paid.
 
  87 // - We can sssign items to invoices.
 
  88 // - We can assign items to a timesheet.
 
  89 // Determine these conditions separately.
 
  90 if ($bean->getAttribute('chapproved') && ($user->can('approve_reports') || $user->can('approve_all_eports')))
 
  91   $useMarkApproved = true;
 
  92 if ($bean->getAttribute('chpaid') && $user->can('manage_invoices'))
 
  94 if ($bean->getAttribute('chinvoice') && $client_id && 'no_grouping' == $bean->getAttribute('group_by1') && !$user->isClient() && $user->can('manage_invoices'))
 
  95   $useAssignToInvoice = true;
 
  96 if ($bean->getAttribute('chtimesheet')) {
 
  97   $timesheets = ttTimesheetHelper::getMatchingTimesheets($options);
 
  98   if ($timesheets) $useAssignToTimesheet = true;
 
 101 $use_checkboxes = $useMarkApproved || $useMarkPaid || $useAssignToInvoice || $useAssignToTimesheet;
 
 103   $smarty->assign('use_checkboxes', true);
 
 105 // Controls for "Mark approved" block.
 
 106 if ($useMarkApproved) {
 
 107   $mark_approved_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
 
 108   $form->addInput(array('type'=>'combobox',
 
 109     'name'=>'mark_approved_select_options',
 
 110     'data'=>$mark_approved_select_options,
 
 111     'value'=>$cl_mark_approved_select_option));
 
 112   $mark_approved_action_options = array('1'=>$i18n->get('dropdown.approved'),'2'=>$i18n->get('dropdown.not_approved'));
 
 113   $form->addInput(array('type'=>'combobox',
 
 114     'name'=>'mark_approved_action_options',
 
 115     'data'=>$mark_approved_action_options,
 
 116     'value'=>$cl_mark_approved_action_option));
 
 117   $form->addInput(array('type'=>'submit','name'=>'btn_mark_approved','value'=>$i18n->get('button.submit')));
 
 118   $smarty->assign('use_mark_approved', true);
 
 121 // Controls for "Mark paid" block.
 
 123   $mark_paid_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
 
 124   $form->addInput(array('type'=>'combobox',
 
 125     'name'=>'mark_paid_select_options',
 
 126     'data'=>$mark_paid_select_options,
 
 127     'value'=>$cl_mark_paid_select_option));
 
 128   $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
 
 129   $form->addInput(array('type'=>'combobox',
 
 130     'name'=>'mark_paid_action_options',
 
 131     'data'=>$mark_paid_action_options,
 
 132     'value'=>$cl_mark_paid_action_option));
 
 133   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
 
 134   $smarty->assign('use_mark_paid', true);
 
 137 // Controls for "Assign to invoice" block.
 
 138 if ($useAssignToInvoice) {
 
 139   // Client is selected and we are displaying the invoice column.
 
 140   $recent_invoices = ttGroupHelper::getRecentInvoices($client_id);
 
 141   if ($recent_invoices) {
 
 142     $assign_invoice_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
 
 143     $form->addInput(array('type'=>'combobox',
 
 144       'name'=>'assign_invoice_select_options',
 
 145       'data'=>$assign_invoice_select_options,
 
 146       'value'=>$cl_assign_invoice_select_option));
 
 147     $form->addInput(array('type'=>'combobox',
 
 148       'name'=>'recent_invoice',
 
 149       'data'=>$recent_invoices,
 
 150       'datakeys'=>array('id','name'),
 
 151       'value'=>$cl_recent_invoice_option,
 
 152       'empty'=>array(''=>$i18n->get('dropdown.select_invoice'))));
 
 153     $form->addInput(array('type'=>'submit','name'=>'btn_assign_invoice','value'=>$i18n->get('button.submit')));
 
 154     $smarty->assign('use_assign_to_invoice', true);
 
 158 // Controls for "Assign to timesheet" block.
 
 159 if ($useAssignToTimesheet) {
 
 160   $assign_timesheet_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
 
 161   $form->addInput(array('type'=>'combobox',
 
 162       'name'=>'assign_timesheet_select_options',
 
 163       'data'=>$assign_timesheet_select_options,
 
 164       'value'=>$cl_assign_timesheet_select_option));
 
 165   $form->addInput(array('type'=>'combobox',
 
 168       'datakeys'=>array('id','name'),
 
 169       'value'=>$cl_timesheet_option,
 
 170       'empty'=>array(''=>$i18n->get('dropdown.select_timesheet'))));
 
 171   $form->addInput(array('type'=>'submit','name'=>'btn_assign_timesheet','value'=>$i18n->get('button.submit')));
 
 172   $smarty->assign('use_assign_to_timesheet', true);
 
 175 if ($request->isPost()) {
 
 177   // Validate parameters and at the same time build arrays of record ids.
 
 178   if (($request->getParameter('btn_mark_approved') && 2 == $request->getParameter('mark_approved_select_options'))
 
 179        || ($request->getParameter('btn_mark_paid') && 2 == $request->getParameter('mark_paid_select_options'))
 
 180        || ($request->getParameter('btn_assign_invoice') && 2 == $request->getParameter('assign_invoice_select_options'))
 
 181        || ($request->getParameter('btn_assign_timesheet') && 2 == $request->getParameter('assign_timesheet_select_options'))) {
 
 182     // We act on selected records. Are there any?
 
 183     foreach($_POST as $key => $val) {
 
 184       if ('log_id_' == substr($key, 0, 7))
 
 185         $time_log_ids[] = substr($key, 7);
 
 186       if ('item_id_' == substr($key, 0, 8))
 
 187         $expense_item_ids[] = substr($key, 8);
 
 189     if (!$time_log_ids && !$expense_item_ids) $err->Add($i18n->get('error.record')); // There are no selected records.
 
 190     // Validation of parameteres ended here.
 
 192     // We are assigning all report items. Get the arrays from session.
 
 193     // Note: getting from session assures we act only on previously displayed records.
 
 194     // Rebuilding from $bean may get us a different set.
 
 195     $item_ids = ttReportHelper::getFromSession();
 
 196     $time_log_ids = $item_ids['report_item_ids'];
 
 197     $expense_item_ids = $item_ids['report_item_expense_ids'];
 
 198     // The above code is here beacues the arrays are used in both "Mark paid" and "Assign to invoice" handlers below.
 
 202     if ($request->getParameter('btn_mark_approved')) {
 
 203       // User clicked the "Mark approved" button to mark some or all items either approved or not approved.
 
 205       // Determine user action.
 
 206       $mark_approved = $request->getParameter('mark_approved_action_options') == 1 ? true : false;
 
 208       // Mark as requested.
 
 209       if ($time_log_ids || $expense_item_ids) {
 
 210         ttReportHelper::markApproved($time_log_ids, $expense_item_ids, $mark_approved);
 
 213       // Re-display this form.
 
 214       header('Location: report.php');
 
 218     if ($request->getParameter('btn_mark_paid')) {
 
 219       // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
 
 221       // Determine user action.
 
 222       $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
 
 224       // Mark as requested.
 
 225       if ($time_log_ids || $expense_item_ids) {
 
 226         ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
 
 229       // Re-display this form.
 
 230       header('Location: report.php');
 
 234     if ($request->getParameter('btn_assign_invoice')) {
 
 235       // User clicked the Submit button to assign all or some items to a recent invoice.
 
 237       // Determine invoice id.
 
 238       $invoice_id = $request->getParameter('recent_invoice');
 
 240       // Assign as requested.
 
 241       if ($time_log_ids || $expense_item_ids) {
 
 242         ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
 
 244       // Re-display this form.
 
 245       header('Location: report.php');
 
 249     if ($request->getParameter('btn_assign_timesheet')) {
 
 250       // User clicked the Submit button to assign all or some items to a timesheet.
 
 252       // Determine invoice id.
 
 253       $timesheet_id = $request->getParameter('timesheet');
 
 255       // Assign as requested.
 
 257         ttReportHelper::assignToTimesheet($timesheet_id, $time_log_ids);
 
 259       // Re-display this form.
 
 260       header('Location: report.php');
 
 266 $report_items = ttReportHelper::getItems($options);
 
 267 // Store record ids in session in case user wants to act on records such as marking them all paid.
 
 268 if ($request->isGet() && $use_checkboxes)
 
 269   ttReportHelper::putInSession($report_items);
 
 271 if (ttReportHelper::grouping($options)) {
 
 272   $subtotals = ttReportHelper::getSubtotals($options);
 
 273   $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
 
 275 $totals = ttReportHelper::getTotals($options);
 
 277 // Assign variables that are used to print subtotals.
 
 279   $smarty->assign('print_subtotals', true);
 
 280   $smarty->assign('first_pass', true);
 
 281   $smarty->assign('prev_grouped_by', '');
 
 282   $smarty->assign('cur_grouped_by', '');
 
 285 // Assign variables that are used to alternate color of rows for different dates.
 
 286 $smarty->assign('prev_date', '');
 
 287 $smarty->assign('cur_date', '');
 
 288 $smarty->assign('report_row_class', 'rowReportItem');
 
 289 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 290 $smarty->assign('report_items', $report_items);
 
 291 $smarty->assign('subtotals', $subtotals);
 
 292 $smarty->assign('totals', $totals);
 
 293 $smarty->assign('bean', $bean);
 
 294 $smarty->assign('title', $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
 
 295 $smarty->assign('content_page_name', 'report.tpl');
 
 296 $smarty->display('index.tpl');