Advertisement
Guest User

addres

a guest
Dec 15th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.98 KB | None | 0 0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26.  
  27.  
  28. /**
  29. * Sales Quote address model
  30. *
  31. * @method Mage_Sales_Model_Resource_Quote_Address _getResource()
  32. * @method Mage_Sales_Model_Resource_Quote_Address getResource()
  33. * @method int getQuoteId()
  34. * @method Mage_Sales_Model_Quote_Address setQuoteId(int $value)
  35. * @method string getCreatedAt()
  36. * @method Mage_Sales_Model_Quote_Address setCreatedAt(string $value)
  37. * @method string getUpdatedAt()
  38. * @method Mage_Sales_Model_Quote_Address setUpdatedAt(string $value)
  39. * @method int getCustomerId()
  40. * @method Mage_Sales_Model_Quote_Address setCustomerId(int $value)
  41. * @method int getSaveInAddressBook()
  42. * @method Mage_Sales_Model_Quote_Address setSaveInAddressBook(int $value)
  43. * @method int getCustomerAddressId()
  44. * @method Mage_Sales_Model_Quote_Address setCustomerAddressId(int $value)
  45. * @method string getAddressType()
  46. * @method Mage_Sales_Model_Quote_Address setAddressType(string $value)
  47. * @method string getEmail()
  48. * @method Mage_Sales_Model_Quote_Address setEmail(string $value)
  49. * @method string getPrefix()
  50. * @method Mage_Sales_Model_Quote_Address setPrefix(string $value)
  51. * @method string getFirstname()
  52. * @method Mage_Sales_Model_Quote_Address setFirstname(string $value)
  53. * @method string getMiddlename()
  54. * @method Mage_Sales_Model_Quote_Address setMiddlename(string $value)
  55. * @method string getLastname()
  56. * @method Mage_Sales_Model_Quote_Address setLastname(string $value)
  57. * @method string getSuffix()
  58. * @method Mage_Sales_Model_Quote_Address setSuffix(string $value)
  59. * @method string getCompany()
  60. * @method Mage_Sales_Model_Quote_Address setCompany(string $value)
  61. * @method string getCity()
  62. * @method Mage_Sales_Model_Quote_Address setCity(string $value)
  63. * @method Mage_Sales_Model_Quote_Address setRegion(string $value)
  64. * @method Mage_Sales_Model_Quote_Address setRegionId(int $value)
  65. * @method string getPostcode()
  66. * @method Mage_Sales_Model_Quote_Address setPostcode(string $value)
  67. * @method string getCountryId()
  68. * @method Mage_Sales_Model_Quote_Address setCountryId(string $value)
  69. * @method string getTelephone()
  70. * @method Mage_Sales_Model_Quote_Address setTelephone(string $value)
  71. * @method string getFax()
  72. * @method Mage_Sales_Model_Quote_Address setFax(string $value)
  73. * @method int getSameAsBilling()
  74. * @method Mage_Sales_Model_Quote_Address setSameAsBilling(int $value)
  75. * @method int getFreeShipping()
  76. * @method Mage_Sales_Model_Quote_Address setFreeShipping(int $value)
  77. * @method int getCollectShippingRates()
  78. * @method Mage_Sales_Model_Quote_Address setCollectShippingRates(int $value)
  79. * @method string getShippingMethod()
  80. * @method Mage_Sales_Model_Quote_Address setShippingMethod(string $value)
  81. * @method string getShippingDescription()
  82. * @method Mage_Sales_Model_Quote_Address setShippingDescription(string $value)
  83. * @method float getWeight()
  84. * @method Mage_Sales_Model_Quote_Address setWeight(float $value)
  85. * @method float getSubtotal()
  86. * @method Mage_Sales_Model_Quote_Address setSubtotal(float $value)
  87. * @method float getBaseSubtotal()
  88. * @method Mage_Sales_Model_Quote_Address setBaseSubtotal(float $value)
  89. * @method Mage_Sales_Model_Quote_Address setSubtotalWithDiscount(float $value)
  90. * @method Mage_Sales_Model_Quote_Address setBaseSubtotalWithDiscount(float $value)
  91. * @method float getTaxAmount()
  92. * @method Mage_Sales_Model_Quote_Address setTaxAmount(float $value)
  93. * @method float getBaseTaxAmount()
  94. * @method Mage_Sales_Model_Quote_Address setBaseTaxAmount(float $value)
  95. * @method float getShippingAmount()
  96. * @method float getBaseShippingAmount()
  97. * @method float getShippingTaxAmount()
  98. * @method Mage_Sales_Model_Quote_Address setShippingTaxAmount(float $value)
  99. * @method float getBaseShippingTaxAmount()
  100. * @method Mage_Sales_Model_Quote_Address setBaseShippingTaxAmount(float $value)
  101. * @method float getDiscountAmount()
  102. * @method Mage_Sales_Model_Quote_Address setDiscountAmount(float $value)
  103. * @method float getBaseDiscountAmount()
  104. * @method Mage_Sales_Model_Quote_Address setBaseDiscountAmount(float $value)
  105. * @method float getGrandTotal()
  106. * @method Mage_Sales_Model_Quote_Address setGrandTotal(float $value)
  107. * @method float getBaseGrandTotal()
  108. * @method Mage_Sales_Model_Quote_Address setBaseGrandTotal(float $value)
  109. * @method string getCustomerNotes()
  110. * @method Mage_Sales_Model_Quote_Address setCustomerNotes(string $value)
  111. * @method string getDiscountDescription()
  112. * @method Mage_Sales_Model_Quote_Address setDiscountDescription(string $value)
  113. * @method null|array getDiscountDescriptionArray()
  114. * @method Mage_Sales_Model_Quote_Address setDiscountDescriptionArray(array $value)
  115. * @method float getShippingDiscountAmount()
  116. * @method Mage_Sales_Model_Quote_Address setShippingDiscountAmount(float $value)
  117. * @method float getBaseShippingDiscountAmount()
  118. * @method Mage_Sales_Model_Quote_Address setBaseShippingDiscountAmount(float $value)
  119. * @method float getSubtotalInclTax()
  120. * @method Mage_Sales_Model_Quote_Address setSubtotalInclTax(float $value)
  121. * @method float getBaseSubtotalTotalInclTax()
  122. * @method Mage_Sales_Model_Quote_Address setBaseSubtotalTotalInclTax(float $value)
  123. * @method int getGiftMessageId()
  124. * @method Mage_Sales_Model_Quote_Address setGiftMessageId(int $value)
  125. * @method float getHiddenTaxAmount()
  126. * @method Mage_Sales_Model_Quote_Address setHiddenTaxAmount(float $value)
  127. * @method float getBaseHiddenTaxAmount()
  128. * @method Mage_Sales_Model_Quote_Address setBaseHiddenTaxAmount(float $value)
  129. * @method float getShippingHiddenTaxAmount()
  130. * @method Mage_Sales_Model_Quote_Address setShippingHiddenTaxAmount(float $value)
  131. * @method float getBaseShippingHiddenTaxAmount()
  132. * @method Mage_Sales_Model_Quote_Address setBaseShippingHiddenTaxAmount(float $value)
  133. * @method float getShippingInclTax()
  134. * @method Mage_Sales_Model_Quote_Address setShippingInclTax(float $value)
  135. * @method float getBaseShippingInclTax()
  136. * @method Mage_Sales_Model_Quote_Address setBaseShippingInclTax(float $value)
  137. *
  138. * @category Mage
  139. * @package Mage_Sales
  140. * @author Magento Core Team <core@magentocommerce.com>
  141. */
  142. class Mage_Sales_Model_Quote_Address extends Mage_Customer_Model_Address_Abstract
  143. {
  144. /**
  145. * Default value for Destination street
  146. */
  147. const DEFAULT_DEST_STREET = -1;
  148.  
  149. /**
  150. * Prefix of model events
  151. *
  152. * @var string
  153. */
  154. protected $_eventPrefix = 'sales_quote_address';
  155.  
  156. /**
  157. * Name of event object
  158. *
  159. * @var string
  160. */
  161. protected $_eventObject = 'quote_address';
  162.  
  163. /**
  164. * Quote object
  165. *
  166. * @var Mage_Sales_Model_Quote
  167. */
  168. protected $_items = null;
  169.  
  170. /**
  171. * Quote object
  172. *
  173. * @var Mage_Sales_Model_Quote
  174. */
  175. protected $_quote = null;
  176.  
  177. /**
  178. * Sales Quote address rates
  179. *
  180. * @var Mage_Sales_Model_Quote_Address_Rate
  181. */
  182. protected $_rates = null;
  183.  
  184. /**
  185. * Total models collector
  186. *
  187. * @var Mage_Sales_Model_Quote_Address_Total_Collector
  188. */
  189. protected $_totalCollector = null;
  190.  
  191. /**
  192. * Total data as array
  193. *
  194. * @var array
  195. */
  196. protected $_totals = array();
  197.  
  198. /**
  199. * Total amounts
  200. *
  201. * @var array
  202. */
  203. protected $_totalAmounts = array();
  204.  
  205. /**
  206. * Total base amounts
  207. *
  208. * @var array
  209. */
  210. protected $_baseTotalAmounts = array();
  211.  
  212. /**
  213. * Whether to segregate by nominal items only
  214. *
  215. * @var bool
  216. */
  217. protected $_nominalOnly = null;
  218.  
  219. /**
  220. * Initialize resource
  221. */
  222. protected function _construct()
  223. {
  224. $this->_init('sales/quote_address');
  225. }
  226.  
  227. /**
  228. * Init mapping array of short fields to its full names
  229. *
  230. * @return Mage_Sales_Model_Quote_Address
  231. */
  232. protected function _initOldFieldsMap()
  233. {
  234. $this->_oldFieldsMap = Mage::helper('sales')->getOldFieldMap('quote_address');
  235. return $this;
  236. }
  237.  
  238. /**
  239. * Initialize Quote identifier before save
  240. *
  241. * @return Mage_Sales_Model_Quote_Address
  242. */
  243. protected function _beforeSave()
  244. {
  245. parent::_beforeSave();
  246. $this->_populateBeforeSaveData();
  247. return $this;
  248. }
  249.  
  250. /**
  251. * Set the required fields
  252. */
  253. protected function _populateBeforeSaveData()
  254. {
  255. if ($this->getQuote()) {
  256. $this->_dataSaveAllowed = (bool)$this->getQuote()->getId();
  257.  
  258. if ($this->getQuote()->getId()) {
  259. $this->setQuoteId($this->getQuote()->getId());
  260. }
  261. $this->setCustomerId($this->getQuote()->getCustomerId());
  262.  
  263. /**
  264. * Init customer address id if customer address is assigned
  265. */
  266. if ($this->getCustomerAddress()) {
  267. $this->setCustomerAddressId($this->getCustomerAddress()->getId());
  268. }
  269.  
  270. /**
  271. * Set same_as_billing to "1" when default shipping address is set as default
  272. * and it is not equal billing address
  273. */
  274. if (!$this->getId()) {
  275. $this->setSameAsBilling((int)$this->_isSameAsBilling());
  276. }
  277. }
  278. }
  279.  
  280. /**
  281. * Returns true if the billing address is same as the shipping
  282. *
  283. * @return bool
  284. */
  285. protected function _isSameAsBilling()
  286. {
  287. return ($this->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_SHIPPING
  288. && ($this->_isNotRegisteredCustomer() || $this->_isDefaultShippingNullOrSameAsBillingAddress()));
  289. }
  290.  
  291. /**
  292. * Checks if the user is a registered customer
  293. *
  294. * @return bool
  295. */
  296. protected function _isNotRegisteredCustomer()
  297. {
  298. return !$this->getQuote()->getCustomerId() || $this->getCustomerAddressId() === null;
  299. }
  300.  
  301. /**
  302. * Returns true if the def billing address is same as customer address
  303. *
  304. * @return bool
  305. */
  306. protected function _isDefaultShippingNullOrSameAsBillingAddress()
  307. {
  308. $customer = $this->getQuote()->getCustomer();
  309. return !$customer->getDefaultShippingAddress()
  310. || $customer->getDefaultBillingAddress() && $customer->getDefaultShippingAddress()
  311. && $customer->getDefaultBillingAddress()->getId() == $customer->getDefaultShippingAddress()->getId();
  312. }
  313.  
  314. /**
  315. * Save child collections
  316. *
  317. * @return Mage_Sales_Model_Quote_Address
  318. */
  319. protected function _afterSave()
  320. {
  321. parent::_afterSave();
  322. if (null !== $this->_items) {
  323. $this->getItemsCollection()->save();
  324. }
  325. if (null !== $this->_rates) {
  326. $this->getShippingRatesCollection()->save();
  327. }
  328. return $this;
  329. }
  330.  
  331. /**
  332. * Declare adress quote model object
  333. *
  334. * @param Mage_Sales_Model_Quote $quote
  335. * @return Mage_Sales_Model_Quote_Address
  336. */
  337. public function setQuote(Mage_Sales_Model_Quote $quote)
  338. {
  339. $this->_quote = $quote;
  340. $this->setQuoteId($quote->getId());
  341. return $this;
  342. }
  343.  
  344. /**
  345. * Retrieve quote object
  346. *
  347. * @return Mage_Sales_Model_Quote
  348. */
  349. public function getQuote()
  350. {
  351. return $this->_quote;
  352. }
  353.  
  354. /**
  355. * Import quote address data from customer address object
  356. *
  357. * @param Mage_Customer_Model_Address $address
  358. * @return Mage_Sales_Model_Quote_Address
  359. */
  360. public function importCustomerAddress(Mage_Customer_Model_Address $address)
  361. {
  362. Mage::helper('core')->copyFieldset('customer_address', 'to_quote_address', $address, $this);
  363. $email = null;
  364. if ($address->hasEmail()) {
  365. $email = $address->getEmail();
  366. } elseif ($address->getCustomer()) {
  367. $email = $address->getCustomer()->getEmail();
  368. }
  369. if ($email) {
  370. $this->setEmail($email);
  371. }
  372. return $this;
  373. }
  374.  
  375. /**
  376. * Export data to customer address object
  377. *
  378. * @return Mage_Customer_Model_Address
  379. */
  380. public function exportCustomerAddress()
  381. {
  382. $address = Mage::getModel('customer/address');
  383. Mage::helper('core')->copyFieldset('sales_convert_quote_address', 'to_customer_address', $this, $address);
  384. return $address;
  385. }
  386.  
  387. /**
  388. * Import address data from order address
  389. *
  390. * @param Mage_Sales_Model_Order_Address $address
  391. * @return Mage_Sales_Model_Quote_Address
  392. */
  393. public function importOrderAddress(Mage_Sales_Model_Order_Address $address)
  394. {
  395. $this->setAddressType($address->getAddressType())
  396. ->setCustomerId($address->getCustomerId())
  397. ->setCustomerAddressId($address->getCustomerAddressId())
  398. ->setEmail($address->getEmail());
  399.  
  400. Mage::helper('core')->copyFieldset('sales_convert_order_address', 'to_quote_address', $address, $this);
  401.  
  402. return $this;
  403. }
  404.  
  405. /**
  406. * Convert object to array
  407. *
  408. * @param array $arrAttributes
  409. * @return array
  410. */
  411. public function toArray(array $arrAttributes = array())
  412. {
  413. $arr = parent::toArray($arrAttributes);
  414. $arr['rates'] = $this->getShippingRatesCollection()->toArray($arrAttributes);
  415. $arr['items'] = $this->getItemsCollection()->toArray($arrAttributes);
  416. foreach ($this->getTotals() as $k => $total) {
  417. $arr['totals'][$k] = $total->toArray();
  418. }
  419. return $arr;
  420. }
  421.  
  422. /**
  423. * Retrieve address items collection
  424. *
  425. * @return Mage_Eav_Model_Entity_Collection_Abstract
  426. */
  427. public function getItemsCollection()
  428. {
  429. if (is_null($this->_items)) {
  430. $this->_items = Mage::getModel('sales/quote_address_item')->getCollection()
  431. ->setAddressFilter($this->getId());
  432.  
  433. if ($this->getId()) {
  434. foreach ($this->_items as $item) {
  435. $item->setAddress($this);
  436. }
  437. }
  438. }
  439. return $this->_items;
  440. }
  441.  
  442. /**
  443. * Get all available address items
  444. *
  445. * @return array
  446. */
  447. public function getAllItems()
  448. {
  449. // We calculate item list once and cache it in three arrays - all items, nominal, non-nominal
  450. $cachedItems = $this->_nominalOnly ? 'nominal' : ($this->_nominalOnly === false ? 'nonnominal' : 'all');
  451. $key = 'cached_items_' . $cachedItems;
  452. if (!$this->hasData($key)) {
  453. // For compatibility we will use $this->_filterNominal to divide nominal items from non-nominal
  454. // (because it can be overloaded)
  455. // So keep current flag $this->_nominalOnly and restore it after cycle
  456. $wasNominal = $this->_nominalOnly;
  457. $this->_nominalOnly = true; // Now $this->_filterNominal() will return positive values for nominal items
  458.  
  459. $quoteItems = $this->getQuote()->getItemsCollection();
  460. $addressItems = $this->getItemsCollection();
  461.  
  462. $items = array();
  463. $nominalItems = array();
  464. $nonNominalItems = array();
  465. if ($this->getQuote()->getIsMultiShipping() && $addressItems->count() > 0) {
  466. foreach ($addressItems as $aItem) {
  467. if ($aItem->isDeleted()) {
  468. continue;
  469. }
  470.  
  471. if (!$aItem->getQuoteItemImported()) {
  472. $qItem = $this->getQuote()->getItemById($aItem->getQuoteItemId());
  473. if ($qItem) {
  474. $aItem->importQuoteItem($qItem);
  475. }
  476. }
  477. $items[] = $aItem;
  478. if ($this->_filterNominal($aItem)) {
  479. $nominalItems[] = $aItem;
  480. } else {
  481. $nonNominalItems[] = $aItem;
  482. }
  483. }
  484. } else {
  485. /*
  486. * For virtual quote we assign items only to billing address, otherwise - only to shipping address
  487. */
  488. $addressType = $this->getAddressType();
  489. $canAddItems = $this->getQuote()->isVirtual()
  490. ? ($addressType == self::TYPE_BILLING)
  491. : ($addressType == self::TYPE_SHIPPING);
  492.  
  493. if ($canAddItems) {
  494. foreach ($quoteItems as $qItem) {
  495. if ($qItem->isDeleted()) {
  496. continue;
  497. }
  498. $items[] = $qItem;
  499. if ($this->_filterNominal($qItem)) {
  500. $nominalItems[] = $qItem;
  501. } else {
  502. $nonNominalItems[] = $qItem;
  503. }
  504. }
  505. }
  506. }
  507.  
  508. // Cache calculated lists
  509. $this->setData('cached_items_all', $items);
  510. $this->setData('cached_items_nominal', $nominalItems);
  511. $this->setData('cached_items_nonnominal', $nonNominalItems);
  512.  
  513. $this->_nominalOnly = $wasNominal; // Restore original value before we changed it
  514. }
  515.  
  516. $items = $this->getData($key);
  517. return $items;
  518. }
  519.  
  520. /**
  521. * Getter for all non-nominal items
  522. *
  523. * @return array
  524. */
  525. public function getAllNonNominalItems()
  526. {
  527. $this->_nominalOnly = false;
  528. $result = $this->getAllItems();
  529. $this->_nominalOnly = null;
  530. return $result;
  531. }
  532.  
  533. /**
  534. * Getter for all nominal items
  535. *
  536. * @return array
  537. */
  538. public function getAllNominalItems()
  539. {
  540. $this->_nominalOnly = true;
  541. $result = $this->getAllItems();
  542. $this->_nominalOnly = null;
  543. return $result;
  544. }
  545.  
  546. /**
  547. * Segregate by nominal criteria
  548. *
  549. * true: get nominals only
  550. * false: get non-nominals only
  551. * null: get all
  552. *
  553. * @param Mage_Sales_Model_Quote_Item_Abstract
  554. * @return Mage_Sales_Model_Quote_Item_Abstract|false
  555. */
  556. protected function _filterNominal($item)
  557. {
  558. return (null === $this->_nominalOnly)
  559. || ((false === $this->_nominalOnly) && !$item->isNominal())
  560. || ((true === $this->_nominalOnly) && $item->isNominal())
  561. ? $item : false;
  562. }
  563.  
  564. /**
  565. * Retrieve all visible items
  566. *
  567. * @return array
  568. */
  569. public function getAllVisibleItems()
  570. {
  571. $items = array();
  572. foreach ($this->getAllItems() as $item) {
  573. if (!$item->getParentItemId()) {
  574. $items[] = $item;
  575. }
  576. }
  577. return $items;
  578. }
  579.  
  580. /**
  581. * Retrieve item quantity by id
  582. *
  583. * @param int $itemId
  584. * @return float|int
  585. */
  586. public function getItemQty($itemId = 0)
  587. {
  588. if ($this->hasData('item_qty')) {
  589. return $this->getData('item_qty');
  590. }
  591.  
  592. $qty = 0;
  593. if ($itemId == 0) {
  594. foreach ($this->getAllItems() as $item) {
  595. $qty += $item->getQty();
  596. }
  597. } else {
  598. $item = $this->getItemById($itemId);
  599. if ($item) {
  600. $qty = $item->getQty();
  601. }
  602. }
  603. return $qty;
  604. }
  605.  
  606. /**
  607. * Check Quote address has Items
  608. *
  609. * @return bool
  610. */
  611. public function hasItems()
  612. {
  613. return sizeof($this->getAllItems())>0;
  614. }
  615.  
  616. /**
  617. * Get address item object by id without
  618. *
  619. * @param int $itemId
  620. * @return Mage_Sales_Model_Quote_Address_Item
  621. */
  622. public function getItemById($itemId)
  623. {
  624. foreach ($this->getItemsCollection() as $item) {
  625. if ($item->getId() == $itemId) {
  626. return $item;
  627. }
  628. }
  629. return false;
  630. }
  631.  
  632. /**
  633. * Get prepared not deleted item
  634. *
  635. * @param $itemId
  636. * @return Mage_Sales_Model_Quote_Address_Item
  637. */
  638. public function getValidItemById($itemId)
  639. {
  640. foreach ($this->getAllItems() as $item) {
  641. if ($item->getId() == $itemId) {
  642. return $item;
  643. }
  644. }
  645. return false;
  646. }
  647.  
  648. /**
  649. * Retrieve item object by quote item Id
  650. *
  651. * @param int $itemId
  652. * @return Mage_Sales_Model_Quote_Address_Item
  653. */
  654. public function getItemByQuoteItemId($itemId)
  655. {
  656. foreach ($this->getItemsCollection() as $item) {
  657. if ($item->getQuoteItemId() == $itemId) {
  658. return $item;
  659. }
  660. }
  661. return false;
  662. }
  663.  
  664. /**
  665. * Remove item from collection
  666. *
  667. * @param int $itemId
  668. * @return Mage_Sales_Model_Quote_Address
  669. */
  670. public function removeItem($itemId)
  671. {
  672. $item = $this->getItemById($itemId);
  673. if ($item) {
  674. $item->isDeleted(true);
  675. }
  676. return $this;
  677. }
  678.  
  679. /**
  680. * Add item to address
  681. *
  682. * @param Mage_Sales_Model_Quote_Item_Abstract $item
  683. * @param int $qty
  684. * @return Mage_Sales_Model_Quote_Address
  685. */
  686. public function addItem(Mage_Sales_Model_Quote_Item_Abstract $item, $qty = null)
  687. {
  688. if ($item instanceof Mage_Sales_Model_Quote_Item) {
  689. if ($item->getParentItemId()) {
  690. return $this;
  691. }
  692. $addressItem = Mage::getModel('sales/quote_address_item')
  693. ->setAddress($this)
  694. ->importQuoteItem($item);
  695. $this->getItemsCollection()->addItem($addressItem);
  696.  
  697. if ($item->getHasChildren()) {
  698. foreach ($item->getChildren() as $child) {
  699. $addressChildItem = Mage::getModel('sales/quote_address_item')
  700. ->setAddress($this)
  701. ->importQuoteItem($child)
  702. ->setParentItem($addressItem);
  703. $this->getItemsCollection()->addItem($addressChildItem);
  704. }
  705. }
  706. } else {
  707. $addressItem = $item;
  708. $addressItem->setAddress($this);
  709. if (!$addressItem->getId()) {
  710. $this->getItemsCollection()->addItem($addressItem);
  711. }
  712. }
  713.  
  714. if ($qty) {
  715. $addressItem->setQty($qty);
  716. }
  717. return $this;
  718. }
  719.  
  720. /**
  721. * Retrieve collection of quote shipping rates
  722. *
  723. * @return Mage_Eav_Model_Entity_Collection_Abstract
  724. */
  725. public function getShippingRatesCollection()
  726. {
  727. if (is_null($this->_rates)) {
  728. $this->_rates = Mage::getModel('sales/quote_address_rate')->getCollection()
  729. ->setAddressFilter($this->getId());
  730. if ($this->getQuote()->hasNominalItems(false)) {
  731. $this->_rates->setFixedOnlyFilter(true);
  732. }
  733. if ($this->getId()) {
  734. foreach ($this->_rates as $rate) {
  735. $rate->setAddress($this);
  736. }
  737. }
  738. }
  739. return $this->_rates;
  740. }
  741.  
  742. /**
  743. * Retrieve all address shipping rates
  744. *
  745. * @return array
  746. */
  747. public function getAllShippingRates()
  748. {
  749. $rates = array();
  750. foreach ($this->getShippingRatesCollection() as $rate) {
  751. if (!$rate->isDeleted()) {
  752. $rates[] = $rate;
  753. }
  754. }
  755. return $rates;
  756. }
  757.  
  758. /**
  759. * Retrieve all grouped shipping rates
  760. *
  761. * @return array
  762. */
  763. public function getGroupedAllShippingRates()
  764. {
  765. $rates = array();
  766. foreach ($this->getShippingRatesCollection() as $rate) {
  767. if (!$rate->isDeleted() && $rate->getCarrierInstance()) {
  768. if (!isset($rates[$rate->getCarrier()])) {
  769. $rates[$rate->getCarrier()] = array();
  770. }
  771.  
  772. $rates[$rate->getCarrier()][] = $rate;
  773. $rates[$rate->getCarrier()][0]->carrier_sort_order = $rate->getCarrierInstance()->getSortOrder();
  774. }
  775. }
  776. uasort($rates, array($this, '_sortRates'));
  777. return $rates;
  778. }
  779.  
  780. /**
  781. * Sort rates recursive callback
  782. *
  783. * @param array $a
  784. * @param array $b
  785. * @return int
  786. */
  787. protected function _sortRates($a, $b)
  788. {
  789. if ((int)$a[0]->carrier_sort_order < (int)$b[0]->carrier_sort_order) {
  790. return -1;
  791. } elseif ((int)$a[0]->carrier_sort_order > (int)$b[0]->carrier_sort_order) {
  792. return 1;
  793. } else {
  794. return 0;
  795. }
  796. }
  797.  
  798. /**
  799. * Retrieve shipping rate by identifier
  800. *
  801. * @param int $rateId
  802. * @return Mage_Sales_Model_Quote_Address_Rate | false
  803. */
  804. public function getShippingRateById($rateId)
  805. {
  806. foreach ($this->getShippingRatesCollection() as $rate) {
  807. if ($rate->getId() == $rateId) {
  808. return $rate;
  809. }
  810. }
  811. return false;
  812. }
  813.  
  814. /**
  815. * Retrieve shipping rate by code
  816. *
  817. * @param string $code
  818. * @return Mage_Sales_Model_Quote_Address_Rate
  819. */
  820. public function getShippingRateByCode($code)
  821. {
  822. foreach ($this->getShippingRatesCollection() as $rate) {
  823. if ($rate->getCode() == $code) {
  824. return $rate;
  825. }
  826. }
  827. return false;
  828. }
  829.  
  830. /**
  831. * Mark all shipping rates as deleted
  832. *
  833. * @return Mage_Sales_Model_Quote_Address
  834. */
  835. public function removeAllShippingRates()
  836. {
  837. foreach ($this->getShippingRatesCollection() as $rate) {
  838. $rate->isDeleted(true);
  839. }
  840. return $this;
  841. }
  842.  
  843. /**
  844. * Add shipping rate
  845. *
  846. * @param Mage_Sales_Model_Quote_Address_Rate $rate
  847. * @return Mage_Sales_Model_Quote_Address
  848. */
  849. public function addShippingRate(Mage_Sales_Model_Quote_Address_Rate $rate)
  850. {
  851. $rate->setAddress($this);
  852. $this->getShippingRatesCollection()->addItem($rate);
  853. return $this;
  854. }
  855.  
  856. /**
  857. * Collecting shipping rates by address
  858. *
  859. * @return Mage_Sales_Model_Quote_Address
  860. */
  861. public function collectShippingRates()
  862. {
  863. if (!$this->getCollectShippingRates()) {
  864. return $this;
  865. }
  866.  
  867. $this->setCollectShippingRates(false);
  868.  
  869. $this->removeAllShippingRates();
  870.  
  871. if (!$this->getCountryId()) {
  872. return $this;
  873. }
  874.  
  875. $found = $this->requestShippingRates();
  876. if (!$found) {
  877. $this->setShippingAmount(0)
  878. ->setBaseShippingAmount(0)
  879. ->setShippingMethod('')
  880. ->setShippingDescription('');
  881. }
  882.  
  883. return $this;
  884. }
  885.  
  886. /**
  887. * Request shipping rates for entire address or specified address item
  888. * Returns true if current selected shipping method code corresponds to one of the found rates
  889. *
  890. * @param Mage_Sales_Model_Quote_Item_Abstract $item
  891. * @return bool
  892. */
  893. public function requestShippingRates(Mage_Sales_Model_Quote_Item_Abstract $item = null)
  894. {
  895. /** @var $request Mage_Shipping_Model_Rate_Request */
  896. $request = Mage::getModel('shipping/rate_request');
  897. $request->setAllItems($item ? array($item) : $this->getAllItems());
  898. $request->setDestCountryId($this->getCountryId());
  899. $request->setDestRegionId($this->getRegionId());
  900. $request->setDestRegionCode($this->getRegionCode());
  901. /**
  902. * need to call getStreet with -1
  903. * to get data in string instead of array
  904. */
  905. $request->setDestStreet($this->getStreet(self::DEFAULT_DEST_STREET));
  906. $request->setDestCity($this->getCity());
  907. $request->setDestPostcode($this->getPostcode());
  908. $request->setPackageValue($item ? $item->getBaseRowTotal() : $this->getBaseSubtotal());
  909. $packageValueWithDiscount = $item
  910. ? $item->getBaseRowTotal() - $item->getBaseDiscountAmount()
  911. : $this->getBaseSubtotalWithDiscount();
  912. $request->setPackageValueWithDiscount($packageValueWithDiscount);
  913. $request->setPackageWeight($item ? $item->getRowWeight() : $this->getWeight());
  914. $request->setPackageQty($item ? $item->getQty() : $this->getItemQty());
  915.  
  916. /**
  917. * Need for shipping methods that use insurance based on price of physical products
  918. */
  919. $packagePhysicalValue = $item
  920. ? $item->getBaseRowTotal()
  921. : $this->getBaseSubtotal() - $this->getBaseVirtualAmount();
  922. $request->setPackagePhysicalValue($packagePhysicalValue);
  923.  
  924. $request->setFreeMethodWeight($item ? 0 : $this->getFreeMethodWeight());
  925.  
  926. /**
  927. * Store and website identifiers need specify from quote
  928. */
  929. /*$request->setStoreId(Mage::app()->getStore()->getId());
  930. $request->setWebsiteId(Mage::app()->getStore()->getWebsiteId());*/
  931.  
  932. $request->setStoreId($this->getQuote()->getStore()->getId());
  933. $request->setWebsiteId($this->getQuote()->getStore()->getWebsiteId());
  934. $request->setFreeShipping($this->getFreeShipping());
  935. /**
  936. * Currencies need to convert in free shipping
  937. */
  938. $request->setBaseCurrency($this->getQuote()->getStore()->getBaseCurrency());
  939. $request->setPackageCurrency($this->getQuote()->getStore()->getCurrentCurrency());
  940. $request->setLimitCarrier($this->getLimitCarrier());
  941.  
  942. $request->setBaseSubtotalInclTax($this->getBaseSubtotalInclTax() + $this->getBaseExtraTaxAmount());
  943.  
  944. $result = Mage::getModel('shipping/shipping')->collectRates($request)->getResult();
  945.  
  946. $found = false;
  947. if ($result) {
  948. $shippingRates = $result->getAllRates();
  949.  
  950. foreach ($shippingRates as $shippingRate) {
  951. $rate = Mage::getModel('sales/quote_address_rate')
  952. ->importShippingRate($shippingRate);
  953. if (!$item) {
  954. $this->addShippingRate($rate);
  955. }
  956.  
  957. if ($this->getShippingMethod() == $rate->getCode()) {
  958. if ($item) {
  959. $item->setBaseShippingAmount($rate->getPrice());
  960. } else {
  961. /**
  962. * possible bug: this should be setBaseShippingAmount(),
  963. * see Mage_Sales_Model_Quote_Address_Total_Shipping::collect()
  964. * where this value is set again from the current specified rate price
  965. * (looks like a workaround for this bug)
  966. */
  967. $this->setShippingAmount($rate->getPrice());
  968. }
  969.  
  970. $found = true;
  971. }
  972. }
  973. }
  974. return $found;
  975. }
  976.  
  977. /**
  978. * Get totals collector model
  979. *
  980. * @return Mage_Sales_Model_Quote_Address_Total_Collector
  981. */
  982. public function getTotalCollector()
  983. {
  984. if ($this->_totalCollector === null) {
  985. $this->_totalCollector = Mage::getSingleton(
  986. 'sales/quote_address_total_collector',
  987. array('store' => $this->getQuote()->getStore())
  988. );
  989. }
  990. return $this->_totalCollector;
  991. }
  992.  
  993. /**
  994. * Retrieve total models
  995. *
  996. * @deprecated
  997. * @return array
  998. */
  999. public function getTotalModels()
  1000. {
  1001. return $this->getTotalCollector()->getRetrievers();
  1002. }
  1003.  
  1004. /**
  1005. * Collect address totals
  1006. *
  1007. * @return Mage_Sales_Model_Quote_Address
  1008. */
  1009. public function collectTotals()
  1010. {
  1011. Mage::dispatchEvent($this->_eventPrefix . '_collect_totals_before', array($this->_eventObject => $this));
  1012. foreach ($this->getTotalCollector()->getCollectors() as $model) {
  1013. $model->collect($this);
  1014. }
  1015. Mage::dispatchEvent($this->_eventPrefix . '_collect_totals_after', array($this->_eventObject => $this));
  1016. return $this;
  1017. }
  1018.  
  1019. /**
  1020. * Get address totals as array
  1021. *
  1022. * @return array
  1023. */
  1024. public function getTotals()
  1025. {
  1026. foreach ($this->getTotalCollector()->getRetrievers() as $model) {
  1027. $model->fetch($this);
  1028. }
  1029. return $this->_totals;
  1030. }
  1031.  
  1032. /**
  1033. * Add total data or model
  1034. *
  1035. * @param Mage_Sales_Model_Quote_Total|array $total
  1036. * @return Mage_Sales_Model_Quote_Address
  1037. */
  1038. public function addTotal($total)
  1039. {
  1040. if (is_array($total)) {
  1041. $totalInstance = Mage::getModel('sales/quote_address_total')
  1042. ->setData($total);
  1043. } elseif ($total instanceof Mage_Sales_Model_Quote_Total) {
  1044. $totalInstance = $total;
  1045. }
  1046. $totalInstance->setAddress($this);
  1047. $this->_totals[$totalInstance->getCode()] = $totalInstance;
  1048. return $this;
  1049. }
  1050.  
  1051. /**
  1052. * Rewrite clone method
  1053. *
  1054. * @return Mage_Sales_Model_Quote_Address
  1055. */
  1056. public function __clone()
  1057. {
  1058. $this->setId(null);
  1059. }
  1060.  
  1061. /**
  1062. * Validate minimum amount
  1063. *
  1064. * @return bool
  1065. */
  1066. public function validateMinimumAmount()
  1067. {
  1068. $storeId = $this->getQuote()->getStoreId();
  1069. if (!Mage::getStoreConfigFlag('sales/minimum_order/active', $storeId)) {
  1070. return true;
  1071. }
  1072.  
  1073. if ($this->getQuote()->getIsVirtual() && $this->getAddressType() == self::TYPE_SHIPPING) {
  1074. return true;
  1075. } elseif (!$this->getQuote()->getIsVirtual() && $this->getAddressType() != self::TYPE_SHIPPING) {
  1076. return true;
  1077. }
  1078.  
  1079. $amount = Mage::getStoreConfig('sales/minimum_order/amount', $storeId);
  1080. if ($this->getBaseSubtotalWithDiscount() < $amount) {
  1081. return false;
  1082. }
  1083. return true;
  1084. }
  1085.  
  1086. /**
  1087. * Retrieve applied taxes
  1088. *
  1089. * @return array
  1090. */
  1091. public function getAppliedTaxes()
  1092. {
  1093. return unserialize($this->getData('applied_taxes'));
  1094. }
  1095.  
  1096. /**
  1097. * Set applied taxes
  1098. *
  1099. * @param array $data
  1100. * @return Mage_Sales_Model_Quote_Address
  1101. */
  1102. public function setAppliedTaxes($data)
  1103. {
  1104. return $this->setData('applied_taxes', serialize($data));
  1105. }
  1106.  
  1107. /**
  1108. * Set shipping amount
  1109. *
  1110. * @param float $value
  1111. * @param bool $alreadyExclTax
  1112. * @return Mage_Sales_Model_Quote_Address
  1113. */
  1114. public function setShippingAmount($value, $alreadyExclTax = false)
  1115. {
  1116. return $this->setData('shipping_amount', $value);
  1117. }
  1118.  
  1119. /**
  1120. * Set base shipping amount
  1121. *
  1122. * @param float $value
  1123. * @param bool $alreadyExclTax
  1124. * @return Mage_Sales_Model_Quote_Address
  1125. */
  1126. public function setBaseShippingAmount($value, $alreadyExclTax = false)
  1127. {
  1128. return $this->setData('base_shipping_amount', $value);
  1129. }
  1130.  
  1131. /**
  1132. * Set total amount value
  1133. *
  1134. * @param string $code
  1135. * @param float $amount
  1136. * @return Mage_Sales_Model_Quote_Address
  1137. */
  1138. public function setTotalAmount($code, $amount)
  1139. {
  1140. $this->_totalAmounts[$code] = $amount;
  1141. if ($code != 'subtotal') {
  1142. $code = $code.'_amount';
  1143. }
  1144. $this->setData($code, $amount);
  1145. return $this;
  1146. }
  1147.  
  1148. /**
  1149. * Set total amount value in base store currency
  1150. *
  1151. * @param string $code
  1152. * @param float $amount
  1153. * @return Mage_Sales_Model_Quote_Address
  1154. */
  1155. public function setBaseTotalAmount($code, $amount)
  1156. {
  1157. $this->_baseTotalAmounts[$code] = $amount;
  1158. if ($code != 'subtotal') {
  1159. $code = $code.'_amount';
  1160. }
  1161. $this->setData('base_'.$code, $amount);
  1162. return $this;
  1163. }
  1164.  
  1165. /**
  1166. * Add amount total amount value
  1167. *
  1168. * @param string $code
  1169. * @param float $amount
  1170. * @return Mage_Sales_Model_Quote_Address
  1171. */
  1172. public function addTotalAmount($code, $amount)
  1173. {
  1174. $amount = $this->getTotalAmount($code)+$amount;
  1175. $this->setTotalAmount($code, $amount);
  1176. return $this;
  1177. }
  1178.  
  1179. /**
  1180. * Add amount total amount value in base store currency
  1181. *
  1182. * @param string $code
  1183. * @param float $amount
  1184. * @return Mage_Sales_Model_Quote_Address
  1185. */
  1186. public function addBaseTotalAmount($code, $amount)
  1187. {
  1188. $amount = $this->getBaseTotalAmount($code)+$amount;
  1189. $this->setBaseTotalAmount($code, $amount);
  1190. return $this;
  1191. }
  1192.  
  1193. /**
  1194. * Get total amount value by code
  1195. *
  1196. * @param string $code
  1197. * @return float
  1198. */
  1199. public function getTotalAmount($code)
  1200. {
  1201. if (isset($this->_totalAmounts[$code])) {
  1202. return $this->_totalAmounts[$code];
  1203. }
  1204. return 0;
  1205. }
  1206.  
  1207. /**
  1208. * Get total amount value by code in base store curncy
  1209. *
  1210. * @param string $code
  1211. * @return float
  1212. */
  1213. public function getBaseTotalAmount($code)
  1214. {
  1215. if (isset($this->_baseTotalAmounts[$code])) {
  1216. return $this->_baseTotalAmounts[$code];
  1217. }
  1218. return 0;
  1219. }
  1220.  
  1221. /**
  1222. * Get all total amount values
  1223. *
  1224. * @return array
  1225. */
  1226. public function getAllTotalAmounts()
  1227. {
  1228. return $this->_totalAmounts;
  1229. }
  1230.  
  1231. /**
  1232. * Get all total amount values in base currency
  1233. *
  1234. * @return array
  1235. */
  1236. public function getAllBaseTotalAmounts()
  1237. {
  1238. return $this->_baseTotalAmounts;
  1239. }
  1240.  
  1241. /**
  1242. * Get subtotal amount with applied discount in base currency
  1243. *
  1244. * @return float
  1245. */
  1246. public function getBaseSubtotalWithDiscount()
  1247. {
  1248. return $this->getBaseSubtotal()+$this->getBaseDiscountAmount();
  1249. }
  1250.  
  1251. /**
  1252. * Get subtotal amount with applied discount
  1253. *
  1254. * @return float
  1255. */
  1256. public function getSubtotalWithDiscount()
  1257. {
  1258. return $this->getSubtotal()+$this->getDiscountAmount();
  1259. }
  1260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement