OrderBillingInterfaceBuilder.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Billing Builder
  14. *
  15. * Create a billing address 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 OrderBillingInterfaceBuilder 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. $billingAddress = $this->rateRequestExtractor->getBillingAddress($rateRequest);
  47. $this->_set(OrderRecipientInterface::COMPANY, $billingAddress->getCompany());
  48. $this->_set(OrderRecipientInterface::LASTNAME, $billingAddress->getLastname());
  49. $this->_set(OrderRecipientInterface::FIRSTNAME, $billingAddress->getFirstname());
  50. $this->_set(OrderRecipientInterface::EMAIL, $billingAddress->getEmail());
  51. $this->_set(OrderRecipientInterface::PHONE, $billingAddress->getTelephone());
  52. $this->_set(OrderRecipientInterface::FAX, $billingAddress->getFax());
  53. if (is_array($billingAddress->getStreet())) {
  54. $billingStreet = $billingAddress->getStreet();
  55. } else {
  56. $billingStreet = explode("\n", $billingAddress->getStreet());
  57. }
  58. $this->_set(OrderRecipientInterface::NATIONAL_ID, '');
  59. $this->_set(OrderRecipientInterface::TAX_ID, $billingAddress->getVatId());
  60. $this->_set(OrderRecipientInterface::STREET, $billingStreet);
  61. $this->_set(OrderRecipientInterface::COUNTRY_CODE, $billingAddress->getCountryId());
  62. $this->_set(OrderRecipientInterface::REGION, $billingAddress->getRegion());
  63. $this->_set(OrderRecipientInterface::POSTAL_CODE, $billingAddress->getPostcode());
  64. $this->_set(OrderRecipientInterface::CITY, $billingAddress->getCity());
  65. $this->_set(OrderRecipientInterface::SUBURB, '');
  66. $this->_set(OrderRecipientInterface::LONGITUDE, null);
  67. $this->_set(OrderRecipientInterface::LATITUDE, null);
  68. } catch (LocalizedException $e) {
  69. // detailed address data unavailable
  70. $this->_set(OrderRecipientInterface::COMPANY, '');
  71. $this->_set(OrderRecipientInterface::LASTNAME, '');
  72. $this->_set(OrderRecipientInterface::FIRSTNAME, '');
  73. $this->_set(OrderRecipientInterface::EMAIL, '');
  74. $this->_set(OrderRecipientInterface::PHONE, '');
  75. $this->_set(OrderRecipientInterface::FAX, '');
  76. $this->_set(OrderRecipientInterface::NATIONAL_ID, '');
  77. $this->_set(OrderRecipientInterface::TAX_ID, '');
  78. $this->_set(OrderRecipientInterface::STREET, []);
  79. $this->_set(OrderRecipientInterface::COUNTRY_CODE, '');
  80. $this->_set(OrderRecipientInterface::REGION, '');
  81. $this->_set(OrderRecipientInterface::POSTAL_CODE, '');
  82. $this->_set(OrderRecipientInterface::CITY, '');
  83. $this->_set(OrderRecipientInterface::SUBURB, '');
  84. $this->_set(OrderRecipientInterface::LONGITUDE, null);
  85. $this->_set(OrderRecipientInterface::LATITUDE, null);
  86. }
  87. }
  88. /**
  89. * @param \Magento\Sales\Api\Data\OrderInterface|\Magento\Sales\Model\Order $order
  90. * @return void
  91. */
  92. public function setOrder(OrderInterface $order)
  93. {
  94. $billingAddress = $order->getBillingAddress();
  95. $this->_set(OrderRecipientInterface::COMPANY, $billingAddress->getCompany());
  96. $this->_set(OrderRecipientInterface::LASTNAME, $billingAddress->getLastname());
  97. $this->_set(OrderRecipientInterface::FIRSTNAME, $billingAddress->getFirstname());
  98. $this->_set(OrderRecipientInterface::EMAIL, $billingAddress->getEmail());
  99. $this->_set(OrderRecipientInterface::PHONE, $billingAddress->getTelephone());
  100. $this->_set(OrderRecipientInterface::FAX, $billingAddress->getFax());
  101. $this->_set(OrderRecipientInterface::NATIONAL_ID, '');
  102. $this->_set(OrderRecipientInterface::TAX_ID, $billingAddress->getVatId());
  103. $this->_set(OrderRecipientInterface::STREET, $billingAddress->getStreet());
  104. $this->_set(OrderRecipientInterface::COUNTRY_CODE, $billingAddress->getCountryId());
  105. $this->_set(OrderRecipientInterface::REGION, $billingAddress->getRegionCode());
  106. $this->_set(OrderRecipientInterface::POSTAL_CODE, $billingAddress->getPostcode());
  107. $this->_set(OrderRecipientInterface::CITY, $billingAddress->getCity());
  108. $this->_set(OrderRecipientInterface::SUBURB, '');
  109. $this->_set(OrderRecipientInterface::LONGITUDE, null);
  110. $this->_set(OrderRecipientInterface::LATITUDE, null);
  111. }
  112. }