src/Controller/StockController.php line 106

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Combination;
  4. use App\Entity\Configuration;
  5. use App\Entity\Product;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Exception;
  8. use App\Services\PrestaShopWebservice;
  9. use App\Services\PrestaShopWebserviceException;
  10. use SimpleXMLElement;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class StockController extends AbstractController
  15. {
  16.     public function import(): Response
  17.     {
  18.         $config $this->getDoctrine()->getRepository(Configuration::class)->findAll()[0];
  19.         //Request xml to server
  20.         $request curl_init();
  21.         curl_setopt($requestCURLOPT_URL$config->getFromApiUrl() . '/ConsultaStocks_B2C');
  22.         curl_setopt($requestCURLOPT_POST1);
  23.         curl_setopt($requestCURLOPT_FAILONERROR1);
  24.         curl_setopt($requestCURLOPT_FOLLOWLOCATION1);
  25.         curl_setopt($requestCURLOPT_RETURNTRANSFER1);
  26.         curl_setopt($requestCURLOPT_TIMEOUT3600);
  27.         curl_setopt($requestCURLOPT_HTTPHEADER, array(
  28.             'Content-Length : 0'
  29.         ));
  30.         if ($config->getFromApiKey() != null) {
  31.             curl_setopt($requestCURLOPT_HTTPHEADER, array(
  32.                 'Authorization: ' $config->getFromApiKey()
  33.             ));
  34.         }
  35.         $response curl_exec($request);
  36.         curl_close($request);
  37.         try {
  38.             //Convert response to xml
  39.             $response = new SimpleXMLElement($response);
  40.             //Convert xml to array
  41.             $response simplexml_load_string($response);
  42.             ini_set('max_execution_time''3600');
  43.             //Loop through stocks
  44.             foreach ($response as $stock) {
  45.                 $product $this->getDoctrine()->getRepository(Product::class)->findOneBy(['idProduct' => (int)$stock->id_product]);
  46.                 if ($product != null) {
  47.                     $combination $this->getDoctrine()->getRepository(Combination::class)->findOneBy(['product' => $product->getId()]);
  48.                     if ($combination != null) {
  49.                         $combination->setStock((int)$stock->quantity);
  50.                         $this->getDoctrine()->getManager()->flush();
  51.                     }
  52.                 }
  53.             }
  54.         } catch (Exception $e) {
  55.             //Request xml to server
  56.             $request curl_init();
  57.             curl_setopt($requestCURLOPT_URL$config->getFromApiUrl() . '/ConsultaStocks_B2C');
  58.             curl_setopt($requestCURLOPT_POST1);
  59.             curl_setopt($requestCURLOPT_FAILONERROR1);
  60.             curl_setopt($requestCURLOPT_FOLLOWLOCATION1);
  61.             curl_setopt($requestCURLOPT_RETURNTRANSFER1);
  62.             curl_setopt($requestCURLOPT_TIMEOUT3600);
  63.             if ($config->getFromApiKey() != null) {
  64.                 curl_setopt($requestCURLOPT_HTTPHEADER, array(
  65.                     'Authorization: ' $config->getFromApiKey()
  66.                 ));
  67.             }
  68.             $response curl_exec($request);
  69.             curl_close($request);
  70.             try {
  71.                 //Convert response to xml
  72.                 $response = new SimpleXMLElement($response);
  73.                 //Convert xml to array
  74.                 $response simplexml_load_string($response);
  75.                 ini_set('max_execution_time''3600');
  76.                 //Loop through stocks
  77.                 foreach ($response as $stock) {
  78.                     $product $this->getDoctrine()->getRepository(Product::class)->findOneBy(['idProduct' => (int)$stock->id_product]);
  79.                     if ($product != null) {
  80.                         $combination $this->getDoctrine()->getRepository(Combination::class)->findOneBy(['product' => $product->getId()]);
  81.                         if ($combination != null) {
  82.                             $combination->setStock((int)$stock->quantity);
  83.                             $this->getDoctrine()->getManager()->flush();
  84.                         }
  85.                     }
  86.                 }
  87.             } catch (Exception $e) {
  88.                 $logFile fopen("log.txt"'a') or die("Error creando archivo");
  89.                 fwrite($logFile"\n" date("d/m/Y H:i:s") . $e->getMessage()) or die("Error escribiendo en el archivo");
  90.                 fclose($logFile);
  91.                 return new JsonResponse(false);
  92.             }
  93.         }
  94.         return new JsonResponse(true);
  95.     }
  96.     public function export(EntityManagerInterface $em)
  97.     {
  98.         $config $this->getDoctrine()->getRepository(Configuration::class)->findAll()[0];
  99.         // $combinations = $this->getDoctrine()->getRepository(Combination::class)->findAll();
  100.         $combinations $em->createQueryBuilder()->select('c')->from('App\Entity\Combination''c')->where('c.idPrestaShop IS NOT NULL')->getQuery()->getResult();
  101.         try {
  102.             $ws = new PrestaShopWebservice($config->getToApiUrl(), $config->getToApiKey());
  103.             foreach ($combinations as $combination) {
  104.                 $xml $ws->get(array('resource' => 'stock_availables''display' => 'full''filter[id_product]' => $combination->getProduct()->getIdPrestaShop(), 'filter[id_product_attribute]' => $combination->getIdPrestaShop()));
  105.                 $resources $xml->children()->children()[0];
  106.                 $stock_a_modificar intval($resources->id);
  107.                 $xml $ws->get(array('url' => $config->getToApiUrl() . '/api/stock_availables?schema=blank'));
  108.                 $resources $xml->children()->children();
  109.                 $resources->id $stock_a_modificar;
  110.                 $resources->id_product $combination->getProduct()->getIdPrestaShop();
  111.                 $resources->id_shop 1;
  112.                 $resources->id_shop_group 0;
  113.                 $resources->quantity $combination->getStock();
  114.                 $resources->out_of_stock 0;
  115.                 $resources->depends_on_stock 0;
  116.                 $resources->id_product_attribute $combination->getIdPrestaShop();
  117.                 $putXml $xml->asXML();
  118.                 $xml $ws->edit(array('resource' => 'stock_availables''id' => $stock_a_modificar'putXml' => $putXml));
  119.             }
  120.         } catch (Exception $e) {
  121.             $logFile fopen("log.txt"'a') or die("Error creando archivo");
  122.             fwrite($logFile"\n" date("d/m/Y H:i:s") . $e->getMessage()) or die("Error escribiendo en el archivo");
  123.             fclose($logFile);
  124.         }
  125.         return new JsonResponse(true);
  126.     }
  127. }