/**
  * save HR settings
  *
  * @param array config
  * @return Sales_Model_Config
  *
  * @todo generalize this
  */
 public function setConfig($config)
 {
     if (!Tinebase_Core::getUser()->hasRight('HumanResources', 'admin')) {
         throw new Tinebase_Exception_AccessDenied(_('You do not have admin rights on HumanResources'));
     }
     foreach (array(HumanResources_Config::VACATION_EXPIRES, HumanResources_Config::DEFAULT_FEAST_CALENDAR) as $cfg) {
         if (!empty($config[$cfg])) {
             HumanResources_Config::getInstance()->set($cfg, $config[$cfg]);
         } else {
             HumanResources_Config::getInstance()->delete($cfg);
         }
     }
     return array('SUCCESS' => TRUE);
 }
 /**
  * book remaining vacation days for the next year
  * 
  * @param array $accountIds
  * @return booleam
  */
 public function bookRemainingVacation($accountIds)
 {
     $filter = new HumanResources_Model_AccountFilter(array(array('field' => 'id', 'operator' => 'in', 'value' => $accountIds)));
     $accounts = $this->search($filter);
     $freeTimeController = HumanResources_Controller_FreeTime::getInstance();
     $freeDayController = HumanResources_Controller_FreeDay::getInstance();
     $configInstance = HumanResources_Config::getInstance();
     $extraFreeTimeController = HumanResources_Controller_ExtraFreeTime::getInstance();
     $db = method_exists($this->_backend, 'getAdapter') ? $this->_backend->getAdapter() : Tinebase_Core::getDb();
     $thisYear = (int) Tinebase_DateTime::now()->format('Y');
     try {
         foreach ($accounts as $account) {
             $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction($db);
             if ($account->year >= $thisYear) {
                 throw new HumanResources_Exception_RemainingNotBookable();
             }
             $data = $this->resolveVirtualFields($account);
             // do nothing if there are no remaining vacation days
             if ($data['remaining_vacation_days'] <= 0) {
                 continue;
             }
             $year = intval($account->year) + 1;
             // get account of next year
             $filter = new HumanResources_Model_AccountFilter(array(array('field' => 'year', 'operator' => 'equals', 'value' => $year)));
             $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'employee_id', 'operator' => 'equals', 'value' => $account->employee_id)));
             $result = $this->search($filter);
             if ($result->count() == 0) {
                 $ca = $this->createMissingAccounts($year, $account->employee_id);
                 $newAccount = $ca->getFirstRecord();
             } elseif ($result->count() > 1) {
                 throw new Tinebase_Exception_Record_NotAllowed('There is more than one account for the year ' . $year . '!');
             } else {
                 $newAccount = $result->getFirstRecord();
             }
             // create new extraFreetime for the new year
             $extraFreeTime = new HumanResources_Model_ExtraFreeTime(array('days' => $data['remaining_vacation_days'], 'account_id' => $newAccount->getId(), 'type' => 'PAYED', 'description' => 'Booked from last year', 'expires' => $configInstance->getVacationExpirationDate()));
             $extraFreeTimeController->create($extraFreeTime);
             // create freetimes for old year
             $freetime = $freeTimeController->create(new HumanResources_Model_FreeTime(array('type' => 'vacation', 'description' => 'Booked as extra freetime for next year.', 'status' => 'ACCEPTED', 'firstday_date' => NULL, 'employee_id' => $account->employee_id, 'account_id' => $account->getId())));
             $i = 0;
             while ($i < $data['remaining_vacation_days']) {
                 $freeDay = $freeDayController->create(new HumanResources_Model_FreeDay(array('freetime_id' => $freetime->getId(), 'duration' => 1, 'date' => null)));
                 $i++;
             }
         }
         Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId);
         return true;
     } catch (Exception $e) {
         Tinebase_TransactionManager::getInstance()->rollBack();
         throw $e;
     }
 }
 /**
  * Returns registry data of the application.
  *
  * Each application has its own registry to supply static data to the client.
  * Registry data is queried only once per session from the client.
  *
  * This registry must not be used for rights or ACL purposes. Use the generic
  * rights and ACL mechanisms instead!
  *
  * @return mixed array 'variable name' => 'data'
  */
 public function getRegistryData()
 {
     $data = parent::getRegistryData();
     $ci = HumanResources_Config::getInstance();
     $calid = $ci->get($ci::DEFAULT_FEAST_CALENDAR, NULL);
     $data[$ci::DEFAULT_FEAST_CALENDAR] = $calid ? Tinebase_Container::getInstance()->get($calid)->toArray() : NULL;
     $data[$ci::VACATION_EXPIRES] = $ci->get($ci::VACATION_EXPIRES);
     return $data;
 }
 /**
  * Returns instance of Tinebase_Config
  *
  * @return Tinebase_Config
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }