<?php
namespace App\Controller\ApiV2\Residents;
use App\Controller\ApiV2\AbstractController;
use App\Entity\MoneyLog;
use App\Repository\BuildingRepository;
use App\Repository\FlatRepository;
use App\Repository\MoneyLogRepository;
use App\Response\PdfResponse;
use App\Security\Voter\FlatVoter;
use App\Services\BillService;
use App\Services\FlatService;
use App\Services\MoneyLogService;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use FOS\RestBundle\Controller\Annotations as Rest;
use Knp\Snappy\Pdf;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BillController extends AbstractController
{
private FlatRepository $flatRepository;
private FlatService $flatService;
private Pdf $pdf;
private MoneyLogRepository $moneyLogRepository;
private MoneyLogService $moneyLogService;
/**
* @param BuildingRepository $buildingRepository
* @param FlatRepository $flatRepository
* @param FlatService $flatService
* @param Pdf $pdf
* @param MoneyLogRepository $moneyLogRepository
* @param MoneyLogService $moneyLogService
*/
public function __construct(
BuildingRepository $buildingRepository,
FlatRepository $flatRepository,
FlatService $flatService,
Pdf $pdf,
MoneyLogRepository $moneyLogRepository,
MoneyLogService $moneyLogService
) {
parent::__construct($buildingRepository);
$this->flatRepository = $flatRepository;
$this->flatService = $flatService;
$this->pdf = $pdf;
$this->moneyLogRepository = $moneyLogRepository;
$this->moneyLogService = $moneyLogService;
}
/**
* @Rest\Get("", name="residents_bills_index")
* @param Request $request
* @param int $id
* @return array<string, mixed>
* @throws NoResultException
* @throws NonUniqueResultException
*/
public function index(Request $request, int $id): array
{
$flat = $this->flatRepository->find($id);
if (!$flat) {
$this->createNotFoundException('Stan nije pronađen.');
}
$this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT, $flat);
return $this->moneyLogService->getMoneyLogsByFlatPaginated($flat, $this->getListOptions($request));
}
/**
* @Rest\Get("/{billId}", name="resident_bill_pdf")
* @param int $id
* @param int $billId
* @return Response
* @throws ContainerExceptionInterface
* @throws NonUniqueResultException
* @throws NotFoundExceptionInterface
*/
public function getFlatBillPdf(int $id, int $billId): Response
{
$flat = $this->flatRepository->find($id);
$this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT, $flat);
if (!$flat) {
throw $this->createNotFoundException('Stan nije pronađen.');
}
/** @var MoneyLog|null $bill */
$bill = $this->moneyLogRepository->find($billId);
if (!$bill) {
throw new NotFoundHttpException('Unable to find entry.');
}
$billInformation = $this->flatService->generateMonthBillPdf($flat, $bill, $billId);
if ($bill->getBuilding()->getType() === 1) {
$html = $this->renderView(
'@views/Bill/generateBillPdf.html.twig',
array_merge($billInformation['basicData'], [
'flat' => $bill->getFlat()->getDisplayFlatId(),
'generatedQrCode' => $billInformation['generatedQrCode'] ?? null,
'separateBillDebit' => $billInformation['previousSeparateBillDebit']
])
);
} else {
$html = $this->renderView(
'@views/Bill/generateBillPdfBusinessSpace.default.html.twig',
array_merge(
$billInformation['basicData'],
[
'flat' => $flat,
'buildingIdBs' => $bill->getBuilding()->getBuildingIdBs()
]
)
);
}
$pdfContent = $this->pdf->getOutputFromHtml($html, [
'page-size' => 'A4',
'orientation' => 'Portrait',
'dpi' => 300
]);
return new Response($pdfContent, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf("%s.pdf", md5($billId))
]);
}
}