<?php
namespace App\Controller\WB;
use App\Entity\WBOrder;
use App\Entity\WBOrderItem;
use App\Entity\WildberriesClient;
use App\Entity\WildberriesProduct;
use App\Service\WB\PriceListXmlService;
use App\Service\WB\Type\WildberriesSellerPostingStatusType;
use App\Service\WB\WildberriesPostingService;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class ApiController.
*/
class ApiController
{
/** @var EntityManagerInterface */
protected EntityManagerInterface $em;
/** @var LoggerInterface */
protected $logger;
protected PriceListXmlService $xmlService;
protected ContainerInterface $ci;
private WildberriesPostingService $postingService;
/**
* ApiController constructor.
*
* @param EntityManagerInterface $em
* @param LoggerInterface $logger
*/
public function __construct(
EntityManagerInterface $em,
LoggerInterface $logger,
PriceListXmlService $xmlService,
WildberriesPostingService $postingService,
ContainerInterface $ci
)
{
$this->em = $em;
$this->logger = $logger;
$this->xmlService = $xmlService;
$this->postingService = $postingService;
$this->ci = $ci;
}
/**
* @Route(
* "/wb/feed/{clientId}",
* name="wb_export",
* host="%host.monitoring%",
* methods={"GET"}
* )
*
* @return Response
* @throws \Exception
*/
public function wbApiExportAction(int $clientId): Response
{
$filePath = $this->ci->getParameter('upload_dir').'/wb/feed-' . $clientId . '.xml';
$data = '';
if (is_file($filePath)) {
$data = file_get_contents($filePath);
}
$response = new StreamedResponse(function () use ($data) {
echo $data;
});
$disposition = HeaderUtils::makeDisposition( HeaderUtils::DISPOSITION_ATTACHMENT,
sprintf("feed-%s.xml", $clientId));
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'application/xml;charset=UTF-8');
return $response;
}
/**
* @Route(
* "/wb/rid/{rid}",
* name="wb_get_order_id",
* host="%host.monitoring%",
* methods={"GET"}
* )
*
* @return Response
* @throws \Exception
*/
public function wbApiGetOrderIdByRid(string $rid)
{
/** @var WBOrderItem $orderItem */
$orderItem = $this->em->getRepository(WBOrderItem::class)->findOneBy(['rid' => $rid]);
if (!$orderItem) {
return new JsonResponse(['error' => 'rid not found']);
}
return new JsonResponse(['orderId' => $orderItem->getOrder()->getOrderId()]);
}
/**
* @Route(
* "/wb/order/{orderId}/receive",
* name="wb_receive_order_id",
* host="%host.monitoring%",
* methods={"GET"}
* )
*
* @return Response
* @throws \Exception
*/
public function wbApiReceiveOrderIdByRid(string $orderId)
{
/** @var WBOrder $orderItem */
$order = $this->em->getRepository(WBOrder::class)->findOneBy(['omsId' => $orderId]);
if (!$order) {
return new JsonResponse(['error' => 'order not found']);
}
if ($this->postingService->apiReceiveWildberriesPosting($order->getClient(), $order->getOrderId())) {
$order->setStatus(WildberriesSellerPostingStatusType::DELIVER);
$this->em->flush();
}
return new JsonResponse(['success' => true]);
}
/**
* @Route(
* "/wb/get/clients",
* name="wb_get_clients",
* host="%host.monitoring%",
* methods={"GET"}
* )
*
* @return Response
* @throws \Exception
*/
public function wbApiGetClientsAction()
{
$clients = $this->em->getRepository(WildberriesClient::class)->findClients();
if (!$clients) {
return new JsonResponse(['error' => 'clients not found']);
}
return new JsonResponse($clients);
}
/**
* @Route(
* "/wb/get/client/{clientId}/products",
* name="wb_get_client_products",
* host="%host.monitoring%",
* methods={"GET"}
* )
*
* @return Response
* @throws \Exception
*/
public function wbApiGetClientProductsAction(int $clientId)
{
$clients = $this->em->getRepository(WildberriesProduct::class)->findProductsInfoByClient($clientId);
if (!$clients) {
return new JsonResponse(['error' => 'clients not found']);
}
return new JsonResponse($clients);
}
}