<?php
namespace App\Controller;
use App\Entity\ChangeItemPriceTransaction;
use App\Entity\Item;
use App\Entity\Product;
use App\Entity\Storage;
use App\Repository\ChangeItemPriceTransactionRepository;
use App\Repository\ItemRepository;
use App\Repository\ProductRepository;
use App\Repository\StorageRepository;
use App\Repository\SupplierAPIParamRepository;
use App\Service\Helper\NormalizerHelper;
use App\Service\Product\ProductCreator;
use App\Service\Product\ProductObject;
use App\Service\Product\SupplierItemUpdater;
use App\Service\Supplier\SupplierApiFactory;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/product")
*/
class ProductController extends BaseController
{
/**
* @Route("/search_list", methods="GET")
*/
public function list(Request $request, ProductRepository $productRepository): JsonResponse
{
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')))
);
}
/**
* @Route("/search_items", methods="GET")
*/
public function searchItems(Request $request, ProductRepository $productRepository): JsonResponse
{
return new JsonResponse([
'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'))),
'count' => $productRepository->findByStringCount($request->query->get('query', ''))
]);
}
/**
* @Route("/search_store_items", methods="GET")
*/
public function searchStoreItems(Request $request, ProductRepository $productRepository): JsonResponse
{
return new JsonResponse([
'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'))),
'count' => $productRepository->findByStringCount($request->query->get('query', ''))
]);
}
/**
* @Route("/search_by_string", methods="GET")
*/
public function searchByString(Request $request, ProductCreator $productCreator): JsonResponse
{
try {
$product = $productCreator->createProduct(
$request->query->get('brandName'),
$request->query->get('article'),
$request->query->get('description')
);
return new JsonResponse(ProductObject::getInfo($product));
} catch (OptimisticLockException | ORMException $e) {
return new JsonResponse([]);
}
}
/**
* @Route("/search_by_api", methods="GET")
*/
public function searchByApi(
Request $request,
ProductRepository $productRepository,
SupplierAPIParamRepository $supplierAPIParamRepository,
SupplierApiFactory $supplierApiFactoryService,
SupplierItemUpdater $supplierItemUpdaterService,
): JsonResponse
{
foreach ($supplierAPIParamRepository->findAll() as $supplierAPIParam) {
$response = $supplierApiFactoryService->searchProduct([
'supplierAPIParam' => $supplierAPIParam,
'oem' => $request->query->get('article'),
]);
$supplierItemUpdaterService->executeUpdateSupplierItemByApi($supplierAPIParam->getSupplier(), $response);
}
$out = [];
foreach ($productRepository->findBy(['articleSearch' => NormalizerHelper::transformToArticleSearch($request->query->get('article'))]) as $product) {
$info = ProductObject::getSupplierInfo($product, true);
if(!$info) continue;
$out[] = $info;
}
return new JsonResponse($out);
}
/**
* @Route("/{product}/search_info", methods="GET")
*/
public function searchInfo(Product $product): JsonResponse
{
return new JsonResponse(ProductObject::getInfo($product));
}
/**
* @Route("/{product}/change_description", methods="POST")
* @throws Exception
*/
public function changeDescription(Request $request, Product $product, ProductRepository $productRepository): JsonResponse
{
$productRepository->update($product->setDescription($request->request->get('description')));
return new JsonResponse(['result' => 'success']);
}
/**
* @Route("/{product}/change_storage_place", methods="POST")
* @throws Exception
*/
public function changeStoragePlace(Request $request, Product $product, StorageRepository $storageRepository): JsonResponse
{
$storagePlace = trim($request->request->get('storagePlace'));
$storage = $storageRepository->findOneBy(['product' => $product]);
if($storage) $storageRepository->update($storage->set($storagePlace));
else $storageRepository->save(new Storage($product, $storagePlace));
return new JsonResponse(['result' => 'success']);
}
/**
* @Route("/item/{item}/change_price", methods="POST")
* @throws Exception
*/
public function changeItemPrice(Request $request, Item $item, ChangeItemPriceTransactionRepository $changeItemPriceTransactionRepository, ItemRepository $itemRepository): JsonResponse
{
$newPrice = $request->request->getInt('price');
$changeItemPriceTransactionRepository->save(new ChangeItemPriceTransaction($item, $this->findUserByCookie($request->cookies->get('user')), $newPrice));
$itemRepository->update($item->setPrice($newPrice));
return new JsonResponse(['result' => 'success']);
}
}