/** * returns the active contract for the given employee and date or now, when no date is given * * @param string $_employeeId * @param Tinebase_DateTime $_firstDayDate * @throws Tinebase_Exception_InvalidArgument * @throws HumanResources_Exception_NoCurrentContract * @throws Tinebase_Exception_Duplicate * @return HumanResources_Model_Contract */ public function getValidContract($_employeeId, $_firstDayDate = NULL) { if (!$_employeeId) { throw new Tinebase_Exception_InvalidArgument('You have to set an account id at least'); } $_firstDayDate = $_firstDayDate ? new Tinebase_DateTime($_firstDayDate) : new Tinebase_DateTime(); $filter = new HumanResources_Model_ContractFilter(array(), 'AND'); $filter->addFilter(new Tinebase_Model_Filter_Date(array('field' => 'start_date', 'operator' => 'before', 'value' => $_firstDayDate))); $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'employee_id', 'operator' => 'equals', 'value' => $_employeeId))); $endDate = new Tinebase_Model_Filter_FilterGroup(array(), 'OR'); $endDate->addFilter(new Tinebase_Model_Filter_Date(array('field' => 'end_date', 'operator' => 'after', 'value' => $_firstDayDate))); $filter->addFilterGroup($endDate); $contracts = $this->search($filter); if ($contracts->count() < 1) { $filter = new HumanResources_Model_ContractFilter(array(), 'AND'); $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'employee_id', 'operator' => 'equals', 'value' => $_employeeId))); $contracts = $this->search($filter); if ($contracts->count() > 0) { $e = new HumanResources_Exception_NoCurrentContract(); $e->addRecord($contracts->getFirstRecord()); throw $e; } else { throw new HumanResources_Exception_NoContract(); } } else { if ($contracts->count() > 1) { throw new Tinebase_Exception_Duplicate('There are more than one valid contracts for this employee!'); } } return $contracts->getFirstRecord(); }