src/Controller/admin/ReportsController.php line 262

Open in your IDE?
  1. <?php
  2. namespace App\Controller\admin;
  3. use App\Entity\institutionAllocation;
  4. use App\Entity\InstitutionApplicationReport;
  5. use App\Entity\Report;
  6. use App\Entity\student\ApplicationDocument;
  7. use App\Entity\Take;
  8. use DateTime;
  9. use Mpdf\MpdfException;
  10. use PDO;
  11. use Sasedev\MpdfBundle\Factory\MpdfFactory;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class ReportsController extends AbstractController {
  19.     /**
  20.      * @Route("/admin/reports/fund_request", name="fundRequestReportRoute")
  21.      * @IsGranted("ROLE_USER")
  22.      * @param Request $request
  23.      * @param MpdfFactory $MpdfFactory
  24.      * @return Response
  25.      */
  26.     public function institutionReport(Request $requestMpdfFactory $MpdfFactory): Response
  27.     {
  28.         $em $this->getDoctrine()->getManager();
  29.         $date date_modify(new \DateTime'-9day '), '+24 hour');
  30.         $bursary $em->getRepository('App:Bursary')->findOneBy([],[ 'id' => 'DESC']);
  31.         $user $em->getRepository('App:User')->findOneBy([
  32.             'id' => $this->getUser()
  33.         ]);
  34. //        $allocations = $em->getRepository('App:StudentAllocation')->getAllocations(58725000);
  35.         $amounts 6036000;
  36.         $allocations $em->getRepository('App:StudentAllocation')->getAllocations($amounts);
  37. //        $allocations = $em->getRepository('App:StudentAllocation')->getAllocations(5498000);
  38.         $totals $em->getRepository('App:StudentAllocation')->getAllocationTotals();
  39.         $realAmount 0;
  40.         foreach ($allocations as $index => $allocation) {
  41.                 $realAmount += str_replace(','''$allocation['amount']);
  42.         }
  43.         dump($realAmount);
  44.         $fileName '../reports/';
  45.         if (!file_exists($fileName)) {
  46.             mkdir($fileName0777true);
  47.         }
  48.         $fileName .= 'fund_allocation_list.pdf';
  49.         try {
  50.             ini_set("pcre.backtrack_limit""50000000");
  51.             ini_set('max_execution_time''300');
  52.             ini_set('memory_limit''1024M');
  53.             $mPdf $MpdfFactory->createMpdfObject([
  54.                 'mode' => 'utf-8',
  55.                 'format' => 'A4',
  56.                 'margin_header' =>,
  57.                 'margin_footer' => 5,
  58.                 'orientation' => 'L'
  59. //                'orientation' => 'P'
  60.             ]);
  61.             $mPdf->SetWatermarkText('2021 - 2022 - ALLOCATION');
  62.             $mPdf->watermark_font 'DejaVuSansCondensed';
  63.             $mPdf->showWatermarkText true;
  64.             $mPdf->showImageErrors true;
  65.             //$mpdf->showWatermarkImage = true;
  66.             $mPdf->watermarkTextAlpha 0.1;
  67. //            $mPdf->simpleTables = true;
  68.             $mPdf->SetTopMargin("50");
  69.             $mPdf->SetHTMLHeader($this->renderView('admin/reports/fund_request/header.html.twig', [
  70.                 'bursary' => $bursary,
  71.                 'file_code' => base64_encode('file-'.rand(100010000))
  72.             ]));
  73.             $mPdf->SetFooter($this->renderView('admin/reports/fund_request/footer.html.twig', [
  74.                 'user' => $user,
  75.                 'today' => $date
  76.             ]));
  77.             dump($totals);
  78. //            return $this->render('admin/reports/fund_request/allocations.html.twig', [
  79. //                'allocations' => $allocations,
  80. //                'today' => new \DateTime(),
  81. //                'totals' => $totals
  82. //            ]);
  83. //            print_r($allocations);
  84.             $mPdf->WriteHTML($this->renderView('admin/reports/fund_request/allocations.html.twig', [
  85.                     'allocations' => $allocations,
  86.                     'today' => $date,
  87.                     'totals' =>$totals
  88.             ]));
  89.             $mPdf->Output($fileName'F');
  90. //            return new Response('OK', Response::HTTP_CREATED);
  91.             return $MpdfFactory->createDownloadResponse($mPdf$fileName);
  92. //            return $MpdfFactory->createInlineResponse($mPdf, "file.pdf");
  93.         } catch (MpdfException $e) {
  94.             return new Response('ERROR -> '.$e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
  95.         }
  96.     }
  97.     /**
  98.      * @Route("/admin/reports/institutions/{institutionId}", name="institutionSingleReportRoute")
  99.      * @IsGranted("ROLE_USER")
  100.      * @param Request $request
  101.      * @param $institutionId
  102.      * @param MpdfFactory $MpdfFactory
  103.      * @return Response
  104.      */
  105.     public function institutionSingleReport(Request $request$institutionIdMpdfFactory $MpdfFactory): Response
  106.     {
  107.          $em $this->getDoctrine()->getManager();
  108.         $institution $em->getRepository("App:Institution")->findOneBy([
  109.             'id' => $institutionId
  110.         ]);
  111.         $totals $em->getRepository('App:Institution')->getInstitutionRegistrationsPerAdmin($institutionId);
  112.         if(!$institution){
  113.             return new Response('ERROR -> Institution not found'Response::HTTP_NOT_FOUND);
  114.         }
  115.         $bursary $em->getRepository('App:Bursary')->findOneBy([],[ 'id' => 'DESC']);
  116.         if(!$bursary){
  117.             return new Response('ERROR -> NO bursary available'Response::HTTP_NOT_FOUND);
  118.         }
  119.         $user $em->getRepository('App:User')->findOneBy([
  120.             'id' => $this->getUser()
  121.         ]);
  122.         if(!$user){
  123.             return new Response('ERROR -> user not found'Response::HTTP_NOT_FOUND);
  124.         }
  125. //        $reportType = 'INSTITUTION APPLICATIONS PRELIMINARY REPORT';
  126.         $reportType 'APPLICATIONS';// PRELIMINARY REPORT';
  127.         $students $em->getRepository("App:Student")->findBy([
  128.             'institution' => $institution
  129.         ],['admissionNumber' => 'ASC']);
  130.         $wardName str_replace(" ""_",$institution->getInstitutionName(), $count);
  131.         $file str_replace(" ""_"$institution->getInstitutionName(), $count). '.pdf';
  132.         try {
  133.             $mPdf $MpdfFactory->createMpdfObject([
  134.                 'mode' => 'utf-8',
  135.                 'format' => 'A4',
  136.                 'margin_header' => 10,
  137.                 'margin_footer' => 5,
  138.                 'orientation' => 'L'
  139. //                'orientation' => 'P'
  140.             ]);
  141.             $mPdf->SetWatermarkText($institution->getInstitutionName().' - '$reportType);
  142.             $mPdf->watermark_font 'DejaVuSansCondensed';
  143.             $mPdf->showWatermarkText true;
  144.             $mPdf->showImageErrors true;
  145.             //$mpdf->showWatermarkImage = true;
  146.             $mPdf->watermarkTextAlpha 0.1;
  147. //            $mPdf->simpleTables = true;
  148.             $mPdf->SetTopMargin("50");
  149.             $mPdf->SetHTMLHeader($this->renderView('admin/reports/institutions/applications/header.html.twig', [
  150.                 'institution_id' => base64_encode($institution->getId()),
  151.                 'institution' => $institution,
  152.                 'totals' => $totals,
  153.                 'reportType'=>$reportType,
  154.                 'bursary' => $bursary
  155.             ]));
  156.             $mPdf->SetFooter($this->renderView('admin/reports/institutions/applications/footer.html.twig', [
  157.                 'user' => $user,
  158.                 'today' => new \DateTime()
  159.             ]));
  160.             $mPdf->WriteHTML($this->renderView('admin/reports/institutions/applications/institution.html.twig', [
  161.                 'students' => $students,
  162.                 'institution' => $institution,
  163.                 'totals' => $totals,
  164.                 'today' => new \DateTime(),
  165.                 'reportType' => $reportType
  166.             ]));
  167.             $report $em->getRepository('App:InstitutionApplicationReport')->findOneBy([
  168.                 'institution' => $institution
  169.             ]);
  170. //            return new Response('OK', Response::HTTP_CREATED);
  171.             return $MpdfFactory->createInlineResponse($mPdf$file);
  172.         } catch (MpdfException $e) {
  173.             return new Response('ERROR -> '.$e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
  174.         }
  175.      }
  176.     /**
  177.      * @Route("/admin/reports/institutions/allocations/{institutionId}", name="adminInstitutionAllocationsReportRoute")
  178.      * @IsGranted("ROLE_USER")
  179.      * @param Request $request
  180.      * @param $institutionId
  181.      * @param MpdfFactory $MpdfFactory
  182.      * @return Response
  183.      */
  184.     public function institutionAllocationReport(Request $request$institutionIdMpdfFactory $MpdfFactory): Response
  185.     {
  186.         $em $this->getDoctrine()->getManager();
  187.         $Aphase $request->get('phase');
  188.         $institution $em->getRepository("App:Institution")->findOneBy([
  189.             'id' => $institutionId
  190.         ]);
  191.         $totals $em->getRepository('App:Institution')->getAdminInstitutionAllocationsPerWard($institutionId,$Aphase);
  192.         if(!$institution){
  193.             return new Response('ERROR -> Institution not found'Response::HTTP_NOT_FOUND);
  194.         }
  195.         $bursary $em->getRepository('App:Bursary')->findOneBy([],[ 'id' => 'DESC']);
  196.         if(!$bursary) {
  197.             return new Response('ERROR -> NO bursary available'Response::HTTP_NOT_FOUND);
  198.         }
  199.         $phase $em->getRepository(Take::class)->findOneBy([
  200.             'bursary' => $bursary
  201.         ],['id' => 'DESC']);
  202.         $availablePhase " AND c.take_id = {$Aphase} ";
  203.         if(!$Aphase){
  204.             $availablePhase '';
  205.         }
  206.         $sql "SELECT a.gender,
  207.                      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,
  208.                         b.institution_name
  209.                       FROM student a 
  210.                             JOIN institution b ON b.id = a.institution_id
  211.                             JOIN student_allocation c ON c.student = a.id
  212.                             LEFT JOIN study_level d ON a.study_level_id = d.id
  213.                             JOIN ward e ON e.id = a.ward_id
  214.                             JOIN institution_type f ON f.id = b.institution_type
  215.                         WHERE c.is_cancelled = false 
  216.                           AND b.id = {$institutionId} 
  217.                           AND c.amount > 0 {$availablePhase} 
  218.                         GROUP BY c.student ORDER BY sname ";
  219. //        dump($sql);
  220. //        die;
  221.         $conn $em->getConnection();
  222.         $stmt $conn->query($sql);
  223.         $students $stmt->fetchAll(PDO::FETCH_ASSOC);
  224.         $user $em->getRepository('App:User')->findOneBy([
  225.             'id' => 78
  226.         ]);
  227. //        die;
  228.         if(!$user){
  229.             return new Response('ERROR -> user not found'Response::HTTP_NOT_FOUND);
  230.         }
  231. //        $reportType = 'INSTITUTION APPLICATIONS PRELIMINARY REPORT';
  232.         $reportType 'INSTITUTION ALLOCATION';// PRELIMINARY REPORT';
  233. //        $students = $em->getRepository("App:Student")->findBy([
  234. //            'institution' => $institution
  235. //        ],['admissionNumber' => 'ASC']);
  236.         $fileName '../public/reports/';
  237.         if (!file_exists($fileName)) {
  238.             mkdir($fileName0777true);
  239.         }
  240.         $fileName .= $phase->getId().'_'.str_replace(" ""_"$institution->getInstitutionName(), $count). '.pdf';
  241.         $file $phase->getId().'_'.str_replace(" ""_"$institution->getInstitutionName(), $count). '.pdf';
  242.         $report = new Report();
  243.         $report->setReportName($file);
  244.         $report->setInstitution($institution);
  245.         $report->setBursary($bursary);
  246.         $report->setCreatedAt(new DateTime());
  247.         $report->setCreatedBy($this->getUser());
  248.         $em->persist($report);
  249.         $em->flush();
  250.         $url $this->generateUrl('bursary_institution_report', ['id' => $report->getId()]);
  251.         $url "https://bursary.admin.embu.go.ke{$url}";
  252.         try {
  253.             $mPdf $MpdfFactory->createMpdfObject([
  254.                 'mode' => 'utf-8',
  255.                 'format' => 'A4',
  256.                 'margin_header' => 10,
  257.                 'margin_footer' => 5,
  258.                 'orientation' => 'L'
  259. //                'orientation' => 'P'
  260.             ]);
  261.             $mPdf->SetWatermarkText('ALLOCATION REPORT - '$reportType);
  262.             $mPdf->watermark_font 'DejaVuSansCondensed';
  263.             $mPdf->showWatermarkText true;
  264.             $mPdf->showImageErrors true;
  265.             //$mpdf->showWatermarkImage = true;
  266.             $mPdf->watermarkTextAlpha 0.1;
  267. //            $mPdf->simpleTables = true;
  268.             $mPdf->SetTopMargin("50");
  269.             $mPdf->SetHTMLHeader($this->renderView('admin/reports/institutions/header.html.twig', [
  270.                 'institution_id' => base64_encode($institution->getId()),
  271.                 'institution' => $institution,
  272.                 'totals' => $totals,
  273.                 'bursary' => $bursary,
  274.                 'reportType' => $reportType,
  275.                 'url' => $url
  276.             ]));
  277.             $mPdf->SetFooter($this->renderView('admin/reports/institutions/footer.html.twig', [
  278.                 'user' => $user,
  279.                 'today' => new DateTime()
  280.             ]));
  281.             $mPdf->WriteHTML($this->renderView('admin/reports/institutions/institution.html.twig', [
  282.                 'students' => $students,
  283.                 'totals' => $totals,
  284.                 'institution' => $institution,
  285.                 'today' => new DateTime(),
  286.                 'reportType' => $reportType
  287.             ]));
  288. //            $filePath = '../reports/INSTITUTION_ALLOCATIONS/'.$fileName;
  289.             $mPdf->Output($fileName'F');
  290.             $institutionAllocation = new InstitutionAllocation();
  291.             $institutionAllocation->setAmount($totals['total_amount']);
  292.             $institutionAllocation->setInstitution($institution);
  293.             $institutionAllocation->setAllocationReport($fileName);
  294.             $institutionAllocation->setPhase($phase);
  295.             $em->persist($institutionAllocation);
  296.             $em->flush();
  297. //            return new Response('OK', Response::HTTP_CREATED);
  298.             return $MpdfFactory->createDownloadResponse($mPdf$file);
  299. //            return $MpdfFactory->createInlineResponse($mPdf);
  300.         } catch (MpdfException $e) {
  301.             return new Response('ERROR -> '.$e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
  302.         }
  303. //        return new Response('OK', Response::HTTP_CREATED);
  304.     }
  305.     /**
  306.      * @Route("/reports/2024/{id}", name="bursary_institution_report")
  307.      */
  308.     public function downloadInstitutionAllocationDocument($id){
  309.         $em $this->getDoctrine()->getManager();
  310.         $report $em->getRepository(Report::class)->findOneBy([
  311.             'id' => $id
  312.         ]);
  313.         $file "../public/reports/{$report->getReportName()}";
  314.         return $this->file($file);
  315.     }
  316. }