forked from thelia/thelia
/
Order.php
368 lines (317 loc) · 10.2 KB
/
Order.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
namespace Thelia\Model;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Map\OrderProductTaxTableMap;
use Thelia\Model\Base\Order as BaseOrder;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\TaxEngine\Calculator;
class Order extends BaseOrder
{
use ModelEventDispatcherTrait;
protected $choosenDeliveryAddress = null;
protected $choosenInvoiceAddress = null;
protected $disableVersioning = false;
/**
* @param Address $choosenDeliveryAddress
* @return $this
*/
public function setChoosenDeliveryAddress($choosenDeliveryAddress)
{
$this->choosenDeliveryAddress = $choosenDeliveryAddress;
return $this;
}
/**
* @param boolean $disableVersioning
* @return $this
*/
public function setDisableVersioning($disableVersioning)
{
$this->disableVersioning = (bool) $disableVersioning;
return $this;
}
public function isVersioningDisable()
{
return $this->disableVersioning;
}
public function isVersioningNecessary($con = null)
{
if ($this->isVersioningDisable()) {
return false;
} else {
return parent::isVersioningNecessary($con);
}
}
/**
* @return Address
*/
public function getChoosenDeliveryAddress()
{
return $this->choosenDeliveryAddress;
}
/**
* @param Address $choosenInvoiceAddress
* @return $this
*/
public function setChoosenInvoiceAddress($choosenInvoiceAddress)
{
$this->choosenInvoiceAddress = $choosenInvoiceAddress;
return $this;
}
/**
* @return Address
*/
public function getChoosenInvoiceAddress()
{
return $this->choosenInvoiceAddress;
}
public function preSave(ConnectionInterface $con = null)
{
if ($this->isPaid(false) && null === $this->getInvoiceDate()) {
$this
->setInvoiceDate(time());
}
return parent::preSave($con);
}
/**
* {@inheritDoc}
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::ORDER_BEFORE_CREATE, new OrderEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postInsert(ConnectionInterface $con = null)
{
$this->setRef($this->generateRef())
->setDisableVersioning(true)
->save($con);
$this->dispatchEvent(TheliaEvents::ORDER_AFTER_CREATE, new OrderEvent($this));
}
public function generateRef()
{
return sprintf('ORD%s', str_pad($this->getId(), 12, 0, STR_PAD_LEFT));
}
/**
* Compute this order amount.
*
* The order amount is only available once the order is persisted in database.
* During invoice process, use all cart methods instead of order methods (the order doest not exists at this moment)
*
* @param float|int $tax (output only) returns the tax amount for this order
* @param bool $includePostage if true, the postage cost is included to the total
* @param bool $includeDiscount if true, the discount will be included to the total
* @return float
*/
public function getTotalAmount(&$tax = 0, $includePostage = true, $includeDiscount = true)
{
$amount = 0;
$tax = 0;
/* browse all products */
foreach ($this->getOrderProducts() as $orderProduct) {
$taxAmountQuery = OrderProductTaxQuery::create();
if ($orderProduct->getWasInPromo() == 1) {
$taxAmountQuery->withColumn('SUM(' . OrderProductTaxTableMap::PROMO_AMOUNT . ')', 'total_tax');
} else {
$taxAmountQuery->withColumn('SUM(' . OrderProductTaxTableMap::AMOUNT . ')', 'total_tax');
}
$taxAmount = $taxAmountQuery->filterByOrderProductId($orderProduct->getId(), Criteria::EQUAL)
->findOne();
$price = ($orderProduct->getWasInPromo() == 1 ? $orderProduct->getPromoPrice() : $orderProduct->getPrice());
$amount += $price * $orderProduct->getQuantity();
$tax += $taxAmount->getVirtualColumn('total_tax') * $orderProduct->getQuantity();
}
$total = $amount + $tax;
// @todo : manage discount : free postage ?
if (true === $includeDiscount) {
$total -= $this->getDiscount();
if ($total<0) {
$total = 0;
} else {
$total = round($total, 2);
}
}
if (false !== $includePostage) {
$total += $this->getPostage();
$tax += $this->getPostageTax();
}
return $total;
}
/**
* Compute this order weight.
*
* The order weight is only available once the order is persisted in database.
* During invoice process, use all cart methods instead of order methods (the order doest not exists at this moment)
*
* @return float
*/
public function getWeight()
{
$weight = 0;
/* browse all products */
foreach ($this->getOrderProducts() as $orderProduct) {
$weight += $orderProduct->getQuantity() * (double)$orderProduct->getWeight();
}
return $weight;
}
/**
* Return the postage without tax
* @return float|int
*/
public function getUntaxedPostage()
{
if (0 < $this->getPostageTax()) {
$untaxedPostage = round($this->getPostage() - $this->getPostageTax(), 2);
} else {
$untaxedPostage = $this->getPostage();
}
return $untaxedPostage;
}
/**
* Check if the current order contains at less 1 virtual product with a file to download
*
* @return bool true if this order have at less 1 file to download, false otherwise.
*/
public function hasVirtualProduct()
{
$virtualProductCount = OrderProductQuery::create()
->filterByOrderId($this->getId())
->filterByVirtual(1, Criteria::EQUAL)
->count()
;
return ($virtualProductCount !== 0);
}
/**
* Set the status of the current order to NOT PAID
*/
public function setNotPaid()
{
$this->setStatusHelper(OrderStatus::CODE_NOT_PAID);
}
/**
* Check if the current status of this order is NOT PAID
*
* @return bool true if this order is NOT PAID, false otherwise.
*/
public function isNotPaid()
{
return $this->hasStatusHelper(OrderStatus::CODE_NOT_PAID);
}
/**
* Set the status of the current order to PAID
*/
public function setPaid()
{
$this->setStatusHelper(OrderStatus::CODE_PAID);
}
/**
* Check if the current status of this order is PAID
*
* @param bool $exact if true, the method will check if the current status is exactly OrderStatus::CODE_PAID.
* if false, it will check if the order has been paid, whatever the current status is. The default is true.
* @return bool true if this order is PAID, false otherwise.
*/
public function isPaid($exact = true)
{
return $this->hasStatusHelper(
$exact ?
OrderStatus::CODE_PAID :
[ OrderStatus::CODE_PAID, OrderStatus::CODE_PROCESSING, OrderStatus::CODE_SENT ]
);
}
/**
* Set the status of the current order to PROCESSING
*/
public function setProcessing()
{
$this->setStatusHelper(OrderStatus::CODE_PROCESSING);
}
/**
* Check if the current status of this order is PROCESSING
*
* @return bool true if this order is PROCESSING, false otherwise.
*/
public function isProcessing()
{
return $this->hasStatusHelper(OrderStatus::CODE_PROCESSING);
}
/**
* Set the status of the current order to SENT
*/
public function setSent()
{
$this->setStatusHelper(OrderStatus::CODE_SENT);
}
/**
* Check if the current status of this order is SENT
*
* @return bool true if this order is SENT, false otherwise.
*/
public function isSent()
{
return $this->hasStatusHelper(OrderStatus::CODE_SENT);
}
/**
* Set the status of the current order to CANCELED
*/
public function setCancelled()
{
$this->setStatusHelper(OrderStatus::CODE_CANCELED);
}
/**
* Check if the current status of this order is CANCELED
*
* @return bool true if this order is CANCELED, false otherwise.
*/
public function isCancelled()
{
return $this->hasStatusHelper(OrderStatus::CODE_CANCELED);
}
/**
* Set the status of the current order to REFUNDED
*/
public function setRefunded()
{
$this->setStatusHelper(OrderStatus::CODE_REFUNDED);
}
/**
* Check if the current status of this order is REFUNDED
*
* @return bool true if this order is CANCELED, false otherwise.
*/
public function isRefunded()
{
return $this->hasStatusHelper(OrderStatus::CODE_REFUNDED);
}
/**
* Set the status of the current order to the provided status
*
* @param string $statusCode the status code, one of OrderStatus::CODE_xxx constants.
*/
public function setStatusHelper($statusCode)
{
if (null !== $ordeStatus = OrderStatusQuery::create()->findOneByCode($statusCode)) {
$this->setOrderStatus($ordeStatus)->save();
}
}
/**
* Check if the current status of this order is $statusCode or, if $statusCode is an array, if the current
* status is in the $statusCode array.
*
* @param string|array $statusCode the status code, one of OrderStatus::CODE_xxx constants.
* @return bool true if this order have the provided status, false otherwise.
*/
public function hasStatusHelper($statusCode)
{
if (is_array($statusCode)) {
return in_array($this->getOrderStatus()->getCode(), $statusCode);
} else {
return $this->getOrderStatus()->getCode() == $statusCode;
}
}
}