39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace External\CykloSpecialityBundle\Query;
|
|
|
|
use KupShop\KupShopBundle\Context\UserContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use Query\Operator;
|
|
|
|
class Product extends \Query\Product
|
|
{
|
|
private const CENTRAL_STORE_ID = 2;
|
|
|
|
public static function getInStoreField($useVariations = true, $qb = null): string
|
|
{
|
|
$storeField = $useVariations ? 'COALESCE(pv.in_store, p.in_store)' : 'p.in_store';
|
|
|
|
if ($qb && Contexts::get(UserContext::class)->isType('b2b')) {
|
|
$storeField = 'COALESCE(('.static::getB2BInStoreField($qb->isAliasPresent('pv'))."), {$storeField})";
|
|
}
|
|
|
|
return static::applyProductReservationsOnInStoreField($storeField, $useVariations);
|
|
}
|
|
|
|
private static function getB2BInStoreField(bool $useVariations): string
|
|
{
|
|
$qb = sqlQueryBuilder()
|
|
->select('COALESCE(SUM(si_shop_store.quantity), 0)')
|
|
->from('stores_items', 'si_shop_store')
|
|
->andWhere('p.id = si_shop_store.id_product')
|
|
->andWhere(Operator::equals(['si_shop_store.id_store' => self::CENTRAL_STORE_ID]));
|
|
|
|
if ($useVariations) {
|
|
$qb->andWhere('pv.id <=> si_shop_store.id_variation');
|
|
}
|
|
|
|
return $qb->getRunnableSQL();
|
|
}
|
|
}
|