/**
  * creates missing accounts for all employees having a valid contract
  * if a year is given, missing accounts for this year will be built, otherwise the current and following year will be used
  *
  * @param integer $year
  * @param HumanResources_Model_Employee
  * @param boolean $useBackend Use Backend instead of this Controller (may called by setup also, skips rigts, creating modlog etc.)
  */
 public function createMissingAccounts($year = NULL, $employee = NULL, $useBackend = FALSE)
 {
     // if no year is given, call myself with this year and just go on with the next year
     if (!$year) {
         $date = new Tinebase_DateTime();
         $year = (int) $date->format('Y');
         $this->createMissingAccounts($year, $employee);
         $date->addYear(1);
         $year = (int) $date->format('Y');
     }
     // tine20 should last a hundred years :)
     if ($year < 2006 || $year >= 2106 || !is_int($year)) {
         throw new Tinebase_Exception_Data('The year must be between 2006 and 2106');
     }
     // borders
     $year_starts = new Tinebase_DateTime($year . '-01-01 00:00:00');
     $year_ends = new Tinebase_DateTime($year . '-12-31 23:59:59');
     $validEmployeeIds = array_unique($this->_contractController->getValidContracts($year_starts, $year_ends, $employee)->employee_id);
     $existingFilter = new HumanResources_Model_AccountFilter(array(array('field' => 'year', 'operator' => 'equals', 'value' => $year)));
     $existingFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'employee_id', 'operator' => 'in', 'value' => $validEmployeeIds)));
     $result = $this->search($existingFilter)->employee_id;
     $validEmployeeIds = array_diff($validEmployeeIds, $result);
     $createdAccounts = new Tinebase_Record_RecordSet('HumanResources_Model_Account');
     if ($useBackend) {
         $be = new HumanResources_Backend_Account();
         foreach ($validEmployeeIds as $id) {
             $createdAccounts->addRecord($be->create(new HumanResources_Model_Account(array('employee_id' => $id, 'year' => $year))));
         }
     } else {
         foreach ($validEmployeeIds as $id) {
             $createdAccounts->addRecord($this->create(new HumanResources_Model_Account(array('employee_id' => $id, 'year' => $year))));
         }
     }
     return $createdAccounts;
 }