src/Repository/EmployeeRepository.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Employee;
  4. use App\Enum\EmployeeHistory\SalaryType;
  5. use App\Helper\All;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\ORM\Query;
  8. use Doctrine\ORM\QueryBuilder;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. /**
  11.  * @extends ServiceEntityRepository<Employee>
  12.  *
  13.  * @method Employee|null find($id, $lockMode = null, $lockVersion = null)
  14.  * @method Employee|null findOneBy(array $criteria, array $orderBy = null)
  15.  * @method Employee[]    findAll()
  16.  * @method Employee[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  17.  */
  18. class EmployeeRepository extends ServiceEntityRepository
  19. {
  20.     public function __construct(ManagerRegistry $registry)
  21.     {
  22.         parent::__construct($registryEmployee::class);
  23.     }
  24.     public function add(Employee $entitybool $flush false): void
  25.     {
  26.         $this->getEntityManager()->persist($entity);
  27.         if ($flush) {
  28.             $this->getEntityManager()->flush();
  29.         }
  30.     }
  31.     public function remove(Employee $entitybool $flush false): void
  32.     {
  33.         $this->getEntityManager()->remove($entity);
  34.         if ($flush) {
  35.             $this->getEntityManager()->flush();
  36.         }
  37.     }
  38.     public function getListTable($q$filters$orderByColumn$orderDirection$limit$offset)
  39.     {
  40.         $query $this->createQueryBuilder('e')
  41.             ->leftJoin('e.lastHistory''eh')
  42.             ->leftJoin('eh.department''ehd')
  43.             ->leftJoin('eh.position''ehp');
  44.         $recordsTotal count($query->getQuery()->getResult());
  45.         if (!empty($filters)) {
  46.             if (true === filter_var($filters['active'], FILTER_VALIDATE_BOOLEAN)) {
  47.                 $query $query
  48.                     ->andWhere($query->expr()->isNull("eh.endDate"))
  49.                     ->orWhere("eh.endDate >= :today");
  50.             } else {
  51.                 $query $query
  52.                     ->andWhere($query->expr()->isNotNull("eh.endDate"))
  53.                     ->orWhere("eh.endDate <= :today");
  54.             }
  55.             if (isset($filters['departments'])) {
  56.                 //$filters['departments'] = json_decode($filters['departments']);
  57.                 $query->andWhere('eh.department in (:departments)')->setParameter('departments'$filters['departments']);
  58.             }
  59.             $query->setParameter('today', (new \DateTime())->format('Y-m-d'));
  60.         }
  61.         if (!empty($q)) {
  62.             $query
  63.                 ->where('e.firstName like :q')
  64.                 ->orWhere('e.lastName like :q')
  65.                 ->orWhere('e.cin like :q')
  66.                 ->setParameters(
  67.                     [
  68.                         'q' => '%' $q '%',
  69.                     ]
  70.                 );
  71.         }
  72.         $recordsFiltered count($query->getQuery()->getResult());
  73.         $query $query
  74.             ->groupBy('e.id')
  75.             ->orderBy($orderByColumn$orderDirection)
  76.             ->getQuery();
  77.         if (!empty($limit)) {
  78.             $query $query->setMaxResults($limit)->setFirstResult($offset);
  79.         }
  80.         return [
  81.             "recordsTotal" => $recordsTotal,
  82.             "recordsFiltered" => $recordsFiltered,
  83.             "data" => $query->getResult(),
  84.         ];
  85.     }
  86.     public function hourlyEmployees()
  87.     {
  88.     }
  89.     /**
  90.      * List of employees with history
  91.      */
  92.     public function activeAndHourlyEmployees($onlyQB false)
  93.     {
  94.         $qb $this->createQueryBuilder('e')
  95.             ->join('e.lastHistory''eh')
  96.             ->join('eh.department''ehd')
  97.             ->join('eh.position''ehp');
  98.         $qb $qb
  99.             ->where($qb->expr()->eq("eh.salaryType"SalaryType::HOURLY->value))
  100.             ->andWhere($qb->expr()->isNull("eh.endDate"))
  101.             ->orWhere($qb->expr()->lte("eh.endDate", (new \DateTime())->format('Y-m-d')))
  102.             ->orderBy('e.id');
  103.         //$qb->setMaxResults(60);
  104.         if ($onlyQB) {
  105.             return $qb;
  106.         }
  107.         return $qb->getQuery()->getResult();
  108.     }
  109.     /**
  110.      * List of employees with active history in date
  111.      */
  112.     public function activeInDateAndHourlyEmployees($startDate$endDate)
  113.     {
  114.         $qb $this->createQueryBuilder('e');
  115.         $qb
  116.             ->join('e.employeeHistories''eh'Query\Expr\Join::WITH$qb->expr()->eq("eh.salaryType"SalaryType::HOURLY->value))
  117.             ->join('eh.department''ehd')
  118.             ->join('eh.position''ehp');
  119.         $startDate = (\DateTime::createFromFormat('d/m/Y'$startDate))->format('Y-m-d');
  120.         $endDate = (\DateTime::createFromFormat('d/m/Y'$endDate))->format('Y-m-d');
  121.         $qb $qb
  122.             //->where('date(eh.endDate) <= :startDate OR date(eh.endDate) >= :endDate')
  123.                 //02/08 - 04/08
  124.                 //18/07
  125.             ->where('date(eh.endDate) >= :startDate OR date(eh.endDate) >= :endDate')
  126.             ->orWhere($qb->expr()->isNull("eh.endDate"))
  127.             ->andWhere('date(eh.startDate) <= :endDate')
  128.             ->setParameters(
  129.                 [
  130.                     'startDate' => $startDate,
  131.                     'endDate' => $endDate
  132.                 ]
  133.             )
  134.             ->orderBy('e.id');
  135. //            ->where($qb->expr()->between("eh.endDate", $startDate, All::paramForExpr($endDate)))
  136. //            ->orWhere($qb->expr()->isNull("eh.endDate"))
  137. //            ->andWhere($qb->expr()->lte("eh.startDate", All::paramForExpr($startDate)))
  138. //            ->orderBy('e.lastName')
  139. //            ->addOrderBy('e.firstName');
  140. //        echo $startDate;
  141. //        echo "<br>";
  142. //        echo $endDate;
  143. //        echo "<br>";
  144. //        die();
  145. //        foreach ($qb->getQuery()->getResult() as $item){
  146. //            /** @var Employee $item */
  147. //            $endDate = !empty($item->getLastHistory()->getEndDate()) ? $item->getLastHistory()->getEndDate()->format('d/m/Y H:i:s') : '';
  148. //            echo $item->getFullName() . " - ".$item->getId() . " - ". $item->getLastHistory()->getStartDate()->format('d/m/Y H:i:s') . " - " . $endDate;
  149. //            echo "<br>";
  150. //        }
  151. //
  152. //        die();
  153.         //dd($qb->getQuery()->getSQL(), $startDate, $endDate);
  154.         return $qb->getQuery()->getResult();
  155.     }
  156.     /**
  157.      * @TODO: TO REFACTO AND REMOVE
  158.      */
  159.     public function activeAndHourlyLogForEmployees($employee false$date false)
  160.     {
  161.         /** @var QueryBuilder $qb */
  162.         $qb $this->activeAndHourlyEmployees(true);
  163.         $conditionType null;
  164.         $condition null;
  165.         if (!empty($employee)) {
  166.             $qb->andWhere('e.id = :employee')->setParameter('employee'$employee);
  167.         }
  168.         if (!empty($date)) {
  169.             $conditionType Query\Expr\Join::WITH;
  170.             $condition $qb->expr()->like('ehl.date'':logDate');
  171.             $qb->setParameter('logDate'$date->format('Y-m-d') . '%');
  172.         }
  173.         $qb->leftJoin('e.employeeHourLogs''ehl'$conditionType$condition);
  174.         $qb->orderBy('eh.department');
  175.         $qb->addOrderBy('ehp.id');
  176.         return $qb->getQuery()
  177.             ->getResult();
  178.     }
  179.     public function getOnlyMachinary(){
  180.         /** @var QueryBuilder $qb */
  181.         $qb $this->activeAndHourlyEmployees(true);
  182.         return $qb->andWhere('ehd = :department')->setParameter('department'4)->getQuery()->getResult();
  183.     }
  184. }