Esempio n. 1
0
 /**
  * checks deadline of record
  * 
  * @param Timetracker_Model_Timesheet $_record
  * @param boolean $_throwException
  * @return void
  * @throws Timetracker_Exception_Deadline
  */
 protected function _checkDeadline(Timetracker_Model_Timesheet $_record, $_throwException = TRUE)
 {
     // get timeaccount
     $timeaccount = Timetracker_Controller_Timeaccount::getInstance()->get($_record->timeaccount_id);
     if (isset($timeaccount->deadline) && $timeaccount->deadline == Timetracker_Model_Timeaccount::DEADLINE_LASTWEEK) {
         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Check if deadline is exceeded for timeaccount ' . $timeaccount->title);
         }
         // it is only on monday allowed to add timesheets for last week
         $date = new Tinebase_DateTime();
         $date->setTime(0, 0, 0);
         $dayOfWeek = $date->get('w');
         if ($dayOfWeek >= 2) {
             // only allow to add ts for this week
             $date->sub($dayOfWeek, Tinebase_DateTime::MODIFIER_DAY);
         } else {
             // only allow to add ts for last week
             $date->sub($dayOfWeek + 7, Tinebase_DateTime::MODIFIER_DAY);
         }
         // convert start date to Tinebase_DateTime
         $startDate = new Tinebase_DateTime($_record->start_date);
         if ($date->compare($startDate) >= 0) {
             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                 Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Deadline exceeded: ' . $startDate . ' < ' . $date);
             }
             if ($this->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE) || Timetracker_Model_TimeaccountGrants::hasGrant($_record->timeaccount_id, Tinebase_Model_Grants::GRANT_ADMIN)) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                     Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' User with admin / manage all rights is allowed to save Timesheet even if it exceeds the deadline.');
                 }
             } else {
                 if ($_throwException) {
                     throw new Timetracker_Exception_Deadline();
                 }
             }
         } else {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Valid date: ' . $startDate . ' >= ' . $date);
             }
         }
     }
 }
 /**
  * get string representation of first and last days of the week defined by date/week number
  * 
  * @param Tinebase_DateTime $_date
  * @param integer $_weekNumber optional
  * @return array
  */
 protected function _getFirstAndLastDayOfWeek(Tinebase_DateTime $_date, $_weekNumber = NULL)
 {
     $firstDayOfWeek = $this->_getFirstDayOfWeek();
     if ($_weekNumber !== NULL) {
         $_date->setWeek($_weekNumber);
     }
     $dayOfWeek = $_date->get('w');
     // in some locales sunday is last day of the week -> we need to init dayOfWeek with 7
     $dayOfWeek = $firstDayOfWeek == 1 && $dayOfWeek == 0 ? 7 : $dayOfWeek;
     $_date->sub($dayOfWeek - $firstDayOfWeek, Tinebase_DateTime::MODIFIER_DAY);
     $firstDay = $_date->toString($this->_dateFormat);
     $_date->add(6, Tinebase_DateTime::MODIFIER_DAY);
     $lastDay = $_date->toString($this->_dateFormat);
     $result = array($firstDay, $lastDay);
     return $result;
 }
 /**
  * try to add a Timesheet with different grants
  * 
  * @param   Tinebase_Record_RecordSet $_grants
  * @param   string $_action
  * @param   mixed $_expect
  * @param   Timetracker_Model_Timesheet
  */
 protected function _grantTestHelper($_grants, $_action = 'create', $_expect = NULL, $_ts = NULL)
 {
     // take default ts?
     $ts = $_ts ? $_ts : $this->_objects['timesheet'];
     // remove BOOK_OWN + BOOK_ALL + ADMIN grant
     Timetracker_Model_TimeaccountGrants::setTimeaccountGrants($this->_objects['timeaccount'], $_grants, TRUE);
     // try to create timesheet
     switch ($_action) {
         case 'create':
             if ($_expect === 'Exception') {
                 $this->setExpectedException('Tinebase_Exception_AccessDenied');
                 $this->_timesheetController->create($ts);
             } else {
                 $ts = $this->_timesheetController->create($ts);
                 $this->assertEquals(Tinebase_Core::getUser()->getId(), $ts->created_by);
             }
             break;
         case 'create_deadline':
             // date is before deadline
             $date = new Tinebase_DateTime();
             $date->sub(8, Tinebase_DateTime::MODIFIER_DAY);
             $ts->start_date = $date->toString('Y-m-d');
             $this->setExpectedException('Timetracker_Exception_Deadline');
             $this->_timesheetController->create($ts);
             break;
         case 'search_bookable':
             $filter = $this->_getTimeaccountFilter(TRUE);
             $result = $this->_timeaccountController->search($filter);
             $this->assertEquals($_expect, count($result));
             break;
         case 'searchTA':
             $filter = $this->_getTimeaccountFilter();
             $result = $this->_timeaccountController->search($filter);
             $this->assertEquals($_expect, count($result));
             break;
         case 'searchTS':
             $filter = $this->_getTimesheetFilter();
             $ts = $this->_timesheetController->create($ts);
             $result = $this->_timesheetController->search($filter);
             $this->assertEquals($_expect, count($result));
             break;
         case 'searchTSExport':
             $filter = $this->_getTimesheetFilter();
             $result = $this->_timesheetController->search($filter, NULL, FALSE, FALSE, 'export');
             $this->assertEquals($_expect, count($result));
             break;
         default:
             echo "nothing tested.";
     }
     // delete (set delete grant first)
     $grants = new Tinebase_Record_RecordSet('Timetracker_Model_TimeaccountGrants', array(array('account_id' => Tinebase_Core::getUser()->getId(), 'account_type' => 'user', Timetracker_Model_TimeaccountGrants::MANAGE_BILLABLE => TRUE, Tinebase_Model_Grants::GRANT_ADMIN => TRUE, Timetracker_Model_TimeaccountGrants::BOOK_ALL => TRUE, Timetracker_Model_TimeaccountGrants::BOOK_OWN => TRUE, Timetracker_Model_TimeaccountGrants::VIEW_ALL => TRUE, Tinebase_Model_Grants::GRANT_EXPORT => TRUE)));
     Timetracker_Model_TimeaccountGrants::setTimeaccountGrants($this->_objects['timeaccount'], $grants, TRUE);
 }