<?php
namespace App\Controller\CashBox;
use App\Entity\FiskalyTransaction;
use App\Entity\Product;
use App\Entity\Receipt;
use Endroid\QrCode\Builder\BuilderInterface;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCodeBundle\Response\QrCodeResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ImageController extends AbstractController
{
/**
* @Route("/cashboximage/{id}", name="cash_box_image")
*/
public function index(Product $product): Response
{
if ($product->getImage() !== null && file_exists($this->getParameter('kernel.project_dir') . '/public/uploads/logo/' . $product->getImage())) {
return new BinaryFileResponse($this->getParameter('kernel.project_dir') . '/public/uploads/logo/' . $product->getImage());
} else {
return new BinaryFileResponse($this->getParameter('kernel.project_dir') . '/public/uploads/logo/noimage.png');
}
}
/**
* @Route("/receiptqrcode/{id}", name="receiptqrcode_image")
*/
public function receiptQrCode(Receipt $receipt, BuilderInterface $builder, PngWriter $pngWriter): ?QrCodeResponse
{
$qr_code_data = "";
/**
* @var FiskalyTransaction $fiskaly_transaction
*/
$fiskaly_transaction = $receipt->getFiskalyTransactions()->last();
if ($fiskaly_transaction !== null && $fiskaly_transaction !== false && $fiskaly_transaction->getQrCodeData() !== null) {
$qr_code_data = $fiskaly_transaction->getQrCodeData();
// Pixel = dpi * mm / 25.4 mm
$pixel_to_mm_factor = 25.4;
$max_paper_mm = 50; // 80mm receipt
$printer_dpi = 203; //most default Thermal-Printer dpi
$size = $printer_dpi * $max_paper_mm / $pixel_to_mm_factor;
$result = $builder
->writer($pngWriter)
->size($size)
->margin(0)
->data($qr_code_data)
->build();
return new QrCodeResponse($result);
}
return null;
}
}