src/Controller/AppraisalController.php line 1123

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Appraisal;
  4. use App\Entity\AppraisalForm;
  5. use App\Entity\AppraisalComment;
  6. use App\Entity\AppraisalNote;
  7. use App\Entity\AppraisalQuestionnaire;
  8. use App\Entity\AppraisalCategory;
  9. use App\Entity\User;
  10. use App\Entity\Office;
  11. use App\Entity\Department;
  12. use App\Form\AppraisalType;
  13. use App\Form\AppraisalNoteType;
  14. use App\Form\AppraisalRequestType;
  15. use App\Form\AppraisalSettingsType;
  16. use App\Form\AppraisalCategoryType;
  17. use App\Repository\AppraisalCategoryRepository;
  18. use App\Repository\AppraisalCommentRepository;
  19. use App\Repository\AppraisalFormRepository;
  20. use App\Repository\AppraisalNoteRepository;
  21. use App\Repository\AppraisalRepository;
  22. use App\Service\AppraisalService;
  23. use App\Service\GoogleDriveService;
  24. use App\Service\MailgunService;
  25. use App\Service\LogService;
  26. use App\Service\UserService;
  27. use App\Service\ObjectiveService;
  28. use App\Service\NotificationService;
  29. use App\Service\ICSService;
  30. use App\Service\LeaveService;
  31. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  32. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  33. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  34. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  35. use Symfony\Component\Routing\Annotation\Route;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use Symfony\Component\HttpFoundation\JsonResponse;
  38. use Symfony\Component\HttpFoundation\RedirectResponse;
  39. use Symfony\Component\HttpFoundation\Response;
  40. use Symfony\Component\Serializer\SerializerInterface;
  41. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  42. use Symfony\Contracts\Translation\TranslatorInterface;
  43. use Symfony\Component\Security\Core\Security;
  44. class AppraisalController extends AbstractController
  45. {
  46.     #[Route(path'/settings/appraisal'name'appraisal_settings')]
  47.     public function appraisalSettings(): \Symfony\Component\HttpFoundation\Response
  48.     {
  49.         $user $this->getUser();
  50.         $company $user->assignedCompany();
  51.         $formSettings $this->createForm(AppraisalSettingsType::class);
  52.        
  53.         return $this->render('private/appraisal/edit-appraisal-v2.html.twig', [
  54.             'formSettings' => $formSettings->createView(),
  55.             'company' => $company
  56.             //'formAppraisal' => $formAppraisal->createView()
  57.         ]);
  58.     }
  59.     #[Route(path'/ajax/appraisal-settings/update'name'ajax_update_appraisal_settings')]
  60.     public function ajaxUpdateAppraisalSettings(Request $requestLogService $logAppraisalService $appraisalService): JsonResponse
  61.     {
  62.         $user $this->getUser();
  63.         $result['status'] = 'OK';
  64.         $form $this->createForm(AppraisalSettingsType::class);
  65.         $form->handleRequest($request);
  66.         if ($form->isSubmitted() && !$form->isValid()) {
  67.             $result['status'] = 'ERROR';
  68.             foreach ($form->getErrors(true) as $key => $error) {
  69.                 $result['message'][$key] = $error->getMessage();
  70.             }
  71.             return new JsonResponse($result);
  72.         }
  73.         if ($form->isSubmitted() && $form->isValid()) {
  74.             //$result['status']
  75.             $company $user->assignedCompany();
  76.             $attributes $company->getAttributes();
  77.             $oldData $attributes;
  78.             $entityManager $this->getDoctrine()->getManager();
  79.             $currentAttributes $company->getAttributes();
  80.             $currentUserSubmitAt = isset($currentAttributes['appraisalUserSubmitAt']) ? new \DateTime($currentAttributes['appraisalUserSubmitAt']['date'].' '.$currentAttributes['appraisalUserSubmitAt']['timezone']) : null;
  81.             $currentManagerSubmitAt = isset($currentAttributes['appraisalManagerSubmitAt']) ? new \DateTime($currentAttributes['appraisalManagerSubmitAt']['date'].' '.$currentAttributes['appraisalManagerSubmitAt']['timezone']) : null;
  82.             
  83.             $attributes['appraisalStartAt'] = $form->get('appraisalStartAt')->getData();
  84.             $attributes['appraisalEndAt'] = $form->get('appraisalEndAt')->getData();
  85.             $attributes['appraisalUserSubmitAt'] = $form->get('appraisalUserSubmitAt')->getData();
  86.             $attributes['appraisalManagerSubmitAt'] = $form->get('appraisalManagerSubmitAt')->getData();
  87.             //$attributes['appraisalPulseCheck'] = $form->get('appraisalPulseCheck')->getData();
  88.             $company->setAttributes($attributes);
  89.             $startDate = isset($attributes['appraisalStartAt']) ? new \DateTime($attributes['appraisalStartAt']->format('Y-m-d')) : null;
  90.             $endDate = isset($attributes['appraisalEndAt']) ? new \DateTime($attributes['appraisalEndAt']->format('Y-m-d')) : null;
  91.             if($currentUserSubmitAt != $attributes['appraisalUserSubmitAt']) {
  92.                 $appraisals $this->getDoctrine()->getRepository(Appraisal::class)->findBy(['probation' => false'adHoc' => false'submitAt' => $currentUserSubmitAt'submittedAt' => null]);
  93.                 foreach ($appraisals as $appraisal) {
  94.                     $appraisal->setSubmitAt($attributes['appraisalUserSubmitAt']);
  95.                 };
  96.             };
  97.             $employees =  $this->getDoctrine()->getRepository(User::class)->findAllEligibleForAppraisal($company$startDate);
  98.             foreach ($employees as $employee) {
  99.                 $employee->getPersonalInfo()->setJobExpiryDate($form->get('appraisalEndAt')->getData());
  100.             };
  101.             $entityManager->flush();
  102.             $entityManager->refresh($company);
  103.             $appraisalService->syncAppraisalStatus();
  104.             $log->save($user, [
  105.                 'owner' => $user,
  106.                 'message' => 'log.appraisal.settings',
  107.                 'old_data' => $oldData,
  108.                 'new_data' => $attributes
  109.             ]);
  110.         };
  111.         return new JsonResponse($result);
  112.     }
  113.     #[Route(path'/ajax/appraisal/crud'name'ajax_crud_appraisal')]
  114.     public function ajaxCRUDAppraisal(Request $requestTranslatorInterface $translatorAppraisalService $appraisalServiceLogService $logAppraisalFormRepository $appraisalFormRepository): JsonResponse
  115.     {
  116.         $id $request->get("id");
  117.         $userId $request->get("userId");
  118.         //$officeId = $request->get("officeId");
  119.         $crud $request->get("crud");
  120.         $user $this->getUser();
  121.         $result['status'] = 'OK';
  122.         if (!$id) {
  123.             $appraisal = new AppraisalForm();
  124.         } else {
  125.             $appraisal =  $this->getDoctrine()->getRepository(AppraisalForm::class)->find($id);
  126.             $oldData = clone $appraisal;
  127.         };
  128.         $form $this->createForm(AppraisalType::class, $appraisal);
  129.         $form->handleRequest($request);
  130.         if ($form->isSubmitted() && !$form->isValid()) {
  131.             $result['status'] = 'ERROR';
  132.             foreach ($form->getErrors(true) as $key => $error) {
  133.                 $result['message'][$key] = $error->getMessage();
  134.             }
  135.             return new JsonResponse($result);
  136.         }
  137.         if ($form->isSubmitted() && $form->isValid()) {
  138.             $entityManager $this->getDoctrine()->getManager();
  139.             if ($id === null) {
  140.                 $appraisal->setCompany($user->assignedCompany());
  141.                 $appraisal->setCreatedBy($user);
  142.                 $appraisal->setCreatedAt(new \DateTimeImmutable());
  143.                 $entityManager->persist($appraisal);
  144.             }
  145.             if ($id) {
  146.                 $appraisal->setUpdatedBy($user);
  147.                 $appraisal->setUpdatedAt(new \DateTimeImmutable());
  148.             }
  149.             $entityManager->flush();
  150.             $entityManager->refresh($appraisal);
  151.             $log->save($user, [
  152.                 'owner' => $user,
  153.                 'message' => $id === null 'log.appraisal.questionnaire.add' 'log.appraisal.questionnaire.update',
  154.                 'old_data' => $id === null null $oldData,
  155.                 'new_data' =>  $appraisal
  156.             ]);
  157.             $result['id'] = $appraisal->getId();
  158.             $result['content'] = $this->renderView('private/appraisal/components/appraisal-row.html.twig', [
  159.                 'appraisal' => $appraisal,
  160.                 'waitForPage' => false
  161.             ]);
  162.             $result['update'] = $id != null;
  163.             // if ($id != null && $appraisal->getIsHidden() != $oldData->getIsHidden()) $result['hide'] = true;
  164.             return  new JsonResponse($result);
  165.         }
  166.         if ($crud === 'delete' && $appraisal !== null) {
  167.             if ($result['status'] == 'OK') {
  168.                 $entityManager $this->getDoctrine()->getManager();
  169.                 $entityManager->remove($appraisal);
  170.                 $entityManager->flush();
  171.                 $log->save($user, [
  172.                     'owner' => $user,
  173.                     'message' => 'log.appraisal.questionnaire.delete',
  174.                     'old_data' => $appraisal,
  175.                     'new_data' => null,
  176.                 ]);
  177.             }
  178.             return  new JsonResponse($result);
  179.         }
  180.         if ($crud === 'create' || $crud === 'update') {
  181.             $result['form'] = $this->renderView('private/appraisal/components/form-appraisal.html.twig', [
  182.                 'id' => $id,
  183.                 'form' => $form->createView(),
  184.             ]);
  185.             return  new JsonResponse($result);
  186.         };
  187.         $order $request->get('order');
  188.         $orderBy $request->get('orderBy');
  189.         $keyword $request->get('keyword');
  190.         $type $request->get('type');
  191.         $department $request->get('department');
  192.         $page  intval($request->query->get('page'));
  193.         $limit intval($request->query->get('limit'));
  194.         if ($limit === 0$limit 20;
  195.         $company $user->assignedCompany();
  196.         $appraisals $appraisalFormRepository->findByPage($page$limit$order$orderBy$company$keyword$type$department);
  197.         $totalCount =  $appraisalFormRepository->countByPage($company$keyword$type$department);
  198.         $body $this->renderView('private/appraisal/components/appraisal-list.html.twig', [
  199.             'appraisals' => $appraisals
  200.         ]);
  201.         $result['content'] = [
  202.             'html' => $body,
  203.             'total' => $totalCount
  204.         ];
  205.         return  new JsonResponse($result);
  206.     }
  207.     #[Route(path'/ajax/list/appraisal'name'ajax_list_appraisal')]
  208.     public function ajaxListAppraisal(TranslatorInterface $translatorAppraisalService $appraisalServiceLogService $log): JsonResponse
  209.     {
  210.     }
  211.     #[Route(path'/ajax/appraisal-schedule/crud'name'ajax_crud_appraisal_schedule')]
  212.     public function ajaxCRUDAppraisalSchedule(Request $requestAppraisalService $appraisalServiceObjectiveService $objectiveServiceLogService $log): JsonResponse
  213.     {
  214.         $user $this->getUser();
  215.         if ($_SERVER['APP_ENV'] != "prod" && $user->getAppraisalStatus() == null && $user->getObjectiveStatus() == null) {
  216.             $appraisalService->syncAppraisalStatus();
  217.             $objectiveService->syncObjectiveStatus();
  218.             $result['sync'] = 'OK';
  219.         };
  220.         $isManager = !$this->isGranted('ROLE_HR') && $user->getIsManager();
  221.         $page  intval($request->query->get('page'));
  222.         $limit intval($request->query->get('limit'));
  223.         if ($limit === 0$limit 20;
  224.         $order $request->get('order');
  225.         $orderBy $request->get('orderBy');
  226.         $keyword $request->get('keyword');
  227.         $manager $isManager $user->getId() : $request->get('manager');
  228.         $subManager $request->get('subManager');
  229.         $office $request->get('office');
  230.         $employment $request->get('employment');
  231.         $department $request->get('department');
  232.         $appraisal $request->get('appraisal');
  233.         $objective $request->get('objective');
  234.         $waitForPage $request->query->get('waitForPage');
  235.         $waitForPage === null $result['waitForPage'] = 'false' $result['waitForPage'] = $waitForPage;
  236.         $result['status'] = 'OK';
  237.         // $appraisals = $this->getDoctrine()->getRepository(Appraisal::class)->findByPage($page, $limit, $order, $orderBy, $keyword);
  238.         // $totalCount =  $this->getDoctrine()->getRepository(Appraisal::class)->countByPage($keyword);
  239.         $company $user->assignedCompany();
  240.         $users $this->getDoctrine()->getRepository(User::class)->findByAppraisalPage($page$limit$order$orderBy$company$keyword$manager$subManager$office$employment$department$appraisal$objective);
  241.         $totalCount =  $this->getDoctrine()->getRepository(User::class)->countByAppraisalPage($company$keyword$manager$subManager$office,$employment$department$appraisal$objective);
  242.         $body $this->renderView('private/appraisal/components/appraisal-schedule-list.html.twig', [
  243.             // 'appraisals' => $appraisals
  244.             'users' => $users,
  245.             'testAccounts' => explode(','str_replace(array("\r""\n"" "), '',$_SERVER['TEST_ACCOUNTS']))
  246.         ]);
  247.         $result['content'] = [
  248.             'html' => $body,
  249.             'total' => $totalCount
  250.         ];
  251.         return new JsonResponse($result);
  252.     }
  253.     #[Route(path'/ajax/appraisal-category/crud'name'ajax_crud_appraisal_category')]
  254.     public function ajaxCRUDAppraisalCategory(Request $requestLogService $logAppraisalCategoryRepository $appraisalCategoryRepository): JsonResponse
  255.     {
  256.         $id $request->get("id");
  257.         $userId $request->get("userId");
  258.         //$officeId = $request->get("officeId");
  259.         $crud $request->get("crud");
  260.         $user $this->getUser();
  261.         $result['status'] = 'OK';
  262.         if (!$id) {
  263.             $category = new AppraisalCategory();
  264.         } else {
  265.             $category =  $this->getDoctrine()->getRepository(AppraisalCategory::class)->find($id);
  266.             $oldData = clone $category;
  267.         };
  268.         $form $this->createForm(AppraisalCategoryType::class, $category);
  269.         $form->handleRequest($request);
  270.         if ($form->isSubmitted() && !$form->isValid()) {
  271.             $result['status'] = 'ERROR';
  272.             foreach ($form->getErrors(true) as $key => $error) {
  273.                 $result['message'][$key] = $error->getMessage();
  274.             }
  275.             return new JsonResponse($result);
  276.         }
  277.         if ($form->isSubmitted() && $form->isValid()) {
  278.             $entityManager $this->getDoctrine()->getManager();
  279.             if ($id === null) {
  280.                 $category->setCompany($user->assignedCompany());
  281.                 $questionnaires $form->get('questionnaire')->getData();
  282.                 foreach($questionnaires as $question){
  283.                     $question->setCompany($user->assignedCompany());
  284.                     $question->setType($category->getType());
  285.                     $question->setDepartment($category->getDepartment());
  286.                     $question->setCreatedAt(new \DateTime());
  287.                     $question->setCreatedBy($user);
  288.                 }
  289.                 $category->setCreatedBy($user);
  290.                 $category->setCreatedAt(new \DateTimeImmutable());
  291.                 $entityManager->persist($category);
  292.             }
  293.             if ($id) {
  294.                 $questionnaires $form->get('questionnaire')->getData();
  295.                 foreach($questionnaires as $question){
  296.                     if(!$question->getCompany()) $question->setCompany($user->assignedCompany());
  297.                     if(!$question->getType()) $question->setType($category->getType());
  298.                     if(!$question->getDepartment()) $question->setDepartment($category->getDepartment());
  299.                     $question->setCreatedAt(new \DateTime());
  300.                     $question->setCreatedBy($user);
  301.                 }
  302.                 $category->setUpdatedBy($user);
  303.                 $category->setUpdatedAt(new \DateTimeImmutable());
  304.             }
  305.             $entityManager->flush();
  306.             $entityManager->refresh($category);
  307.             $log->save($user, [
  308.                 'owner' => $user,
  309.                 'message' => $id === null 'log.appraisal.category.add' 'log.appraisal.category.update',
  310.                 'old_data' => $id === null null $oldData,
  311.                 'new_data' =>  $category
  312.             ]);
  313.             $result['id'] = $category->getId();
  314.             $result['content'] = $this->renderView('private/appraisal/components/appraisal-category-row.html.twig', [
  315.                 'category' => $category,
  316.                 'waitForPage' => false
  317.             ]);
  318.             $result['update'] = $id != null;
  319.             // if ($id != null && $category->getIsHidden() != $oldData->getIsHidden()) $result['hide'] = true;
  320.             return  new JsonResponse($result);
  321.         }
  322.         if ($crud === 'delete' && $category !== null) {
  323.             if ($result['status'] == 'OK') {
  324.                 $entityManager $this->getDoctrine()->getManager();
  325.                 $entityManager->remove($category);
  326.                 $entityManager->flush();
  327.                 $log->save($user, [
  328.                     'owner' => $user,
  329.                     'message' => 'log.appraisal.questionnaire.delete',
  330.                     'old_data' => $category,
  331.                     'new_data' => null,
  332.                 ]);
  333.             }
  334.             return  new JsonResponse($result);
  335.         }
  336.         if ($crud === 'create' || $crud === 'update') {
  337.             $result['form'] = $this->renderView('private/appraisal/components/form-appraisal-category.html.twig', [
  338.                 'id' => $id,
  339.                 'form' => $form->createView(),
  340.             ]);
  341.             return  new JsonResponse($result);
  342.         };
  343.         $order $request->get('order');
  344.         $orderBy $request->get('orderBy');
  345.         $keyword $request->get('keyword');
  346.         $type $request->get('type');
  347.         $department $request->get('department');
  348.         $page  intval($request->query->get('page'));
  349.         $limit intval($request->query->get('limit'));
  350.         if ($limit === 0$limit 20;;
  351.         $company $user->assignedCompany();
  352.         $categories $appraisalCategoryRepository->findByPage($page$limit$order$orderBy$company$keyword$type$department);
  353.         $totalCount =  $appraisalCategoryRepository->countByPage($company$keyword$type$department);
  354.         $body $this->renderView('private/appraisal/components/appraisal-category-list.html.twig', [
  355.             'categories' => $categories
  356.         ]);
  357.         $result['content'] = [
  358.             'html' => $body,
  359.             'total' => $totalCount
  360.         ];
  361.         return  new JsonResponse($result);
  362.     }
  363.     #[Route(path'/ajax/appraisal-request'name'ajax_appraisal_request')]
  364.     public function ajaxAppraisalRequest(Request $requestTranslatorInterface $translatorAppraisalService $appraisalServiceLogService $log): JsonResponse
  365.     {
  366.         $user $this->getUser();
  367.         $appraisal = new Appraisal();
  368.         $form $this->createForm(AppraisalRequestType::class, $appraisal);
  369.         $form->handleRequest($request);
  370.         $result['status'] = 'OK';
  371.         $editedUser =  $this->getDoctrine()->getRepository(User::class)->find($request->get("userId"));
  372.         if ($editedUser == null) {
  373.             $result['status'] = 'ERROR';
  374.             $result['message'] = $translator->trans('messages.request.missing') . ': UID';
  375.         }
  376.         if ($form->isSubmitted() && !$form->isValid() && $result['status'] == 'OK') {
  377.             $result['status'] = 'ERROR';
  378.             foreach ($form->getErrors(true) as $key => $error) {
  379.                 $result['message'][$key] = $error->getMessage();
  380.             }
  381.         };
  382.         if ($result['status'] == 'OK') {
  383.             $result['content'] =  $appraisalService->initiateAppraisal($request$user$editedUser$form->get('title')->getData(), $form->get('submitAt')->getData(), $form->get('reviewAt')->getData(), $request->request->get('addonform-text'), $request->request->get('addonform-score'), $form->get('adHoc')->getData());
  384.             /*$log->save($user, [
  385.                 'owner' => $editedUser,
  386.                 'message' => 'log.appraisal.request',
  387.                 'old_data' => null,
  388.                 'new_data' => $appraisal
  389.             ]);*/
  390.             $result['ordering'] = [
  391.                 'key' => 'submit',
  392.                 'sort' => 'desc'
  393.             ];
  394.         };
  395.         return new JsonResponse($result);
  396.     }
  397.     #[Route(path'/ajax/edit-appraisal-request'name'ajax_edit_appraisal_request'methods'POST')]
  398.     function ajaxEditAppraisalRequest(Request $requestAppraisalService $appraisalServiceTranslatorInterface $translator): \Symfony\Component\HttpFoundation\Response
  399.     {
  400.         $user $this->getUser();
  401.         $appraisal $this->getDoctrine()->getRepository(Appraisal::class)->find($request->get("id"));
  402.         $formAppraisal $this->createForm(AppraisalRequestType::class, $appraisal);
  403.         $addOnQuestions $this->getDoctrine()->getRepository(AppraisalForm::class)->findByAppraisal($appraisal);
  404.         $defaultQuestions $this->getDoctrine()->getRepository(AppraisalQuestionnaire::class)->findByUser($appraisal->getUser());
  405.         return $this->render('private/appraisal/components/form-edit-appraisal-request.html.twig', array(
  406.             'userData' => $user,
  407.             'editedUser' => $appraisal->getUser(),
  408.             'dataId' => $appraisal->getId(),
  409.             'formAppraisal' => $formAppraisal->createView(),
  410.             'addOnQuestions' => $addOnQuestions $addOnQuestions null,
  411.             'defaultQuestions' => $defaultQuestions $defaultQuestions null
  412.         ));
  413.     }
  414.     #[Route(path'/ajax/update-appraisal-request'name'ajax_update_appraisal_request')]
  415.     public function ajaxUpdateRequestAppraisal(Request $requestTranslatorInterface $translatorAppraisalService $appraisalServiceLogService $logMailgunService $mailgunServiceNotificationService $notificationICSService $icsSerializerInterface $serializer): JsonResponse
  416.     {
  417.         $user $this->getUser();
  418.         $appraisal $this->getDoctrine()->getRepository(Appraisal::class)->find($request->get("dataId"));
  419.         $oldData = clone $appraisal;
  420.         $result['status'] = 'OK';
  421.         $loggedIn $request true false;
  422.         if ($appraisal != null) {
  423.             /*
  424.             if ($user->isSubordinate($appraisal->getUser()->getId())) {
  425.                 $result['status'] = 'ERROR';
  426.                 $result['message'] = $translator->trans('messages.security.forbidden');
  427.             };
  428.             */
  429.             $form $this->createForm(AppraisalRequestType::class, $appraisal);
  430.             $form->handleRequest($request);
  431.             if ($form->isSubmitted() && !$form->isValid() && $result['status'] == 'OK') {
  432.                 $result['status'] = 'ERROR';
  433.                 foreach ($form->getErrors(true) as $key => $error) {
  434.                     $result['message'][$key] = $error->getMessage();
  435.                 }
  436.             };
  437.             if ($result['status'] == 'OK') {
  438.                 $entityManager $this->getDoctrine()->getManager();
  439.                 if ($appraisal->getReviewAt() != $oldData->getReviewAt()) {
  440.                     $appraisal->setReschedule($appraisal->getReschedule() + 1);
  441.                     if ($appraisal->getAdHoc()) $appraisal->setSubmitAt($appraisal->getReviewAt());
  442.                 };
  443.                 $entityManager->flush();
  444.                 $entityManager->refresh($appraisal);
  445.                 //$addOnQuestions = $this->getDoctrine()->getRepository(AppraisalForm::class)->findByAppraisal($appraisal);
  446.                 $addOnFormTexts $request->request->get('addonform-text');
  447.                 if ($addOnFormTexts != null) {
  448.                     $addOnFormId $request->request->get('addonform-id');
  449.                     $addOnFormDelete $request->request->get('addonform-delete');
  450.                     $addOnFormScores $request->request->get('addonform-score');
  451.                     $n 1;
  452.                     foreach ($addOnFormTexts as $addOnFormText) {
  453.                         if ($addOnFormId[$n]) {
  454.                             $appraisalForm $this->getDoctrine()->getRepository(AppraisalForm::class)->findById($addOnFormId[$n]);
  455.                             if ($addOnFormDelete[$n] == 'true') {
  456.                                 $entityManager->remove($appraisalForm);
  457.                                 $entityManager->flush();
  458.                                 $n++;
  459.                                 continue;
  460.                             }
  461.                         } else {
  462.                             $appraisalForm = new AppraisalForm();
  463.                             if ($addOnFormText == null) {
  464.                                 $n++;
  465.                                 continue;
  466.                             }
  467.                         }
  468.                         $appraisalForm->setDescription($addOnFormText);
  469.                         if ($addOnFormScores[$n] == 'true') {
  470.                             $appraisalForm->setUseScore(true);
  471.                         } else {
  472.                             $appraisalForm->setUseScore(false);
  473.                         };
  474.                         $appraisalForm->setCreatedBy($user);
  475.                         $appraisalForm->setCreatedAt(new \DateTime());
  476.                         $appraisalForm->setCompany($user->assignedCompany());
  477.                         if ($addOnFormId[$n] == null) {
  478.                             $entityManager->persist($appraisalForm);
  479.                             $appraisalForm->setAppraisal($appraisal);
  480.                             $appraisal->setSubmittedAt(null);
  481.                         };
  482.                         $entityManager->flush();
  483.                         $n++;
  484.                     };
  485.                 }
  486.                 $attendee = [
  487.                     [
  488.                         'name' => $user->getPersonalInfo()->getFullName(),
  489.                         'email' => $user->getEmail(),
  490.                         'role' => $appraisal->getUser()->getManager() == $user 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  491.                     ],
  492.                     [
  493.                         'name' => $appraisal->getUser()->getPersonalInfo()->getFullName(),
  494.                         'email' => $appraisal->getUser()->getEmail(),
  495.                         'role' => 'REQ-PARTICIPANT'
  496.                     ],
  497.                 ];
  498.                 foreach ($appraisal->getUser()->getAllManager() as $manager) {
  499.                     if ($manager != $user) {
  500.                         array_push($attendee,   [
  501.                             'name' => $manager->getPersonalInfo()->getFullName(),
  502.                             'email' => $manager->getEmail(),
  503.                             'role' => $appraisal->getUser()->getManager() == $manager 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  504.                         ]);
  505.                     }
  506.                 }
  507.                 // if ($_SERVER['NOTIF_SUPER_ADMIN'] == 'true') {
  508.                 //     foreach ($user->assignedCompany()->superAdmins($user) as $superAdmin) {
  509.                 //         if ($superAdmin != $user) {
  510.                 //             array_push($attendee,   [
  511.                 //                 'name' => $superAdmin->getPersonalInfo()->getFullName(),
  512.                 //                 'email' => $superAdmin->getEmail(),
  513.                 //                 'role' => 'OPT-PARTICIPANT'
  514.                 //             ]);
  515.                 //         }
  516.                 //     }
  517.                 // };
  518.                 if ($appraisal->getReviewAt() != $oldData->getReviewAt() && in_array($appraisal->currentStep(), [34])) {
  519.                     $hrSetSchedule $oldData->getReviewAt() == null && $appraisal->currentStep() == && $appraisal->getUser()->inProbation() == false true false;
  520.                     if ($hrSetSchedule) {
  521.                         $emailTemplate 'email/user-notification/appraisal/hr-invitation-default.html.twig';
  522.                         $titleTemplate 'email.appraisal.invitation.default.';
  523.                     } else {
  524.                         if ($appraisal->getProbation()) {
  525.                             if($oldData->getReschedule() == && $oldData->getReviewAt() == null){
  526.                                 $emailTemplate 'email/user-notification/appraisal/invitation-probation.html.twig';
  527.                                 $titleTemplate 'email.appraisal.invitation.probation.';
  528.                             } else {
  529.                                 $emailTemplate 'email/user-notification/appraisal/reschedule-probation.html.twig';
  530.                                 $titleTemplate 'email.appraisal.rescheduled.probation.';
  531.                             }
  532.                         } elseif ($oldData->getReschedule() == 0) {
  533.                             $emailTemplate =  'email/user-notification/appraisal/hr-invitation-default.html.twig';
  534.                             $titleTemplate 'email.appraisal.invitation.default.';
  535.                         } else {
  536.                             $emailTemplate =  'email/user-notification/appraisal/reschedule-default.html.twig';
  537.                             $titleTemplate 'email.appraisal.rescheduled.default.';
  538.                         };
  539.                     }
  540.                     $endTime = new \DateTime($appraisal->getReviewAt()->format('Ymd H:i:s'));
  541.                     $endTime->modify('+ 30 minutes');
  542.                     $ICSTitle $appraisal->getUser()->inProbation() ? 'Review (Probation)' 'Review';
  543.                     $icsData =  $ics->generateEventFile([
  544.                         'uid' => $appraisal->getId() . $appraisal->getUser()->getId() . ':' $appraisal->getUser()->getEmail(),
  545.                         'title' => $ICSTitle ': ' $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  546.                         'description' => $translator->trans('ics.appraisal.description', ['%uid%' => $appraisal->getUser()->getId(), '%user%' => strtolower($appraisal->getUser()->getPersonalInfo()->getFirstName()), '%link%' => $this->generateUrl('user_profile', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getUser()->getEmail() . '?g=appraisal&open=appraisal-' $appraisal->getId()]),
  547.                         'startDateTime' => $appraisal->getReviewAt()->format('Ymd') . 'T' $appraisal->getReviewAt()->format('His'),
  548.                         'endDateTime' =>  $appraisal->getReviewAt()->format('Ymd') . 'T' $endTime->format('His'),
  549.                         'location' =>  $this->generateUrl('submit_appraisal', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getId(),
  550.                         'organizer' => [
  551.                             'name' => $this->getParameter('systemEmailName'),
  552.                             'email' => $_SERVER['APP_ENV'] == 'prod' $this->getParameter('systemEmailAddr') : 'devops@mediatropy.com'
  553.                         ],
  554.                         'attendee' => $attendee,
  555.                         'reschedule' => $appraisal->getReschedule(),
  556.                         'reminderTime' => '-15M',
  557.                         'reminderDescription' => $translator->trans('ics.appraisal.title') . ' ' $appraisal->getTitle(),
  558.                         'fileName' => 'Appraisal-' $appraisal->getIcsToken()
  559.                     ], true);
  560.                     $mailgunService->sendEmailWithAttachment([
  561.                         'subject' =>  $translator->trans($titleTemplate 'user', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%date%' => $appraisal->getReviewAt()->format('l, d M Y')]),
  562.                         'to' => $appraisal->getUser()->getEmail(),
  563.                         'template' => $emailTemplate,
  564.                         'params' => [
  565.                             'recipient' => $appraisal->getUser(),
  566.                             'appraisal' => $appraisal,
  567.                             'group' => 'user'
  568.                         ],
  569.                         'ical' => $icsData
  570.                     ], $loggedIn);
  571.                     // $notification->push($appraisal->getUser(), [
  572.                     //     'message' => 'notification.appraisal.rescheduled_alt.user',
  573.                     //     'entity' => 'Appraisal',
  574.                     //     'objectId' => $appraisal->getId(),
  575.                     //     'data' => $serializer->serialize([
  576.                     //         'title' => $appraisal->getTitle(),
  577.                     //         'name' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  578.                     //         'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  579.                     //         'time' => $appraisal->getReviewAt()->format('Ymd His'),
  580.                     //     ], 'json')
  581.                     // ], true);
  582.                     foreach ($appraisal->getUser()->getAllManager() as $manager) {
  583.                         // $notification->push($manager, [
  584.                         //     'message' => 'notification.appraisal.rescheduled_alt.manager',
  585.                         //     'entity' => 'Appraisal',
  586.                         //     'objectId' => $appraisal->getId(),
  587.                         //     'data' => $serializer->serialize([
  588.                         //         'title' => $appraisal->getTitle(),
  589.                         //         'name' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  590.                         //         'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  591.                         //         'time' => $appraisal->getReviewAt()->format('Ymd His'),
  592.                         //     ], 'json')
  593.                         // ], true);
  594.                         $mailgunService->sendEmailWithAttachment([
  595.                             'subject' => $translator->trans($titleTemplate 'manager', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFullName(), '%date%' => $appraisal->getReviewAt()->format('l, d M Y')]),
  596.                             'to' => $manager->getEmail(),
  597.                             'template' => $emailTemplate,
  598.                             'params' => [
  599.                                 'recipient' => $manager,
  600.                                 'appraisal' => $appraisal,
  601.                                 'group' => 'manager'
  602.                             ],
  603.                             'ical' => $icsData
  604.                         ], $loggedIn);
  605.                     }
  606.                     $emailGroup $this->getParameter('systemEmailGroup');
  607.                     foreach ($emailGroup as $groupName => $groupEmail) {
  608.                         if ($groupName == 'hr') { // only HR
  609.                             $mailgunService->sendEmailWithAttachment([
  610.                                 'subject' => $translator->trans($titleTemplate 'hr', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFullName(), '%date%' => $appraisal->getReviewAt()->format('l, d M Y')]),
  611.                                 'to' => $groupEmail,
  612.                                 'template' => $emailTemplate,
  613.                                 'params' => [
  614.                                     'recipient' => strtoupper($groupName) . ' Team',
  615.                                     'appraisal' => $appraisal,
  616.                                     'group' => $groupName
  617.                                 ],
  618.                                 'ical' => $icsData
  619.                             ], $loggedIn);
  620.                         };
  621.                     };
  622.                     /*if ($_SERVER['NOTIF_SUPER_ADMIN'] == 'true') {
  623.                         foreach ($user->assignedCompany()->superAdmins($user) as $superAdmin) {
  624.                             if ($superAdmin != $user && !in_array($superAdmin, $appraisal->getUser()->getAllManager())) {
  625.                                 $notification->push($superAdmin, [
  626.                                     'message' => 'notification.appraisal.rescheduled_alt.manager',
  627.                                     'entity' => 'Appraisal',
  628.                                     'objectId' => $appraisal->getId(),
  629.                                     'data' => $serializer->serialize([
  630.                                         'title' => $appraisal->getTitle(),
  631.                                         'name' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  632.                                         'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  633.                                         'time' => $appraisal->getSubmitAt()->format('Ymd His'),
  634.                                     ], 'json')
  635.                                 ], true);
  636.                                 $mailgunService->sendEmailWithAttachment([
  637.                                     //'subject' => $translator->trans('email.appraisal.created_cc_updated') . ' ' . $appraisal->getSubmitAt()->format('d F Y').' at '.$appraisal->getSubmitAt()->format('H:i'),
  638.                                     'subject' => $translator->trans('email.system.copy_super_admin') . ' ' . $subject,
  639.                                     'to' => $superAdmin->getEmail(),
  640.                                     'template' => 'email/user-notification/appraisal/request-cc.html.twig',
  641.                                     'params' => [
  642.                                         'sender' => $user,
  643.                                         'recipient' => $superAdmin,
  644.                                         'appraisal' => $appraisal,
  645.                                         'reschedule' => true
  646.                                     ],
  647.                                     //'attachments' => $this->generateUrl('download_ics_appraisal', array('token' => $appraisal->getIcsToken()))
  648.                                     'ical' => $icsData
  649.                                 ]);
  650.                             };
  651.                         }
  652.                     }*/
  653.                 } elseif ($appraisal->getSubmitAt() != $oldData->getSubmitAt() and $appraisal->currentStep() <= 1) {
  654.                     //TODO Add email or notification when user submit date changed?
  655.                 };
  656.                 /*
  657.                 if ($appraisal->getReviewAt() != $oldData->getReviewAt()) {
  658.                     $mailgunService->rescheduleEmail(new \DateTime($appraisal->getReviewAt()->format('Ymd H:i:s')), 'Appraisal', $appraisal->getId());
  659.                 };*/
  660.                 $log->save($user, [
  661.                     'owner' => $appraisal->getUser(),
  662.                     'message' => 'log.appraisal.request_update',
  663.                     'old_data' => $oldData,
  664.                     'new_data' => $appraisal
  665.                 ]);
  666.                 /*switch ($appraisal->currentStep($user)) {
  667.                     case 1:
  668.                         $action = $translator->trans('form.appraisal.pending.user');
  669.                         $attr = 'disabled="disabled"';
  670.                         break;
  671.                     case 2:
  672.                         $action = $translator->trans('form.appraisal.pending.manager');
  673.                         $attr = '';
  674.                         break;
  675.                     case 3:
  676.                         $action = $translator->trans('form.appraisal.pending.review');
  677.                         $attr = '';
  678.                         break;
  679.                     case 4:
  680.                         $action = $translator->trans('form.appraisal.ready');
  681.                         $attr = '';
  682.                         break;
  683.                     default:
  684.                         $action = $translator->trans('form.appraisal.done');
  685.                         $attr = '';
  686.                         break;
  687.                 }
  688.                 if ($appraisal->currentStep($user) == 1) {
  689.                     if ($appraisal->getSubmitAt()->format('Ymd') <= date('Ymd')) {
  690.                         $appraisalClass = 'b-danger';
  691.                     } else {
  692.                         $appraisalClass = 'b-warning';
  693.                     };
  694.                 } else {
  695.                     $appraisalClass = '';
  696.                 };*/
  697.                 $result['id'] = $appraisal->getId();
  698.                 if ($user != $appraisal->getUser()) {
  699.                     $result['content'] = $this->renderView('private/user/components/tab-appraisal-list-manager.html.twig', [
  700.                         'appraisal' => $appraisal
  701.                     ]);
  702.                 } else {
  703.                     $result['content'] = $this->renderView('private/user/components/tab-appraisal-list-user.html.twig', [
  704.                         'appraisal' => $appraisal
  705.                     ]);
  706.                 }
  707.                 $result['update'] = true;
  708.                 $result['ordering'] = [
  709.                     'key' => 'submit',
  710.                     'sort' => 'desc'
  711.                 ];
  712.             }
  713.             $appraisalService->syncAppraisalStatus($appraisal->getUser());
  714.         } else {
  715.             $result['status'] = 'ERROR';
  716.             $result['message'] = $translator->trans('messages.request.missing');
  717.         }
  718.         return new JsonResponse($result);
  719.     }
  720.     #[Route(path'/ajax/edit-appraisal-questionnaire'name'ajax_edit_appraisal_questionnaire'methods'POST')]
  721.     function ajaxEditAppraisalQuestionnaire(Request $requestTranslatorInterface $translator)
  722.     {
  723.         $user $this->getUser();
  724.         $editedUser $this->getDoctrine()->getRepository(User::class)->find($request->get("id"));
  725.         if ($editedUser == null) return false;
  726.         $questionnaires $this->getDoctrine()->getRepository(AppraisalQuestionnaire::class)->findByUser($editedUser);
  727.         return $this->render('private/appraisal/components/form-appraisal-questionnaire.html.twig', array(
  728.             'userData' => $user,
  729.             'editedUser' => $editedUser,
  730.             'questionnaires' => $questionnaires $questionnaires null
  731.         ));
  732.     }
  733.     #[Route(path'/ajax/update-appraisal-questionnaire'name'ajax_update_appraisal_questionnaire')]
  734.     public function ajaxUpdateAppraisalQuestionnaire(Request $requestTranslatorInterface $translatorAppraisalService $appraisalServiceSerializerInterface $serializer): JsonResponse
  735.     {
  736.         $user $this->getUser();
  737.         $editedUser $this->getDoctrine()->getRepository(User::class)->find($request->get("userId"));
  738.         $result['status'] = 'OK';
  739.         if ($editedUser == null) { // TODO: Make other error response like this
  740.             $result['status'] = 'ERROR';
  741.             $result['message'] = $translator->trans('messages.request.missing');
  742.             return new JsonResponse($result);
  743.         };
  744.         $result['content'] = $appraisalService->setDefaultQuestionnaire($request$user$editedUser);
  745.         return new JsonResponse($result);
  746.     }
  747.     #[Route(path'/ajax/appraisal/addon-questionnaire/crud'name'ajax_crud_appraisal_addon_questionnaire')]
  748.     public function ajaxCRUDAppraisalAddonQuestionnaire(Request $requestTranslatorInterface $translatorAppraisalService $appraisalServiceLogService $log): JsonResponse
  749.     {
  750.         $crud $request->get("crud");
  751.         $user $this->getUser();
  752.         $editedUser $this->getDoctrine()->getRepository(User::class)->find($request->get("userId"));
  753.         $result['status'] = 'OK';
  754.         $entityManager $this->getDoctrine()->getManager();
  755.         if ($editedUser == null) return false;
  756.         $questionnaires $this->getDoctrine()->getRepository(AppraisalQuestionnaire::class)->findByUser($editedUser);
  757.         if ($crud == 'update') {
  758.             $result['form'] = $this->renderView('private/appraisal/components/form-appraisal-questionnaire.html.twig', [
  759.                 'editedUser' => $editedUser,
  760.                 'questionnaires' => $questionnaires $questionnaires null,
  761.                 'waitForPage' => false
  762.             ]);
  763.         } else {
  764.             $result['total'] = $appraisalService->setDefaultQuestionnaire($request$user$editedUser);
  765.             $result['update'] = true;
  766.             $result['id'] = $request->get("userId");
  767.         }
  768.         return  new JsonResponse($result);
  769.     }
  770.     #[Route(path'/ajax/remind-appraisal'name'ajax_remind_appraisal')]
  771.     public function ajaxRemindAppraisal(Request $requestTranslatorInterface $translatorLogService $logMailgunService $mailgunServiceNotificationService $notificationICSService $icsSerializerInterface $serializer): JsonResponse
  772.     {
  773.         $user $this->getUser();
  774.         $appraisal $this->getDoctrine()->getRepository(Appraisal::class)->find($request->get("id"));
  775.         $result['status'] = 'OK';
  776.         if ($appraisal != null) {
  777.             $attendee = [
  778.                 [
  779.                     'name' => $user->getPersonalInfo()->getFullName(),
  780.                     'email' => $user->getEmail(),
  781.                     'role' => $appraisal->getUser()->getManager() == $user 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  782.                 ],
  783.                 [
  784.                     'name' => $appraisal->getUser()->getPersonalInfo()->getFullName(),
  785.                     'email' => $appraisal->getUser()->getEmail(),
  786.                     'role' => 'REQ-PARTICIPANT'
  787.                 ],
  788.             ];
  789.             foreach ($appraisal->getUser()->getAllManager() as $manager) {
  790.                 if ($manager != $user) {
  791.                     array_push($attendee,   [
  792.                         'name' => $manager->getPersonalInfo()->getFullName(),
  793.                         'email' => $manager->getEmail(),
  794.                         'role' => $appraisal->getUser()->getManager() == $manager 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  795.                     ]);
  796.                 }
  797.             }
  798.             // if ($_SERVER['NOTIF_SUPER_ADMIN'] == 'true') {
  799.             //     foreach ($user->assignedCompany()->superAdmins($user) as $superAdmin) {
  800.             //         if ($superAdmin != $user) {
  801.             //             array_push($attendee,   [
  802.             //                 'name' => $superAdmin->getPersonalInfo()->getFullName(),
  803.             //                 'email' => $superAdmin->getEmail(),
  804.             //                 'role' => 'OPT-PARTICIPANT'
  805.             //             ]);
  806.             //         }
  807.             //     }
  808.             // };
  809.             $scheduleDate $appraisal->getReviewAt() != null $appraisal->getReviewAt() : $appraisal->getSubmitAt();
  810.             $subject $appraisal->getTitle() . ' | ' $appraisal->getUser()->getPersonalInfo()->getFirstName() . ' | ' $scheduleDate->format('d F Y');
  811.             $endTime = new \DateTime($scheduleDate->format('Ymd H:i:s'));
  812.             $endTime->modify('+ 1 hour');
  813.             $ICSTitle $appraisal->getUser()->inProbation() ? 'Review (Probation)' 'Review';
  814.             $icsData =  $ics->generateEventFile([
  815.                 'uid' => $appraisal->getId() . $appraisal->getUser()->getId() . ':' $appraisal->getUser()->getEmail(),
  816.                 'title' => $ICSTitle ': ' $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  817.                 'description' => $translator->trans('ics.appraisal.description', ['%uid%' => $appraisal->getUser()->getId(), '%user%' => strtolower($appraisal->getUser()->getPersonalInfo()->getFirstName()), '%link%' => $this->generateUrl('user_profile', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getUser()->getEmail() . '?g=appraisal&open=appraisal-' $appraisal->getId()]),
  818.                 'startDateTime' => $scheduleDate->format('Ymd') . 'T' $scheduleDate->format('His'),
  819.                 'endDateTime' =>  $scheduleDate->format('Ymd') . 'T' $endTime->format('His'),
  820.                 'location' =>  $this->generateUrl('submit_appraisal', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getId(),
  821.                 'organizer' => [
  822.                     'name' => $this->getParameter('systemEmailName'),
  823.                     'email' => $request->getHost() == 'hr.mediatropy.com' $this->getParameter('systemEmailAddr') : 'devops@mediatropy.com'
  824.                 ],
  825.                 'attendee' => $attendee,
  826.                 'reminderTime' => '-15M',
  827.                 'reminderDescription' => $translator->trans('ics.appraisal.title') . ' ' $appraisal->getTitle(),
  828.                 'fileName' => 'Appraisal-' $appraisal->getIcsToken()
  829.             ], true);
  830.             /*
  831.             $mailgunService->sendEmail(
  832.                 $translator->trans('email.appraisal.scheduled') . ' ' . $appraisal->getSubmitAt()->format('d F Y'),
  833.                 [$appraisal->getUser()->getEmail()],
  834.                 'email/user-notification/appraisal/request.html.twig',
  835.                 [
  836.                     'user' => $appraisal->getUser(),
  837.                     'appraisal' => $appraisal,
  838.                 ]
  839.             );*/
  840.             $mailgunService->sendEmailWithAttachment([
  841.                 'subject' => $subject,
  842.                 'to' => $appraisal->getUser()->getEmail(),
  843.                 'template' => 'email/user-notification/appraisal/request.html.twig',
  844.                 'params' => [
  845.                     'recipient' => $appraisal->getUser(),
  846.                     'sender' => $user,
  847.                     'appraisal' => $appraisal,
  848.                     'reschedule' => false
  849.                 ],
  850.                 'ical' => $icsData
  851.             ]);
  852.             foreach ($appraisal->getUser()->getAllManager() as $manager) {
  853.                 $mailgunService->sendEmailWithAttachment([
  854.                     'subject' => $subject,
  855.                     'to' => $manager->getEmail(),
  856.                     'template' => 'email/user-notification/appraisal/request-cc.html.twig',
  857.                     'params' => [
  858.                         'sender' => $user,
  859.                         'recipient' => $manager,
  860.                         'appraisal' => $appraisal,
  861.                         'reschedule' => false
  862.                     ],
  863.                     'ical' => $icsData
  864.                 ]);
  865.             }
  866.             /*
  867.             if ($_SERVER['NOTIF_SUPER_ADMIN'] == 'true') {
  868.                 foreach ($user->assignedCompany()->superAdmins($user) as $superAdmin) {
  869.                     if ($superAdmin != $user && !in_array($superAdmin, $appraisal->getUser()->getAllManager())) {
  870.                         $mailgunService->sendEmailWithAttachment([
  871.                             'subject' => $translator->trans('email.system.copy_super_admin').' '.$subject,
  872.                             'to' => $superAdmin->getEmail(),
  873.                             'template' => 'email/user-notification/appraisal/request-cc.html.twig',
  874.                             'params' => [
  875.                                 'recipient' => $appraisal->getUser(),
  876.                                 'sender' => $user,
  877.                                 'cc' => $superAdmin,
  878.                                 'appraisal' => $appraisal,
  879.                                 'reschedule' => false
  880.                             ],
  881.                             'ical' => $icsData
  882.                         ]);
  883.                     };
  884.                 }
  885.             };*/
  886.             $log->save($user, [
  887.                 'owner' => $appraisal->getUser(),
  888.                 'message' => 'log.appraisal.reminder',
  889.                 'old_data' => null,
  890.                 'new_data' => null
  891.             ]);
  892.             /*
  893.             if ($appraisal->currentStep($user) == 1) {
  894.                 if ($appraisal->getSubmitAt()->format('Ymd') <= date('Ymd')) {
  895.                     $appraisalClass = 'b-danger';
  896.                 } else {
  897.                     $appraisalClass = 'b-warning';
  898.                 };
  899.             } else {
  900.                 $appraisalClass = '';
  901.             };*/
  902.             $result['content'] = $this->renderView('private/user/components/tab-appraisal-list-manager.html.twig', [
  903.                 'appraisal' => $appraisal
  904.             ]);
  905.         } else {
  906.             $result['status'] = 'ERROR';
  907.             $result['message'] = $translator->trans('messages.request.missing');
  908.         }
  909.         return new JsonResponse($result);
  910.     }
  911.     #[Route(path'/ajax/close-appraisal'name'ajax_close_appraisal')]
  912.     public function ajaxCloseAppraisal(Request $requestTranslatorInterface $translatorLogService $logMailgunService $mailgunServiceNotificationService $notificationICSService $icsSerializerInterface $serializer): JsonResponse
  913.     {
  914.         $user $this->getUser();
  915.         $appraisal $this->getDoctrine()->getRepository(Appraisal::class)->find($request->get("id"));
  916.         $result['status'] = 'OK';
  917.         if ($appraisal != null) {
  918.             $entityManager $this->getDoctrine()->getManager();
  919.             // $appraisal->setReviewAt(new \DateTime());
  920.             // $appraisal->setReviewedBy($user);
  921.             // $appraisal->setReviewedAt(new \DateTime());
  922.             $appraisal->setConcludedAt(new \DateTime());
  923.             $entityManager->flush();
  924.             $entityManager->refresh($appraisal);
  925.             $log->save($user, [
  926.                 'owner' => $appraisal->getUser(),
  927.                 'message' => 'log.appraisal.close',
  928.                 'old_data' => null,
  929.                 'new_data' => $appraisal
  930.             ]);
  931.             // $notification->push($appraisal->getUser(), [
  932.             //     'message' => 'notification.appraisal.reviewed_alt',
  933.             //     'entity' => 'Appraisal',
  934.             //     'objectId' => $appraisal->getId(),
  935.             //     'data' => $serializer->serialize([
  936.             //         'title' => $appraisal->getTitle(),
  937.             //         'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  938.             //         'time' => $appraisal->getReviewAt()->format('Ymd His'),
  939.             //     ], 'json')
  940.             // ], true);
  941.             $subject $appraisal->getTitle() . ' | ' $appraisal->getUser()->getPersonalInfo()->getFirstName() . ' | ' $translator->trans('email.appraisal.result');
  942.             $mailgunService->sendEmail(
  943.                 $subject,
  944.                 [$appraisal->getUser()->getEmail()],
  945.                 'email/user-notification/appraisal-result.html.twig',
  946.                 [
  947.                     'recipient' => $appraisal->getUser(),
  948.                     'appraisal' => $appraisal,
  949.                 ]
  950.             );
  951.         } else {
  952.             $result['status'] = 'ERROR';
  953.             $result['message'] = $translator->trans('messages.request.missing');
  954.         }
  955.         return new JsonResponse($result);
  956.     }
  957.     #[Route(path'/ajax/delete-appraisal-request'name'ajax_delete_appraisal_request'methods'POST')]
  958.     function ajaxDeleteAppraisalRequest(Request $requestTranslatorInterface $translatorMailgunService $mailgunServiceICSService $icsNotificationService $notificationLogService $logSerializerInterface $serializer): JsonResponse
  959.     {
  960.         $user $this->getUser();
  961.         $appraisal =  $this->getDoctrine()->getRepository(Appraisal::class)->find($request->get("id"));
  962.         //$oldData = clone $appraisal;
  963.         $result['status'] = 'OK';
  964.         if ($appraisal != null) {
  965.             $response $this->forward('App\Controller\AppraisalController::deleteAppraisalRequest', [
  966.                 'request' => $request,
  967.                 'user' => $user,
  968.                 'appraisal' => $appraisal,
  969.                 'logged' => true
  970.             ]);
  971.             //dump(json_decode($response->getContent(),true));
  972.             $result['status'] = json_decode($response->getContent(), true);
  973.         } else {
  974.             $result['status'] = 'ERROR';
  975.             $result['message'] = $translator->trans('messages.request.missing');
  976.         }
  977.         return new JsonResponse($result);
  978.     }
  979.     #[Route(path'/function/delete-appraisal-request'name'function_delete_appraisal_request'methods'POST')]
  980.     function deleteAppraisalRequest($request$user$appraisal$logged trueTranslatorInterface $translatorMailgunService $mailgunServiceAppraisalService $appraisalServiceICSService $icsNotificationService $notificationLogService $logSerializerInterface $serializer): JsonResponse
  981.     {
  982.         $oldData = clone $appraisal;
  983.         $loggedIn $request true false;
  984.         if ($logged) {
  985.             $log->save($user, [
  986.                 'owner' => $appraisal->getUser(),
  987.                 'message' => 'log.appraisal.request_delete',
  988.                 'old_data' => $oldData,
  989.                 'new_data' => null
  990.             ]);
  991.         };
  992.         if (($appraisal->getAdHoc() || $appraisal->currentStep() > 2) && $appraisal->getReviewAt() != null && date('YmdHis') < $appraisal->getReviewAt()->format('YmdHis')) { // Only send notification if review set       
  993.             $attendee = [
  994.                 [
  995.                     'name' => $user->getPersonalInfo()->getFullName(),
  996.                     'email' => $user->getEmail(),
  997.                     'role' => $appraisal->getUser()->getManager() == $user 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  998.                 ],
  999.                 [
  1000.                     'name' => $appraisal->getUser()->getPersonalInfo()->getFullName(),
  1001.                     'email' => $appraisal->getUser()->getEmail(),
  1002.                     'role' => 'REQ-PARTICIPANT'
  1003.                 ],
  1004.             ];
  1005.             foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1006.                 try {
  1007.                     if ($manager != $user) {
  1008.                         array_push($attendee,   [
  1009.                             'name' => $manager->getPersonalInfo()->getFullName(),
  1010.                             'email' => $manager->getEmail(),
  1011.                             'role' => $appraisal->getUser()->getManager() == $manager 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  1012.                         ]);
  1013.                     }
  1014.                 } catch (Exception $e) {
  1015.                     continue;
  1016.                 }
  1017.             }
  1018.             /*if ($_SERVER['NOTIF_SUPER_ADMIN'] == 'true') {
  1019.                 foreach ($user->assignedCompany()->superAdmins($user) as $superAdmin) {
  1020.                     if ($superAdmin != $user) {
  1021.                         array_push($attendee,   [
  1022.                             'name' => $superAdmin->getPersonalInfo()->getFullName(),
  1023.                             'email' => $superAdmin->getEmail(),
  1024.                             'role' => 'OPT-PARTICIPANT'
  1025.                         ]);
  1026.                     }
  1027.                 }
  1028.             };*/
  1029.             $subject =  $appraisal->getTitle() . ' | ' $appraisal->getUser()->getPersonalInfo()->getFirstName() . ' | ' $translator->trans('email.appraisal.cancel');
  1030.             $endTime = new \DateTime($appraisal->getReviewAt()->format('Ymd H:i:s'));
  1031.             $endTime->modify('+ 1 hour');
  1032.             $ICSTitle $appraisal->getUser()->inProbation() ? 'Review (Probation)' 'Review';
  1033.             if ($appraisal->getAdHoc()) $ICSTitle 'Ad hoc Review';
  1034.             if ($appraisal->getAdHoc() || $appraisal->currentStep() > 2) {
  1035.                 $icsData =  $ics->generateEventFile([
  1036.                     'uid' => $appraisal->getId() . $appraisal->getUser()->getId() . ':' $appraisal->getUser()->getEmail(),
  1037.                     'title' => $ICSTitle ': ' $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1038.                     'description' => null,
  1039.                     'startDateTime' => $appraisal->getReviewAt()->format('Ymd') . 'T' $appraisal->getReviewAt()->format('His'),
  1040.                     'endDateTime' =>  $appraisal->getReviewAt()->format('Ymd') . 'T' $endTime->format('His'),
  1041.                     'location' =>  $this->generateUrl('submit_appraisal', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getId(),
  1042.                     'organizer' => [
  1043.                         'name' => $this->getParameter('systemEmailName'),
  1044.                         'email' => $request->getHost() == 'hr.mediatropy.com' $this->getParameter('systemEmailAddr') : 'devops@mediatropy.com'
  1045.                     ],
  1046.                     'attendee' => $attendee,
  1047.                     'reschedule' => $appraisal->getReschedule(),
  1048.                     'reminderTime' => '-15M',
  1049.                     'reminderDescription' => $translator->trans('ics.appraisal.title') . ' ' $appraisal->getTitle(),
  1050.                     'fileName' => 'Appraisal-' $appraisal->getIcsToken()
  1051.                 ], true'CANCEL');
  1052.             } else {
  1053.                 $icsData null;
  1054.             };
  1055.             /*$notification->disable($appraisal->getUser(), [
  1056.                 'entity' => 'appraisal',
  1057.                 'objectId' => $appraisal->getId(),
  1058.             ]);*/
  1059.             if ($logged) {
  1060.                 // $notification->push($appraisal->getUser(), [
  1061.                 //     'message' => 'notification.appraisal.canceled.user',
  1062.                 //     'entity' => 'Appraisal',
  1063.                 //     'objectId' => $appraisal->getId(),
  1064.                 //     'data' => $serializer->serialize([
  1065.                 //         'title' => $appraisal->getTitle(),
  1066.                 //         'name' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1067.                 //         'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  1068.                 //         'time' => $appraisal->getReviewAt()->format('Ymd His'),
  1069.                 //     ], 'json')
  1070.                 // ], true);
  1071.             };
  1072.             $mailgunService->sendEmailWithAttachment([
  1073.                 'subject' => $subject,
  1074.                 'to' => $appraisal->getUser()->getEmail(),
  1075.                 'template' => 'email/user-notification/appraisal/cancel.html.twig',
  1076.                 'params' => [
  1077.                     'recipient' => $appraisal->getUser(),
  1078.                     'appraisal' => $appraisal,
  1079.                     'group' => 'user'
  1080.                 ],
  1081.                 'ical' => $icsData
  1082.             ], $loggedIn);
  1083.             foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1084.                 try {
  1085.                     /*$notification->clear($manager, [
  1086.                     'entity' => 'appraisal',
  1087.                     'objectId' => $appraisal->getId(),
  1088.                 ]);*/
  1089.                     // $notification->push($manager, [
  1090.                     //     'message' => 'notification.appraisal.canceled.manager',
  1091.                     //     'entity' => 'Appraisal',
  1092.                     //     'objectId' => $appraisal->getId(),
  1093.                     //     'data' => $serializer->serialize([
  1094.                     //         'title' => $appraisal->getTitle(),
  1095.                     //         'name' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1096.                     //         'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  1097.                     //         'time' => $appraisal->getReviewAt()->format('Ymd His'),
  1098.                     //     ], 'json')
  1099.                     // ], true);
  1100.                     $mailgunService->sendEmailWithAttachment([
  1101.                         'subject' => $subject,
  1102.                         'to' => $manager->getEmail(),
  1103.                         'template' => 'email/user-notification/appraisal/cancel-cc.html.twig',
  1104.                         'params' => [
  1105.                             'recipient' => $manager,
  1106.                             'appraisal' => $appraisal,
  1107.                             'group' => 'manager'
  1108.                         ],
  1109.                         'ical' => $icsData
  1110.                     ], $loggedIn);
  1111.                 } catch (Exception $e) {
  1112.                     continue;
  1113.                 }
  1114.             }
  1115.             $emailGroup $this->getParameter('systemEmailGroup');
  1116.             foreach ($emailGroup as $groupName => $groupEmail) {
  1117.                 if ($appraisal->getUser()->inProbation() == false && $groupName == 'finance') continue;
  1118.                 try {
  1119.                     $mailgunService->sendEmailWithAttachment([
  1120.                         'subject' => $subject,
  1121.                         'to' => $groupEmail,
  1122.                         'template' => 'email/user-notification/appraisal/cancel-cc.html.twig',
  1123.                         'params' => [
  1124.                             'recipient' => strtoupper($groupName) . ' Team',
  1125.                             'appraisal' => $appraisal,
  1126.                             'group' => $groupName
  1127.                         ],
  1128.                         'ical' => $icsData
  1129.                     ], $loggedIn);
  1130.                 } catch (Exception $e) {
  1131.                     continue;
  1132.                 }
  1133.             };
  1134.             /*if ($_SERVER['NOTIF_SUPER_ADMIN'] == 'true') {
  1135.                 foreach ($user->assignedCompany()->superAdmins($user) as $superAdmin) {
  1136.                     if ($superAdmin != $user && !in_array($superAdmin, $appraisal->getUser()->getAllManager())) {
  1137.                         // $notification->push($superAdmin, [
  1138.                         //             'message' => 'notification.appraisal.canceled.manager',
  1139.                         //             'entity' => 'Appraisal',
  1140.                         //             'objectId' => $appraisal->getId(),
  1141.                         //             'data' => $serializer->serialize([
  1142.                         //                 'title' => $appraisal->getTitle(),
  1143.                         //                 'name' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1144.                         //                 'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  1145.                         //             ], 'json')
  1146.                         //         ], true);
  1147.                         $mailgunService->sendEmailWithAttachment([
  1148.                             'subject' => $translator->trans('email.system.copy_super_admin') . ' ' . $subject,
  1149.                             'to' => $superAdmin->getEmail(),
  1150.                             'template' => 'email/user-notification/appraisal/cancel-cc.html.twig',
  1151.                             'params' => [
  1152.                                 'sender' => $user,
  1153.                                 'recipient' => $superAdmin,
  1154.                                 'appraisal' => $appraisal,
  1155.                             ],
  1156.                             'ical' => $icsData
  1157.                         ]);
  1158.                     };
  1159.                 }
  1160.             };*/
  1161.         } else if (in_array($appraisal->currentStep(), [12])) {
  1162.             $subject =  $appraisal->getTitle() . ' | ' $appraisal->getUser()->getPersonalInfo()->getFirstName() . ' | ' $translator->trans('email.appraisal.cancel');
  1163.             $emailTemplate 'email/user-notification/appraisal/cancel.html.twig';
  1164.             $mailgunService->sendEmail(
  1165.                 $subject,
  1166.                 [$appraisal->getUser()->getEmail()],
  1167.                 $emailTemplate,
  1168.                 [
  1169.                     'recipient' => $appraisal->getUser(),
  1170.                     'appraisal' => $appraisal,
  1171.                     'group' => 'user'
  1172.                 ]
  1173.             );
  1174.             $emailTemplate 'email/user-notification/appraisal/cancel-cc.html.twig';
  1175.             foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1176.                 $mailgunService->sendEmail(
  1177.                     $subject,
  1178.                     [$manager->getEmail()],
  1179.                     $emailTemplate,
  1180.                     [
  1181.                         'recipient' => $manager,
  1182.                         'appraisal' => $appraisal,
  1183.                         'group' => 'manager'
  1184.                     ]
  1185.                 );
  1186.             };
  1187.             $emailGroup $this->getParameter('systemEmailGroup');
  1188.             foreach ($emailGroup as $groupName => $groupEmail) {
  1189.                 if ($appraisal->getUser()->inProbation() == false && $groupName == 'finance') continue;
  1190.                 try {
  1191.                     $mailgunService->sendEmail(
  1192.                         $subject,
  1193.                         [$groupEmail],
  1194.                         $emailTemplate,
  1195.                         [
  1196.                             'recipient' => strtoupper($groupName) . ' Team',
  1197.                             'appraisal' => $appraisal,
  1198.                             'group' => $groupName,
  1199.                         ]
  1200.                     );
  1201.                 } catch (Exception $e) {
  1202.                 }
  1203.             };
  1204.         };
  1205.         $entityManager $this->getDoctrine()->getManager();
  1206.         $appraisalComments $this->getDoctrine()->getRepository(AppraisalComment::class)->findByAppraisal($appraisal);
  1207.         if ($appraisalComments != null) {
  1208.             foreach ($appraisalComments as $appraisalComment) {
  1209.                 $entityManager->remove($appraisalComment);
  1210.             };
  1211.         }
  1212.         $appraisalForms $this->getDoctrine()->getRepository(AppraisalForm::class)->findByAppraisal($appraisal);
  1213.         if ($appraisalForms != null) {
  1214.             foreach ($appraisalForms as $appraisalForm) {
  1215.                 $entityManager->remove($appraisalForm);
  1216.             };
  1217.         };
  1218.         $entityManager->remove($appraisal);
  1219.         $entityManager->flush();
  1220.         $appraisalService->syncAppraisalStatus($appraisal->getUser());
  1221.         return new JsonResponse('OK');
  1222.     }
  1223.     #[Route(path'/appraisal/{_id}'defaults: ['_id' => 0], name'submit_appraisal')]
  1224.     public function submitAppraisal(string $_idRequest $requestTranslatorInterface $translatorSerializerInterface $serializerMailgunService $mailgunServiceNotificationService $notificationICSService $icsAppraisalService $appraisalServiceLeaveService $leaveServiceUserService $userServiceLogService $log)
  1225.     {
  1226.         $user $this->getUser();
  1227.         $appraisal =  $this->getDoctrine()->getRepository(Appraisal::class)->find($_id);
  1228.         if ($appraisal == null) {
  1229.             return $this->redirectToRoute('user_profile', ['g' => 'appraisal']);
  1230.         };
  1231.         $oldAppraisal = clone $appraisal;
  1232.         $oldData = [];
  1233.         $newData = [];
  1234.         $userNote null;
  1235.         $appraisalNotes null;
  1236.         $userAppraisalNote = new AppraisalNote();
  1237.         $loggedIn $request true false;
  1238.         $concludeAppraisal false;
  1239.         $permanent=false;
  1240.         if(!is_null($oldAppraisal)){
  1241.             $permanent=$oldAppraisal->getUser()->getPersonalInfo()->getJobStatus();
  1242.             if($permanent=='form.job_status.permanent'){$permanent=true;}else{$permanent=false;}
  1243.         }
  1244.         if ($appraisal->getAppraisalNotes() != null and count($appraisal->getAppraisalNotes()) > 0) {
  1245.             $appraisalNotes $this->getDoctrine()->getRepository(AppraisalNote::class)->findByAppraisal($appraisal);
  1246.             $userNote $this->getDoctrine()->getRepository(AppraisalNote::class)->findByAppraisalByUser($appraisal$user);
  1247.             if ($userNote != null) {
  1248.                 $userAppraisalNote $userNote;
  1249.             }
  1250.         };
  1251.         $oldUserNote is_object($userNote) ? clone $userNote null;
  1252.         $appraisalNoteForm $this->createForm(AppraisalNoteType::class, $userAppraisalNote);
  1253.         // Only HR, Appraisse and included Manager access this function
  1254.         if ($this->isGranted('ROLE_HR') || $appraisal->getUser() == $user || $appraisal->getUser()->getIsIncludedManager($user)) {
  1255.             
  1256.             $categorizedAssessments = [];
  1257.             $categories =  $this->getDoctrine()->getRepository(AppraisalCategory::class)->findByCDT($appraisal->getUser()->assignedCompany(), $appraisal->getUser()->organizationDepartments(), $appraisal->getUser()->appraisalType($appraisal));
  1258.             foreach($categories as $category){
  1259.                 foreach($category->getQuestionnaire() as $question){
  1260.                    $categorizedAssessments[] = $question;
  1261.                 }
  1262.             }
  1263.             if ($_SERVER['APP_ENV'] == "prod" && $appraisal->getUser()->inProbation() && $appraisal->getSubmitAt()->format('Ymd') < '20221001') {
  1264.                 $oldAssessments $this->getDoctrine()->getRepository(AppraisalForm::class)->findByCODA($appraisal->getUser()->assignedCompany(), $appraisal->getUser()->getOffice(), $appraisal->getUser()->organizationDepartments(), $appraisal);
  1265.             } else {
  1266.                 $oldAssessments $this->getDoctrine()->getRepository(AppraisalForm::class)->findByType($appraisal->getUser()->assignedCompany(), $appraisal->getUser()->appraisalType($appraisal), $appraisal);
  1267.             }
  1268.             // This is to check between old and new assessments (has been categorized or not)
  1269.             if(count($categorizedAssessments) > 0){
  1270.                 $assessments $categorizedAssessments;
  1271.                 // Below are for combine old and new assessments
  1272.                 // if(count($oldAssessments) > 0){
  1273.                 //     foreach($oldAssessments as $oldAssessment){
  1274.                 //         $exist = false;
  1275.                 //         foreach($categorizedAssessments as $assessment){
  1276.                 //             if($oldAssessment->getId() == $assessment->getId()){
  1277.                 //                 $exist = true;
  1278.                 //                 break;
  1279.                 //             }
  1280.                 //         }
  1281.                 //         if(!$exist){
  1282.                 //             $assessments[] = $oldAssessment;
  1283.                 //         }
  1284.                 //     }
  1285.                 // }
  1286.             } else {
  1287.                 $assessments $oldAssessments;
  1288.             }
  1289.         
  1290.             if ($request->isMethod('post')) {
  1291.                 $entityManager $this->getDoctrine()->getManager();
  1292.                 
  1293.                 if ($user != $appraisal->getUser()) {
  1294.                     $requestNote $request->request->get('appraisal_note');
  1295.                     $summary false;
  1296.                     if ($userAppraisalNote->getId() && $userAppraisalNote->getNote() != '') {
  1297.                         $oldUserAppraisalNote = clone $userAppraisalNote;
  1298.                     } else {
  1299.                         $oldUserAppraisalNote null;
  1300.                     };
  1301.                     if ($requestNote['note'] != '') {
  1302.                         $summary $userAppraisalNote->getNote() == $requestNote['note'] ? false $requestNote['note'];
  1303.                         $userAppraisalNote->setNote($requestNote['note']);
  1304.                         $userAppraisalNote->setDraft($request->request->get('draft') == "false" false true);
  1305.                     };
  1306.                     if ($userNote == null) {
  1307.                         $userAppraisalNote->setAppraisal($appraisal);
  1308.                         $userAppraisalNote->setUser($user);
  1309.                         $userAppraisalNote->setCreatedAt(new \DateTime());
  1310.                         $entityManager->persist($userAppraisalNote);
  1311.                     } else {
  1312.                         $userAppraisalNote->setUpdatedAt(new \DateTime());
  1313.                     }
  1314.                     // Only proceed if it's not a draft anymore
  1315.                     if ($request->request->get('draft') == 'false') {
  1316.                         // Only proceed if it still not concluded yet
  1317.                         if (($appraisal->getConcludedAt() == null && $appraisal->getUser()->getIsIncludedManager($user)) || ($appraisal->getConcludedAt() == null && $this->isGranted('ROLE_HR'))) {
  1318.                             $currentStep $appraisal->currentStep();
  1319.                             $objectiveInvitation false;
  1320.                             $objectiveEmailTemplate 'email/user-notification/appraisal/set-objective.html.twig';
  1321.                             $emailGroup $this->getParameter('systemEmailGroup'); 
  1322.                             // Only proceed if in step 2 or 3
  1323.                             if ($currentStep == && $appraisal->getUser()->getIsIncludedManager($user) || $currentStep == && $appraisal->getUser()->getManager() == $user && $appraisal->getReviewAt() == null || $this->isGranted('ROLE_HR')) { // Manager or HR
  1324.                                 if ($appraisal->getUser()->getManager() == $user$appraisal->setReviewedAt(new \DateTime());
  1325.                                 // if ($appraisal->getUser()->inProbation()) $appraisal->setReviewAt(\DateTime::createFromFormat('d/m/Y H:i', $request->request->get('reviewDate')));
  1326.                                 if($request->request->get('reviewDate') != null){
  1327.                                     $appraisal->setReviewAt(\DateTime::createFromFormat('d/m/Y H:i'$request->request->get('reviewDate')));
  1328.                                 }
  1329.                                 $entityManager->flush();
  1330.                                 $entityManager->refresh($appraisal);
  1331.                                 // Only proceed if user still in probation, so it can process to permanent or not
  1332.                                 if ($appraisal->getUser()->inProbation() || $appraisal->getUser()->getManager() == $user && $appraisal->getConcludedAt() == null) {
  1333.                                     $notificationData = [
  1334.                                         '%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1335.                                         '%date%' => $appraisal->getReviewAt()->format('l, d M Y') . ' at ' $appraisal->getReviewAt()->format('H:i')
  1336.                                     ];
  1337.                                     $attendee = [
  1338.                                         [
  1339.                                             'name' => $user->getPersonalInfo()->getFullName(),
  1340.                                             'email' => $user->getEmail(),
  1341.                                             'role' => $appraisal->getUser()->getManager() == $user 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  1342.                                         ],
  1343.                                         [
  1344.                                             'name' => $appraisal->getUser()->getPersonalInfo()->getFullName(),
  1345.                                             'email' => $appraisal->getUser()->getEmail(),
  1346.                                             'role' => 'REQ-PARTICIPANT'
  1347.                                         ],
  1348.                                     ];
  1349.                                     foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1350.                                         if ($manager != $user) {
  1351.                                             array_push($attendee,   [
  1352.                                                 'name' => $manager->getPersonalInfo()->getFullName(),
  1353.                                                 'email' => $manager->getEmail(),
  1354.                                                 'role' => $appraisal->getUser()->getManager() == $manager 'REQ-PARTICIPANT' 'OPT-PARTICIPANT'
  1355.                                             ]);
  1356.                                         }
  1357.                                     }
  1358.                                     $endTime = new \DateTime($appraisal->getReviewAt()->format('Ymd H:i:s'));
  1359.                                     $endTime->modify('+ 30 minutes');
  1360.                                     $ICSTitle $appraisal->getUser()->inProbation() ? 'Review (Probation)' 'Review';
  1361.                                     $icsData =  $ics->generateEventFile([
  1362.                                         'uid' => $appraisal->getId() . $appraisal->getUser()->getId() . ':' $appraisal->getUser()->getEmail(),
  1363.                                         'title' => $ICSTitle ': ' $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1364.                                         'description' => $translator->trans('ics.appraisal.description', ['%uid%' => $appraisal->getUser()->getId(), '%user%' => strtolower($appraisal->getUser()->getPersonalInfo()->getFirstName()), '%link%' => $this->generateUrl('user_profile', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getUser()->getEmail() . '?g=appraisal&open=appraisal-' $appraisal->getId()]),
  1365.                                         'startDateTime' => $appraisal->getReviewAt()->format('Ymd') . 'T' $appraisal->getReviewAt()->format('His'),
  1366.                                         'endDateTime' =>  $appraisal->getReviewAt()->format('Ymd') . 'T' $endTime->format('His'),
  1367.                                         'location' =>  $this->generateUrl('submit_appraisal', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getId(),
  1368.                                         'organizer' => [
  1369.                                             'name' => $this->getParameter('systemEmailName'),
  1370.                                             'email' => $_SERVER['APP_ENV'] == 'prod' $this->getParameter('systemEmailAddr') : 'devops@mediatropy.com'
  1371.                                         ],
  1372.                                         'attendee' => $attendee,
  1373.                                         'reminderTime' => '-15M',
  1374.                                         'reminderDescription' => $translator->trans('ics.appraisal.title') . ' ' $appraisal->getTitle(),
  1375.                                         'fileName' => 'Appraisal-' $appraisal->getIcsToken()
  1376.                                     ], true);
  1377.                                     if ($appraisal->getUser()->inProbation()) {
  1378.                                         $emailTemplate 'email/user-notification/appraisal/invitation-probation.html.twig';
  1379.                                         $titleTemplate 'email.appraisal.invitation.probation.';
  1380.                                         $titleNotificationUser $translator->trans('notification.appraisal.invitation.probation.user', ['%link%' => $this->generateURL('submit_appraisal') . '/' $appraisal->getId()]);
  1381.                                         $titleNotificationManager $translator->trans('notification.appraisal.invitation.probation.manager', ['%link%' => $this->generateURL('submit_appraisal') . '/' $appraisal->getId()]);
  1382.                                     } else {
  1383.                                         $emailTemplate 'email/user-notification/appraisal/invitation-default.html.twig';
  1384.                                         $titleTemplate 'email.appraisal.invitation.default.';
  1385.                                         $titleNotificationUser $translator->trans('notification.appraisal.invitation.default.user', ['%link%' => $this->generateURL('submit_appraisal') . '/' $appraisal->getId()]);
  1386.                                         $titleNotificationManager $translator->trans('notification.appraisal.invitation.default.manager', ['%link%' => $this->generateURL('submit_appraisal') . '/' $appraisal->getId()]);
  1387.                                     };
  1388.                                     $mailgunService->sendEmailWithAttachment([
  1389.                                         'subject' =>  $translator->trans($titleTemplate 'user', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%date%' => $appraisal->getReviewAt()->format('l, d M Y')]),
  1390.                                         'to' => $appraisal->getUser()->getEmail(),
  1391.                                         'template' => $emailTemplate,
  1392.                                         'params' => [
  1393.                                             'recipient' => $appraisal->getUser(),
  1394.                                             'appraisal' => $appraisal,
  1395.                                             'group' => 'user'
  1396.                                         ],
  1397.                                         'ical' => $icsData
  1398.                                     ], $loggedIn);
  1399.                                     $notification->push($appraisal->getUser(), $titleNotificationUser$notificationData'i-review'$appraisal->getId());
  1400.                                     foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1401.                                         $mailgunService->sendEmailWithAttachment([
  1402.                                             'subject' => $translator->trans($titleTemplate 'manager', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%date%' => $appraisal->getReviewAt()->format('l, d M Y')]),
  1403.                                             'to' => $manager->getEmail(),
  1404.                                             'template' => $emailTemplate,
  1405.                                             'params' => [
  1406.                                                 'recipient' => $manager,
  1407.                                                 'appraisal' => $appraisal,
  1408.                                                 'group' => 'manager'
  1409.                                             ],
  1410.                                             'ical' => $icsData
  1411.                                         ], $loggedIn);
  1412.                                         $notification->push($manager$titleNotificationManager$notificationData'i-review'$appraisal->getId());
  1413.                                     }
  1414.                                     foreach ($emailGroup as $groupName => $groupEmail) {
  1415.                                         if ($groupName == 'hr') { // only HR
  1416.                                             $mailgunService->sendEmailWithAttachment([
  1417.                                                 'subject' => $translator->trans($titleTemplate 'hr', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%date%' => $appraisal->getReviewAt()->format('l, d M Y')]),
  1418.                                                 'to' => $groupEmail,
  1419.                                                 'template' => $emailTemplate,
  1420.                                                 'params' => [
  1421.                                                     'recipient' => strtoupper($groupName) . ' Team',
  1422.                                                     'appraisal' => $appraisal,
  1423.                                                     'group' => $groupName
  1424.                                                 ],
  1425.                                                 'ical' => $icsData
  1426.                                             ], $loggedIn);
  1427.                                         };
  1428.                                     };
  1429.                                 } elseif ($appraisal->getUser()->getIsIncludedManager($user) && $appraisal->getConcludedAt() == null) { // Yearly
  1430.                                     if ($appraisal->getProbation() == true) {
  1431.                                         $emailTemplate 'email/user-notification/appraisal/manager-submitted-probation.html.twig';
  1432.                                         $titleTemplate 'email.appraisal.submitted.manager_probation.';
  1433.                                     } else {
  1434.                                         $emailTemplate 'email/user-notification/appraisal/manager-submitted-default.html.twig';
  1435.                                         $titleTemplate 'email.appraisal.submitted.manager_default.';
  1436.                                       
  1437.                                     };
  1438.                                     // $mailgunService->sendEmail(
  1439.                                     //     $translator->trans($titleTemplate . 'manager'),
  1440.                                     //     [$user->getEmail()],
  1441.                                     //     $emailTemplate,
  1442.                                     //     [
  1443.                                     //         'recipient' => $user,
  1444.                                     //         'sender' => $user,
  1445.                                     //         'group' => 'manager',
  1446.                                     //         'appraisal' => $appraisal,
  1447.                                     //     ]
  1448.                                     // );
  1449.                                     // foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1450.                                     //     $mailgunService->sendEmail(
  1451.                                     //         $translator->trans($titleTemplate . 'manager', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName()]),
  1452.                                     //         [$manager->getEmail()],
  1453.                                     //         $emailTemplate,
  1454.                                     //         [
  1455.                                     //             'recipient' => $manager,
  1456.                                     //             'group' => 'manager',
  1457.                                     //             'appraisal' => $appraisal,
  1458.                                     //         ]
  1459.                                     //     );
  1460.                                     // }
  1461.                            
  1462.                                         // $appraisal->setReviewAt(new \DateTime());
  1463.                                     
  1464.                                     foreach ($emailGroup as $groupName => $groupEmail) {
  1465.                                         if ($groupName == 'hr') { // only HR
  1466.                                             $mailgunService->sendEmail(
  1467.                                                 $translator->trans($titleTemplate 'hr', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFullName()]),
  1468.                                                 [$groupEmail],
  1469.                                                 $emailTemplate,
  1470.                                                 [
  1471.                                                     'recipient' => strtoupper($groupName) . ' Team',
  1472.                                                     'group' => $groupName,
  1473.                                                     'appraisal' => $appraisal,
  1474.                                                 ]
  1475.                                             );
  1476.                                         };
  1477.                                     };
  1478.                                  
  1479.                                 }
  1480.                             } elseif (in_array($currentStep, [45]) == true && $appraisal->getUser()->inProbation() && $appraisal->getAdHoc() == false) { // Probation User
  1481.                                 $expiryDate $appraisal->getUser()->getPersonalInfo()->getJobExpiryDate() ? $appraisal->getUser()->getPersonalInfo()->getJobExpiryDate() : new \DateTime();
  1482.                                 $joinDate $appraisal->getUser()->getPersonalInfo()->getJobJoinDate();
  1483.                                 $conclusion $request->request->get('conclusion');
  1484.                                 $notificationData = [
  1485.                                     '%link%' => '<a href="' $this->generateURL('submit_appraisal') . '/' $appraisal->getId() . '">Probation\'s Appraisal Form</a>'
  1486.                                 ];
  1487.                                 switch ($conclusion) {
  1488.                                     case 'permanent':
  1489.                                         //$newExpiryDate = date('Y-m-d', strtotime('6 months', $expiryDate->getTimestamp()));
  1490.                                         /*$newExpiryDate = $userService->nextReviewDate();
  1491.                                         $appraisal->getUser()->getPersonalInfo()->setJobExpiryDate(\DateTime::createFromFormat('Y-m-d', $newExpiryDate));
  1492.                                         /*$totalProbation = 3 + $appraisal->getExtendedProbation();
  1493.                                         echo $totalProbation.' '.$appraisal->getExtendedProbation();
  1494.                                         $becomePermanentDate = $joinDate->modify('+'.$totalProbation.' months');
  1495.                                         dump($becomePermanentDate);die;*/
  1496.                                         //$appraisal->getUser()->getPersonalInfo()->setJobPermanentDate($joinDate->modify('+3 months'));
  1497.                                         $attributes =  $appraisal->getUser()->assignedCompany()->getAttributes();
  1498.                                         $newExpiryDate = isset($attributes['appraisalEndAt']) ? new \DateTime($attributes['appraisalEndAt']['date']) : new \DateTime('+ 1 years');
  1499.                                         $appraisal->getUser()->getPersonalInfo()->setJobExpiryDate($newExpiryDate);
  1500.                                         $appraisal->getUser()->getPersonalInfo()->setJobPermanentDate($expiryDate);
  1501.                                         $emailTemplate 'email/user-notification/appraisal/conclude-pass.html.twig';
  1502.                                         $titleTemplate 'email.appraisal.concluded.pass.';
  1503.                                         $objectiveInvitation true;
  1504.                                         $concludeAppraisal true;
  1505.                                         $notification->push($appraisal->getUser(), 'notification.appraisal.concluded.pass'$notificationData'i-star-svg'$appraisal->getId());
  1506.                                         break;
  1507.                                     case 'limited':
  1508.                                         $newExpiryDate date('Y-m-d'strtotime('3 months'$expiryDate->getTimestamp()));
  1509.                                         $appraisal->getUser()->getPersonalInfo()->setJobExpiryDate(\DateTime::createFromFormat('Y-m-d'$newExpiryDate));
  1510.                                         $emailTemplate 'email/user-notification/appraisal/conclude-pass.html.twig';
  1511.                                         $titleTemplate 'email.appraisal.concluded.pass.';
  1512.                                         $objectiveInvitation true;
  1513.                                         $concludeAppraisal true;
  1514.                                         $notification->push($appraisal->getUser(), 'notification.appraisal.concluded.pass'$notificationData'i-star-svg'$appraisal->getId());
  1515.                                         break;
  1516.                                     case 'temporary':
  1517.                                         $newExpiryDate date('Y-m-d'strtotime('3 months'$expiryDate->getTimestamp()));
  1518.                                         $appraisal->getUser()->getPersonalInfo()->setJobExpiryDate(\DateTime::createFromFormat('Y-m-d'$newExpiryDate));
  1519.                                         $emailTemplate 'email/user-notification/appraisal/conclude-pass.html.twig';
  1520.                                         $titleTemplate 'email.appraisal.concluded.pass.';
  1521.                                         $objectiveInvitation true;
  1522.                                         $concludeAppraisal true;
  1523.                                         $notification->push($appraisal->getUser(), 'notification.appraisal.concluded.pass'$notificationData'i-star-svg'$appraisal->getId());
  1524.                                         break;
  1525.                                     case 'fail':
  1526.                                         $appraisal->getUser()->getPersonalInfo()->setJobTerminateReason('form.terminate_reason.probation_failed');
  1527.                                         $appraisal->getUser()->getPersonalInfo()->setJobTerminateDate($expiryDate);
  1528.                                         $emailTemplate 'email/user-notification/appraisal/conclude-fail.html.twig';
  1529.                                         $titleTemplate 'email.appraisal.concluded.fail.';
  1530.                                         break;
  1531.                                     default: // Extended
  1532.                                         $newExpiryDate date('Y-m-d'strtotime($conclusion == '1 month' '3 months'$expiryDate->getTimestamp()));
  1533.                                         $appraisal->getUser()->getPersonalInfo()->setJobExpiryDate(\DateTime::createFromFormat('Y-m-d'$newExpiryDate));
  1534.                                         //$previousExtendedProbation = $appraisal->getUser()->getPersonalInfo()->getExtendedProbation();
  1535.                                         //$appraisal->getUser()->getPersonalInfo()->setExtendedProbation($previousExtendedProbation + $conclusion);
  1536.                                         $emailTemplate 'email/user-notification/appraisal/conclude-extend.html.twig';
  1537.                                         $titleTemplate 'email.appraisal.concluded.extend.';
  1538.                                         $concludeAppraisal true;
  1539.                                         $notification->push($appraisal->getUser(), 'notification.appraisal.concluded.extend'$notificationData'i-star-blank-svg'$appraisal->getId());
  1540.                                         break;
  1541.                                 };
  1542.                                 $appraisal->setConcludedAt(new \DateTime());
  1543.                                 // Email     
  1544.                                 $mailgunService->sendEmail(
  1545.                                     $translator->trans($titleTemplate 'user', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName()]),
  1546.                                     [$appraisal->getUser()->getEmail()],
  1547.                                     $emailTemplate,
  1548.                                     [
  1549.                                         'recipient' => $appraisal->getUser(),
  1550.                                         'appraisal' => $appraisal,
  1551.                                         'group' => 'user',
  1552.                                         'conclusion' => $conclusion
  1553.                                     ]
  1554.                                 );
  1555.                                 foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1556.                                     $mailgunService->sendEmail(
  1557.                                         $translator->trans($titleTemplate 'manager', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName()]),
  1558.                                         [$manager->getEmail()],
  1559.                                         $emailTemplate,
  1560.                                         [
  1561.                                             'recipient' => $manager,
  1562.                                             'appraisal' => $appraisal,
  1563.                                             'group' => 'manager',
  1564.                                             'conclusion' => $conclusion
  1565.                                         ]
  1566.                                     );
  1567.                                 }
  1568.                                 foreach ($emailGroup as $groupName => $groupEmail) {
  1569.                                     if ($groupEmail != '') {
  1570.                                         $mailgunService->sendEmail(
  1571.                                             $translator->trans($titleTemplate $groupName, ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName()]),
  1572.                                             [$groupEmail],
  1573.                                             $emailTemplate,
  1574.                                             [
  1575.                                                 'recipient' => strtoupper($groupName) . ' Team',
  1576.                                                 'appraisal' => $appraisal,
  1577.                                                 'group' => $groupName,
  1578.                                                 'conclusion' => $conclusion
  1579.                                             ]
  1580.                                         );
  1581.                                     };
  1582.                                 };
  1583.                                 switch ($conclusion) {
  1584.                                     case 'permanent':
  1585.                                         $appraisal->getUser()->getPersonalInfo()->setJobStatus('form.job_status.permanent');
  1586.                                         break;
  1587.                                     case 'limited':
  1588.                                         $appraisal->getUser()->getPersonalInfo()->setJobStatus('form.job_status.time_limited');
  1589.                                         break;
  1590.                                     case 'temporary':
  1591.                                         $appraisal->getUser()->getPersonalInfo()->setJobStatus('form.job_status.temporary');
  1592.                                         break;
  1593.                                 };
  1594.                             } elseif (in_array($currentStep, [45]) == true && $appraisal->getUser()->inProbation() == false && $appraisal->getAdHoc() == false) { // Permanent User
  1595.                                 //$expiryDate = $appraisal->getUser()->getPersonalInfo()->getJobExpiryDate();
  1596.                                 //$newExpiryDate = $expiryDate ? date('Y-m-d', strtotime('6 months', $expiryDate->getTimestamp())) : date('Y-m-d');
  1597.                                 /*$newExpiryDate = $userService->nextReviewDate();
  1598.                                 $appraisal->getUser()->getPersonalInfo()->setJobExpiryDate(\DateTime::createFromFormat('Y-m-d', $newExpiryDate));*/
  1599.                                 // $oldData = clone $appraisal->getUser()->getPersonalInfo();
  1600.                              
  1601.                                 if($appraisal->getUser()->getManager() == $user){
  1602.                                     $appraisal->setConcludedAt(new \DateTime());
  1603.                                     $concludeAppraisal true;
  1604.                                 }
  1605.                             } elseif ($appraisal->getAdHoc()) {
  1606.                                 // $oldData = clone $appraisal->getUser()->getPersonalInfo();
  1607.                                 
  1608.                                 $appraisal->setConcludedAt(new \DateTime());
  1609.                                 $concludeAppraisal true;
  1610.                             }
  1611.                             if ($summary) {
  1612.                                 //$updateSummary = $userNote == null ? null : ' (update)';
  1613.                                 $updateSummary null;
  1614.                                 if ($appraisal->getAdHoc()) {
  1615.                                     $titleTemplate 'email.appraisal.ad_hoc.';
  1616.                                     $emailTemplate 'email/user-notification/appraisal/summary-ad-hoc.html.twig';
  1617.                                 } else {
  1618.                                     $titleTemplate 'email.appraisal.summary.';
  1619.                                     $emailTemplate 'email/user-notification/appraisal/summary.html.twig';
  1620.                                 };
  1621.                                 //Summary Copy
  1622.                                 $mailgunService->sendEmail(
  1623.                                     $translator->trans($titleTemplate 'user', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%date%' => $appraisal->getReviewAt()->format('d F Y')]) . $updateSummary,
  1624.                                     [$appraisal->getUser()->getEmail()],
  1625.                                     $emailTemplate,
  1626.                                     [
  1627.                                         'recipient' => $appraisal->getUser(),
  1628.                                         'appraisal' => $appraisal,
  1629.                                         'summary' => $summary,
  1630.                                         'group' => 'user'
  1631.                                     ]
  1632.                                 );
  1633.                                 //$notification->push($appraisal->getUser(),'notification.objective.invitation.user', $notificationData, 'i-review', $appraisal->getId());
  1634.                                 foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1635.                                     $mailgunService->sendEmail(
  1636.                                         $translator->trans($titleTemplate 'manager', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFullName(), '%manager%' => $manager->getPersonalInfo()->getFirstName(), '%date%' => $appraisal->getReviewAt()->format('d F Y')]) . $updateSummary,
  1637.                                         [$manager->getEmail()],
  1638.                                         $emailTemplate,
  1639.                                         [
  1640.                                             'recipient' => $manager,
  1641.                                             'appraisal' => $appraisal,
  1642.                                             'summary' => $summary,
  1643.                                             'group' => 'manager'
  1644.                                         ]
  1645.                                     );
  1646.                                     //$notification->push($manager,'notification.objective.invitation.manager', $notificationData, 'i-review', $appraisal->getId());
  1647.                                 }
  1648.                                 foreach ($emailGroup as $groupName => $groupEmail) {
  1649.                                     if ($appraisal->getUser()->inProbation() == false && $groupName == 'finance') continue;
  1650.                                     $mailgunService->sendEmail(
  1651.                                         $translator->trans($titleTemplate $groupName, ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFullName(), '%date%' => $appraisal->getReviewAt()->format('d F Y')]) . $updateSummary,
  1652.                                         [$groupEmail],
  1653.                                         $emailTemplate,
  1654.                                         [
  1655.                                             'recipient' => strtoupper($groupName) . ' Team',
  1656.                                             'appraisal' => $appraisal,
  1657.                                             'summary' => $summary,
  1658.                                             'group' => $groupName,
  1659.                                         ]
  1660.                                     );
  1661.                                 };
  1662.                             };
  1663.                             //Objective Invitation
  1664.                             /*
  1665.                             if ($objectiveInvitation) {
  1666.                                 $notificationData = [
  1667.                                     '%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1668.                                     '%link%' => '<a href="' . $this->generateURL('user_profile') . '/' . $appraisal->getUser()->getEmail() . '?g=objective">Objectives</a>'
  1669.                                 ];
  1670.                                 $mailgunService->sendEmail(
  1671.                                     $translator->trans('email.objective.invitation.user', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName()]),
  1672.                                     [$appraisal->getUser()->getEmail()],
  1673.                                     $objectiveEmailTemplate,
  1674.                                     [
  1675.                                         'recipient' => $appraisal->getUser(),
  1676.                                         'appraisal' => $appraisal,
  1677.                                         'group' => 'user'
  1678.                                     ]
  1679.                                 );
  1680.                                 $notification->push($appraisal->getUser(), 'notification.objective.invitation.user', $notificationData, 'i-review', $appraisal->getId());
  1681.                                 foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1682.                                     $mailgunService->sendEmail(
  1683.                                         $translator->trans('email.objective.invitation.manager', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName()]),
  1684.                                         [$manager->getEmail()],
  1685.                                         $objectiveEmailTemplate,
  1686.                                         [
  1687.                                             'recipient' => $manager,
  1688.                                             'appraisal' => $appraisal,
  1689.                                             'group' => 'manager'
  1690.                                         ]
  1691.                                     );
  1692.                                     $notification->push($manager, 'notification.objective.invitation.manager', $notificationData, 'i-review', $appraisal->getId());
  1693.                                 }
  1694.                                 foreach ($emailGroup as $groupName => $groupEmail) {
  1695.                                     if ($groupName == 'hr') {
  1696.                                         $mailgunService->sendEmail(
  1697.                                             $translator->trans('email.objective.invitation.hr', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%manager%' => $appraisal->getUser()->getManager()->getPersonalInfo()->getFirstName()]),
  1698.                                             [$groupEmail],
  1699.                                             $objectiveEmailTemplate,
  1700.                                             [
  1701.                                                 'recipient' => strtoupper($groupName) . ' Team',
  1702.                                                 'appraisal' => $appraisal,
  1703.                                                 'group' => $groupName
  1704.                                             ]
  1705.                                         );
  1706.                                     }
  1707.                                 };
  1708.                             }*/
  1709.                         };
  1710.                     };
  1711.                 };
  1712.                 // Only proceed if $user not yet in "reviewed" table or if it for amend purpose
  1713.                 if (!$appraisal->hasReviewed($user)){
  1714.                     foreach ($assessments as $assessment) {
  1715.                         try {
  1716.                             $comment $assessment->appraisalCommentsByOwner($_id$user->getId());
  1717.                             if ($comment != null) {
  1718.                                 $_comment = clone $comment;
  1719.                                 array_push($oldData$_comment);
  1720.                                 if ($request->request->get('score-' $assessment->getId()) != null$comment->setScore($request->request->get('score-' $assessment->getId()));
  1721.                                 if ($request->request->get('comment-' $assessment->getId()) != null$comment->setComment($request->request->get('comment-' $assessment->getId()));
  1722.                                 $comment->setDraft($request->request->get('draft') == "false" false true);
  1723.                                 $comment->setUpdatedAt(new \DateTime());
  1724.                                 if ($user == $appraisal->getUser())
  1725.                                     $comment->setFormContent($serializer->serialize($serializer->normalize($assessmentnull, ['groups' => 'Appraisal']), 'json'));
  1726.                             } else {
  1727.                                 $comment = new AppraisalComment();
  1728.                                 if ($request->request->get('score-' $assessment->getId()) != null$comment->setScore($request->request->get('score-' $assessment->getId()));
  1729.                                 if ($request->request->get('comment-' $assessment->getId()) != null$comment->setComment($request->request->get('comment-' $assessment->getId()));
  1730.                                 $comment->setForm($assessment);
  1731.                                 $comment->setAppraisal($appraisal);
  1732.                                 $comment->setDraft($request->request->get('draft') == "false" false true);
  1733.                                 $comment->setCreatedAt(new \DateTime());
  1734.                                 if ($user == $appraisal->getUser()) {
  1735.                                     $comment->setUser($appraisal->getUser());
  1736.                                     $comment->setType('submit');
  1737.                                 } else {
  1738.                                     $comment->setUser($user);
  1739.                                     $comment->setType('review');
  1740.                                 };
  1741.                                 if ($user == $appraisal->getUser())
  1742.                                     $comment->setFormContent($serializer->serialize($serializer->normalize($assessmentnull, ['groups' => 'Appraisal']), 'json'));
  1743.                                 $entityManager->persist($comment);
  1744.                                 //$oldData = null;
  1745.                             }
  1746.                             array_push($newData$comment);
  1747.                         } catch (Throwable $t) {
  1748.                             continue;
  1749.                         }
  1750.                     }
  1751.                     if ($user == $appraisal->getUser() && $request->request->get('draft') == 'false') {
  1752.                         $appraisal->setSubmittedAt(new \DateTime());
  1753.                         if ($appraisal->getUser()->inProbation()) {
  1754.                             $emailTitle 'email.appraisal.submitted.probation.';
  1755.                             $emailTemplate 'email/user-notification/appraisal/submitted-probation.html.twig';
  1756.                             $titleNotification 'notification.appraisal.submitted.probation.';
  1757.                             $notificationLinkTitle 'Appraisal (Probation) Form';
  1758.                         } else {
  1759.                             $emailTitle 'email.appraisal.submitted.default.';
  1760.                             $emailTemplate 'email/user-notification/appraisal/submitted-default.html.twig';
  1761.                             $titleNotification 'notification.appraisal.submitted.default.';
  1762.                             $notificationLinkTitle 'Appraisal Form';
  1763.                         };
  1764.                         $notificationData = [
  1765.                             '%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(),
  1766.                             '%link%' => '<a href="' $this->generateURL('submit_appraisal') . '/' $appraisal->getId() . '">' $notificationLinkTitle '</a>',
  1767.                         ];
  1768.                         $mailgunService->sendEmail(
  1769.                             $translator->trans($emailTitle 'user', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName()]),
  1770.                             [$appraisal->getUser()->getEmail()],
  1771.                             $emailTemplate,
  1772.                             [
  1773.                                 'recipient' => $appraisal->getUser(),
  1774.                                 'appraisal' => $appraisal,
  1775.                                 'group' => 'user'
  1776.                             ],
  1777.                             $loggedIn
  1778.                         );
  1779.                         $notification->push($appraisal->getUser(), $titleNotification 'user'$notificationData'i-review'$appraisal->getId());
  1780.                         foreach ($appraisal->getUser()->getAllManager() as $manager) {
  1781.                             $mailgunService->sendEmail(
  1782.                                 $translator->trans($emailTitle 'manager', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%manager%' => $manager->getPersonalInfo()->getFirstName()]),
  1783.                                 [$manager->getEmail()],
  1784.                                 $emailTemplate,
  1785.                                 [
  1786.                                     'recipient' => $manager,
  1787.                                     'appraisal' => $appraisal,
  1788.                                     'group' => 'manager'
  1789.                                 ],
  1790.                                 $loggedIn
  1791.                             );
  1792.                             $notification->push($manager$titleNotification 'manager'$notificationData'i-review'$appraisal->getId());
  1793.                         }
  1794.                         $emailGroup $this->getParameter('systemEmailGroup');
  1795.                         foreach ($emailGroup as $groupName => $groupEmail) {
  1796.                             try {
  1797.                                 if ($groupName == 'hr') {
  1798.                                     $mailgunService->sendEmail(
  1799.                                         $translator->trans($emailTitle 'hr', ['%user%' => $appraisal->getUser()->getPersonalInfo()->getFirstName(), '%manager%' => $appraisal->getUser()->getManager()->getPersonalInfo()->getFirstName()]),
  1800.                                         [$groupEmail],
  1801.                                         $emailTemplate,
  1802.                                         [
  1803.                                             'recipient' => strtoupper($groupName) . ' Team',
  1804.                                             'appraisal' => $appraisal,
  1805.                                             'group' => $groupName,
  1806.                                         ],
  1807.                                         $loggedIn
  1808.                                     );
  1809.                                 };
  1810.                             } catch (Exception $e) {
  1811.                             }
  1812.                         };
  1813.                         /*
  1814.                         if ($_SERVER['NOTIF_SUPER_ADMIN'] == 'true') {
  1815.                             foreach ($user->assignedCompany()->superAdmins($user) as $superAdmin) {
  1816.                                 if ($superAdmin != $user && !in_array($superAdmin, $appraisal->getUser()->getAllManager())) {
  1817.                                     $mailgunService->sendEmail(
  1818.                                         $translator->trans('email.system.copy_super_admin').' '.$subject,
  1819.                                         [$superAdmin->getEmail()],
  1820.                                         'email/user-notification/appraisal-review.html.twig',
  1821.                                         [
  1822.                                             'user' => $appraisal->getUser(),
  1823.                                             'user_creator' => $user,
  1824.                                             'user_cc' => $superAdmin,
  1825.                                             'appraisal' => $appraisal,
  1826.                                         ]
  1827.                                     );
  1828.                                 };
  1829.                             }
  1830.                         };*/
  1831.                     } elseif ($request->request->get('draft') == 'false') {
  1832.                         /*
  1833.                         $mailgunService->scheduleEmail([
  1834.                             'subject' => $appraisal->getTitle() . ' | ' .$appraisal->getUser()->getPersonalInfo()->getFirstName(). ' | '.$translator->trans('email.appraisal.result'),
  1835.                             'to' => [$appraisal->getUser()->getEmail()],
  1836.                             'template' => 'email/user-notification/appraisal-result.html.twig',
  1837.                             'params' => [
  1838.                                 'user' => $appraisal->getUser(),
  1839.                                 'appraisal' => $appraisal,
  1840.                             ],
  1841.                         ], $appraisal->getReviewAt(), $user, 'Appraisal', $appraisal->getId());*/
  1842.                         if ($appraisal->getUser()->getManager() != $user && $appraisal->getUser()->getisIncludedManager($user)) {
  1843.                             // $notification->push($appraisal->getUser(), [
  1844.                             //     'message' => 'notification.appraisal.reviewed_alt',
  1845.                             //     'entity' => 'Appraisal',
  1846.                             //     'objectId' => $appraisal->getId(),
  1847.                             //     'data' => $serializer->serialize([
  1848.                             //         'title' => $appraisal->getTitle(),
  1849.                             //         'url' => $this->generateUrl('submit_appraisal') . '/' . $appraisal->getId(),
  1850.                             //         'manager' => $user->getPersonalInfo()->getFirstName()
  1851.                             //     ], 'json')
  1852.                             // ], true);
  1853.                             /*
  1854.                             $subject =  $appraisal->getTitle() . ' | ' . $appraisal->getUser()->getPersonalInfo()->getFirstName() . ' | ' . $translator->trans('email.appraisal.result');
  1855.                             $mailgunService->sendEmail(
  1856.                                 $subject,
  1857.                                 [$appraisal->getUser()->getEmail()],
  1858.                                 'email/user-notification/appraisal-review.html.twig',
  1859.                                 [
  1860.                                     'recipient' => $appraisal->getUser(),
  1861.                                     'sender' => $user,
  1862.                                     'appraisal' => $appraisal,
  1863.                                 ]
  1864.                             );*/
  1865.                         }
  1866.                     }
  1867.                 }
  1868.                 if ($user != $appraisal->getUser() && $request->request->get('draft') == "false" ){
  1869.                     // if($currentStep == 2) $appraisal->setReviewedBy($user);
  1870.                     $appraisal->addReviewer($user);
  1871.                 } 
  1872.                 $entityManager->flush();
  1873.                 $entityManager->refresh($appraisal);
  1874.                 $appraisalService->syncAppraisalStatus($appraisal->getUser());
  1875.              
  1876.                 //$leaveService->leavePermanent($appraisal->getUser(),false,false,null,$permanent);
  1877.                 if ($concludeAppraisal && $appraisal->getUser()->latestAppraisal($appraisal)) {
  1878.                     /*
  1879.                     $inProbation = $appraisal->getUser()->inProbation();
  1880.                     $nextTitleDate = $inProbation ? '' : ucfirst(date('M'));
  1881.                     $appraisalService->initiateAppraisal($request, $user, $appraisal->getUser(), $translator->trans($inProbation ? 'page.appraisal.title.list.probation' : 'page.appraisal.title.list.objective').' '.$nextTitleDate, $appraisal->getUser()->getPersonalInfo()->getJobExpiryDate()->modify('- 1 weeks'), null, null, null);
  1882.                     */
  1883.                 };
  1884.                 if ($request->request->get('draft') == 'false') {
  1885.                     if ($user == $appraisal->getUser()) {
  1886.                         $this->addFlash(
  1887.                             'success',
  1888.                             'messages.appraisal.submit'
  1889.                         );
  1890.                         $log->save($user, [
  1891.                             'owner' => $appraisal->getUser(),
  1892.                             'message' => 'log.appraisal.submit',
  1893.                             'old_data' => $oldData,
  1894.                             'new_data' => $newData
  1895.                         ]);
  1896.                     } else {
  1897.                         if ($request->request->get('note') == 'true') {
  1898.                             if ($appraisal->getAdHoc()) {
  1899.                                 $this->addFlash(
  1900.                                     'success',
  1901.                                     'messages.appraisal.ad_hoc'
  1902.                                 );
  1903.                                 $log->save($user, [
  1904.                                     'owner' => $appraisal->getUser(),
  1905.                                     'message' => 'log.appraisal.ad_hoc.update',
  1906.                                     'old_data' => [],
  1907.                                     'new_data' => $appraisal->getUser()->getPersonalInfo()
  1908.                                 ]);
  1909.                             } else if ($request->request->get('conclusion') || $concludeAppraisal) {
  1910.                                 $this->addFlash(
  1911.                                     'success',
  1912.                                     'messages.appraisal.conclude'
  1913.                                 );
  1914.                                 $log->save($user, [
  1915.                                     'owner' => $appraisal->getUser(),
  1916.                                     'message' => 'log.appraisal.conclude',
  1917.                                     'old_data' => [],
  1918.                                     'new_data' => $appraisal->getUser()->getPersonalInfo()
  1919.                                 ]);
  1920.                             } else {
  1921.                                 $this->addFlash(
  1922.                                     'success',
  1923.                                     'messages.appraisal.summary.save'
  1924.                                 );
  1925.                                 $log->save($user, [
  1926.                                     'owner' => $appraisal->getUser(),
  1927.                                     'message' => $oldUserAppraisalNote 'log.appraisal.note.update' 'log.appraisal.note.add',
  1928.                                     'old_data' => $oldUserAppraisalNote,
  1929.                                     'new_data' => $userAppraisalNote
  1930.                                 ]);
  1931.                             }
  1932.                         } else {
  1933.                             $this->addFlash(
  1934.                                 'success',
  1935.                                 'messages.appraisal.review'
  1936.                             );
  1937.                             $log->save($user, [
  1938.                                 'owner' => $appraisal->getUser(),
  1939.                                 'message' => 'log.appraisal.review',
  1940.                                 'old_data' => $oldData,
  1941.                                 'new_data' => $newData
  1942.                             ]);
  1943.                         }
  1944.                     }
  1945.                     /* $log->save($user, [
  1946.                         'owner' => $appraisal->getUser(),
  1947.                         'message' => $logDesc,
  1948.                         'old_data' => $oldData,
  1949.                         'new_data' => $newData
  1950.                     ], false, true);*/
  1951.                 } else if ($user != $appraisal->getUser() && $request->request->get('note') == 'true' && $userAppraisalNote->getDraft()) {
  1952.                     $userAppraisalNote->setDraft(false);
  1953.                     $entityManager->flush();
  1954.                     $this->addFlash(
  1955.                         'success',
  1956.                         'messages.appraisal.summary.submit'
  1957.                     );
  1958.                     // $log->save($user, [
  1959.                     //     'owner' => $appraisal->getUser(),
  1960.                     //     'message' => 'log.appraisal.review',
  1961.                     //     'old_data' => $oldData,
  1962.                     //     'new_data' => $newData
  1963.                     // ]);
  1964.                 } else {
  1965.                     $log->save($user, [
  1966.                         'owner' => $appraisal->getUser(),
  1967.                         'message' => 'log.appraisal.draft',
  1968.                         'old_data' => $oldData,
  1969.                         'new_data' => $newData
  1970.                     ]);
  1971.                     $this->addFlash(
  1972.                         'success',
  1973.                         'messages.appraisal.draft'
  1974.                     );
  1975.                 }
  1976.                 return $this->redirect($request->getUri());
  1977.             };
  1978.             return $this->render('private/appraisal/appraisal.html.twig', [
  1979.                 'appraisal' => $appraisal,
  1980.                 'assessments' => $assessments,
  1981.                 'categorizedAssessments' => $categorizedAssessments,
  1982.                 'appraisalNotes' => $appraisalNotes,
  1983.                 'appraisalNoteForm' => $appraisalNoteForm->createView()
  1984.                 //'formComments' => $formComments
  1985.                 //'form' => $form->createView()
  1986.             ]);
  1987.         } else {
  1988.             throw new \Exception($translator->trans('messages.security.forbidden'));
  1989.             //throw $this->createNotFoundException($translator->trans('messages.error.not_found'));
  1990.         }
  1991.     }
  1992.     #[Route(path'/ajax/save-appraisal'name'ajax_save_appraisal')]
  1993.     public function saveAppraisal(Request $requestTranslatorInterface $translatorSerializerInterface $serializer): JsonResponse
  1994.     {
  1995.         /*if (!$this->isGranted('ROLE_STAFF')) {
  1996.             return new JsonResponse([
  1997.                 'status' => "TIMEOUT",
  1998.             ]);
  1999.         }*/
  2000.         $_id $request->request->get('appraisalId');
  2001.         $user $this->getUser();
  2002.         $appraisal =  $this->getDoctrine()->getRepository(Appraisal::class)->find($_id);
  2003.         if ($appraisal == null) {
  2004.             return $this->redirectToRoute('user_profile', ['g' => 'appraisal']);
  2005.         };
  2006.         $oldData = [];
  2007.         $newData = [];
  2008.         $userNote null;
  2009.         $appraisalNotes null;
  2010.         $userAppraisalNote = new AppraisalNote();
  2011.         if ($appraisal->getAppraisalNotes() != null and count($appraisal->getAppraisalNotes()) > 0) {
  2012.             $appraisalNotes $this->getDoctrine()->getRepository(AppraisalNote::class)->findByAppraisal($appraisal);
  2013.             $userNote $this->getDoctrine()->getRepository(AppraisalNote::class)->findByAppraisalByUser($appraisal$user);
  2014.             if ($userNote != null) {
  2015.                 $userAppraisalNote $userNote;
  2016.             }
  2017.         };
  2018.         $oldUserNote is_object($userNote) ? clone $userNote null;
  2019.         $appraisalNoteForm $this->createForm(AppraisalNoteType::class, $userAppraisalNote);
  2020.         $result['status'] = 'OK';
  2021.         if ($this->isGranted('ROLE_HR') || $appraisal->getUser() == $user || $appraisal->getUser()->getIsIncludedManager($user)) {
  2022.             if ($_SERVER['APP_ENV'] == "prod" && $appraisal->getUser()->inProbation() && $appraisal->getSubmitAt()->format('Ymd') < '20221001') {
  2023.                 $assessments $this->getDoctrine()->getRepository(AppraisalForm::class)->findByCODA($appraisal->getUser()->assignedCompany(), $appraisal->getUser()->getOffice(), $appraisal->getUser()->organizationDepartments(), $appraisal);
  2024.             } else {
  2025.                 $assessments $this->getDoctrine()->getRepository(AppraisalForm::class)->findByType($appraisal->getUser()->assignedCompany(), $appraisal->getUser()->appraisalType($appraisal), $appraisal);
  2026.             }
  2027.             if ($request->isMethod('post')) {
  2028.                 $entityManager $this->getDoctrine()->getManager();
  2029.                 if (!$appraisal->hasReviewed($user)) {
  2030.                     foreach ($assessments as $assessment) {
  2031.                         $comment $assessment->appraisalCommentsByOwner($_id$user->getId());
  2032.                         if ($comment != null) {
  2033.                             $_comment = clone $comment;
  2034.                             array_push($oldData$_comment);
  2035.                             if ($request->request->get('score-' $assessment->getId()) != null$comment->setScore($request->request->get('score-' $assessment->getId()));
  2036.                             if ($request->request->get('comment-' $assessment->getId()) != null$comment->setComment($request->request->get('comment-' $assessment->getId()));
  2037.                             $comment->setDraft($request->request->get('draft') == "false" false true);
  2038.                             $comment->setUpdatedAt(new \DateTime());
  2039.                             if ($user == $appraisal->getUser())
  2040.                                 $comment->setFormContent($serializer->serialize($serializer->normalize($assessmentnull, ['groups' => 'Appraisal']), 'json'));
  2041.                         } else {
  2042.                             $comment = new AppraisalComment();
  2043.                             if ($request->request->get('score-' $assessment->getId()) != null$comment->setScore($request->request->get('score-' $assessment->getId()));
  2044.                             if ($request->request->get('comment-' $assessment->getId()) != null$comment->setComment($request->request->get('comment-' $assessment->getId()));
  2045.                             $comment->setForm($assessment);
  2046.                             $comment->setAppraisal($appraisal);
  2047.                             $comment->setDraft($request->request->get('draft') == "false" false true);
  2048.                             $comment->setCreatedAt(new \DateTime());
  2049.                             if ($user == $appraisal->getUser()) {
  2050.                                 $comment->setUser($appraisal->getUser());
  2051.                                 $comment->setType('submit');
  2052.                             } else {
  2053.                                 $comment->setUser($user);
  2054.                                 $comment->setType('review');
  2055.                             };
  2056.                             if ($user == $appraisal->getUser())
  2057.                                 $comment->setFormContent($serializer->serialize($serializer->normalize($assessmentnull, ['groups' => 'Appraisal']), 'json'));
  2058.                             $entityManager->persist($comment);
  2059.                             //$oldData = null;
  2060.                         }
  2061.                         array_push($newData$comment);
  2062.                     }
  2063.                 }
  2064.                 if ($user != $appraisal->getUser()) {
  2065.                     $requestNote $request->request->get('appraisal_note');
  2066.                    
  2067.                     if ($requestNote != '') {
  2068.                         $userAppraisalNote->setNote($requestNote['note']);
  2069.                         $userAppraisalNote->setDraft($request->request->get('draft') == "false" false true);
  2070.                         if ($userNote == null) {
  2071.                             $userAppraisalNote->setAppraisal($appraisal);
  2072.                             $userAppraisalNote->setUser($user);
  2073.                             $userAppraisalNote->setCreatedAt(new \DateTime());
  2074.                             $entityManager->persist($userAppraisalNote);
  2075.                         } else {
  2076.                             $userAppraisalNote->setUpdatedAt(new \DateTime());
  2077.                         }
  2078.                     };
  2079.                 };
  2080.             }
  2081.             $entityManager->flush();
  2082.         } else {
  2083.             $result['status'] = 'ERROR';
  2084.         }
  2085.         return new JsonResponse($result);
  2086.     }
  2087.     #[Route(path'/ajax/save-appraisal-field'name'ajax_save_appraisal_field')]
  2088.     public function saveAppraisalField(Request $requestTranslatorInterface $translatorSerializerInterface $serializer): JsonResponse
  2089.     {
  2090.         /*if (!$this->isGranted('ROLE_STAFF')) {
  2091.             return new JsonResponse([
  2092.                 'status' => "TIMEOUT",
  2093.             ]);
  2094.         }*/
  2095.         $appraisalId $request->request->get('appraisalId');
  2096.         $appraisalCommentValue $request->request->get('comment');
  2097.         $appraisalScoreValue $request->request->get('score');
  2098.         $formId $request->request->get('formId');
  2099.         if($appraisalId == null || $formId == null){
  2100.             return new JsonResponse([
  2101.                 'status' => "ID Missing",
  2102.             ]);
  2103.         }
  2104.         $user $this->getUser();
  2105.         $appraisal $this->getDoctrine()->getRepository(Appraisal::class)->find($appraisalId);
  2106.         $appraisalForm $this->getDoctrine()->getRepository(AppraisalForm::class)->find($formId);
  2107.         $appraisalComment $this->getDoctrine()->getRepository(AppraisalComment::class)->findByAppraisalForm($appraisalId$formId$user);
  2108.         
  2109.         if($appraisalComment == null){
  2110.             $appraisalComment = new AppraisalComment();
  2111.         };
  2112.         $result['status'] = 'OK';
  2113.         if ($appraisalId && ($this->isGranted('ROLE_HR') || $appraisal->getUser() == $user || $appraisal->getUser()->getIsIncludedManager($user))) {
  2114.            
  2115.             $entityManager $this->getDoctrine()->getManager(); 
  2116.            
  2117.             if($appraisalScoreValue$appraisalComment->setScore($appraisalScoreValue);
  2118.             if($appraisalCommentValue$appraisalComment->setComment($appraisalCommentValue);
  2119.             $appraisalComment->setFormContent($serializer->serialize($serializer->normalize($appraisalFormnull, ['groups' => 'Appraisal']), 'json'));
  2120.             
  2121.             if($appraisalComment->getId() == null){
  2122.                 if ($user == $appraisal->getUser()) {
  2123.                     $appraisalComment->setUser($appraisal->getUser());
  2124.                     $appraisalComment->setType('submit');
  2125.                 } else {
  2126.                     $appraisalComment->setUser($user);
  2127.                     $appraisalComment->setType('review');
  2128.                 };
  2129.                 $appraisalComment->setAppraisal($appraisal);
  2130.                 $appraisalComment->setForm($appraisalForm);
  2131.                 $appraisalComment->setDraft(true);
  2132.                 $appraisalComment->setCreatedAt(new \DateTime());
  2133.                 $entityManager->persist($appraisalComment);
  2134.             } else {
  2135.                 $appraisalComment->setUpdatedAt(new \DateTime());
  2136.             }
  2137.             
  2138.             $entityManager->flush();
  2139.             if($appraisalComment->getDraft() == false && $appraisalScoreValue){
  2140.                 $result['questionScore'] = $appraisal->appraisalQuestionScore($appraisalComment);
  2141.                 $result['appraisalScore'] = $appraisal->appraisalScore();
  2142.             }
  2143.         } else {
  2144.             $result['status'] = 'ERROR';
  2145.         }
  2146.         return new JsonResponse($result);
  2147.     }
  2148.     #[Route(path'/ajax/add-appraisal'name'ajax_add_appraisal')]
  2149.     public function ajaxAddAppraisal(Request $requestTranslatorInterface $translator): JsonResponse
  2150.     {
  2151.         $user $this->getUser();
  2152.         $appraisal = new AppraisalForm();
  2153.         $form $this->createForm(AppraisalType::class, $appraisal);
  2154.         if ($this->isGranted('ROLE_ACCESS_ALL_OFFICE')) {
  2155.             $office_choices['form.office.all'] = null;
  2156.             foreach ($user->assignedOffices() as $office) {
  2157.                 $office_choices[$office->getFullName()] = $office->getId();
  2158.             };
  2159.             $form->add('office',  ChoiceType::class, [
  2160.                 'mapped' => false,
  2161.                 'label' => 'Office',
  2162.                 'choices' => $office_choices,
  2163.                 'placeholder' => 'form.placeholder.choice'
  2164.             ]);
  2165.         };
  2166.         $department_choices['form.department.all'] = null;
  2167.         foreach ($user->assignedCompany()->getDepartments() as $department) {
  2168.             $department_choices[$department->getName()] = $department->getId();
  2169.         };
  2170.         $form->add('department',  ChoiceType::class, [
  2171.             'mapped' => false,
  2172.             'label' => 'Department',
  2173.             'choices' => $department_choices,
  2174.             'placeholder' => 'form.placeholder.choice'
  2175.         ]);
  2176.         $form->handleRequest($request);
  2177.         $result['status'] = 'OK';
  2178.         if ($form->isSubmitted() && !$form->isValid() && $result['status'] == 'OK') {
  2179.             $result['status'] = 'ERROR';
  2180.             foreach ($form->getErrors(true) as $key => $error) {
  2181.                 $result['message'][$key] = $error->getMessage();
  2182.             }
  2183.         };
  2184.         if ($result['status'] == 'OK') {
  2185.             $appraisal->setCreatedBy($user);
  2186.             $appraisal->setCreatedAt(new \DateTime());
  2187.             $appraisal->setCompany($user->assignedCompany());
  2188.             if ($this->isGranted('ROLE_ACCESS_ALL_OFFICE')) {
  2189.                 if ($form->get('office')->getData() != null) {
  2190.                     $office $this->getDoctrine()->getRepository(Office::class)->find($form->get('office')->getData());
  2191.                     $appraisal->setOffice($office);
  2192.                 } else {
  2193.                     $appraisal->setOffice(null);
  2194.                 }
  2195.             } else {
  2196.                 $appraisal->setOffice($user->getOffice());
  2197.             }
  2198.             if ($form->get('department')->getData() != null) {
  2199.                 $department $this->getDoctrine()->getRepository(Department::class)->find($form->get('department')->getData());
  2200.                 $appraisal->setDepartment($department);
  2201.             } else {
  2202.                 $appraisal->setDepartment(null);
  2203.             }
  2204.             $entityManager $this->getDoctrine()->getManager();
  2205.             $entityManager->persist($appraisal);
  2206.             $entityManager->flush();
  2207.             $result['content'] = $this->renderView('private/appraisal/components/edit-appraisal-list.html.twig', [
  2208.                 'appraisal' => $appraisal,
  2209.                 'waitForPage' => false
  2210.             ]);
  2211.         };
  2212.         return new JsonResponse($result);
  2213.     }
  2214.     #[Route(path'/ajax/edit-appraisal'name'ajax_edit_appraisal')]
  2215.     public function ajaxEditAppraisal(Request $requestTranslatorInterface $translator): \Symfony\Component\HttpFoundation\Response
  2216.     {
  2217.         $user $this->getUser();
  2218.         $appraisal $this->getDoctrine()->getRepository(AppraisalForm::class)->find($request->get("id"));
  2219.         $formAppraisal $this->createForm(AppraisalType::class, $appraisal);
  2220.         if ($this->isGranted('ROLE_ACCESS_ALL_OFFICE')) {
  2221.             $office_choices['form.office.all'] = null;
  2222.             foreach ($user->assignedOffices() as $office) {
  2223.                 $office_choices[$office->getFullName()] = $office->getId();
  2224.             };
  2225.             $formAppraisal->add('office',  ChoiceType::class, [
  2226.                 'mapped' => false,
  2227.                 'label' => 'Office',
  2228.                 'choices' => $office_choices,
  2229.                 'placeholder' => 'form.placeholder.choice',
  2230.                 'data' => $appraisal->getOffice() ? $appraisal->getOffice()->getId() : null,
  2231.             ]);
  2232.         };
  2233.         $department_choices['form.department.all'] = null;
  2234.         foreach ($user->assignedCompany()->getDepartments() as $department) {
  2235.             $department_choices[$department->getName()] = $department->getId();
  2236.         };
  2237.         $formAppraisal->add('department',  ChoiceType::class, [
  2238.             'mapped' => false,
  2239.             'label' => 'Department',
  2240.             'choices' => $department_choices,
  2241.             'placeholder' => 'form.placeholder.choice',
  2242.             'data' => $appraisal->getDepartment() ? $appraisal->getDepartment()->getId() : null,
  2243.         ]);
  2244.         return $this->render('private/appraisal/components/form-appraisal.html.twig', [
  2245.             'dataId' => $appraisal->getId(),
  2246.             'formAppraisal' => $formAppraisal->createView()
  2247.         ]);
  2248.     }
  2249.     #[Route(path'/ajax/update-appraisal'name'ajax_update_appraisal'methods'POST')]
  2250.     function ajaxUpdateAppraisal(Request $requestTranslatorInterface $translator)
  2251.     {
  2252.         $user $this->getUser();
  2253.         $appraisal $this->getDoctrine()->getRepository(AppraisalForm::class)->find($request->get("dataId"));
  2254.         $result['status'] = 'OK';
  2255.         if ($appraisal != null) {
  2256.             // if ($user->assignedCompany() != $appraisal->getCompany() || !$this->isGranted('ROLE_HR')) {
  2257.             //     $result['status'] = 'ERROR';
  2258.             //     $result['message'] = $translator->trans('messages.security.forbidden');
  2259.             // };
  2260.             $form $this->createForm(AppraisalType::class, $appraisal);
  2261.             if ($this->isGranted('ROLE_ACCESS_ALL_OFFICE')) {
  2262.                 $office_choices['form.office.all'] = null;
  2263.                 foreach ($user->assignedOffices() as $office) {
  2264.                     $office_choices[$office->getFullName()] = $office->getId();
  2265.                 };
  2266.                 $form->add('office',  ChoiceType::class, [
  2267.                     'mapped' => false,
  2268.                     'label' => 'Office',
  2269.                     'choices' => $office_choices,
  2270.                     'placeholder' => 'form.placeholder.choice'
  2271.                 ]);
  2272.             };
  2273.             $department_choices['form.department.all'] = null;
  2274.             foreach ($user->assignedCompany()->getDepartments() as $department) {
  2275.                 $department_choices[$department->getName()] = $department->getId();
  2276.             };
  2277.             $form->add('department',  ChoiceType::class, [
  2278.                 'mapped' => false,
  2279.                 'label' => 'Department',
  2280.                 'choices' => $department_choices,
  2281.                 'placeholder' => 'form.placeholder.choice'
  2282.             ]);
  2283.             $form->handleRequest($request);
  2284.             if ($form->isSubmitted() && !$form->isValid() && $result['status'] == 'OK') {
  2285.                 $result['status'] = 'ERROR';
  2286.                 foreach ($form->getErrors(true) as $key => $error) {
  2287.                     $result['message'][$key] = $error->getMessage();
  2288.                 }
  2289.             };
  2290.             if ($result['status'] == 'OK') {
  2291.                 if ($this->isGranted('ROLE_ACCESS_ALL_OFFICE')) {
  2292.                     if ($form->get('office')->getData() != null) {
  2293.                         $office $this->getDoctrine()->getRepository(Office::class)->find($form->get('office')->getData());
  2294.                         $appraisal->setOffice($office);
  2295.                     } else {
  2296.                         $appraisal->setOffice(null);
  2297.                     }
  2298.                 } else {
  2299.                     $appraisal->setOffice($user->getOffice());
  2300.                 }
  2301.                 if ($form->get('department')->getData() != null) {
  2302.                     $department $this->getDoctrine()->getRepository(Department::class)->find($form->get('department')->getData());
  2303.                     $appraisal->setDepartment($department);
  2304.                 } else {
  2305.                     $appraisal->setDepartment(null);
  2306.                 }
  2307.                 $entityManager $this->getDoctrine()->getManager();
  2308.                 $entityManager->flush();
  2309.                 $result['id'] = $appraisal->getId();
  2310.                 $result['content'] = $this->renderView('private/appraisal/components/edit-appraisal-list.html.twig', [
  2311.                     'appraisal' => $appraisal,
  2312.                     'waitForPage' => false
  2313.                 ]);
  2314.                 $result['update'] = true;
  2315.             }
  2316.         } else {
  2317.             $result['status'] = 'ERROR';
  2318.             $result['message'] = $translator->trans('messages.request.missing');
  2319.         }
  2320.         return new JsonResponse($result);
  2321.     }
  2322.     #[Route(path'/ajax/delete-appraisal'name'ajax_delete_appraisal'methods'POST')]
  2323.     function ajaxDeleteAppraisal(Request $requestTranslatorInterface $translator): JsonResponse
  2324.     {
  2325.         $user $this->getUser();
  2326.         $appraisal =  $this->getDoctrine()->getRepository(AppraisalForm::class)->find($request->get("id"));
  2327.         $result['status'] = 'OK';
  2328.         if ($appraisal != null) {
  2329.             // if ($user->assignedCompany() != $appraisal->getCompany() || !$this->isGranted('ROLE_HR')) {
  2330.             //     $result['status'] = 'ERROR';
  2331.             //     $result['message'] = $translator->trans('messages.security.forbidden');
  2332.             // };
  2333.             if ($result['status'] == 'OK') {
  2334.                 $entityManager $this->getDoctrine()->getManager();
  2335.                 // The keep consistenty, the form will not be actually deleted if got some comments
  2336.                 if (count($appraisal->getAppraisalComments()) > 0) {
  2337.                     $appraisal->setIsHidden(true);
  2338.                 } else {
  2339.                     $entityManager->remove($appraisal);
  2340.                 }
  2341.                 $entityManager->remove($appraisal);
  2342.                 $entityManager->flush();
  2343.             }
  2344.         } else {
  2345.             $result['status'] = 'ERROR';
  2346.             $result['message'] = $translator->trans('messages.request.missing');
  2347.         }
  2348.         return new JsonResponse($result);
  2349.     }
  2350.     #[Route(path'/download/ics/appraisal/{token}.ics'name'download_ics_appraisal')]
  2351.     public function downloadAppraisalICS(Request $requestTranslatorInterface $translatorICSService $ics)
  2352.     {
  2353.         $user $this->getUser();
  2354.         $appraisal $this->getDoctrine()->getRepository(Appraisal::class)->findByToken($request->get("token"));
  2355.         if ($appraisal != null) {
  2356.             return $ics->generateEventFile([[
  2357.                 'title' => $translator->trans('ics.appraisal.title') . ' ' $appraisal->getTitle(),
  2358.                 'description' => $translator->trans('ics.appraisal.description', ['%uid%' => $appraisal->getUser()->getId(), '%user%' => strtolower($appraisal->getUser()->getPersonalInfo()->getFirstName()), '%link%' => $this->generateUrl('user_profile', array(), UrlGeneratorInterface::ABSOLUTE_URL) . '/' $appraisal->getUser()->getEmail() . '?g=appraisal&open=appraisal-' $appraisal->getId()]),
  2359.                 'startDate' => $appraisal->getReviewAt()->format('Ymd'),
  2360.                 'startTime' => '090000',
  2361.                 'endDate' =>  $appraisal->getReviewAt()->format('Ymd'),
  2362.                 'endTime' => '180000',
  2363.                 'location' => 'HR Platfrom',
  2364.                 'reminderTime' => '-15M',
  2365.                 'reminderDescription' => $translator->trans('ics.appraisal.title') . ' ' $appraisal->getTitle(),
  2366.                 'fileName' => 'Appraisal-' $appraisal->getIcsToken()
  2367.             ]]);
  2368.         } else {
  2369.             throw $this->createNotFoundException($translator->trans('messages.error.not_found'));
  2370.         }
  2371.     }
  2372.     #[Route(path'/appraisal-export/{_type}/{_target}'defaults: ['_target' => 'local''_type' => 'excel'], name'appraisal_export')]
  2373.     public function exportAppraisal(string $_typestring $_targetRequest $requestAppraisalService $appraisalService)
  2374.     {
  2375.         $appraisalId $request->get('id') ?: null;
  2376.         if($_type == 'excel' || empty($_type) || $_type == 'google'){
  2377.             if($_type == 'google'){
  2378.                 $_target $_type;
  2379.             }
  2380.             return $appraisalService->exportExcel($appraisalId$_target);
  2381.         }elseif($_type == 'pdf'){
  2382.             return $appraisalService->exportPdf($appraisalId$_target);
  2383.         }
  2384.     }
  2385.     #[Route(path'/appraisal-report/{_target}'defaults: ['_target' => 'local'], name'report_appraisal')]
  2386.     public function reportAppraisal(string $_targetRequest $requestTranslatorInterface $translatorLogService $logGoogleDriveService $googleDriveServiceAppraisalRepository $appraisalRepositoryAppraisalNoteRepository $appraisalNoteRepository)
  2387.     {
  2388.         $appraisalYear $request->get('year') ?: null;
  2389.         $type $request->get('type');
  2390.         $department $request->get('department') ?: null;
  2391.         $queries $appraisalRepository->createQueryBuilder('a')
  2392.         ->leftJoin('a.user''u')
  2393.         ->leftJoin('u.department''dept')
  2394.         ->andWhere('u.isActive = :ia')->setParameter('ia'1);
  2395.         // ->andWhere('a.user = :user')->setParameter('user', 128);
  2396.         if($department){
  2397.             $queries->andWhere('dept.id = :department')->setParameter('department'$department);
  2398.         }
  2399.         if ($type == 'probation') {
  2400.             if ($appraisalYear) {
  2401.                 if (strpos($appraisalYear'-end') !== false) {
  2402.                     $year str_replace('-end'''$appraisalYear);
  2403.                 } elseif (strpos($appraisalYear'-mid') !== false) {
  2404.                     $year str_replace('-mid'''$appraisalYear);
  2405.                 } else {
  2406.                     $year $appraisalYear;
  2407.                 }
  2408.                 $queries->andWhere('YEAR(a.reviewAt) = :year')->setParameter('year'$year);
  2409.             }
  2410.             $queries->andWhere('LOWER(a.title) LIKE LOWER(:probation)')->setParameter('probation''%probation%');
  2411.         } elseif ($type == 'permanent') {
  2412.             if (strpos($appraisalYear'-end') !== false) {
  2413.                 $year str_replace('-end'''$appraisalYear);
  2414.                 $queries->andWhere('LOWER(a.title) LIKE LOWER(:yearEnd)')
  2415.                     ->setParameter('yearEnd''%' $year '%end%');
  2416.             } elseif (strpos($appraisalYear'-mid') !== false) {
  2417.                 $year str_replace('-mid'''$appraisalYear);
  2418.                 $queries->andWhere('LOWER(a.title) LIKE LOWER(:yearMid)')
  2419.                     ->setParameter('yearMid''%' $year '%mid%');
  2420.             } else {
  2421.                 $queries->andWhere('LOWER(a.title) LIKE LOWER(:year)')
  2422.                     ->setParameter('year''%' $appraisalYear '%');
  2423.             }
  2424.             $queries->andWhere('LOWER(a.title) NOT LIKE LOWER(:probation)')->setParameter('probation''%probation%');
  2425.         }
  2426.         $appraisals $queries->getQuery()->getResult();
  2427.         usort($appraisals, function($a$b) {
  2428.             $scoreA $a->appraisalScore();
  2429.             $scoreB $b->appraisalScore();
  2430.             
  2431.             if ($scoreA === null && $scoreB === null) return 0;
  2432.             if ($scoreA === null) return 1;
  2433.             if ($scoreB === null) return -1;
  2434.         
  2435.             return $scoreB <=> $scoreA;
  2436.         });
  2437.         $user null;
  2438.         $spreadsheet = new Spreadsheet();
  2439.         $summarySheet $spreadsheet->getActiveSheet()->setTitle('Summary');
  2440.         $summarySheet->setCellValue('A1''Name');
  2441.         $summarySheet->setCellValue('B1''Department');
  2442.         $summarySheet->setCellValue('C1''Office');
  2443.         $summarySheet->setCellValue('D1''Job Title');
  2444.         // $summarySheet->setCellValue('E1', 'Appraisal Title');
  2445.         $summarySheet->setCellValue('E1''Supervised By');
  2446.         $summarySheet->setCellValue('F1''Reviewed At');
  2447.         $summarySheet->setCellValue('G1''Overall Rating');
  2448.         $lastCol $summarySheet->getHighestColumn();
  2449.         $summarySheet->getDefaultColumnDimension()->setWidth(25);
  2450.         // $summarySheet->getColumnDimension('B')->setWidth(120);
  2451.         $summarySheet->getStyle("A1:".$lastCol."1")->getFont()->setBold(true);
  2452.         $summarySheet->getStyle("A1:".$lastCol."1")->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
  2453.         $summarySheet->getStyle("A1:".$lastCol."1")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00000000');
  2454.         $summarySheet->getStyle("A2:".$lastCol."2")->getFont()->setBold(false);
  2455.         $summarySheet->getStyle("A2:".$lastCol."2")->getAlignment()->setWrapText(true);
  2456.         $summarySheet->getStyle("A2:".$lastCol."2")->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP);
  2457.         $filename 'Appraisal-Report_';
  2458.         if ($appraisalYear) {
  2459.             if (strpos($appraisalYear'-end') !== false) {
  2460.                 $filename .= str_replace('-end'''$appraisalYear) . '-End-Year';
  2461.             } elseif (strpos($appraisalYear'-mid') !== false) {
  2462.                 $filename .= str_replace('-mid'''$appraisalYear) . '-Year-Mid';
  2463.             } else {
  2464.                 $filename .= $appraisalYear;
  2465.             }
  2466.         }
  2467.         // $filename .= $user ? "-" . urlencode($user->getPersonalInfo()->getFullName()) : "";
  2468.         // $filename .= '.xlsx';
  2469.         $filename .= "_" date('Y-m-d') . '.xlsx';
  2470.         $row $summarySheet->getHighestRow();
  2471.         foreach($appraisals as $appraisal){
  2472.             $user $appraisal->getUser();
  2473.             $summarySheet->setCellValue('A'.$row$user->getPersonalInfo()->getFullName());
  2474.             $summarySheet->setCellValue('B'.$row$user->getDepartment()->getName());
  2475.             $summarySheet->setCellValue('C'.$row$user->getOffice()->getFullName());
  2476.             $summarySheet->setCellValue('D'.$row$user->organizationTitles());
  2477.             $managerArray = [];
  2478.             foreach ($user->getAllManager() as $manager) {
  2479.                 $managerArray[] = $manager->getPersonalInfo()->getFirstName();
  2480.             }
  2481.             // $summarySheet->setCellValue('E'.$row, $appraisal->getTitle());
  2482.             $summarySheet->setCellValue('E'.$rowimplode(', '$managerArray));
  2483.             $summarySheet->setCellValue('F'.$row$appraisal->getReviewAt() ? $appraisal->getReviewAt()->format('d-M-Y') : '-');
  2484.             $summarySheet->setCellValue('G'.$row$appraisal->appraisalScore() ?: '-');
  2485.             $summarySheet->getStyle('G'.$row)->getFont()->setBold(true);
  2486.             $row++;
  2487.         }
  2488.         $summarySheet->setAutoFilter('A1:'.$lastCol.$summarySheet->getHighestRow());
  2489.         $writer = new Xlsx($spreadsheet);
  2490.         $writer->save($filename);
  2491.         if ($_target == 'google') {
  2492.             $gsheetURL $googleDriveService->uploadToGoogleDrive($filename);
  2493.             if ($gsheetURL) {
  2494.                 unlink($filename);
  2495.                 return new RedirectResponse($gsheetURL302);
  2496.             }
  2497.         } else {
  2498.             $response = new Response();
  2499.             $response->headers->set('Content-type''application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  2500.             $response->headers->set('Content-Disposition'sprintf('attachment; filename="%s"'$filename));
  2501.             $response->setContent(file_get_contents($filename));
  2502.             $response->setStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_OK);
  2503.             $response->headers->set('Content-Transfer-Encoding''binary');
  2504.             $response->headers->set('Pragma''no-cache');
  2505.             $response->headers->set('Expires''0');
  2506.             unlink($filename);
  2507.             return $response;
  2508.             exit;
  2509.         }
  2510.     }
  2511. }