-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathECartPositionBehaviour.php
95 lines (85 loc) · 1.91 KB
/
ECartPositionBehaviour.php
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
<?php
/**
* position in the cart
*
* @author pirrat <[email protected]>
* @version 0.9
* @package ShoppingCart
*
* Can be used with non-AR models.
*/
class ECartPositionBehaviour extends CActiveRecordBehavior {
/**
* Positions number
* @var int
*/
private $quantity = 0;
/**
* Update model on session restore?
* @var boolean
*/
private $refresh = true;
/**
* Position discount sum
* @var float
*/
private $discountPrice = 0.0;
/**
* Returns total price for all units of the position
* @param bool $withDiscount
* @return float
*
*/
public function getSumPrice($withDiscount = true) {
$fullSum = $this->getOwner()->getPrice() * $this->quantity;
if($withDiscount)
$fullSum -= $this->discountPrice;
return $fullSum;
}
/**
* Returns quantity.
* @return int
*/
public function getQuantity() {
return $this->quantity;
}
/**
* Updates quantity.
*
* @param int quantity
*/
public function setQuantity($newVal) {
$this->quantity = $newVal;
}
/**
* Magic method. Called on session restore.
*/
public function __wakeup() {
if ($this->refresh === true)
$this->getOwner()->refresh();
}
/**
* If we need to refresh model on restoring session.
* Default is true.
* @param boolean $refresh
*/
public function setRefresh($refresh) {
$this->refresh = $refresh;
}
/**
* Add $price to position discount sum
* @param float $price
* @return void
*/
public function addDiscountPrice($price) {
$this->discountPrice += $price;
}
/**
* Set position discount sum
* @param float $price
* @return void
*/
public function setDiscountPrice($price) {
$this->discountPrice = $price;
}
}