src/Service/ChecklistService.php line 133

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Checklist;
  4. use App\Entity\ChecklistActivity;
  5. use App\Entity\ChecklistHowTo;
  6. use App\Entity\ChecklistTask;
  7. use App\Entity\ChecklistTaskDependency;
  8. use App\Repository\ChecklistRepository;
  9. use App\Repository\ChecklistTaskRepository;
  10. use App\Repository\CourseAutoEnrollmentRepository;
  11. use App\Repository\CourseRepository;
  12. use App\Repository\CourseUserEnrollmentRepository;
  13. use App\Repository\UserRepository;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Contracts\HttpClient\HttpClientInterface;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. use App\Service\MailgunService;
  21. use Doctrine\Common\Collections\ArrayCollection;
  22. use Firebase\JWT\JWT;
  23. class ChecklistService
  24. {
  25.   private $em;
  26.   private $params;
  27.   private $mailgunService;
  28.   private $translator;
  29.   private $checklistRepository;
  30.   private $authChecker;
  31.   private $userRepository;
  32.   private $checklistTaskRepository;
  33.   private $httpClient;
  34.   private $courseService;
  35.   private $security;
  36.   private $userService;
  37.   public function __construct(EntityManagerInterface $emParameterBagInterface $paramsMailgunService $mailgunServiceTranslatorInterface $translatorChecklistRepository $checklistRepositoryAuthorizationCheckerInterface $authCheckerUserRepository $userRepositoryChecklistTaskRepository $checklistTaskRepositoryHttpClientInterface $httpClientInterfaceCourseService $courseServiceSecurity $securityUserService $userService)
  38.   {
  39.     $this->em $em;
  40.     $this->params $params;
  41.     $this->mailgunService $mailgunService;
  42.     $this->translator $translator;
  43.     $this->checklistRepository $checklistRepository;
  44.     $this->authChecker $authChecker;
  45.     $this->userRepository $userRepository;
  46.     $this->checklistTaskRepository $checklistTaskRepository;
  47.     $this->httpClient $httpClientInterface;
  48.     $this->courseService $courseService;
  49.     $this->security $security;
  50.     $this->userService $userService;
  51.   }
  52.   /*
  53.   public function createChecklistFromTemplate($user = null, $project = null, $checklist)
  54.   {
  55.     // try {
  56.     $checklistTemplate = $this->checklistRepository->find($checklist->getId());
  57.     $newChecklist = new Checklist();
  58.     $newChecklist->setName($checklist->getName());
  59.     $newChecklist->setDescription($checklist->getDescription());
  60.     $newChecklist->setDueDate($checklist->getDueDate());
  61.     $newChecklist->setType($checklist->getType());
  62.     $newChecklist->setDepartment($checklist->getDepartment());
  63.     $newChecklist->setTemplate($checklist);
  64.     $newChecklist->setStatus($checklist->getStatus());
  65.     $newChecklist->setPosition($checklist->getPosition());
  66.     $project ? $newChecklist->setProject($project) : $newChecklist->setProject($checklist->getProject());
  67.     $newChecklist->setDefaultAssignee($checklist->getDefaultAssignee());
  68.     $newChecklist->setOwner($checklist->getOwner());
  69.     $newChecklist->setMattermostUrl($checklist->getMattermostUrl());
  70.     $newChecklist->setIsPrivate($checklist->isIsPrivate());
  71.     $newChecklist->setOnboardUser($user);
  72.     $newChecklist->setIsTemplate(false);
  73.     $this->em->persist($newChecklist);
  74.     foreach ($checklist->getTasks() as $task) {
  75.       $newTask = new ChecklistTask();
  76.       $newTask->setChecklist($newChecklist);
  77.       $newTask->setName($task->getName());
  78.       $newTask->setDescription($task->getDescription());
  79.       $newTask->setPriority($task->getPriority());
  80.       $newTask->setDueDate($task->getDueDate());
  81.       $newTask->setStatus($task->getStatus());
  82.       $newTask->setVisibility($task->getVisibility());
  83.       $newTask->setRequireDocument($task->isRequireDocument());
  84.       $newTask->setDocument($task->getDocument());
  85.       $newTask->setPosition($task->getPosition());
  86.       $newTask->setDefaultAssignee($task->getDefaultAssignee());
  87.       $newTask->setVerifier($task->getVerifier());
  88.       $this->em->persist($newTask);
  89.       $newChecklist->getTasks()->add($newTask);
  90.       foreach ($task->getChecklistHowTos() as $howTo) {
  91.         $newHowTo = new ChecklistHowTo();
  92.         $newHowTo->setTask($newTask);
  93.         $newHowTo->setValue($howTo->getValue());
  94.         $this->em->persist($newHowTo);
  95.         $newTask->getChecklistHowTos()->add($newHowTo);
  96.       }
  97.     }
  98.     foreach ($checklist->getTasks() as $task) {
  99.       foreach ($task->getTaskDependencyAsParent() as $dependency) {
  100.         $newTaskId = $taskMap[$task->getId()] ?? null;
  101.         $newDependsOnTaskId = $taskMap[$dependency->getTaskDependsOn()->getId()] ?? null;
  102.         if ($newTaskId && $newDependsOnTaskId) {
  103.           $dependencyEntity = new ChecklistTaskDependency();
  104.           $dependencyEntity->setTask($newTaskId);
  105.           $dependencyEntity->setTaskParent($newDependsOnTaskId);
  106.           $this->em->persist($dependencyEntity);
  107.         }
  108.       }
  109.     }
  110.     $this->em->flush();
  111.     // return $newChecklist;
  112.     // } catch (\Exception $e) {
  113.     //   throw $e;
  114.     // }
  115.   }
  116.     */
  117.     public function createChecklistFromTemplate($user null$project null$checklist)
  118.     {
  119.       
  120.       $HRManager $this->userService->getHRManager();
  121.       $modifierDays $checklist->getType() == 'OFFBOARDING' 15 5;
  122.       $currentUser $this->security->getUser();
  123.       $initialState = [
  124.         'data' => [
  125.           'role' => $currentUser->getRoles(),
  126.           'id' => $currentUser->getId(),
  127.         ],
  128.         'exp' => time() + 30 24 60 60,
  129.       ];
  130.   
  131.       $jwt JWT::encode($initialState$this->params->get('JWT_SECRET'));
  132.     
  133.       if($user){
  134.         $editedPersonalInfo $user->getPersonalInfo();
  135.       }
  136.       
  137.       $startDate null;
  138.       $endDate null;
  139.       if($checklist->getType() == 'ONBOARDING'){
  140.         $startDate $editedPersonalInfo->getJobJoinDate()->format('Y-m-d\TH:i:s\Z');
  141.         $endDate $this->courseService->addWorkdays($editedPersonalInfo->getJobJoinDate()->format('Y-m-d'), $modifierDays$user->getOffice()->getCountry());
  142.         $endDate = new \DateTime($endDate);
  143.         $endDate $endDate->format('Y-m-d\T00:00:00\Z');
  144.       }elseif($checklist->getType() == 'PERMANENT'){
  145.         $startDate $editedPersonalInfo->getJobPermanentDate()->format('Y-m-d\TH:i:s\Z');
  146.         $endDate $this->courseService->addWorkdays($editedPersonalInfo->getJobPermanentDate()->format('Y-m-d'), $modifierDays$user->getOffice()->getCountry());
  147.         $endDate = new \DateTime($endDate);
  148.         $endDate $endDate->format('Y-m-d\TH:i:s\Z');
  149.         // $endDate = $endDate->format('Y-m-d\T00:00:00\Z');
  150.       }elseif($checklist->getType() == 'OFFBOARDING'){
  151.         $endDate $editedPersonalInfo->getJobTerminateDate()->format('Y-m-d\TH:i:s\Z');
  152.         $startDate $this->subtractWorkdays(new \DateTime($editedPersonalInfo->getJobTerminateDate()->format('Y-m-d')), 15);
  153.         $startDate = new \DateTime($startDate);
  154.         $startDate $startDate->format('Y-m-d\T00:00:00\Z');
  155.       }else{
  156.         $startDate = new \DateTime();
  157.         $startDate->setTime(0,0,0);
  158.         $startDateData $startDate;
  159.         $startDate $startDate->format('Y-m-d\T00:00:00\Z');
  160.         $endDate $this->courseService->addWorkdays($startDateData->format('Y-m-d'), $modifierDays);
  161.       }
  162.      
  163.       if($checklist->getType() == 'PROJECT'){
  164.         $checklistName $checklist->getName() . '-' $project->getGeneratedId();
  165.       } else {
  166.         $checklistName $checklist->getName().' - ' $user->getPersonalInfo()->getShortFullName();
  167.       }
  168.       
  169.       $postData = [
  170.         "name" => $checklistName,
  171.         "type" => $checklist->getType(),
  172.         "start_date" => $startDate,
  173.         "due_date" => $endDate,
  174.         "status" => "ACTIVE",
  175.         "position" => 1,
  176.         "template_id" => null,
  177.         "project_id" => $project $project->getId() : null,
  178.         "onboard_user_id" => $user $user->getId() : null,
  179.         "department_id" => $checklist->getDepartment() ? $checklist->getDepartment()->getId() : null,
  180.         "default_assignee_id" => $HRManager->getId(),
  181.         "owner_id" => $HRManager->getId()
  182.       ];
  183.       try {
  184.         $response $this->httpClient->request('POST''https://hrp-api.mediatropy.com/checklist/from-template/'.$checklist->getId(), [
  185.           'headers' => [
  186.             'Authorization' => $jwt,
  187.             'Content-Type' => 'application/json',
  188.           ],
  189.           'json' => $postData,
  190.         ]);
  191.         
  192.         return $response;
  193.         
  194.       } catch (\Exception $e) {
  195.         return false;
  196.       }
  197.     }
  198.     public function subtractWorkdays(\DateTime $dateint $days): string
  199.     {
  200.         while ($days 0) {
  201.             $date->modify('-1 day');
  202.             if ($date->format('N') < 6) {
  203.                 $days--;
  204.             }
  205.         }
  206.         return $date->format('Y-m-d');
  207.     }
  208.   public function checklistTransferOwnership($userId$managerId nullbool $isTest false): array
  209.   {
  210.     $user $this->userRepository->find($userId);
  211.     $manager $managerId $this->userRepository->find($managerId) : null;
  212.     $userManager $manager $manager $user->getManager();
  213.     $summary = [
  214.       'status' => 'success',
  215.       'message' => '',
  216.       'transferredTasks' => 0,
  217.       'transferredTasksAsAssignee' => 0,
  218.       'transferredTasksAsVerifier' => 0,
  219.       'transferredTasksAsAssigneeAndVerifier' => 0,
  220.       'affectedChecklists' => 0,
  221.       'details' => [],
  222.     ];
  223.     if (!$user) {
  224.       return [
  225.         'status' => 'error',
  226.         'message' => 'Source user not found',
  227.         'transferredTasks' => 0,
  228.         'transferredTasksAsAssignee' => 0,
  229.         'transferredTasksAsVerifier' => 0,
  230.         'transferredTasksAsAssigneeAndVerifier' => 0,
  231.         'affectedChecklists' => 0,
  232.         'details' => [],
  233.       ];
  234.     }
  235.     $qb $this->checklistTaskRepository->createQueryBuilder('ct')
  236.       ->leftJoin('ct.checklist''c')
  237.       ->leftJoin('c.project''p')
  238.       ->where('ct.status = :status')
  239.       ->andWhere('c.type = :type')
  240.       ->andWhere('c.status = :cstatus')
  241.       ->andWhere('c.project IS NOT NULL')
  242.       ->andWhere('ct.defaultAssignee = :assignee OR ct.verifier = :assignee')
  243.       ->setParameter('cstatus''ACTIVE')
  244.       ->setParameter('status''TO_DO')
  245.       ->setParameter('type''PROJECT')
  246.       ->setParameter('assignee'$user->getId());
  247.     $tasks $qb->getQuery()->getResult();
  248.     if (empty($tasks)) {
  249.       return [
  250.         'status' => 'info',
  251.         'message' => 'No tasks found for the specified user',
  252.         'transferredTasks' => 0,
  253.         'transferredTasksAsAssignee' => 0,
  254.         'transferredTasksAsVerifier' => 0,
  255.         'transferredTasksAsAssigneeAndVerifier' => 0,
  256.         'affectedChecklists' => 0,
  257.         'details' => [],
  258.       ];
  259.     }
  260.     $tasksByChecklist = [];
  261.     foreach ($tasks as $task) {
  262.       $checklist $task->getChecklist();
  263.       if (!isset($tasksByChecklist[$checklist->getId()])) {
  264.         $tasksByChecklist[$checklist->getId()] = [
  265.           'checklist' => $checklist,
  266.           'tasks' => []
  267.         ];
  268.       }
  269.       $tasksByChecklist[$checklist->getId()]['tasks'][] = $task;
  270.     }
  271.     foreach ($tasksByChecklist as $checklistId => $checklistData) {
  272.       $checklist $checklistData['checklist'];
  273.       $tasks $checklistData['tasks'];
  274.       foreach ($tasks as $task) {
  275.         if ($task->getDefaultAssignee() === $user && $task->getVerifier() === $user) {
  276.           $task->setVerifier($userManager);
  277.           $task->setDefaultAssignee($userManager);
  278.           $summary['transferredTasksAsAssigneeAndVerifier']++;
  279.         } elseif ($task->getDefaultAssignee() === $user) {
  280.           $task->setDefaultAssignee($userManager);
  281.           $summary['transferredTasksAsAssignee']++;
  282.         } elseif ($task->getVerifier() === $user) {
  283.           $task->setVerifier($userManager);
  284.           $summary['transferredTasksAsVerifier']++;
  285.         }
  286.       }
  287.       $summary['details'][$checklistId] = [
  288.         'name' => $checklist->getName(),
  289.         'tasks' => count($tasks),
  290.       ];
  291.       $summary['transferredTasks'] += count($tasks);
  292.     }
  293.     $summary['affectedChecklists'] = count($tasksByChecklist);
  294.     if (!$isTest) {
  295.       $this->em->flush();
  296.       if (!empty($tasksByChecklist)) {
  297.         foreach ($tasksByChecklist as $checklistId => $checklistData) {
  298.           $checklistActivity = new ChecklistActivity();
  299.           $checklistActivity->setChecklist($checklistData['checklist']);
  300.           $checklistActivity->setDescription('Ownership transferred: ' count($checklistData['tasks']) . ' tasks from' $user->getPersonalInfo()->getFirstName() . ' to ' $userManager->getPersonalInfo()->getFirstName());
  301.           $checklistActivity->setCreatedAt(new \DateTimeImmutable());
  302.           $this->em->persist($checklistActivity);
  303.         }
  304.         $this->em->flush();
  305.       }
  306.     } else {
  307.       $summary['message'] = 'Test mode: No changes were saved to the database.';
  308.     }
  309.     return $summary;
  310.   }
  311. }