Collection.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel\Order\Payment\Transaction;
  7. use Magento\Sales\Api\Data\TransactionSearchResultInterface;
  8. use Magento\Sales\Model\ResourceModel\Order\Collection\AbstractCollection;
  9. /**
  10. * Payment transactions collection
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. class Collection extends AbstractCollection implements TransactionSearchResultInterface
  15. {
  16. /**
  17. * Order ID filter
  18. *
  19. * @var int
  20. */
  21. protected $_orderId = null;
  22. /**
  23. * Columns of order info that should be selected
  24. *
  25. * @var string[]
  26. */
  27. protected $_addOrderInformation = [];
  28. /**
  29. * Columns of payment info that should be selected
  30. *
  31. * @var array
  32. */
  33. protected $_addPaymentInformation = [];
  34. /**
  35. * Order Store ids
  36. *
  37. * @var int[]
  38. */
  39. protected $_storeIds = [];
  40. /**
  41. * Payment ID filter
  42. *
  43. * @var int
  44. */
  45. protected $_paymentId = null;
  46. /**
  47. * Parent ID filter
  48. *
  49. * @var int
  50. */
  51. protected $_parentId = null;
  52. /**
  53. * Filter by transaction type
  54. *
  55. * @var string[]
  56. */
  57. protected $_txnTypes = null;
  58. /**
  59. * Order field for setOrderFilter
  60. *
  61. * @var string
  62. */
  63. protected $_orderField = 'order_id';
  64. /**
  65. * Initialize collection items factory class
  66. *
  67. * @return void
  68. */
  69. protected function _construct()
  70. {
  71. $this->_init(
  72. \Magento\Sales\Model\Order\Payment\Transaction::class,
  73. \Magento\Sales\Model\ResourceModel\Order\Payment\Transaction::class
  74. );
  75. $this->addFilterToMap('created_at', 'main_table.created_at');
  76. parent::_construct();
  77. }
  78. /**
  79. * Join order information
  80. *
  81. * @param string[] $keys
  82. * @return $this
  83. */
  84. public function addOrderInformation(array $keys)
  85. {
  86. $this->_addOrderInformation = array_merge($this->_addOrderInformation, $keys);
  87. return $this;
  88. }
  89. /**
  90. * Join payment information
  91. *
  92. * @param array $keys
  93. * @return $this
  94. */
  95. public function addPaymentInformation(array $keys)
  96. {
  97. $this->_addPaymentInformation = array_merge($this->_addPaymentInformation, $keys);
  98. return $this;
  99. }
  100. /**
  101. * Order ID filter setter
  102. *
  103. * @param int $orderId
  104. * @return $this
  105. */
  106. public function addOrderIdFilter($orderId)
  107. {
  108. $this->_orderId = (int)$orderId;
  109. return $this;
  110. }
  111. /**
  112. * Payment ID filter setter
  113. * Can take either the integer id or the payment instance
  114. *
  115. * @param \Magento\Sales\Model\Order\Payment|int $payment
  116. * @return $this
  117. */
  118. public function addPaymentIdFilter($payment)
  119. {
  120. $id = $payment;
  121. if (is_object($payment)) {
  122. $id = $payment->getId();
  123. }
  124. $this->_paymentId = (int)$id;
  125. return $this;
  126. }
  127. /**
  128. * Parent ID filter setter
  129. *
  130. * @param int $parentId
  131. * @return $this
  132. */
  133. public function addParentIdFilter($parentId)
  134. {
  135. $this->_parentId = (int)$parentId;
  136. return $this;
  137. }
  138. /**
  139. * Transaction type filter setter
  140. *
  141. * @param string[]|string $txnType
  142. * @return $this
  143. */
  144. public function addTxnTypeFilter($txnType)
  145. {
  146. if (!is_array($txnType)) {
  147. $txnType = [$txnType];
  148. }
  149. $this->_txnTypes = $txnType;
  150. return $this;
  151. }
  152. /**
  153. * Add filter by store ids
  154. *
  155. * @param int|int[] $storeIds
  156. * @return $this
  157. */
  158. public function addStoreFilter($storeIds)
  159. {
  160. $storeIds = is_array($storeIds) ? $storeIds : [$storeIds];
  161. $this->_storeIds = array_merge($this->_storeIds, $storeIds);
  162. return $this;
  163. }
  164. /**
  165. * Render additional filters and joins
  166. *
  167. * @return void
  168. */
  169. protected function _renderFiltersBefore()
  170. {
  171. if ($this->_paymentId) {
  172. $this->getSelect()->where('main_table.payment_id = ?', $this->_paymentId);
  173. }
  174. if ($this->_parentId) {
  175. $this->getSelect()->where('main_table.parent_id = ?', $this->_parentId);
  176. }
  177. if ($this->_txnTypes) {
  178. $this->getSelect()->where('main_table.txn_type IN(?)', $this->_txnTypes);
  179. }
  180. if ($this->_orderId) {
  181. $this->getSelect()->where('main_table.order_id = ?', $this->_orderId);
  182. }
  183. if ($this->_addPaymentInformation) {
  184. $this->getSelect()->joinInner(
  185. ['sop' => $this->getTable('sales_order_payment')],
  186. 'main_table.payment_id = sop.entity_id',
  187. $this->_addPaymentInformation
  188. );
  189. }
  190. if ($this->_storeIds) {
  191. $this->getSelect()->where('so.store_id IN(?)', $this->_storeIds);
  192. $this->addOrderInformation(['store_id']);
  193. }
  194. if ($this->_addOrderInformation) {
  195. $this->getSelect()->joinInner(
  196. ['so' => $this->getTable('sales_order')],
  197. 'main_table.order_id = so.entity_id',
  198. $this->_addOrderInformation
  199. );
  200. }
  201. }
  202. /**
  203. * Unserialize additional_information in each item
  204. *
  205. * @return $this
  206. */
  207. protected function _afterLoad()
  208. {
  209. foreach ($this->_items as $item) {
  210. $this->getResource()->unserializeFields($item);
  211. }
  212. return parent::_afterLoad();
  213. }
  214. }