<?php
namespace App\Controller\Invoice;
use App\Entity\Order\Order;
use App\Entity\VignetteInvoice\VignetteInvoice;
use App\Manager\Order\OrderManager;
use App\Model\ShopUserModel;
use App\Service\CustomerService;
use App\Service\InvoiceApiService;
use App\Service\InvoiceService;
use App\Service\SystemService;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
class InvoiceController extends ResourceController
{
public function sendInvoiceToCustomer(Request $request): Response
{
$isAdmin = ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager());
$response = new Response(json_encode(array('error' => 1)));
if ($isAdmin) {
$invoiceId = $request->get('invoiceId');
$invoice = $this->getDoctrine()->getRepository(VignetteInvoice::class)->find($invoiceId);
if ($invoice) {
$invoiceService = new InvoiceService($this->getDoctrine()->getManager(), $this->container);
if ($invoiceService->sendInvoiceToCustomer($invoice)) {
$response = new Response(json_encode(array('success' => 1)));
$this->addFlash('success', 'Factura trimisa cu succes');
} else {
$this->addFlash('error', 'A intervenit o eroare');
}
}
}
return $response;
}
public function issueInvoiceManually(Request $request): Response
{
$response = new Response(json_encode(array('error' => 1)));
$isAdmin = ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager());
if ($isAdmin) {
$orderId = $request->get('orderId');
$order = $this->getDoctrine()->getRepository(Order::class)->find($orderId);
if ($order) {
$invoiceService = new InvoiceService($this->getDoctrine()->getManager(), $this->container);
if ($invoiceService->generateInvoiceManually($order)) {
$response = new Response(json_encode(array('success' => 1)));
$this->addFlash('success', 'Factura geneerata si trimisa cu succes');
} else {
$this->addFlash('error', 'A intervenit o eroare');
}
}
}
return $response;
}
public function issueSpecificInvoiceManually(Request $request)
{
$response = new Response(json_encode(array('error' => 1)));
$isAdmin = ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager());
if ($isAdmin) {
$invoiceService = new InvoiceService($this->getDoctrine()->getManager(), $this->container);
if ($invoiceService->issueSpecificInvoiceManually($request->get('orderId'), $request->get('productType'))) {
$response = new Response(json_encode(array('success' => 1)));
$this->addFlash('success', 'Factura geneerata si trimisa cu succes');
} else {
$this->addFlash('error', 'A intervenit o eroare');
}
}
return $response;
}
public function showSpecificInvoice(Request $request)
{
$response = new Response(json_encode(array('status' => 0)));
$customer = CustomerService::retrieveCustomerFromShopUser($this->getUser());
$invoice = $this->getDoctrine()->getRepository(VignetteInvoice::class)->findOneBy(array('order' => $request->get('orderId'), 'productType' => $request->get('productType')));
if ($customer && $invoice && OrderManager::checkIfCustomerIsTheSameAsInTheInvoice($customer, $invoice)) {
$apiInvoiceService = new InvoiceApiService();
$invoiceFileName = SystemService::retrieveMessage('invoice', $this->container->get('translator'));
if ($invoice->getProductType() === VignetteInvoice::PRODUCT_LICENSE_RECOVERY ||
$invoice->getProductType() === VignetteInvoice::PRODUCT_TELEMEDICINE
) {
$pdfContent = $apiInvoiceService->getInvoicePDFViaBillingService($invoice->getApiInvoiceId());
if (!$pdfContent) {
return $response;
}
$filename = "{$invoiceFileName} {$invoice->getSeries()} {$invoice->getNumber()}";
InvoiceService::openPDFContent($pdfContent, $filename);
$response = new Response(json_encode(array('status' => 1)));
} else {
$apiInvoiceService->getPdfByInvoice($invoice);
if ($response = $apiInvoiceService->getLastResponse()) {
$response = json_decode($response);
if ($response->invoice_id > 0) {
$url = $response->dload_url;
$filename = $invoiceFileName . time();
InvoiceService::openPdf($url, $filename);
$response = new Response(json_encode(array('status' => 1)));
}
}
}
}
return $response;
}
public function showInvoiceAction(Request $request, TranslatorInterface $translator)
{
$currentUser= $this->get('sylius.context.customer')->getCustomer();
if ($currentUser) {
$orderId = $request->get('order_id');
$orderRepository = $this->getDoctrine()->getRepository(Order::class);
if ($orderId) {
$order = $orderRepository->find($orderId);
if ($order) {
$customerId = $order->getCustomer()->getId();
$currentUserId = $currentUser->getId();
if ($customerId == $currentUserId) {
$invoice = $order->getFirstInvoice();
$apiInvoiceService = new InvoiceApiService();
$apiInvoiceService->getPdfByInvoice($invoice);
$response = $apiInvoiceService->getLastResponse();
if ($response) {
$response = json_decode($response);
if ($response->invoice_id > 0) {
$url = $response->dload_url;
$filename = $translator->trans('app.ui.invoice') . time();
InvoiceService::openPdf($url, $filename);
return new Response('success');
}
} else {
return new Response('fail');
}
}
}
}
}
return $this->redirectToRoute('app_shop_vignette_order_history');
}
public function retrieveInvoicePdf(Request $request) {
$response = json_encode('fail');
$invoiceId = $request->get('id');
if ($invoiceId) {
$invoice = $this->getDoctrine()->getRepository(VignetteInvoice::class)->find($invoiceId);
if ($invoice) {
$apiInvoiceService = new InvoiceApiService();
$apiInvoiceService->getPdfByInvoice($invoice);
$response = $apiInvoiceService->getLastResponse();
if ($response) {
$response = json_decode($response);
if ($response->invoice_id > 0) {
$url = $response->dload_url;
$response = json_encode(chunk_split(base64_encode(file_get_contents($url))));
}
}
}
}
return new Response($response);
}
public function showAdminInvoice(Request $request, TranslatorInterface $translator)
{
$invoiceId = $request->get('id');
if ($invoiceId) {
$invoice = $this->getDoctrine()->getRepository(VignetteInvoice::class)->find($invoiceId);
if ($invoice) {
$apiInvoiceService = new InvoiceApiService();
$apiInvoiceService->getPdfByInvoice($invoice);
$response = $apiInvoiceService->getLastResponse();
if ($response) {
$response = json_decode($response);
if ($response->invoice_id > 0) {
$url = $response->dload_url;
$filename = $translator->trans('app.ui.invoice') . time();
InvoiceService::openPdf($url, $filename);
return new Response('success');
}
} else {
return new Response('fail');
}
}
}
return new Response('error');
}
}