/
Order.php
1833 lines (1642 loc) · 52.5 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Magento Enterprise Edition
*
* NOTICE OF LICENSE
*
* This source file is subject to the Magento Enterprise Edition License
* that is bundled with this package in the file LICENSE_EE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magentocommerce.com/license/enterprise-edition
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Sales
* @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
* @license http://www.magentocommerce.com/license/enterprise-edition
*/
/**
* Order model
*
* Supported events:
* sales_order_load_after
* sales_order_save_before
* sales_order_save_after
* sales_order_delete_before
* sales_order_delete_after
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Sales_Model_Order extends Mage_Sales_Model_Abstract
{
/**
* XML configuration paths
*/
const XML_PATH_EMAIL_TEMPLATE = 'sales_email/order/template';
const XML_PATH_EMAIL_GUEST_TEMPLATE = 'sales_email/order/guest_template';
const XML_PATH_EMAIL_IDENTITY = 'sales_email/order/identity';
const XML_PATH_EMAIL_COPY_TO = 'sales_email/order/copy_to';
const XML_PATH_EMAIL_COPY_METHOD = 'sales_email/order/copy_method';
const XML_PATH_EMAIL_ENABLED = 'sales_email/order/enabled';
const XML_PATH_UPDATE_EMAIL_TEMPLATE = 'sales_email/order_comment/template';
const XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE = 'sales_email/order_comment/guest_template';
const XML_PATH_UPDATE_EMAIL_IDENTITY = 'sales_email/order_comment/identity';
const XML_PATH_UPDATE_EMAIL_COPY_TO = 'sales_email/order_comment/copy_to';
const XML_PATH_UPDATE_EMAIL_COPY_METHOD = 'sales_email/order_comment/copy_method';
const XML_PATH_UPDATE_EMAIL_ENABLED = 'sales_email/order_comment/enabled';
/**
* Order states
*/
const STATE_NEW = 'new';
const STATE_PENDING_PAYMENT = 'pending_payment';
const STATE_PROCESSING = 'processing';
const STATE_COMPLETE = 'complete';
const STATE_CLOSED = 'closed';
const STATE_CANCELED = 'canceled';
const STATE_HOLDED = 'holded';
const STATE_PAYMENT_REVIEW = 'payment_review';
/**
* Order flags
*/
const ACTION_FLAG_CANCEL = 'cancel';
const ACTION_FLAG_HOLD = 'hold';
const ACTION_FLAG_UNHOLD = 'unhold';
const ACTION_FLAG_EDIT = 'edit';
const ACTION_FLAG_CREDITMEMO= 'creditmemo';
const ACTION_FLAG_INVOICE = 'invoice';
const ACTION_FLAG_REORDER = 'reorder';
const ACTION_FLAG_SHIP = 'ship';
const ACTION_FLAG_COMMENT = 'comment';
/**
* Report date types
*/
const REPORT_DATE_TYPE_CREATED = 'created';
const REPORT_DATE_TYPE_UPDATED = 'updated';
protected $_eventPrefix = 'sales_order';
protected $_eventObject = 'order';
protected $_addresses = null;
protected $_items = null;
protected $_payments = null;
protected $_statusHistory = null;
protected $_invoices;
protected $_tracks;
protected $_shipments;
protected $_creditmemos;
protected $_relatedObjects = array();
protected $_orderCurrency = null;
protected $_baseCurrency = null;
/**
* Array of action flags for canUnhold, canEdit, etc.
*
* @var array
*/
protected $_actionFlag = array();
/**
* Initialize resource model
*/
protected function _construct()
{
$this->_init('sales/order');
}
/**
* Clear order object data
*
* @param string $key data key
* @return Mage_Sales_Model_Order
*/
public function unsetData($key=null)
{
parent::unsetData($key);
if (is_null($key)) {
$this->_items = null;
}
return $this;
}
/**
* Retrieve can flag for action (edit, unhold, etc..)
*
* @param string $action
* @return boolean|null
*/
public function getActionFlag($action)
{
if (isset($this->_actionFlag[$action])) {
return $this->_actionFlag[$action];
}
return null;
}
/**
* Set can flag value for action (edit, unhold, etc...)
*
* @param string $action
* @param boolean $flag
* @return Mage_Sales_Model_Order
*/
public function setActionFlag($action, $flag)
{
$this->_actionFlag[$action] = (boolean) $flag;
return $this;
}
/**
* Load order by system increment identifier
*
* @param string $incrementId
* @return Mage_Sales_Model_Order
*/
public function loadByIncrementId($incrementId)
{
return $this->loadByAttribute('increment_id', $incrementId);
}
/**
* Load order by custom attribute value. Attribute value should be unique
*
* @param string $attribute
* @param string $value
* @return Mage_Sales_Model_Order
*/
public function loadByAttribute($attribute, $value)
{
$this->load($value, $attribute);
return $this;
}
/**
* Retrieve store model instance
*
* @return Mage_Core_Model_Store
*/
public function getStore()
{
$storeId = $this->getStoreId();
if ($storeId) {
return Mage::app()->getStore($storeId);
}
return Mage::app()->getStore();
}
/**
* Retrieve order cancel availability
*
* @return bool
*/
public function canCancel()
{
if ($this->canUnhold()) { // $this->isPaymentReview()
return false;
}
$allInvoiced = true;
foreach ($this->getAllItems() as $item) {
if ($item->getQtyToInvoice()) {
$allInvoiced = false;
break;
}
}
if ($allInvoiced) {
return false;
}
$state = $this->getState();
if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_CANCEL) === false) {
return false;
}
/**
* Use only state for availability detect
*/
/*foreach ($this->getAllItems() as $item) {
if ($item->getQtyToCancel()>0) {
return true;
}
}
return false;*/
return true;
}
/**
* Getter whether the payment can be voided
* @return bool
*/
public function canVoidPayment()
{
if ($this->canUnhold() || $this->isPaymentReview()) {
return false;
}
$state = $this->getState();
if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
return false;
}
return $this->getPayment()->canVoid(new Varien_Object);
}
/**
* Retrieve order invoice availability
*
* @return bool
*/
public function canInvoice()
{
if ($this->canUnhold() || $this->isPaymentReview()) {
return false;
}
$state = $this->getState();
if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_INVOICE) === false) {
return false;
}
foreach ($this->getAllItems() as $item) {
if ($item->getQtyToInvoice()>0 && !$item->getLockedDoInvoice()) {
return true;
}
}
return false;
}
/**
* Retrieve order credit memo (refund) availability
*
* @return bool
*/
public function canCreditmemo()
{
if ($this->hasForcedCanCreditmemo()) {
return $this->getForcedCanCreditmemo();
}
if ($this->canUnhold() || $this->isPaymentReview()) {
return false;
}
if ($this->isCanceled() || $this->getState() === self::STATE_CLOSED) {
return false;
}
/**
* We can have problem with float in php (on some server $a=762.73;$b=762.73; $a-$b!=0)
* for this we have additional diapason for 0
*/
if (abs($this->getTotalPaid()-$this->getTotalRefunded())<.0001) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_EDIT) === false) {
return false;
}
return true;
}
/**
* Retrieve order hold availability
*
* @return bool
*/
public function canHold()
{
$state = $this->getState();
if ($this->isCanceled() || $this->isPaymentReview()
|| $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED || $state === self::STATE_HOLDED) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_HOLD) === false) {
return false;
}
return true;
}
/**
* Retrieve order unhold availability
*
* @return bool
*/
public function canUnhold()
{
if ($this->getActionFlag(self::ACTION_FLAG_UNHOLD) === false || $this->isPaymentReview()) {
return false;
}
return $this->getState() === self::STATE_HOLDED;
}
/**
* Check if comment can be added to order history
*
* @return bool
*/
public function canComment()
{
if ($this->getActionFlag(self::ACTION_FLAG_COMMENT) === false) {
return false;
}
return true;
}
/**
* Retrieve order shipment availability
*
* @return bool
*/
public function canShip()
{
if ($this->canUnhold() || $this->isPaymentReview()) {
return false;
}
if ($this->getIsVirtual() || $this->isCanceled()) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_SHIP) === false) {
return false;
}
foreach ($this->getAllItems() as $item) {
if ($item->getQtyToShip()>0 && !$item->getIsVirtual()
&& !$item->getLockedDoShip())
{
return true;
}
}
return false;
}
/**
* Retrieve order edit availability
*
* @return bool
*/
public function canEdit()
{
if ($this->canUnhold()) {
return false;
}
$state = $this->getState();
if ($this->isCanceled() || $this->isPaymentReview()
|| $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
return false;
}
if (!$this->getPayment()->getMethodInstance()->canEdit()) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_EDIT) === false) {
return false;
}
return true;
}
/**
* Retrieve order reorder availability
*
* @return bool
*/
public function canReorder()
{
if ($this->canUnhold() || $this->isPaymentReview() || !$this->getCustomerId()) {
return false;
}
$products = array();
foreach ($this->getItemsCollection() as $item) {
$products[] = $item->getProductId();
}
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->setStoreId($this->getStoreId());
if (!empty($products)) {
$productsCollection->addIdFilter($products)
->addAttributeToSelect('status')
->load();
foreach ($productsCollection as $product) {
if (!$product->isSalable()) {
return false;
}
}
}
if ($this->getActionFlag(self::ACTION_FLAG_REORDER) === false) {
return false;
}
return true;
}
/**
* Check whether the payment is in payment review state
* In this state order cannot be normally processed. Possible actions can be:
* - accept or deny payment
* - fetch transaction information
*
* @return bool
*/
public function isPaymentReview()
{
return $this->getState() === self::STATE_PAYMENT_REVIEW;
}
/**
* Check whether payment can be accepted or denied
*
* @return bool
*/
public function canReviewPayment()
{
return $this->isPaymentReview() && $this->getPayment()->canReviewPayment();
}
/**
* Check whether there can be a transaction update fetched for payment in review state
*
* @return bool
*/
public function canFetchPaymentReviewUpdate()
{
return $this->isPaymentReview() && $this->getPayment()->canFetchTransactionInfo();
}
/**
* Retrieve order configuration model
*
* @return Mage_Sales_Model_Order_Config
*/
public function getConfig()
{
return Mage::getSingleton('sales/order_config');
}
/**
* Place order payments
*
* @return Mage_Sales_Model_Order
*/
protected function _placePayment()
{
$this->getPayment()->place();
return $this;
}
/**
* Retrieve order payment model object
*
* @return Mage_Sales_Model_Order_Payment
*/
public function getPayment()
{
foreach ($this->getPaymentsCollection() as $payment) {
if (!$payment->isDeleted()) {
return $payment;
}
}
return false;
}
/**
* Declare order billing address
*
* @param Mage_Sales_Model_Order_Address $address
* @return Mage_Sales_Model_Order
*/
public function setBillingAddress(Mage_Sales_Model_Order_Address $address)
{
$old = $this->getBillingAddress();
if (!empty($old)) {
$address->setId($old->getId());
}
$this->addAddress($address->setAddressType('billing'));
return $this;
}
/**
* Declare order shipping address
*
* @param Mage_Sales_Model_Order_Address $address
* @return Mage_Sales_Model_Order
*/
public function setShippingAddress(Mage_Sales_Model_Order_Address $address)
{
$old = $this->getShippingAddress();
if (!empty($old)) {
$address->setId($old->getId());
}
$this->addAddress($address->setAddressType('shipping'));
return $this;
}
/**
* Retrieve order billing address
*
* @return Mage_Sales_Model_Order_Address
*/
public function getBillingAddress()
{
foreach ($this->getAddressesCollection() as $address) {
if ($address->getAddressType()=='billing' && !$address->isDeleted()) {
return $address;
}
}
return false;
}
/**
* Retrieve order shipping address
*
* @return Mage_Sales_Model_Order_Address
*/
public function getShippingAddress()
{
foreach ($this->getAddressesCollection() as $address) {
if ($address->getAddressType()=='shipping' && !$address->isDeleted()) {
return $address;
}
}
return false;
}
/**
* Order state setter.
* If status is specified, will add order status history with specified comment
* the setData() cannot be overriden because of compatibility issues with resource model
*
* @param string $state
* @param string|bool $status
* @param string $comment
* @param bool $isCustomerNotified
* @return Mage_Sales_Model_Order
*/
public function setState($state, $status = false, $comment = '', $isCustomerNotified = null)
{
return $this->_setState($state, $status, $comment, $isCustomerNotified, true);
}
/**
* Order state protected setter.
* By default allows to set any state. Can also update status to default or specified value
* Сomplete and closed states are encapsulated intentionally, see the _checkState()
*
* @param string $state
* @param string|bool $status
* @param string $comment
* @param bool $isCustomerNotified
* @param $shouldProtectState
* @return Mage_Sales_Model_Order
*/
protected function _setState($state, $status = false, $comment = '', $isCustomerNotified = null, $shouldProtectState = false)
{
// attempt to set the specified state
if ($shouldProtectState) {
if ($this->isStateProtected($state)) {
Mage::throwException(Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state));
}
}
$this->setData('state', $state);
// add status history
if ($status) {
if ($status === true) {
$status = $this->getConfig()->getStateDefaultStatus($state);
}
$this->setStatus($status);
$history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again
$history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility
}
return $this;
}
/**
* Whether specified state can be set from outside
* @param $state
* @return bool
*/
public function isStateProtected($state)
{
if (empty($state)) {
return false;
}
return self::STATE_COMPLETE == $state || self::STATE_CLOSED == $state;
}
/**
* Retrieve label of order status
*
* @return string
*/
public function getStatusLabel()
{
return $this->getConfig()->getStatusLabel($this->getStatus());
}
/**
* Add status change information to history
* @deprecated after 1.4.0.0-alpha3
*
* @param string $status
* @param string $comment
* @param bool $isCustomerNotified
* @return Mage_Sales_Model_Order
*/
public function addStatusToHistory($status, $comment = '', $isCustomerNotified = false)
{
$history = $this->addStatusHistoryComment($comment, $status)
->setIsCustomerNotified($isCustomerNotified);
return $this;
}
/*
* Add a comment to order
* Different or default status may be specified
*
* @param string $comment
* @param string $status
* @return Mage_Sales_Order_Status_History
*/
public function addStatusHistoryComment($comment, $status = false)
{
if (false === $status) {
$status = $this->getStatus();
} elseif (true === $status) {
$status = $this->getConfig()->getStateDefaultStatus($this->getState());
} else {
$this->setStatus($status);
}
$history = Mage::getModel('sales/order_status_history')
->setStatus($status)
->setComment($comment);
$this->addStatusHistory($history);
return $history;
}
/**
* Place order
*
* @return Mage_Sales_Model_Order
*/
public function place()
{
Mage::dispatchEvent('sales_order_place_before', array('order'=>$this));
$this->_placePayment();
Mage::dispatchEvent('sales_order_place_after', array('order'=>$this));
return $this;
}
public function hold()
{
if (!$this->canHold()) {
Mage::throwException(Mage::helper('sales')->__('Hold action is not available.'));
}
$this->setHoldBeforeState($this->getState());
$this->setHoldBeforeStatus($this->getStatus());
$this->setState(self::STATE_HOLDED, true);
return $this;
}
/**
* Attempt to unhold the order
*
* @return Mage_Sales_Model_Order
* @throws Mage_Core_Exception
*/
public function unhold()
{
if (!$this->canUnhold()) {
Mage::throwException(Mage::helper('sales')->__('Unhold action is not available.'));
}
$this->setState($this->getHoldBeforeState(), $this->getHoldBeforeStatus());
$this->setHoldBeforeState(null);
$this->setHoldBeforeStatus(null);
return $this;
}
/**
* Cancel order
*
* @return Mage_Sales_Model_Order
*/
public function cancel()
{
if ($this->canCancel()) {
$this->getPayment()->cancel();
$this->registerCancellation();
}
return $this;
}
/**
* Prepare order totlas to cancellation
* @param string $comment
* @param bool $graceful
* @return Mage_Sales_Model_Order
* @throws Mage_Core_Exception
*/
public function registerCancellation($comment = '', $graceful = true)
{
if ($this->canCancel()) {
$cancelState = self::STATE_CANCELED;
foreach ($this->getAllItems() as $item) {
if ($cancelState != self::STATE_PROCESSING && $item->getQtyToRefund()) {
if ($item->getQtyToShip() > $item->getQtyToCancel()) {
$cancelState = self::STATE_PROCESSING;
} else {
$cancelState = self::STATE_COMPLETE;
}
}
$item->cancel();
}
$this->setSubtotalCanceled($this->getSubtotal() - $this->getSubtotalInvoiced());
$this->setBaseSubtotalCanceled($this->getBaseSubtotal() - $this->getBaseSubtotalInvoiced());
$this->setTaxCanceled($this->getTaxAmount() - $this->getTaxInvoiced());
$this->setBaseTaxCanceled($this->getBaseTaxAmount() - $this->getBaseTaxInvoiced());
$this->setShippingCanceled($this->getShippingAmount() - $this->getShippingInvoiced());
$this->setBaseShippingCanceled($this->getBaseShippingAmount() - $this->getBaseShippingInvoiced());
$this->setDiscountCanceled(abs($this->getDiscountAmount()) - $this->getDiscountInvoiced());
$this->setBaseDiscountCanceled(abs($this->getBaseDiscountAmount()) - $this->getBaseDiscountInvoiced());
$this->setTotalCanceled($this->getSubtotalCanceled() + $this->getTaxCanceled() + $this->getShippingCanceled() - $this->getDiscountCanceled());
$this->setBaseTotalCanceled($this->getBaseSubtotalCanceled() + $this->getBaseTaxCanceled() + $this->getBaseShippingCanceled() - $this->getBaseDiscountCanceled());
$this->_setState($cancelState, true, $comment);
} elseif (!$graceful) {
Mage::throwException(Mage::helper('sales')->__('Order does not allow to be canceled.'));
}
return $this;
}
/**
* Retrieve tracking numbers
*
* @return array
*/
public function getTrackingNumbers()
{
if ($this->getData('tracking_numbers')) {
return explode(',', $this->getData('tracking_numbers'));
}
return array();
}
public function getShippingCarrier()
{
$carrierModel = $this->getData('shipping_carrier');
if (is_null($carrierModel)) {
$carrierModel = false;
/**
* $method - carrier_method
*/
if ($method = $this->getShippingMethod()) {
$data = explode('_', $method);
$carrierCode = $data[0];
$className = Mage::getStoreConfig('carriers/'.$carrierCode.'/model');
if ($className) {
$carrierModel = Mage::getModel($className);
}
}
$this->setData('shipping_carrier', $carrierModel);
}
return $carrierModel;
}
/**
* Sending email with order data
*
* @return Mage_Sales_Model_Order
*/
public function sendNewOrderEmail()
{
if (!Mage::helper('sales')->canSendNewOrderEmail($this->getStore()->getId())) {
return $this;
}
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
$paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore($this->getStore()->getId());
$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $this->getStoreId());
if ($copyTo && $copyMethod == 'bcc') {
foreach ($copyTo as $email) {
$mailTemplate->addBcc($email);
}
}
if ($this->getCustomerIsGuest()) {
$template = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $this->getStoreId());
$customerName = $this->getBillingAddress()->getName();
} else {
$template = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $this->getStoreId());
$customerName = $this->getCustomerName();
}
$sendTo = array(
array(
'email' => $this->getCustomerEmail(),
'name' => $customerName
)
);
if ($copyTo && $copyMethod == 'copy') {
foreach ($copyTo as $email) {
$sendTo[] = array(
'email' => $email,
'name' => null
);
}
}
foreach ($sendTo as $recipient) {
$mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$this->getStoreId()))
->sendTransactional(
$template,
Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $this->getStoreId()),
$recipient['email'],
$recipient['name'],
array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlock->toHtml(),
)
);
}
$this->setEmailSent(true);
$this->_getResource()->saveAttribute($this, 'email_sent');
$translate->setTranslateInline(true);
return $this;
}
/**
* Sending email with order update information
*
* @return Mage_Sales_Model_Order
*/
public function sendOrderUpdateEmail($notifyCustomer=true, $comment='')
{
if (!Mage::helper('sales')->canSendOrderCommentEmail($this->getStore()->getId())) {
return $this;
}
$copyTo = $this->_getEmails(self::XML_PATH_UPDATE_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_COPY_METHOD, $this->getStoreId());
if (!$notifyCustomer && !$copyTo) {
return $this;
}
// set design parameters, required for email (remember current)
$currentDesign = Mage::getDesign()->setAllGetOld(array(
'store' => $this->getStoreId(),
'area' => 'frontend',
'package' => Mage::getStoreConfig('design/package/name', $this->getStoreId()),
));
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
$sendTo = array();
$mailTemplate = Mage::getModel('core/email_template');
if ($this->getCustomerIsGuest()) {
$template = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE, $this->getStoreId());
$customerName = $this->getBillingAddress()->getName();
} else {
$template = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_TEMPLATE, $this->getStoreId());
$customerName = $this->getCustomerName();
}
if ($notifyCustomer) {
$sendTo[] = array(
'name' => $customerName,
'email' => $this->getCustomerEmail()
);
if ($copyTo && $copyMethod == 'bcc') {
foreach ($copyTo as $email) {
$mailTemplate->addBcc($email);
}
}
}
if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
foreach ($copyTo as $email) {
$sendTo[] = array(
'name' => null,
'email' => $email
);
}
}
foreach ($sendTo as $recipient) {
$mailTemplate->setDesignConfig(array('area'=>'frontend', 'store' => $this->getStoreId()))
->sendTransactional(
$template,
Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_IDENTITY, $this->getStoreId()),
$recipient['email'],
$recipient['name'],
array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'comment' => $comment
)
);
}
$translate->setTranslateInline(true);
// revert current design
Mage::getDesign()->setAllGetOld($currentDesign);
return $this;
}
protected function _getEmails($configPath)
{
$data = Mage::getStoreConfig($configPath, $this->getStoreId());
if (!empty($data)) {
return explode(',', $data);
}
return false;
}