<?php
namespace App\Controller;
use App\Entity\Combination;
use App\Entity\Configuration;
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use App\Services\PrestaShopWebservice;
use App\Services\PrestaShopWebserviceException;
use SimpleXMLElement;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class StockController extends AbstractController
{
public function import(): Response
{
$config = $this->getDoctrine()->getRepository(Configuration::class)->findAll()[0];
//Request xml to server
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $config->getFromApiUrl() . '/ConsultaStocks_B2C');
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_FAILONERROR, 1);
curl_setopt($request, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_TIMEOUT, 3600);
curl_setopt($request, CURLOPT_HTTPHEADER, array(
'Content-Length : 0'
));
if ($config->getFromApiKey() != null) {
curl_setopt($request, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $config->getFromApiKey()
));
}
$response = curl_exec($request);
curl_close($request);
try {
//Convert response to xml
$response = new SimpleXMLElement($response);
//Convert xml to array
$response = simplexml_load_string($response);
ini_set('max_execution_time', '3600');
//Loop through stocks
foreach ($response as $stock) {
$product = $this->getDoctrine()->getRepository(Product::class)->findOneBy(['idProduct' => (int)$stock->id_product]);
if ($product != null) {
$combination = $this->getDoctrine()->getRepository(Combination::class)->findOneBy(['product' => $product->getId()]);
if ($combination != null) {
$combination->setStock((int)$stock->quantity);
$this->getDoctrine()->getManager()->flush();
}
}
}
} catch (Exception $e) {
//Request xml to server
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $config->getFromApiUrl() . '/ConsultaStocks_B2C');
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_FAILONERROR, 1);
curl_setopt($request, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_TIMEOUT, 3600);
if ($config->getFromApiKey() != null) {
curl_setopt($request, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $config->getFromApiKey()
));
}
$response = curl_exec($request);
curl_close($request);
try {
//Convert response to xml
$response = new SimpleXMLElement($response);
//Convert xml to array
$response = simplexml_load_string($response);
ini_set('max_execution_time', '3600');
//Loop through stocks
foreach ($response as $stock) {
$product = $this->getDoctrine()->getRepository(Product::class)->findOneBy(['idProduct' => (int)$stock->id_product]);
if ($product != null) {
$combination = $this->getDoctrine()->getRepository(Combination::class)->findOneBy(['product' => $product->getId()]);
if ($combination != null) {
$combination->setStock((int)$stock->quantity);
$this->getDoctrine()->getManager()->flush();
}
}
}
} catch (Exception $e) {
$logFile = fopen("log.txt", 'a') or die("Error creando archivo");
fwrite($logFile, "\n" . date("d/m/Y H:i:s") . $e->getMessage()) or die("Error escribiendo en el archivo");
fclose($logFile);
return new JsonResponse(false);
}
}
return new JsonResponse(true);
}
public function export(EntityManagerInterface $em)
{
$config = $this->getDoctrine()->getRepository(Configuration::class)->findAll()[0];
// $combinations = $this->getDoctrine()->getRepository(Combination::class)->findAll();
$combinations = $em->createQueryBuilder()->select('c')->from('App\Entity\Combination', 'c')->where('c.idPrestaShop IS NOT NULL')->getQuery()->getResult();
try {
$ws = new PrestaShopWebservice($config->getToApiUrl(), $config->getToApiKey());
foreach ($combinations as $combination) {
$xml = $ws->get(array('resource' => 'stock_availables', 'display' => 'full', 'filter[id_product]' => $combination->getProduct()->getIdPrestaShop(), 'filter[id_product_attribute]' => $combination->getIdPrestaShop()));
$resources = $xml->children()->children()[0];
$stock_a_modificar = intval($resources->id);
$xml = $ws->get(array('url' => $config->getToApiUrl() . '/api/stock_availables?schema=blank'));
$resources = $xml->children()->children();
$resources->id = $stock_a_modificar;
$resources->id_product = $combination->getProduct()->getIdPrestaShop();
$resources->id_shop = 1;
$resources->id_shop_group = 0;
$resources->quantity = $combination->getStock();
$resources->out_of_stock = 0;
$resources->depends_on_stock = 0;
$resources->id_product_attribute = $combination->getIdPrestaShop();
$putXml = $xml->asXML();
$xml = $ws->edit(array('resource' => 'stock_availables', 'id' => $stock_a_modificar, 'putXml' => $putXml));
}
} catch (Exception $e) {
$logFile = fopen("log.txt", 'a') or die("Error creando archivo");
fwrite($logFile, "\n" . date("d/m/Y H:i:s") . $e->getMessage()) or die("Error escribiendo en el archivo");
fclose($logFile);
}
return new JsonResponse(true);
}
}