src/Controller/Customer/AddressController.php line 541

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Customer;
  3. use App\Controller\BaseController;
  4. use App\Entity\Addressing\Address;
  5. use App\Entity\Addressing\City;
  6. use App\Entity\Addressing\Country;
  7. use App\Entity\Addressing\County;
  8. use App\Entity\Institution\ProductType;
  9. use App\Model\AddressModel;
  10. use App\Model\CountryModel;
  11. use App\Model\CustomerModel;
  12. use App\Model\OrderModel;
  13. use App\Model\ShopUserModel;
  14. use App\Service\AddressService;
  15. use App\Service\CustomerService;
  16. use App\Service\InvoiceApiService;
  17. use App\Validator\AddressFormValidator;
  18. use Psr\Cache\InvalidArgumentException;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use function Sentry\captureException;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  24. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  25. use Symfony\Component\Serializer\Serializer;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. class AddressController extends BaseController
  28. {
  29.     public function retrieveAddressData(Request $request)
  30.     {
  31.         $addressId $request->get('addressId');
  32.         $isShippingAddress false;
  33.         if ($request->get('isShippingAddress') && $request->get('isShippingAddress') == true) {
  34.             $isShippingAddress true;
  35.         }
  36.         $address null;
  37.         $user $this->getUser();
  38.         $isValid false;
  39.         $city null;
  40.         $country null;
  41.         $county null;
  42.         $customerType null;
  43.         $customer null;
  44.         if ($user) {
  45.             if ($customer $user->getCustomer()) {
  46.                 $customerType $customer->getType();
  47.                 if ($customer && $addressId) {
  48.                     $address $this->getDoctrine()->getRepository(Address::class)->find($addressId);
  49.                     if ($address) {
  50.                         $customerType $address->getCustomerType();
  51.                         if ($address->getCustomer()
  52.                             && $address->getCustomer()->getId() == $customer->getId()) {
  53.                             $isValid true;
  54.                             if ($address->getCountryCode()) {
  55.                                 $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(array('code' => $address->getCountryCode()));
  56.                             }
  57.                             if ($address->getCounty()) {
  58.                                 if ($country) {
  59.                                     $county $this->getDoctrine()->getRepository(County::class)->findByCodeAndCountry($address->getCounty(), $country);
  60.                                 } else {
  61.                                     $county $this->getDoctrine()->getRepository(County::class)->findByCode($address->getCounty());
  62.                                 }
  63.                             }
  64.                             if ($address->getCity() && ctype_digit($address->getCity())) {
  65.                                 $city $this->getDoctrine()->getRepository(City::class)->find((int) $address->getCity());
  66.                             }
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.         $template json_encode('error');
  73.         try {
  74.             if (!$isValid) {
  75.                 $address null;
  76.             }
  77.             $country $country ?: $this->getDoctrine()->getRepository(Country::class)->findOneBy(['code' => $address $address->getCountryCode() : $request->getLocale()]);
  78.             $counties null;
  79.             if ($country) {
  80.                 $counties $this->getDoctrine()->getRepository(County::class)->findBy(['country' => $country->getId()]);
  81.             }
  82.             $template json_encode(array(
  83.                 '$isValid' => $isValid,
  84.                 'address' => $address $address->toArray() : null,
  85.                 'countries' => $this->getDoctrine()->getRepository(Country::class)->findAll(),
  86.                 'counties' => $counties $counties->toArray() : null,
  87.                 'selectedCountry' => $country,
  88.                 'selectedCity' => $city,
  89.                 'selectedCounty' => $county,
  90.                 'customerType' => $customerType,
  91.                 'customer' => $customer,
  92.             ));
  93.         } catch (\Throwable $exception) {
  94.             captureException($exception);
  95.         }
  96.         return new Response($template);
  97.     }
  98.     public function editViewAction(Request $request)
  99.     {
  100.         $render '@templates/Front/Shop/error404.html.twig';
  101.         $addressId $request->get('id');
  102.         $address $this->getDoctrine()->getRepository(Address::class)->find($addressId);
  103.         $data AddressService::getAddressDataForUserIfIsValid($this->getUser(), $address$this->getEntityManager());
  104.         if ($data) {
  105.             $render '@templates/Account/Address/edit-address.html.twig';
  106.         }
  107.         $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(array('code' => $address->getCountryCode()));
  108.         return $this->render($render, [
  109.             'data' => $data,
  110.             'countries' => $this->getDoctrine()->getRepository(Country::class)->findAll(),
  111.             'counties' => $this->getDoctrine()->getRepository(County::class)->findAllCountiesByCountry($country),
  112.         ]);
  113.     }
  114.     public function editSubmitAction(Request $request)
  115.     {
  116.         $session $request->getSession();
  117.         $errors json_encode(array('unknown' => true));
  118.         $addressId $request->get('id');
  119.         $data $request->request->all();
  120.         $data AddressModel::getDataForCustomerType($data);
  121.         $address $this->getDoctrine()->getRepository(Address::class)->find($addressId);
  122.         $user $this->getUser();
  123.         if ($user) {
  124.             $customer $user->getCustomer();
  125.             $customerAddress $address->getCustomer();
  126.             $isAdminUser ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getEntityManager());
  127.             if ($customer && $customerAddress
  128.                 && ($customer->getId() == $customerAddress->getId() || $isAdminUser)) {
  129.                 $addressFormValidator = new AddressFormValidator($this->getEntityManager(), $this->getSerializer());
  130.                 if ($addressFormValidator->checkForm($request)) {
  131.                     $fullAddress AddressModel::constructAddressDataArray($datafalse);
  132.                     $address AddressModel::updateAddress($address$fullAddress$this->getDoctrine()->getRepository(County::class));
  133.                     $this->getEntityManager()->persist($address);
  134.                     $this->getEntityManager()->flush();
  135.                     if ($isAdminUser) {
  136.                         $this->addFlash('address_edited''Adresa editata cu succes');
  137.                     }
  138.                     $errors json_encode(array('success' => true));
  139.                 } else {
  140.                     if ($session->has('addressErrors')) {
  141.                         $errors $session->get('addressErrors');
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.         return new Response($errors);
  147.     }
  148.     public function updateAddressVueAction(Request $request): Response
  149.     {
  150.         $data $request->request->all();
  151.         $addressId = (string) $data["selectedAddressId"];
  152.         $isForeignCustomer false;
  153.         if (isset($data["address"])) {
  154.             $isForeignCustomer $data["address"]["country"]["code"] != Country::RO_COUNTRY_CODE;
  155.         }
  156.         // Mapping array keys to desired keys
  157.         $mappedData = [
  158.             "customerType" => $data["customer_type"],
  159.             "customerCUI" => $data["customer_type"] == $data["cif_code"] : null,
  160.             "customerCompanyName" => $data["customer_type"] == $data["company_name"] : null,
  161.             "legalCustomerCountry" => $data["customer_type"] == $data["address"]["country"]["code"] : null,
  162.             "legalCustomerCounty" => $data["customer_type"] == && !$isForeignCustomer $data["address"]["county"]["id"] : null,
  163.             "legalCustomerCity" => $data["customer_type"] == ? !$isForeignCustomer ? (string) $data["address"]["city"]["id"] : $data["address"]["city_name"] : null,
  164.             "legalCustomerStreet" => $data["customer_type"] == $data["address"]["street"] : null,
  165.             "legalCustomerStreetNumber" => $data["customer_type"] == $data["address"]["street_number"] : null,
  166.             "customerName" => $data["customer_type"] == null $data["customer_name"],
  167.             "customerPhone" => $data["customer_phone"],
  168.             "customerCountry" => $data["customer_type"] == null $data["address"]["country"]["code"],
  169.             "customerCounty" => $data["customer_type"] == null $data["address"]["county"]["id"],
  170.             "customerCity" => $data["customer_type"] == null : (string)  $data["address"]["city"]["id"],
  171.             "customerStreet" => $data["customer_type"] == null $data["address"]["street"],
  172.             "customerStreetNumber" => $data["customer_type"] == null $data["address"]["street_number"],
  173.             "customerBuilding" => $data["customer_type"] == null $data["address"]["building"],
  174.             "customerEntrance" => $data["customer_type"] == null $data["address"]["entrance"],
  175.             "customerApartment" => $data["customer_type"] == null $data["address"]["apartment"]
  176.         ];
  177.         $errors json_encode(array('unknown' => true));
  178.         $data AddressModel::getDataForCustomerType($mappedData);
  179.         $address $this->getDoctrine()->getRepository(Address::class)->find($addressId);
  180.         $user $this->getUser();
  181.         if ($user) {
  182.             $customer $user->getCustomer();
  183.             $customerAddress $address->getCustomer();
  184.             if ($customer && $customerAddress
  185.                 && ($customer->getId() == $customerAddress->getId())) {
  186.                 $addressFormValidator = new AddressFormValidator($this->getEntityManager(), $this->getSerializer());
  187.                 if ($addressFormValidator->checkFormAddressVue($mappedData)) {
  188.                     $fullAddress AddressModel::constructAddressDataArray($datafalse);
  189.                     $address AddressModel::updateAddress($address$fullAddress$this->getDoctrine()->getRepository(County::class));
  190.                     $this->getEntityManager()->persist($address);
  191.                     $this->getEntityManager()->flush();
  192.                     $errors json_encode(array('success' => true));
  193.                 } else {
  194.                     $errors json_encode(array('success' => false));
  195.                 }
  196.             }
  197.         }
  198.         return new Response($errors);
  199.     }
  200.     public function delete(Request $request): Response
  201.     {
  202.         $response json_encode('error');
  203.         $address AddressModel::getAddressById($request->get('id'), $this->getEntityManager());
  204.         if (AddressModel::deleteAddress($address$this->getUser(), $this->getEntityManager())) {
  205.             $response json_encode('success');
  206.         }
  207.         return new Response($response);
  208.     }
  209.     public function updateAdminAddress(Request $request): Response
  210.     {
  211.         $order OrderModel::retrieveOrderEntityById($request->get('orderId'), $this->getEntityManager());
  212.         $data AddressModel::getAddressData($order->getBillingAddress(), $this->getEntityManager());
  213.         $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(array('code' => $order->getBillingAddress()->getCountryCode()));
  214.         return $this->render('@templates/AdminCustom/Customer/Address/update.html.twig', [
  215.             'order' => $order,
  216.             'countries' => $this->getDoctrine()->getRepository(Country::class)->findAll(),
  217.             'counties' => $this->getDoctrine()->getRepository(County::class)->findAllCountiesByCountry($country),
  218.             'data' => $data,
  219.             'lastPath' => $request->get('lastPath'),
  220.         ]);
  221.     }
  222.     public function editAdminAddressModal(Request $request): Response
  223.     {
  224.         $address AddressModel::getAddressById($request->get('addressId'), $this->getEntityManager());
  225.         $addressArray AddressModel::getAddressInArrayFormatById($request->get('addressId'), $this->getEntityManager());
  226.         $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(array('code' => $address->getCountryCode()));
  227.         return $this->render('@templates/AdminCustom/Customer/Address/Modal/edit.html.twig', [
  228.             'address' => $address,
  229.             'countries' => $this->getDoctrine()->getRepository(Country::class)->findAll(),
  230.             'counties' => $this->getDoctrine()->getRepository(County::class)->findAllCountiesByCountry($country),
  231.             'addressArray' => $addressArray,
  232.             'orderId' => $request->get('orderId'),
  233.         ]);
  234.     }
  235.     public function loadSelectCityBlock(): Response
  236.     {
  237.         $template $this->get('twig')
  238.             ->render('@templates/AdminCustom/Customer/Address/Form/_select_city.html.twig');
  239.         return new Response($template);
  240.     }
  241.     public function loadInputCityBlock(): Response
  242.     {
  243.         $template $this->get('twig')
  244.             ->render('@templates/AdminCustom/Customer/Address/Form/_input_city.html.twig');
  245.         return new Response($template);
  246.     }
  247.     /**
  248.      * @param Request $request
  249.      * @return Response
  250.      * If an address is edited in CMS by an Admin, we will save it as a new Address and the old one will be set as inactive
  251.      */
  252.     public function editAdminAddressSubmit(Request $request): Response
  253.     {
  254.         $response null;
  255.         $authService $this->getContainer()->get('app.service.security.auth_service');
  256.         if ($authService->checkIfLoggedUserIsAdmin()) {
  257.             $addressFormValidator = new AddressFormValidator($this->getEntityManager(), $this->getSerializer());
  258.             $oldAddress AddressModel::getAddressById($request->get('addressId'), $this->getEntityManager());
  259.             $order OrderModel::retrieveOrderEntityById($request->get('orderId'), $this->getEntityManager());
  260.             if ($addressFormValidator->checkForm($requesttrue) && $oldAddress && $order) {
  261.                 $customer $oldAddress->getCustomer();
  262.                 $data $request->request->all();
  263.                 $data AddressModel::removeFieldsForCustomerType($data);
  264.                 $addressData AddressModel::constructAddressDataArray($datafalse);
  265.                 $address AddressModel::setNewAddress($addressData$customer$this->getEntityManager()->getRepository(County::class));
  266.                 if ($address) {
  267.                     $oldAddress->setDeleted(true);
  268.                     $this->getEntityManager()->persist($address);
  269.                     $this->getEntityManager()->flush();
  270.                     $order->setBillingAddress($address);
  271.                     $this->getEntityManager()->flush();
  272.                     $response json_encode('success');
  273.                     $this->addFlash('success''Adresa editata cu succes');
  274.                 }
  275.             }
  276.         }
  277.         if (!$response) {
  278.             $response json_encode('fail');
  279.             $this->addFlash('error''A intervenit o eroare!');
  280.         }
  281.         return new Response($response);
  282.     }
  283.     /**
  284.      * @param Request $request
  285.      * @return Response
  286.      * @throws InvalidArgumentException
  287.      */
  288.     public function verifyCustomerCIFCode(Request $request): Response
  289.     {
  290.         $cif $request->get('customerCIF');
  291.         $countryCode $request->get('customerCountryCode');
  292.         $invoiceApiService = new InvoiceApiService();
  293.         $isValid $invoiceApiService->verifyVatCode($cif$countryCode$this->getEntityManager());
  294.         $response $isValid $invoiceApiService->getAugumentedInformationTermene($cif$countryCode$invoiceApiService->getLastResponse()) : array();
  295.         if ($request->get('isFromRca')) {
  296.             $customer CustomerService::retrieveCustomerFromShopUser($this->getUser());
  297.             $rcaTrackerActivityService $this->getContainer()->get('app.service.rca.tracker_activity');
  298.             $rcaTrackerActivityService->setCifInfoRetrieved($customer, array('isValid' => $isValid'response' => $response));
  299.         }
  300.         return new Response(json_encode(array('isValid' => $isValid'response' => $response)));
  301.     }
  302.     public function verifyCustomerPhoneNumber(Request $requestTranslatorInterface $translator)
  303.     {
  304.         $result false;
  305.         $phone $request->get('phone');
  306.         $locale $request->get('_locale') ? $request->get('_locale') : 'ro';
  307.         if ($phone) {
  308.             $result CustomerModel::verifyPhoneNumber($phone);
  309.         }
  310.         $message $translator->trans('app.ui.invalid_phone_for_sms_alert'$parameters = [], $domain null$locale);
  311.         return new Response(json_encode(array('result' => $result'error_message' => $message)));
  312.     }
  313.     public function loadAddressBlock(Request $request)
  314.     {
  315.         $addressId $request->get('addressId');
  316.         $isShippingAddress false;
  317.         if ($request->get('isShippingAddress') && $request->get('isShippingAddress') == true) {
  318.             $isShippingAddress true;
  319.         }
  320.         $address null;
  321.         $user $this->getUser();
  322.         $isValid false;
  323.         $city null;
  324.         $country null;
  325.         $county null;
  326.         $customerType null;
  327.         $customer null;
  328.         if ($user) {
  329.             if ($customer $user->getCustomer()) {
  330.                 $customerType $customer->getType();
  331.                 if ($customer && $addressId) {
  332.                     $address $this->getDoctrine()->getRepository(Address::class)->find($addressId);
  333.                     if ($address) {
  334.                         $customerType $address->getCustomerType();
  335.                         if ($address->getCustomer()
  336.                             && $address->getCustomer()->getId() == $customer->getId()) {
  337.                             $isValid true;
  338.                             if ($address->getCountryCode()) {
  339.                                 $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(array('code' => $address->getCountryCode()));
  340.                             }
  341.                             if ($address->getCounty()) {
  342.                                 if ($country) {
  343.                                     $county $this->getDoctrine()->getRepository(County::class)->findByCodeAndCountry($address->getCounty(), $country);
  344.                                 } else {
  345.                                     $county $this->getDoctrine()->getRepository(County::class)->findByCode($address->getCounty());
  346.                                 }
  347.                             }
  348.                             if ($address->getCity() && ctype_digit($address->getCity())) {
  349.                                 $city $this->getDoctrine()->getRepository(City::class)->find((int) $address->getCity());
  350.                             }
  351.                         }
  352.                     }
  353.                 }
  354.             }
  355.         }
  356.         $template json_encode('error');
  357.         try {
  358.             if (!$isValid) {
  359.                 $address null;
  360.             }
  361.             $country $country ?: $this->getDoctrine()->getRepository(Country::class)->findOneBy(['code' => $address $address->getCountryCode() : $request->getLocale()]);
  362.             $counties null;
  363.             if ($country) {
  364.                 $counties $this->getDoctrine()->getRepository(County::class)->findBy(['country' => $country->getId()]);
  365.             }
  366.             $templatePath = !$this->isMobile '@templates/Front/Checkout/address_block.html.twig'
  367.             '@templates/Mobile/Checkout/address_block.html.twig';
  368.             if ($isShippingAddress) {
  369.                 $templatePath = !$this->isMobile '@templates/Front/Checkout/shipping_address_block.html.twig'
  370.                 '@templates/Mobile/Checkout/shipping_address_block.html.twig';
  371.             }
  372.             $template $this->get('twig')
  373.                 ->render($templatePath, [
  374.                     'address' => $address,
  375.                     'countries' => $this->getDoctrine()->getRepository(Country::class)->findAll(),
  376.                     'counties' => $counties,
  377.                     'selectedCountry' => $country,
  378.                     'selectedCity' => $city,
  379.                     'selectedCounty' => $county,
  380.                     'customerType' => $customerType,
  381.                     'customer' => $customer,
  382.                 ]);
  383.         } catch (\Throwable $exception) {
  384.             captureException($exception);
  385.         }
  386.         return new Response($template);
  387.     }
  388.     public function verifyAddressBlock(Request $request)
  389.     {
  390.         $addressId $request->get('addressId');
  391.         $user $this->getUser();
  392.         $customer null;
  393.         if ($user) {
  394.             if ($customer $user->getCustomer()) {
  395.                 $customerType $customer->getType();
  396.                 if ($customer && $addressId) {
  397.                     $address $this->getDoctrine()->getRepository(Address::class)->find($addressId);
  398.                     if ($address) {
  399.                         // Check if required fields are empty or null
  400.                         if (empty($address->getCountryCode()) || empty($address->getCounty()) || empty($address->getCity()) || empty($address->getStreet()) || empty($address->getStreetNumber()) || (!$customerType && empty($address->getFullName())) || empty($address->getPhoneNumber())) {
  401.                             // Handle the case where one or more required fields are empty or null
  402.                             return new JsonResponse(['isValid' => false]);
  403.                         } else {
  404.                             // The address has all the required fields populated
  405.                             return new JsonResponse(['isValid' => true]);
  406.                         }
  407.                     }
  408.                 }
  409.             }
  410.         }
  411.         return new JsonResponse(['isValid' => true]);
  412.     }
  413.     public function loadShopInputCityBlock(Request $request)
  414.     {
  415.         $template json_encode('error');
  416.         $isShippingAddress false;
  417.         $customerType $request->get('customerType');
  418.         if ($request->get('isShippingAddress') && $request->get('isShippingAddress') == true) {
  419.             $isShippingAddress true;
  420.         }
  421.         try {
  422.             $template $this->get('twig')
  423.                 ->render('@templates/Front/Checkout/input_city_block.html.twig',
  424.                     [
  425.                         'isShippingAddress' => $isShippingAddress,
  426.                         'customerType' => $customerType
  427.                     ]);
  428.             if ($this->isMobile) {
  429.                 $template $this->get('twig')
  430.                     ->render('@templates/Mobile/Checkout/input_city_block.html.twig',
  431.                         [
  432.                             'isShippingAddress' => $isShippingAddress,
  433.                             'customerType' => $customerType
  434.                         ]);
  435.             }
  436.         } catch (\Throwable $exception) {
  437.             captureException($exception);
  438.         }
  439.         return new Response($template);
  440.     }
  441.     public function loadShopSelectCityBlock(Request $request)
  442.     {
  443.         $template json_encode('error');
  444.         $isShippingAddress false;
  445.         $customerType $request->get('customerType');
  446.         if ($request->get('isShippingAddress') && $request->get('isShippingAddress') == true) {
  447.             $isShippingAddress true;
  448.         }
  449.         try {
  450.             $template $this->get('twig')
  451.                 ->render('@templates/Front/Checkout/select_city_block.html.twig',
  452.                     [
  453.                         'isShippingAddress' => $isShippingAddress,
  454.                         'customerType' => $customerType
  455.                     ]);
  456.             if ($this->isMobile) {
  457.                 $template $this->get('twig')
  458.                     ->render('@templates/Mobile/Checkout/select_city_block.html.twig',
  459.                         [
  460.                             'isShippingAddress' => $isShippingAddress,
  461.                             'customerType' => $customerType
  462.                         ]);
  463.             }
  464.         } catch (\Throwable $exception) {
  465.             captureException($exception);
  466.         }
  467.         return new Response($template);
  468.     }
  469.     public function loadSelectCountyBlock()
  470.     {
  471.         $template json_encode('error');
  472.         try {
  473.             $template $this->get('twig')
  474.                 ->render('@templates/Front/Checkout/select_county_block.html.twig', [
  475.                     'counties' => $this->getDoctrine()->getRepository(County::class)->findAll(),
  476.                 ]);
  477.         } catch (\Throwable $exception) {
  478.             captureException($exception);
  479.         }
  480.         return new Response($template);
  481.     }
  482.     public function renderSavedAddressesBlock(Request $request)
  483.     {
  484.         $template json_encode('error');
  485.         $isShippingAddress false;
  486.         if ($request->get('isShippingAddress') && $request->get('isShippingAddress') == true) {
  487.             $isShippingAddress true;
  488.         }
  489.         try {
  490.             $user $this->getUser();
  491.             if ($user) {
  492.                 $customer $user->getCustomer();
  493.                 if ($customer) {
  494.                     $render '@templates/Front/Checkout/saved_addresses_block.html.twig';
  495.                     if ($isShippingAddress) {
  496.                         $render '@templates/Front/Checkout/saved_shipping_addresses_block.html.twig';
  497.                     }
  498.                     $template $this->get('twig')
  499.                         ->render($render, [
  500.                             'customer' => $customer,
  501.                             'cityRepository' => $this->getDoctrine()->getRepository(City::class),
  502.                             'countyRepository' => $this->getDoctrine()->getRepository(County::class),
  503.                         ]);
  504.                 }
  505.             }
  506.         } catch (\Throwable $exception) {
  507.             captureException($exception);
  508.         }
  509.         return new Response($template);
  510.     }
  511.     public function retrieveCitiesByCountyAction(Request $request): Response
  512.     {
  513.         $encoders = [new JsonEncoder()];
  514.         $normalizers = [new ObjectNormalizer()];
  515.         $serializer = new Serializer($normalizers$encoders);
  516.         $countyId $request->get('countyId');
  517.         $cities = [];
  518.         if (is_array($countyId)) {
  519.             foreach ($countyId as $item) {
  520.                 $cities array_merge($cities$this->retrieveCitiesByCountry($item));
  521.             }
  522.         } else {
  523.             $cities $this->retrieveCitiesByCountry($countyId);
  524.         }
  525.         if (($context $request->get('context')) && str_starts_with($context'institution-product')) {
  526.             $type explode('-'$context)[2];
  527.             $productTypeRepo $this->getDoctrine()->getRepository(ProductType::class);
  528.             $lastId 0;
  529.             $filteredCities = [];
  530.             $addAll false;
  531.             $productTypeCategories = [ProductType::CATEGORY_FINEProductType::CATEGORY_TARIFF];
  532.             while (in_array($type$productTypeCategories) &&
  533.                 $productTypes $productTypeRepo->getByCategoryWithOffset($type$lastId)
  534.             ) {
  535.                 foreach ($productTypes as $productType) {
  536.                     $lastId $productType['id'];
  537.                     $ibanData array_merge(
  538.                         json_decode($productType['legalIBANs'] ?? '[]'true),
  539.                         json_decode($productType['physicalIBANs'] ?? '[]'true)
  540.                     );
  541.                     foreach ($ibanData as $iban) {
  542.                         if (empty($iban['county']) || (empty($iban['city']) && $iban['county'] == $countyId)) {
  543.                             $addAll true;
  544.                             break 3;
  545.                         }
  546.                         if (!empty($iban['city']) && $iban['county'] == $countyId) {
  547.                             $filteredCities[] = $iban['city'];
  548.                         }
  549.                     }
  550.                 }
  551.             }
  552.             if (!$addAll) {
  553.                 $cities array_filter($cities, function ($city) use ($filteredCities) {
  554.                     return in_array($city['id'], $filteredCities);
  555.                 });
  556.             }
  557.         }
  558.         return new Response($serializer->serialize(array_values($cities), 'json'));
  559.     }
  560.     public function retrieveCitiesByCountry($countyId): array
  561.     {
  562.         $cityRepo $this->getEntityManager()->getRepository(City::class);
  563.         $cities $cityRepo->findAllCitiesByCounty($countyId);
  564.         if (!$cities) {
  565.             $romania $this->getEntityManager()->getRepository(Country::class)->findOneBy(['code' => 'RO']);
  566.             $county $this->getEntityManager()->getRepository(County::class)
  567.                 ->findByCodeAndCountry($countyId$romania);
  568.             if ($county) {
  569.                 $cities $cityRepo->findAllCitiesByCounty($county);
  570.             }
  571.         }
  572.         return $cities;
  573.     }
  574.     public function retrieveCountryCounties(Request $request)
  575.     {
  576.         $countryCode $request->get('countryCode');
  577.         $encoders = [new JsonEncoder()];
  578.         $normalizers = [new ObjectNormalizer()];
  579.         $serializer = new Serializer($normalizers$encoders);
  580.         $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(array('code' => $countryCode));
  581.         $counties $this->getDoctrine()->getRepository(County::class)->findAllCountiesByCountryWithSpecificFields($country);
  582.         $counties $serializer->serialize($counties'json', [
  583.             'circular_reference_handler' => function ($object) {
  584.                 return $object->getId();
  585.             },
  586.         ]);
  587.         return new Response($counties);
  588.     }
  589.     public function deleteSelectedAddress(Request $request)
  590.     {
  591.         $result json_encode('error');
  592.         $addressId $request->get('addressId');
  593.         $user $this->getUser();
  594.         if ($user) {
  595.             if ($customer $user->getCustomer()) {
  596.                 if ($customer && $addressId) {
  597.                     $address $this->getDoctrine()->getRepository(Address::class)->find($addressId);
  598.                     if ($address) {
  599.                         if ($address->getCustomer()
  600.                             && $address->getCustomer()->getId() == $customer->getId()) {
  601.                             $customer->removeAddress($address);
  602.                             $this->getEntityManager()->persist($address);
  603.                             $this->getEntityManager()->flush();
  604.                             $this->addFlash('address-deleted''success');
  605.                             $result json_encode('success');
  606.                         }
  607.                     }
  608.                 }
  609.             }
  610.         }
  611.         return new Response($result);
  612.     }
  613.     public function retrieveCountryCodeByName(Request $request)
  614.     {
  615.         $code '';
  616.         $countryName $request->get('countryName');
  617.         if ($countryName) {
  618.             $code CountryModel::retrieveCountryCodeByName($countryName$this->getEntityManager());
  619.         }
  620.         return new Response(json_encode($code));
  621.     }
  622.     public function checkCNPAction(Request $request)
  623.     {
  624.         $isNN = !empty($request->get('isNN'));
  625.         $cnp $request->get('customerCNP');
  626.         $isValid CustomerModel::validateCNP($cnp$isNN);
  627.         return new Response(json_encode(array('isValid' => $isValid)));
  628.     }
  629. }