<?php

/**
 * Basket
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    sf
 * @subpackage model
 * @author     Atma
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class Basket extends BaseBasket
{
    public function getSum() //todo: save if BasketProduct modifided
    {
        $price = 0;
        foreach ($this->getBasketProduct() as $basketProduct) {
            $price += $basketProduct->sum();
        }
    }
    public function getCount() //todo: save if BasketProduct modifided
    {
        $count = 0;
        foreach ($this->getBasketProduct() as $basketProduct) {
            $count += $basketProduct->getProductCount();
        }
        return $count;
    }
    public function products($param = array(), $hydration = null)
    {
        return Doctrine_Query::create()
            ->from('BasketProduct bp')
            ->innerJoin('bp.Product p')
            ->leftJoin('p.Offer o ON o.id = bp.offer_id and bp.offer_id is not null')
            ->where('bp.basket_id = ?', $this->getId())
            ->execute($param, $hydration);
    }
    
    /**
     * @param Product|Offer $elem
     * @return BasketProduct|null
     */
    public function addOrUpdate($elem, $count = 1)
    {
        $this->exepIfNotProductOrOffer($elem);
        $count = $count > 0 ? $count : 1;
        $basketProduct = $this->findBasketProduct($elem);
        if(is_null($basketProduct)){
            $basketProduct = new BasketProduct();
            $basketProduct->setProductCount($count);
        } else {
           
            $basketProduct->addProductCount($count);
        }
        $basketProduct->setBasketId($this->getId());
        if($elem instanceof Product) {
            $basketProduct->setProductId($elem->getIncremented());
        } else {
            $basketProduct->setProductId($elem->getProductId());
            $basketProduct->setOfferId($elem->getId());
        }
       // $method = 'set' . get_class($elem) . 'Id';
        //$basketProduct->{$method}($elem->getIncremented());
        
        $basketProduct->save();
    }
    /**
     * @param Product|Offer $elem
     * @return BasketProduct|null
     */
    public function findBasketProduct($elem)
    {
        $this->exepIfNotProductOrOffer($elem);
        $result = null;
        foreach ($this->getBasketProduct() as $basketProduct) {
            $flag = $elem instanceof Product ?
                $elem->getIncremented() == $basketProduct->getProductId() && is_null($basketProduct->getOfferId()):
                $elem->getIncremented() == $basketProduct->getOfferId();
            if($flag) {
                $result = $basketProduct;
                break;
            }
        }
        return $result;
    }
        /**
         * @param Product|Offer $elem
         * @throws
         */
    protected function exepIfNotProductOrOffer($elem)
    {
        if(!($elem instanceof Product || $elem instanceof Offer)) {
            throw new Exception('param mast be Offer or Product');
        }
    }
    
}
