forked from n2bh/magento-enterprise-1.13.1.0
/
Archive.php
246 lines (228 loc) · 8.43 KB
/
Archive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Magento Enterprise Edition
*
* NOTICE OF LICENSE
*
* This source file is subject to the Magento Enterprise Edition License
* that is bundled with this package in the file LICENSE_EE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magentocommerce.com/license/enterprise-edition
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Enterprise
* @package Enterprise_SalesArchive
* @copyright Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
* @license http://www.magentocommerce.com/license/enterprise-edition
*/
/**
* Sales archive operations model
*/
class Enterprise_SalesArchive_Model_Archive
{
const ORDER = 'order';
const INVOICE = 'invoice';
const SHIPMENT = 'shipment';
const CREDITMEMO= 'creditmemo';
/**
* Archive entities definition
* array(
* $entity => $entityModel
* )
* @var $_entities array
*/
protected $_entities = array(
self::ORDER => 'sales/order',
self::INVOICE => 'sales/order_invoice',
self::SHIPMENT => 'sales/order_shipment',
self::CREDITMEMO=> 'sales/order_creditmemo',
);
/**
* Etities data getter
*
* @return string | array | false
*/
public function getEntityModel($entity = null)
{
if ($entity) {
return isset($this->_entities[$entity]) ? $this->_entities[$entity] : false;
}
return $this->_entities;
}
/**
* Get archive resource model
* @return Enterprise_SalesArchive_Model_Mysql4_Archive
*/
protected function _getResource()
{
return Mage::getResourceSingleton('enterprise_salesarchive/archive');
}
/**
* Update grid records in archive
*
* @param string $archiveEntity
* @param array $ids
*/
public function updateGridRecords($archiveEntity, $ids)
{
$this->_getResource()->updateGridRecords($this, $archiveEntity, $ids);
return $this;
}
/**
* Retrieve ids in archive for specified entity
*
* @param string $archiveEntity
* @param array $ids
* @return array
*/
public function getIdsInArchive($archiveEntity, $ids)
{
return $this->_getResource()->getIdsInArchive($archiveEntity, $ids);
}
/**
* Detects archive entity by object class
*
* @param Varien_Object $object
* @return string|boolean
*/
public function detectArchiveEntity($object)
{
foreach ($this->_entities as $archiveEntity => $entityModel) {
$className = Mage::getConfig()->getModelClassName($entityModel);
$resourceClassName = Mage::getConfig()->getResourceModelClassName($entityModel);
if ($object instanceof $className || $object instanceof $resourceClassName) {
return $archiveEntity;
}
}
return false;
}
/**
* Archive orders
*
* @return Enterprise_SalesArchive_Model_Archive
*/
public function archiveOrders()
{
$orderIds = $this->_getResource()->getOrderIdsForArchiveExpression();
$this->_getResource()->beginTransaction();
try {
$this->_getResource()->moveToArchive($this, self::ORDER, 'entity_id', $orderIds);
$this->_getResource()->moveToArchive($this, self::INVOICE, 'order_id', $orderIds);
$this->_getResource()->moveToArchive($this, self::SHIPMENT, 'order_id', $orderIds);
$this->_getResource()->moveToArchive($this, self::CREDITMEMO, 'order_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::ORDER, 'entity_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::INVOICE, 'order_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::SHIPMENT, 'order_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::CREDITMEMO, 'order_id', $orderIds);
$this->_getResource()->commit();
} catch (Exception $e) {
$this->_getResource()->rollBack();
throw $e;
}
Mage::dispatchEvent(
'enterprise_salesarchive_archive_archive_orders',
array('order_ids' => $orderIds)
);
return $this;
}
/**
* Archive orders, returns archived order ids
*
* @param array $orderIds
* @return array
*/
public function archiveOrdersById($orderIds)
{
$orderIds = $this->_getResource()->getOrderIdsForArchive($orderIds, false);
if (!empty($orderIds)) {
$this->_getResource()->beginTransaction();
try {
$this->_getResource()->moveToArchive($this, self::ORDER, 'entity_id', $orderIds);
$this->_getResource()->moveToArchive($this, self::INVOICE, 'order_id', $orderIds);
$this->_getResource()->moveToArchive($this, self::SHIPMENT, 'order_id', $orderIds);
$this->_getResource()->moveToArchive($this, self::CREDITMEMO, 'order_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::ORDER, 'entity_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::INVOICE, 'order_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::SHIPMENT, 'order_id', $orderIds);
$this->_getResource()->removeFromGrid($this, self::CREDITMEMO, 'order_id', $orderIds);
$this->_getResource()->commit();
} catch (Exception $e) {
$this->_getResource()->rollBack();
throw $e;
}
Mage::dispatchEvent(
'enterprise_salesarchive_archive_archive_orders',
array('order_ids' => $orderIds)
);
}
return $orderIds;
}
/**
* Move all orders from archive grid tables to regular grid tables
*
* @return Enterprise_SalesArchive_Model_Archive
*/
public function removeOrdersFromArchive()
{
$this->_getResource()->beginTransaction();
try {
$this->_getResource()->removeFromArchive($this, self::ORDER);
$this->_getResource()->removeFromArchive($this, self::INVOICE);
$this->_getResource()->removeFromArchive($this, self::SHIPMENT);
$this->_getResource()->removeFromArchive($this, self::CREDITMEMO);
$this->_getResource()->commit();
} catch (Exception $e) {
$this->_getResource()->rollBack();
throw $e;
}
return $this;
}
/**
* Removes orders from archive and restore in orders grid tables,
* returns restored order ids
*
* @param array $orderIds
* @return array
*/
public function removeOrdersFromArchiveById($orderIds)
{
$orderIds = $this->_getResource()->getIdsInArchive(self::ORDER, $orderIds);
if (!empty($orderIds)) {
$this->_getResource()->beginTransaction();
try {
$this->_getResource()->removeFromArchive($this, self::ORDER, 'entity_id', $orderIds);
$this->_getResource()->removeFromArchive($this, self::INVOICE, 'order_id', $orderIds);
$this->_getResource()->removeFromArchive($this, self::SHIPMENT, 'order_id', $orderIds);
$this->_getResource()->removeFromArchive($this, self::CREDITMEMO, 'order_id', $orderIds);
$this->_getResource()->commit();
} catch (Exception $e) {
$this->_getResource()->rollBack();
throw $e;
}
Mage::dispatchEvent(
'enterprise_salesarchive_archive_remove_orders_from_archive',
array('order_ids' => $orderIds)
);
}
return $orderIds;
}
/**
* Find related to order entity ids for checking of new items in archive
*
* @param string $archiveEntity
* @param array $ids
* @return array
*/
public function getRelatedIds($archiveEntity, $ids)
{
return $this->_getResource()->getRelatedIds($this, $archiveEntity, $ids);
}
}