src/Controller/DashboardController.php line 63

  1. <?php
  2. namespace App\Controller;
  3. use App\Helper\DashBoardHelper;
  4. use App\Entity\User;
  5. use App\ValueObject\UserPermission;
  6. use App\ValueObject\UserTypes;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Ecommerce121\UtilBundle\Controller\ControllerBase;
  9. use Ecommerce121\UtilBundle\Controller\ControllerUtil;
  10. use Ecommerce121\UtilBundle\Repository\QueryOptions;
  11. use Exception;
  12. use Psr\Cache\InvalidArgumentException;
  13. use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
  14. use Symfony\Component\Routing\Attribute\Route;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class DashboardController extends ControllerBase
  18. {
  19. const int YEAR_PERIOD = 11;
  20. const int SIX_MONTH_PERIOD = 5;
  21. const int SEVEN_MONTH_PERIOD = 6;
  22. const int YEAR_AND_MONTH_PERIOD = 12;
  23. const int DAYS_PERIOD = 30;
  24. const int ITEMS_PER_PAGE = 10;
  25. const int ITEM_PER_PAGE_PENDING_REGISTRATION = 5;
  26. const int ITEMS_PER_PAGE_CANDIDATE = 10;
  27. public function __construct(
  28. ControllerUtil $controllerUtil,
  29. private readonly DashBoardHelper $dashBoardHelper,
  30. private readonly EntityManagerInterface $entityManager
  31. ) {
  32. parent::__construct($controllerUtil);
  33. }
  34. /**
  35. * @throws Exception
  36. * @throws InvalidArgumentException
  37. */
  38. #[Route('/', name: 'app_home')]
  39. public function indexAction(Request $request): array|Response
  40. {
  41. $this->denyAccessUnlessGranted(UserPermission::VIEW_DASHBOARD);
  42. /** @var User $user */
  43. $user = $this->getUser();
  44. $entityId = null;
  45. $options = [];
  46. $totalInvoiced = 0;
  47. $barRegistration = null;
  48. $candidates = null;
  49. $candidatesPaginator = null;
  50. $propertiesPaginator = null;
  51. $dueInvoices = null;
  52. $dueInvoicesPaginator = null;
  53. $properties = null;
  54. $invoicesPaginator = null;
  55. $pendingRegistrationRequest = null;
  56. $pendingRegistrationRequestPaginator = null;
  57. $registrationAndRenewalPaginator = null;
  58. $userEmail = null;
  59. $cache = new DoctrineDbalAdapter(
  60. $this->entityManager->getConnection(),
  61. 'chartData',
  62. );
  63. $page = $request->query->get('page', 1);
  64. if ($user->getType() === UserTypes::MUNICIPALITY) {
  65. $entityId = $user->getMunicipality()->getId();
  66. }
  67. if ($user->getRegisterAsIndividual()) {
  68. $userEmail = $user->getEmail();
  69. }
  70. if ($user->getType() === UserTypes::REGISTER_PARTY) {
  71. if ($user->getOrganization())
  72. $entityId = $user->getOrganization()->getId();
  73. }
  74. if ($user->getType() === UserTypes::MUNIREG) {
  75. // $barInvoice = $this->dashBoardHelper->getInvoiceValues(self::YEAR_AND_MONTH_PERIOD);
  76. // $barRegistration = $this->dashBoardHelper->getRegistrationValues(self::SIX_MONTH_PERIOD, $user->getType());
  77. $barInvoiceCacheItem = $cache->getItem('barInvoice');
  78. $barRegistrationCacheItem = $cache->getItem('barRegistration_'. $user->getType().'_');
  79. $barInvoice = $barInvoiceCacheItem->get();
  80. $barRegistration = $barRegistrationCacheItem->get();
  81. $options = [
  82. 'barInvoice' => $barInvoice,
  83. 'barRegistration' => $barRegistration
  84. ];
  85. }
  86. if ($user->getType() === UserTypes::MUNICIPALITY) {
  87. // $barRegistration = $this->dashBoardHelper->getRegistrationValues(self::YEAR_PERIOD, $user->getType(), $entityId);
  88. // $pieRegistration = $this->dashBoardHelper->getRegistrationPieValues(self::YEAR_PERIOD, $user->getType(), $entityId);
  89. $barRegistrationCacheItem = $cache->getItem('barRegistration_'. $user->getType() .'_' .$entityId);
  90. $pieRegistrationCacheItem = $cache->getItem('pieRegistration_'. $user->getType() .'_' .$entityId);
  91. $barRegistration = $barRegistrationCacheItem->get();
  92. $pieRegistration = $pieRegistrationCacheItem->get();
  93. $lastForms = $this->dashBoardHelper->getRegistrationAndRenewal(
  94. self::DAYS_PERIOD,
  95. $user->getMunicipality(),
  96. new QueryOptions($page, self::ITEMS_PER_PAGE, 'createdDate', 'DESC')
  97. );
  98. $registrationAndRenewalPaginator = $this->dashBoardHelper->getRegistrationAndRenewalPaginator(
  99. self::DAYS_PERIOD,
  100. $user->getMunicipality(),
  101. $page,
  102. self::ITEMS_PER_PAGE
  103. );
  104. $options = [
  105. 'barRegistration' => $barRegistration,
  106. 'pieRegistration' => $pieRegistration,
  107. 'registrationAndRenewalPaginator' => $registrationAndRenewalPaginator,
  108. 'lastForms' => $lastForms
  109. ];
  110. }
  111. if ($user->getType() === UserTypes::REGISTER_PARTY) {
  112. $organization = $user->getOrganization();
  113. if ($organization) {
  114. $properties = $this->dashBoardHelper->getRequiredRenewalFormsByOrganization($organization,
  115. new QueryOptions($page, self::ITEMS_PER_PAGE, 'createdDate', 'DESC')
  116. );
  117. $propertiesPaginator = $this->dashBoardHelper->getRequiredRenewalFormsByOrganizationPaginator($organization, $page, self::ITEMS_PER_PAGE);
  118. $dueInvoices = $this->dashBoardHelper->getDueRenewalsByOrganization(
  119. $organization,
  120. new QueryOptions($page, 5, 'createdDate', 'DESC')
  121. );
  122. $invoicesPaginator = $this->dashBoardHelper->getDueRenewalsByOrganizationPaginator($organization, $page, self::ITEMS_PER_PAGE);
  123. $candidates = $this->dashBoardHelper->getCandidateProperties(
  124. $organization,
  125. new QueryOptions($page, self::ITEMS_PER_PAGE_CANDIDATE, 'createdDate', 'DESC')
  126. );
  127. $candidatesPaginator = $this->dashBoardHelper->getCandidatePropertiesPaginator(
  128. $organization,
  129. $page,
  130. self::ITEMS_PER_PAGE_CANDIDATE
  131. );
  132. $pendingRegistrationRequest = $this->dashBoardHelper->getPendingRegistrationRequestOfUser(
  133. $user,
  134. new QueryOptions($page, self::ITEM_PER_PAGE_PENDING_REGISTRATION,'createdDate', 'DESC'),
  135. );
  136. $pendingRegistrationRequestPaginator = $this->dashBoardHelper->getPendingRegistrationRequestOfUser(
  137. $user,
  138. new QueryOptions($page, self::ITEM_PER_PAGE_PENDING_REGISTRATION,'createdDate', 'DESC'),
  139. true,
  140. $page,
  141. self::ITEM_PER_PAGE_PENDING_REGISTRATION
  142. );
  143. $totalInvoiced = $this->dashBoardHelper->getAccountBalance($organization, $userEmail);
  144. }
  145. if ($entityId) {
  146. // $barRegistration = $this->dashBoardHelper->getRegistrationValues(self::SEVEN_MONTH_PERIOD, $user->getType(), $entityId, $userEmail);
  147. $sanitizedEmail = '';
  148. if (!empty($userEmail)) {
  149. $sanitizedEmail = str_replace(
  150. ['{', '}', '(', ')', '/', '\\', '@', ':'],
  151. '_',
  152. $userEmail
  153. );
  154. }
  155. $barRegistrationCacheItem = $cache->getItem('barRegistration_'. $user->getType() .'_' . $entityId . '_' . $sanitizedEmail);
  156. $barRegistration = $barRegistrationCacheItem->get();
  157. }
  158. $options = [
  159. 'totalInvoiced' => $totalInvoiced,
  160. 'barRegistration' => $barRegistration,
  161. 'candidates' => $candidates,
  162. 'candidatesPaginator' => $candidatesPaginator,
  163. 'dueInvoices' => $dueInvoices,
  164. 'properties' => $properties,
  165. 'propertiesPaginator' => $propertiesPaginator,
  166. 'invoicePaginator' => $invoicesPaginator,
  167. 'pendingRegistrationRequests' => $pendingRegistrationRequest,
  168. 'pendingRegistrationRequestPaginator' => $pendingRegistrationRequestPaginator,
  169. ];
  170. }
  171. if ($user->getType() === UserTypes::REGISTER_PARTY) {
  172. if ($request->isXmlHttpRequest()) {
  173. $loadCandidates = $request->query->get('type');
  174. if ($loadCandidates == 'candidates') {
  175. return $this->jsonView('Dashboard/Register/list_candidates.html.twig', $options);
  176. }
  177. if ($loadCandidates == 'require-renewal') {
  178. return $this->jsonView('Dashboard/Register/list_invoices.html.twig', $options);
  179. }
  180. if ($loadCandidates == 'pending-request') {
  181. return $this->jsonView('Dashboard/Register/list_pending_registration_request.html.twig', $options);
  182. }
  183. if ($loadCandidates == 'properties') {
  184. return $this->jsonView('Dashboard/Register/list_required_renewal_property.html.twig', $options);
  185. }
  186. }
  187. return $this->render('Dashboard/Register/index.html.twig', $options);
  188. } elseif ($user->getType() === UserTypes::MUNICIPALITY) {
  189. if ($request->isXmlHttpRequest()) {
  190. return $this->jsonView('Dashboard/Municipal/list.html.twig', $options);
  191. }
  192. return $this->render('Dashboard/Municipal/index.html.twig', $options);
  193. }
  194. return $this->render('Dashboard/munireg.html.twig', $options);
  195. }
  196. }