<?php

declare(strict_types=1);

namespace External\ZNZBundle\Overrides\Ordering;

use Query\QueryBuilder;

class Cart extends \KupShop\OrderingBundle\Cart
{
    protected function getEnsureInStoreNewPiecesField(QueryBuilder $qb): string
    {
        $newPiecesField = parent::getEnsureInStoreNewPiecesField($qb);

        $qb->leftJoin('pv', 'znz_products', 'zp', 'zp.id_product = p.id AND zp.id_variation <=> pv.id')
            ->leftJoin('zp', 'znz_products_website', 'zpw', 'zpw.id_znz = zp.id_znz');

        return "LEAST({$newPiecesField}, COALESCE(zpw.max_cart_quantity, {$newPiecesField}))";
    }
}
