/**
  * some contract tests (more in jsontests)
  */
 public function testContract()
 {
     $sdate = new Tinebase_DateTime('2013-01-01 00:00:00');
     $employee = $this->_getEmployee('rwright');
     $contractController = HumanResources_Controller_Contract::getInstance();
     $employeeController = HumanResources_Controller_Employee::getInstance();
     $employee = $employeeController->create($employee);
     $contract = $this->_getContract($sdate);
     $contract->workingtime_json = '{"days": [8,8,8,8,8,0,0]}';
     // create feast days
     $feastDays2013 = array(array('2013-12-25', '2013-12-26'), array('2013-04-01'), '2013-05-01', '2013-05-09', '2013-05-20', '2013-10-03', '2013-01-01', '2013-03-29', '2013-12-31', '2009-12-31');
     $feastCalendar = $this->_getFeastCalendar();
     $contract->feast_calendar_id = $feastCalendar->getId();
     foreach ($feastDays2013 as $day) {
         if (is_array($day)) {
             $date = array();
             foreach ($day as $dayQ) {
                 $date[] = new Tinebase_DateTime($dayQ . ' 06:00:00');
             }
         } else {
             $date = new Tinebase_DateTime($day . ' 06:00:00');
         }
         $this->_createFeastDay($date);
     }
     // test "calculateVacationDays"
     $start = new Tinebase_DateTime('2013-01-01');
     $stop = new Tinebase_DateTime('2013-12-31');
     $contract->start_date = $start;
     $contract->end_date = $stop;
     $this->assertEquals(30, $contractController->calculateVacationDays($contract, $start, $stop));
     $newStartDate = new Tinebase_DateTime('2013-07-01');
     $contract->start_date = $newStartDate;
     $contract->employee_id = $employee->getId();
     $contractController->create($contract);
     $this->assertEquals(15, round($contractController->calculateVacationDays($contract, $start, $stop), 0));
     // test "getDatesToWorkOn"
     $contract->start_date = $start;
     // 2013 has 365 days, 52 Saturdays and 52 Sundays, all of the 10 feast days are at working days (a good year for an employee!)
     // so we expect 365-52-52-10 = 251 days
     $workingDates = $contractController->getDatesToWorkOn($contract, $start, $stop);
     $this->assertEquals(251, count($workingDates['results']));
     // test $respectTakenVacationDays parameter of getDatesToWorkOn
     $accountController = HumanResources_Controller_Account::getInstance();
     $accounts = $accountController->createMissingAccounts(2013, $contract->employee_id);
     $account = $accounts->getFirstRecord();
     $refDate = clone $newStartDate;
     // get a monday
     $refDate->addWeek(1)->addDay(1);
     // now add 3 vacation days
     $freetime = array('account_id' => $account->getId(), 'employee_id' => $contract->employee_id, 'type' => 'vacation', 'status' => 'ACCEPTED', 'firstday_date' => $refDate->toString());
     $freetime['freedays'] = array(array('duration' => '1', 'date' => $refDate->toString()), array('duration' => '1', 'date' => $refDate->addDay(1)->toString()), array('duration' => '1', 'date' => $refDate->addDay(1)->toString()));
     $json = new HumanResources_Frontend_Json();
     $freetime = $json->saveFreeTime($freetime);
     $workingDates = $contractController->getDatesToWorkOn($contract, $start, $stop, TRUE);
     $this->assertEquals(248, count($workingDates['results']));
     // test "getFeastDays"
     $feastDays = $contractController->getFeastDays($contract, $start, $stop);
     // we expect 10 here
     $this->assertEquals(10, count($feastDays), '10 feast days should have been found!');
 }
コード例 #2
0
 /**
  * the constructor
  *
  * don't use the constructor. use the singleton
  */
 private function __construct()
 {
     $this->_applicationName = 'HumanResources';
     $this->_backend = new HumanResources_Backend_Account();
     $this->_modelName = 'HumanResources_Model_Account';
     $this->_purgeRecords = FALSE;
     // activate this if you want to use containers
     $this->_doContainerACLChecks = FALSE;
     $this->_contractController = HumanResources_Controller_Contract::getInstance();
 }
