Files
kupshop/bundles/External/ZNZBundle/Controller/ZNZDocumentController.php
2025-08-02 16:30:27 +02:00

75 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace External\ZNZBundle\Controller;
use External\ZNZBundle\Util\ZNZApi;
use KupShop\ContentBundle\View\OrderView;
use KupShop\KupShopBundle\Routing\TranslatedRoute;
use KupShop\OrderingBundle\Attachment\InvoiceXLSAttachment;
use KupShop\OrderingBundle\Exception\UnknownAttachmentTypeException;
use KupShop\OrderingBundle\Util\Attachment\AttachmentLocator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ZNZDocumentController extends AbstractController
{
#[TranslatedRoute(path: '/#orderView#/{id_order}/pdf/', name: 'kupshop_orderingbundle_pdfinvoice', requirements: ['id_order' => '\d+'], priority: 99)]
public function znzDocument(Request $request, ZNZApi $api, int $id_order): Response
{
OrderView::checkOrderOwnership($id_order, $request->get('cf'));
$order = \Order::get($id_order);
$documentId = $request->get('documentId');
$documents = $order->getData('znzDocuments') ?: [];
if (!$documentId && !empty($documents)) {
$documentId = array_keys($documents)[0] ?? null;
}
if (!($document = $order->getData('znzDocuments')[$documentId] ?? null)) {
throw new NotFoundHttpException('Document is not attached to current order!');
}
if (!($content = $api->getDocument($document['path']))) {
throw new NotFoundHttpException('Document not found in remote path!');
}
$response = new Response($content);
$response->headers->set('Content-Type', 'application/pdf; charset=utf8');
return $response;
}
#[TranslatedRoute(path: '/#orderView#/{orderId}/xlsx/', requirements: ['orderId' => '\d+'])]
public function orderAttachmentXLSX(AttachmentLocator $locator, int $orderId): Response
{
$order = new \Order();
$order->createFromDB($orderId);
if ($order->order_no === null) {
$this->createNotFoundException('Order not found!');
}
try {
$attachment = $locator->getServiceByType(InvoiceXLSAttachment::getType(), $order);
$response = new Response();
$response->headers->set('Content-Type', $attachment->getMimeType());
$response->headers->set('Content-Disposition', 'inline; filename="'.$attachment->getFilename().'"');
if (!($content = $attachment->getContent())) {
throw new \UnexpectedValueException('PDF error: server returned false');
}
return $response->setContent($content);
} catch (UnknownAttachmentTypeException) {
$this->createNotFoundException('Unknown attachment type!');
}
throw new \RuntimeException('Failed generating XLSX');
}
}