<?php
namespace App\Controller\CashBox;
use App\Repository\GtinRepository;
use App\Repository\ProductRepository;
use App\Service\CashBox\PriceService;
use App\Service\CashBox\ProductService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SearchController extends AbstractController
{
private PriceService $price_service;
/**
* SearchController constructor.
*/
public function __construct()
{
$this->price_service = new PriceService();
}
/**
* @Route("/cashbox_search", name="cashbox_search_index")
*/
public function index(Request $request, ProductService $product_service, GtinRepository $gtin_repository, ProductRepository $product_repository): Response
{
$success = false;
$ret = [];
$data = json_decode($request->getContent(), true);
if (isset($data['search_string'])) {
$search_result = $gtin_repository->findBy(['gtin' => $data['search_string']]);
if (null !== $search_result) {
$success = true;
foreach ($search_result as $gtin) {
$_p = $gtin->getProducts();
foreach ($_p as $product) {
if (!isset($ret[$product->getId()]) && $product->getStatus()===true) {
$ret[$product->getId()] = $product_service->buildFrontendProductFromProduct($product);
}
}
}
}
$search_result=$product_repository->findByNameLike($data['search_string']);
foreach ($search_result as $product){
if (!isset($ret[$product->getId()]) && $product->getStatus()===true) {
$ret[$product->getId()] = $product_service->buildFrontendProductFromProduct($product);
}
}
$search_result=$product_repository->findByModelLike($data['search_string']);
foreach ($search_result as $product){
if (!isset($ret[$product->getId()]) && $product->getStatus()===true) {
$ret[$product->getId()] = $product_service->buildFrontendProductFromProduct($product);
}
}
}
return $this->json(['_success' => $success, 'products' => $ret, 'result_count' => count($ret)]);
}
}