src/Controller/CompanyController.php line 563

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BankAccount;
  4. use App\Entity\Company;
  5. use App\Entity\CompanyAddress;
  6. use App\Entity\CompanyConsigneeAddress;
  7. use App\Entity\CompanyEndCustomer;
  8. use App\Entity\CompanyDeliveryAddress;
  9. use App\Entity\CompanyInvoiceAddress;
  10. use App\Exception\AccessDeniedException;
  11. use App\Form\Type\CompanyAddressType;
  12. use App\Form\Type\CompanyEndCustomerType;
  13. use App\Form\Type\CompanyType;
  14. use App\Service\CompanyService;
  15. use Exception;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. /**
  22.  * @Route(
  23.  *     "/companies"
  24.  * )
  25.  */
  26. class CompanyController extends AbstractController
  27. {
  28.     /**
  29.      * @var CompanyService
  30.      */
  31.     protected CompanyService $companyService;
  32.     /**
  33.      * @param CompanyService $companyService
  34.      */
  35.     public function __construct(CompanyService $companyService)
  36.     {
  37.         $this->companyService $companyService;
  38.     }
  39.     /**
  40.      * @Route(
  41.      *     "/create",
  42.      *     name="app.company.create"
  43.      * )
  44.      *
  45.      * @param Request $request
  46.      *
  47.      * @return Response
  48.      *
  49.      * @throws AccessDeniedException
  50.      */
  51.     public function createAction(Request $request): Response
  52.     {
  53.         $this->companyService->assertCreate(new Company(''));
  54.         $form $this->createForm(
  55.             CompanyType::class,
  56.             null,
  57.             [
  58.                 'action' => $this->generateUrl(
  59.                     'app.company.create'
  60.                 ),
  61.             ]
  62.         );
  63.         $form->handleRequest($request);
  64.         if ($form->isSubmitted()) {
  65.             if ($form->isValid()) {
  66.                 try {
  67.                     /* @var Company $company */
  68.                     $company $form->getData();
  69.                     $this->companyService->create($company);
  70.                     $this->flashSuccess('ui.success.update', [], 'Company');
  71.                     return $this->redirectResponse(
  72.                         'app.company.index',
  73.                         [],
  74.                         'company-' $company->getId()
  75.                     );
  76.                 } catch (Exception $e) {
  77.                     $this->flashError('ui.error.update', [], 'Company');
  78.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  79.                         $this->flashError($e->getMessage());
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         return $this->render(
  85.             'Company/create.html.twig',
  86.             [
  87.                 'form' => $form->createView(),
  88.             ]
  89.         );
  90.     }
  91.     /**
  92.      * @param Request        $request
  93.      * @param CompanyAddress $address
  94.      *
  95.      * @return Response
  96.      */
  97.     private function createAddress(Request $requestCompanyAddress $address): Response
  98.     {
  99.         $formAction $this->generateUrl(
  100.             'app.company.address.' $address->getType() . '.create',
  101.             [
  102.                 'company' => $address->getCompany()->getId(),
  103.             ]
  104.         );
  105.         $form $this->createForm(
  106.             CompanyAddressType::class,
  107.             $address,
  108.             [
  109.                 'action' => $formAction,
  110.             ]
  111.         );
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted()) {
  114.             if ($form->isValid()) {
  115.                 try {
  116.                     $this->companyService->createAddress($address);
  117.                     $this->flashSuccess('ui.success.create', [], 'CompanyAddress');
  118.                     return $this->redirectResponse(
  119.                         'app.company.index',
  120.                         [],
  121.                         'company-' $address->getCompany()->getId()
  122.                     );
  123.                 } catch (Exception $e) {
  124.                     $this->flashError('ui.error.create', [], 'CompanyAddress');
  125.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  126.                         $this->flashError($e->getMessage());
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.         return $this->render(
  132.             'CompanyAddress/create.html.twig',
  133.             [
  134.                 'type'    => $address->getType(),
  135.                 'company' => $address->getCompany(),
  136.                 'form'    => $form->createView(),
  137.             ]
  138.         );
  139.     }
  140.     /**
  141.      * @Route(
  142.      *     "/{company}/addresses/consignee/create",
  143.      *     name="app.company.address.consignee.create",
  144.      *     requirements={"company" = "\d+"}
  145.      * )
  146.      *
  147.      * @param Request $request
  148.      * @param Company $company
  149.      *
  150.      * @return Response
  151.      */
  152.     public function createConsigneeAddressAction(Request $requestCompany $company): Response
  153.     {
  154.         return $this->createAddress(
  155.             $request,
  156.             new CompanyConsigneeAddress($company)
  157.         );
  158.     }
  159.     /**
  160.      * @Route(
  161.      *     "/{company}/customers/create",
  162.      *     name="app.company.customer.create",
  163.      *     requirements={"company" = "\d+"}
  164.      * )
  165.      *
  166.      * @param Request $request
  167.      * @param Company $company
  168.      *
  169.      * @return Response
  170.      */
  171.     public function createCustomerAction(Request $requestCompany $company): Response
  172.     {
  173.         $form $this->createForm(
  174.             CompanyEndCustomerType::class,
  175.             null,
  176.             [
  177.                 'action' => $this->generateUrl(
  178.                     'app.company.customer.create',
  179.                     [
  180.                         'company' => $company->getId(),
  181.                     ]
  182.                 ),
  183.                 'company' => $company,
  184.             ]
  185.         );
  186.         $form->handleRequest($request);
  187.         if ($form->isSubmitted()) {
  188.             if ($form->isValid()) {
  189.                 try {
  190.                     /* @var CompanyEndCustomer $customer */
  191.                     $customer $form->getData();
  192.                     $this->companyService->createCustomer($customer);
  193.                     $this->flashSuccess('ui.success.update', [], 'EndCustomer');
  194.                     return $this->redirectResponse(
  195.                         'app.company.index',
  196.                         [],
  197.                         'company-' $company->getId()
  198.                     );
  199.                 } catch (Exception $e) {
  200.                     $this->flashError('ui.error.update', [], 'EndCustomer');
  201.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  202.                         $this->flashError($e->getMessage());
  203.                     }
  204.                 }
  205.             }
  206.         }
  207.         return $this->render(
  208.             'CompanyCustomer/create.html.twig',
  209.             [
  210.                 'company' => $company,
  211.                 'form'    => $form->createView(),
  212.             ]
  213.         );
  214.     }
  215.     /**
  216.      * @Route(
  217.      *     "/{company}/addresses/delivery/create",
  218.      *     name="app.company.address.delivery.create",
  219.      *     requirements={"company" = "\d+"}
  220.      * )
  221.      *
  222.      * @param Request $request
  223.      * @param Company $company
  224.      *
  225.      * @return Response
  226.      */
  227.     public function createDeliveryAddressAction(Request $requestCompany $company): Response
  228.     {
  229.         return $this->createAddress(
  230.             $request,
  231.             new CompanyDeliveryAddress($company)
  232.         );
  233.     }
  234.     /**
  235.      * @Route(
  236.      *     "/{company}/addresses/invoice/create",
  237.      *     name="app.company.address.invoice.create",
  238.      *     requirements={"company" = "\d+"}
  239.      * )
  240.      *
  241.      * @param Request $request
  242.      * @param Company $company
  243.      *
  244.      * @return Response
  245.      */
  246.     public function createInvoiceAddressAction(Request $requestCompany $company): Response
  247.     {
  248.         return $this->createAddress(
  249.             $request,
  250.             new CompanyInvoiceAddress($company)
  251.         );
  252.     }
  253.     /**
  254.      * @Route(
  255.      *     "/{company}/delete",
  256.      *     name="app.company.delete",
  257.      *     requirements={"company" = "\d+"}
  258.      * )
  259.      *
  260.      * @param Company $company
  261.      *
  262.      * @return RedirectResponse
  263.      *
  264.      * @throws AccessDeniedException
  265.      */
  266.     public function deleteAction(Company $company): RedirectResponse
  267.     {
  268.         $this->companyService->assertDelete($company);
  269.         try {
  270.             $this->companyService->delete($company);
  271.             $this->flashSuccess('ui.success.delete', [], 'Company');
  272.         } catch (Exception $e) {
  273.             $this->flashError('ui.error.delete', [], 'Company');
  274.             if ($this->getCurrentUser()->isSuperAdmin()) {
  275.                 $this->flashError($e->getMessage());
  276.             }
  277.         }
  278.         return $this->redirectResponse('app.company.index');
  279.     }
  280.     /**
  281.      * @Route(
  282.      *     "/addresses/{address}/delete",
  283.      *     name="app.company.address.delete",
  284.      *     requirements={"address" = "\d+"}
  285.      * )
  286.      *
  287.      * @param CompanyAddress $address
  288.      *
  289.      * @return RedirectResponse
  290.      *
  291.      * @throws AccessDeniedException
  292.      */
  293.     public function deleteAddressAction(CompanyAddress $address): RedirectResponse
  294.     {
  295.         $this->companyService->assertDelete($address->getCompany());
  296.         $company $address->getCompany();
  297.         try {
  298.             $this->companyService->deleteAddress($address);
  299.             $this->flashSuccess('ui.success.delete', [], 'CompanyAddress');
  300.         } catch (Exception $e) {
  301.             $this->flashError('ui.error.delete', [], 'CompanyAddress');
  302.             if ($this->getCurrentUser()->isSuperAdmin()) {
  303.                 $this->flashError($e->getMessage());
  304.             }
  305.         }
  306.         return $this->redirectResponse(
  307.             'app.company.index',
  308.             [],
  309.             'company-' $company->getId()
  310.         );
  311.     }
  312.     /**
  313.      * @Route(
  314.      *     "/customers/{customer}/delete",
  315.      *     name="app.company.customer.delete",
  316.      *     requirements={"customer" = "\d+"}
  317.      * )
  318.      *
  319.      * @param CompanyEndCustomer $customer
  320.      *
  321.      * @return RedirectResponse
  322.      *
  323.      * @throws AccessDeniedException
  324.      */
  325.     public function deleteEndCustomerAction(CompanyEndCustomer $customer): RedirectResponse
  326.     {
  327.         $this->companyService->assertDelete($customer->getCompany());
  328.         $company $customer->getCompany();
  329.         try {
  330.             $this->companyService->deleteCustomer($customer);
  331.             $this->flashSuccess('ui.success.delete', [], 'EndCustomer');
  332.         } catch (Exception $e) {
  333.             $this->flashError('ui.error.delete', [], 'EndCustomer');
  334.             if ($this->getCurrentUser()->isSuperAdmin()) {
  335.                 $this->flashError($e->getMessage());
  336.             }
  337.         }
  338.         return $this->redirectResponse(
  339.             'app.company.index',
  340.             [],
  341.             'company-' $company->getId()
  342.         );
  343.     }
  344.     /**
  345.      * @Route(
  346.      *     "/",
  347.      *     name="app.company.index"
  348.      * )
  349.      *
  350.      * @return Response
  351.      */
  352.     public function indexAction(): Response
  353.     {
  354.         return $this->render(
  355.             'Company/index.html.twig',
  356.             [
  357.                 'companies'  => $this->companyService->getAll(),
  358.                 'newCompany' => new Company(''),
  359.             ]
  360.         );
  361.     }
  362.     /**
  363.      * @Route(
  364.      *     "/rest/addresses/{address}/delete",
  365.      *     name="app.rest.company.address.delete",
  366.      *     requirements={"address" = "\d+"},
  367.      *     methods={"DELETE"}
  368.      * )
  369.      *
  370.      * @param CompanyAddress $address
  371.      *
  372.      * @return JsonResponse
  373.      */
  374.     public function restDeleteAddressAction(CompanyAddress $address): JsonResponse
  375.     {
  376.         $status 200;
  377.         $data = [
  378.             'id'      => $address->getId(),
  379.             'message' => null,
  380.         ];
  381.         try {
  382.             $this->companyService->deleteAddress($address);
  383.             $data['message'] = $this->trans('ui.success.delete', [], 'EndCustomer');
  384.         } catch (Exception $e) {
  385.             $status 500;
  386.             $data['message'] = $this->trans('ui.error.delete', [], 'EndCustomer');
  387.             if ($this->getCurrentUser()->isSuperAdmin()) {
  388.                 $data['message'] .= "\n" $e->getMessage();
  389.             }
  390.         }
  391.         return new JsonResponse($data$status);
  392.     }
  393.     /**
  394.      * @Route(
  395.      *     "/rest/customers/{customer}/delete",
  396.      *     name="app.rest.company.customer.delete",
  397.      *     requirements={"customer" = "\d+"},
  398.      *     methods={"DELETE"}
  399.      * )
  400.      *
  401.      * @param CompanyEndCustomer $customer
  402.      *
  403.      * @return JsonResponse
  404.      */
  405.     public function restDeleteEndCustomerAction(CompanyEndCustomer $customer): JsonResponse
  406.     {
  407.         $status 200;
  408.         $data = [
  409.             'id'      => $customer->getId(),
  410.             'message' => null,
  411.         ];
  412.         try {
  413.             $this->companyService->deleteCustomer($customer);
  414.             $data['message'] = $this->trans('ui.success.delete', [], 'EndCustomer');
  415.         } catch (Exception $e) {
  416.             $status 500;
  417.             $data['message'] = $this->trans('ui.error.delete', [], 'EndCustomer');
  418.             if ($this->getCurrentUser()->isSuperAdmin()) {
  419.                 $data['message'] .= "\n" $e->getMessage();
  420.             }
  421.         }
  422.         return new JsonResponse($data$status);
  423.     }
  424.     /**
  425.      * @Route(
  426.      *     "/{company}/address",
  427.      *     name="app.rest.company.address",
  428.      *     requirements={"company" = "\d+"}
  429.      * )
  430.      *
  431.      * @param Company $company
  432.      *
  433.      * @return JsonResponse
  434.      */
  435.     public function restGetAddressAction(Company $company): JsonResponse
  436.     {
  437.         $data = [
  438.             'success' => true,
  439.             'address' => null,
  440.         ];
  441.         try {
  442.             $data['address'] = $company->serializeAddress();
  443.         } catch (Exception $e) {
  444.             $data['success'] = false;
  445.         }
  446.         return new JsonResponse($data);
  447.     }
  448.     /**
  449.      * @Route(
  450.      *     "/{company}/update",
  451.      *     name="app.company.update",
  452.      *     requirements={"company" = "\d+"}
  453.      * )
  454.      *
  455.      * @param Request $request
  456.      * @param Company $company
  457.      *
  458.      * @return Response
  459.      *
  460.      * @throws AccessDeniedException
  461.      */
  462.     public function updateAction(Request $requestCompany $company): Response
  463.     {
  464.         $this->companyService->assertRead($company);
  465.         $form $this->createForm(
  466.             CompanyType::class,
  467.             $company,
  468.             [
  469.                 'action' => $this->generateUrl(
  470.                     'app.company.update',
  471.                     ['company' => $company->getId()]
  472.                 ),
  473.                 'disabled' => ! $this->companyService->canUpdate($company),
  474.             ]
  475.         );
  476.         $form->handleRequest($request);
  477.         if ($form->isSubmitted()) {
  478.             if ($form->isValid()) {
  479.                 try {
  480.                     $this->companyService->update($company);
  481.                     $this->flashSuccess('ui.success.update', [], 'Company');
  482.                     return $this->redirectResponse(
  483.                         'app.company.index',
  484.                         [],
  485.                         'company-' $company->getId()
  486.                     );
  487.                 } catch (Exception $e) {
  488.                     $this->flashError('ui.error.update', [], 'Company');
  489.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  490.                         $this->flashError($e->getMessage());
  491.                     }
  492.                 }
  493.             } else {
  494.                 $this->flashError('ui.error.validate', [], 'Company');
  495.             }
  496.         }
  497.         return $this->render(
  498.             'Company/update.html.twig',
  499.             [
  500.                 'company' => $company,
  501.                 'form'    => $form->createView(),
  502.             ]
  503.         );
  504.     }
  505.     /**
  506.      * @Route(
  507.      *     "/addresses/{address}/update",
  508.      *     name="app.company.address.update",
  509.      *     requirements={"address" = "\d+"}
  510.      * )
  511.      *
  512.      * @param Request        $request
  513.      * @param CompanyAddress $address
  514.      *
  515.      * @return Response
  516.      *
  517.      * @throws AccessDeniedException
  518.      */
  519.     public function updateAddressAction(Request $requestCompanyAddress $address): Response
  520.     {
  521.         $this->companyService->assertRead($address->getCompany());
  522.         $form $this->createForm(
  523.             CompanyAddressType::class,
  524.             $address,
  525.             [
  526.                 'action' => $this->generateUrl(
  527.                     'app.company.address.update',
  528.                     [
  529.                         'address' => $address->getId(),
  530.                     ]
  531.                 ),
  532.                 'disabled' => ! $this->companyService->canUpdate($address->getCompany()),
  533.             ]
  534.         );
  535.         $form->handleRequest($request);
  536.         if ($form->isSubmitted()) {
  537.             if ($form->isValid()) {
  538.                 try {
  539.                     $this->companyService->updateAddress($address);
  540.                     $this->flashSuccess('ui.success.update', [], 'CompanyAddress');
  541.                     return $this->redirectResponse(
  542.                         'app.company.index',
  543.                         [],
  544.                         'company-' $address->getCompany()->getId()
  545.                     );
  546.                 } catch (Exception $e) {
  547.                     $this->flashError('ui.error.update', [], 'CompanyAddress');
  548.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  549.                         $this->flashError($e->getMessage());
  550.                     }
  551.                 }
  552.             } else {
  553.                 $this->flashError('ui.error.validate', [], 'Company');
  554.             }
  555.         }
  556.         return $this->render(
  557.             'CompanyAddress/update.html.twig',
  558.             [
  559.                 'type'    => $address->getType(),
  560.                 'company' => $address->getCompany(),
  561.                 'form'    => $form->createView(),
  562.             ]
  563.         );
  564.     }
  565.     /**
  566.      * @Route(
  567.      *     "/customers/{customer}/update",
  568.      *     name="app.company.customer.update",
  569.      *     requirements={"customer" = "\d+"}
  570.      * )
  571.      *
  572.      * @param Request            $request
  573.      * @param CompanyEndCustomer $customer
  574.      *
  575.      * @return Response
  576.      *
  577.      * @throws AccessDeniedException
  578.      */
  579.     public function updateCustomerAction(Request $requestCompanyEndCustomer $customer): Response
  580.     {
  581.         $this->companyService->assertRead($customer->getCompany());
  582.         $form $this->createForm(
  583.             CompanyEndCustomerType::class,
  584.             $customer,
  585.             [
  586.                 'action' => $this->generateUrl(
  587.                     'app.company.customer.update',
  588.                     [
  589.                         'customer' => $customer->getId(),
  590.                     ]
  591.                 ),
  592.                 'disabled' => !$this->companyService->canUpdate($customer->getCompany()),
  593.             ]
  594.         );
  595.         $form->handleRequest($request);
  596.         if ($form->isSubmitted()) {
  597.             if ($form->isValid()) {
  598.                 try {
  599.                     $this->companyService->updateCustomer($customer);
  600.                     $this->flashSuccess('ui.success.update', [], 'EndCustomer');
  601.                     return $this->redirectResponse(
  602.                         'app.company.index',
  603.                         [],
  604.                         'company-' $customer->getCompany()->getId()
  605.                     );
  606.                 } catch (Exception $e) {
  607.                     $this->flashError('ui.error.update', [], 'EndCustomer');
  608.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  609.                         $this->flashError($e->getMessage());
  610.                     }
  611.                 }
  612.             } else {
  613.                 $this->flashError('ui.error.validate', [], 'Company');
  614.             }
  615.         }
  616.         return $this->render(
  617.             'CompanyCustomer/update.html.twig',
  618.             [
  619.                 'company' => $customer->getCompany(),
  620.                 'form'    => $form->createView(),
  621.             ]
  622.         );
  623.     }
  624. }