src/Controller/ApiV2/Residents/BillController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ApiV2\Residents;
  3. use App\Controller\ApiV2\AbstractController;
  4. use App\Entity\MoneyLog;
  5. use App\Repository\BuildingRepository;
  6. use App\Repository\FlatRepository;
  7. use App\Repository\MoneyLogRepository;
  8. use App\Response\PdfResponse;
  9. use App\Security\Voter\FlatVoter;
  10. use App\Services\BillService;
  11. use App\Services\FlatService;
  12. use App\Services\MoneyLogService;
  13. use Doctrine\ORM\NonUniqueResultException;
  14. use Doctrine\ORM\NoResultException;
  15. use FOS\RestBundle\Controller\Annotations as Rest;
  16. use Knp\Snappy\Pdf;
  17. use Psr\Container\ContainerExceptionInterface;
  18. use Psr\Container\NotFoundExceptionInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. class BillController extends AbstractController
  23. {
  24.     private FlatRepository $flatRepository;
  25.     private FlatService $flatService;
  26.     private Pdf $pdf;
  27.     private MoneyLogRepository $moneyLogRepository;
  28.     private MoneyLogService $moneyLogService;
  29.     /**
  30.      * @param BuildingRepository $buildingRepository
  31.      * @param FlatRepository $flatRepository
  32.      * @param FlatService $flatService
  33.      * @param Pdf $pdf
  34.      * @param MoneyLogRepository $moneyLogRepository
  35.      * @param MoneyLogService $moneyLogService
  36.      */
  37.     public function __construct(
  38.         BuildingRepository $buildingRepository,
  39.         FlatRepository $flatRepository,
  40.         FlatService $flatService,
  41.         Pdf $pdf,
  42.         MoneyLogRepository $moneyLogRepository,
  43.         MoneyLogService $moneyLogService
  44.     ) {
  45.         parent::__construct($buildingRepository);
  46.         $this->flatRepository $flatRepository;
  47.         $this->flatService $flatService;
  48.         $this->pdf $pdf;
  49.         $this->moneyLogRepository $moneyLogRepository;
  50.         $this->moneyLogService $moneyLogService;
  51.     }
  52.     /**
  53.      * @Rest\Get("", name="residents_bills_index")
  54.      * @param Request $request
  55.      * @param int $id
  56.      * @return array<string, mixed>
  57.      * @throws NoResultException
  58.      * @throws NonUniqueResultException
  59.      */
  60.     public function index(Request $requestint $id): array
  61.     {
  62.         $flat $this->flatRepository->find($id);
  63.         if (!$flat) {
  64.             $this->createNotFoundException('Stan nije pronađen.');
  65.         }
  66.         $this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT$flat);
  67.         return $this->moneyLogService->getMoneyLogsByFlatPaginated($flat$this->getListOptions($request));
  68.     }
  69.     /**
  70.      * @Rest\Get("/{billId}", name="resident_bill_pdf")
  71.      * @param int $id
  72.      * @param int $billId
  73.      * @return Response
  74.      * @throws ContainerExceptionInterface
  75.      * @throws NonUniqueResultException
  76.      * @throws NotFoundExceptionInterface
  77.      */
  78.     public function getFlatBillPdf(int $idint $billId): Response
  79.     {
  80.         $flat $this->flatRepository->find($id);
  81.         $this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT$flat);
  82.         if (!$flat) {
  83.             throw $this->createNotFoundException('Stan nije pronađen.');
  84.         }
  85.         /** @var MoneyLog|null $bill */
  86.         $bill $this->moneyLogRepository->find($billId);
  87.         if (!$bill) {
  88.             throw new NotFoundHttpException('Unable to find entry.');
  89.         }
  90.         $billInformation $this->flatService->generateMonthBillPdf($flat$bill$billId);
  91.         if ($bill->getBuilding()->getType() === 1) {
  92.             $html $this->renderView(
  93.                 '@views/Bill/generateBillPdf.html.twig',
  94.                 array_merge($billInformation['basicData'], [
  95.                     'flat'                => $bill->getFlat()->getDisplayFlatId(),
  96.                     'generatedQrCode'     => $billInformation['generatedQrCode'] ?? null,
  97.                     'separateBillDebit'   => $billInformation['previousSeparateBillDebit']
  98.                 ])
  99.             );
  100.         } else {
  101.             $html $this->renderView(
  102.                 '@views/Bill/generateBillPdfBusinessSpace.default.html.twig',
  103.                 array_merge(
  104.                     $billInformation['basicData'],
  105.                     [
  106.                         'flat'                => $flat,
  107.                         'buildingIdBs'        => $bill->getBuilding()->getBuildingIdBs()
  108.                     ]
  109.                 )
  110.             );
  111.         }
  112.         $pdfContent $this->pdf->getOutputFromHtml($html, [
  113.             'page-size'   => 'A4',
  114.             'orientation' => 'Portrait',
  115.             'dpi'         => 300
  116.         ]);
  117.         return new Response($pdfContent200, [
  118.             'Content-Type'        => 'application/pdf',
  119.             'Content-Disposition' => sprintf("%s.pdf"md5($billId))
  120.         ]);
  121.     }
  122. }