<?php

declare(strict_types=1);

namespace KupShop\B2BBundle\Utils;

use Query\Operator;
use Query\QueryBuilder;

class Query
{
    public static function filterB2BOrders()
    {
        return function (QueryBuilder $qb) {
            $qb->join('u', 'users_groups_relations', 'ugr', 'ugr.id_user = u.id')
                ->join('ugr', 'users_groups', 'ug', 'ug.id = ugr.id_group and FIND_IN_SET("b2b", ug.types)');

            return $qb;
        };
    }

    public static function checkIfOrderIsB2B($orderId)
    {
        $qb = sqlQueryBuilder()->select('1')
           ->from('orders', 'o')
           ->join('o', 'users', 'u', 'u.id = o.id_user')
           ->join('u', 'users_groups_relations', 'ugr', 'ugr.id_user = u.id')
           ->join('ugr', 'users_groups', 'ug', 'ug.id = ugr.id_group and FIND_IN_SET("b2b", ug.types)')
           ->where(Operator::equals(['o.id' => $orderId]));

        return (bool) $qb->execute()->fetchOne();
    }
}
