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