<?php
namespace App\Controller;
use App\Entity\Supplier;
use App\Exception\AccessDeniedException;
use App\Form\Type\SupplierType;
use App\Repository\PurchaseOrderRepository;
use App\Service\SupplierService;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(
* "/suppliers"
* )
*/
class SupplierController extends AbstractController
{
/**
* @var SupplierService
*/
protected SupplierService $supplierService;
/**
* @param SupplierService $supplierService
*/
public function __construct(SupplierService $supplierService) {
$this->supplierService = $supplierService;
}
/**
* @Route(
* "/create",
* name="app.supplier.create"
* )
*
* @param Request $request
*
* @return Response
*
* @throws AccessDeniedException
*/
public function createAction(Request $request): Response
{
$supplier = new Supplier('');
$this->supplierService->assertCreate($supplier);
$form = $this->createForm(
SupplierType::class,
$supplier,
[
'action' => $this->generateUrl(
'app.supplier.create'
),
]
);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
$this->supplierService->create($supplier);
$this->flashSuccess('ui.success.create', [], 'Supplier');
return $this->redirectResponse('app.supplier.index');
} catch (Exception $e) {
$this->flashError('ui.error.create', [], 'Supplier');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
}
}
return $this->render(
'Supplier/create.html.twig',
[
'form' => $form->createView(),
'supplier' => new Supplier(''),
]
);
}
/**
* @Route(
* "/{supplier}/delete",
* name="app.supplier.delete",
* requirements={"supplier" = "\d+"}
* )
*
* @param Supplier $supplier
*
* @return RedirectResponse
*
* @throws AccessDeniedException
*/
public function deleteAction(Supplier $supplier): RedirectResponse
{
$this->supplierService->assertDelete($supplier);
try {
$this->supplierService->delete($supplier);
$this->flashSuccess('ui.success.delete', [], 'Supplier');
} catch (Exception $e) {
$this->flashError('ui.error.delete', [], 'Supplier');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
return $this->redirectResponse('app.supplier.index');
}
/**
* @Route(
* "/",
* name="app.supplier.index"
* )
*
* @return Response
*/
public function indexAction(): Response
{
if (null === $this->supplierService->getInventory()) {
$this->flashWarning('ui.error.no_inventory_address', [], 'Supplier');
}
return $this->render(
'Supplier/index.html.twig',
[
'suppliers' => $this->supplierService->getAll(),
'newSupplier' => new Supplier(''),
]
);
}
/**
* @Route(
* "/inventory-address",
* name="app.rest.supplier.inventory_address"
* )
*
* @return JsonResponse
*/
public function restGetInventoryAddressAction(): JsonResponse
{
$data = [
'success' => true,
'address' => null,
'error' => null,
];
try {
if (null === $inventorySupplier = $this->supplierService->getInventory()) {
throw new Exception(
$this->trans('ui.error.no_inventory_address', [], 'Supplier')
);
}
$data['address'] = $inventorySupplier->serializeAddress();
} catch (Exception $e) {
$data['success'] = false;
$data['error'] = $e->getMessage();
}
return new JsonResponse($data);
}
/**
* @Route(
* "/{supplier}/active",
* name="app.rest.supplier.set_active",
* requirements={"supplier" = "\d+"}
* )
*
* @param Supplier $supplier
*
* @return JsonResponse
*/
public function restSetSupplierActive(Supplier $supplier): JsonResponse
{
$data = [
'success' => true,
'error' => null,
];
try {
$this->supplierService->assertUpdate($supplier);
$supplier->setActive($_POST['active']);
$this->supplierService->update($supplier);
} catch (Exception $e) {
$data['success'] = false;
$data['error'] = $this->trans('ui.error.update', [], 'Supplier');
if ($this->getCurrentUser()->isSuperAdmin()) {
$data['error'] .= ' ' . $e->getMessage();
}
}
return new JsonResponse($data);
}
/**
* @Route(
* "/{supplier}/update",
* name="app.supplier.update",
* requirements={"supplier" = "\d+"}
* )
*
* @param Request $request
* @param Supplier $supplier
*
* @return Response
*
* @throws AccessDeniedException
*/
public function updateAction(Request $request, Supplier $supplier): Response
{
$this->supplierService->assertUpdate($supplier);
$form = $this->createForm(
SupplierType::class,
$supplier,
[
'action' => $this->generateUrl(
'app.supplier.update',
['supplier' => $supplier->getId()]
),
'disabled' => ! $this->supplierService->canUpdate($supplier),
]
);
$wasInventoryTransactions = $supplier->isInventoryTransactions();
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
if ($wasInventoryTransactions && !$supplier->isInventoryTransactions() && count($supplier->getPurchaseOrders()) > 0) {
$this->flashWarning('ui.warning.remove_inventory_transactions', [], 'Supplier');
$supplier->setInventoryTransactions(true);
}
$this->supplierService->update($supplier);
$this->flashSuccess('ui.success.update', [], 'Supplier');
return $this->redirectResponse('app.supplier.index');
} catch (Exception $e) {
$this->flashError('ui.error.update', [], 'Supplier');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
}
}
return $this->render(
'Supplier/update.html.twig',
[
'supplier' => $supplier,
'form' => $form->createView(),
]
);
}
}