<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ProductController extends AbstractController
{
// public function searchForm(Request $request):Response
// {
// $error = null;
// $products = [];
// return $this->render('product/search.html.twig', [
// 'error' => $error,
// 'products' => $products,
// 'last_article' => $request->request->get('Article', ''),
// 'last_api_key' => $request->request->get('api_key', ''),
// ]);
// }
public function searchByArticle(Request $request):Response
{
$error = null;
$products = [];
if ($request->isMethod('POST')) {
// Получение данных из формы
$articleNumber = $request->request->get('Article');
$apiKey = $request->request->get('api_key'); // Получаем api_key от пользователя
if (!$articleNumber || !$apiKey) {
$error = 'Параметры "Article" и "api_key" обязательны.';
} else {
// Инициализация cURL
$ch1 = curl_init();
// 1. Получить все возможные бренды для заданного номера
$fields = array("JSONparameter" => json_encode(['Article' => $articleNumber]));
$url = "http://api.tmparts.ru/api/ArticleBrandList?" . http_build_query($fields);
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Authorization: Bearer ' . $apiKey,
];
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch1);
$artList = json_decode($response, true);
curl_close($ch1);
if (isset($artList['Message']) && $artList['Message'] != "") {
$error = $artList['Message'];
} elseif ($artList['BrandList'] == null) {
$error = 'Номер не найден.';
} else {
// 2. По каждому найденному бренду выполнить проценку
$products = [];
foreach ($artList['BrandList'] as $brand) {
// Инициализация cURL для второго запроса
$ch = curl_init();
$fields = array("JSONparameter" => json_encode([
'Brand' => $brand['BrandName'],
'Article' => $artList['Article'],
'is_main_warehouse' => 0,
]));
$url = "http://api.tmparts.ru/api/StockByArticle?" . http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Authorization: Bearer ' . $apiKey,
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$artListWithPrices = json_decode($response, true);
curl_close($ch);
// Обработка и добавление результатов в $products
foreach ($artListWithPrices as $item) {
if (isset($item['warehouse_offers']) && is_array($item['warehouse_offers'])) {
foreach ($item['warehouse_offers'] as $offer) {
$products[] = [
'brand' => $item['brand'] ?? '',
'article' => $item['article'] ?? '',
'name' => $item['article_name'] ?? '',
'quantity' => $offer['quantity'] ?? 0,
'price' => $offer['price'] ?? 0,
'delivery_duration' => $offer['delivery_period'] ?? 0,
'vendorId' => $offer['id'] ?? '',
'warehouseAlias' => $offer['warehouse_code'] ?? '',
'warehouseName' => $offer['warehouse_name'] ?? '',
'branchName' => $offer['branch_name'] ?? '',
];
}
}
}
// Рендеринг шаблона формы поиска с результатами (если есть)
return $this->render('product/search.html.twig', [
'error' => $error,
'products' => $products,
'last_article' => $request->request->get('Article', ''),
'last_api_key' => $request->request->get('api_key', ''),
]);
}
}
}
}
// Рендеринг шаблона формы поиска с результатами (если есть)
return $this->render('product/search.html.twig', [
'error' => $error,
'products' => $products,
'last_article' => $request->request->get('Article', ''),
'last_api_key' => $request->request->get('api_key', ''),
]);
}
}
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
product_search:
path: /product/search
controller: App\Controller\ProductController::searchByArticle
methods: [GET, POST]
{% extends 'base.html.twig' %}
{% block title %}Поиск товара{% endblock %}
{% block body %}
<h1>Поиск товара по артикулу</h1>
{% if error %}
<div style="color: red;">
<p>{{ error }}</p>
</div>
{% endif %}
<form method="post" action="{{ path('product_search') }}">
<div>
<label for="article">Артикул:</label>
<input type="text" id="article" name="Article" value="{{ last_article }}" required>
</div>
<div>
<label for="api_key">API Key:</label>
<input type="text" id="api_key" name="api_key" value="{{ last_api_key }}" required>
</div>
<button type="submit">Искать</button>
</form>
{% if products is not empty %}
<h2>Результаты поиска</h2>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Бренд</th>
<th>Артикул</th>
<th>Название</th>
<th>Количество</th>
<th>Цена</th>
<th>Время доставки (дней)</th>
<th>Код товара</th>
<th>Код склада</th>
<th>Склад</th>
<th>Филиал</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product.brand }}</td>
<td>{{ product.article }}</td>
<td>{{ product.name }}</td>
<td>{{ product.quantity }}</td>
<td>{{ product.price }}</td>
<td>{{ product.delivery_duration }}</td>
<td>{{ product.vendorId }}</td>
<td>{{ product.warehouseAlias }}</td>
<td>{{ product.warehouseName }}</td>
<td>{{ product.branchName }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% elseif products is empty %}
<p>Товары не найдены.</p>
{% endif %}
{% endblock %}