PluginProbe
Packeta / 1.6.2
Packeta v1.6.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Core / Entity / Order.php

Order.php in Packeta 1.6.2, at src/Packetery/Core/Entity/Order.php

1,039 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Order
4 *
5 * @package Packetery\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Core\Entity;
11
12 use Packetery\Core\Helper;
13 use DateTimeImmutable;
14
15 /**
16 * Class Order
17 *
18 * @package Packetery\Order
19 */
20 class Order {
21
22 /**
23 * Order id.
24 *
25 * @var string
26 */
27 private $number;
28
29 /**
30 * Custom order number.
31 *
32 * @var string|null
33 */
34 private $customNumber;
35
36 /**
37 * Order carrier object.
38 *
39 * @var Carrier
40 */
41 private $carrier;
42
43 /**
44 * Order pickup point object.
45 *
46 * @var PickupPoint|null
47 */
48 private $pickupPoint;
49
50 /**
51 * Customer name.
52 *
53 * @var string|null
54 */
55 private $name;
56
57 /**
58 * Customer surname.
59 *
60 * @var string|null
61 */
62 private $surname;
63
64 /**
65 * Customer e-mail.
66 *
67 * @var string|null
68 */
69 private $email;
70
71 /**
72 * Customer phone.
73 *
74 * @var string|null
75 */
76 private $phone;
77
78 /**
79 * Order value.
80 *
81 * @var float|null
82 */
83 private $value;
84
85 /**
86 * Sender label.
87 *
88 * @var string|null
89 */
90 private $eshop;
91
92 /**
93 * Address.
94 *
95 * @var Address|null
96 */
97 private $address;
98
99 /**
100 * Size.
101 *
102 * @var Size|null
103 */
104 private $size;
105
106 /**
107 * Package weight, set or calculated.
108 *
109 * @var float|null
110 */
111 private $weight;
112
113 /**
114 * Calculated package weight.
115 *
116 * @var float|null
117 */
118 private $calculatedWeight;
119
120 /**
121 * Cash on delivery value.
122 *
123 * @var float|null
124 */
125 private $cod;
126
127 /**
128 * Packet note.
129 *
130 * @var string|null
131 */
132 private $note;
133
134 /**
135 * Adult content presence flag.
136 *
137 * @var bool|null
138 */
139 private $adultContent;
140
141 /**
142 * Packet ID
143 *
144 * @var string|null
145 */
146 private $packetId;
147
148 /**
149 * Packet ID.
150 *
151 * @var string|null
152 */
153 private $packetClaimId;
154
155 /**
156 * Packet password.
157 *
158 * @var string|null
159 */
160 private $packetClaimPassword;
161
162 /**
163 * Packet ID
164 *
165 * @var string|null
166 */
167 private $packetStatus;
168
169 /**
170 * Tells if is packet submitted.
171 *
172 * @var bool
173 */
174 private $isExported;
175
176 /**
177 * Tells if is packet submitted.
178 *
179 * @var bool
180 */
181 private $isLabelPrinted;
182
183 /**
184 * Packet currency.
185 *
186 * @var string
187 */
188 private $currency;
189
190 /**
191 * Carrier number.
192 *
193 * @var string|null
194 */
195 private $carrierNumber;
196
197 /**
198 * Address validated.
199 *
200 * @var bool
201 */
202 private $addressValidated;
203
204 /**
205 * ISO 3166-1 alpha-2 code, lowercase.
206 *
207 * @var string|null
208 */
209 private $shippingCountry;
210
211 /**
212 * Last message built from Packeta API response.
213 *
214 * @var string|null
215 */
216 private $lastApiErrorMessage;
217
218 /**
219 * Last API error datetime.
220 *
221 * @var \DateTimeImmutable|null
222 */
223 private $lastApiErrorDateTime;
224
225 /**
226 * Deliver on
227 *
228 * @var DateTimeImmutable|null
229 */
230 private $deliverOn;
231
232 /**
233 * Customs declaration.
234 *
235 * @var CustomsDeclaration|null
236 */
237 private $customsDeclaration;
238
239 /**
240 * Order entity constructor.
241 *
242 * @param string $number Order id.
243 * @param Carrier $carrier Carrier entity.
244 */
245 public function __construct(
246 string $number,
247 Carrier $carrier
248 ) {
249 $this->number = $number;
250 $this->carrier = $carrier;
251 $this->isExported = false;
252 $this->isLabelPrinted = false;
253 $this->addressValidated = false;
254 }
255
256 /**
257 * Sets custom declaration.
258 *
259 * @param CustomsDeclaration|null $customsDeclaration Customs declaration.
260 *
261 * @return void
262 */
263 public function setCustomsDeclaration( ?CustomsDeclaration $customsDeclaration ): void {
264 $this->customsDeclaration = $customsDeclaration;
265 }
266
267 /**
268 * Gets customs declaration.
269 *
270 * @return CustomsDeclaration|null
271 */
272 public function getCustomsDeclaration(): ?CustomsDeclaration {
273 return $this->customsDeclaration;
274 }
275
276 /**
277 * Has customs declaration items.
278 *
279 * @return bool
280 */
281 public function hasCustomsDeclaration(): bool {
282 return null !== $this->customsDeclaration && [] !== $this->customsDeclaration->getItems();
283 }
284
285 /**
286 * Tells if customs declaration has to be filled to submit packet.
287 *
288 * @return bool
289 */
290 public function hasToFillCustomsDeclaration(): bool {
291 return $this->carrier->requiresCustomsDeclarations() &&
292 false === $this->hasCustomsDeclaration();
293 }
294
295 /**
296 * Gets custom number.
297 *
298 * @return string|null
299 */
300 public function getCustomNumber(): ?string {
301 return $this->customNumber;
302 }
303
304 /**
305 * Sets custom number.
306 *
307 * @param string|null $customNumber Custom number.
308 *
309 * @return void
310 */
311 public function setCustomNumber( ?string $customNumber ): void {
312 $this->customNumber = $customNumber;
313 }
314
315 /**
316 * Is address validated?
317 *
318 * @return bool
319 */
320 public function isAddressValidated(): bool {
321 return $this->addressValidated;
322 }
323
324 /**
325 * Sets address validation flag.
326 *
327 * @param bool $addressValidated Address validated.
328 *
329 * @return void
330 */
331 public function setAddressValidated( bool $addressValidated ): void {
332 $this->addressValidated = $addressValidated;
333 }
334
335 /**
336 * Checks if is home delivery. In that case pointId is not set.
337 *
338 * @return bool
339 */
340 public function isHomeDelivery(): bool {
341 return ( null === $this->pickupPoint );
342 }
343
344 /**
345 * Tells if order has to be shipped to pickup point, run by Packeta or an external carrier.
346 *
347 * @return bool
348 */
349 public function isPickupPointDelivery(): bool {
350 return ( null !== $this->pickupPoint );
351 }
352
353 /**
354 * Checks if order uses external carrier.
355 *
356 * @return bool
357 */
358 public function isExternalCarrier(): bool {
359 return is_numeric( $this->getCarrier()->getId() );
360 }
361
362 /**
363 * Check if delivery method is internal packetery pickup point
364 *
365 * @return bool
366 */
367 public function isPacketaInternalPickupPoint(): bool {
368 return ! $this->isExternalCarrier() && $this->isPickupPointDelivery();
369 }
370
371 /**
372 * Gets pickup point/carrier id.
373 *
374 * @return int|null
375 */
376 public function getPickupPointOrCarrierId(): ?int {
377 if ( $this->isExternalCarrier() ) {
378 return (int) $this->getCarrier()->getId();
379 }
380
381 if ( null === $this->pickupPoint ) {
382 return null;
383 }
384
385 // Typing to int is safe in case of internal pickup points.
386 return (int) $this->pickupPoint->getId();
387 }
388
389 /**
390 * Sets pickup point.
391 *
392 * @param PickupPoint $pickupPoint Pickup point.
393 */
394 public function setPickupPoint( PickupPoint $pickupPoint ): void {
395 $this->pickupPoint = $pickupPoint;
396 }
397
398 /**
399 * Sets delivery address.
400 *
401 * @param Address $address Delivery address.
402 */
403 public function setDeliveryAddress( Address $address ): void {
404 $this->address = $address;
405 }
406
407 /**
408 * Sets packet size.
409 *
410 * @param Size $size Size.
411 */
412 public function setSize( Size $size ): void {
413 $this->size = $size;
414 }
415
416 /**
417 * Sets number.
418 *
419 * @param string $number Number.
420 */
421 public function setNumber( string $number ): void {
422 $this->number = $number;
423 }
424
425 /**
426 * Sets phone.
427 *
428 * @param string $name Name.
429 */
430 public function setName( string $name ): void {
431 $this->name = $name;
432 }
433
434 /**
435 * Sets surname.
436 *
437 * @param string $surname Surname.
438 */
439 public function setSurname( string $surname ): void {
440 $this->surname = $surname;
441 }
442
443 /**
444 * Sets e-mail.
445 *
446 * @param string $email E-mail.
447 */
448 public function setEmail( string $email ): void {
449 $this->email = $email;
450 }
451
452 /**
453 * Sets eshop.
454 *
455 * @param string|null $eshop Eshop.
456 *
457 * @return void
458 */
459 public function setEshop( ?string $eshop ): void {
460 $this->eshop = $eshop;
461 }
462
463 /**
464 * Sets phone.
465 *
466 * @param string $phone Phone.
467 */
468 public function setPhone( string $phone ): void {
469 $this->phone = $phone;
470 }
471
472 /**
473 * Sets value.
474 *
475 * @param float|null $value Value.
476 */
477 public function setValue( ?float $value ): void {
478 $this->value = $value;
479 }
480
481 /**
482 * Sets COD.
483 *
484 * @param float|null $cod COD.
485 */
486 public function setCod( ?float $cod ): void {
487 $this->cod = $cod;
488 }
489
490 /**
491 * Gets packet currency.
492 *
493 * @return string
494 */
495 public function getCurrency(): string {
496 return $this->currency;
497 }
498
499 /**
500 * Sets packet currency.
501 *
502 * @param string $currency Currency.
503 *
504 * @return void
505 */
506 public function setCurrency( string $currency ): void {
507 $this->currency = $currency;
508 }
509
510 /**
511 * Sets packet note.
512 *
513 * @param string $note Packet note.
514 */
515 public function setNote( string $note ): void {
516 $this->note = $note;
517 }
518
519 /**
520 * Sets adult content presence flag.
521 *
522 * @param bool|null $adultContent Adult content presence flag.
523 */
524 public function setAdultContent( ?bool $adultContent ): void {
525 $this->adultContent = $adultContent;
526 }
527
528 /**
529 * Sets packet id.
530 *
531 * @param string|null $packetId Packet id.
532 */
533 public function setPacketId( ?string $packetId ): void {
534 $this->packetId = $packetId;
535 }
536
537 /**
538 * Sets packet claim ID.
539 *
540 * @param string|null $packetClaimId Packet claim ID.
541 *
542 * @return void
543 */
544 public function setPacketClaimId( ?string $packetClaimId ): void {
545 $this->packetClaimId = $packetClaimId;
546 }
547
548 /**
549 * Packet claim password.
550 *
551 * @param string|null $packetClaimPassword Packet claim password.
552 *
553 * @return void
554 */
555 public function setPacketClaimPassword( ?string $packetClaimPassword ): void {
556 $this->packetClaimPassword = $packetClaimPassword;
557 }
558
559 /**
560 * Sets packet status.
561 *
562 * @param string|null $packetStatus Packet status.
563 *
564 * @return void
565 */
566 public function setPacketStatus( ?string $packetStatus ): void {
567 $this->packetStatus = $packetStatus;
568 }
569
570 /**
571 * Sets is exported flag.
572 *
573 * @param bool $isExported Packet id.
574 */
575 public function setIsExported( bool $isExported ): void {
576 $this->isExported = $isExported;
577 }
578
579 /**
580 * Sets flag of label print.
581 *
582 * @param bool $isLabelPrinted Is label printed.
583 *
584 * @return void
585 */
586 public function setIsLabelPrinted( bool $isLabelPrinted ): void {
587 $this->isLabelPrinted = $isLabelPrinted;
588 }
589
590 /**
591 * Sets carrier number.
592 *
593 * @param string|null $carrierNumber Carrier number.
594 *
595 * @return void
596 */
597 public function setCarrierNumber( ?string $carrierNumber ): void {
598 $this->carrierNumber = $carrierNumber;
599 }
600
601 /**
602 * Sets weight.
603 *
604 * @param float|null $weight Weight.
605 *
606 * @return void
607 */
608 public function setWeight( ?float $weight ): void {
609 $this->weight = Helper::simplifyWeight( $weight );
610 }
611
612 /**
613 * Sets calculated weight.
614 *
615 * @param float|null $weight Weight.
616 *
617 * @return void
618 */
619 public function setCalculatedWeight( ?float $weight ): void {
620 $this->calculatedWeight = Helper::simplifyWeight( $weight );
621 }
622
623 /**
624 * Sets shipping country.
625 *
626 * @param string $shippingCountry ISO 3166-1 alpha-2 code, lowercase.
627 *
628 * @return void
629 */
630 public function setShippingCountry( string $shippingCountry ): void {
631 if ( '' === $shippingCountry ) {
632 $this->shippingCountry = null;
633 } else {
634 $this->shippingCountry = $shippingCountry;
635 }
636 }
637
638 /**
639 * Sets API response message.
640 *
641 * @param string|null $lastApiErrorMessage API response message.
642 *
643 * @return void
644 */
645 public function setLastApiErrorMessage( ?string $lastApiErrorMessage ): void {
646 $this->lastApiErrorMessage = $lastApiErrorMessage;
647 }
648
649 /**
650 * Sets API error date.
651 *
652 * @param \DateTimeImmutable|null $lastApiErrorDateTime API error date.
653 */
654 public function setLastApiErrorDateTime( ?\DateTimeImmutable $lastApiErrorDateTime ): void {
655 $this->lastApiErrorDateTime = $lastApiErrorDateTime;
656 }
657
658 /**
659 * Gets carrier object.
660 *
661 * @return Carrier
662 */
663 public function getCarrier(): Carrier {
664 return $this->carrier;
665 }
666
667 /**
668 * Gets pickup point object.
669 *
670 * @return PickupPoint|null
671 */
672 public function getPickupPoint(): ?PickupPoint {
673 return $this->pickupPoint;
674 }
675
676 /**
677 * Gets delivery address object.
678 *
679 * @return Address|null
680 */
681 public function getDeliveryAddress(): ?Address {
682 return $this->address;
683 }
684
685 /**
686 * Gets validated delivery address object.
687 *
688 * @return Address|null
689 */
690 public function getValidatedDeliveryAddress(): ?Address {
691 return ( $this->addressValidated ? $this->address : null );
692 }
693
694 /**
695 * Gets size object.
696 *
697 * @return Size|null
698 */
699 public function getSize(): ?Size {
700 return $this->size;
701 }
702
703 /**
704 * Packet ID
705 *
706 * @return string|null
707 */
708 public function getPacketId(): ?string {
709 return $this->packetId;
710 }
711
712 /**
713 * Packet barcode e.g Z123456789
714 *
715 * @return string|null
716 */
717 public function getPacketBarcode(): ?string {
718 return $this->packetId ? 'Z' . $this->packetId : null;
719 }
720
721 /**
722 * Get packet tracking url
723 *
724 * @return string|null
725 */
726 public function getPacketTrackingUrl(): ?string {
727 return $this->packetId ? sprintf( Helper::TRACKING_URL, $this->packetId ) : null;
728 }
729
730 /**
731 * Gets packet claim ID.
732 *
733 * @return string|null
734 */
735 public function getPacketClaimId(): ?string {
736 return $this->packetClaimId;
737 }
738
739 /**
740 * Gets packet claim password.
741 *
742 * @return string|null
743 */
744 public function getPacketClaimPassword(): ?string {
745 return $this->packetClaimPassword;
746 }
747
748 /**
749 * Packet status.
750 *
751 * @return string|null
752 */
753 public function getPacketStatus(): ?string {
754 return $this->packetStatus;
755 }
756
757 /**
758 * Tells if packet claim creation is possible.
759 *
760 * @return bool
761 */
762 public function isPacketClaimCreationPossible(): bool {
763 return PacketStatus::DELIVERED === $this->packetStatus &&
764 null === $this->packetClaimId;
765 }
766
767 /**
768 * Tells if packet claim label print is possible.
769 *
770 * @return bool
771 */
772 public function isPacketClaimLabelPrintPossible(): bool {
773 return null !== $this->packetClaimId &&
774 false === $this->isExternalCarrier() &&
775 null !== $this->pickupPoint;
776 }
777
778 /**
779 * Tells if is packet submitted.
780 *
781 * @return bool
782 */
783 public function isExported(): bool {
784 return $this->isExported;
785 }
786
787 /**
788 * Gets weight.
789 *
790 * @return float|null
791 */
792 public function getWeight(): ?float {
793 return $this->weight;
794 }
795
796 /**
797 * Tells if order has manual weight set.
798 *
799 * @return bool
800 */
801 public function hasManualWeight(): bool {
802 return null !== $this->weight;
803 }
804
805 /**
806 * Gets final weight.
807 *
808 * @return float|null
809 */
810 public function getFinalWeight(): ?float {
811 return $this->weight ?? $this->calculatedWeight;
812 }
813
814 /**
815 * Gets calculated weight.
816 *
817 * @return float|null
818 */
819 public function getCalculatedWeight(): ?float {
820 return $this->calculatedWeight;
821 }
822
823 /**
824 * Gets length.
825 *
826 * @return float|null
827 */
828 public function getLength(): ?float {
829 if ( empty( $this->size ) ) {
830 return null;
831 }
832
833 return $this->size->getLength();
834 }
835
836 /**
837 * Gets width.
838 *
839 * @return float|null
840 */
841 public function getWidth(): ?float {
842 if ( empty( $this->size ) ) {
843 return null;
844 }
845
846 return $this->size->getWidth();
847 }
848
849 /**
850 * Gets height.
851 *
852 * @return float|null
853 */
854 public function getHeight(): ?float {
855 if ( empty( $this->size ) ) {
856 return null;
857 }
858
859 return $this->size->getHeight();
860 }
861
862 /**
863 * Checks adult content presence.
864 *
865 * @return bool|null
866 */
867 public function containsAdultContent(): ?bool {
868 return $this->adultContent;
869 }
870
871 /**
872 * Gets order id.
873 *
874 * @return string|null
875 */
876 public function getNumber(): ?string {
877 return $this->number;
878 }
879
880 /**
881 * Gets customer name.
882 *
883 * @return string|null
884 */
885 public function getName(): ?string {
886 return $this->name;
887 }
888
889 /**
890 * Gets customer surname.
891 *
892 * @return string|null
893 */
894 public function getSurname(): ?string {
895 return $this->surname;
896 }
897
898 /**
899 * Gets order value.
900 *
901 * @return float|null
902 */
903 public function getValue(): ?float {
904 return $this->value;
905 }
906
907 /**
908 * Gets order COD value.
909 *
910 * @return float|null
911 */
912 public function getCod(): ?float {
913 return $this->cod;
914 }
915
916 /**
917 * Gets sender label.
918 *
919 * @return string|null
920 */
921 public function getEshop(): ?string {
922 return $this->eshop;
923 }
924
925 /**
926 * Gets customer e-mail.
927 *
928 * @return string|null
929 */
930 public function getEmail(): ?string {
931 return $this->email;
932 }
933
934 /**
935 * Gets delivery note.
936 *
937 * @return string|null
938 */
939 public function getNote(): ?string {
940 return $this->note;
941 }
942
943 /**
944 * Gets customer phone.
945 *
946 * @return string|null
947 */
948 public function getPhone(): ?string {
949 return $this->phone;
950 }
951
952 /**
953 * Is label printed?
954 *
955 * @return bool
956 */
957 public function isLabelPrinted(): bool {
958 return $this->isLabelPrinted;
959 }
960
961 /**
962 * Carrier number.
963 *
964 * @return string|null
965 */
966 public function getCarrierNumber(): ?string {
967 return $this->carrierNumber;
968 }
969
970 /**
971 * Gets shipping country.
972 *
973 * @return string|null
974 */
975 public function getShippingCountry(): ?string {
976 return $this->shippingCountry;
977 }
978
979 /**
980 * Tells if order has COD.
981 *
982 * @return bool
983 */
984 public function hasCod(): bool {
985 return ( null !== $this->getCod() );
986 }
987
988 /**
989 * Returns deliver on date
990 *
991 * @return DateTimeImmutable|null
992 */
993 public function getDeliverOn(): ?DateTimeImmutable {
994 return $this->deliverOn;
995 }
996
997 /**
998 * Sets deliver on date
999 *
1000 * @param DateTimeImmutable|null $deliverOn Deliver on.
1001 *
1002 * @return void
1003 */
1004 public function setDeliverOn( ?DateTimeImmutable $deliverOn ): void {
1005 $this->deliverOn = $deliverOn;
1006 }
1007
1008 /**
1009 * Formatted API error message.
1010 *
1011 * @return string|null
1012 */
1013 public function getLastApiErrorMessage(): ?string {
1014 return $this->lastApiErrorMessage;
1015 }
1016
1017 /**
1018 * Gets last API error message date.
1019 *
1020 * @return \DateTimeImmutable|null
1021 */
1022 public function getLastApiErrorDateTime(): ?\DateTimeImmutable {
1023 return $this->lastApiErrorDateTime;
1024 }
1025
1026 /**
1027 * Updates API error message and sets error message date accordingly.
1028 *
1029 * @param string|null $errorMessage Error message.
1030 *
1031 * @return void
1032 */
1033 public function updateApiErrorMessage( ?string $errorMessage ): void {
1034 $this->setLastApiErrorMessage( $errorMessage );
1035 $this->setLastApiErrorDateTime( $errorMessage ? Helper::now() : null );
1036 }
1037
1038 }
1039