src/Controller/IndentRequestController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\IndentRequest;
  4. use App\Form\IndentRequestType;
  5. use App\Repository\CaseRepository;
  6. use App\Repository\IndentRequestRepository;
  7. use App\Service\Pagination;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use PhpOffice\PhpSpreadsheet\{IOFactorySpreadsheetStyle\Alignment};
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\{RequestResponseResponseHeaderBag};
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Nzo\UrlEncryptorBundle\Annotations\ParamDecryptor;
  15. #[Route(path'/indents')]
  16. #[Security("is_granted('ROLE_PHARMA') or is_granted('ROLE_LAB_TECH') or is_granted('ROLE_WARD') or is_granted('ROLE_STOCK') or is_granted('ROLE_RAD') or is_granted('ROLE_DENT_WARD')")]
  17. class IndentRequestController extends AbstractController
  18. {
  19.     #[Route(path'/'name'indent_request_index'methods: ['GET'])]
  20.     public function index(Request $requestPagination $paginationCaseRepository $caseRepoIndentRequestRepository $irRepo): Response
  21.     {
  22.         $indents $pagination->paginate($this->getIndentRequestQuery($request$irRepo), $request$this->getParameter('items_per_page'));
  23.         return $this->render('indent_request/index.html.twig', [
  24.             'indents' => $indents,
  25.             'drugs' => $caseRepo->getEssentialDrugs($this->getUser()->getHospital()->getType()),
  26.             'last_page' => $pagination->lastPage($indents),
  27.             'url' => 'indent_request_index'
  28.         ]);
  29.     }
  30.     #[Route(path'/export'name'indent_request_export'methods: ['GET'])]
  31.     public function export(Request $requestIndentRequestRepository $irRepo): Response
  32.     {
  33.         $indents $this->getIndentRequestQuery($request$irRepo)->getResult();
  34.         $spreadsheet = new Spreadsheet();
  35.         $spreadsheet->setActiveSheetIndex(0)
  36.             ->setCellValue('A1''Indent Requests')
  37.             ->mergeCells('A1:N1')
  38.             ->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  39.         $spreadsheet->getActiveSheet()
  40.             ->setCellValue('A3''Indent No.')
  41.             ->setCellValue('B3''Stock Center')
  42.             ->setCellValue('C3''Major Demand Category')
  43.             ->setCellValue('D3''Sub-category')
  44.             ->setCellValue('E3''Group')
  45.             ->setCellValue('F3''Sub-group')
  46.             ->setCellValue('G3''Item')
  47.             ->setCellValue('H3''Drug Type')
  48.             ->setCellValue('I3''Strength')
  49.             ->setCellValue('J3''Basic Unit')
  50.             ->setCellValue('K3''Unit Pack')
  51.             ->setCellValue('L3''Description')
  52.             ->setCellValue('M3''Demand in Unit Pack')
  53.             ->setCellValue('N3''Status');
  54.         $i 4;
  55.         if ($indents) {
  56.             foreach ($indents as $indent) {
  57.                 if ($indent->getItemOther()) {
  58.                     $itemType $indent->getItemOther();
  59.                 } elseif ($indent->getLabMaterial()) {
  60.                     $itemType $indent->getLabMaterial();
  61.                 } elseif ($indent->getEssentialEquipment()) {
  62.                     $itemType $indent->getEssentialEquipment();
  63.                 } elseif ($indent->getDrug()) {
  64.                     $itemType $indent->getDrug();
  65.                 } else {
  66.                     $itemType '-';
  67.                 }
  68.                 $spreadsheet->getActiveSheet()
  69.                     ->setCellValue('A' $i$indent->getIndentNum())
  70.                     ->setCellValue('B' $i$indent->getSource())
  71.                     ->setCellValue('C' $i$indent->getDemandCategory())
  72.                     ->setCellValue('D' $i$indent->getDemandSubCategory())
  73.                     ->setCellValue('E' $i$indent->getGroupName())
  74.                     ->setCellValue('F' $i$indent->getSubGroupName())
  75.                     ->setCellValue('G' $i$itemType)
  76.                     ->setCellValue('H' $i$indent->getDrugType() ?: '-')
  77.                     ->setCellValue('I' $i$indent->getStrength() ?: '-')
  78.                     ->setCellValue('J' $i$indent->getBasicUnit() ?: '-')
  79.                     ->setCellValue('K' $i$indent->getUnitPack() ?: '-')
  80.                     ->setCellValue('L' $i$indent->getDescription())
  81.                     ->setCellValue('M' $i$indent->getRecdDemandUnitPack() ?: '-')
  82.                     ->setCellValue('N' $i$indent->getStatus() == 'Partial' 'Partially Accepted' $indent->getStatus());
  83.                 $i++;
  84.             }
  85.         } else {
  86.             $spreadsheet->getActiveSheet()
  87.                 ->setCellValue('A4''No records found.')
  88.                 ->mergeCells('A4:N4')
  89.                 ->getStyle('A4')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  90.         }
  91.         $fileName 'indent-requests-' date('d-m-Y') . '.xlsx';
  92.         $tempFile tempnam(sys_get_temp_dir(), $fileName);
  93.         $writer IOFactory::createWriter($spreadsheet'Xlsx');
  94.         $writer->save($tempFile);
  95.         return $this->file($tempFile$fileNameResponseHeaderBag::DISPOSITION_INLINE);
  96.     }
  97.     #[Route(path'/new'name'indent_request_new'methods: ['GET''POST'])]
  98.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  99.     {
  100.         $indentRequest = new IndentRequest();
  101.         $indentRequest
  102.             ->setHospital($this->getUser()->getHospital())
  103.             ->setDepartment($this->getDepartment());
  104.         $form $this->createForm(IndentRequestType::class, $indentRequest);
  105.         $form->handleRequest($request);
  106.         if ($form->isSubmitted() && $form->isValid()) {
  107.             $entityManager->persist($indentRequest);
  108.             $entityManager->flush();
  109.             $indentRequest->setIndentNum('IND-' date('Y') . '-' sprintf('%07d'$indentRequest->getId()));
  110.             $entityManager->persist($indentRequest);
  111.             $entityManager->flush();
  112.             $this->addFlash(
  113.                 'success',
  114.                 'New indent request generated successfully!'
  115.             );
  116.             return $this->redirectToRoute('indent_request_index');
  117.         }
  118.         return $this->renderForm('indent_request/new.html.twig', [
  119.             'indent_request' => $indentRequest,
  120.             'form' => $form
  121.         ]);
  122.     }
  123.     #[Route(path'/view/{id}'name'indent_request_show'methods: ['GET'])]
  124.     #[ParamDecryptor(["id"])]
  125.     public function show(IndentRequest $indentRequest): Response
  126.     {
  127.         return $this->render('indent_request/show.html.twig', [
  128.             'indent' => $indentRequest
  129.         ]);
  130.     }
  131.     private function getDepartment()
  132.     {
  133.         return match (true) {
  134.             $this->isGranted('ROLE_PHARMA') ||
  135.                 $this->isGranted('ROLE_DOC') ||
  136.                 $this->isGranted('ROLE_HOSP_ADMIN') ||
  137.                 $this->isGranted('ROLE_DENT') => 'Pharmacy',
  138.             $this->isGranted('ROLE_LAB_TECH') => 'Laboratory',
  139.             $this->isGranted('ROLE_RAD') => 'Radiology',
  140.             default => 'Ward'
  141.         };
  142.     }
  143.     private function getIndentRequestQuery(Request $requestIndentRequestRepository $irRepo)
  144.     {
  145.         /** @var User user entity */
  146.         $user $this->getUser();
  147.         $qb $irRepo->createQueryBuilder('id')
  148.             ->where('id.hospital = :hospital')
  149.             ->setParameter('hospital'$user->getHospital())
  150.             ->orderBy('id.createdAt''DESC');
  151.         if (!$this->isGranted('ROLE_STOCK')) {
  152.             $qb
  153.                 ->andWhere('id.createdBy = :user')
  154.                 ->setParameter(':user'$user);
  155.         }
  156.         if ($request->get('drug')) {
  157.             $qb
  158.                 ->leftJoin('id.drug''d')
  159.                 ->andWhere('d.id = :id')
  160.                 ->setParameter('id'$request->get('drug'));
  161.         }
  162.         if ($request->get('range')) {
  163.             [$from$to] = explode(' / '$request->get('range'));
  164.             if ($from && $to) {
  165.                 $qb
  166.                     ->andWhere('id.updatedAt BETWEEN :from AND :to')
  167.                     ->setParameter('from'$from)
  168.                     ->setParameter('to'$to);
  169.             }
  170.         }
  171.         return $qb->getQuery();
  172.     }
  173. }