src/Repository/FlatRepository.php line 1200

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Flat;
  4. use App\Entity\Building;
  5. use App\Entity\Resident;
  6. use App\Services\FlatService;
  7. use DateTime;
  8. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  9. use Doctrine\ORM\NonUniqueResultException;
  10. use Doctrine\ORM\NoResultException;
  11. use Doctrine\ORM\OptimisticLockException;
  12. use Doctrine\ORM\QueryBuilder;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use App\Entity\User;
  15. use Doctrine\ORM\Query\Expr\Join;
  16. use PDepend\Util\Coverage\Factory;
  17. use PDepend\Util\Type;
  18. use App\Utils\ApiV2\ListItemsOptions as ListItemsOptionsV2;
  19. /**
  20.  * @method Flat|null find($id, $lockMode = null, $lockVersion = null)
  21.  * @method Flat|null findOneBy(array $criteria, array $orderBy = null)
  22.  * @method Flat[]    findAll()
  23.  * @method Flat[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  24.  */
  25. class FlatRepository extends BaseRepository
  26. {
  27.     /**
  28.      * BuildingRepository constructor.
  29.      *
  30.      * @param ManagerRegistry $registry
  31.      */
  32.     public function __construct(ManagerRegistry $registry)
  33.     {
  34.         $this->entityClass Flat::class;
  35.         parent::__construct($registry);
  36.     }
  37.     public function flush(): void
  38.     {
  39.         $this->_em->flush();
  40.     }
  41.     /**
  42.      * @param int $buildingId
  43.      *
  44.      * @return array<mixed>
  45.      */
  46.     public function getFlatsByBuildingIdNoUser(int $buildingId): array
  47.     {
  48.         return $this->createQueryBuilder('f')
  49.             ->innerJoin('f.building''b')
  50.             ->where('f.building = :building')
  51.             ->andWhere('f.archived = 0')
  52.             ->select([
  53.                 'f.id',
  54.                 'f.displayFlatId',
  55.                 'f.ownerName',
  56.                 'f.ownerLastname',
  57.                 'f.companyName',
  58.                 'f.type',
  59.                 'f.subType',
  60.                 'b.id as buildingId'
  61.             ])
  62.             ->setParameter('building'$buildingId)
  63.             ->getQuery()
  64.             ->getResult();
  65.     }
  66.     /**
  67.      * @param User $user
  68.      * @param Building $building
  69.      * @param int $archived
  70.      * @return array<mixed>
  71.      */
  72.     public function getFlatsByBuilding(User $userBuilding $buildingint $archived 0): array
  73.     {
  74.         $query $this->createQueryBuilder('f')
  75.             ->select('f, u')
  76.             ->innerJoin('f.user''u')
  77.             ->where('f.user = :user')
  78.             ->andWhere('f.building = :building');
  79.         if ($archived) {
  80.             $query->andWhere('f.archived = 0');
  81.         }
  82.         return $query
  83.             ->setParameters([
  84.                 'user' => $user,
  85.                 'building' => $building
  86.             ])
  87.             ->orderBy('f.displayFlatId''ASC')
  88.             ->getQuery()
  89.             ->getArrayResult();
  90.     }
  91.     /**
  92.      * @param User $user
  93.      * @param Building $building
  94.      * @param int $archived
  95.      * @return array<mixed>
  96.      */
  97.     public function getFlatsByBuildingOrderedById(User $userBuilding $buildingint $archived 0): array
  98.     {
  99.         $query $this->createQueryBuilder('f')
  100.             ->select('f, u')
  101.             ->innerJoin('f.user''u')
  102.             ->where('f.user = :user')
  103.             ->andWhere('f.building = :building');
  104.         if ($archived) {
  105.             $query->andWhere('f.archived = 0');
  106.         }
  107.         $query $query
  108.             ->setParameters([
  109.                 'user' => $user,
  110.                 'building' => $building
  111.             ])
  112.             ->orderBy('f.id''ASC')
  113.             ->getQuery()
  114.             ->getArrayResult();
  115.         usort($query, fn($a$b) => (int) $a['displayFlatId'] <=> (int) $b['displayFlatId']);
  116.         return $query;
  117.     }
  118.     /**
  119.      * @param User $user
  120.      * @param Building $building
  121.      * @param int $archived
  122.      * @return int
  123.      * @throws NoResultException
  124.      * @throws NonUniqueResultException
  125.      */
  126.     public function countFlatsByBuildingWithOccupant(User $userBuilding $buildingint $archived 0): int
  127.     {
  128.         return $this->createQueryBuilder('f')
  129.             ->select('count(f.id)')
  130.             ->innerJoin('f.user''u')
  131.             ->where('f.user = :user')
  132.             ->andWhere('f.building = :building')
  133.             ->andWhere('f.occupantEmail is not null')
  134.             ->andWhere('f.archived = 0')
  135.             ->setParameters([
  136.                 'user' => $user,
  137.                 'building' => $building
  138.             ])
  139.             ->getQuery()
  140.             ->getSingleScalarResult();
  141.     }
  142.     /**
  143.      * @param User $user
  144.      * @param Building $building
  145.      * @param int $archived
  146.      * @return float|null
  147.      * @throws NoResultException
  148.      * @throws NonUniqueResultException
  149.      */
  150.     public function getFlatsByBuildingOldDebitSum(User $userBuilding $buildingint $archived 0): float|null
  151.     {
  152.         $query $this->createQueryBuilder('f')
  153.             ->select('sum(f.oldDebit) as sumOldDebit')
  154.             ->innerJoin('f.user''u')
  155.             ->where('f.user = :user')
  156.             ->andWhere('f.building = :building');
  157.         if ($archived) {
  158.             $query->andWhere('f.archived = 0');
  159.         }
  160.         return $query
  161.             ->setParameters([
  162.                 'user' => $user,
  163.                 'building' => $building
  164.             ])->getQuery()
  165.             ->getSingleScalarResult();
  166.     }
  167.     /**
  168.      * @param User $user
  169.      * @param Building $building
  170.      * @param int $archived
  171.      * @return array<Flat>
  172.      */
  173.     public function getFlatsByBuildingArray(User $userBuilding $buildingint $archived 0): array
  174.     {
  175.         $query $this->createQueryBuilder('f')
  176.             ->select('f, u')
  177.             ->innerJoin('f.user''u')
  178.             ->where('f.user = :user')
  179.             ->andWhere('f.building = :building');
  180.         if ($archived) {
  181.             $query->andWhere('f.archived = 0');
  182.         }
  183.         return $query
  184.             ->setParameters([
  185.                 'user' => $user,
  186.                 'building' => $building
  187.             ])
  188.             ->orderBy('f.displayFlatId''ASC')
  189.             ->getQuery()
  190.             ->getResult();
  191.     }
  192.     /**
  193.      * @param array<User> $users
  194.      * @param DateTime|null $lastDayOfPreviousMont
  195.      * @return int
  196.      * @throws NoResultException
  197.      * @throws NonUniqueResultException
  198.      */
  199.     public function getActiveFlatsByUser(array $usersDateTime $lastDayOfPreviousMont null): int
  200.     {
  201.         $query $this->createQueryBuilder('f')
  202.             ->select('count(f.id)')
  203.             ->leftJoin('f.building''b')
  204.             ->where('b.activeStatus = 1')
  205.             ->andWhere('f.user in (:users)')
  206.             ->andWhere('f.archived = 0');
  207.         if (!empty($lastDayOfPreviousMont)) {
  208.             $query->andWhere('f.createdAt < :lastDayOfPreviousMonth')
  209.                 ->setParameter('lastDayOfPreviousMonth'$lastDayOfPreviousMont);
  210.         }
  211.         return $query->setParameter('users'$users)
  212.             ->getQuery()
  213.             ->getSingleScalarResult();
  214.     }
  215.     /**
  216.      * @param int $user
  217.      * @return array<Flat>
  218.      */
  219.     public function getActiveFlatsObjectsByUser(int $user): array
  220.     {
  221.         return $this->createQueryBuilder('f')
  222.             ->leftJoin('f.building''b')
  223.             ->where('b.activeStatus = 1')
  224.             ->andWhere('f.user = :user')
  225.             ->andWhere('f.archived = 0')
  226.             ->setParameters([
  227.                 'user' => $user,
  228.             ])
  229.             ->getQuery()
  230.             ->getResult();
  231.     }
  232.     /**
  233.      * @param User $user
  234.      * @param Building $building
  235.      * @return array<Flat>
  236.      */
  237.     public function getActiveFlatsByUserAndBuilding(User $userBuilding $building): array
  238.     {
  239.         return $this->createQueryBuilder('f')
  240.             ->where('f.building = :building')
  241.             ->andWhere('f.user = :user')
  242.             ->setParameters([
  243.                 'user' => $user,
  244.                 'building' => $building
  245.             ])
  246.             ->getQuery()
  247.             ->getResult();
  248.     }
  249.     /**
  250.      * @param User $user
  251.      * @return array<Flat>
  252.      */
  253.     public function getActiveFlatsArrayByUser(User $user): array
  254.     {
  255.         return $this->createQueryBuilder('f')
  256.             ->leftJoin('f.building''b')
  257.             ->where('b.activeStatus = 1')
  258.             ->andWhere('f.user = :user')
  259.             ->setParameters([
  260.                 'user' => $user,
  261.             ])
  262.             ->getQuery()
  263.             ->getArrayResult();
  264.     }
  265.     /**
  266.      * @param User $user
  267.      * @return int
  268.      * @throws NoResultException
  269.      * @throws NonUniqueResultException
  270.      */
  271.     public function getInactiveFlatsByUser(User $user): int
  272.     {
  273.         return $this->createQueryBuilder('f')
  274.             ->select('count(f.id)')
  275.             ->leftJoin('f.building''b')
  276.             ->where('b.activeStatus = 0')
  277.             ->andWhere('f.user = :user')
  278.             ->setParameters([
  279.                 'user' => $user,
  280.             ])
  281.             ->getQuery()
  282.             ->getSingleScalarResult();
  283.     }
  284.     /**
  285.      * @param array<int> $flatIds
  286.      *
  287.      * @return array<Flat>
  288.      */
  289.     public function findByIds(array $flatIds): array
  290.     {
  291.         $qb $this->createQueryBuilder('f');
  292.         return $qb->where($qb->expr()->in('f.id'$flatIds))
  293.             ->getQuery()
  294.             ->getResult();
  295.     }
  296.     /**
  297.      * @param User $user
  298.      * @param Building $building
  299.      * @param int $archived
  300.      *
  301.      * @return array<Flat>
  302.      */
  303.     public function getFlatsObjByBuilding(User $userBuilding $buildingint $archived 0): array
  304.     {
  305.         $query $this->createQueryBuilder('f')
  306.             ->where('f.user = :user')
  307.             ->andWhere('f.building = :building');
  308.         if ($archived) {
  309.             $query->andWhere('f.archived = 0');
  310.         }
  311.         $query
  312.             ->setParameters([
  313.                 'user' => $user,
  314.                 'building' => $building
  315.             ])
  316.             ->orderBy('f.displayFlatId''ASC');
  317.         return $query
  318.             ->getQuery()
  319.             ->getResult();
  320.     }
  321.     /**
  322.      * @param User $user
  323.      * @param Building $building
  324.      * @param string $number
  325.      * @return Flat|null
  326.      * @throws NonUniqueResultException
  327.      */
  328.     public function getFlatsObjByBuildingAndDisplayFlatId(User $userBuilding $buildingstring $number): ?Flat
  329.     {
  330.         return $this->createQueryBuilder('f')
  331.             ->where('f.user = :user')
  332.             ->andWhere('f.building = :building')
  333.             ->andWhere('f.displayFlatId = :number')
  334.             ->setParameters([
  335.                 'user' => $user,
  336.                 'building' => $building,
  337.                 'number' => $number
  338.             ])
  339.             ->getQuery()
  340.             ->getOneOrNullResult();
  341.     }
  342.     /**
  343.      * @param User $user
  344.      * @param int $id
  345.      *
  346.      * @return Flat
  347.      * @throws NonUniqueResultException
  348.      */
  349.     public function getFlatById(User $userint $id): Flat
  350.     {
  351.         return $this->createQueryBuilder('f')
  352.             ->where('f.user = :user')
  353.             ->andWhere('f.id = :id')
  354.             ->setParameters([
  355.                 'user' => $user,
  356.                 'id' => $id
  357.             ])
  358.             ->getQuery()
  359.             ->getOneOrNullResult();
  360.     }
  361.     /**
  362.      * @param User $user
  363.      * @param array<int> $ids
  364.      *
  365.      * @return array<Flat>
  366.      * @throws NonUniqueResultException
  367.      */
  368.     public function getFlatsById(User $user, array $ids): array
  369.     {
  370.         return $this->createQueryBuilder('f')
  371.             ->where('f.user = :user')
  372.             ->andWhere('f.id IN (:ids)')
  373.             ->setParameters([
  374.                 'user' => $user,
  375.                 'ids' => $ids
  376.             ])
  377.             ->getQuery()
  378.             ->getOneOrNullResult();
  379.     }
  380.     /**
  381.      * @param Building $building
  382.      * @param array<int|string> $ids
  383.      * @param bool|null $exclude
  384.      * @return array<string>
  385.      * @throws NonUniqueResultException
  386.      */
  387.     public function getSumFlatsById(Building $building, array $idsbool $exclude null): array
  388.     {
  389.         $query $this->createQueryBuilder('f')
  390.             ->select('sum(f.members) as totalMembers, sum(f.size) as sizeTotal')
  391.             ->where('f.building = :building')
  392.             ->andWhere('f.archived = 0');
  393.         if ($exclude) {
  394.             $query->andWhere('f.displayFlatId NOT IN (:ids)');
  395.         } else {
  396.             $query->andWhere('f.displayFlatId IN (:ids)');
  397.         }
  398.         $query->setParameters([
  399.             'ids' => $ids,
  400.             'building' => $building
  401.         ]);
  402.         return $query->getQuery()
  403.             ->getOneOrNullResult();
  404.     }
  405.     /**
  406.      * @param array<Flat> $flats
  407.      *
  408.      * @return array<Flat>
  409.      */
  410.     public function assoccFlatArray(array $flats): array
  411.     {
  412.         $ret = [];
  413.         $ret['all'] = 'All';
  414.         /** @var Flat $flat */
  415.         foreach ($flats as $flat) {
  416.             $ret[$flat['displayFlatId']] = $flat['id'];
  417.         }
  418.         return $ret;
  419.     }
  420.     /**
  421.      * @param Building $building
  422.      * @param array<mixed> $flatNumbersComplex
  423.      *
  424.      * @return array<Flat>
  425.      */
  426.     public function getDistinctFlatSizesFiltered(Building $building, array $flatNumbersComplex): array
  427.     {
  428.         $query $this->createQueryBuilder('f')
  429.             ->select('f.size')
  430.             ->where('f.building = :building');
  431.         if ($flatNumbersComplex['participants'][0] != 0) {
  432.             if ($flatNumbersComplex['excludeFromBill']) {
  433.                 $query->andWhere('f.displayFlatId NOT IN (:flatNumbers)');
  434.             } else {
  435.                 $query->andWhere('f.displayFlatId IN (:flatNumbers)');
  436.             }
  437.             $query->setParameters([
  438.                 'building' => $building,
  439.                 'flatNumbers' => $flatNumbersComplex['participants']
  440.             ]);
  441.         } else {
  442.             $query->setParameters([
  443.                 'building' => $building
  444.             ]);
  445.         }
  446.         $values $query->distinct()
  447.             ->getQuery()
  448.             ->getArrayResult();
  449.         asort($values);
  450.         return $values;
  451.     }
  452.     /**
  453.      * @return int
  454.      * @throws NoResultException
  455.      * @throws NonUniqueResultException
  456.      */
  457.     public function countFlats(): int
  458.     {
  459.         return (int)$this->createQueryBuilder('f')
  460.             ->select('count(f.id) as sumFlats')
  461.             ->getQuery()
  462.             ->getSingleScalarResult();
  463.     }
  464.     /**
  465.      * @return int
  466.      * @throws NoResultException
  467.      * @throws NonUniqueResultException
  468.      */
  469.     public function countActiveFlats(): int
  470.     {
  471.         return (int)$this->createQueryBuilder('f')
  472.             ->innerJoin('f.building''b')
  473.             ->where('b.activeStatus = 1')
  474.             ->select('count(f.id) as sumFlats')
  475.             ->getQuery()
  476.             ->getSingleScalarResult();
  477.     }
  478.     /**
  479.      * @param Building $building
  480.      * @param User $user
  481.      * @return array<string>
  482.      */
  483.     public function getFlatEmails(Building $buildingUser $user): array
  484.     {
  485.         $emails $this->createQueryBuilder('f')
  486.             ->select('f.ownerEmail')
  487.             ->where('f.building = :building')
  488.             ->andWhere('f.user = :user')
  489.             ->setParameters([
  490.                 'building' => $building,
  491.                 'user' => $user
  492.             ])
  493.             ->getQuery()
  494.             ->getArrayResult();
  495.         $filteredEmails = [];
  496.         foreach ($emails as $email) {
  497.             if ($email['ownerEmail'] != '') {
  498.                 $filteredEmails[] = $email['ownerEmail'];
  499.             }
  500.         }
  501.         return $filteredEmails;
  502.     }
  503.     /**
  504.      * @param User $user
  505.      * @param Building $building
  506.      * @return float
  507.      * @throws NonUniqueResultException
  508.      */
  509.     public function getBuildingFlatsCurrentDebitSum(User $userBuilding $building): float
  510.     {
  511.         return $this->createQueryBuilder('f')
  512.             ->select('sum(f.debit) as currentDebitSum')
  513.             ->where('f.user = :user')
  514.             ->andWhere('f.building = :building')
  515.             ->setParameters([
  516.                 'user' => $user,
  517.                 'building' => $building
  518.             ])
  519.             ->getQuery()
  520.             ->getOneOrNullResult();
  521.     }
  522.     /**
  523.      * @param User $user
  524.      * @param Building $building
  525.      * @return float
  526.      * @throws NonUniqueResultException|NoResultException
  527.      */
  528.     public function getBuildingFlatsCurrentFullDebitSum(User $userBuilding $building): float
  529.     {
  530.         return $this->createQueryBuilder('f')
  531.             ->select('sum(f.debit) as currentDebitSum')
  532.             ->where('f.user = :user')
  533.             ->andWhere('f.building = :building')
  534.             ->andWhere('f.archived = 0')
  535.             ->setParameters([
  536.                 'user' => $user,
  537.                 'building' => $building
  538.             ])
  539.             ->getQuery()
  540.             ->getSingleScalarResult();
  541.     }
  542.     /**
  543.      * @param Building $building
  544.      * @return array<Flat>
  545.      */
  546.     public function getFlatsWithEmail(Building $building): array
  547.     {
  548.         return $this->createQueryBuilder('f')
  549.             ->andWhere('f.building = :building')
  550.             ->andWhere('f.ownerEmail is not null')
  551.             ->setParameters([
  552.                 'building' => $building
  553.             ])
  554.             ->getQuery()
  555.             ->getResult();
  556.     }
  557.     /**
  558.      * @param Building $building
  559.      * @return array<array<string>>
  560.      */
  561.     public function getDistinctOwnerEmails(Building $building): array
  562.     {
  563.         return $this->createQueryBuilder('f')
  564.             ->select('DISTINCT f.ownerEmail')
  565.             ->andWhere('f.building = :building')
  566.             ->andWhere('f.ownerEmail IS NOT NULL')
  567.             ->andWhere('f.emailCanBeUsed = 1')
  568.             ->andWhere('f.archived = 0')
  569.             ->setParameters([
  570.                 'building' => $building
  571.             ])
  572.             ->getQuery()
  573.             ->getResult();
  574.     }
  575.     /**
  576.      * @param User $user
  577.      * @param Building $building
  578.      *
  579.      * @return array<Flat>
  580.      */
  581.     public function findFlatsWithDebitByBuilding(User $userBuilding $building): array
  582.     {
  583.         return $this->createQueryBuilder('f')
  584.             ->where('f.user = :user')
  585.             ->andWhere('f.building = :building')
  586.             ->andWhere('f.archived = 0')
  587.             ->andWhere('(f.debit + f.oldDebit)<>0 OR f.previousBill is not null')
  588.             ->setParameters([
  589.                 'user' => $user,
  590.                 'building' => $building
  591.             ])
  592.             ->orderBy('f.displayFlatId + 0''ASC')
  593.             ->getQuery()
  594.             ->getResult();
  595.     }
  596.     /**
  597.      * @param int $start
  598.      * @param int $limit
  599.      *
  600.      * @return array<mixed>
  601.      */
  602.     public function findFlatsForMoneyLogData(int $startint $limit): array
  603.     {
  604.         return $this->createQueryBuilder('f')
  605.             ->setFirstResult($start)
  606.             ->setMaxResults($limit)
  607.             ->getQuery()
  608.             ->getResult();
  609.     }
  610.     /**
  611.      * @param Building $building
  612.      * @return int|mixed|string|null
  613.      * @throws NonUniqueResultException
  614.      */
  615.     public function findLastArchiveFlatsByBuilding(Building $building): mixed
  616.     {
  617.         return $this->createQueryBuilder('f')
  618.             ->select('f.displayFlatId')
  619.             ->andWhere('f.building = :building')
  620.             ->andWhere('f.archived = 1')
  621.             ->setParameters([
  622.                 'building' => $building
  623.             ])
  624.             ->orderBy('f.archivedAt''DESC')
  625.             ->setMaxResults(1)
  626.             ->getQuery()
  627.             ->getOneOrNullResult();
  628.     }
  629.     /**
  630.      * @param User $user
  631.      * @param Building $building
  632.      * @return string
  633.      * @throws NonUniqueResultException|NoResultException
  634.      */
  635.     public function countArchiveFlatsByBuilding(User $userBuilding $building): string
  636.     {
  637.         return $this->createQueryBuilder('f')
  638.             ->select('count(f)')
  639.             ->where('f.user = :user')
  640.             ->andWhere('f.building = :building')
  641.             ->andWhere('f.archived = 1')
  642.             ->setParameters([
  643.                 'user' => $user,
  644.                 'building' => $building
  645.             ])
  646.             ->getQuery()
  647.             ->getSingleScalarResult();
  648.     }
  649.     /**
  650.      * @param Flat $currentFlat
  651.      * @param User $user
  652.      *
  653.      * @return array<Flat>
  654.      */
  655.     public function getArchivedFlatsByCurrentFlat(Flat $currentFlatUser $user): array
  656.     {
  657.         return $this->createQueryBuilder('f')
  658.             ->where('f.user = :user')
  659.             ->andWhere('f.currentFlat = :currentFlat')
  660.             ->andWhere('f.archived = 1')
  661.             ->setParameters([
  662.                 'currentFlat' => $currentFlat,
  663.                 'user' => $user
  664.             ])
  665.             ->getQuery()
  666.             ->getResult();
  667.     }
  668.     /**
  669.      * @param User $user
  670.      * @param Building $building
  671.      *
  672.      * @return array<Flat>
  673.      */
  674.     public function findArchivedFlatsWithDebitByBuilding(User $userBuilding $building): array
  675.     {
  676.         return $this->createQueryBuilder('f')
  677.             ->where('f.user = :user')
  678.             ->andWhere('f.building = :building')
  679.             ->andWhere('f.archived = 1')
  680.             ->andWhere('((f.debit + f.previousBill) <> 0) OR (f.oldDebit > 0)')
  681.             ->setParameters([
  682.                 'user' => $user,
  683.                 'building' => $building
  684.             ])
  685.             ->orderBy('f.displayFlatId + 0''ASC')
  686.             ->getQuery()
  687.             ->getResult();
  688.     }
  689.     /**
  690.      * @param Building $building
  691.      * @param string $flatNumber
  692.      * @return Flat|null
  693.      * @throws NonUniqueResultException
  694.      */
  695.     public function getFlatByFlatNumber(Building $buildingstring $flatNumber): ?Flat
  696.     {
  697.         return $this->createQueryBuilder('f')
  698.             ->where('f.building = :building')
  699.             ->andWhere('f.displayFlatId = :flatNumber')
  700.             ->setParameters([
  701.                 'building' => $building,
  702.                 'flatNumber' => $flatNumber
  703.             ])
  704.             ->getQuery()
  705.             ->getOneOrNullResult();
  706.     }
  707.     /**
  708.      * @param User $user
  709.      * @param Building $building
  710.      * @return array<Flat>
  711.      */
  712.     public function getArchivedFlatsObjByBuilding(User $userBuilding $building): array
  713.     {
  714.         $query $this->createQueryBuilder('f')
  715.             ->where('f.user = :user')
  716.             ->andWhere('f.building = :building')
  717.             ->andWhere('f.archived = 1')
  718.             ->setParameters([
  719.                 'user' => $user,
  720.                 'building' => $building
  721.             ])
  722.             ->orderBy('f.displayFlatId''ASC');
  723.         return $query
  724.             ->getQuery()
  725.             ->getResult();
  726.     }
  727.     /**
  728.      * @param User $user
  729.      * @param Building $building
  730.      * @return array<Flat>
  731.      */
  732.     public function getFlatsObjectsByBuilding(User $userBuilding $building): array
  733.     {
  734.         $query $this->createQueryBuilder('f')
  735.             ->where('f.user = :user')
  736.             ->andWhere('f.building = :building')
  737.             ->andWhere('f.archived = 0')
  738.             ->setParameters([
  739.                 'user' => $user,
  740.                 'building' => $building
  741.             ])
  742.             ->orderBy('f.displayFlatId''ASC');
  743.         return $query
  744.             ->getQuery()
  745.             ->getResult();
  746.     }
  747.     /**
  748.      * @param User $user
  749.      * @param Building $building
  750.      * @param bool $haveEmail
  751.      * @return array<Flat>
  752.      */
  753.     public function getFlatsByEmailForOldDebit(User $userBuilding $buildingbool $haveEmail): array
  754.     {
  755.         $query $this->createQueryBuilder('f')
  756.             ->where('f.user = :user')
  757.             ->andWhere('f.building = :building')
  758.             ->andWhere('f.oldDebit > 0')
  759.             ->setParameters([
  760.                 'user' => $user,
  761.                 'building' => $building,
  762.             ]);
  763.         if ($haveEmail) {
  764.             $query->andWhere('f.emailCanBeUsed = 1');
  765.         } else {
  766.             $query->andWhere('f.emailCanBeUsed = 0');
  767.         }
  768.         return $query
  769.             ->orderBy('f.displayFlatId''ASC')
  770.             ->getQuery()
  771.             ->getResult();
  772.     }
  773.     /**
  774.      * @param Building $building
  775.      * @return int
  776.      * @throws NoResultException
  777.      * @throws NonUniqueResultException
  778.      */
  779.     public function countFlatsByPhoneNumberForOldDebit(Building $building): int
  780.     {
  781.         return $this->createQueryBuilder('f')
  782.             ->select('COUNT(f.id)')
  783.             ->where('f.building = :building')
  784.             ->andWhere('f.oldDebit > 0')
  785.             ->andWhere('f.ownerPhoneNumber is not null')
  786.             ->setParameters([
  787.                 'building' => $building,
  788.             ])
  789.             ->getQuery()
  790.             ->getSingleScalarResult();
  791.     }
  792.     /**
  793.      * @param Building $building
  794.      * @return array<Flat>
  795.      */
  796.     public function getFlatsByPhoneNumberForOldDebit(Building $building): array
  797.     {
  798.         return  $this->createQueryBuilder('f')
  799.             ->where('f.building = :building')
  800.             ->andWhere('f.oldDebit > 0')
  801.             ->andWhere('f.ownerPhoneNumber is not null')
  802.             ->setParameters([
  803.                 'building' => $building,
  804.             ])
  805.             ->orderBy('f.displayFlatId''ASC')
  806.             ->getQuery()
  807.             ->getResult();
  808.     }
  809.     /**
  810.      * @param Building $building
  811.      * @return int
  812.      * @throws NoResultException
  813.      * @throws NonUniqueResultException
  814.      */
  815.     public function countFlatsByPhoneNumberForSendBill(Building $building): int
  816.     {
  817.         return $this->createQueryBuilder('f')
  818.             ->select('COUNT(f.id)')
  819.             ->where('f.building = :building')
  820.             ->andWhere('f.ownerPhoneNumber is not null')
  821.             ->setParameters([
  822.                 'building' => $building,
  823.             ])
  824.             ->getQuery()
  825.             ->getSingleScalarResult();
  826.     }
  827.     /**
  828.      * @param Building $building
  829.      * @return array<Flat>
  830.      */
  831.     public function getFlatsCountByTypeAndBuilding(Building $building): array
  832.     {
  833.         return $this->createQueryBuilder('f''f.type')
  834.             ->select('f.type, count(f.type) as sumFlats')
  835.             ->where('f.building = :building')
  836.             ->andWhere('f.archived = 0')
  837.             ->setParameters([
  838.                 'building' => $building,
  839.             ])
  840.             ->groupBy('f.type')
  841.             ->getQuery()
  842.             ->getArrayResult();
  843.     }
  844.     /**
  845.      * @param User $user
  846.      * @param Building $building
  847.      * @return array<Flat>
  848.      */
  849.     public function getFlatsDetailsByBuilding(User $userBuilding $building): array
  850.     {
  851.         return $this->createQueryBuilder('f''f.displayFlatId')
  852.             ->select(
  853.                 'f.displayFlatId as flatDisplayId,
  854.                 f.members as flatMembers,
  855.                 f.size as flatSize,
  856.                 f.type as flatType'
  857.             )
  858.             ->where('f.user = :user')
  859.             ->andWhere('f.building = :building')
  860.             ->andWhere('f.archived = 0')
  861.             ->setParameters([
  862.                 'user' => $user,
  863.                 'building' => $building
  864.             ])
  865.             ->orderBy('f.displayFlatId''ASC')
  866.             ->getQuery()
  867.             ->getArrayResult();
  868.     }
  869.     /**
  870.      * @param User $user
  871.      * @param Building $building
  872.      * @return array
  873.      */
  874.     public function getAllFlatsDetailsByBuilding(User $userBuilding $building): array
  875.     {
  876.         return $this->createQueryBuilder('f''f.displayFlatId')
  877.             ->select(
  878.                 'f.displayFlatId as flatDisplayId,
  879.                 f.members as flatMembers,
  880.                 f.ownerName as ownerName,
  881.                 f.ownerLastname as ownerLastname,
  882.                 f.companyName as companyName,
  883.                 f.ownerAddress as ownerAddress,
  884.                 f.size as flatSize,
  885.                 f.type as flatType'
  886.             )
  887.             ->where('f.user = :user')
  888.             ->andWhere('f.building = :building')
  889.             ->andWhere('f.archived = 0')
  890.             ->setParameters([
  891.                 'user' => $user,
  892.                 'building' => $building
  893.             ])
  894.             ->orderBy('f.displayFlatId''ASC')
  895.             ->getQuery()
  896.             ->getArrayResult();
  897.     }
  898.     /**
  899.      * @param User $user
  900.      * @return array<Flat>
  901.      */
  902.     public function getFlatsByMobileAppUser(User $user): array
  903.     {
  904.         $query $this->createQueryBuilder('f')
  905.             ->where('f.flatUser = :user')
  906.             ->andWhere('f.archived = 0')
  907.             ->setParameters([
  908.                 'user' => $user,
  909.             ])
  910.             ->orderBy('f.displayFlatId''ASC');
  911.         return $query
  912.             ->getQuery()
  913.             ->getResult();
  914.     }
  915.     /**
  916.      * @param DateTime $startDate
  917.      * @param DateTime|null $endDate
  918.      * @return array<mixed>
  919.      */
  920.     public function getFlatImportReviewByDate(DateTime $startDate, ?DateTime $endDate): array
  921.     {
  922.         $result $this->createQueryBuilder('f')
  923.             ->select('count(f.id) as flatNumber, u.email as user')
  924.             ->innerJoin('f.user''u'Join::WITH'f.user = u.id')
  925.             ->where('f.createdAt >= :startDate')
  926.             ->groupBy('user')
  927.             ->setParameters([
  928.                 'startDate' => $startDate,
  929.             ]);
  930.         if ($endDate) {
  931.             $result
  932.                 ->andWhere('f.createdAt <= :endDate')
  933.                 ->setParameter('endDate'$endDate);
  934.         }
  935.         return $result
  936.             ->getQuery()
  937.             ->getResult();
  938.     }
  939.     /**
  940.      * @param User $user
  941.      * @param Building $building
  942.      * @param int $archived
  943.      * @return array<Flat>
  944.      */
  945.     public function getActiveFlatsByUserAndBuildingAndDebit(User $userBuilding $buildingint $archived): array
  946.     {
  947.         return $this->createQueryBuilder('f')
  948.             ->where('f.building = :building')
  949.             ->andWhere('f.user =:user')
  950.             ->andWhere('(f.debit + f.previousBill) > 0')
  951.             ->andWhere('f.archived = :archived')
  952.             ->setParameters([
  953.                 'user' => $user,
  954.                 'building' => $building,
  955.                 'archived' => $archived
  956.             ])
  957.             ->getQuery()
  958.             ->getResult();
  959.     }
  960.     /**
  961.      * @param Building $building
  962.      * @return float
  963.      * @throws NoResultException
  964.      * @throws NonUniqueResultException
  965.      */
  966.     public function calculateBuildingSize(Building $building): float
  967.     {
  968.         return $this->createQueryBuilder('f')
  969.             ->select('sum(f.size)')
  970.             ->where('f.building = :building')
  971.             ->andWhere('f.archived = 0')
  972.             ->setParameters([
  973.                 'building' => $building
  974.             ])
  975.             ->getQuery()
  976.             ->getSingleScalarResult();
  977.     }
  978.     /**
  979.      * @param Building $building
  980.      * @return array<Flat>
  981.      */
  982.     public function getFlatsWithActiveEmailForDictionary(Building $building): array
  983.     {
  984.         $emails $this->createQueryBuilder('f')
  985.             ->select('f.ownerEmail')
  986.             ->where('f.building = :building')
  987.             ->andWhere('f.emailCanBeUsed = 1')
  988.             ->setParameter('building'$building)
  989.             ->getQuery()
  990.             ->getArrayResult();
  991.         $filteredEmails = [];
  992.         foreach ($emails as $email) {
  993.             if ($email['ownerEmail'] != '') {
  994.                 if (!in_array($email['ownerEmail'], $filteredEmails)) {
  995.                     $filteredEmails[] = $email['ownerEmail'];
  996.                 }
  997.             }
  998.         }
  999.         return $filteredEmails;
  1000.     }
  1001.     /**
  1002.      * @param Building $building
  1003.      * @param User $user
  1004.      * @return array<Flat>
  1005.      */
  1006.     public function findFlatByOwner(Building $buildingUser $user): array
  1007.     {
  1008.         return $this->createQueryBuilder('f')
  1009.             ->select('f.ownerName, f.ownerLastname, f.displayFlatId, f.type, f.companyName, f.type')
  1010.             ->where('f.building = :building')
  1011.             ->andWhere('f.user = :user')
  1012.             ->andWhere('f.archived = 0')
  1013.             ->setParameters([
  1014.                 'user' => $user,
  1015.                 'building' => $building
  1016.             ])
  1017.             ->getQuery()
  1018.             ->getResult();
  1019.     }
  1020.     /**
  1021.      * @return array<Flat>
  1022.      */
  1023.     public function getAllArchivedFlats(): array
  1024.     {
  1025.         return $this->createQueryBuilder('f')
  1026.             ->where('f.archived = 1')
  1027.             ->getQuery()
  1028.             ->getResult();
  1029.     }
  1030.     /**
  1031.      * @param User $user
  1032.      * @param Building $building
  1033.      * @return array<mixed>|null
  1034.      * @throws NonUniqueResultException
  1035.      */
  1036.     public function findFlatsBillDebitAndPreviousBillSum(User $userBuilding $building): ?array
  1037.     {
  1038.         return $this->createQueryBuilder('f')
  1039.             ->select('(sum(f.debit + f.previousBill)) as billSum')
  1040.             ->where('f.user = :user')
  1041.             ->andWhere('f.building = :building')
  1042.             ->andWhere('f.archived = 0')
  1043.             ->setParameters([
  1044.                 'user' => $user,
  1045.                 'building' => $building
  1046.             ])
  1047.             ->getQuery()
  1048.             ->getOneOrNullResult();
  1049.     }
  1050.     /**
  1051.      * @param array<User> $users
  1052.      * @return array<Flat>
  1053.      */
  1054.     public function findFlatsForInvoice(array $users): array
  1055.     {
  1056.         return $this->createQueryBuilder('f')
  1057.             ->where('f.eInvoice = true')
  1058.             ->andwhere('f.user IN (:users)')
  1059.             ->andWhere('f.archived = 0')
  1060.             ->setParameters([
  1061.                 'users' => $users
  1062.             ])
  1063.             ->getQuery()
  1064.             ->getResult();
  1065.     }
  1066.     /**
  1067.      * @param Building $building
  1068.      * @param ListItemsOptionsV2 $options
  1069.      * @return array<Flat>
  1070.      */
  1071.     public function getFlatsByBuildingPaginated(Building $buildingListItemsOptionsV2 $options): array
  1072.     {
  1073.         return $this->createQueryBuilder('f')
  1074.             ->where('f.building = :building')
  1075.             ->andWhere('f.archived = 0')
  1076.             ->orderBy('f.' $options->getOrderByColumn(), $options->getOrderByDirection())
  1077.             ->setParameter('building'$building)
  1078.             ->setMaxResults($options->getLimit())
  1079.             ->setFirstResult($options->getOffset())
  1080.             ->getQuery()
  1081.             ->getResult();
  1082.     }
  1083.     /**
  1084.      * @param Building $building
  1085.      * @return int
  1086.      * @throws NoResultException
  1087.      * @throws NonUniqueResultException
  1088.      */
  1089.     public function countFlatsByBuildingPaginated(Building $building): int
  1090.     {
  1091.         return $this->createQueryBuilder('f')
  1092.             ->select('count(f.id) as sumFlats')
  1093.             ->where('f.building = :building')
  1094.             ->andWhere('f.archived = 0')
  1095.             ->setParameter('building'$building)
  1096.             ->getQuery()
  1097.             ->getSingleScalarResult();
  1098.     }
  1099.     /**
  1100.      * @param int $limit
  1101.      * @param int $offset
  1102.      * @return array<int, array<string, int>>
  1103.      */
  1104.     public function getAllFlats(int $limitint $offset): array
  1105.     {
  1106.         return $this->createQueryBuilder('f')
  1107.             ->select('f.id')
  1108.             ->orderBy('f.id')
  1109.             ->setMaxResults($limit)
  1110.             ->setFirstResult($offset)
  1111.             ->getQuery()
  1112.             ->getArrayResult();
  1113.     }
  1114.     /**
  1115.      * @param array<User> $users
  1116.      * @return int
  1117.      * @throws NoResultException
  1118.      * @throws NonUniqueResultException
  1119.      */
  1120.     public function countFlatsForInvoice(array $users): int
  1121.     {
  1122.         return $this->createQueryBuilder('f')
  1123.             ->select('count(f.id) as sumFlats')
  1124.             ->andwhere('f.user IN (:users)')
  1125.             ->andWhere('f.eInvoice = true')
  1126.             ->setParameter('users'$users)
  1127.             ->getQuery()
  1128.             ->getSingleScalarResult();
  1129.     }
  1130.     /**
  1131.      * @param array<User> $users
  1132.      * @return int
  1133.      * @throws NoResultException
  1134.      * @throws NonUniqueResultException
  1135.      */
  1136.     public function countFlatsForPrintBill(array $users): int
  1137.     {
  1138.         return $this->createQueryBuilder('f')
  1139.             ->select('count(f.id) as sumFlats')
  1140.             ->andwhere('f.user IN (:users)')
  1141.             ->andWhere('f.printBill = true')
  1142.             ->setParameter('users'$users)
  1143.             ->getQuery()
  1144.             ->getSingleScalarResult();
  1145.     }
  1146.     /**
  1147.      * @param Building $building
  1148.      * @param int $limit
  1149.      * @param int $offset
  1150.      * @return array<Flat>
  1151.      */
  1152.     public function getAllFlatsByBuilding(Building $buildingint $limitint $offset): array
  1153.     {
  1154.         return $this->createQueryBuilder('f')
  1155.             ->where('f.building = :building')
  1156.             ->andWhere('f.archived = 0')
  1157.             ->orderBy('f.id')
  1158.             ->setParameter('building'$building)
  1159.             ->setMaxResults($limit)
  1160.             ->setFirstResult($offset)
  1161.             ->getQuery()
  1162.             ->getResult();
  1163.     }
  1164.     /**
  1165.      * @param Resident $resident
  1166.      * @return array<Flat>
  1167.      */
  1168.     public function getAllFlatsByResident(Resident $resident): array
  1169.     {
  1170.         return $this->createQueryBuilder('f')
  1171.             ->where('f.resident = :resident')
  1172.             ->andWhere('f.archived = 0')
  1173.             ->orderBy('f.id')
  1174.             ->setParameter('resident'$resident)
  1175.             ->getQuery()
  1176.             ->getResult();
  1177.     }
  1178.     /**
  1179.      * @param int $buildingId
  1180.      * @return array<Flat>
  1181.      */
  1182.     public function findFlatsByBuildingAndOwnerPhoneNumber(int $buildingId): array
  1183.     {
  1184.         return $this->createQueryBuilder('f')
  1185.             ->where('f.building = :buildingId')
  1186.             ->andWhere('f.ownerPhoneNumber IS NOT NULL')
  1187.             ->setParameter('buildingId'$buildingId)
  1188.             ->orderBy('f.displayFlatId''ASC')
  1189.             ->getQuery()
  1190.             ->getResult();
  1191.     }
  1192.     /**
  1193.      * @param array $searchParams
  1194.      * @param int|null $start
  1195.      * @param int|null $limit
  1196.      * @param array $order
  1197.      * @param string|null $search
  1198.      * @return array<mixed>
  1199.      */
  1200.     public function findFlatsForDatatable(
  1201.         array $searchParams,
  1202.         ?int $start,
  1203.         ?int $limit,
  1204.         array $order,
  1205.         string $search null
  1206.     ): array {
  1207.         $query $this->createQueryBuilder('f')
  1208.             ->select(
  1209.                 'f.id,
  1210.                 f.displayFlatId,
  1211.                 f.size,
  1212.                 f.ownerName,
  1213.                 f.ownerLastname,
  1214.                 f.companyName,
  1215.                 f.ownerPhoneNumber,
  1216.                 f.debit,
  1217.                 f.previousBill,
  1218.                 f.type,
  1219.                 f.subType,
  1220.                 f.oldDebit,
  1221.                 f.ownerEmail,
  1222.                 f.emailCanBeUsed,
  1223.                 b.address,
  1224.                 b.id as buildingId'
  1225.             )
  1226.             ->innerJoin('f.building''b')
  1227.             ->where('b.activeStatus = 1')
  1228.             ->andWhere('f.user IN (:users)')
  1229.             ->andWhere('f.archived = 0')
  1230.             ->setParameter('users'$searchParams['users'])
  1231.             ->setMaxResults($limit)
  1232.             ->setFirstResult($start);
  1233.         if ($order['column'] === 'address') {
  1234.             $query->orderBy('b.address'$order['dir']);
  1235.         } else {
  1236.             $query->orderBy('f.' $order['column'], $order['dir']);
  1237.         }
  1238.         if ($search) {
  1239.             $query->andWhere(
  1240.                 'f.ownerName LIKE :search OR
  1241.                 f.ownerLastname LIKE :search OR
  1242.                 f.displayFlatId LIKE :search OR
  1243.                 f.ownerPhoneNumber LIKE :search OR
  1244.                 f.ownerEmail LIKE :search OR
  1245.                 b.address LIKE :search'
  1246.             );
  1247.             $query->setParameter('search''%' $search '%');
  1248.         }
  1249.         return $query
  1250.             ->getQuery()
  1251.             ->getArrayResult();
  1252.     }
  1253.     /**
  1254.      * @param array $searchParams
  1255.      * @param string|null $search
  1256.      * @return int
  1257.      * @throws NoResultException
  1258.      * @throws NonUniqueResultException
  1259.      */
  1260.     public function findFlatsForDatatableCount(array $searchParamsstring $search null): int
  1261.     {
  1262.         $qb $this->createQueryBuilder('f')
  1263.             ->select('COUNT(f.id)')
  1264.             ->innerJoin('f.building''b')
  1265.             ->where('b.activeStatus = 1')
  1266.             ->andWhere('f.user IN (:users)')
  1267.             ->andWhere('f.archived = 0')
  1268.             ->setParameter('users'$searchParams['users']);
  1269.         if ($search) {
  1270.             $qb->andWhere(
  1271.                 'f.ownerName LIKE :search OR
  1272.                 f.ownerLastname LIKE :search OR
  1273.                 f.displayFlatId LIKE :search OR
  1274.                 f.ownerPhoneNumber LIKE :search OR
  1275.                 f.ownerEmail LIKE :search OR
  1276.                 b.address LIKE :search'
  1277.             )
  1278.                 ->setParameter('search''%' $search '%');
  1279.         }
  1280.         return (int) $qb->getQuery()->getSingleScalarResult();
  1281.     }
  1282. }