1: <?php
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: class Mage_Sales_Model_Order_Creditmemo_Api extends Mage_Sales_Model_Api_Resource
35: {
36:
37: 38: 39:
40: public function __construct()
41: {
42: $this->_attributesMap = array(
43: 'creditmemo' => array('creditmemo_id' => 'entity_id'),
44: 'creditmemo_item' => array('item_id' => 'entity_id'),
45: 'creditmemo_comment' => array('comment_id' => 'entity_id')
46: );
47: }
48:
49: 50: 51: 52: 53: 54:
55: public function items($filters = null)
56: {
57: $creditmemos = array();
58:
59: $apiHelper = Mage::helper('api');
60: $filters = $apiHelper->parseFilters($filters, $this->_attributesMap['creditmemo']);
61:
62: $creditmemoModel = Mage::getModel('sales/order_creditmemo');
63: try {
64: $creditMemoCollection = $creditmemoModel->getFilteredCollectionItems($filters);
65: foreach ($creditMemoCollection as $creditmemo) {
66: $creditmemos[] = $this->_getAttributes($creditmemo, 'creditmemo');
67: }
68: } catch (Exception $e) {
69: $this->_fault('invalid_filter', $e->getMessage());
70: }
71: return $creditmemos;
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: protected function _prepareListFilter($filter = null)
82: {
83:
84: if (is_array($filter)) {
85: foreach ($filter as $field => $value) {
86: if (isset($this->_attributesMap['creditmemo'][$field])) {
87: $filter[$this->_attributesMap['creditmemo'][$field]] = $value;
88: unset($filter[$field]);
89: }
90: }
91: }
92: return $filter;
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function info($creditmemoIncrementId)
102: {
103: $creditmemo = $this->_getCreditmemo($creditmemoIncrementId);
104:
105: $result = $this->_getAttributes($creditmemo, 'creditmemo');
106: $result['order_increment_id'] = $creditmemo->getOrder()->load($creditmemo->getOrderId())->getIncrementId();
107:
108: $result['items'] = array();
109: foreach ($creditmemo->getAllItems() as $item) {
110: $result['items'][] = $this->_getAttributes($item, 'creditmemo_item');
111: }
112:
113: $result['comments'] = array();
114: foreach ($creditmemo->getCommentsCollection() as $comment) {
115: $result['comments'][] = $this->_getAttributes($comment, 'creditmemo_comment');
116: }
117:
118: return $result;
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132:
133: public function create($creditmemoIncrementId, $creditmemoData = null, $comment = null, $notifyCustomer = false,
134: $includeComment = false, $refundToStoreCreditAmount = null)
135: {
136:
137: $order = Mage::getModel('sales/order')->load($creditmemoIncrementId, 'increment_id');
138: if (!$order->getId()) {
139: $this->_fault('order_not_exists');
140: }
141: if (!$order->canCreditmemo()) {
142: $this->_fault('cannot_create_creditmemo');
143: }
144: $creditmemoData = $this->_prepareCreateData($creditmemoData);
145:
146:
147: $service = Mage::getModel('sales/service_order', $order);
148:
149: $creditmemo = $service->prepareCreditmemo($creditmemoData);
150:
151:
152: if ($refundToStoreCreditAmount) {
153:
154: if ($order->getCustomerIsGuest()) {
155: $this->_fault('cannot_refund_to_storecredit');
156: }
157: $refundToStoreCreditAmount = max(
158: 0,
159: min($creditmemo->getBaseCustomerBalanceReturnMax(), $refundToStoreCreditAmount)
160: );
161: if ($refundToStoreCreditAmount) {
162: $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice($refundToStoreCreditAmount);
163: $creditmemo->setBaseCustomerBalanceTotalRefunded($refundToStoreCreditAmount);
164: $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice(
165: $refundToStoreCreditAmount*$order->getStoreToOrderRate()
166: );
167:
168: $creditmemo->setBsCustomerBalTotalRefunded($refundToStoreCreditAmount);
169:
170: $creditmemo->setCustomerBalanceRefundFlag(true);
171: }
172: }
173: $creditmemo->setPaymentRefundDisallowed(true)->register();
174:
175: if (!empty($comment)) {
176: $creditmemo->addComment($comment, $notifyCustomer);
177: }
178: try {
179: Mage::getModel('core/resource_transaction')
180: ->addObject($creditmemo)
181: ->addObject($order)
182: ->save();
183:
184: $creditmemo->sendEmail($notifyCustomer, ($includeComment ? $comment : ''));
185: } catch (Mage_Core_Exception $e) {
186: $this->_fault('data_invalid', $e->getMessage());
187: }
188: return $creditmemo->getIncrementId();
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199:
200: public function ($creditmemoIncrementId, $comment, $notifyCustomer = false, $includeComment = false)
201: {
202: $creditmemo = $this->_getCreditmemo($creditmemoIncrementId);
203: try {
204: $creditmemo->addComment($comment, $notifyCustomer)->save();
205: $creditmemo->sendUpdateEmail($notifyCustomer, ($includeComment ? $comment : ''));
206: } catch (Mage_Core_Exception $e) {
207: $this->_fault('data_invalid', $e->getMessage());
208: }
209:
210: return true;
211: }
212:
213: 214: 215: 216: 217: 218:
219: public function cancel($creditmemoIncrementId)
220: {
221: $creditmemo = $this->_getCreditmemo($creditmemoIncrementId);
222:
223: if (!$creditmemo->canCancel()) {
224: $this->_fault('status_not_changed', Mage::helper('sales')->__('Credit memo cannot be canceled.'));
225: }
226: try {
227: $creditmemo->cancel()->save();
228: } catch (Exception $e) {
229: $this->_fault('status_not_changed', Mage::helper('sales')->__('Credit memo canceling problem.'));
230: }
231:
232: return true;
233: }
234:
235: 236: 237: 238: 239: 240:
241: protected function _prepareCreateData($data)
242: {
243: $data = isset($data) ? $data : array();
244:
245: if (isset($data['qtys']) && count($data['qtys'])) {
246: $qtysArray = array();
247: foreach ($data['qtys'] as $qKey => $qVal) {
248:
249: if (is_array($qVal)) {
250: if (isset($qVal['order_item_id']) && isset($qVal['qty'])) {
251: $qtysArray[$qVal['order_item_id']] = $qVal['qty'];
252: }
253: } else {
254: $qtysArray[$qKey] = $qVal;
255: }
256: }
257: $data['qtys'] = $qtysArray;
258: }
259: return $data;
260: }
261:
262: 263: 264: 265: 266: 267:
268: protected function _getCreditmemo($incrementId)
269: {
270:
271: $creditmemo = Mage::getModel('sales/order_creditmemo')->load($incrementId, 'increment_id');
272: if (!$creditmemo->getId()) {
273: $this->_fault('not_exists');
274: }
275: return $creditmemo;
276: }
277:
278: }
279: