/**
  * Go show the payroll home page.
  */
 function indexAction()
 {
     // Set the page title.
     $this->view->title = "Payroll Management";
     // Get the user's session.
     $session = new Zend_Session_Namespace('Web');
     // Check the session for the active pay period.
     if (!isset($session->activePayPeriod)) {
         // Retrieve the current pay period and save it to the view.
         $payPeriodDao = new PayPeriodDao();
         $this->view->payPeriod = $payPeriodDao->getCurrent();
     } else {
         // Use the pay period stored in the session.
         $this->view->payPeriod = $session->activePayPeriod;
     }
     // Set the manage layout for this action.
     $this->_helper->layout->setLayout('manage');
     // Include the payroll management scripts on the page.
     $this->view->scripts = "payroll";
 }
 /**
  * Retrieve the specified timesheet and show it on the timesheet page.
  */
 function viewAction()
 {
     // Set the page title.
     $this->view->title = "Viewer";
     // Get the date within the pay period.
     $date = $this->getStr('date');
     // Attempt to convert the current start time into a date.
     if (!strtotime($date)) {
         // Throw an exception.
         throw new Exception("Invalid date passed into " . "the timesheet controller: {$date}");
     }
     // Get the requested pay period.
     $payPeriodDao = new PayPeriodDao();
     $payPeriod = $payPeriodDao->getContaining($date);
     // Make sure the pay period was found.
     if (!isset($payPeriod)) {
         // Save an error message.
         $this->view->error = "Failed to find a system pay period " . "containing {$date}, so displaying the current pay period.\n";
         // Retrieve the current pay period.
         $payPeriod = $payPeriodDao->getCurrent();
     }
     // Get the employee id.
     $id = $this->view->employee->id;
     // Get the employee's latest timesheet and save it to the view.
     $timesheetDao = new TimesheetDao();
     $this->view->timesheet = $timesheetDao->getForEmployee($id, $payPeriod);
     // Make sure a timesheet was found.
     if (!isset($this->view->timesheet)) {
         // Throw an exception if we couldn't find the timesheet.
         throw new Exception("Failed to find a timesheet for the " . "requested pay period.");
     }
     // Set the timesheet layout for this action.
     $this->_helper->layout->setLayout('timesheet');
     // Render the timesheet using the index view.
     $this->render('index');
 }
 /**
  * Used to retrieve the latest incomplete employee timesheet.
  *
  * @param id The employee id for which the timesheet will be retrieved.
  *
  * @return Returns the latest incomplete employee timesheet.
  */
 public function getLatestForEmployee($id)
 {
     // Make sure the id is valid.
     if (!isset($id) || !is_numeric($id)) {
         return null;
     }
     // Get the database adapter.
     $db = $this->getAdapter();
     // Set the fetch mode.
     $db->setFetchMode(Zend_Db::FETCH_OBJ);
     // Get the first incomplete timesheet.
     $incomplete = $db->select()->from($this->_name)->where('employee_id = ?', $id)->where('completed = false')->order('pp_start');
     // Retrieve all the incomplete timesheets.
     $objs = $db->query($incomplete)->fetchAll();
     // Get the incomplete timesheets that have bills.
     $withBills = array();
     foreach ($objs as $ts) {
         // Post-process the timesheet.
         $this->postProcess($ts);
         // Add the timesheet enhancement information.
         $this->enhanceTimesheet($ts);
         // Add the timesheet summary information.
         $this->addSummary($ts, $ts->pay_period);
         // Only add timesheets that have some hours.
         if ($ts->total) {
             $withBills[] = $ts;
         }
     }
     // If there is only one, then use it.
     $obj = null;
     if (count($withBills) == 1) {
         $obj = $withBills[0];
     }
     // Make sure an object was found.
     if (!isset($obj) || !isset($obj->id)) {
         // Get the most recent completed timesheet.
         $complete = $db->select()->from($this->_name)->where('employee_id = ?', $id)->where('completed = true')->order('pp_start DESC')->limit(1);
         // Retrieve the timesheet.
         $obj = $db->query($complete)->fetchObject();
         // Make sure the timesheet was found.
         if (!isset($obj) || !isset($obj->id) && count($objs) == 0) {
             // Get the current pay period.
             $payPeriodDao = new PayPeriodDao();
             $currPP = $payPeriodDao->getCurrent();
             // Make sure the pay period exists.
             if (isset($currPP) && isset($currPP->start)) {
                 // Create an empty timesheet for the current pay period.
                 $this->add(array('employee_id' => $id, 'pp_start' => $currPP->start));
                 // Retrieve the newly added timesheet.
                 $obj = $db->query($incomplete)->fetchObject();
             }
         }
         // Use an existing timesheet.
         if (!isset($obj) || !isset($obj->id)) {
             if (count($withBills) > 0) {
                 // Use the first incomplete timesheet that has bills.
                 $obj = $withBills[0];
             } else {
                 if (count($objs) > 0) {
                     // Use the first incomplete timesheet.
                     $obj = $objs[0];
                 }
             }
         }
     }
     // Make sure a timesheet was found.
     if (!isset($obj) || !isset($obj->id)) {
         return null;
     }
     // Check to see if the timesheet has already been enhanced.
     if (!isset($obj->pay_period)) {
         // Post-process the timesheet.
         $this->postProcess($obj);
         // Add the timesheet enhancement information.
         $this->enhanceTimesheet($obj);
         // Add the timesheet summary information.
         $this->addSummary($obj, $obj->pay_period);
     }
     // Return the retrieved object.
     return $obj;
 }
 /**
  * Initialize the user session information.
  */
 function init()
 {
     try {
         // Make sure the session has been created.
         if (!isset(self::$cachedSession)) {
             self::$cachedSession = new Zend_Session_Namespace('Web');
         }
     } catch (Zend_Session_Exception $ex) {
         // Log the error.
         Logger::getLogger()->debug("Zend_Session_Exception" . $ex->getMessage());
     }
     // Get the session.
     $session = self::$cachedSession;
     // Fix the request.
     self::fixRequest($this->getRequest());
     // Get the request URI.
     $requestUri = $this->getRequest()->getRequestUri();
     // Write a log of this page request.
     if (isset($session->employee)) {
         Logger::getLogger()->info("Request [" . $session->employee->login . "]: " . $requestUri);
     } else {
         Logger::getLogger()->info("Request [guest]: " . $requestUri);
     }
     // Wrap the whole thing in a try/catch.
     try {
         // Check to see if the user is trying to access an admin page.
         if (preg_match("/^\\/admin/i", $requestUri)) {
             // Make sure the employee is an administrator.
             if (!isset($session->employee) || !$session->employee->admin) {
                 // Redirect to the home page.
                 $this->_helper->redirector('index', 'index', 'default');
             }
         } else {
             if (preg_match("/^\\/manager/i", $requestUri)) {
                 // Make sure the employee is a manager.
                 if (!isset($session->employee) || !$session->employee->manager) {
                     // Redirect to the home page.
                     $this->_helper->redirector('index', 'index', 'default');
                 } else {
                     // Set the manage layout.
                     $this->_helper->layout->setLayout('manage');
                 }
             } else {
                 if (preg_match("/^\\/payroll/i", $requestUri)) {
                     // Make sure the employee is in payroll.
                     if (!isset($session->employee) || !$session->employee->payroll) {
                         // Redirect to the home page.
                         $this->_helper->redirector('index', 'index', 'default');
                     } else {
                         // Set the manage layout.
                         $this->_helper->layout->setLayout('manage');
                     }
                 } else {
                     if (preg_match("/^\\/supervisor/i", $requestUri)) {
                         // Make sure the employee is a supervisor.
                         if (!isset($session->employee) || !$session->employee->supervisor) {
                             // Redirect to the home page.
                             $this->_helper->redirector('index', 'index', 'default');
                         } else {
                             // Set the manage layout.
                             $this->_helper->layout->setLayout('manage');
                         }
                     } else {
                         if (preg_match("/^\\/user/i", $requestUri)) {
                             // Make sure the employee is logged in.
                             if (!isset($session->employee)) {
                                 // Redirect to the home page.
                                 $this->_helper->redirector('index', 'index', 'default');
                             }
                         }
                     }
                 }
             }
         }
         // Set the pay period in the session.
         if (!isset($session->payPeriod)) {
             // Save the current pay period to the session.
             $payPeriodDao = new PayPeriodDao();
             $session->payPeriod = $payPeriodDao->getCurrent();
             // Make sure the pay period was found.
             if (!isset($session->payPeriod)) {
                 // Make sure all the pay periods exist in the database,
                 // then retrieve the current pay period.
                 $session->payPeriod = $payPeriodDao->addThroughCurrent();
             }
         }
     } catch (Zend_Exception $ex) {
         // Log the exception.
         Logger::getLogger()->debug("Base Controller Error: " . $ex->getMessage());
     }
     // Save the pay period to the view.
     $this->view->payPeriod = $session->payPeriod;
     // Save the employee to the view.
     $this->view->employee = $session->employee;
 }
 /**
  * Retrieve the previous pay period and show the employee timesheets.
  */
 function prevAction()
 {
     // Set the page title.
     $this->view->title = "Employee Timesheets";
     // Get the requested date.
     $day = $this->getDate('day');
     // Get the ids of the employees whose timesheets are to be displayed.
     $ids = $this->getInts('ids');
     // Flag that the timesheets should be editable.
     $this->view->editable = $this->getBool('edit');
     if (!isset($this->view->editable)) {
         $this->view->editable = false;
     }
     // Retrieve the current pay period and save it to the view.
     $payPeriodDao = new PayPeriodDao();
     $payPeriod = $payPeriodDao->getContaining($day);
     // Make sure the pay period was found.
     if (!isset($payPeriod)) {
         // Save an error message.
         $this->view->error = "Failed to find a system pay period " . "containing {$day}, so displaying the current pay period.\n";
         // Retrieve the current pay period.
         $payPeriod = $payPeriodDao->getCurrent();
     }
     // Get the previous pay period.
     $prev = PayPeriodHelper::getPrev($payPeriod);
     // Attempt to retrieve the previous pay period from the database.
     $realprev = $payPeriodDao->get($prev->start);
     // Make sure it was found.
     if (!isset($realprev)) {
         // Create the new pay period.
         $payPeriodDao->add(array('start' => $prev->start, 'end' => $prev->end, 'type' => $prev->type));
         $realprev = $prev;
     }
     // Get the previous pay period.
     $this->view->payPeriod = $realprev;
     // Save the active pay period in the session.
     $session = new Zend_Session_Namespace('Web');
     $session->activePayPeriod = $this->view->payPeriod;
     // Get the DAO.
     $timesheetDao = new TimesheetDao();
     // This will hold all the timesheets.
     $this->view->timesheets = array();
     // Retrieve all the timesheets.
     foreach ($ids as $id) {
         $this->view->timesheets[] = $timesheetDao->getForEmployee($id, $this->view->payPeriod);
     }
     // Set the timesheet layout for this action.
     $this->_helper->layout->setLayout('timesheet');
     // Render the view.phtml page.
     $this->render('view');
 }