src/Controller/Api/CheckoutController.php line 166

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\BaselinkerOrder;
  4. use App\Entity\Cart as EntityCart;
  5. use App\Env;
  6. use App\Func;
  7. use stdClass;
  8. use App\Entity\City;
  9. use App\Entity\Page;
  10. use App\Entity\User;
  11. use App\Entity\Region;
  12. use App\Entity\Esystem;
  13. use App\Entity\Delivery;
  14. use App\Entity\Order;
  15. use App\Repository\BaselinkerOrderRepository;
  16. use App\Repository\CartRepository;
  17. use App\Service\Auth\Auth;
  18. use App\Service\Cart\Cart;
  19. use App\Repository\CityRepository;
  20. use App\Repository\UserRepository;
  21. use App\Service\Checkout\Checkout;
  22. use App\Repository\RegionRepository;
  23. use App\Repository\EsystemRepository;
  24. use App\Repository\DeliveryRepository;
  25. use App\Repository\OrderRepository;
  26. use App\Service\Baselinker\Baselinker;
  27. use Doctrine\ORM\EntityManagerInterface;
  28. use Exception;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\Security\Core\Security;
  31. use Symfony\Component\HttpFoundation\Response;
  32. use Symfony\Component\Routing\Annotation\Route;
  33. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  34. use App\Service\Baselinker\DTO\Prod as ProdDTO;
  35. use App\Service\Baselinker\DTO\Order as OrderDTO;
  36. use App\Service\Baselinker\DTO\Company as CompanyDTO;
  37. use App\Service\Baselinker\DTO\Delivery as DeliveryDTO;
  38. use ASweb\Baselinker\DTO\Delivery as DTODelivery;
  39. use ASweb\Baselinker\DTO\Order as DTOOrder;
  40. class CheckoutController extends AbstractController
  41. {
  42.     protected $default_region_id '0c5b2444-70a0-4932-980c-b4dc0d3f02b5';
  43.     protected $default_city_id '0c5b2444-70a0-4932-980c-b4dc0d3f02b5';
  44.     protected User $User;
  45.     //Repositories    
  46.     protected RegionRepository $Regions;
  47.     protected CityRepository $Cities;
  48.     protected DeliveryRepository $Deliveries;
  49.     protected EsystemRepository $Esystems;
  50.     protected UserRepository $Users;
  51.     protected OrderRepository $Orders;
  52.     protected CartRepository $cartRepository;
  53.     public function __construct(
  54.         private EntityManagerInterface $em
  55.         private Checkout $Checkout
  56.         private Cart $Cart
  57.         private Auth $Auth
  58.         private Security $security,
  59.         private Baselinker $baselinker,
  60.         private BaselinkerOrderRepository $baselinkerOrderRepository,
  61.     )
  62.     {
  63.         if (Env::site() == Env::DOM || Env::site() == Env::OPT) {
  64.             $this->default_region_id 'dcaadb64-4b33-11e4-ab6d-005056801329';
  65.             $this->default_city_id 'e718a680-4b33-11e4-ab6d-005056801329';
  66.         }
  67.         $this->em $em;
  68.         $this->Checkout $Checkout;
  69.         $this->Cart $Cart;
  70.         $this->Auth $Auth;
  71.         $this->Auth->setUser($security->getUser());
  72.         $this->Regions $this->em->getRepository(Region::class);
  73.         $this->Cities $this->em->getRepository(City::class);
  74.         $this->Deliveries $this->em->getRepository(Delivery::class);
  75.         $this->Esystems $this->em->getRepository(Esystem::class);
  76.         $this->Users $this->em->getRepository(User::class);
  77.         $this->Orders $this->em->getRepository(Order::class);
  78.         $this->cartRepository $this->em->getRepository(EntityCart::class);
  79.     }
  80.     // Разделение строки на фамилию, имя, определение пола
  81.     #[Route(path'/api/checkout/cleanname'name'api_checkout_cleanname_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  82.     #[Route(path'/{_locale}/api/checkout/cleanname'name'api_checkout_cleanname'requirements: ['_locale' => '%app.langs%'])]
  83.     public function cleanname(Request $request): Response
  84.     {
  85.         $fullname = (string) $request->query->get('fullname');
  86.         $curl curl_init();
  87.         $headers[] = 'Content-Type: application/json';
  88.         $headers[] = 'Authorization: Token ' Env::code_dadata();
  89.         $headers[] = 'X-Secret: ' Env::code_dadata_2();
  90.         $request = [$fullname];
  91.         curl_setopt($curlCURLOPT_URL"https://cleaner.dadata.ru/api/v1/clean/name");
  92.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  93.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  94.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($requestJSON_UNESCAPED_UNICODE));
  95.         $response curl_exec($curl);
  96.         $data json_decode($responsetrue)[0];
  97.         $response = [];
  98.         if (($data['name']) && ($data['surname'])) {
  99.             $usersurname $data['surname'];
  100.             $username $data['name'];
  101.             switch ($data['gender']) {
  102.                 case 'Ж':
  103.                     $usergender 'F';
  104.                     break;
  105.                 case 'М':
  106.                     $usergender 'M';
  107.                     break;
  108.                 default:
  109.                     $usergender null;
  110.             }
  111.         }
  112.         $response['username'] = $username;
  113.         $response['usersurname'] = $usersurname;
  114.         $response['usergender'] = $usergender;
  115.         return $this->json($response);
  116.     }
  117.     // Подсказки при вводе ФИО
  118.     #[Route(path'/api/checkout/fiohint'name'api_checkout_fiohint_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  119.     #[Route(path'/{_locale}/api/checkout/fiohint'name'api_checkout_fiohint'requirements: ['_locale' => '%app.langs%'])]
  120.     public function fiohint(Request $request): Response
  121.     {
  122.         $fullname = (string) $request->query->get('fullname');
  123.         $curl curl_init();
  124.         $headers[] = 'Content-Type: application/json';
  125.         $headers[] = 'Authorization: Token ' Env::code_dadata();
  126.         $request = ['query' => $fullname'parts' => ['surname''name'], 'count' => 10];
  127.         curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/fio");
  128.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  129.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  130.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($request));
  131.         $response curl_exec($curl);
  132.         $suggestions json_decode($response);
  133.         //var_dump($suggestions);
  134.         $apiResponce = [];
  135.         foreach ($suggestions->suggestions as $suggestion) {
  136.             $obj = new stdClass();
  137.             $obj->value $suggestion->value;
  138.             $obj->name $suggestion->data->name;
  139.             $obj->surname $suggestion->data->surname;
  140.             $obj->gender $suggestion->data->gender;
  141.             $apiResponce[] = $obj;
  142.         }
  143.         return $this->json($apiResponce);
  144.     }
  145.     #[Route(path'/api/checkout/citydetect'name'api_checkout_citydetect_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  146.     #[Route(path'/{_locale}/api/checkout/citydetect'name'api_checkout_citydetect'requirements: ['_locale' => '%app.langs%'])]
  147.     public function citydetect(Request $request): Response
  148.     {
  149.         $city_fias_id $this->Checkout->getCity_fias_id();
  150.         if ($city_fias_id) {
  151.         } else {
  152.             if (Env::site() == env::DOM || Env::site() == env::OPT) {
  153.                 $ip $_SERVER["REMOTE_ADDR"];
  154.                 $request = @file_get_contents("https://api.sypexgeo.net/json/" $ip);
  155.                 $array json_decode($requesttrue);
  156.                 $city_name = isset($array['city']['name_ru']) ? $array['city']['name_ru'] : "";
  157.                 $city $this->Cities->findOneBy(["name" => $city_name]);
  158.                 if (!$city) {
  159.                     $region $this->Regions->findOneBy(['fias_id' => $this->default_region_id]);
  160.                     $city $this->Cities->findOneBy(['fias_id' => $this->default_city_id]);
  161.                     $this->Checkout->setRegion_fias_id($region->getFiasId());
  162.                     $this->Checkout->setRegion_name($region->getName());
  163.                     $this->Checkout->setCity_fias_id($city->getFiasId());
  164.                     $this->Checkout->setCity_name($city->getName());
  165.                     $this->Checkout->setCity_post_code($city->getPostalCode());
  166.                 } else {
  167.                     $region $this->Regions->find($city->getRegion());
  168.                     $this->Checkout->setRegion_fias_id($region->getFiasId());
  169.                     $this->Checkout->setRegion_name($region->getName());
  170.                     $this->Checkout->setCity_fias_id($city->getFiasId());
  171.                     $this->Checkout->setCity_name($city->getName());
  172.                     $this->Checkout->setCity_post_code($city->getPostalCode());
  173.                 }
  174.             } elseif (Env::site() == env::MIR || Env::site() == env::OPT_MIR) {
  175.                 $curl curl_init();
  176.                 $headers[] = 'Accept: application/json';
  177.                 $headers[] = 'Authorization: Token ' Env::code_dadata();
  178.                 curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/iplocate/address?ip=" $_SERVER['REMOTE_ADDR']);
  179.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  180.                 curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  181.                 $response json_decode(curl_exec($curl));
  182.                 if ($response->location == null) {
  183.                     $region $this->Regions->findOneBy(['fias_id' => $this->default_region_id]);
  184.                     $city $this->Cities->findOneBy(['fias_id' => $this->default_city_id]);
  185.                     $this->Checkout->setRegion_fias_id($region->getFiasId());
  186.                     $this->Checkout->setRegion_name($region->getName());
  187.                     $this->Checkout->setCity_fias_id($city->getFiasId());
  188.                     $this->Checkout->setCity_name($city->getName());
  189.                     $this->Checkout->setCity_post_code($city->getPostalCode());
  190.                 } else {
  191.                     $this->Checkout->setRegion_fias_id($response->location->data->region_fias_id);
  192.                     $this->Checkout->setRegion_name($response->location->data->region_with_type);
  193.                     $this->Checkout->setCity_fias_id(($response->location->data->settlement_fias_id $response->location->data->settlement_fias_id $response->location->data->city_fias_id));
  194.                     $this->Checkout->setCity_name(($response->location->data->settlement_with_type $response->location->data->settlement_with_type $response->location->data->city_with_type));
  195.                     $this->Checkout->setCity_post_code($response->location->data->postal_code);
  196.                 }
  197.             } else {
  198.                 $this->Checkout->setRegion_fias_id('default');
  199.             }
  200.         }
  201.         return $this->json($this->Checkout->getCheckout());
  202.     }
  203.     // Подсказки при вводе города
  204.     #[Route(path'/api/checkout/citylist'name'api_checkout_citylist_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  205.     #[Route(path'/{_locale}/api/checkout/citylist'name'api_checkout_citylist'requirements: ['_locale' => '%app.langs%'])]
  206.     public function citylist(Request $request): Response
  207.     {
  208.         $q = (string) $request->query->get('string');
  209.         if (Env::site() == env::DOM || Env::site() == env::OPT) {
  210.             /** @var \App\Entity\City[] $cities */
  211.             $cities $this->em->createQuery("SELECT c FROM App\Entity\City c WHERE c.kw LIKE '%" $q "%' ORDER BY c.type ASC")->setMaxResults(10)->getResult(); // Check
  212.             if (count($cities)) {
  213.                 $response = new \stdClass();
  214.                 $response->suggestions = [];
  215.                 foreach ($cities as $city) {
  216.                     $region $this->Regions->find($city->getRegion());
  217.                     if ($region) {
  218.                         $suggestion = new \stdClass();
  219.                         $suggestion->value $city->getType() . ' ' $city->getName() . ", " $region->getName();
  220.                         $suggestion->data = new \stdClass();
  221.                         $suggestion->data->city_fias_id $city->getFiasId();
  222.                         array_push($response->suggestions$suggestion);
  223.                     }
  224.                 }
  225.             } else {
  226.                 $response = new \stdClass();
  227.                 $response->suggestions = [];
  228.             }
  229.         } else {
  230.             $curl curl_init();
  231.             $headers[] = 'Content-Type: application/json';
  232.             $headers[] = 'Accept: application/json';
  233.             $headers[] = 'Authorization: Token ' Env::code_dadata();
  234.             $request = ['query' => $q'from_bound' => ['value' => 'city'], 'to_bound' => ['value' => 'settlement'], 'count' => 10];
  235.             curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address");
  236.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  237.             curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  238.             curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($requestJSON_UNESCAPED_UNICODE));
  239.             $response curl_exec($curl);
  240.             $response json_decode($response);
  241.         }
  242.         return $this->json($response);
  243.     }
  244.     // Выбор города
  245.     #[Route(path'/api/checkout/selectcity'name'api_checkout_selectcity_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  246.     #[Route(path'/{_locale}/api/checkout/selectcity'name'api_checkout_selectcity'requirements: ['_locale' => '%app.langs%'])]
  247.     public function selectcity(Request $requestCheckout $Checkout): Response
  248.     {
  249.         $fias_id = (string) $request->query->get('fias_id');
  250.         if ($fias_id) {
  251.             if (Env::site() == env::DOM || Env::site() == env::OPT) {
  252.                 $city $this->Cities->findOneBy(['fias_id' => $fias_id]); // Check
  253.                 if ($city->getId()) {
  254.                     $region $this->Regions->find($city->getRegion());
  255.                     if ($region) {
  256.                         $Checkout->setRegion_fias_id($region->getFiasId());
  257.                         $Checkout->setRegion_name($region->getName());
  258.                         $Checkout->setCity_fias_id($city->getFiasId());
  259.                         $Checkout->setCity_name($city->getName());
  260.                         $Checkout->setCity_post_code($city->getPostalCode());
  261.                         $Checkout->setUseraddr('');
  262.                         $Checkout->setUserlat('');
  263.                         $Checkout->setUserlon('');
  264.                         $Checkout->setDelivery_post_code('');
  265.                         $Checkout->setDelivery_post_name('');
  266.                         $Checkout->setDelivery_post_worktime('');
  267.                         $Checkout->setDelivery_post_phone('');
  268.                         $Checkout->setDelivery_post_email('');
  269.                         $Checkout->setDelivery_sub_id(0);
  270.                         $Checkout->setDelivery_sub_name('');
  271.                         $Checkout->setDelivery_cost(0);
  272.                     }
  273.                 }
  274.             } else {
  275.                 $curl curl_init();
  276.                 $headers[] = 'Content-Type: application/json';
  277.                 $headers[] = 'Accept: application/json';
  278.                 $headers[] = 'Authorization: Token ' Env::code_dadata();
  279.                 $request = ['query' => $fias_id];
  280.                 curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/address");
  281.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  282.                 curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  283.                 curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($request));
  284.                 $response json_decode(curl_exec($curl));
  285.                 if (!count($response->suggestions)) {
  286.                     $region $this->Regions->findOneBy(['fias_id' => $this->default_region_id]);
  287.                     $city $this->Cities->findOneBy(['fias_id' => $this->default_city_id]);
  288.                     $Checkout->setRegion_fias_id($region->getFiasId());
  289.                     $Checkout->setRegion_name($region->getName());
  290.                     $Checkout->setCity_fias_id($city->getFiasId());
  291.                     $Checkout->setCity_name($city->getName());
  292.                     $Checkout->setCity_post_code($city->getPostalCode());
  293.                     $Checkout->setUseraddr('');
  294.                     $Checkout->setUserlat('');
  295.                     $Checkout->setUserlon('');
  296.                     $Checkout->setDelivery_post_code('');
  297.                     $Checkout->setDelivery_post_name('');
  298.                     $Checkout->setDelivery_post_worktime('');
  299.                     $Checkout->setDelivery_post_phone('');
  300.                     $Checkout->setDelivery_post_email('');
  301.                     $Checkout->setDelivery_sub_id(0);
  302.                     $Checkout->setDelivery_sub_name('');
  303.                     $Checkout->setDelivery_cost(0);
  304.                 } else {
  305.                     $Checkout->setRegion_fias_id($response->suggestions[0]->data->region_fias_id);
  306.                     $Checkout->setRegion_name($response->suggestions[0]->data->region_with_type);
  307.                     $Checkout->setCity_fias_id(($response->suggestions[0]->data->settlement_fias_id $response->suggestions[0]->data->settlement_fias_id $response->suggestions[0]->data->city_fias_id));
  308.                     $Checkout->setCity_name(($response->suggestions[0]->data->settlement_with_type $response->suggestions[0]->data->settlement_with_type $response->suggestions[0]->data->city_with_type));
  309.                     $Checkout->setCity_post_code($response->suggestions[0]->data->postal_code);
  310.                     $city $this->Cities->findOneBy(['fias_id' => $Checkout->getCity_fias_id()]);
  311.                     if ($city->getPostalCode()) {
  312.                         $Checkout->setCity_post_code($city->getPostalCode());
  313.                     }
  314.                     $Checkout->setUseraddr('');
  315.                     $Checkout->setUserlat('');
  316.                     $Checkout->setUserlon('');
  317.                     $Checkout->setDelivery_post_code('');
  318.                     $Checkout->setDelivery_post_name('');
  319.                     $Checkout->setDelivery_post_worktime('');
  320.                     $Checkout->setDelivery_post_phone('');
  321.                     $Checkout->setDelivery_post_email('');
  322.                     $Checkout->setDelivery_sub_id(0);
  323.                     $Checkout->setDelivery_sub_name('');
  324.                     $Checkout->setDelivery_cost(0);
  325.                 }
  326.             }
  327.         }
  328.         return $this->json($Checkout->getCheckout());
  329.     }
  330.     // Подсказки при вводе адреса
  331.     #[Route(path'/api/checkout/addresslist'name'api_checkout_addresslist_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  332.     #[Route(path'/{_locale}/api/checkout/addresslist'name'api_checkout_addresslist'requirements: ['_locale' => '%app.langs%'])]
  333.     public function addressList(Request $requestCheckout $Checkout): Response
  334.     {
  335.         $q = (string) $request->query->get('string');
  336.         $curl curl_init();
  337.         $headers[] = 'Content-Type: application/json';
  338.         $headers[] = 'Accept: application/json';
  339.         $headers[] = 'Authorization: Token ' Env::code_dadata();
  340.         $request = ['query' => $q'locations' => ['city_fias_id' => $Checkout->getCity_fias_id(), ['settlement_fias_id' => $Checkout->getCity_fias_id()]], 'from_bound' => ['value' => 'street'], 'to_bound' => ['value' => 'flat'], 'count' => 10];
  341.         curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address");
  342.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  343.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  344.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($request));
  345.         $response json_decode(curl_exec($curl), true);
  346.         return $this->json($response);
  347.     }
  348.     // Get Session Vars
  349.     #[Route(path'/api/checkout/get-session-vars'name'api_checkout_get_session_vars_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  350.     #[Route(path'/{_locale}/api/checkout/get-session-vars'name'api_checkout_get_session_vars'requirements: ['_locale' => '%app.langs%'])]
  351.     public function getSessionVars(): Response
  352.     {
  353.         $result = [
  354.             'amount_delivery' => Func::fmtmoney($this->Cart->getAmountDelivery()),
  355.             'amount_with_delivery' => Func::fmtmoney($this->Cart->getAmountWithDelivery()),
  356.             'userlat' => $this->Checkout->getUserlat(),
  357.             'userlon' => $this->Checkout->getUserlon(),
  358.             'delivery_id' => (int) $this->Checkout->getDelivery_id(),
  359.             'delivery_intname' => $this->Checkout->getDelivery_intname(),
  360.             'delivery_name' => $this->Checkout->getDelivery_name(),
  361.             'delivery_post_code' => $this->Checkout->getDelivery_post_code(),
  362.         ];
  363.         return $this->json($result);
  364.     }
  365.     // Выбор сервиса доставки
  366.     #[Route(path'/api/checkout/select-type/{delivery_id}'name'api_checkout_select_type_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  367.     #[Route(path'/{_locale}/api/checkout/select-type/{delivery_id}'name'api_checkout_select_type'requirements: ['_locale' => '%app.langs%'])]
  368.     public function selectType(int $delivery_id 0): Response
  369.     {
  370.         $delivery $this->Deliveries->find($delivery_id);
  371.         if ($delivery) {
  372.             $this->Checkout->setDelivery_id($delivery->getId());
  373.             $this->Checkout->setDelivery_intname($delivery->getIntname());
  374.             $this->Checkout->setDelivery_name($delivery->getName());
  375.             $result = [
  376.                 'error' => null,
  377.                 'delivery_id' => $delivery->getId(),
  378.                 'delivery_intname' => $delivery->getIntname(),
  379.                 'delivery_name' => $delivery->getName(),
  380.             ];
  381.         } else {
  382.             $result = [
  383.                 "error" => "wrong delivery id",
  384.             ];
  385.         }
  386.         return $this->json($result);
  387.     }
  388.     // Выбор варианта доставки
  389.     #[Route(path'/api/checkout/select-price/{sub_id}/{sub_code}/{sub_name}/{cost}'name'api_checkout_select_price_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  390.     #[Route(path'/{_locale}/api/checkout/select-price/{sub_id}/{sub_code}/{sub_name}/{cost}'name'api_checkout_select_price'requirements: ['_locale' => '%app.langs%'])]
  391.     public function selectPrice(int $sub_id 0string $sub_code ''string $sub_name ''string $cost ''): Response
  392.     {
  393.         $this->Checkout->setDelivery_sub_id($sub_id);
  394.         $this->Checkout->setDelivery_sub_code($sub_code);
  395.         $this->Checkout->setDelivery_sub_name($sub_name);
  396.         $this->Checkout->setDelivery_cost($cost);
  397.         $result = [
  398.             'error' => null,
  399.             'delivery_sub_id' => $sub_id,
  400.             'delivery_sub_code' => $sub_code,
  401.             'delivery_sub_name' => $sub_name,
  402.             'delivery_cost' => $cost,
  403.         ];
  404.         return $this->json($result);
  405.     }
  406.     // Изменение цены доставки
  407.     #[Route(path'/api/checkout/set-price/{cost}'name'api_checkout_set_price_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  408.     #[Route(path'/{_locale}/api/checkout/set-price/{cost}'name'api_checkout_set_price'requirements: ['_locale' => '%app.langs%'])]
  409.     public function setPrice($cost ''): Response
  410.     {
  411.         $this->Checkout->setDelivery_cost($cost);
  412.         $result = [
  413.             'delivery_cost' => $cost,
  414.         ];
  415.         return $this->json($result);
  416.     }
  417.     // Выбор отделения доставки
  418.     #[Route(path'/api/checkout/select-code/{code}/{name}/{worktime}/{phone}/{email}'name'api_checkout_select_code_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  419.     #[Route(path'/{_locale}/api/checkout/select-code/{code}/{name}/{worktime}/{phone}/{email}'name'api_checkout_select_code'requirements: ['_locale' => '%app.langs%'])]
  420.     public function selectCode(string $code ''string $name ''string $worktime ''string $phone ''string $email ''): Response
  421.     {
  422.         $this->Checkout->setUserlat('');
  423.         $this->Checkout->setDelivery_post_code($code);
  424.         $this->Checkout->setDelivery_post_name($name);
  425.         $this->Checkout->setDelivery_post_worktime($worktime);
  426.         $this->Checkout->setDelivery_post_phone($phone);
  427.         $this->Checkout->setDelivery_post_email($email);
  428.         $result = [
  429.             'error' => null,
  430.             'delivery_post_code' => $code,
  431.             'delivery_post_name' => $name,
  432.             'delivery_post_worktime' => $worktime,
  433.             'delivery_post_phone' => $phone,
  434.             'delivery_post_email' => $email,
  435.         ];
  436.         return $this->json($result);
  437.     }
  438.     // Выбор отделения доставки
  439.     #[Route(path'/api/checkout/select-address/{addr}/{lat}/{lon}'name'api_checkout_select-address_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  440.     #[Route(path'/{_locale}/api/checkout/select-address/{addr}/{lat}/{lon}'name'api_checkout_select-address'requirements: ['_locale' => '%app.langs%'])]
  441.     public function selectAddress(string $addr ''string $lat ''string $lon ''): Response
  442.     {
  443.         $this->Checkout->setUseraddr($addr);
  444.         $this->Checkout->setUserlat($lat);
  445.         $this->Checkout->setUserlon($lon);
  446.         $result = [
  447.             'useraddr' => $addr,
  448.             'userlat' => $lat,
  449.             'userlon' => $lon,
  450.         ];
  451.         return $this->json($result);
  452.     }
  453.     // Выбор способа оплаты
  454.     #[Route(path'/api/checkout/payment/select-type/{id}'name'checkout_payment_select_type_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  455.     #[Route(path'/{_locale}/api/checkout/payment/select-type/{id}'name'checkout_payment_select_type'requirements: ['_locale' => '%app.langs%'])]
  456.     public function payment(int $id): Response
  457.     {
  458.         $esystem $this->Esystems->find($id);
  459.         if ($esystem) {
  460.             $this->Checkout->setPayment_id($id);
  461.             $this->Checkout->setPayment_name($esystem->getName());
  462.             $result = [
  463.                 'payment_id' => $id,
  464.                 'payment_name' => $esystem->getName(),
  465.             ];
  466.         } else {
  467.             $result = [
  468.                 'error' => "wrong id",
  469.             ];
  470.         }
  471.         return $this->json($result);
  472.     }
  473.     // Данные компании
  474.     #[Route(path'/api/checkout/payment/company-data'name'checkout_payment_company_data_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  475.     #[Route(path'/{_locale}/api/checkout/payment/company-data'name'checkout_payment_company_data'requirements: ['_locale' => '%app.langs%'])]
  476.     public function companyData(Request $request): Response
  477.     {      
  478.         $company_nip $request->get('company_nip');
  479.         $company_name $request->get('company_name');
  480.         $company_index $request->get('company_index');
  481.         $company_city $request->get('company_city');
  482.         $company_street $request->get('company_street');
  483.         $company_house $request->get('company_house');
  484.         $company_flat $request->get('company_flat');
  485.         $this->Checkout->setCompanyNip($company_nip);
  486.         $this->Checkout->setCompanyName($company_name);
  487.         $this->Checkout->setCompanyIndex($company_index);
  488.         $this->Checkout->setCompanyCity($company_city);
  489.         $this->Checkout->setCompanyStreet($company_street);
  490.         $this->Checkout->setCompanyHouse($company_house);
  491.         $this->Checkout->setCompanyFlat($company_flat);
  492.         if($this->Auth->getUserId()) {
  493.             $user $this->Users->find($this->Auth->getUserId());
  494.             $user->setCompanyNip($company_nip);
  495.             $user->setCompanyName($company_name);
  496.             $user->setCompanyIndex($company_index);
  497.             $user->setCompanyCity($company_city);
  498.             $user->setCompanyStreet($company_street);
  499.             $user->setCompanyHouse($company_house);
  500.             $user->setCompanyFlat($company_flat);
  501.             $this->em->persist($user);
  502.             $this->em->flush();
  503.         }
  504.         return $this->json(true);
  505.     }
  506.     // Очистка данных компании
  507.     #[Route(path'/api/checkout/payment/clear-company-data'name'checkout_payment_clear_company_data_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  508.     #[Route(path'/{_locale}/api/checkout/payment/clear-company-data'name'checkout_payment_clear_company_data'requirements: ['_locale' => '%app.langs%'])]
  509.     public function clearCompanyData(): Response
  510.     {
  511.         $this->Checkout->setCompanyNip('');
  512.         $this->Checkout->setCompanyName('');
  513.         $this->Checkout->setCompanyIndex('');
  514.         $this->Checkout->setCompanyCity('');
  515.         $this->Checkout->setCompanyStreet('');
  516.         $this->Checkout->setCompanyHouse('');
  517.         $this->Checkout->setCompanyFlat('');
  518.         return $this->json(true);
  519.     }
  520.     // Добавление заказа в Baselinker
  521.     #[Route(path'/api/checkout/baselinker/{id}'name'checkout_baselinker_add'defaults: ['_locale' => '%app.default_lang%'])]
  522.     public function addToBaselinker(int $id): Response
  523.     {
  524.         if (Env::site() != Env::MIX && Env::site() != Env::OPT_MIX) {
  525.             return $this->json(false);
  526.         }
  527.         
  528.         $this->baselinker->setToken(Env::baselinker_token());
  529.         $order $this->Orders->find($id);
  530.         if (!$order) {
  531.             return new Response("Ошибка добавления заказа в Baselinker");
  532.         }
  533.         $user $this->Users->find($order->getUser());
  534.         $cartitems $this->cartRepository->getByOrder($order->getId());
  535.         $delivery $this->Deliveries->find($order->getDelivery());
  536.         $esystem $this->Esystems->find($order->getEsystem());
  537.         $bl_prods = [];
  538.         foreach ($cartitems as $k => $v) {            
  539.             $prod $v->getProd();
  540.             $discount = ($v->getSkidka()) ? $v->getSkidka() :  $v->getUserdiscount() + $v->getNumdiscount();
  541.             $bl_prods[] = new ProdDTO((string) $prod->getId(), '', (string) $prod->getName(), (string) $prod->getExternalId(), $v->getPrice() * (100 $discount) / 1000$v->getNum(), (float) $prod->getWeight());
  542.         }
  543.         
  544.         try {
  545.             // if (!$this->baselinkerOrderRepository->findOneBy(["order_id" => $order->getId()])) {
  546.                 $bo = new BaselinkerOrder();
  547.                 $bo->setOrderId($order->getId());
  548.                 $bo->setBaselinkerId(0);
  549.                 $this->baselinkerOrderRepository->add($botrue);
  550.                 $bl $this->baselinker->newOrder(
  551.                     new OrderDTO((int) $order->getId(), 0, (int) $order->getTstamp(), (string) $order->getPhone(), (string) $order->getEmail(), Env::currency_code(), (string) $esystem->getName(), (string) $order->getComment()),
  552.                     $bl_prods,
  553.                     new DeliveryDTO((string) $delivery->getIntname(), (string) $delivery->getName(), (string) $order->getDeliverycost(), (string) $order->getName(), (string) $order->getAddr(), (string) $order->getCity(), '', (string) $order->getPostcode(), ""$order->getSklad()),
  554.                     new CompanyDTO((string) $order->getCompanyNip(), (string) $order->getCompanyName(), (string) $order->getCompanyIndex(), (string) $order->getCompanyCity(), (string) $order->getCompanyStreet(), (string) $order->getCompanyHouse(), (string) $order->getCompanyFlat()),
  555.                     $bo
  556.                 );
  557.                 return new Response("Заказ добавлен в Baselinker");
  558.             // } else {
  559.                 return new Response("Заказ уже в Baselinker");
  560.             // }            
  561.         } catch (Exception $e) {
  562.             return new Response("Ошибка добавления заказа в Baselinker: ".$e->getMessage());    
  563.         }
  564.         return new Response("Ошибка добавления заказа в Baselinker");
  565.     }
  566. }