protected function linkLeaveToUnusedLeaveEntitlement(LeaveEntitlement $leaveEntitlement)
 {
     $balance = $leaveEntitlement->getNoOfDays() - $leaveEntitlement->getDaysUsed();
     $entitlementId = $leaveEntitlement->getId();
     if ($balance > 0) {
         $leaveList = $this->getLeaveWithoutEntitlements($leaveEntitlement->getEmpNumber(), $leaveEntitlement->getLeaveTypeId(), $leaveEntitlement->getFromDate(), $leaveEntitlement->getToDate());
         $query = Doctrine_Query::create()->from('LeaveLeaveEntitlement l')->where('l.leave_id = ?')->andWhere('l.entitlement_id = ?');
         foreach ($leaveList as $leave) {
             $daysLeft = $leave['days_left'];
             $leaveId = $leave['id'];
             $daysToAssign = $daysLeft > $balance ? $balance : $daysLeft;
             $leaveEntitlement->setDaysUsed($leaveEntitlement->getDaysUsed() - $daysToAssign);
             $balance -= $daysToAssign;
             // assign to leave
             $entitlementAssignment = $query->fetchOne(array($leaveId, $entitlementId));
             if ($entitlementAssignment === false) {
                 $entitlementAssignment = new LeaveLeaveEntitlement();
                 $entitlementAssignment->setLeaveId($leaveId);
                 $entitlementAssignment->setEntitlementId($entitlementId);
                 $entitlementAssignment->setLengthDays($daysToAssign);
             } else {
                 $entitlementAssignment->setLengthDays($entitlementAssignment->getLengthDays() + $daysToAssign);
             }
             $entitlementAssignment->save();
             $entitlementAssignment->free();
             if ($balance <= 0) {
                 break;
             }
         }
     }
     return $leaveEntitlement;
 }
Ejemplo n.º 2
0
 public function changeLeaveStatus(Leave $leave, $entitlementChanges, $removeLinkedEntitlements = false)
 {
     $conn = Doctrine_Manager::connection();
     $conn->beginTransaction();
     try {
         if ($removeLinkedEntitlements) {
             $leaveId = $leave->getId();
             $stmt = $conn->prepare("UPDATE ohrm_leave_leave_entitlement le LEFT JOIN ohrm_leave_entitlement e " . "on e.id = le.entitlement_id " . "SET e.days_used = IF(e.days_used<le.length_days,0,e.days_used - le.length_days) " . "WHERE le.leave_id = ?");
             $stmt->execute(array($leaveId));
             Doctrine_Query::create()->delete()->from('LeaveLeaveEntitlement l')->where('l.leave_id = ?', $leaveId)->execute();
         }
         $leave->save();
         if (isset($entitlementChanges['change'])) {
             // TODO: Need to update days_used here
             // Also need to check if we need to delete all entitlements or only have changes
             $changes = $entitlementChanges['change'];
             foreach ($changes as $leaveId => $change) {
                 $updateSql = '';
                 $idList = '';
                 $separator = '';
                 foreach ($change as $entitlementId => $length) {
                     $idList .= $separator . $entitlementId;
                     $updateSql .= sprintf(' WHEN e.id = %d THEN e.days_used + %f', $entitlementId, $length);
                     $separator = ',';
                     $entitlementAssignment = Doctrine_Query::create()->from('LeaveLeaveEntitlement l')->where('l.leave_id = ?', $leaveId)->andWhere('l.entitlement_id = ?', $entitlementId)->fetchOne();
                     if ($entitlementAssignment === false) {
                         $entitlementAssignment = new LeaveLeaveEntitlement();
                         $entitlementAssignment->setLeaveId($leaveId);
                         $entitlementAssignment->setEntitlementId($entitlementId);
                         $entitlementAssignment->setLengthDays($length);
                     } else {
                         $entitlementAssignment->setLengthDays($entitlementAssignment->getLengthDays() + $length);
                     }
                     $entitlementAssignment->save();
                 }
                 if ($updateSql != '') {
                     $query = "UPDATE ohrm_leave_entitlement e " . "SET e.days_used = CASE " . $updateSql . " END " . sprintf(" WHERE e.id IN (%s)", $idList);
                     $conn->execute($query);
                 }
             }
         }
         $conn->commit();
         return true;
     } catch (DaoException $e) {
         $conn->rollback();
         throw new DaoException($e->getMessage(), 0, $e);
     }
 }