src/Controller/SupplierController.php line 220

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Supplier;
  4. use App\Exception\AccessDeniedException;
  5. use App\Form\Type\SupplierType;
  6. use App\Repository\PurchaseOrderRepository;
  7. use App\Service\SupplierService;
  8. use Exception;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route(
  16.  *     "/suppliers"
  17.  * )
  18.  */
  19. class SupplierController extends AbstractController
  20. {
  21.     /**
  22.      * @var SupplierService
  23.      */
  24.     protected SupplierService $supplierService;
  25.     /**
  26.      * @param SupplierService $supplierService
  27.      */
  28.     public function __construct(SupplierService $supplierService) {
  29.         $this->supplierService $supplierService;
  30.     }
  31.     /**
  32.      * @Route(
  33.      *     "/create",
  34.      *     name="app.supplier.create"
  35.      * )
  36.      *
  37.      * @param Request $request
  38.      *
  39.      * @return Response
  40.      *
  41.      * @throws AccessDeniedException
  42.      */
  43.     public function createAction(Request $request): Response
  44.     {
  45.         $supplier = new Supplier('');
  46.         $this->supplierService->assertCreate($supplier);
  47.         $form $this->createForm(
  48.             SupplierType::class,
  49.             $supplier,
  50.             [
  51.                 'action' => $this->generateUrl(
  52.                     'app.supplier.create'
  53.                 ),
  54.             ]
  55.         );
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted()) {
  58.             if ($form->isValid()) {
  59.                 try {
  60.                     $this->supplierService->create($supplier);
  61.                     $this->flashSuccess('ui.success.create', [], 'Supplier');
  62.                     return $this->redirectResponse('app.supplier.index');
  63.                 } catch (Exception $e) {
  64.                     $this->flashError('ui.error.create', [], 'Supplier');
  65.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  66.                         $this->flashError($e->getMessage());
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.         return $this->render(
  72.             'Supplier/create.html.twig',
  73.             [
  74.                 'form'     => $form->createView(),
  75.                 'supplier' => new Supplier(''),
  76.             ]
  77.         );
  78.     }
  79.     /**
  80.      * @Route(
  81.      *     "/{supplier}/delete",
  82.      *     name="app.supplier.delete",
  83.      *     requirements={"supplier" = "\d+"}
  84.      * )
  85.      *
  86.      * @param Supplier $supplier
  87.      *
  88.      * @return RedirectResponse
  89.      *
  90.      * @throws AccessDeniedException
  91.      */
  92.     public function deleteAction(Supplier $supplier): RedirectResponse
  93.     {
  94.         $this->supplierService->assertDelete($supplier);
  95.         try {
  96.             $this->supplierService->delete($supplier);
  97.             $this->flashSuccess('ui.success.delete', [], 'Supplier');
  98.         } catch (Exception $e) {
  99.             $this->flashError('ui.error.delete', [], 'Supplier');
  100.             if ($this->getCurrentUser()->isSuperAdmin()) {
  101.                 $this->flashError($e->getMessage());
  102.             }
  103.         }
  104.         return $this->redirectResponse('app.supplier.index');
  105.     }
  106.     /**
  107.      * @Route(
  108.      *     "/",
  109.      *     name="app.supplier.index"
  110.      * )
  111.      *
  112.      * @return Response
  113.      */
  114.     public function indexAction(): Response
  115.     {
  116.         if (null === $this->supplierService->getInventory()) {
  117.             $this->flashWarning('ui.error.no_inventory_address', [], 'Supplier');
  118.         }
  119.         return $this->render(
  120.             'Supplier/index.html.twig',
  121.             [
  122.                 'suppliers'   => $this->supplierService->getAll(),
  123.                 'newSupplier' => new Supplier(''),
  124.             ]
  125.         );
  126.     }
  127.     /**
  128.      * @Route(
  129.      *     "/inventory-address",
  130.      *     name="app.rest.supplier.inventory_address"
  131.      * )
  132.      *
  133.      * @return JsonResponse
  134.      */
  135.     public function restGetInventoryAddressAction(): JsonResponse
  136.     {
  137.         $data = [
  138.             'success' => true,
  139.             'address' => null,
  140.             'error'   => null,
  141.         ];
  142.         try {
  143.             if (null === $inventorySupplier $this->supplierService->getInventory()) {
  144.                 throw new Exception(
  145.                     $this->trans('ui.error.no_inventory_address', [], 'Supplier')
  146.                 );
  147.             }
  148.             $data['address'] = $inventorySupplier->serializeAddress();
  149.         } catch (Exception $e) {
  150.             $data['success'] = false;
  151.             $data['error'] = $e->getMessage();
  152.         }
  153.         return new JsonResponse($data);
  154.     }
  155.     /**
  156.      * @Route(
  157.      *     "/{supplier}/active",
  158.      *     name="app.rest.supplier.set_active",
  159.      *     requirements={"supplier" = "\d+"}
  160.      * )
  161.      *
  162.      * @param Supplier $supplier
  163.      *
  164.      * @return JsonResponse
  165.      */
  166.     public function restSetSupplierActive(Supplier $supplier): JsonResponse
  167.     {
  168.         $data = [
  169.             'success' => true,
  170.             'error'   => null,
  171.         ];
  172.         try {
  173.             $this->supplierService->assertUpdate($supplier);
  174.             $supplier->setActive($_POST['active']);
  175.             $this->supplierService->update($supplier);
  176.         } catch (Exception $e) {
  177.             $data['success'] = false;
  178.             $data['error'] = $this->trans('ui.error.update', [], 'Supplier');
  179.             if ($this->getCurrentUser()->isSuperAdmin()) {
  180.                 $data['error'] .= ' ' $e->getMessage();
  181.             }
  182.         }
  183.         return new JsonResponse($data);
  184.     }
  185.     /**
  186.      * @Route(
  187.      *     "/{supplier}/update",
  188.      *     name="app.supplier.update",
  189.      *     requirements={"supplier" = "\d+"}
  190.      * )
  191.      *
  192.      * @param Request  $request
  193.      * @param Supplier $supplier
  194.      *
  195.      * @return Response
  196.      *
  197.      * @throws AccessDeniedException
  198.      */
  199.     public function updateAction(Request $requestSupplier $supplier): Response
  200.     {
  201.         $this->supplierService->assertUpdate($supplier);
  202.         $form $this->createForm(
  203.             SupplierType::class,
  204.             $supplier,
  205.             [
  206.                 'action' => $this->generateUrl(
  207.                     'app.supplier.update',
  208.                     ['supplier' => $supplier->getId()]
  209.                 ),
  210.                 'disabled' => ! $this->supplierService->canUpdate($supplier),
  211.             ]
  212.         );
  213.         $wasInventoryTransactions $supplier->isInventoryTransactions();
  214.         $form->handleRequest($request);
  215.         if ($form->isSubmitted()) {
  216.             if ($form->isValid()) {
  217.                 try {
  218.                     if ($wasInventoryTransactions && !$supplier->isInventoryTransactions() && count($supplier->getPurchaseOrders()) > 0) {
  219.                         $this->flashWarning('ui.warning.remove_inventory_transactions', [], 'Supplier');
  220.                         $supplier->setInventoryTransactions(true);
  221.                     }
  222.                     $this->supplierService->update($supplier);
  223.                     $this->flashSuccess('ui.success.update', [], 'Supplier');
  224.                     return $this->redirectResponse('app.supplier.index');
  225.                 } catch (Exception $e) {
  226.                     $this->flashError('ui.error.update', [], 'Supplier');
  227.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  228.                         $this->flashError($e->getMessage());
  229.                     }
  230.                 }
  231.             }
  232.         }
  233.         return $this->render(
  234.             'Supplier/update.html.twig',
  235.             [
  236.                 'supplier' => $supplier,
  237.                 'form'     => $form->createView(),
  238.             ]
  239.         );
  240.     }
  241. }