コード例 #3
0
 /**
  * @see: https://forge.tine20.org/mantisbt/view.php?id=10176
  */
 public function testSavingRelatedRecordWithCorruptId()
 {
     $date = new Tinebase_DateTime();
     $e = $this->_getEmployee();
     $c = $this->_getContract($date);
     $c->id = '1234567890';
     $employeeJson = $e->toArray();
     $employeeJson = $this->_json->saveEmployee($employeeJson);
     $c->employee_id = $employeeJson['id'];
     $c = HumanResources_Controller_Contract::getInstance()->create($c);
     $this->assertEquals('1234567890', $c->getId());
     $employeeJson['contracts'] = array($c->toArray());
     $employeeJson = $this->_json->saveEmployee($employeeJson);
     // if it has been corrupted before this change was committed, the corrupted id should stay
     $this->assertEquals('1234567890', $employeeJson['contracts'][0]['id']);
 }
コード例 #4
0
 /**
  * delete linked objects (notes, relations, ...) of record
  *
  * @param Tinebase_Record_Interface $_record
  */
 protected function _deleteLinkedObjects(Tinebase_Record_Interface $_record)
 {
     // use textfilter for employee_id
     $eFilter = new Tinebase_Model_Filter_Text(array('field' => 'employee_id', 'operator' => 'equals', 'value' => $_record->getId()));
     // delete free times
     $filter = new HumanResources_Model_FreeTimeFilter(array(), 'AND');
     $filter->addFilter($eFilter);
     HumanResources_Controller_FreeTime::getInstance()->deleteByFilter($filter);
     // delete contracts
     $filter = new HumanResources_Model_ContractFilter(array(), 'AND');
     $filter->addFilter($eFilter);
     HumanResources_Controller_Contract::getInstance()->deleteByFilter($filter);
     // delete costcenters
     if ($_record->has('costcenters')) {
         $filter = new HumanResources_Model_CostCenterFilter(array(), 'AND');
         $filter->addFilter($eFilter);
         HumanResources_Controller_CostCenter::getInstance()->deleteByFilter($filter);
     }
     // delete accounts
     $filter = new HumanResources_Model_AccountFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'employee_id', 'operator' => 'equals', 'value' => $_record->getId())));
     HumanResources_Controller_Account::getInstance()->deleteByFilter($filter);
     parent::_deleteLinkedObjects($_record);
 }
