1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Discounts\Model\Discount; |
6
|
|
|
use Dynamic\Foxy\Discounts\Model\DiscountTier; |
7
|
|
|
use Dynamic\Foxy\Model\ProductOption; |
8
|
|
|
use SilverStripe\Core\Injector\Injectable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DiscountHelper |
12
|
|
|
* @package Dynamic\Foxy\Discounts |
13
|
|
|
*/ |
14
|
|
|
class DiscountHelper |
15
|
|
|
{ |
16
|
|
|
use Injectable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var |
20
|
|
|
*/ |
21
|
|
|
private $product; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ProductOption |
25
|
|
|
*/ |
26
|
|
|
private $product_option; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Discount |
30
|
|
|
*/ |
31
|
|
|
private $discount; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
private $discounted_price; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DiscountTier |
40
|
|
|
*/ |
41
|
|
|
private $discount_tier; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* DiscountHelper constructor. |
45
|
|
|
* @param $product |
46
|
|
|
* @param $discount |
47
|
|
|
* @param ProductOption|string|null $productOption |
48
|
|
|
*/ |
49
|
|
|
public function __construct($product, $discount, $productOption = null) |
50
|
|
|
{ |
51
|
|
|
$this->setProduct($product); |
52
|
|
|
$this->setDiscount($discount); |
53
|
|
|
|
54
|
|
|
if ($productOption instanceof ProductOption || is_string($productOption)) { |
55
|
|
|
$this->setProductOption($productOption); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->setDiscountedPrice(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function getProduct() |
65
|
|
|
{ |
66
|
|
|
return $this->product; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $product |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function setProduct($product): self |
74
|
|
|
{ |
75
|
|
|
$this->product = $product; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function getProductOption() |
84
|
|
|
{ |
85
|
|
|
return $this->product_option; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ProductOption $productOption |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function setProductOption($productOption): self |
93
|
|
|
{ |
94
|
|
|
$this->product_option = ($productOption instanceof ProductOption) |
|
|
|
|
95
|
|
|
? $productOption |
96
|
|
|
: $this->getProduct()->Options()->filter('OptionModifierKey', $productOption)->first(); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
public function getDiscount() |
105
|
|
|
{ |
106
|
|
|
return $this->discount; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Discount $discount |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setDiscount(Discount $discount): self |
114
|
|
|
{ |
115
|
|
|
$this->discount = $discount; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param int $quantity |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function setDiscountTier($quantity = 1) |
125
|
|
|
{ |
126
|
|
|
$this->discount_tier = $this->getDiscount()->DiscountTiers() |
|
|
|
|
127
|
|
|
->filter('Quantity:LessThanOrEqual', $quantity) |
128
|
|
|
->sort('Quantity DESC')->first(); |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return DiscountTier |
135
|
|
|
*/ |
136
|
|
|
public function getDiscountTier() |
137
|
|
|
{ |
138
|
|
|
if (!$this->discount_tier) { |
139
|
|
|
$this->setDiscountTier(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->discount_tier; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return float|int |
147
|
|
|
*/ |
148
|
|
|
public function setDiscountedPrice() |
149
|
|
|
{ |
150
|
|
|
$price = ($this->getProductOption()) |
151
|
|
|
? $this->getProductOption()->getPrice($this->getProduct()) |
152
|
|
|
: $this->getProduct()->Price; |
153
|
|
|
|
154
|
|
|
$tier = $this->getDiscountTier(); |
155
|
|
|
|
156
|
|
|
$price = ($this->getDiscount()->Type == 'Percent') |
|
|
|
|
157
|
|
|
? $price - ($price * ($tier->Percentage / 100)) |
|
|
|
|
158
|
|
|
: $price - $tier->Amount; |
|
|
|
|
159
|
|
|
|
160
|
|
|
return $this->discounted_price = $price; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function getDiscountedPrice() |
167
|
|
|
{ |
168
|
|
|
if (!$this->discounted_price) { |
169
|
|
|
$this->setDiscountedPrice(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->discounted_price; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|