/** * * @param \Opit\OpitHrm\LeaveBundle\Entity\LeaveRequest $resource * @param boolean $toGeneralManager * @param \Opit\OpitHrm\StatusBundle\Entity\Status $status */ public function addNewLeaveNotification(LeaveRequest $resource, $toGeneralManager, Status $status) { // get last status name from resource $resourceStatus = strtolower($status->getName()); $message = $resource->getLeaveRequestId(); if (strpos('approved', $resourceStatus) !== false || strpos('rejected', $resourceStatus) !== false) { $message .= ' has been ' . $resourceStatus; $message = ucfirst($message); } else { $message .= ' changed to ' . $resourceStatus; } $message .= ' for ' . $resource->getEmployee()->getEmployeeName() . '.'; $receiver = false === $toGeneralManager ? $this->entityManager->getRepository('OpitOpitHrmUserBundle:User')->findOneByEmployee($resource->getEmployee()) : $resource->getGeneralManager(); $notification = new LRNotification(); $notification->setLeaveRequest($resource); $notification->setMessage($message); $notification->setReceiver($receiver); $notification->setDateTime(new \DateTime('now')); $this->setNotificationStatus($notification); $this->entityManager->persist($notification); // Send notifications to additional recipients if status is set to approved if ($status->getId() === Status::APPROVED) { if ($teamManager = $resource->getTeamManager()) { $notificationsTM = clone $notification; $notificationsTM->setReceiver($teamManager); $this->entityManager->persist($notificationsTM); } $ccRecipients = $this->entityManager->getRepository('OpitOpitHrmUserBundle:Employee')->findNotificationRecipients($receiver); $notifications = array(); foreach ($ccRecipients as $i => $employee) { $notifications[$i] = clone $notification; $notifications[$i]->setReceiver($employee->getUser()); $this->entityManager->persist($notifications[$i]); } } $this->entityManager->flush(); }
/** * Method to check if the status of a leave request can be changed * * @param \Symfony\Component\Security\Core\User\UserInterface $user * @param \Opit\OpitHrm\LeaveBundle\Entity\LeaveRequest $leaveRequest * @param type $isAdmin * @param type $isGeneralManager * @param type $leaveRequestStatusId * @return type */ protected function isLRStatusChangeable(UserInterface $user, $leaveRequest, $isAdmin, $isGeneralManager, $leaveRequestStatusId) { if (null === $leaveRequest->getId()) { return VoterInterface::ACCESS_DENIED; } if (in_array($leaveRequestStatusId, array(Status::APPROVED, Status::REJECTED))) { return VoterInterface::ACCESS_DENIED; } // If user is admin and status of lr is not approved allow status change if ($isAdmin) { return VoterInterface::ACCESS_GRANTED; } elseif ($isGeneralManager) { // Check if general manager is owner of leave request if ($user->getEmployee() === $leaveRequest->getEmployee()) { return VoterInterface::ACCESS_GRANTED; } else { // If user is gm only allow status change when lr status is for approval if (Status::FOR_APPROVAL === $leaveRequestStatusId) { return VoterInterface::ACCESS_GRANTED; } } } elseif ($user->getEmployee() === $leaveRequest->getEmployee()) { // If user is assigned employee and status is created or revice allow status change if (in_array($leaveRequestStatusId, array(Status::CREATED, Status::REVISE))) { 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 . '.')); } } } }