src/Service/OperationTravailService.php line 283

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\OperationSearch;
  4. use Doctrine\ORM\QueryBuilder;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\ORM\Tools\Pagination\Paginator;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use App\Entity\RefGroupe;
  9. use App\Utils\LibreOfficeWrapper\LibreOffice;
  10. use App\Entity\PersonnelSearch;
  11. use App\Entity\RefFonction;
  12. use App\Entity\RefTypeOperationPaie;
  13. use App\Entity\TblOperationPaie;
  14. use App\Entity\TblPersonnel;
  15. use App\Repository\RefTypeOperationPaieRepository;
  16. use App\Repository\TblOperationPaieRepository;
  17. use App\Repository\TblOperationTravailRepository;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use PhpOffice\PhpWord\TemplateProcessor;
  21. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  22. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  23. use Symfony\Component\Security\Core\Security;
  24. use alhimik1986\PhpExcelTemplator\params\ExcelParam;
  25. use App\Entity\RefTypeOperationTravail;
  26. use App\Entity\TblOperationTravail;
  27. use DateTime;
  28. class OperationTravailService
  29. {
  30.     /**
  31.      * @var EntityManagerInterface $em
  32.      */
  33.     private $em;
  34.     /**
  35.      * @var TblOperationTravailRepository $repository
  36.      */
  37.     private $repository;
  38.     /**
  39.      * @var TblOperationPaieRepository $repositoryPaie
  40.      */
  41.     private $repositoryPaie;
  42.     /**
  43.      * @var RefTypeOperationPaieRepository $repositoryTypePaie
  44.      */
  45.     private $repositoryTypePaie;
  46.     /**
  47.      * @var OperationPaieService $paieService
  48.      */
  49.     private $paieService;
  50.     /**
  51.      * @var UrlGeneratorInterface  $router
  52.      */
  53.     private $router;
  54.     /**
  55.      * @var SessionInterface $session
  56.      */
  57.     private $session;
  58.     /**
  59.      * @var ChiffreEnLettreService $chiffreToLettreService
  60.      */
  61.     private $chiffreToLettreService;
  62.   
  63.     
  64.     public function __construct(
  65.         EntityManagerInterface $em,
  66.         Security $security,
  67.         TblOperationTravailRepository $repository,
  68.         TblOperationPaieRepository $repositoryPaie,
  69.         RefTypeOperationPaieRepository $repositoryTypePaie,
  70.         OperationPaieService $paieService,
  71.         UrlGeneratorInterface $router,
  72.         SessionInterface $session
  73.         ChiffreEnLettreService $chiffreToLettreService,
  74.         $template_personnel_opperationPaie_dir,
  75.         $template_personnel_opperationPaie_generer_dir
  76.        
  77.     ) {
  78.         $this->em $em;
  79.         $this->security $security;
  80.         $this->repository $repository;
  81.         $this->repositoryPaie $repositoryPaie;
  82.         $this->repositoryTypePaie $repositoryTypePaie;
  83.         $this->paieService $paieService;
  84.         $this->router $router;
  85.         $this->session $session;
  86.         $this->chiffreToLettreService $chiffreToLettreService;
  87.         $this->template_personnel_opperationPaie_dir$template_personnel_opperationPaie_dir;
  88.         $this->template_personnel_opperationPaie_generer_dir $template_personnel_opperationPaie_generer_dir;
  89.         
  90.     }
  91.     public function getOperationAjax(Request $request$params)
  92.     {
  93.         if ($request->getMethod() == 'POST') {
  94.             $draw intval($request->request->get('draw'));
  95.             $start $request->request->get('start');
  96.             $length $request->request->get('length');
  97.             $search $request->request->get('search');
  98.             $orders $request->request->get('order');
  99.             $columns $request->request->get('columns');
  100.         } else {
  101.             $draw intval($request->get('draw'));
  102.             $start $request->get('start');
  103.             $length $request->get('length');
  104.             $search $request->get('search');
  105.             $orders $request->get('order');
  106.             $columns $request->get('columns');
  107.         }
  108.         if ($orders) {
  109.             foreach ($orders as $key => $order) {
  110.                 $orders[$key]['name'] = $columns[$order['column']]['name'];
  111.             }
  112.         }
  113.         $operationQb $this->getOperationQb($params);
  114.         $operationQb $this->filtersDatatable($operationQb$orders$search$params);
  115.        
  116.         $operationQbCount = clone $operationQb;
  117.         $count $this->getCountOperations($operationQbCount);
  118.         $operations $this->paginateDatatable($operationQb$start$length);
  119.         $data = ($params["paie"] || $params["resultats-paie"]) ? $this->getDataPaieForDatatable($operations'toArrayDatatable') : $this->getDataPointageForDatatable($operations'toArrayDatatable');
  120.         
  121.         return [
  122.             'draw' => $draw,
  123.             'recordsTotal' => $count,
  124.             'recordsFiltered' => $count,
  125.             'data' => $data
  126.         ];
  127.     }
  128.     private function getDataPaieForDatatable($paies$funcDisplay)
  129.     {
  130.         $data = [];
  131.         
  132.         foreach ($paies as $paie) {
  133.             $urlEdit $this->router->generate('app_operation_paie_edit', [
  134.                 'id' => $paie->getId(),
  135.             ]);
  136.             $data[] = $paie->{$funcDisplay}($urlEdit);
  137.         }
  138.         return $data;
  139.     }
  140.     private function getDataPointageForDatatable($operations$funcDisplay)
  141.     {
  142.         $data = [];
  143.         foreach ($operations as $operation) {
  144.             $urlEdit $this->router->generate('app_tbl_operation_travail_edit', [
  145.                 'id' => $operation->getId(),
  146.             ]);
  147.             $data[] = $operation->{$funcDisplay}($urlEdit);
  148.         }
  149.         return $data;
  150.     }
  151.     public function getDataForPointageCalendar($personnel null)
  152.     {
  153.         $data = [];
  154.         $params = [
  155.             "paie"     =>false,
  156.             "pointage" =>true,
  157.             "resultats-paie"=>false,
  158.             "resultats-pointage"=>false,
  159.         ];
  160.         $operations  $this->getOperationQb($params)
  161.                             ->getQuery()
  162.                             ->getResult();
  163.         foreach ($operations as $operation) {
  164.             $data[]= [
  165.                 'start' => $operation->getDateOperation()->format('Y-m-d'),
  166.                 'title' =>  $operation->getPersonnel() .' (' $operation->getTypeOperationTravail() . ': '$operation->getMontantTJM() * $operation->getDureeOperationTravail()->getValeurJour() . ' DH)',
  167.                 'textColor' => 'black',
  168.                 'extendedProps'=> [
  169.                     'id'=> $operation->getId()
  170.                 ]
  171.             ];
  172.         }
  173.         return $data;
  174.     }
  175.     public function getDataForPaieCalendar($personnel null)
  176.     {
  177.         $data = [];
  178.         $params = [
  179.             "paie"     =>true,
  180.             "pointage" =>false,
  181.             "resultats-paie"=>false,
  182.             "resultats-pointage"=>false,
  183.         ];
  184.         $operations  $this->getOperationQb($params)
  185.                             ->getQuery()
  186.                             ->getResult();
  187.         
  188.         foreach ($operations as $operation) {
  189.             $data[]= [
  190.                 'start' => $operation->getDate()->format('Y-m-d'),
  191.                 'title' =>  $operation->getPersonnel() .' : '.$operation->getMontant() .'DH',
  192.                 'textColor' => 'black',
  193.                 'extendedProps'=> [
  194.                     'id'=> $operation->getId()
  195.                 ]
  196.             ];
  197.         }
  198.         return $data;
  199.     }
  200.     public function getMontantDataForPaieCalendar($personnel null$groupe null $dateDebut null $dateFin  null)
  201.     {
  202.         $data[]= [
  203.             'montantTotal' => $this->paieService->getMontantTotal($personnel$groupe$dateDebut $dateFin ),
  204.             'montantAvance' =>  $this->paieService->getMontantAvance($personnel$groupe$dateDebut $dateFin ),
  205.             'montantPaye' =>  $this->paieService->getMontantPaye($personnel$groupe$dateDebut $dateFin),
  206.             'montantRestePaye' =>  $this->paieService->getMontantRestePayer($personnel$groupe $dateDebut $dateFin)
  207.         ]
  208.         ;
  209.         return $data;
  210.     }
  211.     private function getCountOperations(QueryBuilder $qb)
  212.     {
  213.         return (int)$qb->select('COUNT(o)')
  214.         ->getQuery()
  215.         ->getSingleScalarResult();
  216.     }
  217.     private function paginateDatatable(QueryBuilder $qb$start$length)
  218.     {
  219.         $query $qb->getQuery();
  220.         $query->setFirstResult($start)
  221.                 ->setMaxResults($length);
  222.         return new Paginator($query);
  223.     }
  224.     private function getOperationQb($params)
  225.     {
  226.         $operationQb = ($params["paie"] ? $this->repositoryPaie $this->repository)->createQueryBuilder('o')
  227.                             ->where('o.deletedAt is NULL');
  228.         
  229.         $operationQb $this->searchWihSessionForOperation($operationQb$params);
  230.         return $operationQb;
  231.     }
  232.     private function searchWihSessionForOperation(QueryBuilder $qb$params)
  233.     {
  234.         $operationSearch $this->getSessionForSearch($params);
  235.         
  236.         if (!$operationSearch instanceof OperationSearch) {
  237.             $operationSearch = new operationSearch();
  238.         }
  239.         $qb $this->repository->searchWithSession($qb$operationSearch,$params);
  240.         return $qb;
  241.     }
  242.     private function filtersDatatable(QueryBuilder $qb$orders$search$params)
  243.     {
  244.         return ($params["paie"] ? $this->repositoryPaie $this->repository)->filterDatatable($qb$orders$search);
  245.     }
  246.     public function getSessionForSearch($params)
  247.     {
  248.         $searchSession = ($params["paie"] || $params["resultats-paie"] || $params["resultats-pointage"]) ? $this->session->get('operationPaieSearch') : $this->session->get('operationSearch');
  249.         return $searchSession;
  250.     }
  251.     public function setOperationSearchForm()
  252.     {
  253.         $operationSearch = new OperationSearch();
  254.         $operationSearchSession $this->session->get('operationSearch');
  255.         
  256.         if ($operationSearchSession) {
  257.             if ($operationSearchSession->personnel) {
  258.                 $personnel $this->em->getRepository(TblPersonnel::class)->find($operationSearchSession->personnel->getId());
  259.                 $operationSearch->personnel $personnel;
  260.             }
  261.             if ($operationSearchSession->groupe) {
  262.                 $groupe $this->em->getRepository(RefGroupe::class)->find($operationSearchSession->groupe->getId());
  263.                 $operationSearch->groupe $groupe;
  264.             }
  265.             if ($operationSearchSession->dateDebut) {
  266.                 $operationSearch->dateDebut $operationSearchSession->dateDebut;
  267.             } 
  268.             if ($operationSearchSession->dateFin) {
  269.                 $operationSearch->dateFin $operationSearchSession->dateFin;
  270.             }
  271.         }
  272.         return $operationSearch;
  273.     }
  274.     public function setOperationSearchPaieForm($isResultats false)
  275.     {
  276.         $operationSearch = new OperationSearch();
  277.         $operationSearchSession $this->session->get('operationPaieSearch');
  278.         if(!$isResultats){
  279.             //pour le moment afficher que les avances dans calendar
  280.             $typeOperation $this->em->getRepository(RefTypeOperationPaie::class)->find(RefTypeOperationPaie::AVANCE);
  281.             $operationSearch->typeOperation $typeOperation;
  282.         }
  283.         if ($operationSearchSession) {
  284.             
  285.             if ($operationSearchSession->personnel ) {
  286.                 $personnel $this->em->getRepository(TblPersonnel::class)->find($operationSearchSession->personnel->getId());
  287.                 $operationSearch->personnel $personnel;      
  288.             }
  289.             if ($operationSearchSession->groupe) {
  290.                 $groupe $this->em->getRepository(RefGroupe::class)->find($operationSearchSession->groupe->getId());
  291.                 $operationSearch->groupe $groupe;
  292.             }
  293.             if ($operationSearchSession->typeOperationTravail) {
  294.                 $typeOperationTravail $this->em->getRepository(RefTypeOperationTravail::class)->find($operationSearchSession->typeOperationTravail->getId());
  295.                 $operationSearch->typeOperationTravail $typeOperationTravail;
  296.             }
  297.             if ($operationSearchSession->dateDebut) {
  298.                 $operationSearch->dateDebut $operationSearchSession->dateDebut;
  299.             }
  300.             if ($operationSearchSession->dateFin) {
  301.                 $operationSearch->dateFin $operationSearchSession->dateFin;
  302.             }
  303.         }
  304.         return $operationSearch;
  305.     }
  306.     public function setTypeOperationPaie(TblOperationPaie $tblOperationPaie$typePaie RefTypeOperationPaie::AVANCE)
  307.     {
  308.         if(!$tblOperationPaie->getTypeOperation()){
  309.             $typePaie $this->repositoryTypePaie->find(intval($typePaie));
  310.             $tblOperationPaie->setTypeOperation($typePaie);
  311.         }
  312.         return $tblOperationPaie;
  313.     }
  314.     public function filltblOperationPaieTemplatePdf(TblPersonnel $personnel$dateDebut $dateFin   )
  315.     {
  316.         $params $this->getParamsOperationPaieTemplate($personnel$dateDebut $dateFin); 
  317.         $fileName=$personnel->getNom().'-'.(new DateTime())->format('d-m-Y');
  318.         $outputFile $this->template_personnel_opperationPaie_generer_dir.$fileName.'.docx';
  319.         $outputFilePdf $this->template_personnel_opperationPaie_generer_dir.$fileName.'.pdf';
  320.         $templateFileDocx $this->template_personnel_opperationPaie_dir.'template-justificatif-paie.docx';
  321.         $templatePersonnel = new TemplateProcessor($templateFileDocx);
  322.         $operationTravails $this->em->getRepository(TblOperationTravail::class)->getOperationsByDate($personnel$dateDebut $dateFin );
  323.         $nbrLigne count($operationTravails) > count($operationTravails) : ;
  324.         $templatePersonnel->cloneRow('datepointage',$nbrLigne);
  325.         if($operationTravails){
  326.             foreach($operationTravails as $key => $tblOperationTravail)
  327.             {
  328.                 $templatePersonnel->setValue('datepointage#'.($key+1), $tblOperationTravail->getDateOperation()->format('d/m/Y'));
  329.                 $templatePersonnel->setValue('nbr_heures#'.($key+1), $tblOperationTravail->getDureeOperationTravail());
  330.                 $templatePersonnel->setValue('typeoperation#'.($key+1), $tblOperationTravail->getTypeOperationTravail()->getTypeOperation());
  331.                 $templatePersonnel->setValue('total#'.($key+1), number_format($tblOperationTravail->getMontantTJM()*$tblOperationTravail->getDureeOperationTravail()->getValeurJour(), 2','' '));
  332.             }
  333.             $templatePersonnel->setValue('dateFinPaiement'strval(  $dateFin $dateFin->format('d/m/Y') : $tblOperationTravail->getDateOperation()->format('d/m/Y') )) ;
  334.         }else{
  335.             $templatePersonnel->setValue('datepointage#1'"--");
  336.             $templatePersonnel->setValue('nbr_heures#1'"--");
  337.             $templatePersonnel->setValue('typeoperation#1'"--");
  338.             $templatePersonnel->setValue('total#1'"--");
  339.             $templatePersonnel->setValue('dateFinPaiement', (new DateTime())->format('d/m/Y'));
  340.         }
  341.         $templatePersonnel->setValue('montantTotal'number_format($this->paieService->getMontantTotalPointage($personnelnull$dateDebut $dateFin), 2','' '));
  342.         $templatePersonnel->setValue('heuresTotal'$this->paieService->getHeuresTotal($personnel$dateDebut $dateFin ));      
  343.         $operationPaie $this->em->getRepository(TblOperationPaie::class)->getOperationsPaieByDate($personnel$dateDebut $dateFin );
  344.         $nbrLigneOP count($operationPaie) > count($operationPaie) : ;
  345.         $templatePersonnel->cloneRow('date',$nbrLigneOP);
  346.         if($operationPaie){
  347.             foreach($operationPaie as $key => $tblOperationPaie)
  348.             { 
  349.                 $templatePersonnel->setValue('date#'.($key+1), $tblOperationPaie->getDate()->format('d/m/Y'));
  350.                 $templatePersonnel->setValue('type_paiement#'.($key+1), $tblOperationPaie->getTypeOperation());
  351.                 $templatePersonnel->setValue('nbr_travailler#'.($key+1), number_format($tblOperationPaie->getMontant(), 2','' '));       
  352.                 $templatePersonnel->setValue('montantAvance'number_format(($this->paieService->getMontantAvance($personnelnull$dateDebut $dateFin) + $this->paieService->getMontantPaye($personnelnull$dateDebut $dateFin)), 2','' ')); 
  353.             }
  354.         }else{
  355.             $templatePersonnel->setValue('date#1'"--");
  356.             $templatePersonnel->setValue('type_paiement#1'"--");
  357.             $templatePersonnel->setValue('nbr_travailler#1'"--");
  358.             $templatePersonnel->setValue('montantAvance'"--");
  359.         }
  360.         foreach($params as $index => $param)
  361.         {
  362.             $index trim($index,"{}");
  363.             $templatePersonnel->setValue($index$param->value);
  364.         }
  365.         $templatePersonnel->setValue('montantRestePaye'number_format$this->paieService->getMontantRestePayer($personnel null$dateDebut $dateFin), 2','' '));
  366.         $templatePersonnel->setValue('montanttotal'number_format$this->paieService-> getMontantTotal$personnel ,  null,  $dateDebut  $dateFin ), 2','' '));
  367.         $templatePersonnel->saveAs($outputFile);
  368.         $libreOffice = new LibreOffice('dev');
  369.         $libreOffice->convert_docx_to_pdf($outputFile$outputFilePdf);
  370.         unlink($outputFile);
  371.         $response = new BinaryFileResponse($outputFilePdf);
  372.         $response->deleteFileAfterSend(true);
  373.         $response->headers->set('Content-Type''application/pdf');
  374.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE);
  375.         return $response;
  376.     }
  377.     public function getParamsOperationPaieTemplate(TblPersonnel $personnel $dateDebut  $dateFin )
  378.     { 
  379.         $params = [
  380.             '{nomPrenomPersonnel}' => new ExcelParam(CellSetterStringValue::class, $personnel),
  381.             '{codeRHPersonnel}' => new ExcelParam(CellSetterStringValue::class, strval($personnel->getCodeRh())),
  382.             '{dateDebutPaiement}' => new ExcelParam(CellSetterStringValue::class, strval($dateDebut $dateDebut->format('d/m/Y') : $personnel->getCreatedAt()->format('d/m/Y') )  ),
  383.             '{Adresse}' => new ExcelParam(CellSetterStringValue::class, strval($personnel->getVille())),
  384.             '{dateDuJour}' => new ExcelParam(CellSetterStringValue::class, (new DateTime())->format('d/m/Y')),
  385.             '{montantEnLettre}' => new ExcelParam(CellSetterStringValue::class, strtoupper$this->chiffreToLettreService->Conversion($this->paieService->getMontantRestePayer($personnel,null$dateDebut $dateFin)))),
  386.         ];
  387.           
  388.         return $params;
  389.     }
  390. }