/**
  * @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']);
 }
 /**
  * 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!');
 }
 /**
  * update employee
  * 
  * @param Zend_Console_Getopt $opts
  * @param HumanResources_Model_Employee $employee
  * @param HumanResources_Model_Employee $currentEmployee
  * @return HumanResources_Model_Employee
  */
 protected function _updateImportedEmployee($opts, $employee, $currentEmployee)
 {
     if ($opts->v) {
         echo "Updating employee " . $employee->n_fn . ".\n";
     }
     // update only some fields
     $fieldsToUpdate = array('postalcode', 'locality', 'street', 'bday', 'employment_begin', 'employment_end', 'bank_account_number', 'bank_name', 'bank_code_number', 'health_insurance', 'number', 'contracts');
     $changed = FALSE;
     foreach ($fieldsToUpdate as $field) {
         if (!empty($employee->{$field}) && $currentEmployee->{$field} !== $employee->{$field}) {
             if ($field === 'contracts' && !empty($currentEmployee->{$field})) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($currentEmployee->toArray(), TRUE));
                 }
             } else {
                 $currentEmployee->{$field} = $employee->{$field};
             }
             $changed = TRUE;
         }
     }
     if ($opts->d || !$changed) {
         $result = $currentEmployee;
     } else {
         $json = new HumanResources_Frontend_Json();
         $result = $json->saveEmployee($currentEmployee->toArray());
         if ($currentEmployee->contracts) {
             $rs = new Tinebase_Record_RecordSet('HumanResources_Model_Contract');
             foreach ($currentEmployee->contracts as $contract) {
                 $rs->addRecord(new HumanResources_Model_Contract($contract));
             }
             $currentEmployee->contracts = $rs;
         }
         if ($currentEmployee->costcenters) {
             $ccrs = new Tinebase_Record_RecordSet('HumanResources_Model_CostCenter');
             foreach ($currentEmployee->costcenters as $costcenter) {
                 $ccrs->addRecord(new HumanResources_Model_CostCenter($costcenter));
             }
             $currentEmployee->costcenters = $ccrs;
         }
         $result = HumanResources_Controller_Employee::getInstance()->update($currentEmployee);
     }
     return $result;
 }