<?php
namespace App\Controller\admin;
use App\Entity\Bursary;
use App\Entity\FundAccount;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AdminHomeController extends AbstractController {
/**
* @Route("/admin", name="adminHomeRoute")
* @IsGranted("ROLE_ADMIN")
* @return Response
*/
public function home(Request $request): Response {
$em = $this->getDoctrine()->getManager();
$countyId = $request->getSession()->get('county_id');
$currentBursary = $em->getRepository(Bursary::class)->findOneBy([
// 'county' => $countyId
],[ 'id' => 'desc']);
/** @var FundAccount[] $funds */
$funds = $em->getRepository("App:FundAccount")->findBy([
'isCounty' => true
]);
$countyTotals = $em->getRepository('App:Student')
->getCountyTotals($currentBursary);
$perWardTotals = $em->getRepository('App:Student')->getPerWardTotals($currentBursary);
return $this->render('admin/home.html.twig',[
"totals" => $countyTotals,
"ward_totals" => $perWardTotals,
"bursary" => $currentBursary,
"funds" => $funds
]);
}
/**
* @Route("/admin/application_statistics", name="adminDashStatistics")
* @IsGranted("ROLE_ADMIN")
* @return Response
*/
public function dashStatistics(): Response
{
$em = $this->getDoctrine()->getManager();
$currentBursary = $em->getRepository(Bursary::class)->findOneBy([
// 'county' => $countyId
],[ 'id' => 'desc']);
$stats = $em->getRepository('App:Student')->getPerWardTotals($currentBursary);
$categoryStats = $em->getRepository('App:Student')->getPerCategoryTotals($currentBursary);
$data = [
'gender_stats' => $stats,
'category_stats' => $categoryStats
];
return new JsonResponse($data, Response::HTTP_OK, [
'content-type' => 'application/json'
]);
}
/**
* @Route("/admin/fix_users", name="adminHomeUserfixRoute")
* @IsGranted("ROLE_ADMIN")
* @return Response
*/
public function userFixAdmin(Request $request): Response {
$em = $this->getDoctrine()->getManager();
$countyId = $request->getSession()->get('county_id');
$county = $em->getRepository("App:County")->findOneBy([
'id' => $countyId
]);
$wards = $em->getRepository('App:Ward')->findAll();
foreach ($wards as $index => $ward) {
$fundAccount = new FundAccount();
$fundAccount->setAmount(0);
$fundAccount->setBalance(0);
$fundAccount->setCounty($county);
$fundAccount->setCreatedBy($this->getUser());
$fundAccount->setAccountName($ward->getWardName().' WARD BURSARY FUND ACCOUNT');
$fundAccount->setIsDefault(false);
$fundAccount->setCreatedAt(new \DateTimeImmutable());
$em->persist($fundAccount);
}
$em->flush();
return $this->redirectToRoute('adminHomeRoute');
}
}