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 Building $building
  524.      * @return array<Flat>
  525.      */
  526.     public function getFlatsWithEmail(Building $building): array
  527.     {
  528.         return $this->createQueryBuilder('f')
  529.             ->andWhere('f.building = :building')
  530.             ->andWhere('f.ownerEmail is not null')
  531.             ->setParameters([
  532.                 'building' => $building
  533.             ])
  534.             ->getQuery()
  535.             ->getResult();
  536.     }
  537.     /**
  538.      * @param Building $building
  539.      * @return array<array<string>>
  540.      */
  541.     public function getDistinctOwnerEmails(Building $building): array
  542.     {
  543.         return $this->createQueryBuilder('f')
  544.             ->select('DISTINCT f.ownerEmail')
  545.             ->andWhere('f.building = :building')
  546.             ->andWhere('f.ownerEmail IS NOT NULL')
  547.             ->andWhere('f.emailCanBeUsed = 1')
  548.             ->andWhere('f.archived = 0')
  549.             ->setParameters([
  550.                 'building' => $building
  551.             ])
  552.             ->getQuery()
  553.             ->getResult();
  554.     }
  555.     /**
  556.      * @param User $user
  557.      * @param Building $building
  558.      *
  559.      * @return array<Flat>
  560.      */
  561.     public function findFlatsWithDebitByBuilding(User $userBuilding $building): array
  562.     {
  563.         return $this->createQueryBuilder('f')
  564.             ->where('f.user = :user')
  565.             ->andWhere('f.building = :building')
  566.             ->andWhere('f.archived = 0')
  567.             ->andWhere('(f.debit + f.oldDebit)<>0 OR f.previousBill is not null')
  568.             ->setParameters([
  569.                 'user' => $user,
  570.                 'building' => $building
  571.             ])
  572.             ->orderBy('f.displayFlatId + 0''ASC')
  573.             ->getQuery()
  574.             ->getResult();
  575.     }
  576.     /**
  577.      * @param int $start
  578.      * @param int $limit
  579.      *
  580.      * @return array<mixed>
  581.      */
  582.     public function findFlatsForMoneyLogData(int $startint $limit): array
  583.     {
  584.         return $this->createQueryBuilder('f')
  585.             ->setFirstResult($start)
  586.             ->setMaxResults($limit)
  587.             ->getQuery()
  588.             ->getResult();
  589.     }
  590.     /**
  591.      * @param Building $building
  592.      * @return int|mixed|string|null
  593.      * @throws NonUniqueResultException
  594.      */
  595.     public function findLastArchiveFlatsByBuilding(Building $building): mixed
  596.     {
  597.         return $this->createQueryBuilder('f')
  598.             ->select('f.displayFlatId')
  599.             ->andWhere('f.building = :building')
  600.             ->andWhere('f.archived = 1')
  601.             ->setParameters([
  602.                 'building' => $building
  603.             ])
  604.             ->orderBy('f.archivedAt''DESC')
  605.             ->setMaxResults(1)
  606.             ->getQuery()
  607.             ->getOneOrNullResult();
  608.     }
  609.     /**
  610.      * @param User $user
  611.      * @param Building $building
  612.      * @return string
  613.      * @throws NonUniqueResultException|NoResultException
  614.      */
  615.     public function countArchiveFlatsByBuilding(User $userBuilding $building): string
  616.     {
  617.         return $this->createQueryBuilder('f')
  618.             ->select('count(f)')
  619.             ->where('f.user = :user')
  620.             ->andWhere('f.building = :building')
  621.             ->andWhere('f.archived = 1')
  622.             ->setParameters([
  623.                 'user' => $user,
  624.                 'building' => $building
  625.             ])
  626.             ->getQuery()
  627.             ->getSingleScalarResult();
  628.     }
  629.     /**
  630.      * @param Flat $currentFlat
  631.      * @param User $user
  632.      *
  633.      * @return array<Flat>
  634.      */
  635.     public function getArchivedFlatsByCurrentFlat(Flat $currentFlatUser $user): array
  636.     {
  637.         return $this->createQueryBuilder('f')
  638.             ->where('f.user = :user')
  639.             ->andWhere('f.currentFlat = :currentFlat')
  640.             ->andWhere('f.archived = 1')
  641.             ->setParameters([
  642.                 'currentFlat' => $currentFlat,
  643.                 'user' => $user
  644.             ])
  645.             ->getQuery()
  646.             ->getResult();
  647.     }
  648.     /**
  649.      * @param User $user
  650.      * @param Building $building
  651.      *
  652.      * @return array<Flat>
  653.      */
  654.     public function findArchivedFlatsWithDebitByBuilding(User $userBuilding $building): array
  655.     {
  656.         return $this->createQueryBuilder('f')
  657.             ->where('f.user = :user')
  658.             ->andWhere('f.building = :building')
  659.             ->andWhere('f.archived = 1')
  660.             ->andWhere('((f.debit + f.previousBill) <> 0) OR (f.oldDebit > 0)')
  661.             ->setParameters([
  662.                 'user' => $user,
  663.                 'building' => $building
  664.             ])
  665.             ->orderBy('f.displayFlatId + 0''ASC')
  666.             ->getQuery()
  667.             ->getResult();
  668.     }
  669.     /**
  670.      * @param Building $building
  671.      * @param string $flatNumber
  672.      * @return Flat|null
  673.      * @throws NonUniqueResultException
  674.      */
  675.     public function getFlatByFlatNumber(Building $buildingstring $flatNumber): ?Flat
  676.     {
  677.         return $this->createQueryBuilder('f')
  678.             ->where('f.building = :building')
  679.             ->andWhere('f.displayFlatId = :flatNumber')
  680.             ->setParameters([
  681.                 'building' => $building,
  682.                 'flatNumber' => $flatNumber
  683.             ])
  684.             ->getQuery()
  685.             ->getOneOrNullResult();
  686.     }
  687.     /**
  688.      * @param User $user
  689.      * @param Building $building
  690.      * @return array<Flat>
  691.      */
  692.     public function getArchivedFlatsObjByBuilding(User $userBuilding $building): array
  693.     {
  694.         $query $this->createQueryBuilder('f')
  695.             ->where('f.user = :user')
  696.             ->andWhere('f.building = :building')
  697.             ->andWhere('f.archived = 1')
  698.             ->setParameters([
  699.                 'user' => $user,
  700.                 'building' => $building
  701.             ])
  702.             ->orderBy('f.displayFlatId''ASC');
  703.         return $query
  704.             ->getQuery()
  705.             ->getResult();
  706.     }
  707.     /**
  708.      * @param User $user
  709.      * @param Building $building
  710.      * @return array<Flat>
  711.      */
  712.     public function getFlatsObjectsByBuilding(User $userBuilding $building): array
  713.     {
  714.         $query $this->createQueryBuilder('f')
  715.             ->where('f.user = :user')
  716.             ->andWhere('f.building = :building')
  717.             ->andWhere('f.archived = 0')
  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.      * @param bool $haveEmail
  731.      * @return array<Flat>
  732.      */
  733.     public function getFlatsByEmailForOldDebit(User $userBuilding $buildingbool $haveEmail): array
  734.     {
  735.         $query $this->createQueryBuilder('f')
  736.             ->where('f.user = :user')
  737.             ->andWhere('f.building = :building')
  738.             ->andWhere('f.oldDebit > 0')
  739.             ->setParameters([
  740.                 'user' => $user,
  741.                 'building' => $building,
  742.             ]);
  743.         if ($haveEmail) {
  744.             $query->andWhere('f.emailCanBeUsed = 1');
  745.         } else {
  746.             $query->andWhere('f.emailCanBeUsed = 0');
  747.         }
  748.         return $query
  749.             ->orderBy('f.displayFlatId''ASC')
  750.             ->getQuery()
  751.             ->getResult();
  752.     }
  753.     /**
  754.      * @param Building $building
  755.      * @return int
  756.      * @throws NoResultException
  757.      * @throws NonUniqueResultException
  758.      */
  759.     public function countFlatsByPhoneNumberForOldDebit(Building $building): int
  760.     {
  761.         return $this->createQueryBuilder('f')
  762.             ->select('COUNT(f.id)')
  763.             ->where('f.building = :building')
  764.             ->andWhere('f.oldDebit > 0')
  765.             ->andWhere('f.ownerPhoneNumber is not null')
  766.             ->setParameters([
  767.                 'building' => $building,
  768.             ])
  769.             ->getQuery()
  770.             ->getSingleScalarResult();
  771.     }
  772.     /**
  773.      * @param Building $building
  774.      * @return array<Flat>
  775.      */
  776.     public function getFlatsByPhoneNumberForOldDebit(Building $building): array
  777.     {
  778.         return  $this->createQueryBuilder('f')
  779.             ->where('f.building = :building')
  780.             ->andWhere('f.oldDebit > 0')
  781.             ->andWhere('f.ownerPhoneNumber is not null')
  782.             ->setParameters([
  783.                 'building' => $building,
  784.             ])
  785.             ->orderBy('f.displayFlatId''ASC')
  786.             ->getQuery()
  787.             ->getResult();
  788.     }
  789.     /**
  790.      * @param Building $building
  791.      * @return int
  792.      * @throws NoResultException
  793.      * @throws NonUniqueResultException
  794.      */
  795.     public function countFlatsByPhoneNumberForSendBill(Building $building): int
  796.     {
  797.         return $this->createQueryBuilder('f')
  798.             ->select('COUNT(f.id)')
  799.             ->where('f.building = :building')
  800.             ->andWhere('f.ownerPhoneNumber is not null')
  801.             ->setParameters([
  802.                 'building' => $building,
  803.             ])
  804.             ->getQuery()
  805.             ->getSingleScalarResult();
  806.     }
  807.     /**
  808.      * @param Building $building
  809.      * @return array<Flat>
  810.      */
  811.     public function getFlatsCountByTypeAndBuilding(Building $building): array
  812.     {
  813.         return $this->createQueryBuilder('f''f.type')
  814.             ->select('f.type, count(f.type) as sumFlats')
  815.             ->where('f.building = :building')
  816.             ->andWhere('f.archived = 0')
  817.             ->setParameters([
  818.                 'building' => $building,
  819.             ])
  820.             ->groupBy('f.type')
  821.             ->getQuery()
  822.             ->getArrayResult();
  823.     }
  824.     /**
  825.      * @param User $user
  826.      * @param Building $building
  827.      * @return array<Flat>
  828.      */
  829.     public function getFlatsDetailsByBuilding(User $userBuilding $building): array
  830.     {
  831.         return $this->createQueryBuilder('f''f.displayFlatId')
  832.             ->select(
  833.                 'f.displayFlatId as flatDisplayId,
  834.                 f.members as flatMembers,
  835.                 f.size as flatSize,
  836.                 f.type as flatType'
  837.             )
  838.             ->where('f.user = :user')
  839.             ->andWhere('f.building = :building')
  840.             ->andWhere('f.archived = 0')
  841.             ->setParameters([
  842.                 'user' => $user,
  843.                 'building' => $building
  844.             ])
  845.             ->orderBy('f.displayFlatId''ASC')
  846.             ->getQuery()
  847.             ->getArrayResult();
  848.     }
  849.     /**
  850.      * @param User $user
  851.      * @return array<Flat>
  852.      */
  853.     public function getFlatsByMobileAppUser(User $user): array
  854.     {
  855.         $query $this->createQueryBuilder('f')
  856.             ->where('f.flatUser = :user')
  857.             ->andWhere('f.archived = 0')
  858.             ->setParameters([
  859.                 'user' => $user,
  860.             ])
  861.             ->orderBy('f.displayFlatId''ASC');
  862.         return $query
  863.             ->getQuery()
  864.             ->getResult();
  865.     }
  866.     /**
  867.      * @param DateTime $startDate
  868.      * @param DateTime|null $endDate
  869.      * @return array<mixed>
  870.      */
  871.     public function getFlatImportReviewByDate(DateTime $startDate, ?DateTime $endDate): array
  872.     {
  873.         $result $this->createQueryBuilder('f')
  874.             ->select('count(f.id) as flatNumber, u.email as user')
  875.             ->innerJoin('f.user''u'Join::WITH'f.user = u.id')
  876.             ->where('f.createdAt >= :startDate')
  877.             ->groupBy('user')
  878.             ->setParameters([
  879.                 'startDate' => $startDate,
  880.             ]);
  881.         if ($endDate) {
  882.             $result
  883.                 ->andWhere('f.createdAt <= :endDate')
  884.                 ->setParameter('endDate'$endDate);
  885.         }
  886.         return $result
  887.             ->getQuery()
  888.             ->getResult();
  889.     }
  890.     /**
  891.      * @param User $user
  892.      * @param Building $building
  893.      * @param int $archived
  894.      * @return array<Flat>
  895.      */
  896.     public function getActiveFlatsByUserAndBuildingAndDebit(User $userBuilding $buildingint $archived): array
  897.     {
  898.         return $this->createQueryBuilder('f')
  899.             ->where('f.building = :building')
  900.             ->andWhere('f.user =:user')
  901.             ->andWhere('(f.debit + f.previousBill) > 0')
  902.             ->andWhere('f.archived = :archived')
  903.             ->setParameters([
  904.                 'user' => $user,
  905.                 'building' => $building,
  906.                 'archived' => $archived
  907.             ])
  908.             ->getQuery()
  909.             ->getResult();
  910.     }
  911.     /**
  912.      * @param Building $building
  913.      * @return float
  914.      * @throws NoResultException
  915.      * @throws NonUniqueResultException
  916.      */
  917.     public function calculateBuildingSize(Building $building): float
  918.     {
  919.         return $this->createQueryBuilder('f')
  920.             ->select('sum(f.size)')
  921.             ->where('f.building = :building')
  922.             ->andWhere('f.archived = 0')
  923.             ->setParameters([
  924.                 'building' => $building
  925.             ])
  926.             ->getQuery()
  927.             ->getSingleScalarResult();
  928.     }
  929.     /**
  930.      * @param Building $building
  931.      * @return array<Flat>
  932.      */
  933.     public function getFlatsWithActiveEmailForDictionary(Building $building): array
  934.     {
  935.         $emails $this->createQueryBuilder('f')
  936.             ->select('f.ownerEmail')
  937.             ->where('f.building = :building')
  938.             ->andWhere('f.emailCanBeUsed = 1')
  939.             ->setParameter('building'$building)
  940.             ->getQuery()
  941.             ->getArrayResult();
  942.         $filteredEmails = [];
  943.         foreach ($emails as $email) {
  944.             if ($email['ownerEmail'] != '') {
  945.                 if (!in_array($email['ownerEmail'], $filteredEmails)) {
  946.                     $filteredEmails[] = $email['ownerEmail'];
  947.                 }
  948.             }
  949.         }
  950.         return $filteredEmails;
  951.     }
  952.     /**
  953.      * @param Building $building
  954.      * @param User $user
  955.      * @return array<Flat>
  956.      */
  957.     public function findFlatByOwner(Building $buildingUser $user): array
  958.     {
  959.         return $this->createQueryBuilder('f')
  960.             ->select('f.ownerName, f.ownerLastname, f.displayFlatId, f.type, f.companyName, f.type')
  961.             ->where('f.building = :building')
  962.             ->andWhere('f.user = :user')
  963.             ->andWhere('f.archived = 0')
  964.             ->setParameters([
  965.                 'user' => $user,
  966.                 'building' => $building
  967.             ])
  968.             ->getQuery()
  969.             ->getResult();
  970.     }
  971.     /**
  972.      * @return array<Flat>
  973.      */
  974.     public function getAllArchivedFlats(): array
  975.     {
  976.         return $this->createQueryBuilder('f')
  977.             ->where('f.archived = 1')
  978.             ->getQuery()
  979.             ->getResult();
  980.     }
  981.     /**
  982.      * @param User $user
  983.      * @param Building $building
  984.      * @return array<mixed>|null
  985.      * @throws NonUniqueResultException
  986.      */
  987.     public function findFlatsBillDebitAndPreviousBillSum(User $userBuilding $building): ?array
  988.     {
  989.         return $this->createQueryBuilder('f')
  990.             ->select('(sum(f.debit + f.previousBill)) as billSum')
  991.             ->where('f.user = :user')
  992.             ->andWhere('f.building = :building')
  993.             ->andWhere('f.archived = 0')
  994.             ->setParameters([
  995.                 'user' => $user,
  996.                 'building' => $building
  997.             ])
  998.             ->getQuery()
  999.             ->getOneOrNullResult();
  1000.     }
  1001.     /**
  1002.      * @param array<User> $users
  1003.      * @return array<Flat>
  1004.      */
  1005.     public function findFlatsForInvoice(array $users): array
  1006.     {
  1007.         return $this->createQueryBuilder('f')
  1008.             ->where('f.eInvoice = true')
  1009.             ->andwhere('f.user IN (:users)')
  1010.             ->setParameters([
  1011.                 'users' => $users
  1012.             ])
  1013.             ->getQuery()
  1014.             ->getResult();
  1015.     }
  1016.     /**
  1017.      * @param Building $building
  1018.      * @param ListItemsOptionsV2 $options
  1019.      * @return array<Flat>
  1020.      */
  1021.     public function getFlatsByBuildingPaginated(Building $buildingListItemsOptionsV2 $options): array
  1022.     {
  1023.         return $this->createQueryBuilder('f')
  1024.             ->where('f.building = :building')
  1025.             ->andWhere('f.archived = 0')
  1026.             ->orderBy('f.' $options->getOrderByColumn(), $options->getOrderByDirection())
  1027.             ->setParameter('building'$building)
  1028.             ->setMaxResults($options->getLimit())
  1029.             ->setFirstResult($options->getOffset())
  1030.             ->getQuery()
  1031.             ->getResult();
  1032.     }
  1033.     /**
  1034.      * @param Building $building
  1035.      * @return int
  1036.      * @throws NoResultException
  1037.      * @throws NonUniqueResultException
  1038.      */
  1039.     public function countFlatsByBuildingPaginated(Building $building): int
  1040.     {
  1041.         return $this->createQueryBuilder('f')
  1042.             ->select('count(f.id) as sumFlats')
  1043.             ->where('f.building = :building')
  1044.             ->andWhere('f.archived = 0')
  1045.             ->setParameter('building'$building)
  1046.             ->getQuery()
  1047.             ->getSingleScalarResult();
  1048.     }
  1049.     /**
  1050.      * @param int $limit
  1051.      * @param int $offset
  1052.      * @return array<int, array<string, int>>
  1053.      */
  1054.     public function getAllFlats(int $limitint $offset): array
  1055.     {
  1056.         return $this->createQueryBuilder('f')
  1057.             ->select('f.id')
  1058.             ->orderBy('f.id')
  1059.             ->setMaxResults($limit)
  1060.             ->setFirstResult($offset)
  1061.             ->getQuery()
  1062.             ->getArrayResult();
  1063.     }
  1064.     /**
  1065.      * @param array<User> $users
  1066.      * @return int
  1067.      * @throws NoResultException
  1068.      * @throws NonUniqueResultException
  1069.      */
  1070.     public function countFlatsForInvoice(array $users): int
  1071.     {
  1072.         return $this->createQueryBuilder('f')
  1073.             ->select('count(f.id) as sumFlats')
  1074.             ->andwhere('f.user IN (:users)')
  1075.             ->andWhere('f.eInvoice = true')
  1076.             ->setParameter('users'$users)
  1077.             ->getQuery()
  1078.             ->getSingleScalarResult();
  1079.     }
  1080.     /**
  1081.      * @param array<User> $users
  1082.      * @return int
  1083.      * @throws NoResultException
  1084.      * @throws NonUniqueResultException
  1085.      */
  1086.     public function countFlatsForPrintBill(array $users): int
  1087.     {
  1088.         return $this->createQueryBuilder('f')
  1089.             ->select('count(f.id) as sumFlats')
  1090.             ->andwhere('f.user IN (:users)')
  1091.             ->andWhere('f.printBill = true')
  1092.             ->setParameter('users'$users)
  1093.             ->getQuery()
  1094.             ->getSingleScalarResult();
  1095.     }
  1096.     /**
  1097.      * @param Building $building
  1098.      * @param int $limit
  1099.      * @param int $offset
  1100.      * @return array<Flat>
  1101.      */
  1102.     public function getAllFlatsByBuilding(Building $buildingint $limitint $offset): array
  1103.     {
  1104.         return $this->createQueryBuilder('f')
  1105.             ->where('f.building = :building')
  1106.             ->andWhere('f.archived = 0')
  1107.             ->orderBy('f.id')
  1108.             ->setParameter('building'$building)
  1109.             ->setMaxResults($limit)
  1110.             ->setFirstResult($offset)
  1111.             ->getQuery()
  1112.             ->getResult();
  1113.     }
  1114.     /**
  1115.      * @param Resident $resident
  1116.      * @return array<Flat>
  1117.      */
  1118.     public function getAllFlatsByResident(Resident $resident): array
  1119.     {
  1120.         return $this->createQueryBuilder('f')
  1121.             ->where('f.resident = :resident')
  1122.             ->andWhere('f.archived = 0')
  1123.             ->orderBy('f.id')
  1124.             ->setParameter('resident'$resident)
  1125.             ->getQuery()
  1126.             ->getResult();
  1127.     }
  1128.     /**
  1129.      * @param int $buildingId
  1130.      * @return array<Flat>
  1131.      */
  1132.     public function findFlatsByBuildingAndOwnerPhoneNumber(int $buildingId): array
  1133.     {
  1134.         return $this->createQueryBuilder('f')
  1135.             ->where('f.building = :buildingId')
  1136.             ->andWhere('f.ownerPhoneNumber IS NOT NULL')
  1137.             ->setParameter('buildingId'$buildingId)
  1138.             ->orderBy('f.displayFlatId''ASC')
  1139.             ->getQuery()
  1140.             ->getResult();
  1141.     }
  1142. }