1 <?php
2 /* --------------------------------------------------------------
3 GxEngineOrder.inc.php 2016-02-16
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2016 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('OrderInterface');
13 MainFactory::load_class('AddonValueContainerInterface');
14
15 /**
16 * Class GXEngineOrder
17 *
18 * This class is used for managing order data.
19 *
20 * @category System
21 * @package Order
22 * @subpackage Entities
23 */
24 class GXEngineOrder implements OrderInterface, AddonValueContainerInterface
25 {
26 /**
27 * Order ID.
28 *
29 * @var int
30 */
31 protected $orderId = 0;
32
33 /**
34 * Unique order hash.
35 *
36 * @var string
37 */
38 protected $orderHash = '';
39
40 /**
41 * Customer ID.
42 *
43 * @var int
44 */
45 protected $customerId = 0;
46
47 /**
48 * Customer email address.
49 *
50 * @var string
51 */
52 protected $customerEmail = '';
53
54 /**
55 * Customer telephone number.
56 *
57 * @var string
58 */
59 protected $customerTelephone = '';
60
61 /**
62 * Order Status ID.
63 *
64 * @var int
65 */
66 protected $statusId = 0;
67
68 /**
69 * Customer number.
70 * @var string
71 */
72 protected $customerNumber = '';
73
74 /**
75 * VAT ID number.
76 * @var string
77 */
78 protected $vatIdNumber = '';
79
80 /**
81 * Customer status information.
82 * @var CustomerStatusInformation
83 */
84 protected $customerStatusInformation;
85
86 /**
87 * Customer address.
88 *
89 * @var AddressBlockInterface
90 */
91 protected $customerAddress;
92
93 /**
94 * Billing address.
95 *
96 * @var AddressBlockInterface
97 */
98 protected $billingAddress;
99
100 /**
101 * Delivery address.
102 *
103 * @var AddressBlockInterface
104 */
105 protected $deliveryAddress;
106
107 /**
108 * Order items.
109 *
110 * @var OrderItemCollection
111 */
112 protected $orderItems;
113
114 /**
115 * Order totals.
116 *
117 * @var OrderTotalCollection
118 */
119 protected $orderTotals;
120
121 /**
122 * Order shipping type.
123 *
124 * @var OrderShippingType
125 */
126 protected $shippingType;
127
128 /**
129 * Order payment type.
130 *
131 * @var OrderPaymentType
132 */
133 protected $paymentType;
134
135 /**
136 * Order currency code.
137 *
138 * @var CurrencyCode
139 */
140 protected $currencyCode;
141
142 /**
143 * Order language code.
144 *
145 * @var LanguageCode
146 */
147 protected $languageCode;
148
149 /**
150 * Order purchase date time.
151 *
152 * @var DateTime
153 */
154 protected $purchaseDateTime;
155
156 /**
157 * Order last modified date time
158 *
159 * @var DateTime
160 */
161 protected $lastModifiedDateTime;
162
163 /**
164 * Order comment.
165 *
166 * @var string
167 */
168 protected $comment = '';
169
170 /**
171 * Order addon collection.
172 *
173 * @var EditableKeyValueCollection
174 */
175 protected $addonValues;
176
177 /**
178 * Order status history.
179 *
180 * @var OrderStatusHistoryStorage
181 */
182 protected $orderStatusHistoryReader;
183
184
185 /**
186 * GXEngineOrder constructor.
187 */
188 public function __construct()
189 {
190 // Set object properties which need a AddressBlock.
191 $this->_initializeAddressProperties();
192
193 // Set object properties which need collections.
194 $this->_initializeCollectionProperties();
195
196 // Set object properties which need a
197 // type or code object like 'OrderShippingType' or 'CurrencyCode'.
198 $this->_initializeTypeAndCodeProperties();
199
200 // Set $purchaseDateTime and $lastModifiedDateTime to current datetime as default value.
201 $this->setPurchaseDateTime(new DateTime());
202 $this->setLastModifiedDateTime(new DateTime());
203
204 // Set empty CustomerStatusInformation object
205 $this->setCustomerStatusInformation(MainFactory::create('CustomerStatusInformation', new IdType(1)));
206 }
207
208
209 /**
210 * Initializes default values for AddressBlock properties.
211 */
212 protected function _initializeAddressProperties()
213 {
214 // Create value objects.
215 $gender = MainFactory::create('CustomerGender', '');
216 $firstName = MainFactory::create('CustomerFirstname', '');
217 $lastName = MainFactory::create('CustomerLastname', '');
218 $company = MainFactory::create('CustomerCompany', '');
219 $B2BStatus = MainFactory::create('CustomerB2BStatus', true);
220 $street = MainFactory::create('CustomerStreet', '');
221 $suburb = MainFactory::create('CustomerSuburb', '');
222 $postCode = MainFactory::create('CustomerPostcode', '');
223 $city = MainFactory::create('CustomerCity', '');
224 $country = MainFactory::create('CustomerCountry', new IdType(123),
225 MainFactory::create('CustomerCountryName', ''),
226 MainFactory::create('CustomerCountryIso2', ''),
227 MainFactory::create('CustomerCountryIso3', ''), new IdType(823), true);
228 $countryZone = MainFactory::create('CustomerCountryZone', new IdType(123),
229 MainFactory::create('CustomerCountryZoneName', ''),
230 MainFactory::create('CustomerCountryZoneIsoCode', ''));
231
232 // Set customer address.
233 $customerAddress = MainFactory::create('AddressBlock', $gender, $firstName, $lastName, $company, $B2BStatus,
234 $street, $suburb, $postCode, $city, $country, $countryZone);
235 $this->setCustomerAddress($customerAddress);
236
237 // Set billing address.
238 $billingAddress = MainFactory::create('AddressBlock', $gender, $firstName, $lastName, $company, $B2BStatus,
239 $street, $suburb, $postCode, $city, $country, $countryZone);
240 $this->setBillingAddress($billingAddress);
241
242 // Set delivery address.
243 $deliveryAddress = MainFactory::create('AddressBlock', $gender, $firstName, $lastName, $company, $B2BStatus,
244 $street, $suburb, $postCode, $city, $country, $countryZone);
245 $this->setDeliveryAddress($deliveryAddress);
246 }
247
248
249 /**
250 * Initializes default values for Collection properties.
251 */
252 protected function _initializeCollectionProperties()
253 {
254 // Create empty array as collection.
255 $collection = array();
256
257 // Set order items.
258 $orderItems = MainFactory::create('OrderItemCollection', $collection);
259 $this->setOrderItems($orderItems);
260
261 // Set order totals.
262 $orderTotals = MainFactory::create('OrderTotalCollection', $collection);
263 $this->setOrderTotals($orderTotals);
264
265 // Set addon values collection.
266 // Note, that there is no setter method for assign the addonValues collection.
267 $addonValues = MainFactory::create('EditableKeyValueCollection', $collection);
268 $this->addonValues = $addonValues;
269 }
270
271
272 /**
273 * Initializes default values for type and code properties.
274 */
275 protected function _initializeTypeAndCodeProperties()
276 {
277 // Create empty title for type value objects.
278 $title = new StringType('');
279
280 // Create empty module for type value objects.
281 $module = new StringType('');
282
283 // Set type object: shipping type.
284 $shippingType = MainFactory::create('OrderShippingType', $title, $module);
285 $this->setShippingType($shippingType);
286
287 // Set type object: payment type.
288 $paymentType = MainFactory::create('OrderPaymentType', $title, $module);
289 $this->setPaymentType($paymentType);
290
291 // Set code object: currency code.
292 $currencyCode = MainFactory::create('EmptyCurrencyCode');
293 $this->setCurrencyCode($currencyCode);
294
295 // Set code object: language code.
296 $languageCode = MainFactory::create('EmptyLanguageCode');
297 $this->setLanguageCode($languageCode);
298 }
299
300
301 /**
302 * Sets the order ID.
303 *
304 * Note, that the ID will be saved as string to the class property.
305 *
306 * @param IdType $id Order ID.
307 *
308 * @return GXEngineOrder Same instance for method chaining.
309 */
310 public function setOrderId(IdType $id)
311 {
312 $this->orderId = $id->asInt();
313
314 return $this;
315 }
316
317
318 /**
319 * Returns the order ID.
320 *
321 * @return int Order ID.
322 */
323 public function getOrderId()
324 {
325 return $this->orderId;
326 }
327
328
329 /**
330 * Sets the unique order hash.
331 *
332 * @param StringType $orderHash Unique order hash.
333 *
334 * @return GXEngineOrder Same instance for method chaining.
335 */
336 public function setOrderHash(StringType $orderHash)
337 {
338 $this->orderHash = $orderHash->asString();
339
340 return $this;
341 }
342
343
344 /**
345 * Returns the unique order hash.
346 *
347 * @return string Unique order hash.
348 */
349 public function getOrderHash()
350 {
351 return $this->orderHash;
352 }
353
354
355 /**
356 * Sets the associated customer ID.
357 *
358 * @param IdType $id Customer ID.
359 *
360 * @return GXEngineOrder Same instance for method chaining.
361 */
362 public function setCustomerId(IdType $id)
363 {
364 $this->customerId = $id->asInt();
365
366 return $this;
367 }
368
369
370 /**
371 * Returns the associated customer ID.
372 *
373 * @return int Associated customer ID.
374 */
375 public function getCustomerId()
376 {
377 return $this->customerId;
378 }
379
380
381 /**
382 * Sets the customer email address.
383 *
384 * @param EmailStringType $email Customer email address.
385 *
386 * @return GXEngineOrder Same instance for method chaining.
387 */
388 public function setCustomerEmail(EmailStringType $email)
389 {
390 $this->customerEmail = $email->asString();
391
392 return $this;
393 }
394
395
396 /**
397 * Returns the associated customer email address.
398 *
399 * @return string Associated customer email address.
400 */
401 public function getCustomerEmail()
402 {
403 return $this->customerEmail;
404 }
405
406
407 /**
408 * Sets the customer telephone number.
409 *
410 * @param StringType $telephone Customer telephone number.
411 *
412 * @return GXEngineOrder Same instance for method chaining.
413 */
414 public function setCustomerTelephone(StringType $telephone)
415 {
416 $this->customerTelephone = $telephone->asString();
417
418 return $this;
419 }
420
421
422 /**
423 * Returns the associated customer telephone number.
424 *
425 * @return string Associated customer telephone number.
426 */
427 public function getCustomerTelephone()
428 {
429 return $this->customerTelephone;
430 }
431
432
433 /**
434 * Sets the order status ID.
435 *
436 * @param IdType $id Status ID.
437 *
438 * @return GXEngineOrder Same instance for method chaining.
439 */
440 public function setStatusId(IdType $id)
441 {
442 $this->statusId = $id->asInt();
443
444 return $this;
445 }
446
447
448 /**
449 * Returns the order status ID.
450 *
451 * @return int Order status ID.
452 */
453 public function getStatusId()
454 {
455 return $this->statusId;
456 }
457
458
459 /**
460 * Sets customer number of Order
461 *
462 * @param StringType $customerNumber Customer number.
463 *
464 * @return GXEngineOrder Same instance for method chaining.
465 */
466 public function setCustomerNumber(StringType $customerNumber)
467 {
468 $this->customerNumber = $customerNumber->asString();
469 }
470
471
472 /**
473 * Returns the customer number.
474 *
475 * @return string Customer number.
476 */
477 public function getCustomerNumber()
478 {
479 return $this->customerNumber;
480 }
481
482
483 /**
484 * Sets the VAT ID Number.
485 *
486 * @param StringType $vatIdNumber VAT ID number.
487 *
488 * @return GXEngineOrder Same instance for method chaining.
489 */
490 public function setVatIdNumber(StringType $vatIdNumber)
491 {
492 $this->vatIdNumber = $vatIdNumber->asString();
493 }
494
495
496 /**
497 * Returns the VAT ID number.
498 *
499 * @return string VAT ID number.
500 */
501 public function getVatIdNumber()
502 {
503 return $this->vatIdNumber;
504 }
505
506
507 /**
508 * Sets the customer status information.
509 *
510 * @param CustomerStatusInformation $customerStatusInformation Customer status information.
511 */
512 public function setCustomerStatusInformation(CustomerStatusInformation $customerStatusInformation)
513 {
514 $this->customerStatusInformation = $customerStatusInformation;
515 }
516
517
518 /**
519 * Returns the Customer status information.
520 *
521 * @return CustomerStatusInformation Customer status information.
522 */
523 public function getCustomerStatusInformation()
524 {
525 return $this->customerStatusInformation;
526 }
527
528
529 /**
530 * Sets the customer address.
531 *
532 * @param AddressBlockInterface $address Customer address.
533 *
534 * @return GXEngineOrder Same instance for method chaining.
535 */
536 public function setCustomerAddress(AddressBlockInterface $address)
537 {
538 $this->customerAddress = $address;
539
540 return $this;
541 }
542
543
544 /**
545 * Returns the customer address.
546 *
547 * @return AddressBlockInterface Customer address.
548 */
549 public function getCustomerAddress()
550 {
551 return $this->customerAddress;
552 }
553
554
555 /**
556 * Sets the billing address.
557 *
558 * @param AddressBlockInterface $address Billing address.
559 *
560 * @return GXEngineOrder Same instance for method chaining.
561 */
562 public function setBillingAddress(AddressBlockInterface $address)
563 {
564 $this->billingAddress = $address;
565
566 return $this;
567 }
568
569
570 /**
571 * Returns the billing address.
572 *
573 * @return AddressBlockInterface Billing address.
574 */
575 public function getBillingAddress()
576 {
577 return $this->billingAddress;
578 }
579
580
581 /**
582 * Sets the delivery address.
583 *
584 * @param AddressBlockInterface $address Delivery address.
585 *
586 * @return GXEngineOrder Same instance for method chaining.
587 */
588 public function setDeliveryAddress(AddressBlockInterface $address)
589 {
590 $this->deliveryAddress = $address;
591
592 return $this;
593 }
594
595
596 /**
597 * Returns the delivery address.
598 *
599 * @return AddressBlockInterface Delivery address.
600 */
601 public function getDeliveryAddress()
602 {
603 return $this->deliveryAddress;
604 }
605
606
607 /**
608 * Sets the order items collection.
609 *
610 * @param OrderItemCollection $collection Items collection.
611 *
612 * @return GXEngineOrder Same instance for method chaining.
613 */
614 public function setOrderItems(OrderItemCollection $collection)
615 {
616 $this->orderItems = $collection;
617
618 return $this;
619 }
620
621
622 /**
623 * Returns the order items collection.
624 *
625 * @return OrderItemCollection Order items collection.
626 */
627 public function getOrderItems()
628 {
629 return $this->orderItems;
630 }
631
632
633 /**
634 * Sets the order total collection.
635 *
636 * @param OrderTotalCollection $collection Total collection.
637 *
638 * @return GXEngineOrder Same instance for method chaining.
639 */
640 public function setOrderTotals(OrderTotalCollection $collection)
641 {
642 $this->orderTotals = $collection;
643
644 return $this;
645 }
646
647
648 /**
649 * Returns the order totals collection.
650 *
651 * @return OrderTotalCollection Order totals collection.
652 */
653 public function getOrderTotals()
654 {
655 return $this->orderTotals;
656 }
657
658
659 /**
660 * Sets the order shipping type.
661 *
662 * @param OrderShippingType $shippingType Shipping type.
663 *
664 * @return GXEngineOrder Same instance for method chaining.
665 */
666 public function setShippingType(OrderShippingType $shippingType)
667 {
668 $this->shippingType = $shippingType;
669
670 return $this;
671 }
672
673
674 /**
675 * Returns the order shipping type.
676 *
677 * @return OrderShippingType Order shipping type.
678 */
679 public function getShippingType()
680 {
681 return $this->shippingType;
682 }
683
684
685 /**
686 * Sets the order payment type.
687 *
688 * @param OrderPaymentType $paymentType Payment type.
689 *
690 * @return GXEngineOrder Same instance for method chaining.
691 */
692 public function setPaymentType(OrderPaymentType $paymentType)
693 {
694 $this->paymentType = $paymentType;
695
696 return $this;
697 }
698
699
700 /**
701 * Returns the order payment type.
702 *
703 * @return OrderPaymentType Order payment type.
704 */
705 public function getPaymentType()
706 {
707 return $this->paymentType;
708 }
709
710
711 /**
712 * Sets the order currency code.
713 *
714 * @param CurrencyCode $currencyCode Currency code.
715 *
716 * @return GXEngineOrder Same instance for method chaining.
717 */
718 public function setCurrencyCode(CurrencyCode $currencyCode)
719 {
720 $this->currencyCode = $currencyCode;
721
722 return $this;
723 }
724
725
726 /**
727 * Returns the order currency code.
728 *
729 * @return CurrencyCode Order currency code.
730 */
731 public function getCurrencyCode()
732 {
733 return $this->currencyCode;
734 }
735
736
737 /**
738 * Sets the order language code.
739 *
740 * @param LanguageCode $languageCode Language code.
741 *
742 * @return GXEngineOrder Same instance for method chaining.
743 */
744 public function setLanguageCode(LanguageCode $languageCode)
745 {
746 $this->languageCode = $languageCode;
747
748 return $this;
749 }
750
751
752 /**
753 * Returns the order language code.
754 *
755 * @return LanguageCode Order language code.
756 */
757 public function getLanguageCode()
758 {
759 return $this->languageCode;
760 }
761
762
763 /**
764 * Sets the order purchase date time.
765 *
766 * @param DateTime $purchaseDateTime Purchase date time.
767 *
768 * @return GXEngineOrder Same instance for method chaining.
769 */
770 public function setPurchaseDateTime(DateTime $purchaseDateTime)
771 {
772 $this->purchaseDateTime = $purchaseDateTime;
773
774 return $this;
775 }
776
777
778 /**
779 * Returns the order purchase datetime.
780 *
781 * @return DateTime Order purchase datetime.
782 */
783 public function getPurchaseDateTime()
784 {
785 return $this->purchaseDateTime;
786 }
787
788
789 /**
790 * Sets the date time of last modification.
791 *
792 * @param DateTime $lastModifiedDateTime Last modification date time
793 *
794 * @return GXEngineOrder Same instance for method chaining.
795 */
796 public function setLastModifiedDateTime(DateTime $lastModifiedDateTime)
797 {
798 $this->lastModifiedDateTime = $lastModifiedDateTime;
799
800 return $this;
801 }
802
803
804 /**
805 * Returns the datetime of last modification.
806 *
807 * @return DateTime Datetime of last modification.
808 */
809 public function getLastModifiedDateTime()
810 {
811 return $this->lastModifiedDateTime;
812 }
813
814
815 /**
816 * Sets order status history storage object.
817 *
818 * @param OrderStatusHistoryReaderInterface $orderStatusHistoryReader Order status history storage object.
819 *
820 * @return GXEngineOrder Same instance for method chaining.
821 */
822 public function setOrderStatusHistoryReader(OrderStatusHistoryReaderInterface $orderStatusHistoryReader)
823 {
824 $this->orderStatusHistoryReader = $orderStatusHistoryReader;
825
826 return $this;
827 }
828
829
830 /**
831 * Returns the order status history.
832 *
833 * @return OrderStatusHistoryListItemCollection Order status history.
834 */
835 public function getStatusHistory()
836 {
837 if(isset($this->orderStatusHistoryReader)
838 && $this->orderStatusHistoryReader instanceof OrderStatusHistoryReaderInterface
839 )
840 {
841 return $this->orderStatusHistoryReader->getStatusHistory(new IdType($this->orderId));
842 }
843
844 return MainFactory::create('OrderStatusHistoryListItemCollection', array());
845 }
846
847
848 /**
849 * Sets the order comment.
850 *
851 * @param StringType $comment Comment.
852 *
853 * @return GXEngineOrder Same instance for method chaining.
854 */
855 public function setComment(StringType $comment)
856 {
857 $this->comment = $comment->asString();
858
859 return $this;
860 }
861
862
863 /**
864 * Returns the order comment.
865 *
866 * @return string Order comment.
867 */
868 public function getComment()
869 {
870 return $this->comment;
871 }
872
873
874 /**
875 * Returns the addon value container ID.
876 * The addon value container id is equal to the orders id.
877 *
878 * @return int Addon value container ID
879 */
880 public function getAddonValueContainerId()
881 {
882 return $this->getOrderId();
883 }
884
885
886 /**
887 * Returns the order addon value collection.
888 *
889 * @return EditableKeyValueCollection Order addon value collection.
890 */
891 public function getAddonValue(StringType $key)
892 {
893 return $this->addonValues->getValue($key->asString());
894 }
895
896
897 /**
898 * Returns the order addon value collection.
899 *
900 * @return EditableKeyValueCollection Order addon value collection.
901 */
902 public function getAddonValues()
903 {
904 return $this->addonValues->getClone();
905 }
906
907
908 /**
909 * Adds/updates a key value in the addon value collection.
910 *
911 * @param StringType $key Addon key.
912 * @param StringType $value Addon value.
913 *
914 * @return GXEngineOrder Same instance for method chaining.
915 */
916 public function setAddonValue(StringType $key, StringType $value)
917 {
918 $this->addonValues->setValue($key->asString(), $value->asString());
919
920 return $this;
921 }
922
923
924 /**
925 * Adds an addon collection to the existing one.
926 *
927 * @param KeyValueCollection $addonCollection Addon collection.
928 *
929 * @return GXEngineOrder Same instance for method chaining.
930 */
931 public function addAddonValues(KeyValueCollection $addonCollection)
932 {
933 $this->addonValues->addCollection($addonCollection);
934
935 return $this;
936 }
937
938
939 /**
940 * Removes an addon value from the addon value container by the given key.
941 *
942 * @param StringType $key Addon key.
943 *
944 * @return GXEngineOrder Same instance for method chaining.
945 */
946 public function deleteAddonValue(StringType $key)
947 {
948 $this->addonValues->deleteValue($key->asString());
949
950 return $this;
951 }
952 }