OrderRecipientInterfaceBuilder.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Order;
  6. use Magento\Framework\Api\AbstractSimpleObjectBuilder;
  7. use Magento\Framework\Api\ObjectFactory;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Quote\Model\Quote\Address\RateRequest;
  10. use Magento\Sales\Api\Data\OrderInterface;
  11. use Temando\Shipping\Model\Checkout\RateRequest\Extractor;
  12. /**
  13. * Temando Order Recipient Builder
  14. *
  15. * Create a recipient entity to be shared between shipping module and Temando platform.
  16. *
  17. * @package Temando\Shipping\Model
  18. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link http://www.temando.com/
  21. */
  22. class OrderRecipientInterfaceBuilder extends AbstractSimpleObjectBuilder
  23. {
  24. /**
  25. * @var Extractor
  26. */
  27. private $rateRequestExtractor;
  28. /**
  29. * @param ObjectFactory $objectFactory
  30. * @param Extractor $rateRequestExtractor
  31. */
  32. public function __construct(
  33. ObjectFactory $objectFactory,
  34. Extractor $rateRequestExtractor
  35. ) {
  36. $this->rateRequestExtractor = $rateRequestExtractor;
  37. parent::__construct($objectFactory);
  38. }
  39. /**
  40. * @param RateRequest $rateRequest
  41. * @return void
  42. */
  43. public function setRateRequest(RateRequest $rateRequest)
  44. {
  45. try {
  46. $shippingAddress = $this->rateRequestExtractor->getShippingAddress($rateRequest);
  47. $this->_set(OrderRecipientInterface::COMPANY, $shippingAddress->getCompany());
  48. $this->_set(OrderRecipientInterface::LASTNAME, $shippingAddress->getLastname());
  49. $this->_set(OrderRecipientInterface::FIRSTNAME, $shippingAddress->getFirstname());
  50. $this->_set(OrderRecipientInterface::EMAIL, $shippingAddress->getEmail());
  51. $this->_set(OrderRecipientInterface::PHONE, $shippingAddress->getTelephone());
  52. $this->_set(OrderRecipientInterface::FAX, $shippingAddress->getFax());
  53. } catch (LocalizedException $e) {
  54. // detailed address data unavailable
  55. $this->_set(OrderRecipientInterface::COMPANY, '');
  56. $this->_set(OrderRecipientInterface::LASTNAME, '');
  57. $this->_set(OrderRecipientInterface::FIRSTNAME, '');
  58. $this->_set(OrderRecipientInterface::EMAIL, '');
  59. $this->_set(OrderRecipientInterface::PHONE, '');
  60. $this->_set(OrderRecipientInterface::FAX, '');
  61. }
  62. if (is_array($rateRequest->getDestStreet())) {
  63. $destStreet = $rateRequest->getDestStreet();
  64. } else {
  65. $destStreet = explode("\n", $rateRequest->getDestStreet());
  66. }
  67. $this->_set(OrderRecipientInterface::NATIONAL_ID, '');
  68. $this->_set(OrderRecipientInterface::TAX_ID, '');
  69. $this->_set(OrderRecipientInterface::STREET, $destStreet);
  70. $this->_set(OrderRecipientInterface::COUNTRY_CODE, $rateRequest->getDestCountryId());
  71. $this->_set(OrderRecipientInterface::REGION, $rateRequest->getDestRegionCode());
  72. $this->_set(OrderRecipientInterface::POSTAL_CODE, $rateRequest->getDestPostcode());
  73. $this->_set(OrderRecipientInterface::CITY, $rateRequest->getDestCity());
  74. $this->_set(OrderRecipientInterface::SUBURB, '');
  75. $this->_set(OrderRecipientInterface::LONGITUDE, null);
  76. $this->_set(OrderRecipientInterface::LATITUDE, null);
  77. }
  78. /**
  79. * @param \Magento\Sales\Api\Data\OrderInterface|\Magento\Sales\Model\Order $order
  80. * @return void
  81. */
  82. public function setOrder(OrderInterface $order)
  83. {
  84. $shippingAddress = $order->getShippingAddress();
  85. $this->_set(OrderRecipientInterface::COMPANY, $shippingAddress->getCompany());
  86. $this->_set(OrderRecipientInterface::LASTNAME, $shippingAddress->getLastname());
  87. $this->_set(OrderRecipientInterface::FIRSTNAME, $shippingAddress->getFirstname());
  88. $this->_set(OrderRecipientInterface::EMAIL, $shippingAddress->getEmail());
  89. $this->_set(OrderRecipientInterface::PHONE, $shippingAddress->getTelephone());
  90. $this->_set(OrderRecipientInterface::FAX, $shippingAddress->getFax());
  91. $this->_set(OrderRecipientInterface::NATIONAL_ID, '');
  92. $this->_set(OrderRecipientInterface::TAX_ID, '');
  93. $this->_set(OrderRecipientInterface::STREET, $shippingAddress->getStreet());
  94. $this->_set(OrderRecipientInterface::COUNTRY_CODE, $shippingAddress->getCountryId());
  95. $this->_set(OrderRecipientInterface::REGION, $shippingAddress->getRegionCode());
  96. $this->_set(OrderRecipientInterface::POSTAL_CODE, $shippingAddress->getPostcode());
  97. $this->_set(OrderRecipientInterface::CITY, $shippingAddress->getCity());
  98. $this->_set(OrderRecipientInterface::SUBURB, '');
  99. $this->_set(OrderRecipientInterface::LONGITUDE, '');
  100. $this->_set(OrderRecipientInterface::LATITUDE, '');
  101. }
  102. }