コード例 #5
0
 /**
  * returns feast days and freedays of an employee for the freetime edit dialog
  * 
  * @param string $_employeeId
  * @param integer $_year
  * @param string $_freeTimeId
  * @param string $_accountId
  */
 public function getFeastAndFreeDays($_employeeId, $_year = NULL, $_freeTimeId = NULL, $_accountId = NULL)
 {
     $cController = HumanResources_Controller_Contract::getInstance();
     $eController = HumanResources_Controller_Employee::getInstance();
     $aController = HumanResources_Controller_Account::getInstance();
     $ftController = HumanResources_Controller_FreeTime::getInstance();
     $fdController = HumanResources_Controller_FreeDay::getInstance();
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' $_employeeId ' . $_employeeId . ' $_year ' . $_year . ' $_freeTimeId ' . $_freeTimeId . ' $_accountId ' . $_accountId);
     }
     // validate employeeId
     $employee = $eController->get($_employeeId);
     $_freeTimeId = strlen($_freeTimeId) == 40 ? $_freeTimeId : NULL;
     // set period to search for
     $minDate = Tinebase_DateTime::now()->setTimezone(Tinebase_Core::getUserTimezone())->setTime(0, 0, 0);
     if ($_year && !$_freeTimeId) {
         $minDate->setDate($_year, 1, 1);
     } elseif ($_freeTimeId) {
         // if a freetime id is given, take the year of the freetime
         $myFreeTime = $ftController->get($_freeTimeId);
         $minDate->setDate($myFreeTime->firstday_date->format('Y'), 1, 1);
     } else {
         $minDate->setDate($minDate->format('Y'), 1, 1);
     }
     if (!$_accountId) {
         // find account
         $filter = new HumanResources_Model_AccountFilter(array(array('field' => 'year', 'operator' => 'equals', 'value' => intval($_year))));
         $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'employee_id', 'operator' => 'equals', 'value' => $_employeeId)));
         $account = $aController->search($filter)->getFirstRecord();
     } else {
         try {
             $account = $aController->get($_accountId);
         } catch (Exception $e) {
             // throws a few lines later: HumanResources_Exception_NoAccount
         }
     }
     if (!$account) {
         throw new HumanResources_Exception_NoAccount();
     }
     $accountYear = $account->year;
     $minAccountDate = Tinebase_DateTime::now()->setTimezone(Tinebase_Core::getUserTimezone())->setTime(0, 0, 0);
     $minAccountDate->setDate($accountYear, 1, 1);
     $maxAccountDate = clone $minAccountDate;
     $maxAccountDate->addYear(1)->subSecond(1);
     $maxDate = clone $minDate;
     $maxDate->addYear(1)->subSecond(1);
     // find contracts of the account year
     $contracts = $cController->getValidContracts($minAccountDate, $maxAccountDate, $_employeeId);
     $contracts->sort('start_date', 'ASC');
     if ($contracts->count() < 1) {
         throw new HumanResources_Exception_NoContract();
     }
     $remainingVacation = 0;
     $contracts->setTimezone(Tinebase_Core::getUserTimezone());
     // find out total amount of vacation days for the different contracts
     foreach ($contracts as $contract) {
         $remainingVacation += $cController->calculateVacationDays($contract, $minDate, $maxDate);
     }
     $remainingVacation = round($remainingVacation, 0);
     $allVacation = $remainingVacation;
     // find contracts of the year in which the vacation days will be taken
     $contracts = $cController->getValidContracts($minDate, $maxDate, $_employeeId);
     $contracts->sort('start_date', 'ASC');
     $excludeDates = array();
     if ($contracts->count() < 1) {
         throw new HumanResources_Exception_NoContract();
     }
     $first = TRUE;
     $feastDays = array();
     $contracts->setTimezone(Tinebase_Core::getUserTimezone());
     // find out disabled days for the different contracts
     foreach ($contracts as $contract) {
         $json = $contract->getWorkingTimeJson();
         $startDay = $contract->start_date == NULL ? $minDate : $contract->start_date < $minDate ? $minDate : $contract->start_date;
         $stopDay = $contract->end_date == NULL ? $maxDate : $contract->end_date > $maxDate ? $maxDate : $contract->end_date;
         if ($first) {
             $firstDay = clone $startDay;
             $first = FALSE;
         }
         // find out weekdays to disable
         if (is_object($json)) {
             foreach ($json->days as $index => $hours) {
                 $hours = intval($hours);
                 if ($hours === 0) {
                     $day = clone $startDay;
                     $day->setWeekDay($index + 1);
                     while ($day->compare($stopDay) == -1) {
                         $exdate = clone $day;
                         $exdate->setTimezone(Tinebase_Core::getUserTimezone());
                         $excludeDates[] = $exdate;
                         $day->addWeek(1);
                     }
                 }
             }
         }
         // search feast days
         $feastDays = array_merge($cController->getFeastDays($contract, $startDay, $stopDay), $feastDays);
     }
     // set time to 0
     foreach ($feastDays as &$feastDay) {
         $feastDay->setTimezone(Tinebase_Core::getUserTimezone())->setTime(0, 0, 0);
     }
     // search free times for the account and the interval
     // prepare free time filter, add employee_id
     $freeTimeFilter = new HumanResources_Model_FreeTimeFilter(array(), 'AND');
     $freeTimeFilter->addFilter(new Tinebase_Model_Filter_Id(array('field' => 'employee_id', 'operator' => 'equals', 'value' => $_employeeId)));
     // don't search for freetimes belonging to the freetime handled itself
     if ($_freeTimeId) {
         $freeTimeFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'id', 'operator' => 'not', 'value' => $_freeTimeId)));
     }
     // prepare vacation times filter
     $vacationTimesFilter = clone $freeTimeFilter;
     $vacationTimesFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'type', 'operator' => 'equals', 'value' => 'vacation')));
     // search all vacation times belonging to the account, regardless which interval we want
     $accountFreeTimesFilter = clone $vacationTimesFilter;
     $accountFreeTimesFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'account_id', 'operator' => 'equals', 'value' => $account->getId())));
     $accountVacationTimeIds = $ftController->search($accountFreeTimesFilter)->id;
     // search all vacation times for the interval
     $fddMin = clone $minDate;
     $fddMin->subDay(1);
     $fddMax = clone $maxDate;
     $fddMax->addDay(1);
     $vacationTimesFilter->addFilter(new Tinebase_Model_Filter_Date(array('field' => 'firstday_date', 'operator' => 'after', 'value' => $fddMin)));
     $vacationTimesFilter->addFilter(new Tinebase_Model_Filter_Date(array('field' => 'firstday_date', 'operator' => 'before', 'value' => $fddMax)));
     $vacationTimes = $ftController->search($vacationTimesFilter);
     $acceptedVacationTimes = $vacationTimes->filter('status', 'ACCEPTED');
     // search all sickness times for the interval
     $sicknessTimesFilter = clone $freeTimeFilter;
     $sicknessTimesFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'type', 'operator' => 'equals', 'value' => 'sickness')));
     $sicknessTimesFilter->addFilter(new Tinebase_Model_Filter_Date(array('field' => 'firstday_date', 'operator' => 'after', 'value' => $fddMin)));
     $sicknessTimesFilter->addFilter(new Tinebase_Model_Filter_Date(array('field' => 'firstday_date', 'operator' => 'before', 'value' => $fddMax)));
     $sicknessTimes = $ftController->search($sicknessTimesFilter);
     // search free days belonging the found free times
     // prepare free day filter
     $freeDayFilter = new HumanResources_Model_FreeDayFilter(array(), 'AND');
     $freeDayFilter->addFilter(new Tinebase_Model_Filter_Int(array('field' => 'duration', 'operator' => 'equals', 'value' => 1)));
     // find vacation days belonging to the account (date doesn't matter, may be from another year, just count the days)
     if (count($accountVacationTimeIds)) {
         $accountFreeDayFilter = clone $freeDayFilter;
         $accountFreeDayFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'freetime_id', 'operator' => 'in', 'value' => $accountVacationTimeIds)));
         $remainingVacation = $remainingVacation - $fdController->search($accountFreeDayFilter)->count();
     }
     // find all vacation days of the period
     $vacationDayFilter = clone $freeDayFilter;
     $vacationDayFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'freetime_id', 'operator' => 'in', 'value' => $vacationTimes->id)));
     $vacationDays = $fdController->search($vacationDayFilter);
     // find out accepted vacation days. Vacation days will be substracted from remainingVacation only if they are accepted,
     // but they will be shown in the freetime edit dialog
     // TODO: discuss this
     $acceptedVacationDayFilter = clone $freeDayFilter;
     $acceptedVacationDayFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'freetime_id', 'operator' => 'in', 'value' => $acceptedVacationTimes->id)));
     $acceptedVacationDays = $fdController->search($acceptedVacationDayFilter);
     // calculate extra vacation days
     if ($account) {
         $filter = new HumanResources_Model_ExtraFreeTimeFilter(array());
         $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'account_id', 'operator' => 'equals', 'value' => $account->getId())));
         $account->extra_free_times = HumanResources_Controller_ExtraFreeTime::getInstance()->search($filter);
         $extraFreeTimes = $aController->calculateExtraFreeTimes($account, $acceptedVacationDays);
         $allVacation = $allVacation + $extraFreeTimes['remaining'];
         $remainingVacation = $remainingVacation + $extraFreeTimes['remaining'];
     } else {
         $extraFreeTimes = NULL;
     }
     // find all sickness days of the period
     $sicknessDayFilter = clone $freeDayFilter;
     $sicknessDayFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'freetime_id', 'operator' => 'in', 'value' => $sicknessTimes->id)));
     $sicknessDays = $fdController->search($sicknessDayFilter);
     $ownFreeDays = NULL;
     if ($_freeTimeId) {
         $ownFreeDaysFilter = clone $freeDayFilter;
         $ownFreeDaysFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'freetime_id', 'operator' => 'in', 'value' => array($_freeTimeId))));
         $ownFreeDays = $fdController->search($ownFreeDaysFilter);
         $remainingVacation = $remainingVacation - $ownFreeDays->count();
         $ownFreeDays = $ownFreeDays->toArray();
     }
     // TODO: remove results property, just return results array itself
     return array('results' => array('remainingVacation' => floor($remainingVacation), 'extraFreeTimes' => $extraFreeTimes, 'vacationDays' => $vacationDays->toArray(), 'sicknessDays' => $sicknessDays->toArray(), 'excludeDates' => $excludeDates, 'ownFreeDays' => $ownFreeDays, 'allVacation' => $allVacation, 'feastDays' => $feastDays, 'contracts' => $contracts->toArray(), 'employee' => $employee->toArray(), 'firstDay' => $firstDay, 'lastDay' => $stopDay));
 }
