<?php
namespace App\Controller\admin;
use App\Entity\institutionAllocation;
use App\Entity\InstitutionApplicationReport;
use App\Entity\Report;
use App\Entity\student\ApplicationDocument;
use App\Entity\Take;
use DateTime;
use Mpdf\MpdfException;
use PDO;
use Sasedev\MpdfBundle\Factory\MpdfFactory;
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 ReportsController extends AbstractController {
/**
* @Route("/admin/reports/fund_request", name="fundRequestReportRoute")
* @IsGranted("ROLE_USER")
* @param Request $request
* @param MpdfFactory $MpdfFactory
* @return Response
*/
public function institutionReport(Request $request, MpdfFactory $MpdfFactory): Response
{
$em = $this->getDoctrine()->getManager();
$date = date_modify(new \DateTime( '-9day '), '+24 hour');
$bursary = $em->getRepository('App:Bursary')->findOneBy([],[ 'id' => 'DESC']);
$user = $em->getRepository('App:User')->findOneBy([
'id' => $this->getUser()
]);
// $allocations = $em->getRepository('App:StudentAllocation')->getAllocations(58725000);
$amounts = 6036000;
$allocations = $em->getRepository('App:StudentAllocation')->getAllocations($amounts);
// $allocations = $em->getRepository('App:StudentAllocation')->getAllocations(5498000);
$totals = $em->getRepository('App:StudentAllocation')->getAllocationTotals();
$realAmount = 0;
foreach ($allocations as $index => $allocation) {
$realAmount += str_replace(',', '', $allocation['amount']);
}
dump($realAmount);
$fileName = '../reports/';
if (!file_exists($fileName)) {
mkdir($fileName, 0777, true);
}
$fileName .= 'fund_allocation_list.pdf';
try {
ini_set("pcre.backtrack_limit", "50000000");
ini_set('max_execution_time', '300');
ini_set('memory_limit', '1024M');
$mPdf = $MpdfFactory->createMpdfObject([
'mode' => 'utf-8',
'format' => 'A4',
'margin_header' =>0 ,
'margin_footer' => 5,
'orientation' => 'L'
// 'orientation' => 'P'
]);
$mPdf->SetWatermarkText('2021 - 2022 - ALLOCATION');
$mPdf->watermark_font = 'DejaVuSansCondensed';
$mPdf->showWatermarkText = true;
$mPdf->showImageErrors = true;
//$mpdf->showWatermarkImage = true;
$mPdf->watermarkTextAlpha = 0.1;
// $mPdf->simpleTables = true;
$mPdf->SetTopMargin("50");
$mPdf->SetHTMLHeader($this->renderView('admin/reports/fund_request/header.html.twig', [
'bursary' => $bursary,
'file_code' => base64_encode('file-'.rand(1000, 10000))
]));
$mPdf->SetFooter($this->renderView('admin/reports/fund_request/footer.html.twig', [
'user' => $user,
'today' => $date
]));
dump($totals);
// return $this->render('admin/reports/fund_request/allocations.html.twig', [
// 'allocations' => $allocations,
// 'today' => new \DateTime(),
// 'totals' => $totals
// ]);
// print_r($allocations);
$mPdf->WriteHTML($this->renderView('admin/reports/fund_request/allocations.html.twig', [
'allocations' => $allocations,
'today' => $date,
'totals' =>$totals
]));
$mPdf->Output($fileName, 'F');
// return new Response('OK', Response::HTTP_CREATED);
return $MpdfFactory->createDownloadResponse($mPdf, $fileName);
// return $MpdfFactory->createInlineResponse($mPdf, "file.pdf");
} catch (MpdfException $e) {
return new Response('ERROR -> '.$e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* @Route("/admin/reports/institutions/{institutionId}", name="institutionSingleReportRoute")
* @IsGranted("ROLE_USER")
* @param Request $request
* @param $institutionId
* @param MpdfFactory $MpdfFactory
* @return Response
*/
public function institutionSingleReport(Request $request, $institutionId, MpdfFactory $MpdfFactory): Response
{
$em = $this->getDoctrine()->getManager();
$institution = $em->getRepository("App:Institution")->findOneBy([
'id' => $institutionId
]);
$totals = $em->getRepository('App:Institution')->getInstitutionRegistrationsPerAdmin($institutionId);
if(!$institution){
return new Response('ERROR -> Institution not found', Response::HTTP_NOT_FOUND);
}
$bursary = $em->getRepository('App:Bursary')->findOneBy([],[ 'id' => 'DESC']);
if(!$bursary){
return new Response('ERROR -> NO bursary available', Response::HTTP_NOT_FOUND);
}
$user = $em->getRepository('App:User')->findOneBy([
'id' => $this->getUser()
]);
if(!$user){
return new Response('ERROR -> user not found', Response::HTTP_NOT_FOUND);
}
// $reportType = 'INSTITUTION APPLICATIONS PRELIMINARY REPORT';
$reportType = 'APPLICATIONS';// PRELIMINARY REPORT';
$students = $em->getRepository("App:Student")->findBy([
'institution' => $institution
],['admissionNumber' => 'ASC']);
$wardName = str_replace(" ", "_",$institution->getInstitutionName(), $count);
$file = str_replace(" ", "_", $institution->getInstitutionName(), $count). '.pdf';
try {
$mPdf = $MpdfFactory->createMpdfObject([
'mode' => 'utf-8',
'format' => 'A4',
'margin_header' => 10,
'margin_footer' => 5,
'orientation' => 'L'
// 'orientation' => 'P'
]);
$mPdf->SetWatermarkText($institution->getInstitutionName().' - '. $reportType);
$mPdf->watermark_font = 'DejaVuSansCondensed';
$mPdf->showWatermarkText = true;
$mPdf->showImageErrors = true;
//$mpdf->showWatermarkImage = true;
$mPdf->watermarkTextAlpha = 0.1;
// $mPdf->simpleTables = true;
$mPdf->SetTopMargin("50");
$mPdf->SetHTMLHeader($this->renderView('admin/reports/institutions/applications/header.html.twig', [
'institution_id' => base64_encode($institution->getId()),
'institution' => $institution,
'totals' => $totals,
'reportType'=>$reportType,
'bursary' => $bursary
]));
$mPdf->SetFooter($this->renderView('admin/reports/institutions/applications/footer.html.twig', [
'user' => $user,
'today' => new \DateTime()
]));
$mPdf->WriteHTML($this->renderView('admin/reports/institutions/applications/institution.html.twig', [
'students' => $students,
'institution' => $institution,
'totals' => $totals,
'today' => new \DateTime(),
'reportType' => $reportType
]));
$report = $em->getRepository('App:InstitutionApplicationReport')->findOneBy([
'institution' => $institution
]);
// return new Response('OK', Response::HTTP_CREATED);
return $MpdfFactory->createInlineResponse($mPdf, $file);
} catch (MpdfException $e) {
return new Response('ERROR -> '.$e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* @Route("/admin/reports/institutions/allocations/{institutionId}", name="adminInstitutionAllocationsReportRoute")
* @IsGranted("ROLE_USER")
* @param Request $request
* @param $institutionId
* @param MpdfFactory $MpdfFactory
* @return Response
*/
public function institutionAllocationReport(Request $request, $institutionId, MpdfFactory $MpdfFactory): Response
{
$em = $this->getDoctrine()->getManager();
$Aphase = $request->get('phase');
$institution = $em->getRepository("App:Institution")->findOneBy([
'id' => $institutionId
]);
$totals = $em->getRepository('App:Institution')->getAdminInstitutionAllocationsPerWard($institutionId,$Aphase);
if(!$institution){
return new Response('ERROR -> Institution not found', Response::HTTP_NOT_FOUND);
}
$bursary = $em->getRepository('App:Bursary')->findOneBy([],[ 'id' => 'DESC']);
if(!$bursary) {
return new Response('ERROR -> NO bursary available', Response::HTTP_NOT_FOUND);
}
$phase = $em->getRepository(Take::class)->findOneBy([
'bursary' => $bursary
],['id' => 'DESC']);
$availablePhase = " AND c.take_id = {$Aphase} ";
if(!$Aphase){
$availablePhase = '';
}
$sql = "SELECT a.gender,
concat(a.first_name,' ',a.middle_name,' ',a.sir_name) as sname,a.delete_,a.nemis_number,a.gender,sum(c.amount) as amount,d.level_name,a.admission_number,e.ward_name, a.course,
b.institution_name
FROM student a
JOIN institution b ON b.id = a.institution_id
JOIN student_allocation c ON c.student = a.id
LEFT JOIN study_level d ON a.study_level_id = d.id
JOIN ward e ON e.id = a.ward_id
JOIN institution_type f ON f.id = b.institution_type
WHERE c.is_cancelled = false
AND b.id = {$institutionId}
AND c.amount > 0 {$availablePhase}
GROUP BY c.student ORDER BY sname ";
// dump($sql);
// die;
$conn = $em->getConnection();
$stmt = $conn->query($sql);
$students = $stmt->fetchAll(PDO::FETCH_ASSOC);
$user = $em->getRepository('App:User')->findOneBy([
'id' => 78
]);
// die;
if(!$user){
return new Response('ERROR -> user not found', Response::HTTP_NOT_FOUND);
}
// $reportType = 'INSTITUTION APPLICATIONS PRELIMINARY REPORT';
$reportType = 'INSTITUTION ALLOCATION';// PRELIMINARY REPORT';
// $students = $em->getRepository("App:Student")->findBy([
// 'institution' => $institution
// ],['admissionNumber' => 'ASC']);
$fileName = '../public/reports/';
if (!file_exists($fileName)) {
mkdir($fileName, 0777, true);
}
$fileName .= $phase->getId().'_'.str_replace(" ", "_", $institution->getInstitutionName(), $count). '.pdf';
$file = $phase->getId().'_'.str_replace(" ", "_", $institution->getInstitutionName(), $count). '.pdf';
$report = new Report();
$report->setReportName($file);
$report->setInstitution($institution);
$report->setBursary($bursary);
$report->setCreatedAt(new DateTime());
$report->setCreatedBy($this->getUser());
$em->persist($report);
$em->flush();
$url = $this->generateUrl('bursary_institution_report', ['id' => $report->getId()]);
$url = "https://bursary.admin.embu.go.ke{$url}";
try {
$mPdf = $MpdfFactory->createMpdfObject([
'mode' => 'utf-8',
'format' => 'A4',
'margin_header' => 10,
'margin_footer' => 5,
'orientation' => 'L'
// 'orientation' => 'P'
]);
$mPdf->SetWatermarkText('ALLOCATION REPORT - '. $reportType);
$mPdf->watermark_font = 'DejaVuSansCondensed';
$mPdf->showWatermarkText = true;
$mPdf->showImageErrors = true;
//$mpdf->showWatermarkImage = true;
$mPdf->watermarkTextAlpha = 0.1;
// $mPdf->simpleTables = true;
$mPdf->SetTopMargin("50");
$mPdf->SetHTMLHeader($this->renderView('admin/reports/institutions/header.html.twig', [
'institution_id' => base64_encode($institution->getId()),
'institution' => $institution,
'totals' => $totals,
'bursary' => $bursary,
'reportType' => $reportType,
'url' => $url
]));
$mPdf->SetFooter($this->renderView('admin/reports/institutions/footer.html.twig', [
'user' => $user,
'today' => new DateTime()
]));
$mPdf->WriteHTML($this->renderView('admin/reports/institutions/institution.html.twig', [
'students' => $students,
'totals' => $totals,
'institution' => $institution,
'today' => new DateTime(),
'reportType' => $reportType
]));
// $filePath = '../reports/INSTITUTION_ALLOCATIONS/'.$fileName;
$mPdf->Output($fileName, 'F');
$institutionAllocation = new InstitutionAllocation();
$institutionAllocation->setAmount($totals['total_amount']);
$institutionAllocation->setInstitution($institution);
$institutionAllocation->setAllocationReport($fileName);
$institutionAllocation->setPhase($phase);
$em->persist($institutionAllocation);
$em->flush();
// return new Response('OK', Response::HTTP_CREATED);
return $MpdfFactory->createDownloadResponse($mPdf, $file);
// return $MpdfFactory->createInlineResponse($mPdf);
} catch (MpdfException $e) {
return new Response('ERROR -> '.$e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
// return new Response('OK', Response::HTTP_CREATED);
}
/**
* @Route("/reports/2024/{id}", name="bursary_institution_report")
*/
public function downloadInstitutionAllocationDocument($id){
$em = $this->getDoctrine()->getManager();
$report = $em->getRepository(Report::class)->findOneBy([
'id' => $id
]);
$file = "../public/reports/{$report->getReportName()}";
return $this->file($file);
}
}