src/Repository/ActionRepository.php line 307

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Action;
  4. use App\Entity\ActionPlan;
  5. use App\Entity\Configuration\CompanyEntity;
  6. use App\Entity\User;
  7. use App\Enum\PriorityEnum;
  8. use App\Enum\StatusEnum;
  9. use App\Service\SelectedEntityService;
  10. use DateTime;
  11. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use DoctrineExtensions\Query\Mysql\Month;
  14. use App\Utils\Utility;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Security\Core\Security;
  17. /**
  18.  * @method Company|null find($id, $lockMode = null, $lockVersion = null)
  19.  * @method Company|null findOneBy(array $criteria, array $orderBy = null)
  20.  * @method Company[]    findAll()
  21.  * @method Company[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  22.  */
  23. class ActionRepository extends ServiceEntityRepository
  24. {
  25.     private $requestStack;
  26.     private $security;
  27.     private $selectedEntityService;
  28.     public function __construct(
  29.         ManagerRegistry $registry,
  30.         Security $security,
  31.         RequestStack $requestStack,
  32.         SelectedEntityService $selectedEntityService
  33.     )
  34.     {
  35.         parent::__construct($registryAction::class);
  36.         $this->security $security;
  37.         $this->requestStack $requestStack;
  38.         $this->selectedEntityService $selectedEntityService;
  39.     }
  40.     
  41.     /*public function findLastActionEE(): ?Action
  42.     {
  43.         return $this->createQueryBuilder('a')
  44.         ->where('a.action IS NOT NULL')
  45.         ->orderBy('a.id', 'desc')
  46.         ->getQuery()
  47.         ->getOneOrNullResult();
  48.         
  49.     }*/
  50.     public function getActionsPeridoques()
  51.     {
  52.         return $this->createQueryBuilder('a')
  53.         ->where('a.periodicity IS NOT NULL')
  54.         ->andWhere('a.action IS NOT 1')
  55.         ->getQuery()
  56.         ->getOneOrNullResult();
  57.     }
  58.     public function findLastRef($prefix)
  59.     {
  60.         $qb$this->createQueryBuilder('a');
  61.         $qb->select('MAX(a.ref)')
  62.             ->where(
  63.                 $qb->expr()->like('a.ref'':prefix')
  64.             )
  65.             ->setParameter('prefix'$prefix.'-%');
  66.         
  67.         $ref=$qb->getQuery()->getOneOrNullResult();
  68.         
  69.         return $ref[1];
  70.     }
  71.     public function transform(Action $action,User $user$remove_link='')
  72.     {
  73.         
  74.         $respectBudget ="OK";
  75.         if($action->getBudgetEstimated()<$action->getBudgetReal()) $respectBudget ="NOK";
  76.         
  77.         $respectDuree "OK";
  78.         if($action->getDurationEstimated()<$action->getDurationReal()) $respectDuree ="NOK";
  79.         
  80.          
  81.         $respectDelais "OK";
  82.         $joursRetard="0";
  83.         $now = new DateTime("now");
  84.         if($action->getDateEndEstimated()<$action->getDateEndReal()) 
  85.         {
  86.             $respectDelais ="NOK";
  87.             $joursRetard $action->getDateEndEstimated()->diff($action->getDateEndReal())->format("%a");
  88.         }
  89.         elseif($action->getDateEndEstimated() < $now)
  90.         {
  91.             $respectDelais ="NOK";
  92.             $joursRetard $action->getDateEndEstimated()->diff($now)->format("%a");
  93.         }
  94.         
  95.         if(in_array($action->getStatus(),[StatusEnum::STATUS_PLANNED,StatusEnum::STATUS_DRAFT,StatusEnum::STATUS_IN_PROGRESS]))
  96.         {
  97.             $respectBudget="";
  98.             $respectDuree="";
  99.             $respectDelais="";
  100.         }
  101.         if(is_null($action->getBudgetEstimated()) || is_null($action->getBudgetReal()))
  102.         {
  103.             $respectBudget="";
  104.         }
  105.         if(is_null($action->getDurationEstimated()) || is_null($action->getDurationReal()))
  106.         {
  107.             $respectDuree="";
  108.         }
  109.         
  110.         $editable="0";
  111.         
  112.         //if(in_array($user,[$action->getResponsible(),$action->getCreatedBy()]) ) $editable="1";
  113.         if($user==$action->getResponsible() ) $editable="1";
  114.         $actionIsEE="0";
  115.         if($action->getTypeAction()=="AE/AI"$actionIsEE="1";
  116.         $delete_btn="";
  117.         $annuler_btn="";
  118.         if($action->getCreatedBy()==$user)
  119.         {
  120.             $urlDelete"/action/delete/".$action->getId();
  121.             $delete_btn"<div class='type_p'>
  122.             <button class='btn bg-delete' 
  123.             onclick='open_delete_dialog_without_from(\"".$urlDelete."\",\"".$action->getRef()."\" )'
  124.             onmouseover='showToolTip(\"Supprimer l'action\",\"span_delete_".$action->getId()."\")' 
  125.             onmouseout='hideToolTip(\"span_delete_".$action->getId()."\")'
  126.             >
  127.             <i class='fa fa-trash-o'></i>
  128.             </button>
  129.             <span class='span_tootltip span_priority' style='left:-80px' id='span_delete_".$action->getId()."'></span>
  130.             </div> ";
  131.             if($action->getStatus() === StatusEnum::STATUS_ANNULE) {
  132.                 $urlAnnuler="/action/activate/".$action->getId();
  133.                 $annuler_btn="<div class='type_p'>
  134.                 <a class='btn bg-activate' 
  135.                 data-toggle='modal' data-target='#myModal'
  136.                 href='/action/activate/".$action->getId()."'
  137.                 onmouseover='showToolTip(\"Réactiver l'action\",\"span_cancel_".$action->getId()."\")' 
  138.                 onmouseout='hideToolTip(\"span_cancel_".$action->getId()."\")'
  139.                 >
  140.                 <i class='fa fa-check'></i>
  141.                 </a> 
  142.                 <span class='span_tootltip span_priority' style='left:-60px' id='span_cancel_".$action->getId()."'></span>
  143.                 </div>";
  144.             }else {
  145.                 $urlAnnuler="/action/annuler/".$action->getId();
  146.                 $annuler_btn="<div class='type_p'>
  147.                 <button class='btn bg-annuler' 
  148.                 onclick='open_annuler_dialog_without_from(\"".$urlAnnuler."\",\"".$action->getRef()."\" )'
  149.                 onmouseover='showToolTip(\"Annuler l'action\",\"span_cancel_".$action->getId()."\")' 
  150.                 onmouseout='hideToolTip(\"span_cancel_".$action->getId()."\")'
  151.                 >
  152.                 <i class='fa fa-times'></i>
  153.                 </button> 
  154.                 <span class='span_tootltip span_priority' style='left:-60px' id='span_cancel_".$action->getId()."'></span>
  155.                 </div>";
  156.             }
  157.             
  158.         }
  159.         
  160.        //validation
  161.         $validation='';
  162.         if(!empty($action->getValidator()) && $action->getStatus()==StatusEnum::STATUS_PLANNED)
  163.         {
  164.             if($action->getValidator()==$user)
  165.             {
  166.                 $validation="<a class='btn btn-xs btn-danger pull-right' style='margin-left:5px' href='/action/".$action->getId()."/validation'>Valider cette action</a> ";
  167.             }
  168.         }
  169.         
  170.         $ftlrActionTypePlanInd="1";
  171.         if(!empty($action->getActionPlan()))
  172.         {
  173.             $ftlrActionTypePlanInd="0";
  174.         }
  175.         $ftlrProcessus=[];
  176.         $list_processus='';
  177.         foreach($action->getProcessus() as $proc)
  178.         {
  179.             $ftlrProcessus[]=$proc->getTitle();
  180.             $list_processus.="<span class='badge bg-color-darken'>".$proc->getTitle()."</span>";
  181.         }
  182.        
  183.         
  184.         $durationEstimated strval($action->getDurationEstimated());
  185.         
  186.         
  187.         $budgetEstimated="<span class='badge bg-color-darken'>".$action->getBudgetEstimated()." â‚¬</span>";
  188.         $img_file="";
  189.         $pdf '';
  190.         if($action->getPdfFile()) {
  191.             $pdf strval($action->getId());
  192.             $extension_array explode('.'$action->getPdfFile());
  193.             $extension =  strtolower($extension_array[1]);
  194.             if($extension === 'png' || $extension === 'jpg') {
  195.                 $img_file='/img/icons/img.png';
  196.             }else if($extension==='doc' || $extension==='docx') {
  197.                 $img_file='/img/icons/doc.png';
  198.             }else if($extension==='ppt') {
  199.                 $img_file='/img/icons/ppt.png';
  200.             }else if (str_contains($extension,'xl')) {
  201.                 $img_file='/img/icons/xl.png';
  202.             }else {
  203.                 $img_file='/img/icons/txt.png';
  204.             }
  205.         } else {
  206.             $pdf "";
  207.         }
  208.         $ftlrDomaine=[];
  209.         
  210.         if(!empty($action->getDomaines()))
  211.         {
  212.             foreach($action->getDomaines() as $domain)
  213.             {
  214.                 $ftlrDomaine[]=$domain->getTitle();
  215.             }
  216.         }
  217.         $ftlrSource='';
  218.         if($action->getSource()) $ftlrSource $action->getSource()->getId();
  219. //        if($action->getCreatedBy()->getEntity()) $entity = $action->getCreatedBy()->getEntity()->getNom();
  220.         $entity "";
  221.         foreach($action->getCompanyEntities() as $ce)
  222.         {
  223.             $entity.="<span class='badge bg-color-darken' style='margin-right: 5px'>".$ce->getNom()."</span>";
  224.         }
  225.         $ref="";
  226.         if($remove_link) {
  227.             $ref$action->getRef();
  228.         }else {
  229.             $ref="<a href='/action/".$action->getId()."/edit' class='ref'>".$action->getRef()."</a>";
  230.         }
  231.         $comment_color "#CEDCEF";
  232.         if($action->hasComment()) $comment_color "#3466A8";
  233.         $comment "<a href='/action/".$action->getId()."/comment' data-toggle='modal' data-target='#myModal' style='color:".$comment_color."' id='action_comment_".$action->getId()."'><i class='fa fa-comments' style='font-size:20px'></i></a>";
  234.         return [
  235.             "ftlrDomaine" => $ftlrDomaine,
  236.             "ftlrType" => $action->getType()->getId(),
  237.             "ftlrCategorie" => $action->getCategory(),
  238.             "ftlrSource" => $ftlrSource,
  239.             "ftlrProcessus" =>$ftlrProcessus,
  240.             "ftlrEnCharge" => $action->getResponsible()->getId(),
  241.             "responsibleName" => $action->getResponsible()->getFullName(),
  242.             "ftlrActionTypePlanInd" => $ftlrActionTypePlanInd,
  243.             "ftlrActionHasEfficacite" => strval($action->hasActionEE()),
  244.             "ftlrActionEfficacite" => "0",
  245.             "ftlrTypePa" =>  strval($action->getTypePa()),
  246.             "DT_RowId"=> $action->getId(),
  247.             "ref" => $ref,
  248.             "priorite" => $action->getPriority(),
  249.             "dateDebut" => $action->getDateStartReal(true),
  250.             "avancement" => $action->getProgress()."%",
  251.             "datefin" => $action->getDateEndReal(true),
  252.             "duree" => strval($action->getDurationReal()),
  253.             "cout" => strval($action->getBudgetReal()),
  254.             "respectBudget" => $respectBudget,
  255.             "statut" => $action->getStatus(),
  256.             "respectDuree" => $respectDuree,
  257.             "respectDelais" => $respectDelais,
  258.             "joursRetard" => '<span class="chiffre">'.$joursRetard.'</span>',
  259.             "nbrReport" =>'<span class="chiffre">'$action->getReportNbr().'</span>',
  260.             "efficacite" => strval($action->getEfficient()),
  261.             "dateEnregistrement" => $action->getCreatedAt()->format('d/m/Y'),
  262.             "resume" => '<span class="border-text">'.$action->getTitle().'</span>',
  263.             "desc" => strval($action->getDescription()),
  264.             "commentaires" => "",
  265.             "periodePrevisionelle" => $action->getDateEndEstimated(true),
  266.             "dureeEstimee" => strval($durationEstimated),
  267.             "budgetPrevisionel" => strval($budgetEstimated), 
  268.             "rapport" => $pdf,
  269.             "img_file"=>$img_file,
  270.             "action" => $delete_btn,
  271.             "actionAnnuler" => $annuler_btn,
  272.             "editable"=>$editable,
  273.             "actionIsEE"=>$actionIsEE,
  274.             "validation"=> $validation,
  275.             "list_processus"=>$list_processus,
  276.             "affectedBy"=>$action->getCreatedBy()->getFullName()." / ".$action->getCreatedBy()->getFonction(),
  277.             "entity"=> $entity,
  278.             "periodicity"=>$action->getPeriodicity() ? $action->getPeriodicity():'',
  279.             "comment"=>$comment
  280.         ];
  281.     }
  282.     public function transformAll(User $user,$actionPlan='',$remove_link='',$startDate='',$endDate='')
  283.     {
  284.        
  285.         $actions $this->findActions($actionPlan,true,$startDate,$endDate);
  286.         
  287.         //dd($actions);
  288.         $actionsArray = ['data' => []];
  289.         $actionsList=[];
  290.         foreach ($actions as $action
  291.         {
  292.             
  293.             $actionsList[]=$action;
  294.             if($action->hasActionEE())
  295.             {
  296.                 $actionsList[]=$action->getAction();
  297.             }
  298.         }
  299.         
  300.         foreach ($actionsList as $action
  301.         {
  302.             
  303.             $actionsArray['data'][] = $this->transform($action,$user$remove_link);
  304.         }
  305.         return $actionsArray;
  306.     }
  307.     public function findNbrAction(
  308.         $processus ='',
  309.         $actionPlan='',
  310.         $status='',
  311.         $lateCondition="",
  312.         $priority='',
  313.         $budgetCondition='',
  314.         $month='',
  315.         $cumultative=true,
  316.         $source='',
  317.         $type='',
  318.         $min_progress='',
  319.         $max_progress='',
  320.         $has_started='',
  321.         $durationCondition=''
  322.         )
  323.     {
  324.         $qb =  $this->createQueryBuilder('a')->select('COUNT(a.id)');
  325.         if(!empty($processus))
  326.         {
  327.             if($processus->getId() > 0)
  328.             {
  329.                 $qb ->where(':processus MEMBER OF a.processus')
  330.                     ->setParameter("processus"$processus);
  331.             }
  332.         }
  333.         if(!empty($actionPlan))
  334.         {
  335.             if($actionPlan=="include")
  336.             {
  337.                 $qb ->andWhere('a.actionPlan is NULL');
  338.             }
  339.             elseif($actionPlan=="not_include")
  340.             {
  341.                 $qb ->andWhere('a.actionPlan is NOT NULL');
  342.             }
  343.             elseif($actionPlan->getId()>0)
  344.             {
  345.                 $qb ->andWhere('a.actionPlan = :actionPlan')
  346.                     ->setParameter('actionPlan'$actionPlan);
  347.             }
  348.         }
  349.         
  350.         if(!empty($status))
  351.         {
  352.             
  353.             $qb ->andWhere('a.status = :status')
  354.                 ->setParameter('status'$status);
  355.         }
  356.         if(!empty($lateCondition))
  357.         {
  358.             $condition '';
  359.             if($lateCondition=='late')
  360.             {
  361.                 
  362.                 $condition 'a.dateEndEstimated < a.dateEndReal';
  363.                 if($status != StatusEnum::STATUS_CLOSED)
  364.                 {
  365.                     $condition 'a.dateEndEstimated < NOW()';
  366.                 }
  367.             }
  368.             else
  369.             {
  370.                 $condition 'a.dateEndEstimated >= a.dateEndReal';
  371.                 if($status != StatusEnum::STATUS_CLOSED)
  372.                 {
  373.                     $condition 'a.dateEndEstimated >= NOW()';
  374.                 }
  375.             }
  376.             $qb ->andWhere($condition);
  377.             
  378.         }
  379.         if(!empty($priority))
  380.         {
  381.             $qb ->andWhere('a.priority = :priority')
  382.                 ->setParameter('priority'$priority);
  383.         }
  384.         if(!empty($budgetCondition))
  385.         {
  386.             if($budgetCondition=='over')
  387.             {
  388.                 $qb ->andWhere('a.budgetEstimated < a.budgetReal');
  389.             }
  390.             else
  391.             {
  392.                 $qb ->andWhere('a.budgetEstimated >= a.budgetReal');
  393.             }
  394.             
  395.         }
  396.         if($month 0)
  397.         {
  398.             $month_condition 'MONTH(a.createdAt) = :month ';
  399.             if($cumultative)
  400.             {
  401.                 $month_condition 'MONTH(a.createdAt) <= :month ';
  402.             }
  403.             $qb ->andWhere($month_condition)
  404.                 ->setParameter('month'$month);
  405.             
  406.         }
  407.         if(!empty($source))
  408.         {
  409.             
  410.             $qb ->andWhere('a.source = :source')
  411.                 ->setParameter('source'$source);
  412.         }
  413.         if(!empty($type))
  414.         {
  415.             
  416.             $qb ->andWhere('a.type = :type')
  417.                 ->setParameter('type'$type);
  418.         }
  419.         if($max_progress 0)
  420.         {
  421.             $qb ->andWhere('a.progress >= :min_progress')
  422.                 ->andWhere('a.progress < :max_progress')
  423.                 ->setParameter('min_progress'$min_progress)
  424.                 ->setParameter('max_progress'$max_progress);
  425.         }
  426.         if(!empty($has_started))
  427.         {
  428.             $condition '';
  429.             if($has_started=='no')
  430.             {
  431.                 
  432.                 $condition 'a.dateStartEstimated < NOW()';
  433.             }
  434.             else
  435.             {
  436.                 $condition 'a.dateStartEstimated < NOW()';
  437.             }
  438.             $qb ->andWhere($condition);
  439.         }
  440.         if(!empty($durationCondition))
  441.         {
  442.             $condition '';
  443.             if($durationCondition=='over')
  444.             {
  445.                 $condition 'a.durationEstimated < a.durationReal';
  446.             }
  447.             else
  448.             {
  449.                 $condition 'a.durationEstimated >= a.durationReal';
  450.             }
  451.             $qb ->andWhere($condition);
  452.             
  453.         }
  454.         //draft
  455.         /*
  456.         $qb ->andWhere("a.status  <> :draftstatus")
  457.             ->setParameter('draftstatus', StatusEnum::STATUS_DRAFT);*/
  458.         //$qb ->andWhere("a.validated = 1");
  459.         //dump($qb->getQuery()->getSQL());
  460.         $nbrActions=$qb->getQuery()->getOneOrNullResult();
  461.         return $nbrActions[1];
  462.     }
  463.     public function findSumNbrReport($processus ='',$actionPlan='')
  464.     {
  465.         $qb =  $this->createQueryBuilder('a')->select('SUM(a.reportNbr)');
  466.         if(!empty($processus))
  467.         {
  468.             if($processus->getId() > 0)
  469.             {
  470.                 $qb ->where(':processus MEMBER OF a.processus')
  471.                     ->setParameter("processus"$processus);
  472.             }
  473.         }
  474.         if(!empty($actionPlan))
  475.         {
  476.             if($actionPlan->getId()>0)
  477.             {
  478.                 $qb ->andWhere('a.actionPlan = :actionPlan')
  479.                     ->setParameter('actionPlan'$actionPlan);
  480.             }
  481.         }
  482.         //draft
  483.         // $qb ->andWhere("a.status  <> :draftstatus")
  484.         //     ->setParameter('draftstatus', StatusEnum::STATUS_DRAFT);
  485.         $nbrReport $qb->getQuery()->getOneOrNullResult();
  486.         return $nbrReport[1];
  487.     }
  488.     public function findByCustom($actionPlan='',$status='',$priority='',$lateCondition='',$limit=0)
  489.     {
  490.         $qb =  $this->createQueryBuilder('a');
  491.         if(!empty($actionPlan))
  492.         {
  493.             if($actionPlan->getId() > 0)
  494.             {
  495.                 $qb ->where('a.actionPlan = :actionPlan')
  496.                     ->setParameter("actionPlan"$actionPlan);
  497.             }
  498.         }
  499.         if(!empty($status))
  500.         {
  501.             $qb ->andWhere('a.status = :status')
  502.                 ->setParameter('status'$status);
  503.         }
  504.         if(!empty($priority))
  505.         {
  506.             $qb ->andWhere('a.priority = :priority')
  507.                 ->setParameter('priority'$priority);
  508.         }
  509.         if(!empty($lateCondition))
  510.         {
  511.             $condition '';
  512.             if($lateCondition=='late')
  513.             {
  514.                 
  515.                 $condition 'a.dateEndEstimated < a.dateEndReal';
  516.                 if($status != StatusEnum::STATUS_CLOSED)
  517.                 {
  518.                     $condition 'a.dateEndEstimated < NOW()';
  519.                 }
  520.             }
  521.             else
  522.             {
  523.                 $condition 'a.dateEndEstimated >= a.dateEndReal';
  524.                 if($status != StatusEnum::STATUS_CLOSED)
  525.                 {
  526.                     $condition 'a.dateEndEstimated >= NOW()';
  527.                 }
  528.             }
  529.             $qb ->andWhere($condition);
  530.             
  531.         }
  532.         //draft
  533.         // $qb ->andWhere("a.status  <> :draftstatus")
  534.         //     ->setParameter('draftstatus', StatusEnum::STATUS_DRAFT);
  535.             
  536.         if($limit0)
  537.         {
  538.             $qb->setMaxResults($limit);
  539.         }
  540.         $qb->orderBy('a.createdAt''DESC');
  541.         //dump($qb->getQuery()->getSQL());
  542.         return $qb->getQuery()->getResult();
  543.     }
  544.     public function findActions($actionPlan='',$withoutAcEE=true,$startDate='',$endDate='')
  545.     {
  546.         $qb =  $this->createQueryBuilder('a');
  547.         if(!empty($actionPlan))
  548.         {
  549.             if($actionPlan->getId() > 0)
  550.             {
  551.                 $qb ->where('a.actionPlan = :actionPlan')
  552.                     ->setParameter("actionPlan"$actionPlan);
  553.             }
  554.         }
  555.         if($withoutAcEE)
  556.         {
  557.             $qb ->andWhere('a.typeAction != :typeAction')
  558.                 ->setParameter("typeAction"'AE/AI');
  559.         }
  560.         if($startDate) {
  561.             $qb->andWhere('a.dateEndEstimated >= :startDate ')
  562.             ->setParameter("startDate"$startDate);
  563.         }
  564.         if($endDate) {
  565.             $qb->andWhere('a.dateEndEstimated < :endDate ')
  566.             ->setParameter("endDate"$endDate);
  567.         }
  568.         //filtrage par entité
  569.         $selectedEntity $this->selectedEntityService->getSelectedEntity();
  570.         if ($selectedEntity) {
  571.             $qb->join('a.companyEntities''ce')
  572.                 ->andWhere(':entity MEMBER OF a.companyEntities')
  573.                 ->setParameter('entity'$selectedEntity);
  574.         } else {
  575.             $user $this->security->getUser();
  576.             $entities $user->getEntities();
  577.             if (count($entities) > 0) {
  578.                 $qb->join('a.companyEntities''ce2')
  579.                     ->andWhere('ce2 IN (:entities)')
  580.                     ->setParameter('entities'$entities);
  581.             }
  582.         }
  583.         $qb->orderBy('a.createdAt''DESC');
  584.         return $qb->getQuery()->getResult();
  585.     }
  586.     
  587. }