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.             ->andWhere('f.debit > 0')
  536.             ->setParameters([
  537.                 'user' => $user,
  538.                 'building' => $building
  539.             ])
  540.             ->getQuery()
  541.             ->getSingleScalarResult();
  542.     }
  543.     /**
  544.      * @param Building $building
  545.      * @return array<Flat>
  546.      */
  547.     public function getFlatsWithEmail(Building $building): array
  548.     {
  549.         return $this->createQueryBuilder('f')
  550.             ->andWhere('f.building = :building')
  551.             ->andWhere('f.ownerEmail is not null')
  552.             ->setParameters([
  553.                 'building' => $building
  554.             ])
  555.             ->getQuery()
  556.             ->getResult();
  557.     }
  558.     /**
  559.      * @param Building $building
  560.      * @return array<array<string>>
  561.      */
  562.     public function getDistinctOwnerEmails(Building $building): array
  563.     {
  564.         return $this->createQueryBuilder('f')
  565.             ->select('DISTINCT f.ownerEmail')
  566.             ->andWhere('f.building = :building')
  567.             ->andWhere('f.ownerEmail IS NOT NULL')
  568.             ->andWhere('f.emailCanBeUsed = 1')
  569.             ->andWhere('f.archived = 0')
  570.             ->setParameters([
  571.                 'building' => $building
  572.             ])
  573.             ->getQuery()
  574.             ->getResult();
  575.     }
  576.     /**
  577.      * @param User $user
  578.      * @param Building $building
  579.      *
  580.      * @return array<Flat>
  581.      */
  582.     public function findFlatsWithDebitByBuilding(User $userBuilding $building): array
  583.     {
  584.         return $this->createQueryBuilder('f')
  585.             ->where('f.user = :user')
  586.             ->andWhere('f.building = :building')
  587.             ->andWhere('f.archived = 0')
  588.             ->andWhere('(f.debit + f.oldDebit)<>0 OR f.previousBill is not null')
  589.             ->setParameters([
  590.                 'user' => $user,
  591.                 'building' => $building
  592.             ])
  593.             ->orderBy('f.displayFlatId + 0''ASC')
  594.             ->getQuery()
  595.             ->getResult();
  596.     }
  597.     /**
  598.      * @param int $start
  599.      * @param int $limit
  600.      *
  601.      * @return array<mixed>
  602.      */
  603.     public function findFlatsForMoneyLogData(int $startint $limit): array
  604.     {
  605.         return $this->createQueryBuilder('f')
  606.             ->setFirstResult($start)
  607.             ->setMaxResults($limit)
  608.             ->getQuery()
  609.             ->getResult();
  610.     }
  611.     /**
  612.      * @param Building $building
  613.      * @return int|mixed|string|null
  614.      * @throws NonUniqueResultException
  615.      */
  616.     public function findLastArchiveFlatsByBuilding(Building $building): mixed
  617.     {
  618.         return $this->createQueryBuilder('f')
  619.             ->select('f.displayFlatId')
  620.             ->andWhere('f.building = :building')
  621.             ->andWhere('f.archived = 1')
  622.             ->setParameters([
  623.                 'building' => $building
  624.             ])
  625.             ->orderBy('f.archivedAt''DESC')
  626.             ->setMaxResults(1)
  627.             ->getQuery()
  628.             ->getOneOrNullResult();
  629.     }
  630.     /**
  631.      * @param User $user
  632.      * @param Building $building
  633.      * @return string
  634.      * @throws NonUniqueResultException|NoResultException
  635.      */
  636.     public function countArchiveFlatsByBuilding(User $userBuilding $building): string
  637.     {
  638.         return $this->createQueryBuilder('f')
  639.             ->select('count(f)')
  640.             ->where('f.user = :user')
  641.             ->andWhere('f.building = :building')
  642.             ->andWhere('f.archived = 1')
  643.             ->setParameters([
  644.                 'user' => $user,
  645.                 'building' => $building
  646.             ])
  647.             ->getQuery()
  648.             ->getSingleScalarResult();
  649.     }
  650.     /**
  651.      * @param Flat $currentFlat
  652.      * @param User $user
  653.      *
  654.      * @return array<Flat>
  655.      */
  656.     public function getArchivedFlatsByCurrentFlat(Flat $currentFlatUser $user): array
  657.     {
  658.         return $this->createQueryBuilder('f')
  659.             ->where('f.user = :user')
  660.             ->andWhere('f.currentFlat = :currentFlat')
  661.             ->andWhere('f.archived = 1')
  662.             ->setParameters([
  663.                 'currentFlat' => $currentFlat,
  664.                 'user' => $user
  665.             ])
  666.             ->getQuery()
  667.             ->getResult();
  668.     }
  669.     /**
  670.      * @param User $user
  671.      * @param Building $building
  672.      *
  673.      * @return array<Flat>
  674.      */
  675.     public function findArchivedFlatsWithDebitByBuilding(User $userBuilding $building): array
  676.     {
  677.         return $this->createQueryBuilder('f')
  678.             ->where('f.user = :user')
  679.             ->andWhere('f.building = :building')
  680.             ->andWhere('f.archived = 1')
  681.             ->andWhere('((f.debit + f.previousBill) <> 0) OR (f.oldDebit > 0)')
  682.             ->setParameters([
  683.                 'user' => $user,
  684.                 'building' => $building
  685.             ])
  686.             ->orderBy('f.displayFlatId + 0''ASC')
  687.             ->getQuery()
  688.             ->getResult();
  689.     }
  690.     /**
  691.      * @param Building $building
  692.      * @param string $flatNumber
  693.      * @return Flat|null
  694.      * @throws NonUniqueResultException
  695.      */
  696.     public function getFlatByFlatNumber(Building $buildingstring $flatNumber): ?Flat
  697.     {
  698.         return $this->createQueryBuilder('f')
  699.             ->where('f.building = :building')
  700.             ->andWhere('f.displayFlatId = :flatNumber')
  701.             ->setParameters([
  702.                 'building' => $building,
  703.                 'flatNumber' => $flatNumber
  704.             ])
  705.             ->getQuery()
  706.             ->getOneOrNullResult();
  707.     }
  708.     /**
  709.      * @param User $user
  710.      * @param Building $building
  711.      * @return array<Flat>
  712.      */
  713.     public function getArchivedFlatsObjByBuilding(User $userBuilding $building): array
  714.     {
  715.         $query $this->createQueryBuilder('f')
  716.             ->where('f.user = :user')
  717.             ->andWhere('f.building = :building')
  718.             ->andWhere('f.archived = 1')
  719.             ->setParameters([
  720.                 'user' => $user,
  721.                 'building' => $building
  722.             ])
  723.             ->orderBy('f.displayFlatId''ASC');
  724.         return $query
  725.             ->getQuery()
  726.             ->getResult();
  727.     }
  728.     /**
  729.      * @param User $user
  730.      * @param Building $building
  731.      * @return array<Flat>
  732.      */
  733.     public function getFlatsObjectsByBuilding(User $userBuilding $building): array
  734.     {
  735.         $query $this->createQueryBuilder('f')
  736.             ->where('f.user = :user')
  737.             ->andWhere('f.building = :building')
  738.             ->andWhere('f.archived = 0')
  739.             ->setParameters([
  740.                 'user' => $user,
  741.                 'building' => $building
  742.             ])
  743.             ->orderBy('f.displayFlatId''ASC');
  744.         return $query
  745.             ->getQuery()
  746.             ->getResult();
  747.     }
  748.     /**
  749.      * @param User $user
  750.      * @param Building $building
  751.      * @param bool $haveEmail
  752.      * @return array<Flat>
  753.      */
  754.     public function getFlatsByEmailForOldDebit(User $userBuilding $buildingbool $haveEmail): array
  755.     {
  756.         $query $this->createQueryBuilder('f')
  757.             ->where('f.user = :user')
  758.             ->andWhere('f.building = :building')
  759.             ->andWhere('f.oldDebit > 0')
  760.             ->setParameters([
  761.                 'user' => $user,
  762.                 'building' => $building,
  763.             ]);
  764.         if ($haveEmail) {
  765.             $query->andWhere('f.emailCanBeUsed = 1');
  766.         } else {
  767.             $query->andWhere('f.emailCanBeUsed = 0');
  768.         }
  769.         return $query
  770.             ->orderBy('f.displayFlatId''ASC')
  771.             ->getQuery()
  772.             ->getResult();
  773.     }
  774.     /**
  775.      * @param Building $building
  776.      * @return int
  777.      * @throws NoResultException
  778.      * @throws NonUniqueResultException
  779.      */
  780.     public function countFlatsByPhoneNumberForOldDebit(Building $building): int
  781.     {
  782.         return $this->createQueryBuilder('f')
  783.             ->select('COUNT(f.id)')
  784.             ->where('f.building = :building')
  785.             ->andWhere('f.oldDebit > 0')
  786.             ->andWhere('f.ownerPhoneNumber is not null')
  787.             ->setParameters([
  788.                 'building' => $building,
  789.             ])
  790.             ->getQuery()
  791.             ->getSingleScalarResult();
  792.     }
  793.     /**
  794.      * @param Building $building
  795.      * @return array<Flat>
  796.      */
  797.     public function getFlatsByPhoneNumberForOldDebit(Building $building): array
  798.     {
  799.         return  $this->createQueryBuilder('f')
  800.             ->where('f.building = :building')
  801.             ->andWhere('f.oldDebit > 0')
  802.             ->andWhere('f.ownerPhoneNumber is not null')
  803.             ->setParameters([
  804.                 'building' => $building,
  805.             ])
  806.             ->orderBy('f.displayFlatId''ASC')
  807.             ->getQuery()
  808.             ->getResult();
  809.     }
  810.     /**
  811.      * @param Building $building
  812.      * @return int
  813.      * @throws NoResultException
  814.      * @throws NonUniqueResultException
  815.      */
  816.     public function countFlatsByPhoneNumberForSendBill(Building $building): int
  817.     {
  818.         return $this->createQueryBuilder('f')
  819.             ->select('COUNT(f.id)')
  820.             ->where('f.building = :building')
  821.             ->andWhere('f.ownerPhoneNumber is not null')
  822.             ->setParameters([
  823.                 'building' => $building,
  824.             ])
  825.             ->getQuery()
  826.             ->getSingleScalarResult();
  827.     }
  828.     /**
  829.      * @param Building $building
  830.      * @return array<Flat>
  831.      */
  832.     public function getFlatsCountByTypeAndBuilding(Building $building): array
  833.     {
  834.         return $this->createQueryBuilder('f''f.type')
  835.             ->select('f.type, count(f.type) as sumFlats')
  836.             ->where('f.building = :building')
  837.             ->andWhere('f.archived = 0')
  838.             ->setParameters([
  839.                 'building' => $building,
  840.             ])
  841.             ->groupBy('f.type')
  842.             ->getQuery()
  843.             ->getArrayResult();
  844.     }
  845.     /**
  846.      * @param User $user
  847.      * @param Building $building
  848.      * @return array<Flat>
  849.      */
  850.     public function getFlatsDetailsByBuilding(User $userBuilding $building): array
  851.     {
  852.         return $this->createQueryBuilder('f''f.displayFlatId')
  853.             ->select(
  854.                 'f.displayFlatId as flatDisplayId,
  855.                 f.members as flatMembers,
  856.                 f.size as flatSize,
  857.                 f.type as flatType'
  858.             )
  859.             ->where('f.user = :user')
  860.             ->andWhere('f.building = :building')
  861.             ->andWhere('f.archived = 0')
  862.             ->setParameters([
  863.                 'user' => $user,
  864.                 'building' => $building
  865.             ])
  866.             ->orderBy('f.displayFlatId''ASC')
  867.             ->getQuery()
  868.             ->getArrayResult();
  869.     }
  870.     /**
  871.      * @param User $user
  872.      * @param Building $building
  873.      * @return array
  874.      */
  875.     public function getAllFlatsDetailsByBuilding(User $userBuilding $building): array
  876.     {
  877.         return $this->createQueryBuilder('f''f.displayFlatId')
  878.             ->select(
  879.                 'f.displayFlatId as flatDisplayId,
  880.                 f.members as flatMembers,
  881.                 f.ownerName as ownerName,
  882.                 f.ownerLastname as ownerLastname,
  883.                 f.companyName as companyName,
  884.                 f.ownerAddress as ownerAddress,
  885.                 f.size as flatSize,
  886.                 f.type as flatType'
  887.             )
  888.             ->where('f.user = :user')
  889.             ->andWhere('f.building = :building')
  890.             ->andWhere('f.archived = 0')
  891.             ->setParameters([
  892.                 'user' => $user,
  893.                 'building' => $building
  894.             ])
  895.             ->orderBy('f.displayFlatId''ASC')
  896.             ->getQuery()
  897.             ->getArrayResult();
  898.     }
  899.     /**
  900.      * @param User $user
  901.      * @return array<Flat>
  902.      */
  903.     public function getFlatsByMobileAppUser(User $user): array
  904.     {
  905.         $query $this->createQueryBuilder('f')
  906.             ->where('f.flatUser = :user')
  907.             ->andWhere('f.archived = 0')
  908.             ->setParameters([
  909.                 'user' => $user,
  910.             ])
  911.             ->orderBy('f.displayFlatId''ASC');
  912.         return $query
  913.             ->getQuery()
  914.             ->getResult();
  915.     }
  916.     /**
  917.      * @param DateTime $startDate
  918.      * @param DateTime|null $endDate
  919.      * @return array<mixed>
  920.      */
  921.     public function getFlatImportReviewByDate(DateTime $startDate, ?DateTime $endDate): array
  922.     {
  923.         $result $this->createQueryBuilder('f')
  924.             ->select('count(f.id) as flatNumber, u.email as user')
  925.             ->innerJoin('f.user''u'Join::WITH'f.user = u.id')
  926.             ->where('f.createdAt >= :startDate')
  927.             ->groupBy('user')
  928.             ->setParameters([
  929.                 'startDate' => $startDate,
  930.             ]);
  931.         if ($endDate) {
  932.             $result
  933.                 ->andWhere('f.createdAt <= :endDate')
  934.                 ->setParameter('endDate'$endDate);
  935.         }
  936.         return $result
  937.             ->getQuery()
  938.             ->getResult();
  939.     }
  940.     /**
  941.      * @param User $user
  942.      * @param Building $building
  943.      * @param int $archived
  944.      * @return array<Flat>
  945.      */
  946.     public function getActiveFlatsByUserAndBuildingAndDebit(User $userBuilding $buildingint $archived): array
  947.     {
  948.         return $this->createQueryBuilder('f')
  949.             ->where('f.building = :building')
  950.             ->andWhere('f.user =:user')
  951.             ->andWhere('(f.debit + f.previousBill) > 0')
  952.             ->andWhere('f.archived = :archived')
  953.             ->setParameters([
  954.                 'user' => $user,
  955.                 'building' => $building,
  956.                 'archived' => $archived
  957.             ])
  958.             ->getQuery()
  959.             ->getResult();
  960.     }
  961.     /**
  962.      * @param Building $building
  963.      * @return float
  964.      * @throws NoResultException
  965.      * @throws NonUniqueResultException
  966.      */
  967.     public function calculateBuildingSize(Building $building): float
  968.     {
  969.         return $this->createQueryBuilder('f')
  970.             ->select('sum(f.size)')
  971.             ->where('f.building = :building')
  972.             ->andWhere('f.archived = 0')
  973.             ->setParameters([
  974.                 'building' => $building
  975.             ])
  976.             ->getQuery()
  977.             ->getSingleScalarResult();
  978.     }
  979.     /**
  980.      * @param Building $building
  981.      * @return array<Flat>
  982.      */
  983.     public function getFlatsWithActiveEmailForDictionary(Building $building): array
  984.     {
  985.         $emails $this->createQueryBuilder('f')
  986.             ->select('f.ownerEmail')
  987.             ->where('f.building = :building')
  988.             ->andWhere('f.emailCanBeUsed = 1')
  989.             ->setParameter('building'$building)
  990.             ->getQuery()
  991.             ->getArrayResult();
  992.         $filteredEmails = [];
  993.         foreach ($emails as $email) {
  994.             if ($email['ownerEmail'] != '') {
  995.                 if (!in_array($email['ownerEmail'], $filteredEmails)) {
  996.                     $filteredEmails[] = $email['ownerEmail'];
  997.                 }
  998.             }
  999.         }
  1000.         return $filteredEmails;
  1001.     }
  1002.     /**
  1003.      * @param Building $building
  1004.      * @param User $user
  1005.      * @return array<Flat>
  1006.      */
  1007.     public function findFlatByOwner(Building $buildingUser $user): array
  1008.     {
  1009.         return $this->createQueryBuilder('f')
  1010.             ->select('f.ownerName, f.ownerLastname, f.displayFlatId, f.type, f.companyName, f.type')
  1011.             ->where('f.building = :building')
  1012.             ->andWhere('f.user = :user')
  1013.             ->andWhere('f.archived = 0')
  1014.             ->setParameters([
  1015.                 'user' => $user,
  1016.                 'building' => $building
  1017.             ])
  1018.             ->getQuery()
  1019.             ->getResult();
  1020.     }
  1021.     /**
  1022.      * @return array<Flat>
  1023.      */
  1024.     public function getAllArchivedFlats(): array
  1025.     {
  1026.         return $this->createQueryBuilder('f')
  1027.             ->where('f.archived = 1')
  1028.             ->getQuery()
  1029.             ->getResult();
  1030.     }
  1031.     /**
  1032.      * @param User $user
  1033.      * @param Building $building
  1034.      * @return array<mixed>|null
  1035.      * @throws NonUniqueResultException
  1036.      */
  1037.     public function findFlatsBillDebitAndPreviousBillSum(User $userBuilding $building): ?array
  1038.     {
  1039.         return $this->createQueryBuilder('f')
  1040.             ->select('(sum(f.debit + f.previousBill)) as billSum')
  1041.             ->where('f.user = :user')
  1042.             ->andWhere('f.building = :building')
  1043.             ->andWhere('f.archived = 0')
  1044.             ->setParameters([
  1045.                 'user' => $user,
  1046.                 'building' => $building
  1047.             ])
  1048.             ->getQuery()
  1049.             ->getOneOrNullResult();
  1050.     }
  1051.     /**
  1052.      * @param User $user
  1053.      * @param Building $building
  1054.      * @return array<mixed>|null
  1055.      * @throws NonUniqueResultException
  1056.      */
  1057.     public function findFlatsBillDebitAndPreviousBillSumReport(User $userBuilding $building): ?array
  1058.     {
  1059.         return $this->createQueryBuilder('f')
  1060.             ->select('(sum(f.debit + f.previousBill)) as billSum')
  1061.             ->where('f.user = :user')
  1062.             ->andWhere('f.building = :building')
  1063.             ->andWhere('f.archived = 0')
  1064.             ->andWhere('(f.debit + f.previousBill) > 0')
  1065.             ->setParameters([
  1066.                 'user' => $user,
  1067.                 'building' => $building
  1068.             ])
  1069.             ->getQuery()
  1070.             ->getOneOrNullResult();
  1071.     }
  1072.     /**
  1073.      * @param array<User> $users
  1074.      * @return array<Flat>
  1075.      */
  1076.     public function findFlatsForInvoice(array $users): array
  1077.     {
  1078.         return $this->createQueryBuilder('f')
  1079.             ->where('f.eInvoice = true')
  1080.             ->andwhere('f.user IN (:users)')
  1081.             ->andWhere('f.archived = 0')
  1082.             ->setParameters([
  1083.                 'users' => $users
  1084.             ])
  1085.             ->getQuery()
  1086.             ->getResult();
  1087.     }
  1088.     /**
  1089.      * @param Building $building
  1090.      * @param ListItemsOptionsV2 $options
  1091.      * @return array<Flat>
  1092.      */
  1093.     public function getFlatsByBuildingPaginated(Building $buildingListItemsOptionsV2 $options): array
  1094.     {
  1095.         return $this->createQueryBuilder('f')
  1096.             ->where('f.building = :building')
  1097.             ->andWhere('f.archived = 0')
  1098.             ->orderBy('f.' $options->getOrderByColumn(), $options->getOrderByDirection())
  1099.             ->setParameter('building'$building)
  1100.             ->setMaxResults($options->getLimit())
  1101.             ->setFirstResult($options->getOffset())
  1102.             ->getQuery()
  1103.             ->getResult();
  1104.     }
  1105.     /**
  1106.      * @param Building $building
  1107.      * @return int
  1108.      * @throws NoResultException
  1109.      * @throws NonUniqueResultException
  1110.      */
  1111.     public function countFlatsByBuildingPaginated(Building $building): int
  1112.     {
  1113.         return $this->createQueryBuilder('f')
  1114.             ->select('count(f.id) as sumFlats')
  1115.             ->where('f.building = :building')
  1116.             ->andWhere('f.archived = 0')
  1117.             ->setParameter('building'$building)
  1118.             ->getQuery()
  1119.             ->getSingleScalarResult();
  1120.     }
  1121.     /**
  1122.      * @param int $limit
  1123.      * @param int $offset
  1124.      * @return array<int, array<string, int>>
  1125.      */
  1126.     public function getAllFlats(int $limitint $offset): array
  1127.     {
  1128.         return $this->createQueryBuilder('f')
  1129.             ->select('f.id')
  1130.             ->orderBy('f.id')
  1131.             ->setMaxResults($limit)
  1132.             ->setFirstResult($offset)
  1133.             ->getQuery()
  1134.             ->getArrayResult();
  1135.     }
  1136.     /**
  1137.      * @param array<User> $users
  1138.      * @return int
  1139.      * @throws NoResultException
  1140.      * @throws NonUniqueResultException
  1141.      */
  1142.     public function countFlatsForInvoice(array $users): int
  1143.     {
  1144.         return $this->createQueryBuilder('f')
  1145.             ->select('count(f.id) as sumFlats')
  1146.             ->andwhere('f.user IN (:users)')
  1147.             ->andWhere('f.eInvoice = true')
  1148.             ->setParameter('users'$users)
  1149.             ->getQuery()
  1150.             ->getSingleScalarResult();
  1151.     }
  1152.     /**
  1153.      * @param array<User> $users
  1154.      * @return int
  1155.      * @throws NoResultException
  1156.      * @throws NonUniqueResultException
  1157.      */
  1158.     public function countFlatsForPrintBill(array $users): int
  1159.     {
  1160.         return $this->createQueryBuilder('f')
  1161.             ->select('count(f.id) as sumFlats')
  1162.             ->andwhere('f.user IN (:users)')
  1163.             ->andWhere('f.printBill = true')
  1164.             ->setParameter('users'$users)
  1165.             ->getQuery()
  1166.             ->getSingleScalarResult();
  1167.     }
  1168.     /**
  1169.      * @param Building $building
  1170.      * @param int $limit
  1171.      * @param int $offset
  1172.      * @return array<Flat>
  1173.      */
  1174.     public function getAllFlatsByBuilding(Building $buildingint $limitint $offset): array
  1175.     {
  1176.         return $this->createQueryBuilder('f')
  1177.             ->where('f.building = :building')
  1178.             ->andWhere('f.archived = 0')
  1179.             ->orderBy('f.id')
  1180.             ->setParameter('building'$building)
  1181.             ->setMaxResults($limit)
  1182.             ->setFirstResult($offset)
  1183.             ->getQuery()
  1184.             ->getResult();
  1185.     }
  1186.     /**
  1187.      * @param Resident $resident
  1188.      * @return array<Flat>
  1189.      */
  1190.     public function getAllFlatsByResident(Resident $resident): array
  1191.     {
  1192.         return $this->createQueryBuilder('f')
  1193.             ->where('f.resident = :resident')
  1194.             ->andWhere('f.archived = 0')
  1195.             ->orderBy('f.id')
  1196.             ->setParameter('resident'$resident)
  1197.             ->getQuery()
  1198.             ->getResult();
  1199.     }
  1200.     /**
  1201.      * @param int $buildingId
  1202.      * @return array<Flat>
  1203.      */
  1204.     public function findFlatsByBuildingAndOwnerPhoneNumber(int $buildingId): array
  1205.     {
  1206.         return $this->createQueryBuilder('f')
  1207.             ->where('f.building = :buildingId')
  1208.             ->andWhere('f.ownerPhoneNumber IS NOT NULL')
  1209.             ->setParameter('buildingId'$buildingId)
  1210.             ->orderBy('f.displayFlatId''ASC')
  1211.             ->getQuery()
  1212.             ->getResult();
  1213.     }
  1214.     /**
  1215.      * @param array $searchParams
  1216.      * @param int|null $start
  1217.      * @param int|null $limit
  1218.      * @param array $order
  1219.      * @param string|null $search
  1220.      * @return array<mixed>
  1221.      */
  1222.     public function findFlatsForDatatable(
  1223.         array $searchParams,
  1224.         ?int $start,
  1225.         ?int $limit,
  1226.         array $order,
  1227.         string $search null
  1228.     ): array {
  1229.         $query $this->createQueryBuilder('f')
  1230.             ->select(
  1231.                 'f.id,
  1232.                 f.displayFlatId,
  1233.                 f.size,
  1234.                 f.ownerName,
  1235.                 f.ownerLastname,
  1236.                 f.companyName,
  1237.                 f.ownerPhoneNumber,
  1238.                 f.debit,
  1239.                 f.previousBill,
  1240.                 f.type,
  1241.                 f.subType,
  1242.                 f.oldDebit,
  1243.                 f.ownerEmail,
  1244.                 f.emailCanBeUsed,
  1245.                 b.address,
  1246.                 b.id as buildingId'
  1247.             )
  1248.             ->innerJoin('f.building''b')
  1249.             ->where('b.activeStatus = 1')
  1250.             ->andWhere('f.user IN (:users)')
  1251.             ->andWhere('f.archived = 0')
  1252.             ->setParameter('users'$searchParams['users'])
  1253.             ->setMaxResults($limit)
  1254.             ->setFirstResult($start);
  1255.         if ($order['column'] === 'address') {
  1256.             $query->orderBy('b.address'$order['dir']);
  1257.         } else {
  1258.             $query->orderBy('f.' $order['column'], $order['dir']);
  1259.         }
  1260.         if ($search) {
  1261.             $query->andWhere(
  1262.                 'f.ownerName LIKE :search OR
  1263.                 f.ownerLastname LIKE :search OR
  1264.                 f.displayFlatId LIKE :search OR
  1265.                 f.ownerPhoneNumber LIKE :search OR
  1266.                 f.ownerEmail LIKE :search OR
  1267.                 b.address LIKE :search'
  1268.             );
  1269.             $query->setParameter('search''%' $search '%');
  1270.         }
  1271.         return $query
  1272.             ->getQuery()
  1273.             ->getArrayResult();
  1274.     }
  1275.     /**
  1276.      * @param array $searchParams
  1277.      * @param string|null $search
  1278.      * @return int
  1279.      * @throws NoResultException
  1280.      * @throws NonUniqueResultException
  1281.      */
  1282.     public function findFlatsForDatatableCount(array $searchParamsstring $search null): int
  1283.     {
  1284.         $qb $this->createQueryBuilder('f')
  1285.             ->select('COUNT(f.id)')
  1286.             ->innerJoin('f.building''b')
  1287.             ->where('b.activeStatus = 1')
  1288.             ->andWhere('f.user IN (:users)')
  1289.             ->andWhere('f.archived = 0')
  1290.             ->setParameter('users'$searchParams['users']);
  1291.         if ($search) {
  1292.             $qb->andWhere(
  1293.                 'f.ownerName LIKE :search OR
  1294.                 f.ownerLastname LIKE :search OR
  1295.                 f.displayFlatId LIKE :search OR
  1296.                 f.ownerPhoneNumber LIKE :search OR
  1297.                 f.ownerEmail LIKE :search OR
  1298.                 b.address LIKE :search'
  1299.             )
  1300.                 ->setParameter('search''%' $search '%');
  1301.         }
  1302.         return (int) $qb->getQuery()->getSingleScalarResult();
  1303.     }
  1304. }