src/Controller/ApiV2/Residents/HomeController.php line 57
<?phpnamespace App\Controller\ApiV2\Residents;use App\Controller\ApiV2\AbstractController;use App\Entity\Building;use App\Entity\Flat;use App\Entity\NotificationToken;use App\Entity\Resident;use App\Form\ApiV2\NotificationTokenType;use App\Repository\BuildingRepository;use App\Repository\FlatRepository;use App\Repository\MoneyLogRepository;use App\Repository\NotificationRepository;use App\Repository\NotificationTokenRepository;use App\Repository\SettingsRepository;use App\Security\Voter\FlatVoter;use App\Services\BillService;use App\Services\MoneyLogService;use DateTime;use Doctrine\ORM\NonUniqueResultException;use Doctrine\ORM\NoResultException;use Symfony\Component\HttpFoundation\Request;use FOS\RestBundle\Controller\Annotations as Rest;class HomeController extends AbstractController{/*** @param BuildingRepository $buildingRepository* @param MoneyLogRepository $moneyLogRepository* @param NotificationRepository $notificationRepository* @param MoneyLogService $moneyLogService* @param BillService $billService* @param NotificationTokenRepository $notificationTokenRepository* @param SettingsRepository $settingsRepository* @param FlatRepository $flatRepository*/public function __construct(BuildingRepository $buildingRepository,private readonly MoneyLogRepository $moneyLogRepository,private readonly NotificationRepository $notificationRepository,private readonly MoneyLogService $moneyLogService,private readonly BillService $billService,private readonly NotificationTokenRepository $notificationTokenRepository,private readonly SettingsRepository $settingsRepository,private readonly FlatRepository $flatRepository) {parent::__construct($buildingRepository);}/*** @param Request $request* @return array<string, array<string, Flat|Building>>* @throws NoResultException* @throws NonUniqueResultException* @throws \Exception*/#[Rest\Get('/dashboard', name: 'residents_dashboard_index')]public function index(Request $request): array{/** @var Resident $resident */$resident = $this->getUser();$showAccountBalance = false;$showIncomesOutcomes = false;$hideProblemTab = false;$showBuildingDebits = false;$showInvesticioniFond = false;$showTekuciFond = false;$showBuildingPayments = false;$now = new DateTime();$flat = null;if ($id = $request->get('id')) {/** @var Flat $flat */$flat = $this->flatRepository->findOneBy(['id' => $id]);}if (!$flat) {$flat = $this->flatRepository->getAllFlatsByResident($resident)[0];}$this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT, $flat);if ($flat->getUser()->getId() === 216) {$hideProblemTab = true;}$building = $flat->getBuilding();$settings = $this->settingsRepository->getSettingForBuilding($building);if ($settings && $settings->getOptions() !== null) {$help = json_decode($settings->getOptions(), true);$showAccountBalance = $help['showAccountBalance'] ?? false;$showIncomesOutcomes = $help['showIncomesOutcomes'] ?? false;$showBuildingDebits = $help['showBuildingDebits'] ?? false;$showInvesticioniFond = $help['showInvesticioniFond'] ?? false;$showTekuciFond = $help['showTekuciFond'] ?? false;$showBuildingPayments = $help['showBuildingPayments'] ?? false;}$buildingDebits = $this->flatRepository->findFlatsBillDebitAndPreviousBillSum($building->getUser(), $building);$investicioniFond = $this->billService->getPaymentsForOptionFondCard($building, 3);$tekuciFond = $this->billService->getPaymentsForOptionFondCard($building, 4);$buildingPayments = $this->moneyLogRepository->findBillByMoneyLog($flat->getBuilding(), $now->format('Y-m-01'), $now->format('Y-m-t'), [0,2,3]);return ['data' => ['flat' => $flat,'building' => $building,'bills' => $this->moneyLogRepository->getLastFourMoneyLogsByFlat($flat),'statistics' => $this->moneyLogService->getPaymentsStatisticsByFlat($flat, '1970-01-01'),'notifications' => $this->notificationRepository->getLastNotificationsByBuilding($flat->getBuilding(),4),'showAccountBalance' => $showAccountBalance,'showIncomesOutcomes' => $showIncomesOutcomes,'showBuildingDebits' => $showBuildingDebits,'showInvesticioniFond' => $showInvesticioniFond,'showTekuciFond' => $showTekuciFond,'showBuildingPayments' => $showBuildingPayments,'hideProblemTab' => $hideProblemTab,'currency' => $flat->getUser()->getCurrency(),'accountBalance' => $showAccountBalance ?$this->billService->getPaymentsForTekuciRacunCardCurrentOptimized($building->getUser(), $building)['saldo']: null,'buildingDebits' => $showBuildingDebits ? $buildingDebits['billSum'] : null,'investicioniFond' => $showInvesticioniFond ? $investicioniFond['saldo'] : null,'tekuciFond' => $showTekuciFond ? $tekuciFond['saldo'] : null,'buildingPayments' => $showBuildingPayments ? $buildingPayments : null,]];}/*** @param Request $request* @return array<mixed>*/#[Rest\Post('/save-token', name: 'residents_save_token')]public function saveToken(Request $request): array{/** @var Resident $resident */$resident = $this->getUser();$data = $this->validateForm($request, NotificationTokenType::class);/** @var NotificationToken|null $notificationToken */$notificationToken = $this->notificationTokenRepository->findOneBy(['token' => $data['token']]);if ($notificationToken) {$notificationToken->setResident($resident);} else {$notificationToken = new NotificationToken();$notificationToken->setResident($resident);$notificationToken->setUserType(0);$notificationToken->setToken($data['token']);}$this->notificationTokenRepository->save($notificationToken);return ['data' => [],'message' => "Token saved"];}}