/** * Removes related leave request leave instances. * * @param \Opit\OpitHrm\LeaveBundle\Entity\LeaveRequest $leaveRequest * @param ArrayCollection $children */ public function removeChildNodes(LeaveRequest $leaveRequest, $children) { foreach ($children as $child) { if (false === $leaveRequest->getLeaves()->contains($child)) { $child->setLeaveRequest(); $this->entityManager->remove($child); } } }
/** * Method to check if a leave request can be deleted * * @param \Symfony\Component\Security\Core\User\UserInterface $user * @param \Opit\OpitHrm\LeaveBundle\Entity\LeaveRequest $leaveRequest * @param type $isAdmin * @param type $isGeneralManager * @return type */ protected function isLRDeleteable(UserInterface $user, $leaveRequest, $isAdmin, $isGeneralManager) { // If user is gm of lr or user is admin always allow delete if ($isGeneralManager || $isAdmin) { return VoterInterface::ACCESS_GRANTED; } elseif ($user->getEmployee() === $leaveRequest->getEmployee()) { // If lr is child of mlr do not allow to deletion if ($leaveRequest->getLeaveRequestGroup()) { return VoterInterface::ACCESS_DENIED; } else { $leaves = $leaveRequest->getLeaves(); $numberOfPastLeaves = 0; foreach ($leaves as $leave) { if ($leave->getStartDate()->format('Y-m-d') < date('Y-m-d')) { $numberOfPastLeaves++; } } if (0 === $numberOfPastLeaves) { return VoterInterface::ACCESS_GRANTED; } } } return VoterInterface::ACCESS_DENIED; }
/** * Check if leave category can be selected using the left to availed days, * add error message to leave category if days left to avail were exceeded. * * @param \Opit\OpitHrm\UserBundle\Entity\Employee $employee * @param \Opit\OpitHrm\LeaveBundle\Entity\LeaveRequest $leaveRequest * @param \Opit\OpitHrm\LeaveBundle\Model\LeaveRequestService $leaveRequestService * @param \Doctrine\ORM\EntityManagerInterface $entityManager * @param type $form */ protected function validateLeaveDatesCategory(LeaveRequest $leaveRequest, LeaveRequestService $leaveRequestService, EntityManagerInterface $entityManager, $form) { $leaveCalculationService = $this->get('opit_opithrm_leave.leave_calculation_service'); $employee = $leaveRequest->getEmployee(); // Leave entitlements of an employee. $leaveEntitlement = $leaveCalculationService->leaveDaysCalculationByEmployee($employee); // Availed leave days of an employee. $employeeAvailedLeaveDays = $entityManager->getRepository('OpitOpitHrmLeaveBundle:LeaveRequest')->totalCountedLeaveDays($employee->getId()); // Left to avail days of an employee. $leftToAvail = $leaveEntitlement - $employeeAvailedLeaveDays; $countLeaveDays = 0; $leaves = $leaveRequest->getLeaves(); $message = 'Entitlement exceeded - kindly change category'; // Loop through all leaves employee has posted. foreach ($leaves as $index => $leave) { if (LeaveCategory::UNPAID !== $leave->getCategory()->getName()) { $countLeaveDays += $leaveRequestService->countLeaveDays($leave->getStartDate(), $leave->getEndDate()); // Check if count of leave days are more than days left to avail. if ($countLeaveDays > $leftToAvail) { // If there are days left to avail if ($leftToAvail > 0) { // Add error to leave category $form->get('leaves')->get($index)->get('category')->addError(new FormError($message . ' or dates.')); $leftToAvail = 0; } // Add error to leave category $form->get('leaves')->get($index)->get('category')->addError(new FormError($message . '.')); } } } }