src/Controller/Invoice/InvoiceController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Invoice;
  3. use App\Entity\Order\Order;
  4. use App\Entity\VignetteInvoice\VignetteInvoice;
  5. use App\Manager\Order\OrderManager;
  6. use App\Model\ShopUserModel;
  7. use App\Service\CustomerService;
  8. use App\Service\InvoiceApiService;
  9. use App\Service\InvoiceService;
  10. use App\Service\SystemService;
  11. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Translation\TranslatorInterface;
  15. class InvoiceController extends ResourceController
  16. {
  17.     public function sendInvoiceToCustomer(Request $request): Response
  18.     {
  19.         $isAdmin ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager());
  20.         $response = new Response(json_encode(array('error' => 1)));
  21.         if ($isAdmin) {
  22.             $invoiceId $request->get('invoiceId');
  23.             $invoice $this->getDoctrine()->getRepository(VignetteInvoice::class)->find($invoiceId);
  24.             if ($invoice) {
  25.                 $invoiceService = new InvoiceService($this->getDoctrine()->getManager(), $this->container);
  26.                 if ($invoiceService->sendInvoiceToCustomer($invoice)) {
  27.                     $response = new Response(json_encode(array('success' => 1)));
  28.                     $this->addFlash('success''Factura trimisa cu succes');
  29.                 } else {
  30.                     $this->addFlash('error''A intervenit o eroare');
  31.                 }
  32.             }
  33.         }
  34.         return $response;
  35.     }
  36.     public function issueInvoiceManually(Request $request): Response
  37.     {
  38.         $response = new Response(json_encode(array('error' => 1)));
  39.         $isAdmin ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager());
  40.         if ($isAdmin) {
  41.             $orderId $request->get('orderId');
  42.             $order $this->getDoctrine()->getRepository(Order::class)->find($orderId);
  43.             if ($order) {
  44.                 $invoiceService = new InvoiceService($this->getDoctrine()->getManager(), $this->container);
  45.                 if ($invoiceService->generateInvoiceManually($order)) {
  46.                     $response = new Response(json_encode(array('success' => 1)));
  47.                     $this->addFlash('success''Factura geneerata si trimisa cu succes');
  48.                 } else {
  49.                     $this->addFlash('error''A intervenit o eroare');
  50.                 }
  51.             }
  52.         }
  53.         return $response;
  54.     }
  55.     public function issueSpecificInvoiceManually(Request $request)
  56.     {
  57.         $response = new Response(json_encode(array('error' => 1)));
  58.         $isAdmin ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager());
  59.         if ($isAdmin) {
  60.             $invoiceService = new InvoiceService($this->getDoctrine()->getManager(), $this->container);
  61.             if ($invoiceService->issueSpecificInvoiceManually($request->get('orderId'), $request->get('productType'))) {
  62.                 $response = new Response(json_encode(array('success' => 1)));
  63.                 $this->addFlash('success''Factura geneerata si trimisa cu succes');
  64.             } else {
  65.                 $this->addFlash('error''A intervenit o eroare');
  66.             }
  67.         }
  68.         return $response;
  69.     }
  70.     public function showSpecificInvoice(Request $request)
  71.     {
  72.         $response = new Response(json_encode(array('status' => 0)));
  73.         $customer CustomerService::retrieveCustomerFromShopUser($this->getUser());
  74.         $invoice $this->getDoctrine()->getRepository(VignetteInvoice::class)->findOneBy(array('order' => $request->get('orderId'), 'productType' => $request->get('productType')));
  75.         if ($customer && $invoice && OrderManager::checkIfCustomerIsTheSameAsInTheInvoice($customer$invoice)) {
  76.             $apiInvoiceService = new InvoiceApiService();
  77.             $invoiceFileName SystemService::retrieveMessage('invoice'$this->container->get('translator'));
  78.             if ($invoice->getProductType() === VignetteInvoice::PRODUCT_LICENSE_RECOVERY ||
  79.                 $invoice->getProductType() === VignetteInvoice::PRODUCT_TELEMEDICINE
  80.             ) {
  81.                 $pdfContent $apiInvoiceService->getInvoicePDFViaBillingService($invoice->getApiInvoiceId());
  82.                 if (!$pdfContent) {
  83.                     return $response;
  84.                 }
  85.                 $filename "{$invoiceFileName} {$invoice->getSeries()} {$invoice->getNumber()}";
  86.                 InvoiceService::openPDFContent($pdfContent$filename);
  87.                 $response = new Response(json_encode(array('status' => 1)));
  88.             } else {
  89.                 $apiInvoiceService->getPdfByInvoice($invoice);
  90.                 if ($response $apiInvoiceService->getLastResponse()) {
  91.                     $response json_decode($response);
  92.                     if ($response->invoice_id 0) {
  93.                         $url $response->dload_url;
  94.                         $filename $invoiceFileName time();
  95.                         InvoiceService::openPdf($url$filename);
  96.                         $response = new Response(json_encode(array('status' => 1)));
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.         return $response;
  102.     }
  103.     public function showInvoiceAction(Request $requestTranslatorInterface $translator)
  104.     {
  105.         $currentUser$this->get('sylius.context.customer')->getCustomer();
  106.         if ($currentUser) {
  107.             $orderId $request->get('order_id');
  108.             $orderRepository $this->getDoctrine()->getRepository(Order::class);
  109.             if ($orderId) {
  110.                 $order $orderRepository->find($orderId);
  111.                 if ($order) {
  112.                     $customerId $order->getCustomer()->getId();
  113.                     $currentUserId $currentUser->getId();
  114.                     if ($customerId == $currentUserId) {
  115.                         $invoice $order->getFirstInvoice();
  116.                         $apiInvoiceService = new InvoiceApiService();
  117.                         $apiInvoiceService->getPdfByInvoice($invoice);
  118.                         $response $apiInvoiceService->getLastResponse();
  119.                         if ($response) {
  120.                             $response json_decode($response);
  121.                             if ($response->invoice_id 0) {
  122.                                 $url $response->dload_url;
  123.                                 $filename $translator->trans('app.ui.invoice') . time();
  124.                                 InvoiceService::openPdf($url$filename);
  125.                                 return new Response('success');
  126.                             }
  127.                         } else {
  128.                             return  new Response('fail');
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.         return $this->redirectToRoute('app_shop_vignette_order_history');
  135.     }
  136.     public function retrieveInvoicePdf(Request $request) {
  137.         $response json_encode('fail');
  138.         $invoiceId $request->get('id');
  139.         if ($invoiceId) {
  140.             $invoice $this->getDoctrine()->getRepository(VignetteInvoice::class)->find($invoiceId);
  141.             if ($invoice) {
  142.                 $apiInvoiceService = new InvoiceApiService();
  143.                 $apiInvoiceService->getPdfByInvoice($invoice);
  144.                 $response $apiInvoiceService->getLastResponse();
  145.                 if ($response) {
  146.                     $response json_decode($response);
  147.                     if ($response->invoice_id 0) {
  148.                         $url $response->dload_url;
  149.                         $response json_encode(chunk_split(base64_encode(file_get_contents($url))));
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.         return new Response($response);
  155.     }
  156.     public function showAdminInvoice(Request $requestTranslatorInterface $translator)
  157.     {
  158.         $invoiceId $request->get('id');
  159.         if ($invoiceId) {
  160.             $invoice $this->getDoctrine()->getRepository(VignetteInvoice::class)->find($invoiceId);
  161.             if ($invoice) {
  162.                 $apiInvoiceService = new InvoiceApiService();
  163.                 $apiInvoiceService->getPdfByInvoice($invoice);
  164.                 $response $apiInvoiceService->getLastResponse();
  165.                 if ($response) {
  166.                     $response json_decode($response);
  167.                     if ($response->invoice_id 0) {
  168.                         $url $response->dload_url;
  169.                         $filename $translator->trans('app.ui.invoice') . time();
  170.                         InvoiceService::openPdf($url$filename);
  171.                         return new Response('success');
  172.                     }
  173.                 } else {
  174.                     return  new Response('fail');
  175.                 }
  176.             }
  177.         }
  178.         return new Response('error');
  179.     }
  180. }