posaune
[timetracker.git] / timesheet_view.php
index 136c7f1..c8d7048 100644 (file)
 
 require_once('initialize.php');
 import('ttTimesheetHelper');
+import('ttReportHelper');
 
 // Access checks.
-if (!(ttAccessAllowed('view_own_timesheets') || ttAccessAllowed('view_timesheets') || ttAccessAllowed('view_all_timesheets') || ttAccessAllowed('view_client_timesheets'))) {
+if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
   header('Location: access_denied.php');
   exit();
 }
@@ -38,94 +39,116 @@ if (!$user->isPluginEnabled('ts')) {
   header('Location: feature_disabled.php');
   exit();
 }
-$timesheet_id = (int)$request->getParameter('id');
-$timesheet = ttTimesheetHelper::getTimesheet($timesheet_id);
+$cl_timesheet_id = (int)$request->getParameter('id');
+$timesheet = ttTimesheetHelper::getTimesheet($cl_timesheet_id);
 if (!$timesheet) {
   header('Location: access_denied.php');
   exit();
 }
 // TODO: add other checks here for timesheet being appropriate for user role.
+// TODO: if this is a timesheet submit, validate approver id, too.
 // End of access checks.
 
-$invoice_date = new DateAndTime(DB_DATEFORMAT, $invoice['date']);
-$client = ttClientHelper::getClient($invoice['client_id'], true);
-if (!$client) // In case client was deleted.
-  $client = ttClientHelper::getDeletedClient($invoice['client_id']);
+if ($request->isPost()) {
+  $cl_comment = trim($request->getParameter('comment'));
+  $approver_id = $request->getParameter('approver');
+}
 
-$invoice_items = ttInvoiceHelper::getInvoiceItems($cl_invoice_id);
-$tax_percent = $client['tax'];
+$options = ttTimesheetHelper::getReportOptions($timesheet);
+$subtotals = ttReportHelper::getSubtotals($options);
+$totals = ttReportHelper::getTotals($options);
 
-$subtotal = 0;
-$tax = 0;
-foreach($invoice_items as $item)
-  $subtotal += $item['cost'];
-if ($tax_percent > 0) {
-  $tax_expenses = $user->isPluginEnabled('et');
-  foreach($invoice_items as $item) {
-    if ($item['type'] == 2 && !$tax_expenses)
-      continue;
-    $tax += round($item['cost'] * $tax_percent / 100, 2);
-  }
+// Determine which controls to show and obtain date for them.
+$showSubmit = !$timesheet['submit_status'];
+if ($showSubmit) {
+  $approvers = ttTimesheetHelper::getApprovers();
+  $showApprovers = count($approvers) >= 1;
 }
-$total = $subtotal + $tax;
-
-$currency = $user->getCurrency();
-$decimalMark = $user->getDecimalMark();
+$canApprove = $user->can('approve_timesheets') || $user->can('approve_own_timesheets');
+$showApprove = $timesheet['submit_status'] && $timesheet['approve_status'] == null;
 
-$smarty->assign('subtotal', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($subtotal, 2))));
-if ($tax) $smarty->assign('tax', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($tax, 2))));
-$smarty->assign('total', $currency.' '.str_replace('.', $decimalMark, sprintf('%8.2f', round($total, 2))));
+// Add a form with controls.
+$form = new Form('timesheetForm');
+$form->addInput(array('type'=>'hidden','name'=>'id','value'=>$timesheet['id']));
 
