src/Controller/ApiV2/Residents/HomeController.php line 57

  1. <?php
  2. namespace App\Controller\ApiV2\Residents;
  3. use App\Controller\ApiV2\AbstractController;
  4. use App\Entity\Building;
  5. use App\Entity\Flat;
  6. use App\Entity\NotificationToken;
  7. use App\Entity\Resident;
  8. use App\Form\ApiV2\NotificationTokenType;
  9. use App\Repository\BuildingRepository;
  10. use App\Repository\FlatRepository;
  11. use App\Repository\MoneyLogRepository;
  12. use App\Repository\NotificationRepository;
  13. use App\Repository\NotificationTokenRepository;
  14. use App\Repository\SettingsRepository;
  15. use App\Security\Voter\FlatVoter;
  16. use App\Services\BillService;
  17. use App\Services\MoneyLogService;
  18. use DateTime;
  19. use Doctrine\ORM\NonUniqueResultException;
  20. use Doctrine\ORM\NoResultException;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use FOS\RestBundle\Controller\Annotations as Rest;
  23. class HomeController extends AbstractController
  24. {
  25. /**
  26. * @param BuildingRepository $buildingRepository
  27. * @param MoneyLogRepository $moneyLogRepository
  28. * @param NotificationRepository $notificationRepository
  29. * @param MoneyLogService $moneyLogService
  30. * @param BillService $billService
  31. * @param NotificationTokenRepository $notificationTokenRepository
  32. * @param SettingsRepository $settingsRepository
  33. * @param FlatRepository $flatRepository
  34. */
  35. public function __construct(
  36. BuildingRepository $buildingRepository,
  37. private readonly MoneyLogRepository $moneyLogRepository,
  38. private readonly NotificationRepository $notificationRepository,
  39. private readonly MoneyLogService $moneyLogService,
  40. private readonly BillService $billService,
  41. private readonly NotificationTokenRepository $notificationTokenRepository,
  42. private readonly SettingsRepository $settingsRepository,
  43. private readonly FlatRepository $flatRepository
  44. ) {
  45. parent::__construct($buildingRepository);
  46. }
  47. /**
  48. * @param Request $request
  49. * @return array<string, array<string, Flat|Building>>
  50. * @throws NoResultException
  51. * @throws NonUniqueResultException
  52. * @throws \Exception
  53. */
  54. #[Rest\Get('/dashboard', name: 'residents_dashboard_index')]
  55. public function index(Request $request): array
  56. {
  57. /** @var Resident $resident */
  58. $resident = $this->getUser();
  59. $showAccountBalance = false;
  60. $showIncomesOutcomes = false;
  61. $hideProblemTab = false;
  62. $showBuildingDebits = false;
  63. $showInvesticioniFond = false;
  64. $showTekuciFond = false;
  65. $showBuildingPayments = false;
  66. $now = new DateTime();
  67. $flat = null;
  68. if ($id = $request->get('id')) {
  69. /** @var Flat $flat */
  70. $flat = $this->flatRepository->findOneBy(['id' => $id]);
  71. }
  72. if (!$flat) {
  73. $flat = $this->flatRepository->getAllFlatsByResident($resident)[0];
  74. }
  75. $this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT, $flat);
  76. if ($flat->getUser()->getId() === 216) {
  77. $hideProblemTab = true;
  78. }
  79. $building = $flat->getBuilding();
  80. $settings = $this->settingsRepository->getSettingForBuilding($building);
  81. if ($settings && $settings->getOptions() !== null) {
  82. $help = json_decode($settings->getOptions(), true);
  83. $showAccountBalance = $help['showAccountBalance'] ?? false;
  84. $showIncomesOutcomes = $help['showIncomesOutcomes'] ?? false;
  85. $showBuildingDebits = $help['showBuildingDebits'] ?? false;
  86. $showInvesticioniFond = $help['showInvesticioniFond'] ?? false;
  87. $showTekuciFond = $help['showTekuciFond'] ?? false;
  88. $showBuildingPayments = $help['showBuildingPayments'] ?? false;
  89. }
  90. $buildingDebits = $this->flatRepository->findFlatsBillDebitAndPreviousBillSum($building->getUser(), $building);
  91. $investicioniFond = $this->billService->getPaymentsForOptionFondCard($building, 3);
  92. $tekuciFond = $this->billService->getPaymentsForOptionFondCard($building, 4);
  93. $buildingPayments = $this->moneyLogRepository
  94. ->findBillByMoneyLog($flat->getBuilding(), $now->format('Y-m-01'), $now->format('Y-m-t'), [0,2,3]);
  95. return [
  96. 'data' => [
  97. 'flat' => $flat,
  98. 'building' => $building,
  99. 'bills' => $this->moneyLogRepository->getLastFourMoneyLogsByFlat($flat),
  100. 'statistics' => $this->moneyLogService->getPaymentsStatisticsByFlat($flat, '1970-01-01'),
  101. 'notifications' => $this->notificationRepository->getLastNotificationsByBuilding(
  102. $flat->getBuilding(),
  103. 4
  104. ),
  105. 'showAccountBalance' => $showAccountBalance,
  106. 'showIncomesOutcomes' => $showIncomesOutcomes,
  107. 'showBuildingDebits' => $showBuildingDebits,
  108. 'showInvesticioniFond' => $showInvesticioniFond,
  109. 'showTekuciFond' => $showTekuciFond,
  110. 'showBuildingPayments' => $showBuildingPayments,
  111. 'hideProblemTab' => $hideProblemTab,
  112. 'currency' => $flat->getUser()->getCurrency(),
  113. 'accountBalance' => $showAccountBalance ?
  114. $this->billService
  115. ->getPaymentsForTekuciRacunCardCurrentOptimized($building->getUser(), $building)['saldo']
  116. : null,
  117. 'buildingDebits' => $showBuildingDebits ? $buildingDebits['billSum'] : null,
  118. 'investicioniFond' => $showInvesticioniFond ? $investicioniFond['saldo'] : null,
  119. 'tekuciFond' => $showTekuciFond ? $tekuciFond['saldo'] : null,
  120. 'buildingPayments' => $showBuildingPayments ? $buildingPayments : null,
  121. ]
  122. ];
  123. }
  124. /**
  125. * @param Request $request
  126. * @return array<mixed>
  127. */
  128. #[Rest\Post('/save-token', name: 'residents_save_token')]
  129. public function saveToken(Request $request): array
  130. {
  131. /** @var Resident $resident */
  132. $resident = $this->getUser();
  133. $data = $this->validateForm($request, NotificationTokenType::class);
  134. /** @var NotificationToken|null $notificationToken */
  135. $notificationToken = $this->notificationTokenRepository->findOneBy(['token' => $data['token']]);
  136. if ($notificationToken) {
  137. $notificationToken->setResident($resident);
  138. } else {
  139. $notificationToken = new NotificationToken();
  140. $notificationToken->setResident($resident);
  141. $notificationToken->setUserType(0);
  142. $notificationToken->setToken($data['token']);
  143. }
  144. $this->notificationTokenRepository->save($notificationToken);
  145. return [
  146. 'data' => [],
  147. 'message' => "Token saved"
  148. ];
  149. }
  150. }