Address.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  9. use Magento\Customer\Api\Data\RegionInterfaceFactory;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Framework\Serialize\Serializer\Json;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. /**
  14. * Sales Quote address model
  15. *
  16. * @api
  17. * @method int getQuoteId()
  18. * @method Address setQuoteId(int $value)
  19. * @method string getCreatedAt()
  20. * @method Address setCreatedAt(string $value)
  21. * @method string getUpdatedAt()
  22. * @method Address setUpdatedAt(string $value)
  23. * @method \Magento\Customer\Api\Data\AddressInterface getCustomerAddress()
  24. * @method Address setCustomerAddressData(\Magento\Customer\Api\Data\AddressInterface $value)
  25. * @method string getAddressType()
  26. * @method Address setAddressType(string $value)
  27. * @method int getFreeShipping()
  28. * @method Address setFreeShipping(int $value)
  29. * @method bool getCollectShippingRates()
  30. * @method Address setCollectShippingRates(bool $value)
  31. * @method Address setShippingMethod(string $value)
  32. * @method string getShippingDescription()
  33. * @method Address setShippingDescription(string $value)
  34. * @method float getWeight()
  35. * @method Address setWeight(float $value)
  36. * @method float getSubtotal()
  37. * @method Address setSubtotal(float $value)
  38. * @method float getBaseSubtotal()
  39. * @method Address setBaseSubtotal(float $value)
  40. * @method Address setSubtotalWithDiscount(float $value)
  41. * @method Address setBaseSubtotalWithDiscount(float $value)
  42. * @method float getTaxAmount()
  43. * @method Address setTaxAmount(float $value)
  44. * @method float getBaseTaxAmount()
  45. * @method Address setBaseTaxAmount(float $value)
  46. * @method float getShippingAmount()
  47. * @method float getBaseShippingAmount()
  48. * @method float getShippingTaxAmount()
  49. * @method Address setShippingTaxAmount(float $value)
  50. * @method float getBaseShippingTaxAmount()
  51. * @method Address setBaseShippingTaxAmount(float $value)
  52. * @method float getDiscountAmount()
  53. * @method Address setDiscountAmount(float $value)
  54. * @method float getBaseDiscountAmount()
  55. * @method Address setBaseDiscountAmount(float $value)
  56. * @method float getGrandTotal()
  57. * @method Address setGrandTotal(float $value)
  58. * @method float getBaseGrandTotal()
  59. * @method Address setBaseGrandTotal(float $value)
  60. * @method string getCustomerNotes()
  61. * @method Address setCustomerNotes(string $value)
  62. * @method string getDiscountDescription()
  63. * @method Address setDiscountDescription(string $value)
  64. * @method null|array getDiscountDescriptionArray()
  65. * @method Address setDiscountDescriptionArray(array $value)
  66. * @method float getShippingDiscountAmount()
  67. * @method Address setShippingDiscountAmount(float $value)
  68. * @method float getBaseShippingDiscountAmount()
  69. * @method Address setBaseShippingDiscountAmount(float $value)
  70. * @method float getSubtotalInclTax()
  71. * @method Address setSubtotalInclTax(float $value)
  72. * @method float getBaseSubtotalTotalInclTax()
  73. * @method Address setBaseSubtotalTotalInclTax(float $value)
  74. * @method int getGiftMessageId()
  75. * @method Address setGiftMessageId(int $value)
  76. * @method float getDiscountTaxCompensationAmount()
  77. * @method Address setDiscountTaxCompensationAmount(float $value)
  78. * @method float getBaseDiscountTaxCompensationAmount()
  79. * @method Address setBaseDiscountTaxCompensationAmount(float $value)
  80. * @method float getShippingDiscountTaxCompensationAmount()
  81. * @method Address setShippingDiscountTaxCompensationAmount(float $value)
  82. * @method float getBaseShippingDiscountTaxCompensationAmnt()
  83. * @method Address setBaseShippingDiscountTaxCompensationAmnt(float $value)
  84. * @method float getShippingInclTax()
  85. * @method Address setShippingInclTax(float $value)
  86. * @method float getBaseShippingInclTax()
  87. * @method \Magento\SalesRule\Model\Rule[] getCartFixedRules()
  88. * @method int[] getAppliedRuleIds()
  89. * @method Address setBaseShippingInclTax(float $value)
  90. *
  91. * @property $_objectCopyService \Magento\Framework\DataObject\Copy
  92. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  93. * @SuppressWarnings(PHPMD.TooManyFields)
  94. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  95. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  96. * @since 100.0.2
  97. */
  98. class Address extends \Magento\Customer\Model\Address\AbstractAddress implements
  99. \Magento\Quote\Api\Data\AddressInterface
  100. {
  101. const RATES_FETCH = 1;
  102. const RATES_RECALCULATE = 2;
  103. const ADDRESS_TYPE_BILLING = 'billing';
  104. const ADDRESS_TYPE_SHIPPING = 'shipping';
  105. /**
  106. * Prefix of model events
  107. *
  108. * @var string
  109. */
  110. protected $_eventPrefix = 'sales_quote_address';
  111. /**
  112. * Name of event object
  113. *
  114. * @var string
  115. */
  116. protected $_eventObject = 'quote_address';
  117. /**
  118. * Quote object
  119. *
  120. * @var \Magento\Quote\Model\Quote
  121. */
  122. protected $_items;
  123. /**
  124. * Quote object
  125. *
  126. * @var \Magento\Quote\Model\Quote
  127. */
  128. protected $_quote;
  129. /**
  130. * Sales Quote address rates
  131. *
  132. * @var \Magento\Quote\Model\Quote\Address\Rate
  133. */
  134. protected $_rates;
  135. /**
  136. * Total models collector
  137. *
  138. * @var \Magento\Quote\Model\Quote\Address\Total\Collector
  139. */
  140. protected $_totalCollector;
  141. /**
  142. * Total data as array
  143. *
  144. * @var array
  145. */
  146. protected $_totals = [];
  147. /**
  148. * @var array
  149. */
  150. protected $_totalAmounts = [];
  151. /**
  152. * @var array
  153. */
  154. protected $_baseTotalAmounts = [];
  155. /**
  156. * Core store config
  157. *
  158. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  159. */
  160. protected $_scopeConfig;
  161. /**
  162. * @var \Magento\Quote\Model\Quote\Address\ItemFactory
  163. */
  164. protected $_addressItemFactory;
  165. /**
  166. * @var \Magento\Quote\Model\ResourceModel\Quote\Address\Item\CollectionFactory
  167. */
  168. protected $_itemCollectionFactory;
  169. /**
  170. * @var \Magento\Quote\Model\Quote\Address\RateCollectorInterfaceFactory
  171. */
  172. protected $_rateCollector;
  173. /**
  174. * @var \Magento\Quote\Model\ResourceModel\Quote\Address\Rate\CollectionFactory
  175. */
  176. protected $_rateCollectionFactory;
  177. /**
  178. * @var \Magento\Quote\Model\Quote\Address\Total\CollectorFactory
  179. */
  180. protected $_totalCollectorFactory;
  181. /**
  182. * @var \Magento\Quote\Model\Quote\Address\TotalFactory
  183. */
  184. protected $_addressTotalFactory;
  185. /**
  186. * @var \Magento\Quote\Model\Quote\Address\RateFactory
  187. * @since 101.0.0
  188. */
  189. protected $_addressRateFactory;
  190. /**
  191. * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
  192. */
  193. protected $addressDataFactory;
  194. /**
  195. * @var Address\Validator
  196. */
  197. protected $validator;
  198. /**
  199. * @var \Magento\Customer\Model\Address\Mapper
  200. */
  201. protected $addressMapper;
  202. /**
  203. * @var Address\RateRequestFactory
  204. */
  205. protected $_rateRequestFactory;
  206. /**
  207. * @var Address\CustomAttributeListInterface
  208. */
  209. protected $attributeList;
  210. /**
  211. * @var TotalsCollector
  212. */
  213. protected $totalsCollector;
  214. /**
  215. * @var \Magento\Quote\Model\Quote\TotalsReader
  216. */
  217. protected $totalsReader;
  218. /**
  219. * @var Json
  220. */
  221. private $serializer;
  222. /**
  223. * @var StoreManagerInterface
  224. */
  225. private $storeManager;
  226. /**
  227. * @param \Magento\Framework\Model\Context $context
  228. * @param \Magento\Framework\Registry $registry
  229. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  230. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  231. * @param \Magento\Directory\Helper\Data $directoryData
  232. * @param \Magento\Eav\Model\Config $eavConfig
  233. * @param \Magento\Customer\Model\Address\Config $addressConfig
  234. * @param \Magento\Directory\Model\RegionFactory $regionFactory
  235. * @param \Magento\Directory\Model\CountryFactory $countryFactory
  236. * @param AddressMetadataInterface $metadataService
  237. * @param AddressInterfaceFactory $addressDataFactory
  238. * @param RegionInterfaceFactory $regionDataFactory
  239. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  240. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  241. * @param Address\ItemFactory $addressItemFactory
  242. * @param \Magento\Quote\Model\ResourceModel\Quote\Address\Item\CollectionFactory $itemCollectionFactory
  243. * @param \Magento\Quote\Model\Quote\Address\RateFactory $addressRateFactory
  244. * @param Address\RateCollectorInterfaceFactory $rateCollector
  245. * @param \Magento\Quote\Model\ResourceModel\Quote\Address\Rate\CollectionFactory $rateCollectionFactory
  246. * @param Address\RateRequestFactory $rateRequestFactory
  247. * @param Address\Total\CollectorFactory $totalCollectorFactory
  248. * @param Address\TotalFactory $addressTotalFactory
  249. * @param \Magento\Framework\DataObject\Copy $objectCopyService
  250. * @param \Magento\Shipping\Model\CarrierFactoryInterface $carrierFactory
  251. * @param Address\Validator $validator
  252. * @param \Magento\Customer\Model\Address\Mapper $addressMapper
  253. * @param Address\CustomAttributeListInterface $attributeList
  254. * @param TotalsCollector $totalsCollector
  255. * @param \Magento\Quote\Model\Quote\TotalsReader $totalsReader
  256. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  257. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  258. * @param array $data
  259. * @param Json $serializer
  260. * @param StoreManagerInterface $storeManager
  261. *
  262. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  263. */
  264. public function __construct(
  265. \Magento\Framework\Model\Context $context,
  266. \Magento\Framework\Registry $registry,
  267. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  268. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  269. \Magento\Directory\Helper\Data $directoryData,
  270. \Magento\Eav\Model\Config $eavConfig,
  271. \Magento\Customer\Model\Address\Config $addressConfig,
  272. \Magento\Directory\Model\RegionFactory $regionFactory,
  273. \Magento\Directory\Model\CountryFactory $countryFactory,
  274. AddressMetadataInterface $metadataService,
  275. AddressInterfaceFactory $addressDataFactory,
  276. RegionInterfaceFactory $regionDataFactory,
  277. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  278. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  279. \Magento\Quote\Model\Quote\Address\ItemFactory $addressItemFactory,
  280. \Magento\Quote\Model\ResourceModel\Quote\Address\Item\CollectionFactory $itemCollectionFactory,
  281. \Magento\Quote\Model\Quote\Address\RateFactory $addressRateFactory,
  282. \Magento\Quote\Model\Quote\Address\RateCollectorInterfaceFactory $rateCollector,
  283. \Magento\Quote\Model\ResourceModel\Quote\Address\Rate\CollectionFactory $rateCollectionFactory,
  284. \Magento\Quote\Model\Quote\Address\RateRequestFactory $rateRequestFactory,
  285. \Magento\Quote\Model\Quote\Address\Total\CollectorFactory $totalCollectorFactory,
  286. \Magento\Quote\Model\Quote\Address\TotalFactory $addressTotalFactory,
  287. \Magento\Framework\DataObject\Copy $objectCopyService,
  288. \Magento\Shipping\Model\CarrierFactoryInterface $carrierFactory,
  289. Address\Validator $validator,
  290. \Magento\Customer\Model\Address\Mapper $addressMapper,
  291. Address\CustomAttributeListInterface $attributeList,
  292. \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
  293. \Magento\Quote\Model\Quote\TotalsReader $totalsReader,
  294. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  295. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  296. array $data = [],
  297. Json $serializer = null,
  298. StoreManagerInterface $storeManager = null
  299. ) {
  300. $this->_scopeConfig = $scopeConfig;
  301. $this->_addressItemFactory = $addressItemFactory;
  302. $this->_itemCollectionFactory = $itemCollectionFactory;
  303. $this->_addressRateFactory = $addressRateFactory;
  304. $this->_rateCollector = $rateCollector;
  305. $this->_rateCollectionFactory = $rateCollectionFactory;
  306. $this->_rateRequestFactory = $rateRequestFactory;
  307. $this->_totalCollectorFactory = $totalCollectorFactory;
  308. $this->_addressTotalFactory = $addressTotalFactory;
  309. $this->_objectCopyService = $objectCopyService;
  310. $this->_carrierFactory = $carrierFactory;
  311. $this->addressDataFactory = $addressDataFactory;
  312. $this->validator = $validator;
  313. $this->addressMapper = $addressMapper;
  314. $this->attributeList = $attributeList;
  315. $this->totalsCollector = $totalsCollector;
  316. $this->totalsReader = $totalsReader;
  317. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  318. $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
  319. parent::__construct(
  320. $context,
  321. $registry,
  322. $extensionFactory,
  323. $customAttributeFactory,
  324. $directoryData,
  325. $eavConfig,
  326. $addressConfig,
  327. $regionFactory,
  328. $countryFactory,
  329. $metadataService,
  330. $addressDataFactory,
  331. $regionDataFactory,
  332. $dataObjectHelper,
  333. $resource,
  334. $resourceCollection,
  335. $data
  336. );
  337. }
  338. /**
  339. * Initialize resource
  340. *
  341. * @return void
  342. */
  343. protected function _construct()
  344. {
  345. $this->_init(\Magento\Quote\Model\ResourceModel\Quote\Address::class);
  346. }
  347. /**
  348. * Initialize quote identifier before save
  349. *
  350. * @return $this
  351. */
  352. public function beforeSave()
  353. {
  354. parent::beforeSave();
  355. $this->_populateBeforeSaveData();
  356. return $this;
  357. }
  358. /**
  359. * Set the required fields
  360. *
  361. * @return void
  362. */
  363. protected function _populateBeforeSaveData()
  364. {
  365. if ($this->getQuote()) {
  366. $this->_dataSaveAllowed = (bool)$this->getQuote()->getId();
  367. if ($this->getQuote()->getId()) {
  368. $this->setQuoteId($this->getQuote()->getId());
  369. }
  370. $this->setCustomerId($this->getQuote()->getCustomerId());
  371. /**
  372. * Init customer address id if customer address is assigned
  373. */
  374. if ($this->getCustomerAddressData()) {
  375. $this->setCustomerAddressId($this->getCustomerAddressData()->getId());
  376. }
  377. if (!$this->getId()) {
  378. $this->setSameAsBilling((int)$this->_isSameAsBilling());
  379. }
  380. }
  381. }
  382. /**
  383. * Returns true if shipping address is same as billing
  384. *
  385. * @return bool
  386. */
  387. protected function _isSameAsBilling()
  388. {
  389. return $this->getAddressType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING &&
  390. ($this->_isNotRegisteredCustomer() ||
  391. $this->_isDefaultShippingNullOrSameAsBillingAddress());
  392. }
  393. /**
  394. * Checks if the user is a registered customer
  395. *
  396. * @return bool
  397. */
  398. protected function _isNotRegisteredCustomer()
  399. {
  400. return !$this->getQuote()->getCustomerId() || $this->getCustomerAddressId() === null;
  401. }
  402. /**
  403. * Returns true if shipping address is same as billing or it is undefined
  404. *
  405. * @return bool
  406. */
  407. protected function _isDefaultShippingNullOrSameAsBillingAddress()
  408. {
  409. $customer = $this->getQuote()->getCustomer();
  410. $customerId = $customer->getId();
  411. $defaultBillingAddress = null;
  412. $defaultShippingAddress = null;
  413. if ($customerId) {
  414. /* we should load data from the service once customer is saved */
  415. $defaultBillingAddress = $customer->getDefaultBilling();
  416. $defaultShippingAddress = $customer->getDefaultShipping();
  417. } else {
  418. /* we should load data from the quote if customer is not saved yet */
  419. $defaultBillingAddress = $customer->getDefaultBilling();
  420. $defaultShippingAddress = $customer->getDefaultShipping();
  421. }
  422. return !$defaultShippingAddress
  423. || $defaultBillingAddress
  424. && $defaultShippingAddress
  425. && $defaultBillingAddress == $defaultShippingAddress;
  426. }
  427. /**
  428. * Declare address quote model object
  429. *
  430. * @param \Magento\Quote\Model\Quote $quote
  431. * @return $this
  432. */
  433. public function setQuote(\Magento\Quote\Model\Quote $quote)
  434. {
  435. $this->_quote = $quote;
  436. $this->setQuoteId($quote->getId());
  437. return $this;
  438. }
  439. /**
  440. * Retrieve quote object
  441. *
  442. * @return \Magento\Quote\Model\Quote
  443. */
  444. public function getQuote()
  445. {
  446. return $this->_quote;
  447. }
  448. /**
  449. * Import quote address data from customer address Data Object.
  450. *
  451. * @param \Magento\Customer\Api\Data\AddressInterface $address
  452. * @return $this
  453. */
  454. public function importCustomerAddressData(\Magento\Customer\Api\Data\AddressInterface $address)
  455. {
  456. $this->_objectCopyService->copyFieldsetToTarget(
  457. 'customer_address',
  458. 'to_quote_address',
  459. $this->addressMapper->toFlatArray($address),
  460. $this
  461. );
  462. $quote = $this->getQuote();
  463. if ($address->getCustomerId() && (!empty($quote) && $address->getCustomerId() == $quote->getCustomerId())) {
  464. $customer = $quote->getCustomer();
  465. $this->setEmail($customer->getEmail());
  466. }
  467. return $this;
  468. }
  469. /**
  470. * Export data to customer address Data Object.
  471. *
  472. * @return \Magento\Customer\Api\Data\AddressInterface
  473. */
  474. public function exportCustomerAddress()
  475. {
  476. $customerAddressData = $this->_objectCopyService->getDataFromFieldset(
  477. 'sales_convert_quote_address',
  478. 'to_customer_address',
  479. $this
  480. );
  481. $customerAddressDataWithRegion = [];
  482. $customerAddressDataWithRegion['region']['region'] = $customerAddressData['region'];
  483. if (isset($customerAddressData['region_code'])) {
  484. $customerAddressDataWithRegion['region']['region_code'] = $customerAddressData['region_code'];
  485. }
  486. if ($customerAddressData['region_id']) {
  487. $customerAddressDataWithRegion['region']['region_id'] = $customerAddressData['region_id'];
  488. }
  489. $customerAddressData = array_merge($customerAddressData, $customerAddressDataWithRegion);
  490. $addressDataObject = $this->addressDataFactory->create();
  491. $this->dataObjectHelper->populateWithArray(
  492. $addressDataObject,
  493. $customerAddressData,
  494. \Magento\Customer\Api\Data\AddressInterface::class
  495. );
  496. return $addressDataObject;
  497. }
  498. /**
  499. * Convert object to array
  500. *
  501. * @param array $arrAttributes
  502. * @return array
  503. */
  504. public function toArray(array $arrAttributes = [])
  505. {
  506. $arr = parent::toArray($arrAttributes);
  507. $arr['rates'] = $this->getShippingRatesCollection()->toArray($arrAttributes);
  508. $arr['items'] = $this->getItemsCollection()->toArray($arrAttributes);
  509. foreach ($this->getTotals() as $k => $total) {
  510. $arr['totals'][$k] = $total->toArray();
  511. }
  512. return $arr;
  513. }
  514. /**
  515. * Retrieve address items collection
  516. *
  517. * @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  518. */
  519. public function getItemsCollection()
  520. {
  521. if (null === $this->_items) {
  522. $this->_items = $this->_itemCollectionFactory->create()->setAddressFilter($this->getId());
  523. if ($this->getId()) {
  524. foreach ($this->_items as $item) {
  525. $item->setAddress($this);
  526. }
  527. }
  528. }
  529. return $this->_items;
  530. }
  531. /**
  532. * Get all available address items
  533. *
  534. * @return \Magento\Quote\Model\Quote\Address\Item[]
  535. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  536. * @SuppressWarnings(PHPMD.NPathComplexity)
  537. */
  538. public function getAllItems()
  539. {
  540. // We calculate item list once and cache it in three arrays - all items
  541. $key = 'cached_items_all';
  542. if (!$this->hasData($key)) {
  543. $quoteItems = $this->getQuote()->getItemsCollection();
  544. $addressItems = $this->getItemsCollection();
  545. $items = [];
  546. if ($this->getQuote()->getIsMultiShipping() && $addressItems->count() > 0) {
  547. foreach ($addressItems as $aItem) {
  548. if ($aItem->isDeleted()) {
  549. continue;
  550. }
  551. if (!$aItem->getQuoteItemImported()) {
  552. $qItem = $this->getQuote()->getItemById($aItem->getQuoteItemId());
  553. if ($qItem) {
  554. $aItem->importQuoteItem($qItem);
  555. }
  556. }
  557. $items[] = $aItem;
  558. }
  559. } else {
  560. /*
  561. * For virtual quote we assign items only to billing address, otherwise - only to shipping address
  562. */
  563. $addressType = $this->getAddressType();
  564. $canAddItems = $this->getQuote()->isVirtual()
  565. ? $addressType == self::TYPE_BILLING
  566. : $addressType == self::TYPE_SHIPPING;
  567. if ($canAddItems) {
  568. foreach ($quoteItems as $qItem) {
  569. if ($qItem->isDeleted()) {
  570. continue;
  571. }
  572. $items[] = $qItem;
  573. }
  574. }
  575. }
  576. // Cache calculated lists
  577. $this->setData('cached_items_all', $items);
  578. }
  579. $items = $this->getData($key);
  580. return $items;
  581. }
  582. /**
  583. * Retrieve all visible items
  584. *
  585. * @return array
  586. */
  587. public function getAllVisibleItems()
  588. {
  589. $items = [];
  590. foreach ($this->getAllItems() as $item) {
  591. if (!$item->getParentItemId()) {
  592. $items[] = $item;
  593. }
  594. }
  595. return $items;
  596. }
  597. /**
  598. * Retrieve item quantity by id
  599. *
  600. * @param int $itemId
  601. * @return float|int
  602. */
  603. public function getItemQty($itemId = 0)
  604. {
  605. if ($this->hasData('item_qty')) {
  606. return $this->getData('item_qty');
  607. }
  608. $qty = 0;
  609. if ($itemId == 0) {
  610. foreach ($this->getAllItems() as $item) {
  611. $qty += $item->getQty();
  612. }
  613. } else {
  614. $item = $this->getItemById($itemId);
  615. if ($item) {
  616. $qty = $item->getQty();
  617. }
  618. }
  619. return $qty;
  620. }
  621. /**
  622. * Check Quote address has Items
  623. *
  624. * @return bool
  625. */
  626. public function hasItems()
  627. {
  628. return sizeof($this->getAllItems()) > 0;
  629. }
  630. /**
  631. * Get address item object by id without
  632. *
  633. * @param int $itemId
  634. * @return \Magento\Quote\Model\Quote\Address\Item|false
  635. */
  636. public function getItemById($itemId)
  637. {
  638. foreach ($this->getItemsCollection() as $item) {
  639. if ($item->getId() == $itemId) {
  640. return $item;
  641. }
  642. }
  643. return false;
  644. }
  645. /**
  646. * Get prepared not deleted item
  647. *
  648. * @param int $itemId
  649. * @return \Magento\Quote\Model\Quote\Address\Item|false
  650. */
  651. public function getValidItemById($itemId)
  652. {
  653. foreach ($this->getAllItems() as $item) {
  654. if ($item->getId() == $itemId) {
  655. return $item;
  656. }
  657. }
  658. return false;
  659. }
  660. /**
  661. * Retrieve item object by quote item Id
  662. *
  663. * @param int $itemId
  664. * @return \Magento\Quote\Model\Quote\Address\Item|false
  665. */
  666. public function getItemByQuoteItemId($itemId)
  667. {
  668. foreach ($this->getItemsCollection() as $item) {
  669. if ($item->getQuoteItemId() == $itemId) {
  670. return $item;
  671. }
  672. }
  673. return false;
  674. }
  675. /**
  676. * Remove item from collection
  677. *
  678. * @param int $itemId
  679. * @return $this
  680. */
  681. public function removeItem($itemId)
  682. {
  683. $item = $this->getItemById($itemId);
  684. if ($item) {
  685. $item->isDeleted(true);
  686. }
  687. return $this;
  688. }
  689. /**
  690. * Add item to address
  691. *
  692. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  693. * @param int $qty
  694. * @return $this
  695. */
  696. public function addItem(\Magento\Quote\Model\Quote\Item\AbstractItem $item, $qty = null)
  697. {
  698. if ($item instanceof \Magento\Quote\Model\Quote\Item) {
  699. if ($item->getParentItemId()) {
  700. return $this;
  701. }
  702. $addressItem = $this->_addressItemFactory->create()->setAddress($this)->importQuoteItem($item);
  703. $this->getItemsCollection()->addItem($addressItem);
  704. if ($item->getHasChildren()) {
  705. foreach ($item->getChildren() as $child) {
  706. $addressChildItem = $this->_addressItemFactory->create()->setAddress(
  707. $this
  708. )->importQuoteItem(
  709. $child
  710. )->setParentItem(
  711. $addressItem
  712. );
  713. $this->getItemsCollection()->addItem($addressChildItem);
  714. }
  715. }
  716. } else {
  717. $addressItem = $item;
  718. $addressItem->setAddress($this);
  719. if (!$addressItem->getId()) {
  720. $this->getItemsCollection()->addItem($addressItem);
  721. }
  722. }
  723. if ($qty) {
  724. $addressItem->setQty($qty);
  725. }
  726. return $this;
  727. }
  728. /**
  729. * Retrieve collection of quote shipping rates
  730. *
  731. * @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  732. */
  733. public function getShippingRatesCollection()
  734. {
  735. if (null === $this->_rates) {
  736. $this->_rates = $this->_rateCollectionFactory->create()->setAddressFilter($this->getId());
  737. if ($this->getId()) {
  738. foreach ($this->_rates as $rate) {
  739. $rate->setAddress($this);
  740. }
  741. }
  742. }
  743. return $this->_rates;
  744. }
  745. /**
  746. * Retrieve all address shipping rates
  747. *
  748. * @return array
  749. */
  750. public function getAllShippingRates()
  751. {
  752. $rates = [];
  753. foreach ($this->getShippingRatesCollection() as $rate) {
  754. if (!$rate->isDeleted()) {
  755. $rates[] = $rate;
  756. }
  757. }
  758. return $rates;
  759. }
  760. /**
  761. * Retrieve all grouped shipping rates
  762. *
  763. * @return array
  764. */
  765. public function getGroupedAllShippingRates()
  766. {
  767. $rates = [];
  768. foreach ($this->getShippingRatesCollection() as $rate) {
  769. if (!$rate->isDeleted() && $this->_carrierFactory->get($rate->getCarrier())) {
  770. if (!isset($rates[$rate->getCarrier()])) {
  771. $rates[$rate->getCarrier()] = [];
  772. }
  773. $rates[$rate->getCarrier()][] = $rate;
  774. $rates[$rate->getCarrier()][0]->carrier_sort_order = $this->_carrierFactory->get(
  775. $rate->getCarrier()
  776. )->getSortOrder();
  777. }
  778. }
  779. uasort($rates, [$this, '_sortRates']);
  780. return $rates;
  781. }
  782. /**
  783. * Sort rates recursive callback
  784. *
  785. * @param array $firstItem
  786. * @param array $secondItem
  787. * @return int
  788. */
  789. protected function _sortRates($firstItem, $secondItem)
  790. {
  791. return (int) $firstItem[0]->carrier_sort_order <=> (int) $secondItem[0]->carrier_sort_order;
  792. }
  793. /**
  794. * Retrieve shipping rate by identifier
  795. *
  796. * @param int $rateId
  797. * @return \Magento\Quote\Model\Quote\Address\Rate|false
  798. */
  799. public function getShippingRateById($rateId)
  800. {
  801. foreach ($this->getShippingRatesCollection() as $rate) {
  802. if ($rate->getId() == $rateId) {
  803. return $rate;
  804. }
  805. }
  806. return false;
  807. }
  808. /**
  809. * Retrieve shipping rate by code
  810. *
  811. * @param string $code
  812. * @return \Magento\Quote\Model\Quote\Address\Rate|false
  813. */
  814. public function getShippingRateByCode($code)
  815. {
  816. foreach ($this->getShippingRatesCollection() as $rate) {
  817. if ($rate->getCode() == $code) {
  818. return $rate;
  819. }
  820. }
  821. return false;
  822. }
  823. /**
  824. * Mark all shipping rates as deleted
  825. *
  826. * @return $this
  827. */
  828. public function removeAllShippingRates()
  829. {
  830. foreach ($this->getShippingRatesCollection() as $rate) {
  831. $rate->isDeleted(true);
  832. }
  833. return $this;
  834. }
  835. /**
  836. * Add shipping rate
  837. *
  838. * @param \Magento\Quote\Model\Quote\Address\Rate $rate
  839. * @return $this
  840. */
  841. public function addShippingRate(\Magento\Quote\Model\Quote\Address\Rate $rate)
  842. {
  843. $rate->setAddress($this);
  844. $this->getShippingRatesCollection()->addItem($rate);
  845. return $this;
  846. }
  847. /**
  848. * Collecting shipping rates by address
  849. *
  850. * @return $this
  851. */
  852. public function collectShippingRates()
  853. {
  854. if (!$this->getCollectShippingRates()) {
  855. return $this;
  856. }
  857. $this->setCollectShippingRates(false);
  858. $this->removeAllShippingRates();
  859. if (!$this->getCountryId()) {
  860. return $this;
  861. }
  862. $found = $this->requestShippingRates();
  863. if (!$found) {
  864. $this->setShippingAmount(0)->setBaseShippingAmount(0)->setShippingMethod('')->setShippingDescription('');
  865. }
  866. return $this;
  867. }
  868. /**
  869. * Request shipping rates for entire address or specified address item
  870. *
  871. * Returns true if current selected shipping method code corresponds to one of the found rates
  872. *
  873. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  874. * @return bool
  875. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  876. * @SuppressWarnings(PHPMD.NPathComplexity)
  877. */
  878. public function requestShippingRates(\Magento\Quote\Model\Quote\Item\AbstractItem $item = null)
  879. {
  880. /** @var $request \Magento\Quote\Model\Quote\Address\RateRequest */
  881. $request = $this->_rateRequestFactory->create();
  882. $request->setAllItems($item ? [$item] : $this->getAllItems());
  883. $request->setDestCountryId($this->getCountryId());
  884. $request->setDestRegionId($this->getRegionId());
  885. $request->setDestRegionCode($this->getRegionCode());
  886. $request->setDestStreet($this->getStreetFull());
  887. $request->setDestCity($this->getCity());
  888. $request->setDestPostcode($this->getPostcode());
  889. $request->setPackageValue($item ? $item->getBaseRowTotal() : $this->getBaseSubtotal());
  890. $packageWithDiscount = $item ? $item->getBaseRowTotal() -
  891. $item->getBaseDiscountAmount() : $this->getBaseSubtotalWithDiscount();
  892. $request->setPackageValueWithDiscount($packageWithDiscount);
  893. $request->setPackageWeight($item ? $item->getRowWeight() : $this->getWeight());
  894. $request->setPackageQty($item ? $item->getQty() : $this->getItemQty());
  895. /**
  896. * Need for shipping methods that use insurance based on price of physical products
  897. */
  898. $packagePhysicalValue = $item ? $item->getBaseRowTotal() : $this->getBaseSubtotal() -
  899. $this->getBaseVirtualAmount();
  900. $request->setPackagePhysicalValue($packagePhysicalValue);
  901. $request->setFreeMethodWeight($item ? 0 : $this->getFreeMethodWeight());
  902. /**
  903. * Store and website identifiers specified from StoreManager
  904. */
  905. if ($this->getQuote()->getStoreId()) {
  906. $storeId = $this->getQuote()->getStoreId();
  907. $request->setStoreId($storeId);
  908. $request->setWebsiteId($this->storeManager->getStore($storeId)->getWebsiteId());
  909. } else {
  910. $request->setStoreId($this->storeManager->getStore()->getId());
  911. $request->setWebsiteId($this->storeManager->getWebsite()->getId());
  912. }
  913. $request->setFreeShipping($this->getFreeShipping());
  914. /**
  915. * Currencies need to convert in free shipping
  916. */
  917. $request->setBaseCurrency($this->storeManager->getStore()->getBaseCurrency());
  918. $request->setPackageCurrency($this->storeManager->getStore()->getCurrentCurrency());
  919. $request->setLimitCarrier($this->getLimitCarrier());
  920. $baseSubtotalInclTax = $this->getBaseSubtotalTotalInclTax();
  921. $request->setBaseSubtotalInclTax($baseSubtotalInclTax);
  922. $result = $this->_rateCollector->create()->collectRates($request)->getResult();
  923. $found = false;
  924. if ($result) {
  925. $shippingRates = $result->getAllRates();
  926. foreach ($shippingRates as $shippingRate) {
  927. $rate = $this->_addressRateFactory->create()->importShippingRate($shippingRate);
  928. if (!$item) {
  929. $this->addShippingRate($rate);
  930. }
  931. if ($this->getShippingMethod() == $rate->getCode()) {
  932. if ($item) {
  933. $item->setBaseShippingAmount($rate->getPrice());
  934. } else {
  935. /** @var \Magento\Store\Api\Data\StoreInterface */
  936. $store = $this->storeManager->getStore();
  937. $amountPrice = $store->getBaseCurrency()
  938. ->convert($rate->getPrice(), $store->getCurrentCurrencyCode());
  939. $this->setBaseShippingAmount($rate->getPrice());
  940. $this->setShippingAmount($amountPrice);
  941. }
  942. $found = true;
  943. }
  944. }
  945. }
  946. return $found;
  947. }
  948. /******************************* Total Collector Interface *******************************************/
  949. /**
  950. * Get address totals as array
  951. *
  952. * @return array
  953. */
  954. public function getTotals()
  955. {
  956. $totalsData = array_merge($this->getData(), ['address_quote_items' => $this->getAllItems()]);
  957. $totals = $this->totalsReader->fetch($this->getQuote(), $totalsData);
  958. foreach ($totals as $total) {
  959. $this->addTotal($total);
  960. }
  961. return $this->_totals;
  962. }
  963. /**
  964. * Add total data or model
  965. *
  966. * @param \Magento\Quote\Model\Quote\Address\Total|array $total
  967. * @return $this
  968. */
  969. public function addTotal($total)
  970. {
  971. $addressTotal = null;
  972. if (is_array($total)) {
  973. /** @var \Magento\Quote\Model\Quote\Address\Total $addressTotal */
  974. $addressTotal = $this->_addressTotalFactory->create(\Magento\Quote\Model\Quote\Address\Total::class);
  975. $addressTotal->setData($total);
  976. } elseif ($total instanceof \Magento\Quote\Model\Quote\Address\Total) {
  977. $addressTotal = $total;
  978. }
  979. if ($addressTotal !== null) {
  980. $addressTotal->setAddress($this);
  981. $this->_totals[$addressTotal->getCode()] = $addressTotal;
  982. }
  983. return $this;
  984. }
  985. /******************************* End Total Collector Interface *******************************************/
  986. /**
  987. * Rewrite clone method
  988. *
  989. * @return \Magento\Quote\Model\Quote\Address
  990. */
  991. public function __clone()
  992. {
  993. $this->setId(null);
  994. }
  995. /**
  996. * Checks if it was set
  997. *
  998. * @return bool
  999. */
  1000. public function itemsCollectionWasSet()
  1001. {
  1002. return null !== $this->_items;
  1003. }
  1004. /**
  1005. * Checks if it was set
  1006. *
  1007. * @return bool
  1008. */
  1009. public function shippingRatesCollectionWasSet()
  1010. {
  1011. return null !== $this->_rates;
  1012. }
  1013. /**
  1014. * Validate minimum amount
  1015. *
  1016. * @return bool
  1017. */
  1018. public function validateMinimumAmount()
  1019. {
  1020. $storeId = $this->getQuote()->getStoreId();
  1021. $validateEnabled = $this->_scopeConfig->isSetFlag(
  1022. 'sales/minimum_order/active',
  1023. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1024. $storeId
  1025. );
  1026. if (!$validateEnabled) {
  1027. return true;
  1028. }
  1029. if (!$this->getQuote()->getIsVirtual() xor $this->getAddressType() == self::TYPE_SHIPPING) {
  1030. return true;
  1031. }
  1032. $includeDiscount = $this->_scopeConfig->getValue(
  1033. 'sales/minimum_order/include_discount_amount',
  1034. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1035. $storeId
  1036. );
  1037. $amount = $this->_scopeConfig->getValue(
  1038. 'sales/minimum_order/amount',
  1039. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1040. $storeId
  1041. );
  1042. $taxInclude = $this->_scopeConfig->getValue(
  1043. 'sales/minimum_order/tax_including',
  1044. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1045. $storeId
  1046. );
  1047. $taxes = $taxInclude ? $this->getBaseTaxAmount() : 0;
  1048. return $includeDiscount ?
  1049. ($this->getBaseSubtotalWithDiscount() + $taxes >= $amount) :
  1050. ($this->getBaseSubtotal() + $taxes >= $amount);
  1051. }
  1052. /**
  1053. * Retrieve applied taxes
  1054. *
  1055. * @return array
  1056. */
  1057. public function getAppliedTaxes()
  1058. {
  1059. $taxes = $this->getData('applied_taxes');
  1060. return $taxes ? $this->serializer->unserialize($taxes) : [];
  1061. }
  1062. /**
  1063. * Set applied taxes
  1064. *
  1065. * @param array $data
  1066. * @return $this
  1067. */
  1068. public function setAppliedTaxes($data)
  1069. {
  1070. return $this->setData('applied_taxes', $this->serializer->serialize($data));
  1071. }
  1072. /******************************* Start Total Collector Interface *******************************************/
  1073. /**
  1074. * Set shipping amount
  1075. *
  1076. * @param float $value
  1077. * @param bool $alreadyExclTax
  1078. * @return $this
  1079. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  1080. */
  1081. public function setShippingAmount($value, $alreadyExclTax = false)
  1082. {
  1083. return $this->setData('shipping_amount', $value);
  1084. }
  1085. /**
  1086. * Set base shipping amount
  1087. *
  1088. * @param float $value
  1089. * @param bool $alreadyExclTax
  1090. * @return $this
  1091. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  1092. */
  1093. public function setBaseShippingAmount($value, $alreadyExclTax = false)
  1094. {
  1095. return $this->setData('base_shipping_amount', $value);
  1096. }
  1097. /**
  1098. * Set total amount value
  1099. *
  1100. * @param string $code
  1101. * @param float $amount
  1102. * @return $this
  1103. */
  1104. public function setTotalAmount($code, $amount)
  1105. {
  1106. $this->_totalAmounts[$code] = $amount;
  1107. if ($code != 'subtotal') {
  1108. $code = $code . '_amount';
  1109. }
  1110. $this->setData($code, $amount);
  1111. return $this;
  1112. }
  1113. /**
  1114. * Set total amount value in base store currency
  1115. *
  1116. * @param string $code
  1117. * @param float $amount
  1118. * @return $this
  1119. */
  1120. public function setBaseTotalAmount($code, $amount)
  1121. {
  1122. $this->_baseTotalAmounts[$code] = $amount;
  1123. if ($code != 'subtotal') {
  1124. $code = $code . '_amount';
  1125. }
  1126. $this->setData('base_' . $code, $amount);
  1127. return $this;
  1128. }
  1129. /**
  1130. * Add amount total amount value
  1131. *
  1132. * @param string $code
  1133. * @param float $amount
  1134. * @return $this
  1135. */
  1136. public function addTotalAmount($code, $amount)
  1137. {
  1138. $amount = $this->getTotalAmount($code) + $amount;
  1139. $this->setTotalAmount($code, $amount);
  1140. return $this;
  1141. }
  1142. /**
  1143. * Add amount total amount value in base store currency
  1144. *
  1145. * @param string $code
  1146. * @param float $amount
  1147. * @return $this
  1148. */
  1149. public function addBaseTotalAmount($code, $amount)
  1150. {
  1151. $amount = $this->getBaseTotalAmount($code) + $amount;
  1152. $this->setBaseTotalAmount($code, $amount);
  1153. return $this;
  1154. }
  1155. /**
  1156. * Get total amount value by code
  1157. *
  1158. * @param string $code
  1159. * @return float|int
  1160. */
  1161. public function getTotalAmount($code)
  1162. {
  1163. if (isset($this->_totalAmounts[$code])) {
  1164. return $this->_totalAmounts[$code];
  1165. }
  1166. return 0;
  1167. }
  1168. /**
  1169. * Get total amount value by code in base store currency
  1170. *
  1171. * @param string $code
  1172. * @return float|int
  1173. */
  1174. public function getBaseTotalAmount($code)
  1175. {
  1176. if (isset($this->_baseTotalAmounts[$code])) {
  1177. return $this->_baseTotalAmounts[$code];
  1178. }
  1179. return 0;
  1180. }
  1181. /**
  1182. * Get subtotal amount with applied discount in base currency
  1183. *
  1184. * @return float
  1185. */
  1186. public function getBaseSubtotalWithDiscount()
  1187. {
  1188. return $this->getBaseSubtotal() + $this->getBaseDiscountAmount();
  1189. }
  1190. /**
  1191. * Get subtotal amount with applied discount
  1192. *
  1193. * @return float
  1194. */
  1195. public function getSubtotalWithDiscount()
  1196. {
  1197. return $this->getSubtotal() + $this->getDiscountAmount();
  1198. }
  1199. //@codeCoverageIgnoreStart
  1200. /**
  1201. * Get all total amount values
  1202. *
  1203. * @return array
  1204. */
  1205. public function getAllTotalAmounts()
  1206. {
  1207. return $this->_totalAmounts;
  1208. }
  1209. /**
  1210. * Get all total amount values in base currency
  1211. *
  1212. * @return array
  1213. */
  1214. public function getAllBaseTotalAmounts()
  1215. {
  1216. return $this->_baseTotalAmounts;
  1217. }
  1218. /******************************* End Total Collector Interface *******************************************/
  1219. /**
  1220. * @inheritdoc
  1221. */
  1222. protected function _getValidationRulesBeforeSave()
  1223. {
  1224. return $this->validator;
  1225. }
  1226. /**
  1227. * @inheritdoc
  1228. */
  1229. public function getCountryId()
  1230. {
  1231. return $this->getData(self::KEY_COUNTRY_ID);
  1232. }
  1233. /**
  1234. * @inheritdoc
  1235. */
  1236. public function setCountryId($countryId)
  1237. {
  1238. return $this->setData(self::KEY_COUNTRY_ID, $countryId);
  1239. }
  1240. /**
  1241. * @inheritdoc
  1242. */
  1243. public function getStreet()
  1244. {
  1245. $street = $this->getData(self::KEY_STREET);
  1246. return is_array($street) ? $street : explode("\n", $street);
  1247. }
  1248. /**
  1249. * @inheritdoc
  1250. */
  1251. public function setStreet($street)
  1252. {
  1253. return $this->setData(self::KEY_STREET, $street);
  1254. }
  1255. /**
  1256. * @inheritdoc
  1257. */
  1258. public function getCompany()
  1259. {
  1260. return $this->getData(self::KEY_COMPANY);
  1261. }
  1262. /**
  1263. * @inheritdoc
  1264. */
  1265. public function setCompany($company)
  1266. {
  1267. return $this->setData(self::KEY_COMPANY, $company);
  1268. }
  1269. /**
  1270. * @inheritdoc
  1271. */
  1272. public function getTelephone()
  1273. {
  1274. return $this->getData(self::KEY_TELEPHONE);
  1275. }
  1276. /**
  1277. * @inheritdoc
  1278. */
  1279. public function setTelephone($telephone)
  1280. {
  1281. return $this->setData(self::KEY_TELEPHONE, $telephone);
  1282. }
  1283. /**
  1284. * @inheritdoc
  1285. */
  1286. public function getFax()
  1287. {
  1288. return $this->getData(self::KEY_FAX);
  1289. }
  1290. /**
  1291. * @inheritdoc
  1292. */
  1293. public function setFax($fax)
  1294. {
  1295. return $this->setData(self::KEY_FAX, $fax);
  1296. }
  1297. /**
  1298. * @inheritdoc
  1299. */
  1300. public function getPostcode()
  1301. {
  1302. return $this->getData(self::KEY_POSTCODE);
  1303. }
  1304. /**
  1305. * @inheritdoc
  1306. */
  1307. public function setPostcode($postcode)
  1308. {
  1309. return $this->setData(self::KEY_POSTCODE, $postcode);
  1310. }
  1311. /**
  1312. * @inheritdoc
  1313. */
  1314. public function getCity()
  1315. {
  1316. return $this->getData(self::KEY_CITY);
  1317. }
  1318. /**
  1319. * @inheritdoc
  1320. */
  1321. public function setCity($city)
  1322. {
  1323. return $this->setData(self::KEY_CITY, $city);
  1324. }
  1325. /**
  1326. * @inheritdoc
  1327. */
  1328. public function getFirstname()
  1329. {
  1330. return $this->getData(self::KEY_FIRSTNAME);
  1331. }
  1332. /**
  1333. * @inheritdoc
  1334. */
  1335. public function setFirstname($firstname)
  1336. {
  1337. return $this->setData(self::KEY_FIRSTNAME, $firstname);
  1338. }
  1339. /**
  1340. * @inheritdoc
  1341. */
  1342. public function getLastname()
  1343. {
  1344. return $this->getData(self::KEY_LASTNAME);
  1345. }
  1346. /**
  1347. * @inheritdoc
  1348. */
  1349. public function setLastname($lastname)
  1350. {
  1351. return $this->setData(self::KEY_LASTNAME, $lastname);
  1352. }
  1353. /**
  1354. * @inheritdoc
  1355. */
  1356. public function getMiddlename()
  1357. {
  1358. return $this->getData(self::KEY_MIDDLENAME);
  1359. }
  1360. /**
  1361. * @inheritdoc
  1362. */
  1363. public function setMiddlename($middlename)
  1364. {
  1365. return $this->setData(self::KEY_MIDDLENAME, $middlename);
  1366. }
  1367. /**
  1368. * @inheritdoc
  1369. */
  1370. public function getPrefix()
  1371. {
  1372. return $this->getData(self::KEY_PREFIX);
  1373. }
  1374. /**
  1375. * @inheritdoc
  1376. */
  1377. public function setPrefix($prefix)
  1378. {
  1379. return $this->setData(self::KEY_PREFIX, $prefix);
  1380. }
  1381. /**
  1382. * @inheritdoc
  1383. */
  1384. public function getSuffix()
  1385. {
  1386. return $this->getData(self::KEY_SUFFIX);
  1387. }
  1388. /**
  1389. * @inheritdoc
  1390. */
  1391. public function setSuffix($suffix)
  1392. {
  1393. return $this->setData(self::KEY_SUFFIX, $suffix);
  1394. }
  1395. /**
  1396. * @inheritdoc
  1397. */
  1398. public function getVatId()
  1399. {
  1400. return $this->getData(self::KEY_VAT_ID);
  1401. }
  1402. /**
  1403. * @inheritdoc
  1404. */
  1405. public function setVatId($vatId)
  1406. {
  1407. return $this->setData(self::KEY_VAT_ID, $vatId);
  1408. }
  1409. /**
  1410. * @inheritdoc
  1411. */
  1412. public function getCustomerId()
  1413. {
  1414. return $this->getData(self::KEY_CUSTOMER_ID);
  1415. }
  1416. /**
  1417. * @inheritdoc
  1418. */
  1419. public function setCustomerId($customerId)
  1420. {
  1421. return $this->setData(self::KEY_CUSTOMER_ID, $customerId);
  1422. }
  1423. /**
  1424. * @inheritdoc
  1425. */
  1426. public function getEmail()
  1427. {
  1428. $email = $this->getData(self::KEY_EMAIL);
  1429. if (!$email && $this->getQuote()) {
  1430. $email = $this->getQuote()->getCustomerEmail();
  1431. $this->setEmail($email);
  1432. }
  1433. return $email;
  1434. }
  1435. /**
  1436. * @inheritdoc
  1437. */
  1438. public function setEmail($email)
  1439. {
  1440. return $this->setData(self::KEY_EMAIL, $email);
  1441. }
  1442. /**
  1443. * @inheritdoc
  1444. */
  1445. public function setRegion($region)
  1446. {
  1447. return $this->setData(self::KEY_REGION, $region);
  1448. }
  1449. /**
  1450. * @inheritdoc
  1451. */
  1452. public function setRegionId($regionId)
  1453. {
  1454. return $this->setData(self::KEY_REGION_ID, $regionId);
  1455. }
  1456. /**
  1457. * @inheritdoc
  1458. */
  1459. public function setRegionCode($regionCode)
  1460. {
  1461. return $this->setData(self::KEY_REGION_CODE, $regionCode);
  1462. }
  1463. /**
  1464. * @inheritdoc
  1465. */
  1466. public function getSameAsBilling()
  1467. {
  1468. return $this->getData(self::SAME_AS_BILLING);
  1469. }
  1470. /**
  1471. * @inheritdoc
  1472. */
  1473. public function setSameAsBilling($sameAsBilling)
  1474. {
  1475. return $this->setData(self::SAME_AS_BILLING, $sameAsBilling);
  1476. }
  1477. /**
  1478. * @inheritdoc
  1479. */
  1480. public function getCustomerAddressId()
  1481. {
  1482. return $this->getData(self::CUSTOMER_ADDRESS_ID);
  1483. }
  1484. /**
  1485. * @inheritdoc
  1486. */
  1487. public function setCustomerAddressId($customerAddressId)
  1488. {
  1489. return $this->setData(self::CUSTOMER_ADDRESS_ID, $customerAddressId);
  1490. }
  1491. /**
  1492. * Get save in address book flag
  1493. *
  1494. * @return int|null
  1495. */
  1496. public function getSaveInAddressBook()
  1497. {
  1498. return $this->getData(self::SAVE_IN_ADDRESS_BOOK);
  1499. }
  1500. /**
  1501. * Set save in address book flag
  1502. *
  1503. * @param int|null $saveInAddressBook
  1504. * @return $this
  1505. */
  1506. public function setSaveInAddressBook($saveInAddressBook)
  1507. {
  1508. return $this->setData(self::SAVE_IN_ADDRESS_BOOK, $saveInAddressBook);
  1509. }
  1510. //@codeCoverageIgnoreEnd
  1511. /**
  1512. * @inheritdoc
  1513. *
  1514. * @return \Magento\Quote\Api\Data\AddressExtensionInterface|null
  1515. */
  1516. public function getExtensionAttributes()
  1517. {
  1518. return $this->_getExtensionAttributes();
  1519. }
  1520. /**
  1521. * @inheritdoc
  1522. *
  1523. * @param \Magento\Quote\Api\Data\AddressExtensionInterface $extensionAttributes
  1524. * @return $this
  1525. */
  1526. public function setExtensionAttributes(\Magento\Quote\Api\Data\AddressExtensionInterface $extensionAttributes)
  1527. {
  1528. return $this->_setExtensionAttributes($extensionAttributes);
  1529. }
  1530. /**
  1531. * Shipping method
  1532. *
  1533. * @return string
  1534. */
  1535. public function getShippingMethod()
  1536. {
  1537. return $this->getData('shipping_method');
  1538. }
  1539. /**
  1540. * @inheritdoc
  1541. */
  1542. protected function getCustomAttributesCodes()
  1543. {
  1544. return array_keys($this->attributeList->getAttributes());
  1545. }
  1546. }