|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Orders\Entity; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Entity\EntityInterface; |
|
14
|
|
|
use Core\Entity\EntityTrait; |
|
15
|
|
|
use Core\Entity\ImmutableEntityInterface; |
|
16
|
|
|
use Core\Entity\ImmutableEntityTrait; |
|
17
|
|
|
use Doctrine\Common\Collections\Collection; |
|
18
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
|
19
|
|
|
use Core\Entity\IdentifiableEntityTrait; |
|
20
|
|
|
use Core\Entity\ModificationDateAwareEntityTrait; |
|
21
|
|
|
use Doctrine\ODM\MongoDB\Proxy\Proxy; |
|
22
|
|
|
use Orders\Entity\Snapshot\SnapshotInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Order entity |
|
26
|
|
|
* |
|
27
|
|
|
* @ODM\Document(collection="orders", repositoryClass="Orders\Repository\Orders") |
|
28
|
|
|
* @ODM\HasLifeCycleCallbacks |
|
29
|
|
|
* |
|
30
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
31
|
|
|
* @todo write test |
|
32
|
|
|
*/ |
|
33
|
|
|
class Order implements OrderInterface, ImmutableEntityInterface |
|
34
|
|
|
{ |
|
35
|
|
|
use EntityTrait, IdentifiableEntityTrait, ModificationDateAwareEntityTrait, ImmutableEntityTrait; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The order number |
|
40
|
|
|
* |
|
41
|
|
|
* @Odm\String |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $number; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Order type. |
|
48
|
|
|
* Used for filtering and organization issues. |
|
49
|
|
|
* |
|
50
|
|
|
* @ODM\String |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $type = self::TYPE_GENERAL; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The snapshot entity. |
|
57
|
|
|
* |
|
58
|
|
|
* @ODM\EmbedOne(discriminatorField="_entity") |
|
59
|
|
|
* @var SnapshotInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $entity; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The invoice address. |
|
65
|
|
|
* |
|
66
|
|
|
* @ODM\EmbedOne(targetDocument="\Orders\Entity\InvoiceAddress") |
|
67
|
|
|
* @var InvoiceAddressInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $invoiceAddress; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The products of this order. |
|
73
|
|
|
* |
|
74
|
|
|
* @ODM\EmbedMany(discriminatorField="_entity") |
|
75
|
|
|
* @var Collection |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $products; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Currency (ISO 4217) |
|
81
|
|
|
* |
|
82
|
|
|
* @ODM\String |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $currency; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Currency symbol. |
|
89
|
|
|
* |
|
90
|
|
|
* @ODM\String |
|
91
|
|
|
* @var string |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $currencySymbol; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The tax rate. |
|
97
|
|
|
* |
|
98
|
|
|
* @ODM\Field(type="float") |
|
99
|
|
|
* @var float |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $taxRate = 0; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* The prices of this order. |
|
105
|
|
|
* |
|
106
|
|
|
* It's an array with three fields: |
|
107
|
|
|
* <pre> |
|
108
|
|
|
* [ |
|
109
|
|
|
* "products" => [ |
|
110
|
|
|
* <product_name> => [ |
|
111
|
|
|
* 'single_total' => total price of ONE product incl. tax, |
|
112
|
|
|
* 'single_tax' => tax of ONE product, |
|
113
|
|
|
* 'single_pretax' => total price excl. tax |
|
114
|
|
|
* 'total' => total price incl. tax of ALL quantities of the product. |
|
115
|
|
|
* 'tax' => total tax of ALL quantities, |
|
116
|
|
|
* 'pretax' => total price excl. tax of ALL quantities. |
|
117
|
|
|
* ], |
|
118
|
|
|
* ... |
|
119
|
|
|
* ], |
|
120
|
|
|
* "total" => total amount incl. tax |
|
121
|
|
|
* "tax" => amount of the tax |
|
122
|
|
|
* "pretax" => total amount excl. tax |
|
123
|
|
|
* ] |
|
124
|
|
|
* if this entity is not yet persisted, prices are |
|
125
|
|
|
* calculated each time on access. |
|
126
|
|
|
* |
|
127
|
|
|
* @ODM\Hash |
|
128
|
|
|
* @var array |
|
129
|
|
|
*/ |
|
130
|
|
|
protected $prices; |
|
131
|
|
|
|
|
132
|
|
|
public function getNumber() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->number; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function setNumber($number) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->number = $number; |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function setEntity(SnapshotInterface $entity) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->entity = $entity; |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getEntity() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->entity; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function setType($type) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->type = $type; |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function getType() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->type; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function setCurrency($currency) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->currency = $currency; |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getCurrency() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->currency; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function setCurrencySymbol($currencySymbol) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->currencySymbol = $currencySymbol; |
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function getCurrencySymbol() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->currencySymbol; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function setInvoiceAddress(InvoiceAddressInterface $invoiceAddress) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->invoiceAddress = $invoiceAddress; |
|
195
|
|
|
|
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function getInvoiceAddress() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->invoiceAddress; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function setProducts(Collection $products) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->products = $products; |
|
207
|
|
|
|
|
208
|
|
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function getProducts() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->products; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function setTaxRate($taxRate) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->taxRate = $taxRate; |
|
219
|
|
|
|
|
220
|
|
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function getTaxRate() |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->taxRate; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Sets the total amount without tax. |
|
230
|
|
|
* |
|
231
|
|
|
* Discount and sconti should be calculated before hand. |
|
232
|
|
|
* |
|
233
|
|
|
* @param int $amount |
|
234
|
|
|
* |
|
235
|
|
|
* @return self |
|
236
|
|
|
*/ |
|
237
|
|
|
public function setPrice($amount) |
|
238
|
|
|
{ |
|
239
|
|
|
$tax = $this->getTaxRate() / 100; |
|
240
|
|
|
$taxAmount = $amount * $tax; |
|
241
|
|
|
|
|
242
|
|
|
$this->prices = [ |
|
243
|
|
|
'pretax' => round($amount, 2), |
|
244
|
|
|
'tax' => round($taxAmount, 2), |
|
245
|
|
|
'net' => round($amount + $taxAmount, 2), |
|
246
|
|
|
]; |
|
247
|
|
|
|
|
248
|
|
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
public function getPrice($type="net") |
|
252
|
|
|
{ |
|
253
|
|
|
if (!$this->prices || !array_key_exists($type, $this->prices)) { |
|
|
|
|
|
|
254
|
|
|
return 0; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $this->prices[$type]; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Gets the calculated prices. |
|
262
|
|
|
* |
|
263
|
|
|
* @param bool $calculate Should the prices be recalculated. |
|
264
|
|
|
* |
|
265
|
|
|
* @return array |
|
266
|
|
|
*/ |
|
267
|
|
|
public function getPrices($calculate=false) |
|
268
|
|
|
{ |
|
269
|
|
|
/*if (!$this->prices || $calculate) { |
|
|
|
|
|
|
270
|
|
|
$this->calculatePrices(); |
|
271
|
|
|
}*/ |
|
272
|
|
|
|
|
273
|
|
|
return $this->prices; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Calculates the prices. |
|
278
|
|
|
* |
|
279
|
|
|
* Not used at the moment. |
|
280
|
|
|
*/ |
|
281
|
|
|
public function calculatePrices() |
|
282
|
|
|
{ |
|
283
|
|
|
if ($this->getId()) { |
|
284
|
|
|
return; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
$taxFactor = $this->getTaxRate() / 100; |
|
288
|
|
|
$total = $pretax = $tax = 0; |
|
289
|
|
|
$sums = []; |
|
290
|
|
|
|
|
291
|
|
|
/* @var ProductInterface $product */ |
|
292
|
|
|
foreach ($this->getProducts() as $product) { |
|
293
|
|
|
$pPreTax = $product->getPrice(); |
|
294
|
|
|
$pTax = $pPreTax * $taxFactor; |
|
295
|
|
|
$pTotal = $pPreTax + $pTax; |
|
296
|
|
|
$pQuantity = $product->getQuantity(); |
|
297
|
|
|
$ptPreTax = $pQuantity * $pPreTax; |
|
298
|
|
|
$ptTax = $pQuantity * $pTax; |
|
299
|
|
|
$ptTotal = $pQuantity * $pTotal; |
|
300
|
|
|
|
|
301
|
|
|
$sums[$product->getName()] = [ |
|
302
|
|
|
'single_pretax' => $pPreTax, |
|
303
|
|
|
'single_tax' => $pTax, |
|
304
|
|
|
'single_total' => $pTotal, |
|
305
|
|
|
'pretax' => $ptPreTax, |
|
306
|
|
|
'tax' => $ptTax, |
|
307
|
|
|
'total' => $ptTotal, |
|
308
|
|
|
]; |
|
309
|
|
|
|
|
310
|
|
|
$total += $ptTotal; |
|
311
|
|
|
$pretax += $ptPreTax; |
|
312
|
|
|
$tax += $ptTax; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$this->prices = [ |
|
316
|
|
|
'products' => $sums, |
|
317
|
|
|
'total' => $total, |
|
318
|
|
|
'tax' => $tax, |
|
319
|
|
|
'pretax' => $pretax, |
|
320
|
|
|
]; |
|
321
|
|
|
} |
|
322
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.