PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Sales/Model/Resource/Order/Collection.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 247 lines | 126 code | 18 blank | 103 comment | 5 complexity | d3dab5f90d3a84ba0548b217a12c9b72 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Flat sales order collection
  28. *
  29. * @category Mage
  30. * @package Mage_Sales
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Sales_Model_Resource_Order_Collection extends Mage_Sales_Model_Resource_Collection_Abstract
  34. {
  35. /**
  36. * Event prefix
  37. *
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'sales_order_collection';
  41. /**
  42. * Event object
  43. *
  44. * @var string
  45. */
  46. protected $_eventObject = 'order_collection';
  47. /**
  48. * Model initialization
  49. *
  50. */
  51. protected function _construct()
  52. {
  53. $this->_init('sales/order');
  54. $this
  55. ->addFilterToMap('entity_id', 'main_table.entity_id')
  56. ->addFilterToMap('customer_id', 'main_table.customer_id')
  57. ->addFilterToMap('quote_address_id', 'main_table.quote_address_id');
  58. }
  59. /**
  60. * Add items count expr to collection select, backward capability with eav structure
  61. *
  62. * @return Mage_Sales_Model_Resource_Order_Collection
  63. */
  64. public function addItemCountExpr()
  65. {
  66. if (is_null($this->_fieldsToSelect)) {
  67. // If we select all fields from table, we need to add column alias
  68. $this->getSelect()->columns(array('items_count'=>'total_item_count'));
  69. } else {
  70. $this->addFieldToSelect('total_item_count', 'items_count');
  71. }
  72. return $this;
  73. }
  74. /**
  75. * Minimize usual count select
  76. *
  77. * @return Varien_Db_Select
  78. */
  79. public function getSelectCountSql()
  80. {
  81. /* @var $countSelect Varien_Db_Select */
  82. $countSelect = parent::getSelectCountSql();
  83. $countSelect->resetJoinLeft();
  84. $countSelect->reset(Zend_Db_Select::GROUP);
  85. return $countSelect;
  86. }
  87. /**
  88. * Reset left join
  89. *
  90. * @param int $limit
  91. * @param int $offset
  92. * @return Mage_Eav_Model_Entity_Collection_Abstract
  93. */
  94. protected function _getAllIdsSelect($limit = null, $offset = null)
  95. {
  96. $idsSelect = parent::_getAllIdsSelect($limit, $offset);
  97. $idsSelect->resetJoinLeft();
  98. return $idsSelect;
  99. }
  100. /**
  101. * Join table sales_flat_order_address to select for billing and shipping order addresses.
  102. * Create corillation map
  103. *
  104. * @return Mage_Sales_Model_Resource_Order_Collection
  105. */
  106. protected function _addAddressFields()
  107. {
  108. $billingAliasName = 'billing_o_a';
  109. $shippingAliasName = 'shipping_o_a';
  110. $joinTable = $this->getTable('sales/order_address');
  111. $this
  112. ->addFilterToMap('billing_firstname', $billingAliasName . '.firstname')
  113. ->addFilterToMap('billing_middlename', $billingAliasName . '.middlename')
  114. ->addFilterToMap('billing_lastname', $billingAliasName . '.lastname')
  115. ->addFilterToMap('billing_telephone', $billingAliasName . '.telephone')
  116. ->addFilterToMap('billing_postcode', $billingAliasName . '.postcode')
  117. ->addFilterToMap('shipping_firstname', $shippingAliasName . '.firstname')
  118. ->addFilterToMap('shipping_middlename', $shippingAliasName . '.middlename')
  119. ->addFilterToMap('shipping_lastname', $shippingAliasName . '.lastname')
  120. ->addFilterToMap('shipping_telephone', $shippingAliasName . '.telephone')
  121. ->addFilterToMap('shipping_postcode', $shippingAliasName . '.postcode');
  122. $this
  123. ->getSelect()
  124. ->joinLeft(
  125. array($billingAliasName => $joinTable),
  126. "(main_table.entity_id = {$billingAliasName}.parent_id"
  127. . " AND {$billingAliasName}.address_type = 'billing')",
  128. array(
  129. $billingAliasName . '.firstname',
  130. $billingAliasName . '.middlename',
  131. $billingAliasName . '.lastname',
  132. $billingAliasName . '.telephone',
  133. $billingAliasName . '.postcode'
  134. )
  135. )
  136. ->joinLeft(
  137. array($shippingAliasName => $joinTable),
  138. "(main_table.entity_id = {$shippingAliasName}.parent_id"
  139. . " AND {$shippingAliasName}.address_type = 'shipping')",
  140. array(
  141. $shippingAliasName . '.firstname',
  142. $shippingAliasName . '.middlename',
  143. $shippingAliasName . '.lastname',
  144. $shippingAliasName . '.telephone',
  145. $shippingAliasName . '.postcode'
  146. )
  147. );
  148. Mage::getResourceHelper('core')->prepareColumnsList($this->getSelect());
  149. return $this;
  150. }
  151. /**
  152. * Add addresses information to select
  153. *
  154. * @return Mage_Sales_Model_Resource_Collection_Abstract
  155. */
  156. public function addAddressFields()
  157. {
  158. return $this->_addAddressFields();
  159. }
  160. /**
  161. * Add field search filter to collection as OR condition
  162. *
  163. * @see self::_getConditionSql for $condition
  164. *
  165. * @param string $field
  166. * @param null|string|array $condition
  167. * @return Mage_Sales_Model_Resource_Order_Collection
  168. */
  169. public function addFieldToSearchFilter($field, $condition = null)
  170. {
  171. $field = $this->_getMappedField($field);
  172. $this->_select->orWhere($this->_getConditionSql($field, $condition));
  173. return $this;
  174. }
  175. /**
  176. * Specify collection select filter by attribute value
  177. *
  178. * @param array $attributes
  179. * @param array|integer|string|null $condition
  180. * @return Mage_Sales_Model_Resource_Order_Collection
  181. */
  182. public function addAttributeToSearchFilter($attributes, $condition = null)
  183. {
  184. if (is_array($attributes) && !empty($attributes)) {
  185. $this->_addAddressFields();
  186. $toFilterData = array();
  187. foreach ($attributes as $attribute) {
  188. $this->addFieldToSearchFilter($this->_attributeToField($attribute['attribute']), $attribute);
  189. }
  190. } else {
  191. $this->addAttributeToFilter($attributes, $condition);
  192. }
  193. return $this;
  194. }
  195. /**
  196. * Add filter by specified billing agreements
  197. *
  198. * @param int|array $agreements
  199. * @return Mage_Sales_Model_Resource_Order_Collection
  200. */
  201. public function addBillingAgreementsFilter($agreements)
  202. {
  203. $agreements = (is_array($agreements)) ? $agreements : array($agreements);
  204. $this->getSelect()
  205. ->joinInner(
  206. array('sbao' => $this->getTable('sales/billing_agreement_order')),
  207. 'main_table.entity_id = sbao.order_id',
  208. array())
  209. ->where('sbao.agreement_id IN(?)', $agreements);
  210. return $this;
  211. }
  212. /**
  213. * Add filter by specified recurring profile id(s)
  214. *
  215. * @param array|int $ids
  216. * @return Mage_Sales_Model_Resource_Order_Collection
  217. */
  218. public function addRecurringProfilesFilter($ids)
  219. {
  220. $ids = (is_array($ids)) ? $ids : array($ids);
  221. $this->getSelect()
  222. ->joinInner(
  223. array('srpo' => $this->getTable('sales/recurring_profile_order')),
  224. 'main_table.entity_id = srpo.order_id',
  225. array())
  226. ->where('srpo.profile_id IN(?)', $ids);
  227. return $this;
  228. }
  229. }