PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Sales/Model/Api2/Order.php

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 319 lines | 157 code | 33 blank | 129 comment | 13 complexity | 93425465ef15601bf55b11dc824904bd 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-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * API2 class for orders
  28. *
  29. * @category Mage
  30. * @package Mage_Sales
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Sales_Model_Api2_Order extends Mage_Api2_Model_Resource
  34. {
  35. /**#@+
  36. * Parameters' names in config with special ACL meaning
  37. */
  38. const PARAM_GIFT_MESSAGE = '_gift_message';
  39. const PARAM_ORDER_COMMENTS = '_order_comments';
  40. const PARAM_PAYMENT_METHOD = '_payment_method';
  41. const PARAM_TAX_NAME = '_tax_name';
  42. const PARAM_TAX_RATE = '_tax_rate';
  43. /**#@-*/
  44. /**
  45. * Add gift message info to select
  46. *
  47. * @param Mage_Sales_Model_Resource_Order_Collection $collection
  48. * @return Mage_Sales_Model_Api2_Order
  49. */
  50. protected function _addGiftMessageInfo(Mage_Sales_Model_Resource_Order_Collection $collection)
  51. {
  52. $collection->getSelect()->joinLeft(
  53. array('gift_message' => $collection->getTable('giftmessage/message')),
  54. 'main_table.gift_message_id = gift_message.gift_message_id',
  55. array(
  56. 'gift_message_from' => 'gift_message.sender',
  57. 'gift_message_to' => 'gift_message.recipient',
  58. 'gift_message_body' => 'gift_message.message'
  59. )
  60. );
  61. return $this;
  62. }
  63. /**
  64. * Add order payment method field to select
  65. *
  66. * @param Mage_Sales_Model_Resource_Order_Collection $collection
  67. * @return Mage_Sales_Model_Api2_Order
  68. */
  69. protected function _addPaymentMethodInfo(Mage_Sales_Model_Resource_Order_Collection $collection)
  70. {
  71. $collection->getSelect()->joinLeft(
  72. array('payment_method' => $collection->getTable('sales/order_payment')),
  73. 'main_table.entity_id = payment_method.parent_id',
  74. array('payment_method' => 'payment_method.method')
  75. );
  76. return $this;
  77. }
  78. /**
  79. * Add order tax information to select
  80. *
  81. * @param Mage_Sales_Model_Resource_Order_Collection $collection
  82. * @return Mage_Sales_Model_Api2_Order
  83. */
  84. protected function _addTaxInfo(Mage_Sales_Model_Resource_Order_Collection $collection)
  85. {
  86. $taxInfoFields = array();
  87. if ($this->_isTaxNameAllowed()) {
  88. $taxInfoFields['tax_name'] = 'order_tax.title';
  89. }
  90. if ($this->_isTaxRateAllowed()) {
  91. $taxInfoFields['tax_rate'] = 'order_tax.percent';
  92. }
  93. if ($taxInfoFields) {
  94. $collection->getSelect()->joinLeft(
  95. array('order_tax' => $collection->getTable('sales/order_tax')),
  96. 'main_table.entity_id = order_tax.order_id',
  97. $taxInfoFields
  98. );
  99. $collection->getSelect()->group('main_table.entity_id');
  100. }
  101. return $this;
  102. }
  103. /**
  104. * Retrieve a list or orders' addresses in a form of [order ID => array of addresses, ...]
  105. *
  106. * @param array $orderIds Orders identifiers
  107. * @return array
  108. */
  109. protected function _getAddresses(array $orderIds)
  110. {
  111. $addresses = array();
  112. if ($this->_isSubCallAllowed('order_address')) {
  113. /** @var $addressesFilter Mage_Api2_Model_Acl_Filter */
  114. $addressesFilter = $this->_getSubModel('order_address', array())->getFilter();
  115. // do addresses request if at least one attribute allowed
  116. if ($addressesFilter->getAllowedAttributes()) {
  117. /* @var $collection Mage_Sales_Model_Resource_Order_Address_Collection */
  118. $collection = Mage::getResourceModel('sales/order_address_collection');
  119. $collection->addAttributeToFilter('parent_id', $orderIds);
  120. foreach ($collection->getItems() as $item) {
  121. $addresses[$item->getParentId()][] = $addressesFilter->out($item->toArray());
  122. }
  123. }
  124. }
  125. return $addresses;
  126. }
  127. /**
  128. * Retrieve collection instance for orders list
  129. *
  130. * @return Mage_Sales_Model_Resource_Order_Collection
  131. */
  132. protected function _getCollectionForRetrieve()
  133. {
  134. /** @var $collection Mage_Sales_Model_Resource_Order_Collection */
  135. $collection = Mage::getResourceModel('sales/order_collection');
  136. $this->_applyCollectionModifiers($collection);
  137. return $collection;
  138. }
  139. /**
  140. * Retrieve collection instance for single order
  141. *
  142. * @param int $orderId Order identifier
  143. * @return Mage_Sales_Model_Resource_Order_Collection
  144. */
  145. protected function _getCollectionForSingleRetrieve($orderId)
  146. {
  147. /** @var $collection Mage_Sales_Model_Resource_Order_Collection */
  148. $collection = Mage::getResourceModel('sales/order_collection');
  149. return $collection->addFieldToFilter($collection->getResource()->getIdFieldName(), $orderId);
  150. }
  151. /**
  152. * Retrieve a list or orders' comments in a form of [order ID => array of comments, ...]
  153. *
  154. * @param array $orderIds Orders' identifiers
  155. * @return array
  156. */
  157. protected function _getComments(array $orderIds)
  158. {
  159. $comments = array();
  160. if ($this->_isOrderCommentsAllowed() && $this->_isSubCallAllowed('order_comment')) {
  161. /** @var $commentsFilter Mage_Api2_Model_Acl_Filter */
  162. $commentsFilter = $this->_getSubModel('order_comment', array())->getFilter();
  163. // do comments request if at least one attribute allowed
  164. if ($commentsFilter->getAllowedAttributes()) {
  165. foreach ($this->_getCommentsCollection($orderIds)->getItems() as $item) {
  166. $comments[$item->getParentId()][] = $commentsFilter->out($item->toArray());
  167. }
  168. }
  169. }
  170. return $comments;
  171. }
  172. /**
  173. * Prepare and return order comments collection
  174. *
  175. * @param array $orderIds Orders' identifiers
  176. * @return Mage_Sales_Model_Resource_Order_Status_History_Collection|Object
  177. */
  178. protected function _getCommentsCollection(array $orderIds)
  179. {
  180. /* @var $collection Mage_Sales_Model_Resource_Order_Status_History_Collection */
  181. $collection = Mage::getResourceModel('sales/order_status_history_collection');
  182. $collection->setOrderFilter($orderIds);
  183. return $collection;
  184. }
  185. /**
  186. * Retrieve a list or orders' items in a form of [order ID => array of items, ...]
  187. *
  188. * @param array $orderIds Orders identifiers
  189. * @return array
  190. */
  191. protected function _getItems(array $orderIds)
  192. {
  193. $items = array();
  194. if ($this->_isSubCallAllowed('order_item')) {
  195. /** @var $itemsFilter Mage_Api2_Model_Acl_Filter */
  196. $itemsFilter = $this->_getSubModel('order_item', array())->getFilter();
  197. // do items request if at least one attribute allowed
  198. if ($itemsFilter->getAllowedAttributes()) {
  199. /* @var $collection Mage_Sales_Model_Resource_Order_Item_Collection */
  200. $collection = Mage::getResourceModel('sales/order_item_collection');
  201. $collection->addAttributeToFilter('order_id', $orderIds);
  202. foreach ($collection->getItems() as $item) {
  203. $items[$item->getOrderId()][] = $itemsFilter->out($item->toArray());
  204. }
  205. }
  206. }
  207. return $items;
  208. }
  209. /**
  210. * Check gift messages information is allowed
  211. *
  212. * @return bool
  213. */
  214. public function _isGiftMessageAllowed()
  215. {
  216. return in_array(self::PARAM_GIFT_MESSAGE, $this->getFilter()->getAllowedAttributes());
  217. }
  218. /**
  219. * Check order comments information is allowed
  220. *
  221. * @return bool
  222. */
  223. public function _isOrderCommentsAllowed()
  224. {
  225. return in_array(self::PARAM_ORDER_COMMENTS, $this->getFilter()->getAllowedAttributes());
  226. }
  227. /**
  228. * Check payment method information is allowed
  229. *
  230. * @return bool
  231. */
  232. public function _isPaymentMethodAllowed()
  233. {
  234. return in_array(self::PARAM_PAYMENT_METHOD, $this->getFilter()->getAllowedAttributes());
  235. }
  236. /**
  237. * Check tax name information is allowed
  238. *
  239. * @return bool
  240. */
  241. public function _isTaxNameAllowed()
  242. {
  243. return in_array(self::PARAM_TAX_NAME, $this->getFilter()->getAllowedAttributes());
  244. }
  245. /**
  246. * Check tax rate information is allowed
  247. *
  248. * @return bool
  249. */
  250. public function _isTaxRateAllowed()
  251. {
  252. return in_array(self::PARAM_TAX_RATE, $this->getFilter()->getAllowedAttributes());
  253. }
  254. /**
  255. * Get orders list
  256. *
  257. * @return array
  258. */
  259. protected function _retrieveCollection()
  260. {
  261. $collection = $this->_getCollectionForRetrieve();
  262. if ($this->_isPaymentMethodAllowed()) {
  263. $this->_addPaymentMethodInfo($collection);
  264. }
  265. if ($this->_isGiftMessageAllowed()) {
  266. $this->_addGiftMessageInfo($collection);
  267. }
  268. $this->_addTaxInfo($collection);
  269. $ordersData = array();
  270. foreach ($collection->getItems() as $order) {
  271. $ordersData[$order->getId()] = $order->toArray();
  272. }
  273. if ($ordersData) {
  274. foreach ($this->_getAddresses(array_keys($ordersData)) as $orderId => $addresses) {
  275. $ordersData[$orderId]['addresses'] = $addresses;
  276. }
  277. foreach ($this->_getItems(array_keys($ordersData)) as $orderId => $items) {
  278. $ordersData[$orderId]['order_items'] = $items;
  279. }
  280. foreach ($this->_getComments(array_keys($ordersData)) as $orderId => $comments) {
  281. $ordersData[$orderId]['order_comments'] = $comments;
  282. }
  283. }
  284. return $ordersData;
  285. }
  286. }