src/Controller/Front/CartController.php line 69

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front;
  4. use App\Application\Service\Cart\ProductCartFulfillmentService;
  5. use App\Application\Service\Helper\LegacyTranslator;
  6. use App\Application\Service\Order\OrderReferenceValidationService;
  7. use App\Application\Service\Serializer\SerializerService;
  8. use App\Application\Service\Session\SessionService;
  9. use App\Application\Service\Validator\AddressValidator;
  10. use App\Entity\System\Customer;
  11. use App\Factory\Address\AddressFactory;
  12. use App\Manager\System\AddressManager;
  13. use App\Manager\System\CartManager;
  14. use App\Model\Order\OrderShippingAddressRequest;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. class CartController extends AbstractController
  23. {
  24. public function __construct(
  25. private readonly CartManager $cartManager,
  26. private readonly TranslatorInterface $translatorService,
  27. private readonly SessionService $sessionService,
  28. private readonly AddressFactory $addressFactory,
  29. private readonly AddressManager $addressManager,
  30. private readonly AddressValidator $addressValidator,
  31. private readonly OrderReferenceValidationService $orderReferenceValidationService,
  32. private readonly LegacyTranslator $legacyTranslator,
  33. private readonly SerializerService $serializerService,
  34. ) {
  35. }
  36. /**
  37. * @param Request $request
  38. * @param ProductCartFulfillmentService $productCartFulfillmentService
  39. *
  40. * @return JsonResponse
  41. *
  42. * @Route("cart/delete-product-fba-not-allowed", name="cart_delete_product_fba_not_allowed", priority="5", methods={"GET"})
  43. */
  44. public function deleteProductFbaNotAllowed(
  45. Request $request,
  46. ProductCartFulfillmentService $productCartFulfillmentService,
  47. ): JsonResponse {
  48. try {
  49. $productsFbaNotAllowed = json_decode($request->get('items'), true);
  50. $productCartFulfillmentService->deleteProductsWithoutFulfillment($productsFbaNotAllowed);
  51. return new JsonResponse(true);
  52. } catch (\Exception $exception) {
  53. return new JsonResponse(false);
  54. }
  55. }
  56. /**
  57. * @param Request $request
  58. *
  59. * @return JsonResponse
  60. *
  61. * @Route("cart/add-shipment-choices", name="cart_add_shipment_choices", methods={"POST"})
  62. */
  63. public function addShipmentChoices(Request $request): JsonResponse
  64. {
  65. if (!$this->sessionService->get('id_cart')) {
  66. return $this->json($this->translatorService->trans('global.error_page_not_found.message.title'), JsonResponse::HTTP_NOT_FOUND);
  67. }
  68. $cartId = $this->sessionService->get('id_cart');
  69. $cart = $this->cartManager->findOneById((int)$cartId);
  70. if (!$cart) {
  71. return $this->json($this->translatorService->trans('global.error_page_not_found.message.title'), JsonResponse::HTTP_NOT_FOUND);
  72. }
  73. $cart->setShipmentChoices($request->get('shipmentChoices'));
  74. $this->cartManager->save($cart);
  75. return new JsonResponse(true);
  76. }
  77. /**
  78. * @param Request $request
  79. *
  80. * @return JsonResponse
  81. *
  82. * @Route("cart/add-delivery-address", name="cart_add_delivery_address", methods={"POST"})
  83. *
  84. * @throws \Exception
  85. */
  86. public function addDeliveryAddress(Request $request): JsonResponse
  87. {
  88. if (!$this->sessionService->get('id_cart')) {
  89. return $this->json($this->translatorService->trans('global.error_page_not_found.message.title'), JsonResponse::HTTP_NOT_FOUND);
  90. }
  91. $cartId = $this->sessionService->get('id_cart');
  92. $cart = $this->cartManager->findOneById((int)$cartId);
  93. if (!$cart) {
  94. return new JsonResponse($this->translatorService->trans('global.error_page_not_found.message.title'), Response::HTTP_NOT_FOUND);
  95. }
  96. try {
  97. $deliveryAddressRequest = $this->serializerService->deserialize(\json_encode($request->request->all(), JSON_THROW_ON_ERROR), OrderShippingAddressRequest::class, JsonEncoder::FORMAT);
  98. } catch (\Throwable $exception) {
  99. return new JsonResponse('invalid format request', Response::HTTP_BAD_REQUEST);
  100. }
  101. if ($deliveryAddressRequest->phone === null
  102. || !$this->addressValidator->isValidPhoneNumber($deliveryAddressRequest->phone)
  103. || !$this->addressValidator->isPhoneLengthValid($deliveryAddressRequest->phone)) {
  104. return new JsonResponse('phone_invalid_length', Response::HTTP_BAD_REQUEST);
  105. }
  106. if (!$this->addressValidator->isValidName($deliveryAddressRequest->name)) {
  107. return new JsonResponse('name_invalid_format', Response::HTTP_BAD_REQUEST);
  108. }
  109. if (!$this->addressValidator->isValidAddress($deliveryAddressRequest->address1)) {
  110. return new JsonResponse('address_invalid_format', Response::HTTP_BAD_REQUEST);
  111. }
  112. try {
  113. $deliveryAddress = $this->addressFactory->createDeliveryAddress(
  114. $deliveryAddressRequest,
  115. $cart
  116. );
  117. } catch (\Exception $exception) {
  118. return new JsonResponse('country_invalid_format', Response::HTTP_BAD_REQUEST);
  119. }
  120. $this->addressManager->save($deliveryAddress);
  121. $cart->setAddressDelivery($deliveryAddress);
  122. $this->cartManager->save($cart);
  123. return new JsonResponse(true);
  124. }
  125. /**
  126. * @Route("order/validateRefOrderSupplier", name="validateRefOrderSupplier")
  127. * @Route("cart/validate-order-reference", name="validate_order_reference", methods={"POST"})
  128. */
  129. public function validateOrderReference(Request $request): Response
  130. {
  131. $customerOrderReference = $request->get('ref_interna');
  132. /** @var ?Customer $user */
  133. $user = $this->getUser();
  134. if (!$user || !$customerOrderReference) {
  135. return new JsonResponse(['error' => false]);
  136. }
  137. if (!$this->orderReferenceValidationService->isReferenceAvailable((string)$user->getId(), $customerOrderReference)) {
  138. return new Response(
  139. \json_encode(
  140. [
  141. 'error' => true,
  142. 'msg' => $this->legacyTranslator->translate(
  143. 'La referencia interna o identificador del pedido ya existe',
  144. 'orderController'
  145. ),
  146. ]
  147. )
  148. );
  149. }
  150. if ($cartId = $this->sessionService->get('id_cart')) {
  151. $cart = $this->cartManager->findOneById($cartId);
  152. $cart->setRefOrderSupplier($customerOrderReference);
  153. $this->cartManager->save($cart);
  154. }
  155. return new Response(json_encode(['error' => false]));
  156. }
  157. }