<?php
namespace Abcpremium\Magento2PimcoreBundle\Controller\Rest;
use Abcpremium\Magento2PimcoreBundle\Model\Api\Connector\SimpleProduct;
use Abcpremium\Magento2PimcoreBundle\Model\Configuration;
use Abcpremium\Magento2PimcoreBundle\Model\Log\Logger;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\HttpFoundation\Request;
class OrderQuantity extends FrontendController
{
public function requestAction(Request $request)
{
$configurationModel = new Configuration();
$content = json_decode($request->getContent(), true);
$host = $content['config']['base_url'];
$mapping = null;
if (($content['config']['use_mapping'] ?? false) === true) {
$lines = explode(PHP_EOL, $content['config']['mapping']);
foreach ($lines as $line) {
$parts = array_map('trim', explode(',', $line));
$mapping[$parts[0]] = $parts[1];
}
}
if ($host === 'localhost') {
$entry = $configurationModel->getByHost('https://pimcore.abcpremium.de/');
} else {
if (str_contains($host, 'http') === false) {
$host = 'https://' . $host . '/';
}
$entry = $configurationModel->getByHost($host);
}
if (empty($entry) && $host !== 'localhost') {
return $this->json(["authenticated" => false]);
}
$authToken = $request->headers->all('Authorization');
if (count($authToken) === 0) {
return $this->json(["authenticated" => false]);
} else {
$authToken = str_replace('Bearer ', '', $authToken[0]);
}
$token = $entry->getAccess_token();
if ($authToken !== $token) {
return $this->json(["authenticated" => false]);
}
foreach ($content['skus'] as $sku => $qty) {
$dataObject = self::getClassInstance($entry);
$objects = $dataObject::getByArtikelNr($sku);
foreach ($objects as $object) {
$object = self::getLatestVersion($object);
$currentQty = $object->get($content['config']['qty_field']);
/*if ((int)$currentQty === 0) {
continue;
}*/
if ($currentQty < 0) {
$currentQty = 0;
}
if ($content['config']['action'] === 'add') {
$newQty = $currentQty + $qty;
} else {
$newQty = $currentQty - $qty;
}
if ($newQty < 0) {
$newQty = 0;
}
$object->set($content['config']['qty_field'], $newQty);
$object->save();
$transferQty = $newQty;
$webshops = $object->getWebshops();
foreach ($webshops as $webshop) {
if ($webshop === $host) {
continue;
}
$entry = $configurationModel->getByHost($webshop);
if (empty($entry)) {
Logger::toProductLog('Could not find the configuration entry of host ' . $webshop);
continue;
}
if ($mapping !== null && array_key_exists($webshop, $mapping)) {
$pimcoreQtyField = $mapping[$webshop];
$transferQty = $object->get($pimcoreQtyField);
}
$apiConfig = [
'host' => $webshop,
'access_token' => $entry->getAccess_token(),
'store_code' => 'all'
];
$body = [
'extension_attributes' => [
'stock_item' => [
'qty' => $transferQty
]
],
'sku' => $sku
];
$response = SimpleProduct::update($apiConfig, $body);
if (empty($response['id'])) {
Logger::toProductLog('Could not set the quantity (' . $transferQty . ') of product ' . $sku . ' in webshop ' . $webshop);
}
}
}
}
return $this->json(["success" => true]);
}
private static function getClassInstance($entry)
{
$productClass = "\Pimcore\Model\DataObject\\" . $entry->getProduct_class();
return new $productClass();
}
private static function getLatestVersion($object)
{
$latestVersion = $object->getLatestVersion();
if (null === $latestVersion) {
return $object;
}
$latestVersionObject = $latestVersion->getData();
if (!$latestVersionObject instanceof Concrete) {
return $object;
}
return $latestVersionObject;
}
}