コード例 #6
0
 /**
  * tests set_contracts_end_date
  */
 public function testSetContractsEndDate()
 {
     $this->_doImport(FALSE);
     $cc = HumanResources_Controller_Contract::getInstance();
     $ec = HumanResources_Controller_Employee::getInstance();
     $user = Tinebase_Core::getUser();
     $begin = Tinebase_DateTime::now()->subYear(1);
     $end = clone $begin;
     $end->addMonth(11);
     $employee = new HumanResources_Model_Employee(array('account_id' => $user->getId(), 'n_family' => 'TEST', 'n_given' => 'UNIT', 'employment_begin' => $begin, 'employment_end' => NULL));
     $employee = $ec->create($employee);
     $contract = new HumanResources_Model_Contract(array('employee_id' => $employee->getId(), 'start_date' => $begin, 'end_date' => NULL, 'workingtime_json' => '{days: [4,4,4,4,4,0,0]}', 'vacation_days' => 34));
     $contract = $cc->create($contract);
     $this->assertEquals(NULL, $contract->end_date);
     $this->assertEquals(NULL, $employee->employment_end);
     $newEnd = Tinebase_DateTime::now()->subDay(1);
     $employee->employment_end = $newEnd;
     $employee->contracts = array($contract->toArray());
     $ec->update($employee);
     $this->_cli->set_contracts_end_date();
     $allContracts = $cc->getAll();
     $updatedContract = $cc->get($contract->getId());
     $this->assertEquals($newEnd, $updatedContract->end_date);
 }
コード例 #7
0
 /**
  * sets the contracts end_date to the date of employment_begin of the corresponding 
  * employee, if employee has an employment end date
  */
 public function set_contracts_end_date()
 {
     $eController = HumanResources_Controller_Employee::getInstance();
     $cController = HumanResources_Controller_Contract::getInstance();
     $filter = new HumanResources_Model_EmployeeFilter(array(array('field' => 'is_employed', 'operator' => 'equals', 'value' => FALSE)));
     $oldEmployees = $eController->search($filter);
     foreach ($oldEmployees as $employee) {
         $contract = $cController->getContractsByEmployeeId($employee->getId())->sort('start_date', 'DESC')->getFirstRecord();
         if ($contract) {
             $contract->end_date = $employee->employment_end;
             $cController->update($contract);
         }
     }
 }