src/Controller/CashBox/SearchController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller\CashBox;
  3. use App\Repository\GtinRepository;
  4. use App\Repository\ProductRepository;
  5. use App\Service\CashBox\PriceService;
  6. use App\Service\CashBox\ProductService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class SearchController extends AbstractController
  12. {
  13.     private PriceService $price_service;
  14.     /**
  15.      * SearchController constructor.
  16.      */
  17.     public function __construct()
  18.     {
  19.         $this->price_service = new PriceService();
  20.     }
  21.     /**
  22.      * @Route("/cashbox_search", name="cashbox_search_index")
  23.      */
  24.     public function index(Request $requestProductService $product_serviceGtinRepository $gtin_repositoryProductRepository $product_repository): Response
  25.     {
  26.         $success false;
  27.         $ret = [];
  28.         $data json_decode($request->getContent(), true);
  29.         if (isset($data['search_string'])) {
  30.             $search_result $gtin_repository->findBy(['gtin' => $data['search_string']]);
  31.             if (null !== $search_result) {
  32.                 $success true;
  33.                 foreach ($search_result as $gtin) {
  34.                     $_p $gtin->getProducts();
  35.                     foreach ($_p as $product) {
  36.                         if (!isset($ret[$product->getId()]) && $product->getStatus()===true) {
  37.                             $ret[$product->getId()] = $product_service->buildFrontendProductFromProduct($product);
  38.                         }
  39.                     }
  40.                 }
  41.             }
  42.             $search_result=$product_repository->findByNameLike($data['search_string']);
  43.             foreach ($search_result as $product){
  44.                 if (!isset($ret[$product->getId()]) && $product->getStatus()===true) {
  45.                     $ret[$product->getId()] = $product_service->buildFrontendProductFromProduct($product);
  46.                 }
  47.             }
  48.             $search_result=$product_repository->findByModelLike($data['search_string']);
  49.             foreach ($search_result as $product){
  50.                 if (!isset($ret[$product->getId()]) && $product->getStatus()===true) {
  51.                     $ret[$product->getId()] = $product_service->buildFrontendProductFromProduct($product);
  52.                 }
  53.             }
  54.         }
  55.         return $this->json(['_success' => $success'products' => $ret'result_count' => count($ret)]);
  56.     }
  57. }