/** * Test case for updateHistory(). */ public function testUpdateHistory() { $history = new LocationHistory(); // invalid emp number try { $history->updateHistory('ab1', 'LOC003'); $this->fail("Exception expected"); } catch (EmpHistoryException $e) { $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode()); } // invalid location code try { $history->updateHistory(11, 'JOBA003'); $this->fail("Exception expected"); } catch (EmpHistoryException $e) { $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode()); } // No change $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL")); $before = $this->_getNumRows(); $result = $history->updateHistory(12, 'LOC003'); $this->assertFalse($result); $this->assertEquals($before, $this->_getNumRows()); $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL")); // Employee with 2 current items, verify allowed $this->_runQuery('UPDATE hs_hr_emp_location_history SET end_date = null WHERE id=' . $this->subDivisionHistory[3]->getId()); $this->assertEquals(2, $this->_getNumRows("emp_number = 11 AND end_date IS NULL")); $before = $this->_getNumRows(); // Update location already one of the current location - no change expected $result = $history->updateHistory(11, 'LOC001'); $this->assertFalse($result); $this->assertEquals(2, $this->_getNumRows("emp_number = 11 AND end_date IS NULL")); $this->assertEquals($before, $this->_getNumRows()); // Update new location, should be added to list of current locations $before = $this->_getNumRows(); $result = $history->updateHistory(11, 'LOC003'); $this->assertTrue($result); $this->assertEquals(3, $this->_getNumRows("emp_number = 11 AND end_date IS NULL")); $this->assertEquals($before + 1, $this->_getNumRows()); // Change location $before = $this->_getNumRows(); $result = $history->updateHistory(12, 'LOC001'); $this->assertTrue($result); $this->assertEquals($before + 1, $this->_getNumRows()); // Verify that existing current item's end date is not set (since multiple current items are allowed) $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL")); $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC001' AND end_date IS NULL")); // validate end date of old location not set $result = $this->_getMatchingRows('id = ' . $this->subDivisionHistory[6]->getId()); $this->assertTrue(is_array($result)); $this->assertEquals(1, count($result)); $this->assertNull($result[0]['end_date']); // validate start date of new location correctly set $result = $this->_getMatchingRows("emp_number = 12 AND code = 'LOC001' AND end_date IS NULL"); $this->assertTrue(is_array($result)); $this->assertEquals(1, count($result)); $this->assertNotNull($result[0]['start_date']); // Verify the start time is correct $startDate = $result[0]['start_date']; $this->assertTrue(time() - strtotime($startDate) < 30); // Verify name is current $this->assertEquals('Kandy', $result[0]['name']); // Update history for employee with no current history items. $this->_runQuery('DELETE from hs_hr_emp_location_history'); $this->assertEquals(0, $this->_getNumRows()); $result = $history->updateHistory(12, 'LOC003'); $this->assertTrue($result); $this->assertEquals(1, $this->_getNumRows()); $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL")); }
/** * Remove given location from employee * * @param int $empNumber Employee number * @param string $locationCode Location code to remove * * @return boolean true if successfully assigned, false otherwise */ public function removeLocation($empNumber, $locationCode) { $result = false; $auth = new authorize($_SESSION['empID'], $_SESSION['isAdmin']); /* Only allow admins and supervisors of the given employee to assign locations */ if ($auth->isAdmin() || $auth->isSupervisor() && $auth->isTheSupervisor($empNumber)) { $empLocation = new EmpLocation($empNumber, $locationCode); try { $empLocation->delete(); $result = true; $history = new LocationHistory(); $history->updateHistory($empNumber, $locationCode, true); } catch (EmpLocationException $e) { } } return $result; }