src/Controller/SalesJournalController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BankAccount;
  4. use App\Entity\Company;
  5. use App\Entity\PotentialCustomerCompany;
  6. use App\Exception\AccessDeniedException;
  7. use App\Form\Type\CompanyType;
  8. use App\Form\Type\PotentialCustomerCompanyType;
  9. use App\Service\CompanyService;
  10. use App\Service\PotentialCustomerCompanyService;
  11. use Exception;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Route(
  18.  *     "/sales-journals"
  19.  * )
  20.  */
  21. class SalesJournalController extends AbstractController
  22. {
  23.     /**
  24.      * @var PotentialCustomerCompanyService
  25.      */
  26.     protected PotentialCustomerCompanyService $companyService;
  27.     /**
  28.      * @var CompanyService
  29.      */
  30.     protected CompanyService $varService;
  31.     /**
  32.      * @param PotentialCustomerCompanyService $companyService
  33.      * @param CompanyService                  $varService
  34.      */
  35.     public function __construct(
  36.         PotentialCustomerCompanyService $companyService,
  37.         CompanyService $varService
  38.     ) {
  39.         $this->companyService $companyService;
  40.         $this->varService $varService;
  41.     }
  42.     /**
  43.      * @Route(
  44.      *     "/create",
  45.      *     name="app.sales_journal.create"
  46.      * )
  47.      *
  48.      * @param Request $request
  49.      *
  50.      * @return Response
  51.      *
  52.      * @throws AccessDeniedException
  53.      */
  54.     public function createAction(Request $request): Response
  55.     {
  56.         $company = new PotentialCustomerCompany();
  57.         $form $this->createForm(
  58.             PotentialCustomerCompanyType::class,
  59.             $company,
  60.             [
  61.                 'action' => $this->generateUrl(
  62.                     'app.sales_journal.create'
  63.                 ),
  64.             ]
  65.         );
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted()) {
  68.             if ($form->isValid()) {
  69.                 try {
  70.                     $company $form->getData();
  71.                     $this->companyService->create($company);
  72.                     $this->flashSuccess('ui.success.update', [], 'PotentialCustomerCompany');
  73.                     return $this->redirectResponse(
  74.                         'app.sales_journal.index',
  75.                         [],
  76.                         'company-' $company->getId()
  77.                     );
  78.                 } catch (Exception $e) {
  79.                     $this->flashError('ui.error.update', [], 'PotentialCustomerCompany');
  80.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  81.                         $this->flashError($e->getMessage());
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         return $this->render(
  87.             'SalesJournal/create.html.twig',
  88.             [
  89.                 'form' => $form->createView(),
  90.             ]
  91.         );
  92.     }
  93.     /**
  94.      * @Route(
  95.      *     "/{company}/create-var",
  96.      *     name="app.sales_journal.create_var",
  97.      *     requirements={"company" = "\d+"}
  98.      * )
  99.      *
  100.      * @param Request                  $request
  101.      * @param PotentialCustomerCompany $company
  102.      *
  103.      * @return Response
  104.      *
  105.      * @throws AccessDeniedException
  106.      */
  107.     public function createVarAction(Request $requestPotentialCustomerCompany $company): Response
  108.     {
  109.         $var = new Company($company->getName());
  110.         $this->varService->assertCreate($var);
  111.         $form $this->createForm(
  112.             CompanyType::class,
  113.             $var,
  114.             [
  115.                 'action' => $this->generateUrl(
  116.                     'app.sales_journal.create_var',
  117.                     [
  118.                         'company' => $company->getId(),
  119.                     ]
  120.                 ),
  121.             ]
  122.         );
  123.         $form->handleRequest($request);
  124.         if ($form->isSubmitted()) {
  125.             if ($form->isValid()) {
  126.                 try {
  127.                     $company->setCompany($var);
  128.                     $this->companyService->update($company);
  129.                     $this->flashSuccess('ui.success.update', [], 'Company');
  130.                     return $this->redirectResponse(
  131.                         'app.sales_journal.index',
  132.                         [],
  133.                         'company-' $company->getId()
  134.                     );
  135.                 } catch (Exception $e) {
  136.                     $this->flashError('ui.error.update', [], 'Company');
  137.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  138.                         $this->flashError($e->getMessage());
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.         return $this->render(
  144.             'SalesJournal/createVar.html.twig',
  145.             [
  146.                 'form'    => $form->createView(),
  147.                 'company' => $company,
  148.             ]
  149.         );
  150.     }
  151.     /**
  152.      * @Route(
  153.      *     "/{company}/delete",
  154.      *     name="app.sales_journal.delete",
  155.      *     requirements={"company" = "\d+"}
  156.      * )
  157.      *
  158.      * @param PotentialCustomerCompany $company
  159.      *
  160.      * @return RedirectResponse
  161.      *
  162.      * @throws AccessDeniedException
  163.      */
  164.     public function deleteAction(PotentialCustomerCompany $company): RedirectResponse
  165.     {
  166.         try {
  167.             $this->companyService->delete($company);
  168.             $this->flashSuccess('ui.success.delete', [], 'PotentialCustomerCompany');
  169.         } catch (Exception $e) {
  170.             $this->flashError('ui.error.delete', [], 'PotentialCustomerCompany');
  171.             if ($this->getCurrentUser()->isSuperAdmin()) {
  172.                 $this->flashError($e->getMessage());
  173.             }
  174.         }
  175.         return $this->redirectResponse('app.sales_journal.index');
  176.     }
  177.     /**
  178.      * @Route(
  179.      *     "/",
  180.      *     name="app.sales_journal.index"
  181.      * )
  182.      *
  183.      * @return Response
  184.      */
  185.     public function indexAction(): Response
  186.     {
  187.         return $this->render(
  188.             'SalesJournal/index.html.twig',
  189.             [
  190.                 'companies'  => $this->companyService->getAll(),
  191.                 'newCompany' => new PotentialCustomerCompany(),
  192.                 'newVar'     => new Company(''),
  193.             ]
  194.         );
  195.     }
  196.     /**
  197.      * @Route(
  198.      *     "/{company}/print",
  199.      *     name="app.sales_journal.print",
  200.      *     requirements={"company" = "\d+"}
  201.      * )
  202.      *
  203.      * @param PotentialCustomerCompany $company
  204.      *
  205.      * @return Response
  206.      */
  207.     public function printAction(PotentialCustomerCompany $company): Response
  208.     {
  209.         return $this->render(
  210.             'SalesJournal/print.html.twig',
  211.             [
  212.                 'company' => $company,
  213.             ]
  214.         );
  215.     }
  216.     /**
  217.      * @Route(
  218.      *     "/print",
  219.      *     name="app.sales_journal.print_all"
  220.      * )
  221.      *
  222.      * @return Response
  223.      */
  224.     public function printAllAction(): Response
  225.     {
  226.         return $this->render(
  227.             'SalesJournal/printAll.html.twig',
  228.             [
  229.                 'companies' => $this->companyService->getAll(),
  230.             ]
  231.         );
  232.     }
  233.     /**
  234.      * @Route(
  235.      *     "/{company}/update",
  236.      *     name="app.sales_journal.update",
  237.      *     requirements={"company" = "\d+"}
  238.      * )
  239.      *
  240.      * @param Request                  $request
  241.      * @param PotentialCustomerCompany $company
  242.      *
  243.      * @return Response
  244.      *
  245.      * @throws AccessDeniedException
  246.      */
  247.     public function updateAction(Request $requestPotentialCustomerCompany $company): Response
  248.     {
  249.         $form $this->createForm(
  250.             PotentialCustomerCompanyType::class,
  251.             $company,
  252.             [
  253.                 'action' => $this->generateUrl(
  254.                     'app.sales_journal.update',
  255.                     [
  256.                         'company' => $company->getId(),
  257.                     ]
  258.                 ),
  259.             ]
  260.         );
  261.         $form->handleRequest($request);
  262.         if ($form->isSubmitted()) {
  263.             if ($form->isValid()) {
  264.                 try {
  265.                     $this->companyService->update($company);
  266.                     $this->flashSuccess('ui.success.update', [], 'PotentialCustomerCompany');
  267.                     return $this->redirectResponse(
  268.                         'app.sales_journal.index',
  269.                         [],
  270.                         'company-' $company->getId()
  271.                     );
  272.                 } catch (Exception $e) {
  273.                     $this->flashError('ui.error.update', [], 'PotentialCustomerCompany');
  274.                     if ($this->getCurrentUser()->isSuperAdmin()) {
  275.                         $this->flashError($e->getMessage());
  276.                     }
  277.                 }
  278.             }
  279.         }
  280.         return $this->render(
  281.             'SalesJournal/update.html.twig',
  282.             [
  283.                 'company' => $company,
  284.                 'form'    => $form->createView(),
  285.             ]
  286.         );
  287.     }
  288. }