src/Controller/ProductController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ChangeItemPriceTransaction;
  4. use App\Entity\Item;
  5. use App\Entity\Product;
  6. use App\Entity\Storage;
  7. use App\Repository\ChangeItemPriceTransactionRepository;
  8. use App\Repository\ItemRepository;
  9. use App\Repository\ProductRepository;
  10. use App\Repository\StorageRepository;
  11. use App\Repository\SupplierAPIParamRepository;
  12. use App\Service\Helper\NormalizerHelper;
  13. use App\Service\Product\ProductCreator;
  14. use App\Service\Product\ProductObject;
  15. use App\Service\Product\SupplierItemUpdater;
  16. use App\Service\Supplier\SupplierApiFactory;
  17. use Doctrine\ORM\OptimisticLockException;
  18. use Doctrine\ORM\ORMException;
  19. use Exception;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. /**
  24.  * @Route("/product")
  25.  */
  26. class ProductController extends BaseController
  27. {
  28.     /**
  29.      * @Route("/search_list", methods="GET")
  30.      */
  31.     public function list(Request $requestProductRepository $productRepository): JsonResponse
  32.     {
  33.         return new JsonResponse(ProductObject::getList(...$productRepository->findBySubString($request->query->get('query'''), $request->query->get('brand'), $request->query->getInt('limit'100), $request->query->getInt('offset'), $request->query->getBoolean('from_crm')))
  34.         );
  35.     }
  36.     /**
  37.      * @Route("/search_items", methods="GET")
  38.      */
  39.     public function searchItems(Request $requestProductRepository $productRepository): JsonResponse
  40.     {
  41.         return new JsonResponse([
  42.             'items' => ProductObject::getItems(true, ...$productRepository->findBySubString($request->query->get('query'''), $request->query->get('brand'''), $request->query->getInt('limit'100), $request->query->getInt('offset'), $request->query->getBoolean('from_crm'))),
  43.             'count' => $productRepository->findByStringCount($request->query->get('query'''))
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/search_store_items", methods="GET")
  48.      */
  49.     public function searchStoreItems(Request $requestProductRepository $productRepository): JsonResponse
  50.     {
  51.         return new JsonResponse([
  52.             'items' => ProductObject::getItems(true, ...$productRepository->findBySubString($request->query->get('query'''), $request->query->get('brand'''), $request->query->getInt('limit'100), $request->query->getInt('offset'), $request->query->getBoolean('from_crm'))),
  53.             'count' => $productRepository->findByStringCount($request->query->get('query'''))
  54.         ]);
  55.     }
  56.     /**
  57.      * @Route("/search_by_string", methods="GET")
  58.      */
  59.     public function searchByString(Request $requestProductCreator $productCreator): JsonResponse
  60.     {
  61.         try {
  62.             $product $productCreator->createProduct(
  63.                 $request->query->get('brandName'),
  64.                 $request->query->get('article'),
  65.                 $request->query->get('description')
  66.             );
  67.             return new JsonResponse(ProductObject::getInfo($product));
  68.         } catch (OptimisticLockException ORMException $e) {
  69.             return new JsonResponse([]);
  70.         }
  71.     }
  72.     /**
  73.      * @Route("/search_by_api", methods="GET")
  74.      */
  75.     public function searchByApi(
  76.         Request $request,
  77.         ProductRepository $productRepository,
  78.         SupplierAPIParamRepository $supplierAPIParamRepository,
  79.         SupplierApiFactory $supplierApiFactoryService,
  80.         SupplierItemUpdater $supplierItemUpdaterService,
  81.     ): JsonResponse
  82.     {
  83.         foreach ($supplierAPIParamRepository->findAll() as $supplierAPIParam) {
  84.             $response $supplierApiFactoryService->searchProduct([
  85.                 'supplierAPIParam' => $supplierAPIParam,
  86.                 'oem' => $request->query->get('article'),
  87.             ]);
  88.             $supplierItemUpdaterService->executeUpdateSupplierItemByApi($supplierAPIParam->getSupplier(), $response);
  89.         }
  90.         $out = [];
  91.         foreach ($productRepository->findBy(['articleSearch' => NormalizerHelper::transformToArticleSearch($request->query->get('article'))]) as $product) {
  92.             $info ProductObject::getSupplierInfo($producttrue);
  93.             if(!$info) continue;
  94.             $out[] = $info;
  95.         }
  96.         return new JsonResponse($out);
  97.     }
  98.     /**
  99.      * @Route("/{product}/search_info", methods="GET")
  100.      */
  101.     public function searchInfo(Product $product): JsonResponse
  102.     {
  103.         return new JsonResponse(ProductObject::getInfo($product));
  104.     }
  105.     /**
  106.      * @Route("/{product}/change_description", methods="POST")
  107.      * @throws Exception
  108.      */
  109.     public function changeDescription(Request $requestProduct $productProductRepository $productRepository): JsonResponse
  110.     {
  111.         $productRepository->update($product->setDescription($request->request->get('description')));
  112.         return new JsonResponse(['result' => 'success']);
  113.     }
  114.     /**
  115.      * @Route("/{product}/change_storage_place", methods="POST")
  116.      * @throws Exception
  117.      */
  118.     public function changeStoragePlace(Request $requestProduct $productStorageRepository $storageRepository): JsonResponse
  119.     {
  120.         $storagePlace trim($request->request->get('storagePlace'));
  121.         $storage $storageRepository->findOneBy(['product' => $product]);
  122.         if($storage$storageRepository->update($storage->set($storagePlace));
  123.         else $storageRepository->save(new Storage($product$storagePlace));
  124.         return new JsonResponse(['result' => 'success']);
  125.     }
  126.     /**
  127.      * @Route("/item/{item}/change_price", methods="POST")
  128.      * @throws Exception
  129.      */
  130.     public function changeItemPrice(Request $requestItem $itemChangeItemPriceTransactionRepository $changeItemPriceTransactionRepositoryItemRepository $itemRepository): JsonResponse
  131.     {
  132.         $newPrice $request->request->getInt('price');
  133.         $changeItemPriceTransactionRepository->save(new ChangeItemPriceTransaction($item$this->findUserByCookie($request->cookies->get('user')), $newPrice));
  134.         $itemRepository->update($item->setPrice($newPrice));
  135.         return new JsonResponse(['result' => 'success']);
  136.     }
  137. }