Basket.class.php 2.99 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?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');
        }
    }
    
}