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 getArchivedFlatsOrderByArchivedAt(User $userBuilding $building): array
  735.     {
  736.         return $this->createQueryBuilder('f')
  737.             ->where('f.user = :user')
  738.             ->andWhere('f.building = :building')
  739.             ->andWhere('f.archived = 1')
  740.             ->setParameters([
  741.                 'user' => $user,
  742.                 'building' => $building
  743.             ])
  744.             ->orderBy('f.archivedAt''DESC')
  745.             ->getQuery()
  746.             ->getResult();
  747.     }
  748.     /**
  749.      * @param User $user
  750.      * @param Building $building
  751.      * @return array<Flat>
  752.      */
  753.     public function getFlatsObjectsByBuilding(User $userBuilding $building): array
  754.     {
  755.         $query $this->createQueryBuilder('f')
  756.             ->where('f.user = :user')
  757.             ->andWhere('f.building = :building')
  758.             ->andWhere('f.archived = 0')
  759.             ->setParameters([
  760.                 'user' => $user,
  761.                 'building' => $building
  762.             ])
  763.             ->orderBy('f.displayFlatId''ASC');
  764.         return $query
  765.             ->getQuery()
  766.             ->getResult();
  767.     }
  768.     /**
  769.      * @param User $user
  770.      * @param Building $building
  771.      * @param bool $haveEmail
  772.      * @return array<Flat>
  773.      */
  774.     public function getFlatsByEmailForOldDebit(User $userBuilding $buildingbool $haveEmail): array
  775.     {
  776.         $query $this->createQueryBuilder('f')
  777.             ->where('f.user = :user')
  778.             ->andWhere('f.building = :building')
  779.             ->andWhere('f.oldDebit > 0')
  780.             ->setParameters([
  781.                 'user' => $user,
  782.                 'building' => $building,
  783.             ]);
  784.         if ($haveEmail) {
  785.             $query->andWhere('f.emailCanBeUsed = 1');
  786.         } else {
  787.             $query->andWhere('f.emailCanBeUsed = 0');
  788.         }
  789.         return $query
  790.             ->orderBy('f.displayFlatId''ASC')
  791.             ->getQuery()
  792.             ->getResult();
  793.     }
  794.     /**
  795.      * @param Building $building
  796.      * @return int
  797.      * @throws NoResultException
  798.      * @throws NonUniqueResultException
  799.      */
  800.     public function countFlatsByPhoneNumberForOldDebit(Building $building): int
  801.     {
  802.         return $this->createQueryBuilder('f')
  803.             ->select('COUNT(f.id)')
  804.             ->where('f.building = :building')
  805.             ->andWhere('f.oldDebit > 0')
  806.             ->andWhere('f.ownerPhoneNumber is not null')
  807.             ->setParameters([
  808.                 'building' => $building,
  809.             ])
  810.             ->getQuery()
  811.             ->getSingleScalarResult();
  812.     }
  813.     /**
  814.      * @param Building $building
  815.      * @return array<Flat>
  816.      */
  817.     public function getFlatsByPhoneNumberForOldDebit(Building $building): array
  818.     {
  819.         return  $this->createQueryBuilder('f')
  820.             ->where('f.building = :building')
  821.             ->andWhere('f.oldDebit > 0')
  822.             ->andWhere('f.ownerPhoneNumber is not null')
  823.             ->setParameters([
  824.                 'building' => $building,
  825.             ])
  826.             ->orderBy('f.displayFlatId''ASC')
  827.             ->getQuery()
  828.             ->getResult();
  829.     }
  830.     /**
  831.      * @param Building $building
  832.      * @return int
  833.      * @throws NoResultException
  834.      * @throws NonUniqueResultException
  835.      */
  836.     public function countFlatsByPhoneNumberForSendBill(Building $building): int
  837.     {
  838.         return $this->createQueryBuilder('f')
  839.             ->select('COUNT(f.id)')
  840.             ->where('f.building = :building')
  841.             ->andWhere('f.ownerPhoneNumber is not null')
  842.             ->setParameters([
  843.                 'building' => $building,
  844.             ])
  845.             ->getQuery()
  846.             ->getSingleScalarResult();
  847.     }
  848.     /**
  849.      * @param Building $building
  850.      * @return array<Flat>
  851.      */
  852.     public function getFlatsCountByTypeAndBuilding(Building $building): array
  853.     {
  854.         return $this->createQueryBuilder('f''f.type')
  855.             ->select('f.type, count(f.type) as sumFlats')
  856.             ->where('f.building = :building')
  857.             ->andWhere('f.archived = 0')
  858.             ->setParameters([
  859.                 'building' => $building,
  860.             ])
  861.             ->groupBy('f.type')
  862.             ->getQuery()
  863.             ->getArrayResult();
  864.     }
  865.     /**
  866.      * @param User $user
  867.      * @param Building $building
  868.      * @return array<Flat>
  869.      */
  870.     public function getFlatsDetailsByBuilding(User $userBuilding $building): array
  871.     {
  872.         return $this->createQueryBuilder('f''f.displayFlatId')
  873.             ->select(
  874.                 'f.displayFlatId as flatDisplayId,
  875.                 f.members as flatMembers,
  876.                 f.size as flatSize,
  877.                 f.type as flatType'
  878.             )
  879.             ->where('f.user = :user')
  880.             ->andWhere('f.building = :building')
  881.             ->andWhere('f.archived = 0')
  882.             ->setParameters([
  883.                 'user' => $user,
  884.                 'building' => $building
  885.             ])
  886.             ->orderBy('f.displayFlatId''ASC')
  887.             ->getQuery()
  888.             ->getArrayResult();
  889.     }
  890.     /**
  891.      * @param User $user
  892.      * @param Building $building
  893.      * @return array
  894.      */
  895.     public function getAllFlatsDetailsByBuilding(User $userBuilding $building): array
  896.     {
  897.         return $this->createQueryBuilder('f''f.displayFlatId')
  898.             ->select(
  899.                 'f.displayFlatId as flatDisplayId,
  900.                 f.members as flatMembers,
  901.                 f.ownerName as ownerName,
  902.                 f.ownerLastname as ownerLastname,
  903.                 f.companyName as companyName,
  904.                 f.ownerAddress as ownerAddress,
  905.                 f.size as flatSize,
  906.                 f.type as flatType'
  907.             )
  908.             ->where('f.user = :user')
  909.             ->andWhere('f.building = :building')
  910.             ->andWhere('f.archived = 0')
  911.             ->setParameters([
  912.                 'user' => $user,
  913.                 'building' => $building
  914.             ])
  915.             ->orderBy('f.displayFlatId''ASC')
  916.             ->getQuery()
  917.             ->getArrayResult();
  918.     }
  919.     /**
  920.      * @param User $user
  921.      * @return array<Flat>
  922.      */
  923.     public function getFlatsByMobileAppUser(User $user): array
  924.     {
  925.         $query $this->createQueryBuilder('f')
  926.             ->where('f.flatUser = :user')
  927.             ->andWhere('f.archived = 0')
  928.             ->setParameters([
  929.                 'user' => $user,
  930.             ])
  931.             ->orderBy('f.displayFlatId''ASC');
  932.         return $query
  933.             ->getQuery()
  934.             ->getResult();
  935.     }
  936.     /**
  937.      * @param DateTime $startDate
  938.      * @param DateTime|null $endDate
  939.      * @return array<mixed>
  940.      */
  941.     public function getFlatImportReviewByDate(DateTime $startDate, ?DateTime $endDate): array
  942.     {
  943.         $result $this->createQueryBuilder('f')
  944.             ->select('count(f.id) as flatNumber, u.email as user')
  945.             ->innerJoin('f.user''u'Join::WITH'f.user = u.id')
  946.             ->where('f.createdAt >= :startDate')
  947.             ->groupBy('user')
  948.             ->setParameters([
  949.                 'startDate' => $startDate,
  950.             ]);
  951.         if ($endDate) {
  952.             $result
  953.                 ->andWhere('f.createdAt <= :endDate')
  954.                 ->setParameter('endDate'$endDate);
  955.         }
  956.         return $result
  957.             ->getQuery()
  958.             ->getResult();
  959.     }
  960.     /**
  961.      * @param User $user
  962.      * @param Building $building
  963.      * @param int $archived
  964.      * @return array<Flat>
  965.      */
  966.     public function getActiveFlatsByUserAndBuildingAndDebit(User $userBuilding $buildingint $archived): array
  967.     {
  968.         return $this->createQueryBuilder('f')
  969.             ->where('f.building = :building')
  970.             ->andWhere('f.user =:user')
  971.             ->andWhere('(f.debit + f.previousBill) > 0')
  972.             ->andWhere('f.archived = :archived')
  973.             ->setParameters([
  974.                 'user' => $user,
  975.                 'building' => $building,
  976.                 'archived' => $archived
  977.             ])
  978.             ->getQuery()
  979.             ->getResult();
  980.     }
  981.     /**
  982.      * @param Building $building
  983.      * @return float
  984.      * @throws NoResultException
  985.      * @throws NonUniqueResultException
  986.      */
  987.     public function calculateBuildingSize(Building $building): float
  988.     {
  989.         return $this->createQueryBuilder('f')
  990.             ->select('sum(f.size)')
  991.             ->where('f.building = :building')
  992.             ->andWhere('f.archived = 0')
  993.             ->setParameters([
  994.                 'building' => $building
  995.             ])
  996.             ->getQuery()
  997.             ->getSingleScalarResult();
  998.     }
  999.     /**
  1000.      * @param Building $building
  1001.      * @return array<Flat>
  1002.      */
  1003.     public function getFlatsWithActiveEmailForDictionary(Building $building): array
  1004.     {
  1005.         $emails $this->createQueryBuilder('f')
  1006.             ->select('f.ownerEmail')
  1007.             ->where('f.building = :building')
  1008.             ->andWhere('f.emailCanBeUsed = 1')
  1009.             ->setParameter('building'$building)
  1010.             ->getQuery()
  1011.             ->getArrayResult();
  1012.         $filteredEmails = [];
  1013.         foreach ($emails as $email) {
  1014.             if ($email['ownerEmail'] != '') {
  1015.                 if (!in_array($email['ownerEmail'], $filteredEmails)) {
  1016.                     $filteredEmails[] = $email['ownerEmail'];
  1017.                 }
  1018.             }
  1019.         }
  1020.         return $filteredEmails;
  1021.     }
  1022.     /**
  1023.      * @param Building $building
  1024.      * @param User $user
  1025.      * @return array<Flat>
  1026.      */
  1027.     public function findFlatByOwner(Building $buildingUser $user): array
  1028.     {
  1029.         return $this->createQueryBuilder('f')
  1030.             ->select('f.ownerName, f.ownerLastname, f.displayFlatId, f.type, f.companyName, f.type')
  1031.             ->where('f.building = :building')
  1032.             ->andWhere('f.user = :user')
  1033.             ->andWhere('f.archived = 0')
  1034.             ->setParameters([
  1035.                 'user' => $user,
  1036.                 'building' => $building
  1037.             ])
  1038.             ->getQuery()
  1039.             ->getResult();
  1040.     }
  1041.     /**
  1042.      * @return array<Flat>
  1043.      */
  1044.     public function getAllArchivedFlats(): array
  1045.     {
  1046.         return $this->createQueryBuilder('f')
  1047.             ->where('f.archived = 1')
  1048.             ->getQuery()
  1049.             ->getResult();
  1050.     }
  1051.     /**
  1052.      * @param User $user
  1053.      * @param Building $building
  1054.      * @return array<mixed>|null
  1055.      * @throws NonUniqueResultException
  1056.      */
  1057.     public function findFlatsBillDebitAndPreviousBillSum(User $userBuilding $building): ?array
  1058.     {
  1059.         return $this->createQueryBuilder('f')
  1060.             ->select('(sum(f.debit + f.previousBill)) as billSum')
  1061.             ->where('f.user = :user')
  1062.             ->andWhere('f.building = :building')
  1063.             ->andWhere('f.archived = 0')
  1064.             ->setParameters([
  1065.                 'user' => $user,
  1066.                 'building' => $building
  1067.             ])
  1068.             ->getQuery()
  1069.             ->getOneOrNullResult();
  1070.     }
  1071.     /**
  1072.      * @param User $user
  1073.      * @param Building $building
  1074.      * @return array<mixed>|null
  1075.      * @throws NonUniqueResultException
  1076.      */
  1077.     public function findFlatsBillDebitAndPreviousBillSumReport(User $userBuilding $building): ?array
  1078.     {
  1079.         return $this->createQueryBuilder('f')
  1080.             ->select('(sum(f.debit + f.previousBill)) as billSum')
  1081.             ->where('f.user = :user')
  1082.             ->andWhere('f.building = :building')
  1083.             ->andWhere('f.archived = 0')
  1084.             ->andWhere('(f.debit + f.previousBill) > 0')
  1085.             ->setParameters([
  1086.                 'user' => $user,
  1087.                 'building' => $building
  1088.             ])
  1089.             ->getQuery()
  1090.             ->getOneOrNullResult();
  1091.     }
  1092.     /**
  1093.      * @param array<User> $users
  1094.      * @return array<Flat>
  1095.      */
  1096.     public function findFlatsForInvoice(array $users): array
  1097.     {
  1098.         return $this->createQueryBuilder('f')
  1099.             ->where('f.eInvoice = true')
  1100.             ->andwhere('f.user IN (:users)')
  1101.             ->andWhere('f.archived = 0')
  1102.             ->setParameters([
  1103.                 'users' => $users
  1104.             ])
  1105.             ->getQuery()
  1106.             ->getResult();
  1107.     }
  1108.     /**
  1109.      * @param Building $building
  1110.      * @param ListItemsOptionsV2 $options
  1111.      * @return array<Flat>
  1112.      */
  1113.     public function getFlatsByBuildingPaginated(Building $buildingListItemsOptionsV2 $options): array
  1114.     {
  1115.         return $this->createQueryBuilder('f')
  1116.             ->where('f.building = :building')
  1117.             ->andWhere('f.archived = 0')
  1118.             ->orderBy('f.' $options->getOrderByColumn(), $options->getOrderByDirection())
  1119.             ->setParameter('building'$building)
  1120.             ->setMaxResults($options->getLimit())
  1121.             ->setFirstResult($options->getOffset())
  1122.             ->getQuery()
  1123.             ->getResult();
  1124.     }
  1125.     /**
  1126.      * @param Building $building
  1127.      * @return int
  1128.      * @throws NoResultException
  1129.      * @throws NonUniqueResultException
  1130.      */
  1131.     public function countFlatsByBuildingPaginated(Building $building): int
  1132.     {
  1133.         return $this->createQueryBuilder('f')
  1134.             ->select('count(f.id) as sumFlats')
  1135.             ->where('f.building = :building')
  1136.             ->andWhere('f.archived = 0')
  1137.             ->setParameter('building'$building)
  1138.             ->getQuery()
  1139.             ->getSingleScalarResult();
  1140.     }
  1141.     /**
  1142.      * @param int $limit
  1143.      * @param int $offset
  1144.      * @return array<int, array<string, int>>
  1145.      */
  1146.     public function getAllFlats(int $limitint $offset): array
  1147.     {
  1148.         return $this->createQueryBuilder('f')
  1149.             ->select('f.id')
  1150.             ->orderBy('f.id')
  1151.             ->setMaxResults($limit)
  1152.             ->setFirstResult($offset)
  1153.             ->getQuery()
  1154.             ->getArrayResult();
  1155.     }
  1156.     /**
  1157.      * @param array<User> $users
  1158.      * @return int
  1159.      * @throws NoResultException
  1160.      * @throws NonUniqueResultException
  1161.      */
  1162.     public function countFlatsForInvoice(array $users): int
  1163.     {
  1164.         return $this->createQueryBuilder('f')
  1165.             ->select('count(f.id) as sumFlats')
  1166.             ->andwhere('f.user IN (:users)')
  1167.             ->andWhere('f.eInvoice = true')
  1168.             ->setParameter('users'$users)
  1169.             ->getQuery()
  1170.             ->getSingleScalarResult();
  1171.     }
  1172.     /**
  1173.      * @param array<User> $users
  1174.      * @return int
  1175.      * @throws NoResultException
  1176.      * @throws NonUniqueResultException
  1177.      */
  1178.     public function countFlatsForPrintBill(array $users): int
  1179.     {
  1180.         return $this->createQueryBuilder('f')
  1181.             ->select('count(f.id) as sumFlats')
  1182.             ->andwhere('f.user IN (:users)')
  1183.             ->andWhere('f.printBill = true')
  1184.             ->setParameter('users'$users)
  1185.             ->getQuery()
  1186.             ->getSingleScalarResult();
  1187.     }
  1188.     /**
  1189.      * @param Building $building
  1190.      * @param int $limit
  1191.      * @param int $offset
  1192.      * @return array<Flat>
  1193.      */
  1194.     public function getAllFlatsByBuilding(Building $buildingint $limitint $offset): array
  1195.     {
  1196.         return $this->createQueryBuilder('f')
  1197.             ->where('f.building = :building')
  1198.             ->andWhere('f.archived = 0')
  1199.             ->orderBy('f.id')
  1200.             ->setParameter('building'$building)
  1201.             ->setMaxResults($limit)
  1202.             ->setFirstResult($offset)
  1203.             ->getQuery()
  1204.             ->getResult();
  1205.     }
  1206.     /**
  1207.      * @param Resident $resident
  1208.      * @return array<Flat>
  1209.      */
  1210.     public function getAllFlatsByResident(Resident $resident): array
  1211.     {
  1212.         return $this->createQueryBuilder('f')
  1213.             ->where('f.resident = :resident')
  1214.             ->andWhere('f.archived = 0')
  1215.             ->orderBy('f.id')
  1216.             ->setParameter('resident'$resident)
  1217.             ->getQuery()
  1218.             ->getResult();
  1219.     }
  1220.     /**
  1221.      * @param int $buildingId
  1222.      * @return array<Flat>
  1223.      */
  1224.     public function findFlatsByBuildingAndOwnerPhoneNumber(int $buildingId): array
  1225.     {
  1226.         return $this->createQueryBuilder('f')
  1227.             ->where('f.building = :buildingId')
  1228.             ->andWhere('f.ownerPhoneNumber IS NOT NULL')
  1229.             ->setParameter('buildingId'$buildingId)
  1230.             ->orderBy('f.displayFlatId''ASC')
  1231.             ->getQuery()
  1232.             ->getResult();
  1233.     }
  1234.     /**
  1235.      * @param array $searchParams
  1236.      * @param int|null $start
  1237.      * @param int|null $limit
  1238.      * @param array $order
  1239.      * @param string|null $search
  1240.      * @return array<mixed>
  1241.      */
  1242.     public function findFlatsForDatatable(
  1243.         array $searchParams,
  1244.         ?int $start,
  1245.         ?int $limit,
  1246.         array $order,
  1247.         string $search null
  1248.     ): array {
  1249.         $query $this->createQueryBuilder('f')
  1250.             ->select(
  1251.                 'f.id,
  1252.                 f.displayFlatId,
  1253.                 f.size,
  1254.                 f.ownerName,
  1255.                 f.ownerLastname,
  1256.                 f.companyName,
  1257.                 f.ownerPhoneNumber,
  1258.                 f.debit,
  1259.                 f.previousBill,
  1260.                 f.type,
  1261.                 f.subType,
  1262.                 f.oldDebit,
  1263.                 f.ownerEmail,
  1264.                 f.emailCanBeUsed,
  1265.                 b.address,
  1266.                 b.id as buildingId'
  1267.             )
  1268.             ->innerJoin('f.building''b')
  1269.             ->where('b.activeStatus = 1')
  1270.             ->andWhere('f.user IN (:users)')
  1271.             ->andWhere('f.archived = 0')
  1272.             ->setParameter('users'$searchParams['users'])
  1273.             ->setMaxResults($limit)
  1274.             ->setFirstResult($start);
  1275.         if ($order['column'] === 'address') {
  1276.             $query->orderBy('b.address'$order['dir']);
  1277.         } else {
  1278.             $query->orderBy('f.' $order['column'], $order['dir']);
  1279.         }
  1280.         if ($search) {
  1281.             $query->andWhere(
  1282.                 'f.ownerName LIKE :search OR
  1283.                 f.ownerLastname LIKE :search OR
  1284.                 f.displayFlatId LIKE :search OR
  1285.                 f.ownerPhoneNumber LIKE :search OR
  1286.                 f.ownerEmail LIKE :search OR
  1287.                 b.address LIKE :search'
  1288.             );
  1289.             $query->setParameter('search''%' $search '%');
  1290.         }
  1291.         return $query
  1292.             ->getQuery()
  1293.             ->getArrayResult();
  1294.     }
  1295.     /**
  1296.      * @param array $searchParams
  1297.      * @param string|null $search
  1298.      * @return int
  1299.      * @throws NoResultException
  1300.      * @throws NonUniqueResultException
  1301.      */
  1302.     public function findFlatsForDatatableCount(array $searchParamsstring $search null): int
  1303.     {
  1304.         $qb $this->createQueryBuilder('f')
  1305.             ->select('COUNT(f.id)')
  1306.             ->innerJoin('f.building''b')
  1307.             ->where('b.activeStatus = 1')
  1308.             ->andWhere('f.user IN (:users)')
  1309.             ->andWhere('f.archived = 0')
  1310.             ->setParameter('users'$searchParams['users']);
  1311.         if ($search) {
  1312.             $qb->andWhere(
  1313.                 'f.ownerName LIKE :search OR
  1314.                 f.ownerLastname LIKE :search OR
  1315.                 f.displayFlatId LIKE :search OR
  1316.                 f.ownerPhoneNumber LIKE :search OR
  1317.                 f.ownerEmail LIKE :search OR
  1318.                 b.address LIKE :search'
  1319.             )
  1320.                 ->setParameter('search''%' $search '%');
  1321.         }
  1322.         return (int) $qb->getQuery()->getSingleScalarResult();
  1323.     }
  1324. }