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 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: * Payment transactions collection
30: *
31: * @category Mage
32: * @package Mage_Sales
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
36: extends Mage_Sales_Model_Resource_Order_Collection_Abstract
37: {
38: /**
39: * Order ID filter
40: *
41: * @var int
42: */
43: protected $_orderId = null;
44:
45: /**
46: * Columns of order info that should be selected
47: *
48: * @var array
49: */
50: protected $_addOrderInformation = array();
51:
52: /**
53: * Columns of payment info that should be selected
54: *
55: * @var array
56: */
57: protected $_addPaymentInformation = array();
58:
59: /**
60: * Order Store ids
61: *
62: * @var array
63: */
64: protected $_storeIds = array();
65:
66: /**
67: * Payment ID filter
68: *
69: * @var int
70: */
71: protected $_paymentId = null;
72:
73: /**
74: * Parent ID filter
75: *
76: * @var int
77: */
78: protected $_parentId = null;
79:
80: /**
81: * Filter by transaction type
82: *
83: * @var array
84: */
85: protected $_txnTypes = null;
86:
87: /**
88: * Order field for setOrderFilter
89: *
90: * @var string
91: */
92: protected $_orderField = 'order_id';
93:
94: /**
95: * Initialize collection items factory class
96: */
97: protected function _construct()
98: {
99: $this->_init('sales/order_payment_transaction');
100: parent::_construct();
101: }
102:
103: /**
104: * Join order information
105: *
106: * @param array $keys
107: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
108: */
109: public function addOrderInformation(array $keys)
110: {
111: $this->_addOrderInformation = array_merge($this->_addOrderInformation, $keys);
112: $this->addFilterToMap('created_at', 'main_table.created_at');
113: return $this;
114: }
115:
116: /**
117: * Join payment information
118: *
119: * @param array $keys
120: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
121: */
122: public function addPaymentInformation(array $keys)
123: {
124: $this->_addPaymentInformation = array_merge($this->_addPaymentInformation, $keys);
125: return $this;
126: }
127:
128: /**
129: * Order ID filter setter
130: *
131: * @param int $orderId
132: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
133: */
134: public function addOrderIdFilter($orderId)
135: {
136: $this->_orderId = (int)$orderId;
137: return $this;
138: }
139:
140: /**
141: * Payment ID filter setter
142: * Can take either the integer id or the payment instance
143: *
144: * @param Mage_Sales_Model_Order_Payment|int $payment
145: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
146: */
147: public function addPaymentIdFilter($payment)
148: {
149: $id = $payment;
150: if (is_object($payment)) {
151: $id = $payment->getId();
152: }
153: $this->_paymentId = (int)$id;
154: return $this;
155: }
156:
157: /**
158: * Parent ID filter setter
159: *
160: * @param int $parentId
161: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
162: */
163: public function addParentIdFilter($parentId)
164: {
165: $this->_parentId = (int)$parentId;
166: return $this;
167: }
168:
169: /**
170: * Transaction type filter setter
171: *
172: * @param array|string $txnType
173: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
174: */
175: public function addTxnTypeFilter($txnType)
176: {
177: if (!is_array($txnType)) {
178: $txnType = array($txnType);
179: }
180: $this->_txnTypes = $txnType;
181: return $this;
182: }
183:
184: /**
185: * Add filter by store ids
186: *
187: * @param int|array $storeIds
188: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
189: */
190: public function addStoreFilter($storeIds)
191: {
192: $storeIds = (is_array($storeIds)) ? $storeIds : array($storeIds);
193: $this->_storeIds = array_merge($this->_storeIds, $storeIds);
194: return $this;
195: }
196:
197: /**
198: * Prepare filters
199: *
200: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
201: */
202: protected function _beforeLoad()
203: {
204: parent::_beforeLoad();
205:
206: if ($this->isLoaded()) {
207: return $this;
208: }
209:
210: // filters
211: if ($this->_paymentId) {
212: $this->getSelect()->where('main_table.payment_id = ?', $this->_paymentId);
213: }
214: if ($this->_parentId) {
215: $this->getSelect()->where('main_table.parent_id = ?', $this->_parentId);
216: }
217: if ($this->_txnTypes) {
218: $this->getSelect()->where('main_table.txn_type IN(?)', $this->_txnTypes);
219: }
220: if ($this->_orderId) {
221: $this->getSelect()->where('main_table.order_id = ?', $this->_orderId);
222: }
223: if ($this->_addPaymentInformation) {
224: $this->getSelect()->joinInner(
225: array('sop' => $this->getTable('sales/order_payment')),
226: 'main_table.payment_id = sop.entity_id',
227: $this->_addPaymentInformation
228: );
229: }
230: if ($this->_storeIds) {
231: $this->getSelect()->where('so.store_id IN(?)', $this->_storeIds);
232: $this->addOrderInformation(array('store_id'));
233: }
234: if ($this->_addOrderInformation) {
235: $this->getSelect()->joinInner(
236: array('so' => $this->getTable('sales/order')),
237: 'main_table.order_id = so.entity_id',
238: $this->_addOrderInformation
239: );
240: }
241: return $this;
242: }
243:
244: /**
245: * Unserialize additional_information in each item
246: *
247: * @return Mage_Sales_Model_Resource_Order_Payment_Transaction_Collection
248: */
249: protected function _afterLoad()
250: {
251: foreach ($this->_items as $item) {
252: $this->getResource()->unserializeFields($item);
253: }
254: return parent::_afterLoad();
255: }
256: }
257: