<?php
namespace App\Controller;
use App\Entity\BankAccount;
use App\Entity\Company;
use App\Entity\CompanyAddress;
use App\Entity\CompanyConsigneeAddress;
use App\Entity\CompanyEndCustomer;
use App\Entity\CompanyDeliveryAddress;
use App\Entity\CompanyInvoiceAddress;
use App\Exception\AccessDeniedException;
use App\Form\Type\CompanyAddressType;
use App\Form\Type\CompanyEndCustomerType;
use App\Form\Type\CompanyType;
use App\Service\CompanyService;
use Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(
* "/companies"
* )
*/
class CompanyController extends AbstractController
{
/**
* @var CompanyService
*/
protected CompanyService $companyService;
/**
* @param CompanyService $companyService
*/
public function __construct(CompanyService $companyService)
{
$this->companyService = $companyService;
}
/**
* @Route(
* "/create",
* name="app.company.create"
* )
*
* @param Request $request
*
* @return Response
*
* @throws AccessDeniedException
*/
public function createAction(Request $request): Response
{
$this->companyService->assertCreate(new Company(''));
$form = $this->createForm(
CompanyType::class,
null,
[
'action' => $this->generateUrl(
'app.company.create'
),
]
);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
/* @var Company $company */
$company = $form->getData();
$this->companyService->create($company);
$this->flashSuccess('ui.success.update', [], 'Company');
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $company->getId()
);
} catch (Exception $e) {
$this->flashError('ui.error.update', [], 'Company');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
}
}
return $this->render(
'Company/create.html.twig',
[
'form' => $form->createView(),
]
);
}
/**
* @param Request $request
* @param CompanyAddress $address
*
* @return Response
*/
private function createAddress(Request $request, CompanyAddress $address): Response
{
$formAction = $this->generateUrl(
'app.company.address.' . $address->getType() . '.create',
[
'company' => $address->getCompany()->getId(),
]
);
$form = $this->createForm(
CompanyAddressType::class,
$address,
[
'action' => $formAction,
]
);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
$this->companyService->createAddress($address);
$this->flashSuccess('ui.success.create', [], 'CompanyAddress');
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $address->getCompany()->getId()
);
} catch (Exception $e) {
$this->flashError('ui.error.create', [], 'CompanyAddress');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
}
}
return $this->render(
'CompanyAddress/create.html.twig',
[
'type' => $address->getType(),
'company' => $address->getCompany(),
'form' => $form->createView(),
]
);
}
/**
* @Route(
* "/{company}/addresses/consignee/create",
* name="app.company.address.consignee.create",
* requirements={"company" = "\d+"}
* )
*
* @param Request $request
* @param Company $company
*
* @return Response
*/
public function createConsigneeAddressAction(Request $request, Company $company): Response
{
return $this->createAddress(
$request,
new CompanyConsigneeAddress($company)
);
}
/**
* @Route(
* "/{company}/customers/create",
* name="app.company.customer.create",
* requirements={"company" = "\d+"}
* )
*
* @param Request $request
* @param Company $company
*
* @return Response
*/
public function createCustomerAction(Request $request, Company $company): Response
{
$form = $this->createForm(
CompanyEndCustomerType::class,
null,
[
'action' => $this->generateUrl(
'app.company.customer.create',
[
'company' => $company->getId(),
]
),
'company' => $company,
]
);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
/* @var CompanyEndCustomer $customer */
$customer = $form->getData();
$this->companyService->createCustomer($customer);
$this->flashSuccess('ui.success.update', [], 'EndCustomer');
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $company->getId()
);
} catch (Exception $e) {
$this->flashError('ui.error.update', [], 'EndCustomer');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
}
}
return $this->render(
'CompanyCustomer/create.html.twig',
[
'company' => $company,
'form' => $form->createView(),
]
);
}
/**
* @Route(
* "/{company}/addresses/delivery/create",
* name="app.company.address.delivery.create",
* requirements={"company" = "\d+"}
* )
*
* @param Request $request
* @param Company $company
*
* @return Response
*/
public function createDeliveryAddressAction(Request $request, Company $company): Response
{
return $this->createAddress(
$request,
new CompanyDeliveryAddress($company)
);
}
/**
* @Route(
* "/{company}/addresses/invoice/create",
* name="app.company.address.invoice.create",
* requirements={"company" = "\d+"}
* )
*
* @param Request $request
* @param Company $company
*
* @return Response
*/
public function createInvoiceAddressAction(Request $request, Company $company): Response
{
return $this->createAddress(
$request,
new CompanyInvoiceAddress($company)
);
}
/**
* @Route(
* "/{company}/delete",
* name="app.company.delete",
* requirements={"company" = "\d+"}
* )
*
* @param Company $company
*
* @return RedirectResponse
*
* @throws AccessDeniedException
*/
public function deleteAction(Company $company): RedirectResponse
{
$this->companyService->assertDelete($company);
try {
$this->companyService->delete($company);
$this->flashSuccess('ui.success.delete', [], 'Company');
} catch (Exception $e) {
$this->flashError('ui.error.delete', [], 'Company');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
return $this->redirectResponse('app.company.index');
}
/**
* @Route(
* "/addresses/{address}/delete",
* name="app.company.address.delete",
* requirements={"address" = "\d+"}
* )
*
* @param CompanyAddress $address
*
* @return RedirectResponse
*
* @throws AccessDeniedException
*/
public function deleteAddressAction(CompanyAddress $address): RedirectResponse
{
$this->companyService->assertDelete($address->getCompany());
$company = $address->getCompany();
try {
$this->companyService->deleteAddress($address);
$this->flashSuccess('ui.success.delete', [], 'CompanyAddress');
} catch (Exception $e) {
$this->flashError('ui.error.delete', [], 'CompanyAddress');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $company->getId()
);
}
/**
* @Route(
* "/customers/{customer}/delete",
* name="app.company.customer.delete",
* requirements={"customer" = "\d+"}
* )
*
* @param CompanyEndCustomer $customer
*
* @return RedirectResponse
*
* @throws AccessDeniedException
*/
public function deleteEndCustomerAction(CompanyEndCustomer $customer): RedirectResponse
{
$this->companyService->assertDelete($customer->getCompany());
$company = $customer->getCompany();
try {
$this->companyService->deleteCustomer($customer);
$this->flashSuccess('ui.success.delete', [], 'EndCustomer');
} catch (Exception $e) {
$this->flashError('ui.error.delete', [], 'EndCustomer');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $company->getId()
);
}
/**
* @Route(
* "/",
* name="app.company.index"
* )
*
* @return Response
*/
public function indexAction(): Response
{
return $this->render(
'Company/index.html.twig',
[
'companies' => $this->companyService->getAll(),
'newCompany' => new Company(''),
]
);
}
/**
* @Route(
* "/rest/addresses/{address}/delete",
* name="app.rest.company.address.delete",
* requirements={"address" = "\d+"},
* methods={"DELETE"}
* )
*
* @param CompanyAddress $address
*
* @return JsonResponse
*/
public function restDeleteAddressAction(CompanyAddress $address): JsonResponse
{
$status = 200;
$data = [
'id' => $address->getId(),
'message' => null,
];
try {
$this->companyService->deleteAddress($address);
$data['message'] = $this->trans('ui.success.delete', [], 'EndCustomer');
} catch (Exception $e) {
$status = 500;
$data['message'] = $this->trans('ui.error.delete', [], 'EndCustomer');
if ($this->getCurrentUser()->isSuperAdmin()) {
$data['message'] .= "\n" . $e->getMessage();
}
}
return new JsonResponse($data, $status);
}
/**
* @Route(
* "/rest/customers/{customer}/delete",
* name="app.rest.company.customer.delete",
* requirements={"customer" = "\d+"},
* methods={"DELETE"}
* )
*
* @param CompanyEndCustomer $customer
*
* @return JsonResponse
*/
public function restDeleteEndCustomerAction(CompanyEndCustomer $customer): JsonResponse
{
$status = 200;
$data = [
'id' => $customer->getId(),
'message' => null,
];
try {
$this->companyService->deleteCustomer($customer);
$data['message'] = $this->trans('ui.success.delete', [], 'EndCustomer');
} catch (Exception $e) {
$status = 500;
$data['message'] = $this->trans('ui.error.delete', [], 'EndCustomer');
if ($this->getCurrentUser()->isSuperAdmin()) {
$data['message'] .= "\n" . $e->getMessage();
}
}
return new JsonResponse($data, $status);
}
/**
* @Route(
* "/{company}/address",
* name="app.rest.company.address",
* requirements={"company" = "\d+"}
* )
*
* @param Company $company
*
* @return JsonResponse
*/
public function restGetAddressAction(Company $company): JsonResponse
{
$data = [
'success' => true,
'address' => null,
];
try {
$data['address'] = $company->serializeAddress();
} catch (Exception $e) {
$data['success'] = false;
}
return new JsonResponse($data);
}
/**
* @Route(
* "/{company}/update",
* name="app.company.update",
* requirements={"company" = "\d+"}
* )
*
* @param Request $request
* @param Company $company
*
* @return Response
*
* @throws AccessDeniedException
*/
public function updateAction(Request $request, Company $company): Response
{
$this->companyService->assertRead($company);
$form = $this->createForm(
CompanyType::class,
$company,
[
'action' => $this->generateUrl(
'app.company.update',
['company' => $company->getId()]
),
'disabled' => ! $this->companyService->canUpdate($company),
]
);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
$this->companyService->update($company);
$this->flashSuccess('ui.success.update', [], 'Company');
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $company->getId()
);
} catch (Exception $e) {
$this->flashError('ui.error.update', [], 'Company');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
} else {
$this->flashError('ui.error.validate', [], 'Company');
}
}
return $this->render(
'Company/update.html.twig',
[
'company' => $company,
'form' => $form->createView(),
]
);
}
/**
* @Route(
* "/addresses/{address}/update",
* name="app.company.address.update",
* requirements={"address" = "\d+"}
* )
*
* @param Request $request
* @param CompanyAddress $address
*
* @return Response
*
* @throws AccessDeniedException
*/
public function updateAddressAction(Request $request, CompanyAddress $address): Response
{
$this->companyService->assertRead($address->getCompany());
$form = $this->createForm(
CompanyAddressType::class,
$address,
[
'action' => $this->generateUrl(
'app.company.address.update',
[
'address' => $address->getId(),
]
),
'disabled' => ! $this->companyService->canUpdate($address->getCompany()),
]
);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
$this->companyService->updateAddress($address);
$this->flashSuccess('ui.success.update', [], 'CompanyAddress');
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $address->getCompany()->getId()
);
} catch (Exception $e) {
$this->flashError('ui.error.update', [], 'CompanyAddress');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
} else {
$this->flashError('ui.error.validate', [], 'Company');
}
}
return $this->render(
'CompanyAddress/update.html.twig',
[
'type' => $address->getType(),
'company' => $address->getCompany(),
'form' => $form->createView(),
]
);
}
/**
* @Route(
* "/customers/{customer}/update",
* name="app.company.customer.update",
* requirements={"customer" = "\d+"}
* )
*
* @param Request $request
* @param CompanyEndCustomer $customer
*
* @return Response
*
* @throws AccessDeniedException
*/
public function updateCustomerAction(Request $request, CompanyEndCustomer $customer): Response
{
$this->companyService->assertRead($customer->getCompany());
$form = $this->createForm(
CompanyEndCustomerType::class,
$customer,
[
'action' => $this->generateUrl(
'app.company.customer.update',
[
'customer' => $customer->getId(),
]
),
'disabled' => !$this->companyService->canUpdate($customer->getCompany()),
]
);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
$this->companyService->updateCustomer($customer);
$this->flashSuccess('ui.success.update', [], 'EndCustomer');
return $this->redirectResponse(
'app.company.index',
[],
'company-' . $customer->getCompany()->getId()
);
} catch (Exception $e) {
$this->flashError('ui.error.update', [], 'EndCustomer');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
} else {
$this->flashError('ui.error.validate', [], 'Company');
}
}
return $this->render(
'CompanyCustomer/update.html.twig',
[
'company' => $customer->getCompany(),
'form' => $form->createView(),
]
);
}
}