46 lines
1.3 KiB
PHP
46 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\ZNZBundle\Query;
|
|
|
|
use Doctrine\DBAL\Connection;
|
|
use External\ZNZBundle\Util\ZNZUtil;
|
|
use KupShop\KupShopBundle\Context\LanguageContext;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
|
|
class Product
|
|
{
|
|
public const STORES_CACHE_KEY = 'store_StoreIdsZNZ';
|
|
|
|
public static function getInStoreField($useVariations = true, $qb = null): string
|
|
{
|
|
$storeIds = static::getStoreIds(
|
|
Contexts::get(LanguageContext::class)->getActiveId()
|
|
);
|
|
|
|
if ($qb && !empty($storeIds)) {
|
|
if ($qb->isAliasPresent('pv') || empty($qb->getQueryPart('select'))) {
|
|
$qb->leftJoin('pv', 'stores_items', 'znz_si', 'znz_si.id_product = p.id AND pv.id <=> znz_si.id_variation AND znz_si.id_store IN (:storeIds)')
|
|
->setParameter('storeIds', $storeIds, Connection::PARAM_INT_ARRAY);
|
|
|
|
return 'COALESCE(znz_si.quantity, 0)';
|
|
}
|
|
}
|
|
|
|
return $useVariations ? 'COALESCE(pv.in_store, p.in_store)' : 'p.in_store';
|
|
}
|
|
|
|
private static function getStoreIds(string $languageId): array
|
|
{
|
|
static $znzUtil;
|
|
|
|
if (!$znzUtil) {
|
|
$znzUtil = ServiceContainer::getService(ZNZUtil::class);
|
|
}
|
|
|
|
return $znzUtil->getActiveStoreIds($languageId);
|
|
}
|
|
}
|