1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Order\Model; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Adjustment implements AdjustmentInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var mixed |
21
|
|
|
*/ |
22
|
|
|
protected $id; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var OrderInterface |
26
|
|
|
*/ |
27
|
|
|
protected $order; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var OrderItemInterface |
31
|
|
|
*/ |
32
|
|
|
protected $orderItem; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var OrderItemUnitInterface |
36
|
|
|
*/ |
37
|
|
|
protected $orderItemUnit; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $type; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $description; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $amount = 0; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Is adjustment neutral? |
56
|
|
|
* Should it modify the order total? |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $neutral = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
protected $locked = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
protected $originId; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $originType; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var \DateTime |
79
|
|
|
*/ |
80
|
|
|
protected $createdAt; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var \DateTime |
84
|
|
|
*/ |
85
|
|
|
protected $updatedAt; |
86
|
|
|
|
87
|
|
|
public function __construct() |
88
|
|
|
{ |
89
|
|
|
$this->createdAt = new \DateTime(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function getId() |
96
|
|
|
{ |
97
|
|
|
return $this->id; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getAdjustable() |
104
|
|
|
{ |
105
|
|
|
if (null !== $this->order) { |
106
|
|
|
return $this->order; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (null !== $this->orderItem) { |
110
|
|
|
return $this->orderItem; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (null !== $this->orderItemUnit) { |
114
|
|
|
return $this->orderItemUnit; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function setAdjustable(AdjustableInterface $adjustable = null) |
124
|
|
|
{ |
125
|
|
|
$this->assertNotLocked(); |
126
|
|
|
|
127
|
|
|
$currentAdjustable = $this->getAdjustable(); |
128
|
|
|
if ($currentAdjustable === $adjustable) { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->order = $this->orderItem = $this->orderItemUnit = null; |
133
|
|
|
if (null !== $currentAdjustable) { |
134
|
|
|
$currentAdjustable->removeAdjustment($this); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (null === $adjustable) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->assignAdjustable($adjustable); |
142
|
|
|
$adjustable->addAdjustment($this); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function getType() |
149
|
|
|
{ |
150
|
|
|
return $this->type; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function setType($type) |
157
|
|
|
{ |
158
|
|
|
$this->type = $type; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function getDescription() |
165
|
|
|
{ |
166
|
|
|
return $this->description; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function setDescription($description) |
173
|
|
|
{ |
174
|
|
|
$this->description = $description; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function getAmount() |
181
|
|
|
{ |
182
|
|
|
return $this->amount; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function setAmount($amount) |
189
|
|
|
{ |
190
|
|
|
if (!is_int($amount)) { |
191
|
|
|
throw new \InvalidArgumentException('Amount must be an integer.'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->amount = $amount; |
195
|
|
|
if (!$this->isNeutral()) { |
196
|
|
|
$this->recalculateAdjustable(); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function isNeutral() |
204
|
|
|
{ |
205
|
|
|
return $this->neutral; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function setNeutral($neutral) |
212
|
|
|
{ |
213
|
|
|
$neutral = (bool) $neutral; |
214
|
|
|
|
215
|
|
|
if ($this->neutral !== $neutral) { |
216
|
|
|
$this->neutral = $neutral; |
217
|
|
|
$this->recalculateAdjustable(); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
*/ |
224
|
|
|
public function isLocked() |
225
|
|
|
{ |
226
|
|
|
return $this->locked; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function lock() |
233
|
|
|
{ |
234
|
|
|
$this->locked = true; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function unlock() |
241
|
|
|
{ |
242
|
|
|
$this->locked = false; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
|
|
public function isCharge() |
249
|
|
|
{ |
250
|
|
|
return 0 > $this->amount; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
|
|
public function isCredit() |
257
|
|
|
{ |
258
|
|
|
return 0 < $this->amount; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* {@inheritdoc} |
263
|
|
|
*/ |
264
|
|
|
public function getOriginId() |
265
|
|
|
{ |
266
|
|
|
return $this->originId; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
|
|
public function setOriginId($originId) |
273
|
|
|
{ |
274
|
|
|
$this->originId = $originId; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* {@inheritdoc} |
279
|
|
|
*/ |
280
|
|
|
public function getOriginType() |
281
|
|
|
{ |
282
|
|
|
return $this->originType; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* {@inheritdoc} |
287
|
|
|
*/ |
288
|
|
|
public function setOriginType($originType) |
289
|
|
|
{ |
290
|
|
|
$this->originType = $originType; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritdoc} |
295
|
|
|
*/ |
296
|
|
|
public function getCreatedAt() |
297
|
|
|
{ |
298
|
|
|
return $this->createdAt; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
|
|
public function setCreatedAt(\DateTime $createdAt) |
305
|
|
|
{ |
306
|
|
|
$this->createdAt = $createdAt; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
*/ |
312
|
|
|
public function getUpdatedAt() |
313
|
|
|
{ |
314
|
|
|
return $this->updatedAt; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* {@inheritdoc} |
319
|
|
|
*/ |
320
|
|
|
public function setUpdatedAt(\DateTime $updatedAt) |
321
|
|
|
{ |
322
|
|
|
$this->updatedAt = $updatedAt; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
private function recalculateAdjustable() |
326
|
|
|
{ |
327
|
|
|
$adjustable = $this->getAdjustable(); |
328
|
|
|
if (null !== $adjustable) { |
329
|
|
|
$adjustable->recalculateAdjustmentsTotal(); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param AdjustableInterface $adjustable |
335
|
|
|
* |
336
|
|
|
* @throws \InvalidArgumentException when adjustable class is not supported |
337
|
|
|
*/ |
338
|
|
|
private function assignAdjustable(AdjustableInterface $adjustable) |
339
|
|
|
{ |
340
|
|
|
if ($adjustable instanceof OrderInterface) { |
341
|
|
|
$this->order = $adjustable; |
342
|
|
|
} elseif ($adjustable instanceof OrderItemInterface) { |
343
|
|
|
$this->orderItem = $adjustable; |
344
|
|
|
} elseif ($adjustable instanceof OrderItemUnitInterface) { |
345
|
|
|
$this->orderItemUnit = $adjustable; |
346
|
|
|
} else { |
347
|
|
|
throw new \InvalidArgumentException('Given adjustable object class is not supported.'); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @throws \LogicException when adjustment is locked |
353
|
|
|
*/ |
354
|
|
|
private function assertNotLocked() |
355
|
|
|
{ |
356
|
|
|
if ($this->isLocked()) { |
357
|
|
|
throw new \LogicException('Adjustment is locked and cannot be modified.'); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|