180 lines
6.4 KiB
PHP
180 lines
6.4 KiB
PHP
<?php
|
|
class StockAvailable extends StockAvailableCore
|
|
{
|
|
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public $enable_redis = false;
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public $use_cache_for_stock_manager = true;
|
|
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public function __construct($id = null, $id_lang = null, $id_shop = null, $translator = null) {
|
|
parent::__construct($id, $id_lang, $id_shop, $translator);
|
|
if (_PS_CACHE_ENABLED_) {
|
|
$caching_system = _PS_CACHING_SYSTEM_;
|
|
if (
|
|
$caching_system === 'Redis'
|
|
&& class_exists(\Teamwant_Redis::class)
|
|
) {
|
|
$this->enable_redis = true;
|
|
$this->use_cache_for_stock_manager = \Teamwant_Redis::getCacheConfiguration()['use_cache_for_stock_manager'];
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
private static function getUseCacheForStockManager() {
|
|
if (_PS_CACHE_ENABLED_) {
|
|
$caching_system = _PS_CACHING_SYSTEM_;
|
|
if (
|
|
$caching_system === 'Redis'
|
|
&& class_exists(\Teamwant_Redis::class)
|
|
) {
|
|
return (bool) \Teamwant_Redis::getCacheConfiguration()['use_cache_for_stock_manager'];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public static function getStockAvailableIdByProductId($id_product, $id_product_attribute = null, $id_shop = null)
|
|
{
|
|
if (!Validate::isUnsignedId($id_product)) {
|
|
return false;
|
|
}
|
|
$query = new DbQuery();
|
|
$query->select('id_stock_available');
|
|
$query->from('stock_available');
|
|
$query->where('id_product = ' . (int) $id_product);
|
|
if ($id_product_attribute !== null) {
|
|
$query->where('id_product_attribute = ' . (int) $id_product_attribute);
|
|
}
|
|
$query = StockAvailable::addSqlShopRestriction($query, $id_shop);
|
|
if (($isEnabled = self::getUseCacheForStockManager()) !== null) {
|
|
return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query, $isEnabled);
|
|
}
|
|
return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
|
}
|
|
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public static function dependsOnStock($id_product, $id_shop = null)
|
|
{
|
|
if (!Validate::isUnsignedId($id_product)) {
|
|
return false;
|
|
}
|
|
$query = new DbQuery();
|
|
$query->select('depends_on_stock');
|
|
$query->from('stock_available');
|
|
$query->where('id_product = ' . (int) $id_product);
|
|
$query->where('id_product_attribute = 0');
|
|
$query = StockAvailable::addSqlShopRestriction($query, $id_shop);
|
|
if (($isEnabled = self::getUseCacheForStockManager()) !== null) {
|
|
return (bool) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query, $isEnabled);
|
|
}
|
|
return (bool) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
|
}
|
|
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public static function outOfStock($id_product, $id_shop = null)
|
|
{
|
|
if (!Validate::isUnsignedId($id_product)) {
|
|
return false;
|
|
}
|
|
$query = new DbQuery();
|
|
$query->select('out_of_stock');
|
|
$query->from('stock_available');
|
|
$query->where('id_product = ' . (int) $id_product);
|
|
$query->where('id_product_attribute = 0');
|
|
$query = StockAvailable::addSqlShopRestriction($query, $id_shop);
|
|
if (($isEnabled = self::getUseCacheForStockManager()) !== null) {
|
|
return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query, $isEnabled);
|
|
}
|
|
return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
|
}
|
|
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public static function getLocation($id_product, $id_product_attribute = null, $id_shop = null)
|
|
{
|
|
$id_product = (int) $id_product;
|
|
if (null === $id_product_attribute) {
|
|
$id_product_attribute = 0;
|
|
} else {
|
|
$id_product_attribute = (int) $id_product_attribute;
|
|
}
|
|
$query = new DbQuery();
|
|
$query->select('location');
|
|
$query->from('stock_available');
|
|
$query->where('id_product = ' . $id_product);
|
|
$query->where('id_product_attribute = ' . $id_product_attribute);
|
|
$query = StockAvailable::addSqlShopRestriction($query, $id_shop);
|
|
if (($isEnabled = self::getUseCacheForStockManager()) !== null) {
|
|
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query, $isEnabled);
|
|
}
|
|
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
|
}
|
|
|
|
/*
|
|
* module: teamwant_redis
|
|
* date: 2024-06-10 21:12:21
|
|
* version: 2.1.1
|
|
*/
|
|
public static function getQuantityAvailableByProduct($id_product = null, $id_product_attribute = null, $id_shop = null)
|
|
{
|
|
$isEnableCache = true;
|
|
if ($id_product_attribute === null) {
|
|
$id_product_attribute = 0;
|
|
}
|
|
$key = 'StockAvailable::getQuantityAvailableByProduct_' . (int) $id_product . '-' . (int) $id_product_attribute . '-' . (int) $id_shop;
|
|
if (($isEnabled = self::getUseCacheForStockManager()) !== null) {
|
|
$isEnableCache = $isEnabled;
|
|
}
|
|
if (!Cache::isStored($key) || !$isEnableCache) {
|
|
$query = new DbQuery();
|
|
$query->select('SUM(quantity)');
|
|
$query->from('stock_available');
|
|
if ($id_product !== null) {
|
|
$query->where('id_product = ' . (int) $id_product);
|
|
}
|
|
$query->where('id_product_attribute = ' . (int) $id_product_attribute);
|
|
$query = StockAvailable::addSqlShopRestriction($query, $id_shop);
|
|
$result = (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query, $isEnableCache);
|
|
if ($isEnableCache) {
|
|
Cache::store($key, $result);
|
|
}
|
|
return $result;
|
|
}
|
|
return Cache::retrieve($key);
|
|
}
|
|
}
|