diff --git a/retailcrm/lib/RetailcrmInventories.php b/retailcrm/lib/RetailcrmInventories.php new file mode 100644 index 00000000..7bc889f3 --- /dev/null +++ b/retailcrm/lib/RetailcrmInventories.php @@ -0,0 +1,83 @@ + + * @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL + * @license https://opensource.org/licenses/MIT The MIT License + * + * Don't forget to prefix your containers with your own identifier + * to avoid any conflicts with others containers. + */ + +class RetailcrmInventories +{ + /** + * @var \RetailcrmProxy|\RetailcrmApiClientV5 + */ + public static $api; + + /** + * Load stock from retailCRM + * + * @return mixed + */ + public static function loadStocks() + { + $page = 1; + + do { + $result = self::$api->storeInventories([], $page, 250); + + if (false === $result) { + return $result; + } + + foreach ($result['offers'] as $offer) { + self::setQuantityOffer($offer); + } + + $totalPageCount = $result['pagination']['totalPageCount']; + ++$page; + } while ($page <= $totalPageCount); + } + + private static function setQuantityOffer($offer) + { + if (isset($offer['externalId'])) { + $invOffer = explode('#', $offer['externalId']); + + if (isset($invOffer[1])) { + StockAvailable::setQuantity($invOffer[0], $invOffer[1], $offer['quantity']); + } else { + StockAvailable::setQuantity($offer['externalId'], 0, $offer['quantity']); + } + } + } +}