-if ('.' != $decimalMark) {
-  foreach ($invoice_items as &$item)
-    $item['cost'] = str_replace('.', $decimalMark, $item['cost']);
+if ($showSubmit) {
+  if ($showApprovers) {
+    $form->addInput(array('type'=>'combobox',
+      'name'=>'approver',
+      'style'=>'width: 200px;',
+      'data'=>$approvers,
+      'datakeys'=>array('id','name','email')));
+  }
+  $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
 }
 
-// Calculate colspan for invoice summary.
-$colspan = 4;
-$trackingMode = $user->getTrackingMode();
-if (MODE_PROJECTS == $trackingMode)
-  $colspan++;
-elseif (MODE_PROJECTS_AND_TASKS == $trackingMode)
-  $colspan += 2;
-
-$form = new Form('invoiceForm');
-// Hidden control for invoice id.
-$form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_invoice_id));
-// invoiceForm only contains controls for "Mark paid" block below invoice table.
-if ($user->isPluginEnabled('ps')) {
-  $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
-  $form->addInput(array('type'=>'combobox',
-    'name'=>'mark_paid_action_options',
-    'data'=>$mark_paid_action_options,
-    'value'=>$cl_mark_paid_action_option));
-  $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
+if ($showApprove) {
+  $form->addInput(array('type'=>'textarea','name'=>'comment','maxlength'=>'250','style'=>'width: 300px; height: 60px;'));
+  $form->addInput(array('type'=>'submit','name'=>'btn_approve','value'=>$i18n->get('button.approve')));
+  $form->addInput(array('type'=>'submit','name'=>'btn_disapprove','value'=>$i18n->get('button.disapprove')));
 }
 
+// Submit.
 if ($request->isPost()) {
-  if ($request->getParameter('btn_mark_paid')) {
-    // User clicked the "Mark paid" button to mark all invoice items either paid or not paid.
+  if ($request->getParameter('btn_submit')) {
+    $fields = array('timesheet_id' => $timesheet['id'],
+      'approver_id' => $approver_id);
+    if (!ttTimesheetHelper::markSubmitted($fields))
+      $err->add($i18n->get('error.db'));
+    if ($err->no() && !ttTimesheetHelper::sendSubmitEmail($fields)) {
+      $err->add($i18n->get('error.mail_send'));
+    }
+    if ($err->no()) {
+      // Redirect to self.
+      header('Location: timesheet_view.php?id='.$timesheet['id']);
+      exit();
+    }
+  }
 
-    // Determine user action.
-    $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
-    ttInvoiceHelper::markPaid($cl_invoice_id, $mark_paid);
+  if ($request->getParameter('btn_approve')) {
+    $fields = array('timesheet_id' => $timesheet['id'],
+      'name' => $timesheet['name'],
+      'user_id' => $timesheet['user_id'],
+      'comment' => $cl_comment);
+    if (!ttTimesheetHelper::markApproved($fields))
+      $err->add($i18n->get('error.db'));
+    if ($err->no() && !ttTimesheetHelper::sendApprovedEmail($fields)) {
+      $err->add($i18n->get('error.mail_send'));
+    }
+    if ($err->no()) {
+      // Redirect to self.
+      header('Location: timesheet_view.php?id='.$timesheet['id']);
+      exit();
+    }
+  }
 
-    // Re-display this form.
-    header('Location: invoice_view.php?id='.$cl_invoice_id);
-    exit();
+  if ($request->getParameter('btn_disapprove')) {
+    $fields = array('timesheet_id' => $timesheet['id'],
+      'name' => $timesheet['name'],
+      'user_id' => $timesheet['user_id'],
+      'comment' => $cl_comment);
+    if (!ttTimesheetHelper::markDisapproved($fields))
+      $err->add($i18n->get('error.db'));
+    if ($err->no() && !ttTimesheetHelper::sendDisapprovedEmail($fields)) {
+      $err->add($i18n->get('error.mail_send'));
+    }
+    if ($err->no()) {
+      // Redirect to self.
+      header('Location: timesheet_view.php?id='.$timesheet['id']);
+      exit();
+    }
   }
 }
 
+$smarty->assign('not_client', $notClient);
+$smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
+$smarty->assign('timesheet', $timesheet);
+$smarty->assign('subtotals', $subtotals);
+$smarty->assign('totals', $totals);
+$smarty->assign('show_approvers', $showApprovers);
+$smarty->assign('show_submit', $showSubmit);
+$smarty->assign('show_approve', $showApprove);
 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
-$smarty->assign('invoice_id', $cl_invoice_id);
-$smarty->assign('timesheet_name', $timesheet['name']);
-$smarty->assign('invoice_date', $invoice_date->toString($user->getDateFormat()));
-$smarty->assign('client_name', $client['name']);
-$smarty->assign('client_address', $client['address']);
-$smarty->assign('show_project', MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode);
-$smarty->assign('show_task', MODE_PROJECTS_AND_TASKS == $trackingMode);
-$smarty->assign('invoice_items', $invoice_items);
-$smarty->assign('colspan', $colspan);
-$smarty->assign('title', $i18n->get('title.timesheet'));
+$smarty->assign('title', $i18n->get('title.timesheet').": ".$timesheet['start_date']." - ".$timesheet['end_date']);
 $smarty->assign('content_page_name', 'timesheet_view.tpl');
 $smarty->display('index.tpl');