<?php
namespace App\Controller;
use App\Helper\DashBoardHelper;
use App\Repository\PropertyRepository;
use App\Repository\RegistrationFormRepository;
use App\Entity\User;
use App\ValueObject\UserPermission;
use App\ValueObject\UserTypes;
use Ecommerce121\UtilBundle\Controller\ControllerBase;
use Ecommerce121\UtilBundle\Controller\ControllerUtil;
use Ecommerce121\UtilBundle\Repository\QueryOptions;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Class with default actions.
*/
class DashboardController extends ControllerBase
{
const YEAR_PERIOD = 11;
const SIX_MONTH_PERIOD = 5;
const SEVEN_MONTH_PERIOD = 6;
const YEAR_AND_MONTH_PERIOD = 12;
const DAYS_PERIOD = 30;
const ITEMS_PER_PAGE = 10;
const ITEM_PER_PAGE_PENDING_REGISTRATION = 5;
const ITEMS_PER_PAGE_CANDIDATE = 10;
/**
* @var DashBoardHelper
*/
protected $dashBoardHelper;
/**
* DashboardController constructor.
*
* @param ControllerUtil $controllerUtil
* @param DashBoardHelper $dashBoardHelper
*/
public function __construct(
ControllerUtil $controllerUtil,
DashBoardHelper $dashBoardHelper
) {
parent::__construct($controllerUtil);
$this->dashBoardHelper = $dashBoardHelper;
}
/**
* Show default home.
*
* @Route("/", name="app_home")
*
* @param Request $request HTTP request.
*
* @return array|Response
*/
public function indexAction(Request $request)
{
$this->denyAccessUnlessGranted(UserPermission::VIEW_DASHBOARD);
/** @var User $user */
$user = $this->getUser();
$entityId = null;
$options = [];
$totalInvoiced = 0;
$barRegistration = null;
$candidates = null;
$candidatesPaginator = null;
$propertiesPaginator = null;
$dueInvoices = null;
$dueInvoicesPaginator = null;
$properties = null;
$invoicesPaginator = null;
$pendingRegistrationRequest = null;
$pendingRegistrationRequestPaginator = null;
$registrationAndRenewalPaginator = null;
$userEmail = null;
$page = $request->query->get('page', 1);
if ($user->getType() === UserTypes::MUNICIPALITY) {
$entityId = $user->getMunicipality()->getId();
}
if ($user->getRegisterAsIndividual()) {
$userEmail = $user->getEmail();
}
if ($user->getType() === UserTypes::REGISTER_PARTY) {
if ($user->getOrganization())
$entityId = $user->getOrganization()->getId();
}
if ($user->getType() === UserTypes::MUNIREG) {
$barInvoice = $this->dashBoardHelper->getInvoiceValues(self::YEAR_AND_MONTH_PERIOD);
$barRegistration = $this->dashBoardHelper->getRegistrationValues(self::SIX_MONTH_PERIOD, $user->getType());
$options = [
'barInvoice' => $barInvoice,
'barRegistration' => $barRegistration
];
}
if ($user->getType() === UserTypes::MUNICIPALITY) {
$barRegistration = $this->dashBoardHelper->getRegistrationValues(self::YEAR_PERIOD, $user->getType(), $entityId);
$pieRegistration = $this->dashBoardHelper->getRegistrationPieValues(self::YEAR_PERIOD, $user->getType(), $entityId);
$lastForms = $this->dashBoardHelper->getRegistrationAndRenewal(
self::DAYS_PERIOD,
$user->getMunicipality(),
new QueryOptions($page, self::ITEMS_PER_PAGE, 'createdDate', 'DESC')
);
$registrationAndRenewalPaginator = $this->dashBoardHelper->getRegistrationAndRenewalPaginator(
self::DAYS_PERIOD,
$user->getMunicipality(),
$page,
self::ITEMS_PER_PAGE
);
$options = [
'barRegistration' => $barRegistration,
'pieRegistration' => $pieRegistration,
'registrationAndRenewalPaginator' => $registrationAndRenewalPaginator,
'lastForms' => $lastForms
];
}
if ($user->getType() === UserTypes::REGISTER_PARTY) {
$organization = $user->getOrganization();
if ($organization) {
$properties = $this->dashBoardHelper->getRequiredRenewalFormsByOrganization($organization,
new QueryOptions($page, self::ITEMS_PER_PAGE, 'createdDate', 'DESC')
);
$propertiesPaginator = $this->dashBoardHelper->getRequiredRenewalFormsByOrganizationPaginator($organization, $page, self::ITEMS_PER_PAGE);
$dueInvoices = $this->dashBoardHelper->getDueRenewalsByOrganization(
$organization,
new QueryOptions($page, 5, 'createdDate', 'DESC')
);
$invoicesPaginator = $this->dashBoardHelper->getDueRenewalsByOrganizationPaginator($organization, $page, self::ITEMS_PER_PAGE);
$candidates = $this->dashBoardHelper->getCandidateProperties(
$organization,
new QueryOptions($page, self::ITEMS_PER_PAGE_CANDIDATE, 'createdDate', 'DESC')
);
$candidatesPaginator = $this->dashBoardHelper->getCandidatePropertiesPaginator(
$organization,
$page,
self::ITEMS_PER_PAGE_CANDIDATE
);
$pendingRegistrationRequest = $this->dashBoardHelper->getPendingRegistrationRequestOfUser(
$user,
new QueryOptions($page, self::ITEM_PER_PAGE_PENDING_REGISTRATION,'createdDate', 'DESC'),
);
$pendingRegistrationRequestPaginator = $this->dashBoardHelper->getPendingRegistrationRequestOfUser(
$user,
new QueryOptions($page, self::ITEM_PER_PAGE_PENDING_REGISTRATION,'createdDate', 'DESC'),
true,
$page,
self::ITEM_PER_PAGE_PENDING_REGISTRATION
);
$totalInvoiced = $this->dashBoardHelper->getAccountBalance($organization, $userEmail);
}
if ($entityId) {
$barRegistration = $this->dashBoardHelper->getRegistrationValues(self::SEVEN_MONTH_PERIOD, $user->getType(), $entityId, $userEmail);
}
$options = [
'totalInvoiced' => $totalInvoiced,
'barRegistration' => $barRegistration,
'candidates' => $candidates,
'candidatesPaginator' => $candidatesPaginator,
'dueInvoices' => $dueInvoices,
'properties' => $properties,
'propertiesPaginator' => $propertiesPaginator,
'invoicePaginator' => $invoicesPaginator,
'pendingRegistrationRequests' => $pendingRegistrationRequest,
'pendingRegistrationRequestPaginator' => $pendingRegistrationRequestPaginator,
];
}
if ($user->getType() === UserTypes::REGISTER_PARTY) {
if ($request->isXmlHttpRequest()) {
$loadCandidates = $request->query->get('type');
if ($loadCandidates == 'candidates') {
return $this->jsonView('Dashboard/Register/list_candidates.html.twig', $options);
}
if ($loadCandidates == 'require-renewal') {
return $this->jsonView('Dashboard/Register/list_invoices.html.twig', $options);
}
if ($loadCandidates == 'pending-request') {
return $this->jsonView('Dashboard/Register/list_pending_registration_request.html.twig', $options);
}
if ($loadCandidates == 'properties') {
return $this->jsonView('Dashboard/Register/list_required_renewal_property.html.twig', $options);
}
}
return $this->render('Dashboard/Register/index.html.twig', $options);
} elseif ($user->getType() === UserTypes::MUNICIPALITY) {
if ($request->isXmlHttpRequest()) {
return $this->jsonView('Dashboard/Municipal/list.html.twig', $options);
}
return $this->render('Dashboard/Municipal/index.html.twig', $options);
}
return $this->render('Dashboard/munireg.html.twig', $options);
}
}