<?

namespace Shop\Services;

/**
 * Description of ComplexRate
 *
 * @author Admin
 */
class ComplexRate
{
    
    protected $customerService;
            
    function __construct(Customer $customerService)
    {
        $this->customerService = $customerService;
    }

    /**
     * 
     */
    public function getComplexRate($brandId): float
    {

        if(!$customer = $this->customerService->getCurrentCustomer()){
            $customerId = "%";
            $customerGroupId = "%";
        }
        else{
            $customerId = $customer->getId();
            $customerGroupId = $customer->getCustomerGroup()->getId();            
        }

        
        $shopId = $this->customerService->getCurrentShopId();
        
        $complexQ =
        \Shop\Models\ComplexRate\ComplexRateQuery::create()
                ->addAnd("", sprintf("'%s' LIKE `brand_id`", $brandId) , \Propel\Runtime\ActiveQuery\Criteria::CUSTOM)
                ->addAnd("", sprintf("'%s' LIKE `shop_id`", $shopId) , \Propel\Runtime\ActiveQuery\Criteria::CUSTOM)
                ->addAnd("", sprintf("'%s' LIKE `customer_id`", $customerId) , \Propel\Runtime\ActiveQuery\Criteria::CUSTOM)
                ->addAnd("", sprintf("'%s' LIKE `customer_group_id`", $customerGroupId) , \Propel\Runtime\ActiveQuery\Criteria::CUSTOM)
                
                ->orderBy("brand_id",\Propel\Runtime\ActiveQuery\Criteria::DESC)
                ->orderBy("shop_id",\Propel\Runtime\ActiveQuery\Criteria::DESC)
                ->orderBy("customer_id",\Propel\Runtime\ActiveQuery\Criteria::DESC)
                ->orderBy("customer_group_id",\Propel\Runtime\ActiveQuery\Criteria::DESC)
                
                ->orderByPriority(\Propel\Runtime\ActiveQuery\Criteria::DESC)
                
                ;
                
          $complex =      $complexQ->findOne();
                    
        if(!$complex){
            return (float)0;
        }
        
        return (float)$complex->getRate();
    }
}