|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Finance module for HiPanel |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
|
6
|
|
|
* @package hipanel-module-finance |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\cart; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\modules\finance\models\CalculableModelInterface; |
|
14
|
|
|
use hipanel\modules\finance\models\Calculation; |
|
|
|
|
|
|
15
|
|
|
use hiqdev\hiart\ActiveRecord; |
|
16
|
|
|
use hiqdev\yii2\cart\CartPositionInterface; |
|
17
|
|
|
use hiqdev\yii2\cart\CartPositionTrait; |
|
18
|
|
|
use Serializable; |
|
19
|
|
|
use Yii; |
|
20
|
|
|
use yii\base\InvalidConfigException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* AbstractCartPosition represents position (item) in cart. |
|
24
|
|
|
* Holds: |
|
25
|
|
|
* - calculation object |
|
26
|
|
|
* - purchase object |
|
27
|
|
|
* - price for single item |
|
28
|
|
|
* - value for selected quantity. |
|
29
|
|
|
* |
|
30
|
|
|
* @property Calculation $actionCalcModel |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract class AbstractCartPosition extends ActiveRecord implements CartPositionInterface, CalculableModelInterface, Serializable |
|
33
|
|
|
{ |
|
34
|
|
|
use CartPositionTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string|array|Calculation |
|
38
|
|
|
* - string: the action calculation model class name |
|
39
|
|
|
* - array: array of options for [[Yii::createObject()]] call |
|
40
|
|
|
* - object: the object that extends [[Calculation]] class and represents calculation of the specified object type |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $_calculationModel; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string|array |
|
46
|
|
|
* - string: the position purchase model class name |
|
47
|
|
|
* - array: array of options for [[Yii::createObject()]] call |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $_purchaseModel; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var double the price of the 1 piece of the position |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $_price; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var double the full value of position |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $_value; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $_currency; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function init() |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->_purchaseModel === null) { |
|
72
|
|
|
throw new InvalidConfigException('Purchase model is not defined. The position can not be ordered'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return double |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getPrice() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->_price; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets the [[price]]. |
|
86
|
|
|
* |
|
87
|
|
|
* The $price will be casted to double |
|
88
|
|
|
* |
|
89
|
|
|
* @param mixed $price |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setPrice($price) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->_price = (float) $price; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getCost($withDiscount = true) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($withDiscount) { |
|
102
|
|
|
return $this->getValue(); |
|
103
|
|
|
} else { |
|
104
|
|
|
return $this->getQuantity() * $this->getPrice(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* The method returns the `crc32b` hash of a distinct condition |
|
110
|
|
|
* Example:. |
|
111
|
|
|
* |
|
112
|
|
|
* ```php |
|
113
|
|
|
* return hash('crc32b', implode('_', ['domain', 'registration', $this->name])); |
|
114
|
|
|
* ``` |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
abstract public function getId(); |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array $options Options that override defaults on [[Yii::createObject()]] |
|
122
|
|
|
* @throws \yii\base\InvalidConfigException |
|
123
|
|
|
* @return Calculation |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getCalculationModel($options = []) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!($this->_calculationModel instanceof Calculation)) { |
|
128
|
|
|
$config = ['position' => $this]; |
|
129
|
|
|
|
|
130
|
|
|
if (is_string($this->_calculationModel)) { |
|
131
|
|
|
$config['class'] = $this->_calculationModel; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->_calculationModel = Yii::createObject(array_merge($config, $options)); |
|
135
|
|
|
} else { |
|
136
|
|
|
$this->_calculationModel->synchronize(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->_calculationModel; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $options Options that override defaults on [[Yii::createObject()]] |
|
144
|
|
|
* @throws \yii\base\InvalidConfigException |
|
145
|
|
|
* @return AbstractPurchase |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getPurchaseModel($options = []) |
|
148
|
|
|
{ |
|
149
|
|
|
$config = ['position' => $this]; |
|
150
|
|
|
|
|
151
|
|
|
if (is_string($this->_purchaseModel)) { |
|
152
|
|
|
$config['class'] = $this->_purchaseModel; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return Yii::createObject(array_merge($config, $options)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns the value of the domain. |
|
160
|
|
|
* @return double |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getValue() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->_value; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Sets the [[value]]. |
|
169
|
|
|
* |
|
170
|
|
|
* The $value will be casted to double |
|
171
|
|
|
* |
|
172
|
|
|
* @param mixed $value |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setValue($value) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->_value = (float) $value; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function serialize() |
|
180
|
|
|
{ |
|
181
|
|
|
return serialize($this->serializationMap()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Method stores map of attributes that must be serialized. |
|
186
|
|
|
* |
|
187
|
|
|
* @return array key is the attribute name where the value should be extracted |
|
188
|
|
|
* In case when the value shoule be assigned in a special way, use [[unserializationMap]] |
|
189
|
|
|
* to map key to a closure, that will set the value |
|
190
|
|
|
* |
|
191
|
|
|
* @see unserializationMap |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function serializationMap() |
|
194
|
|
|
{ |
|
195
|
|
|
return [ |
|
196
|
|
|
'attributes' => $this->getAttributes(), |
|
197
|
|
|
'_currency' => $this->_currency, |
|
198
|
|
|
'_quantity' => $this->_quantity, |
|
199
|
|
|
'_price' => $this->_price, |
|
200
|
|
|
'_value' => $this->_value, |
|
201
|
|
|
'_id' => $this->_id, |
|
202
|
|
|
]; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return array |
|
207
|
|
|
* Key is the attribute name, value - the closure to unserialize the value |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function unserializationMap() |
|
210
|
|
|
{ |
|
211
|
|
|
return [ |
|
212
|
|
|
'attributes' => function ($value) { |
|
213
|
|
|
$this->setAttributes($value, false); |
|
214
|
|
|
}, |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function unserialize($serialized) |
|
219
|
|
|
{ |
|
220
|
|
|
$map = $this->unserializationMap(); |
|
221
|
|
|
$array = unserialize($serialized); |
|
222
|
|
|
foreach ($array as $key => $value) { |
|
223
|
|
|
if (!isset($map[$key])) { |
|
224
|
|
|
$this->{$key} = $value; |
|
225
|
|
|
continue; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
if ($map[$key] instanceof \Closure) { |
|
229
|
|
|
$map[$key]($value); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getCurrency(): ?string |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->_currency; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param string $currency |
|
244
|
|
|
*/ |
|
245
|
|
|
public function setCurrency(string $currency): void |
|
246
|
|
|
{ |
|
247
|
|
|
$this->_currency = $currency; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: