Address.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Customer\Model\Address\AddressModelInterface;
  8. use Magento\Sales\Api\Data\OrderAddressInterface;
  9. use Magento\Sales\Model\AbstractModel;
  10. /**
  11. * Sales order address model
  12. *
  13. * @api
  14. * @method \Magento\Customer\Api\Data\AddressInterface getCustomerAddressData()
  15. * @method Address setCustomerAddressData(\Magento\Customer\Api\Data\AddressInterface $value)
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  18. * @since 100.0.2
  19. */
  20. class Address extends AbstractModel implements OrderAddressInterface, AddressModelInterface
  21. {
  22. /**
  23. * Possible customer address types
  24. */
  25. const TYPE_BILLING = 'billing';
  26. const TYPE_SHIPPING = 'shipping';
  27. /**
  28. * @var \Magento\Sales\Model\Order
  29. */
  30. protected $order;
  31. /**
  32. * @var string
  33. */
  34. protected $_eventPrefix = 'sales_order_address';
  35. /**
  36. * @var string
  37. */
  38. protected $_eventObject = 'address';
  39. /**
  40. * @var \Magento\Sales\Model\OrderFactory
  41. */
  42. protected $orderFactory;
  43. /**
  44. * @var \Magento\Directory\Model\RegionFactory
  45. */
  46. protected $regionFactory;
  47. /**
  48. * @param \Magento\Framework\Model\Context $context
  49. * @param \Magento\Framework\Registry $registry
  50. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  51. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  52. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  53. * @param \Magento\Directory\Model\RegionFactory $regionFactory
  54. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  55. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  56. * @param array $data
  57. */
  58. public function __construct(
  59. \Magento\Framework\Model\Context $context,
  60. \Magento\Framework\Registry $registry,
  61. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  62. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  63. \Magento\Sales\Model\OrderFactory $orderFactory,
  64. \Magento\Directory\Model\RegionFactory $regionFactory,
  65. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  66. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  67. array $data = []
  68. ) {
  69. $data = $this->implodeStreetField($data);
  70. $this->regionFactory = $regionFactory;
  71. $this->orderFactory = $orderFactory;
  72. parent::__construct(
  73. $context,
  74. $registry,
  75. $extensionFactory,
  76. $customAttributeFactory,
  77. $resource,
  78. $resourceCollection,
  79. $data
  80. );
  81. }
  82. /**
  83. * Initialize resource
  84. *
  85. * @return void
  86. */
  87. protected function _construct()
  88. {
  89. $this->_init(\Magento\Sales\Model\ResourceModel\Order\Address::class);
  90. }
  91. /**
  92. * Set order
  93. *
  94. * @codeCoverageIgnore
  95. *
  96. * @param \Magento\Sales\Model\Order $order
  97. * @return $this
  98. */
  99. public function setOrder(\Magento\Sales\Model\Order $order)
  100. {
  101. $this->order = $order;
  102. return $this;
  103. }
  104. /**
  105. * Return 2 letter state code if available, otherwise full region name
  106. *
  107. * @return null|string
  108. */
  109. public function getRegionCode()
  110. {
  111. $regionId = (!$this->getRegionId() && is_numeric($this->getRegion())) ?
  112. $this->getRegion() :
  113. $this->getRegionId();
  114. $model = $this->regionFactory->create()->load($regionId);
  115. if ($model->getCountryId() == $this->getCountryId()) {
  116. return $model->getCode();
  117. } elseif (is_string($this->getRegion())) {
  118. return $this->getRegion();
  119. } else {
  120. return null;
  121. }
  122. }
  123. /**
  124. * Get full customer name
  125. *
  126. * @return string
  127. */
  128. public function getName()
  129. {
  130. $name = '';
  131. if ($this->getPrefix()) {
  132. $name .= $this->getPrefix() . ' ';
  133. }
  134. $name .= $this->getFirstname();
  135. if ($this->getMiddlename()) {
  136. $name .= ' ' . $this->getMiddlename();
  137. }
  138. $name .= ' ' . $this->getLastname();
  139. if ($this->getSuffix()) {
  140. $name .= ' ' . $this->getSuffix();
  141. }
  142. return $name;
  143. }
  144. /**
  145. * Combine values of street lines into a single string
  146. *
  147. * @param string[]|string $value
  148. * @return string
  149. */
  150. protected function implodeStreetValue($value)
  151. {
  152. if (is_array($value)) {
  153. $value = trim(implode(PHP_EOL, $value));
  154. }
  155. return $value;
  156. }
  157. /**
  158. * Enforce format of the street field
  159. *
  160. * @param array|string $key
  161. * @param array|string $value
  162. *
  163. * @return \Magento\Framework\DataObject
  164. */
  165. public function setData($key, $value = null)
  166. {
  167. if (is_array($key)) {
  168. $key = $this->implodeStreetField($key);
  169. } elseif ($key == OrderAddressInterface::STREET) {
  170. $value = $this->implodeStreetValue($value);
  171. }
  172. return parent::setData($key, $value);
  173. }
  174. /**
  175. * Implode value of the street field, if it is present among other fields
  176. *
  177. * @param array $data
  178. * @return array
  179. */
  180. protected function implodeStreetField(array $data)
  181. {
  182. if (array_key_exists(OrderAddressInterface::STREET, $data)) {
  183. $data[OrderAddressInterface::STREET] = $this->implodeStreetValue($data[OrderAddressInterface::STREET]);
  184. }
  185. return $data;
  186. }
  187. /**
  188. * Create fields street1, street2, etc.
  189. *
  190. * To be used in controllers for views data
  191. *
  192. * @return $this
  193. */
  194. public function explodeStreetAddress()
  195. {
  196. $streetLines = $this->getStreet();
  197. foreach ($streetLines as $lineNumber => $lineValue) {
  198. $this->setData(OrderAddressInterface::STREET . ($lineNumber + 1), $lineValue);
  199. }
  200. return $this;
  201. }
  202. /**
  203. * Get order
  204. *
  205. * @return \Magento\Sales\Model\Order
  206. */
  207. public function getOrder()
  208. {
  209. if (!$this->order) {
  210. $this->order = $this->orderFactory->create()->load($this->getParentId());
  211. }
  212. return $this->order;
  213. }
  214. /**
  215. * Retrieve street field of an address
  216. *
  217. * @return string[]
  218. */
  219. public function getStreet()
  220. {
  221. if (is_array($this->getData(OrderAddressInterface::STREET))) {
  222. return $this->getData(OrderAddressInterface::STREET);
  223. }
  224. return explode(PHP_EOL, $this->getData(OrderAddressInterface::STREET));
  225. }
  226. /**
  227. * Get street line by number
  228. *
  229. * @param int $number
  230. * @return string
  231. */
  232. public function getStreetLine($number)
  233. {
  234. $lines = $this->getStreet();
  235. return $lines[$number - 1] ?? '';
  236. }
  237. //@codeCoverageIgnoreStart
  238. /**
  239. * Returns address_type
  240. *
  241. * @return string
  242. */
  243. public function getAddressType()
  244. {
  245. return $this->getData(OrderAddressInterface::ADDRESS_TYPE);
  246. }
  247. /**
  248. * Returns city
  249. *
  250. * @return string
  251. */
  252. public function getCity()
  253. {
  254. return $this->getData(OrderAddressInterface::CITY);
  255. }
  256. /**
  257. * Returns company
  258. *
  259. * @return string
  260. */
  261. public function getCompany()
  262. {
  263. return $this->getData(OrderAddressInterface::COMPANY);
  264. }
  265. /**
  266. * Returns country_id
  267. *
  268. * @return string
  269. */
  270. public function getCountryId()
  271. {
  272. return $this->getData(OrderAddressInterface::COUNTRY_ID);
  273. }
  274. /**
  275. * Returns customer_address_id
  276. *
  277. * @return int
  278. */
  279. public function getCustomerAddressId()
  280. {
  281. return $this->getData(OrderAddressInterface::CUSTOMER_ADDRESS_ID);
  282. }
  283. /**
  284. * Returns customer_id
  285. *
  286. * @return int
  287. */
  288. public function getCustomerId()
  289. {
  290. return $this->getData(OrderAddressInterface::CUSTOMER_ID);
  291. }
  292. /**
  293. * Returns email
  294. *
  295. * @return string
  296. */
  297. public function getEmail()
  298. {
  299. return $this->getData(OrderAddressInterface::EMAIL);
  300. }
  301. /**
  302. * Returns entity_id
  303. *
  304. * @return int
  305. */
  306. public function getEntityId()
  307. {
  308. return $this->getData(OrderAddressInterface::ENTITY_ID);
  309. }
  310. /**
  311. * Sets the ID for the order address.
  312. *
  313. * @param int $entityId
  314. * @return $this
  315. */
  316. public function setEntityId($entityId)
  317. {
  318. return $this->setData(OrderAddressInterface::ENTITY_ID, $entityId);
  319. }
  320. /**
  321. * Returns fax
  322. *
  323. * @return string
  324. */
  325. public function getFax()
  326. {
  327. return $this->getData(OrderAddressInterface::FAX);
  328. }
  329. /**
  330. * Returns firstname
  331. *
  332. * @return string
  333. */
  334. public function getFirstname()
  335. {
  336. return $this->getData(OrderAddressInterface::FIRSTNAME);
  337. }
  338. /**
  339. * Returns lastname
  340. *
  341. * @return string
  342. */
  343. public function getLastname()
  344. {
  345. return $this->getData(OrderAddressInterface::LASTNAME);
  346. }
  347. /**
  348. * Returns middlename
  349. *
  350. * @return string
  351. */
  352. public function getMiddlename()
  353. {
  354. return $this->getData(OrderAddressInterface::MIDDLENAME);
  355. }
  356. /**
  357. * Returns parent_id
  358. *
  359. * @return int
  360. */
  361. public function getParentId()
  362. {
  363. return $this->getData(OrderAddressInterface::PARENT_ID);
  364. }
  365. /**
  366. * Returns postcode
  367. *
  368. * @return string
  369. */
  370. public function getPostcode()
  371. {
  372. return $this->getData(OrderAddressInterface::POSTCODE);
  373. }
  374. /**
  375. * Returns prefix
  376. *
  377. * @return string
  378. */
  379. public function getPrefix()
  380. {
  381. return $this->getData(OrderAddressInterface::PREFIX);
  382. }
  383. /**
  384. * Returns region
  385. *
  386. * @return string
  387. */
  388. public function getRegion()
  389. {
  390. return $this->getData(OrderAddressInterface::REGION);
  391. }
  392. /**
  393. * Returns region_id
  394. *
  395. * @return int
  396. */
  397. public function getRegionId()
  398. {
  399. return $this->getData(OrderAddressInterface::REGION_ID);
  400. }
  401. /**
  402. * Returns suffix
  403. *
  404. * @return string
  405. */
  406. public function getSuffix()
  407. {
  408. return $this->getData(OrderAddressInterface::SUFFIX);
  409. }
  410. /**
  411. * Returns telephone
  412. *
  413. * @return string
  414. */
  415. public function getTelephone()
  416. {
  417. return $this->getData(OrderAddressInterface::TELEPHONE);
  418. }
  419. /**
  420. * Returns vat_id
  421. *
  422. * @return string
  423. */
  424. public function getVatId()
  425. {
  426. return $this->getData(OrderAddressInterface::VAT_ID);
  427. }
  428. /**
  429. * Returns vat_is_valid
  430. *
  431. * @return int
  432. */
  433. public function getVatIsValid()
  434. {
  435. return $this->getData(OrderAddressInterface::VAT_IS_VALID);
  436. }
  437. /**
  438. * Returns vat_request_date
  439. *
  440. * @return string
  441. */
  442. public function getVatRequestDate()
  443. {
  444. return $this->getData(OrderAddressInterface::VAT_REQUEST_DATE);
  445. }
  446. /**
  447. * Returns vat_request_id
  448. *
  449. * @return string
  450. */
  451. public function getVatRequestId()
  452. {
  453. return $this->getData(OrderAddressInterface::VAT_REQUEST_ID);
  454. }
  455. /**
  456. * Returns vat_request_success
  457. *
  458. * @return int
  459. */
  460. public function getVatRequestSuccess()
  461. {
  462. return $this->getData(OrderAddressInterface::VAT_REQUEST_SUCCESS);
  463. }
  464. /**
  465. * @inheritdoc
  466. */
  467. public function setParentId($id)
  468. {
  469. return $this->setData(OrderAddressInterface::PARENT_ID, $id);
  470. }
  471. /**
  472. * @inheritdoc
  473. */
  474. public function setCustomerAddressId($id)
  475. {
  476. return $this->setData(OrderAddressInterface::CUSTOMER_ADDRESS_ID, $id);
  477. }
  478. /**
  479. * @inheritdoc
  480. */
  481. public function setRegionId($id)
  482. {
  483. return $this->setData(OrderAddressInterface::REGION_ID, $id);
  484. }
  485. /**
  486. * @inheritdoc
  487. */
  488. public function setStreet($street)
  489. {
  490. return $this->setData(OrderAddressInterface::STREET, $street);
  491. }
  492. /**
  493. * @inheritdoc
  494. */
  495. public function setCustomerId($id)
  496. {
  497. return $this->setData(OrderAddressInterface::CUSTOMER_ID, $id);
  498. }
  499. /**
  500. * @inheritdoc
  501. */
  502. public function setFax($fax)
  503. {
  504. return $this->setData(OrderAddressInterface::FAX, $fax);
  505. }
  506. /**
  507. * @inheritdoc
  508. */
  509. public function setRegion($region)
  510. {
  511. return $this->setData(OrderAddressInterface::REGION, $region);
  512. }
  513. /**
  514. * @inheritdoc
  515. */
  516. public function setPostcode($postcode)
  517. {
  518. return $this->setData(OrderAddressInterface::POSTCODE, $postcode);
  519. }
  520. /**
  521. * @inheritdoc
  522. */
  523. public function setLastname($lastname)
  524. {
  525. return $this->setData(OrderAddressInterface::LASTNAME, $lastname);
  526. }
  527. /**
  528. * @inheritdoc
  529. */
  530. public function setCity($city)
  531. {
  532. return $this->setData(OrderAddressInterface::CITY, $city);
  533. }
  534. /**
  535. * @inheritdoc
  536. */
  537. public function setEmail($email)
  538. {
  539. return $this->setData(OrderAddressInterface::EMAIL, $email);
  540. }
  541. /**
  542. * @inheritdoc
  543. */
  544. public function setTelephone($telephone)
  545. {
  546. return $this->setData(OrderAddressInterface::TELEPHONE, $telephone);
  547. }
  548. /**
  549. * @inheritdoc
  550. */
  551. public function setCountryId($id)
  552. {
  553. return $this->setData(OrderAddressInterface::COUNTRY_ID, $id);
  554. }
  555. /**
  556. * @inheritdoc
  557. */
  558. public function setFirstname($firstname)
  559. {
  560. return $this->setData(OrderAddressInterface::FIRSTNAME, $firstname);
  561. }
  562. /**
  563. * @inheritdoc
  564. */
  565. public function setAddressType($addressType)
  566. {
  567. return $this->setData(OrderAddressInterface::ADDRESS_TYPE, $addressType);
  568. }
  569. /**
  570. * @inheritdoc
  571. */
  572. public function setPrefix($prefix)
  573. {
  574. return $this->setData(OrderAddressInterface::PREFIX, $prefix);
  575. }
  576. /**
  577. * @inheritdoc
  578. */
  579. public function setMiddlename($middlename)
  580. {
  581. return $this->setData(OrderAddressInterface::MIDDLENAME, $middlename);
  582. }
  583. /**
  584. * @inheritdoc
  585. */
  586. public function setSuffix($suffix)
  587. {
  588. return $this->setData(OrderAddressInterface::SUFFIX, $suffix);
  589. }
  590. /**
  591. * @inheritdoc
  592. */
  593. public function setCompany($company)
  594. {
  595. return $this->setData(OrderAddressInterface::COMPANY, $company);
  596. }
  597. /**
  598. * @inheritdoc
  599. */
  600. public function setVatId($id)
  601. {
  602. return $this->setData(OrderAddressInterface::VAT_ID, $id);
  603. }
  604. /**
  605. * @inheritdoc
  606. */
  607. public function setVatIsValid($vatIsValid)
  608. {
  609. return $this->setData(OrderAddressInterface::VAT_IS_VALID, $vatIsValid);
  610. }
  611. /**
  612. * @inheritdoc
  613. */
  614. public function setVatRequestId($id)
  615. {
  616. return $this->setData(OrderAddressInterface::VAT_REQUEST_ID, $id);
  617. }
  618. /**
  619. * @inheritdoc
  620. */
  621. public function setRegionCode($regionCode)
  622. {
  623. return $this->setData(OrderAddressInterface::KEY_REGION_CODE, $regionCode);
  624. }
  625. /**
  626. * @inheritdoc
  627. */
  628. public function setVatRequestDate($vatRequestDate)
  629. {
  630. return $this->setData(OrderAddressInterface::VAT_REQUEST_DATE, $vatRequestDate);
  631. }
  632. /**
  633. * @inheritdoc
  634. */
  635. public function setVatRequestSuccess($vatRequestSuccess)
  636. {
  637. return $this->setData(OrderAddressInterface::VAT_REQUEST_SUCCESS, $vatRequestSuccess);
  638. }
  639. /**
  640. * @inheritdoc
  641. *
  642. * @return \Magento\Sales\Api\Data\OrderAddressExtensionInterface|null
  643. */
  644. public function getExtensionAttributes()
  645. {
  646. return $this->_getExtensionAttributes();
  647. }
  648. /**
  649. * @inheritdoc
  650. *
  651. * @param \Magento\Sales\Api\Data\OrderAddressExtensionInterface $extensionAttributes
  652. *
  653. * @return $this
  654. */
  655. public function setExtensionAttributes(\Magento\Sales\Api\Data\OrderAddressExtensionInterface $extensionAttributes)
  656. {
  657. return $this->_setExtensionAttributes($extensionAttributes);
  658. }
  659. //@codeCoverageIgnoreEnd
  660. }