-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Order.php
495 lines (409 loc) · 17.8 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* Isotope eCommerce for Contao Open Source CMS
*
* Copyright (C) 2009-2014 terminal42 gmbh & Isotope eCommerce Workgroup
*
* @package Isotope
* @link http://isotopeecommerce.org
* @license http://opensource.org/licenses/lgpl-3.0.html
*/
namespace Isotope\Model\ProductCollection;
use Haste\Generator\RowClass;
use Haste\Haste;
use Haste\Util\Format;
use Isotope\Interfaces\IsotopeOrderStatusAware;
use Isotope\Interfaces\IsotopeProductCollection;
use Isotope\Isotope;
use Isotope\Model\Address;
use Isotope\Model\Document;
use Isotope\Model\OrderStatus;
use Isotope\Model\Payment;
use Isotope\Model\ProductCollection;
use Isotope\Model\ProductCollectionDownload;
use Isotope\Model\ProductCollectionSurcharge;
use Isotope\Model\Shipping;
use NotificationCenter\Model\Notification;
/**
* Class Order
*
* Provide methods to handle Isotope orders.
*
* @method static Order findOneBy(string $strColumn, $varValue, array $arrOptions=array())
*
* @property int locked
* @property array checkout_info
* @property array payment_data
* @property array shipping_data
* @property string document_number
* @property int order_status
* @property int date_paid
* @property int date_shipped
* @property string notes
*/
class Order extends ProductCollection implements IsotopeProductCollection
{
/**
* Return true if order has been paid.
* This is the case if either payment date is set or order status has the paid flag
*
* @return bool
*/
public function isPaid()
{
// Order is paid if a payment date is set
$paid = (int) $this->date_paid;
if ($paid > 0 && $paid <= time()) {
return true;
}
// Otherwise we check the orderstatus checkbox
/** @var OrderStatus $objStatus */
$objStatus = $this->getRelated('order_status');
return (null !== $objStatus && $objStatus->isPaid()) ? true : false;
}
/**
* Returns true if checkout has been completed
*
* @return bool
*/
public function isCheckoutComplete()
{
return (bool) $this->checkout_complete;
}
/**
* Get label for current order status
*
* @return string
*/
public function getStatusLabel()
{
/** @var OrderStatus $objStatus */
$objStatus = $this->getRelated('order_status');
return (null === $objStatus) ? '' : $objStatus->getName();
}
/**
* Get the alias for current order status
*
* @return string
*/
public function getStatusAlias()
{
/** @var OrderStatus $objStatus */
$objStatus = $this->getRelated('order_status');
return (null === $objStatus) ? $this->order_status : $objStatus->getAlias();
}
/**
* Process the order checkout
*
* @return bool
*/
public function checkout()
{
if ($this->isCheckoutComplete()) {
return true;
}
// Finish and lock the order (do this now, because otherwise surcharges etc. will not be loaded form the database)
$this->checkout_complete = true;
$this->generateDocumentNumber($this->getRelated('config_id')->orderPrefix, (int) $this->getRelated('config_id')->orderDigits);
if (!$this->isLocked()) {
$this->lock();
}
\System::log('New order ID ' . $this->id . ' has been placed', __METHOD__, TL_ACCESS);
// Delete cart after migrating to order
if (($objCart = Cart::findByPk($this->source_collection_id)) !== null) {
$objCart->delete();
}
// Delete all other orders that relate to the current cart
if (($objOrders = static::findSiblingsBy('source_collection_id', $this)) !== null) {
/** @var static $objOrder */
foreach ($objOrders as $objOrder) {
if (!$objOrder->isCheckoutComplete()) {
$objOrder->delete(true);
}
}
}
// Generate tokens
$arrTokens = $this->getNotificationTokens($this->nc_notification);
// Send notification
if ($this->nc_notification) {
$blnNotificationError = true;
/** @type Notification $objNotification */
if (($objNotification = Notification::findByPk($this->nc_notification)) !== null) {
$arrResult = $objNotification->send($arrTokens, $this->language);
if (!empty($arrResult) && !in_array(false, $arrResult)) {
$blnNotificationError = false;
}
}
if ($blnNotificationError === true) {
\System::log('Error sending new order notification for order ID ' . $this->id, __METHOD__, TL_ERROR);
}
} else {
\System::log('No notification for order ID ' . $this->id, __METHOD__, TL_ERROR);
}
// Set order status only if a payment module has not already set it
if ($this->order_status == 0) {
$this->updateOrderStatus($this->getRelated('config_id')->orderstatus_new);
}
// !HOOK: post-process checkout
if (isset($GLOBALS['ISO_HOOKS']['postCheckout']) && is_array($GLOBALS['ISO_HOOKS']['postCheckout'])) {
foreach ($GLOBALS['ISO_HOOKS']['postCheckout'] as $callback) {
$objCallback = \System::importStatic($callback[0]);
$objCallback->$callback[1]($this, $arrTokens);
}
}
return true;
}
/**
* Complete order if the checkout has been made. This will cleanup session data
*
* @return bool
*/
public function complete()
{
if ($this->isCheckoutComplete()) {
unset($_SESSION['FORM_DATA']);
unset($_SESSION['FILES']);
// Retain custom config ID
if (($objCart = Isotope::getCart()) !== null && $objCart->config_id != $this->config_id) {
$objCart->config_id = $this->config_id;
}
return true;
}
return false;
}
/**
* Update the status of this order and trigger actions (email & hook)
*
* @param int $intNewStatus
*
* @return bool
*/
public function updateOrderStatus($intNewStatus)
{
// Status already set, nothing to do
if ($this->order_status == $intNewStatus) {
return true;
}
/** @var OrderStatus $objNewStatus */
$objNewStatus = OrderStatus::findByPk($intNewStatus);
if (null === $objNewStatus) {
return false;
}
// !HOOK: allow to cancel a status update
if (isset($GLOBALS['ISO_HOOKS']['preOrderStatusUpdate']) && is_array($GLOBALS['ISO_HOOKS']['preOrderStatusUpdate'])) {
foreach ($GLOBALS['ISO_HOOKS']['preOrderStatusUpdate'] as $callback) {
$objCallback = \System::importStatic($callback[0]);
$blnCancel = $objCallback->$callback[1]($this, $objNewStatus);
if ($blnCancel === true) {
return false;
}
}
}
// Add the payment date if there is none
if ($objNewStatus->isPaid() && $this->date_paid == '') {
$this->date_paid = time();
}
// Trigger notification
$blnNotificationError = null;
if ($objNewStatus->notification > 0) {
$arrTokens = $this->getNotificationTokens($objNewStatus->notification);
// Override order status and save the old one to the tokens too
$arrTokens['order_status_id'] = $objNewStatus->id;
$arrTokens['order_status'] = $objNewStatus->getName();
$arrTokens['order_status_old'] = $this->getStatusLabel();
$arrTokens['order_status_id_old'] = $this->order_status;
$blnNotificationError = true;
/** @var Notification $objNotification */
if (($objNotification = Notification::findByPk($objNewStatus->notification)) !== null) {
$arrResult = $objNotification->send($arrTokens, $this->language);
if (in_array(false, $arrResult)) {
$blnNotificationError = true;
\System::log('Error sending status update notification for order ID ' . $this->id, __METHOD__, TL_ERROR);
} elseif (!empty($arrResult)) {
$blnNotificationError = false;
}
} else {
\System::log('Invalid notification for order status ID ' . $objNewStatus->id, __METHOD__, TL_ERROR);
}
}
if (TL_MODE == 'BE') {
\Message::addConfirmation($GLOBALS['TL_LANG']['tl_iso_product_collection']['orderStatusUpdate']);
if ($blnNotificationError === true) {
\Message::addError($GLOBALS['TL_LANG']['tl_iso_product_collection']['orderStatusNotificationError']);
} elseif ($blnNotificationError === false) {
\Message::addConfirmation($GLOBALS['TL_LANG']['tl_iso_product_collection']['orderStatusNotificationSuccess']);
}
}
// Store old status and set the new one
$intOldStatus = $this->order_status;
$this->order_status = $objNewStatus->id;
$this->save();
// !HOOK: order status has been updated
if (isset($GLOBALS['ISO_HOOKS']['postOrderStatusUpdate']) && is_array($GLOBALS['ISO_HOOKS']['postOrderStatusUpdate'])) {
foreach ($GLOBALS['ISO_HOOKS']['postOrderStatusUpdate'] as $callback) {
$objCallback = \System::importStatic($callback[0]);
$objCallback->$callback[1]($this, $intOldStatus, $objNewStatus);
}
}
// Trigger payment and shipping methods that implement the interface
if (($objPayment = $this->getPaymentMethod()) !== null && $objPayment instanceof IsotopeOrderStatusAware) {
$objPayment->onOrderStatusUpdate($this, $intOldStatus, $objNewStatus);
}
if (($objShipping = $this->getShippingMethod()) !== null && $objShipping instanceof IsotopeOrderStatusAware) {
$objShipping->onOrderStatusUpdate($this, $intOldStatus, $objNewStatus);
}
return true;
}
/**
* Retrieve the array of notification data for parsing simple tokens
*
* @param int $intNotification
*
* @return array
*/
public function getNotificationTokens($intNotification)
{
/** @type \Isotope\Model\Config $objConfig */
$objConfig = $this->getRelated('config_id');
$arrTokens = deserialize($this->email_data, true);
$arrTokens['uniqid'] = $this->uniqid;
$arrTokens['order_status_id'] = $this->order_status;
$arrTokens['order_status'] = $this->getStatusLabel();
$arrTokens['recipient_email'] = $this->getEmailRecipient();
$arrTokens['order_id'] = $this->id;
$arrTokens['order_items'] = $this->sumItemsQuantity();
$arrTokens['order_products'] = $this->countItems();
$arrTokens['order_subtotal'] = Isotope::formatPriceWithCurrency($this->getSubtotal(), false);
$arrTokens['order_total'] = Isotope::formatPriceWithCurrency($this->getTotal(), false);
$arrTokens['document_number'] = $this->document_number;
$arrTokens['cart_html'] = '';
$arrTokens['cart_text'] = '';
$arrTokens['document'] = '';
// Add all the collection fields
foreach ($this->row() as $k => $v) {
$arrTokens['collection_' . $k] = $v;
}
// Add billing/customer address fields
if (($objAddress = $this->getBillingAddress()) !== null) {
foreach ($objAddress->row() as $k => $v) {
$arrTokens['billing_address_' . $k] = Format::dcaValue($objAddress->getTable(), $k, $v);
// @deprecated (use ##billing_address_*##)
$arrTokens['billing_' . $k] = $arrTokens['billing_address_' . $k];
}
$arrTokens['billing_address'] = $objAddress->generate($objConfig->getBillingFieldsConfig());
// @deprecated (use ##billing_address##)
$arrTokens['billing_address_text'] = $arrTokens['billing_address'];
}
// Add shipping address fields
if (($objAddress = $this->getShippingAddress()) !== null) {
foreach ($objAddress->row() as $k => $v) {
$arrTokens['shipping_address_' . $k] = Format::dcaValue($objAddress->getTable(), $k, $v);
// @deprecated (use ##billing_address_*##)
$arrTokens['shipping_' . $k] = $arrTokens['shipping_address_' . $k];
}
$arrTokens['shipping_address'] = $objAddress->generate($objConfig->getShippingFieldsConfig());
// Shipping address equals billing address
// @deprecated (use ##shipping_address##)
if ($objAddress->id == $this->getBillingAddress()->id) {
$arrTokens['shipping_address_text'] = ($this->requiresPayment() ? $GLOBALS['TL_LANG']['MSC']['useBillingAddress'] : $GLOBALS['TL_LANG']['MSC']['useCustomerAddress']);
} else {
$arrTokens['shipping_address_text'] = $arrTokens['shipping_address'];
}
}
// Add payment method info
if ($this->hasPayment() && ($objPayment = $this->getPaymentMethod()) !== null) {
$arrTokens['payment_id'] = $objPayment->id;
$arrTokens['payment_label'] = $objPayment->getLabel();
$arrTokens['payment_note'] = $objPayment->note;
}
// Add shipping method info
if ($this->hasShipping() && ($objShipping = $this->getShippingMethod()) !== null) {
$arrTokens['shipping_id'] = $objShipping->id;
$arrTokens['shipping_label'] = $objShipping->getLabel();
$arrTokens['shipping_note'] = $objShipping->note;
}
// Add config fields
if ($this->getRelated('config_id') !== null) {
foreach ($this->getRelated('config_id')->row() as $k => $v) {
$arrTokens['config_' . $k] = Format::dcaValue($this->getRelated('config_id')->getTable(), $k, $v);
}
}
// Add member fields
if ($this->member > 0 && $this->getRelated('member') !== null) {
foreach ($this->getRelated('member')->row() as $k => $v) {
$arrTokens['member_' . $k] = Format::dcaValue($this->getRelated('member')->getTable(), $k, $v);
}
}
if ($intNotification > 0 && ($objNotification = Notification::findByPk($intNotification)) !== null) {
$objTemplate = new \Isotope\Template($objNotification->iso_collectionTpl);
$objTemplate->isNotification = true;
$this->addToTemplate(
$objTemplate,
array(
'gallery' => $objNotification->iso_gallery,
'sorting' => $this->getItemsSortingCallable($objNotification->iso_orderCollectionBy),
)
);
$arrTokens['cart_html'] = Haste::getInstance()->call('replaceInsertTags', array($objTemplate->parse(), false));
$objTemplate->textOnly = true;
$arrTokens['cart_text'] = strip_tags(Haste::getInstance()->call('replaceInsertTags', array($objTemplate->parse(), true)));
// Generate and "attach" document
/** @var \Isotope\Interfaces\IsotopeDocument $objDocument */
if ($objNotification->iso_document > 0 && (($objDocument = Document::findByPk($objNotification->iso_document)) !== null)) {
$strFilePath = $objDocument->outputToFile($this, TL_ROOT . '/system/tmp');
$arrTokens['document'] = str_replace(TL_ROOT . '/', '', $strFilePath);
}
}
// !HOOK: add custom email tokens
if (isset($GLOBALS['ISO_HOOKS']['getOrderNotificationTokens']) && is_array($GLOBALS['ISO_HOOKS']['getOrderNotificationTokens'])) {
foreach ($GLOBALS['ISO_HOOKS']['getOrderNotificationTokens'] as $callback) {
$objCallback = \System::importStatic($callback[0]);
$arrTokens = $objCallback->$callback[1]($this, $arrTokens);
}
}
return $arrTokens;
}
/**
* Include downloads when adding items to template
*
* @param \Isotope\Template $objTemplate
* @param Callable $varCallable
*
* @return array
*/
protected function addItemsToTemplate(\Isotope\Template $objTemplate, $varCallable = null)
{
$arrItems = array();
$arrAllDownloads = array();
foreach ($this->getItems($varCallable) as $objItem) {
$arrDownloads = array();
$arrItem = $this->generateItem($objItem);
foreach ($objItem->getDownloads() as $objDownload) {
$arrDownloads = array_merge($arrDownloads, $objDownload->getForTemplate($this->isPaid()));
}
$arrItem['downloads'] = $arrDownloads;
$arrAllDownloads = array_merge($arrAllDownloads, $arrDownloads);
$arrItems[] = $arrItem;
}
RowClass::withKey('rowClass')->addCount('row_')->addFirstLast('row_')->addEvenOdd('row_')->applyTo($arrItems);
$objTemplate->items = $arrItems;
$objTemplate->downloads = $arrAllDownloads;
return $arrItems;
}
/**
* Generate unique order ID including the order prefix
*
* @return string
*/
protected function generateUniqueId()
{
if ($this->arrData['uniqid'] != '') {
return $this->arrData['uniqid'];
}
$objConfig = $this->getRelated('config_id');
if (null === $objConfig) {
$objConfig = Isotope::getConfig();
}
return uniqid(Haste::getInstance()->call('replaceInsertTags', array((string) $objConfig->orderPrefix, false)), true);
}
}