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

https://bitbucket.org/jokusafet/magento2 · PHP · 318 lines · 156 code · 33 blank · 129 comment · 13 complexity · 6f90bde27a2cdcd3cf0411bdab6c0ee9 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@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) 2012 X.commerce, Inc. (http://www.magentocommerce.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('gift_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_flat_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. }
  100. return $this;
  101. }
  102. /**
  103. * Retrieve a list or orders' addresses in a form of [order ID => array of addresses, ...]
  104. *
  105. * @param array $orderIds Orders identifiers
  106. * @return array
  107. */
  108. protected function _getAddresses(array $orderIds)
  109. {
  110. $addresses = array();
  111. if ($this->_isSubCallAllowed('order_address')) {
  112. /** @var $addressesFilter Mage_Api2_Model_Acl_Filter */
  113. $addressesFilter = $this->_getSubModel('order_address', array())->getFilter();
  114. // do addresses request if at least one attribute allowed
  115. if ($addressesFilter->getAllowedAttributes()) {
  116. /* @var $collection Mage_Sales_Model_Resource_Order_Address_Collection */
  117. $collection = Mage::getResourceModel('Mage_Sales_Model_Resource_Order_Address_Collection');
  118. $collection->addAttributeToFilter('parent_id', $orderIds);
  119. foreach ($collection->getItems() as $item) {
  120. $addresses[$item->getParentId()][] = $addressesFilter->out($item->toArray());
  121. }
  122. }
  123. }
  124. return $addresses;
  125. }
  126. /**
  127. * Retrieve collection instance for orders list
  128. *
  129. * @return Mage_Sales_Model_Resource_Order_Collection
  130. */
  131. protected function _getCollectionForRetrieve()
  132. {
  133. /** @var $collection Mage_Sales_Model_Resource_Order_Collection */
  134. $collection = Mage::getResourceModel('Mage_Sales_Model_Resource_Order_Collection');
  135. $this->_applyCollectionModifiers($collection);
  136. return $collection;
  137. }
  138. /**
  139. * Retrieve collection instance for single order
  140. *
  141. * @param int $orderId Order identifier
  142. * @return Mage_Sales_Model_Resource_Order_Collection
  143. */
  144. protected function _getCollectionForSingleRetrieve($orderId)
  145. {
  146. /** @var $collection Mage_Sales_Model_Resource_Order_Collection */
  147. $collection = Mage::getResourceModel('Mage_Sales_Model_Resource_Order_Collection');
  148. return $collection->addFieldToFilter($collection->getResource()->getIdFieldName(), $orderId);
  149. }
  150. /**
  151. * Retrieve a list or orders' comments in a form of [order ID => array of comments, ...]
  152. *
  153. * @param array $orderIds Orders' identifiers
  154. * @return array
  155. */
  156. protected function _getComments(array $orderIds)
  157. {
  158. $comments = array();
  159. if ($this->_isOrderCommentsAllowed() && $this->_isSubCallAllowed('order_comment')) {
  160. /** @var $commentsFilter Mage_Api2_Model_Acl_Filter */
  161. $commentsFilter = $this->_getSubModel('order_comment', array())->getFilter();
  162. // do comments request if at least one attribute allowed
  163. if ($commentsFilter->getAllowedAttributes()) {
  164. foreach ($this->_getCommentsCollection($orderIds)->getItems() as $item) {
  165. $comments[$item->getParentId()][] = $commentsFilter->out($item->toArray());
  166. }
  167. }
  168. }
  169. return $comments;
  170. }
  171. /**
  172. * Prepare and return order comments collection
  173. *
  174. * @param array $orderIds Orders' identifiers
  175. * @return Mage_Sales_Model_Resource_Order_Status_History_Collection|Object
  176. */
  177. protected function _getCommentsCollection(array $orderIds)
  178. {
  179. /* @var $collection Mage_Sales_Model_Resource_Order_Status_History_Collection */
  180. $collection = Mage::getResourceModel('Mage_Sales_Model_Resource_Order_Status_History_Collection');
  181. $collection->setOrderFilter($orderIds);
  182. return $collection;
  183. }
  184. /**
  185. * Retrieve a list or orders' items in a form of [order ID => array of items, ...]
  186. *
  187. * @param array $orderIds Orders identifiers
  188. * @return array
  189. */
  190. protected function _getItems(array $orderIds)
  191. {
  192. $items = array();
  193. if ($this->_isSubCallAllowed('order_item')) {
  194. /** @var $itemsFilter Mage_Api2_Model_Acl_Filter */
  195. $itemsFilter = $this->_getSubModel('order_item', array())->getFilter();
  196. // do items request if at least one attribute allowed
  197. if ($itemsFilter->getAllowedAttributes()) {
  198. /* @var $collection Mage_Sales_Model_Resource_Order_Item_Collection */
  199. $collection = Mage::getResourceModel('Mage_Sales_Model_Resource_Order_Item_Collection');
  200. $collection->addAttributeToFilter('order_id', $orderIds);
  201. foreach ($collection->getItems() as $item) {
  202. $items[$item->getOrderId()][] = $itemsFilter->out($item->toArray());
  203. }
  204. }
  205. }
  206. return $items;
  207. }
  208. /**
  209. * Check gift messages information is allowed
  210. *
  211. * @return bool
  212. */
  213. public function _isGiftMessageAllowed()
  214. {
  215. return in_array(self::PARAM_GIFT_MESSAGE, $this->getFilter()->getAllowedAttributes());
  216. }
  217. /**
  218. * Check order comments information is allowed
  219. *
  220. * @return bool
  221. */
  222. public function _isOrderCommentsAllowed()
  223. {
  224. return in_array(self::PARAM_ORDER_COMMENTS, $this->getFilter()->getAllowedAttributes());
  225. }
  226. /**
  227. * Check payment method information is allowed
  228. *
  229. * @return bool
  230. */
  231. public function _isPaymentMethodAllowed()
  232. {
  233. return in_array(self::PARAM_PAYMENT_METHOD, $this->getFilter()->getAllowedAttributes());
  234. }
  235. /**
  236. * Check tax name information is allowed
  237. *
  238. * @return bool
  239. */
  240. public function _isTaxNameAllowed()
  241. {
  242. return in_array(self::PARAM_TAX_NAME, $this->getFilter()->getAllowedAttributes());
  243. }
  244. /**
  245. * Check tax rate information is allowed
  246. *
  247. * @return bool
  248. */
  249. public function _isTaxRateAllowed()
  250. {
  251. return in_array(self::PARAM_TAX_RATE, $this->getFilter()->getAllowedAttributes());
  252. }
  253. /**
  254. * Get orders list
  255. *
  256. * @return array
  257. */
  258. protected function _retrieveCollection()
  259. {
  260. $collection = $this->_getCollectionForRetrieve();
  261. if ($this->_isPaymentMethodAllowed()) {
  262. $this->_addPaymentMethodInfo($collection);
  263. }
  264. if ($this->_isGiftMessageAllowed()) {
  265. $this->_addGiftMessageInfo($collection);
  266. }
  267. $this->_addTaxInfo($collection);
  268. $ordersData = array();
  269. foreach ($collection->getItems() as $order) {
  270. $ordersData[$order->getId()] = $order->toArray();
  271. }
  272. if ($ordersData) {
  273. foreach ($this->_getAddresses(array_keys($ordersData)) as $orderId => $addresses) {
  274. $ordersData[$orderId]['addresses'] = $addresses;
  275. }
  276. foreach ($this->_getItems(array_keys($ordersData)) as $orderId => $items) {
  277. $ordersData[$orderId]['order_items'] = $items;
  278. }
  279. foreach ($this->_getComments(array_keys($ordersData)) as $orderId => $comments) {
  280. $ordersData[$orderId]['order_comments'] = $comments;
  281. }
  282. }
  283. return $ordersData;
  284. }
  285. }