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_Invoice_Api extends Mage_Sales_Model_Api_Resource
35: {
36: 37: 38:
39: public function __construct()
40: {
41: $this->_attributesMap = array(
42: 'invoice' => array('invoice_id' => 'entity_id'),
43: 'invoice_item' => array('item_id' => 'entity_id'),
44: 'invoice_comment' => array('comment_id' => 'entity_id'));
45: }
46:
47: 48: 49: 50: 51: 52:
53: public function items($filters = null)
54: {
55: $invoices = array();
56:
57: $invoiceCollection = Mage::getResourceModel('sales/order_invoice_collection');
58: $invoiceCollection->addAttributeToSelect('entity_id')
59: ->addAttributeToSelect('order_id')
60: ->addAttributeToSelect('increment_id')
61: ->addAttributeToSelect('created_at')
62: ->addAttributeToSelect('state')
63: ->addAttributeToSelect('grand_total')
64: ->addAttributeToSelect('order_currency_code');
65:
66:
67: $apiHelper = Mage::helper('api');
68: try {
69: $filters = $apiHelper->parseFilters($filters, $this->_attributesMap['invoice']);
70: foreach ($filters as $field => $value) {
71: $invoiceCollection->addFieldToFilter($field, $value);
72: }
73: } catch (Mage_Core_Exception $e) {
74: $this->_fault('filters_invalid', $e->getMessage());
75: }
76: foreach ($invoiceCollection as $invoice) {
77: $invoices[] = $this->_getAttributes($invoice, 'invoice');
78: }
79: return $invoices;
80: }
81:
82: 83: 84: 85: 86: 87:
88: public function info($invoiceIncrementId)
89: {
90: $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
91:
92:
93:
94: if (!$invoice->getId()) {
95: $this->_fault('not_exists');
96: }
97:
98: $result = $this->_getAttributes($invoice, 'invoice');
99: $result['order_increment_id'] = $invoice->getOrderIncrementId();
100:
101: $result['items'] = array();
102: foreach ($invoice->getAllItems() as $item) {
103: $result['items'][] = $this->_getAttributes($item, 'invoice_item');
104: }
105:
106: $result['comments'] = array();
107: foreach ($invoice->getCommentsCollection() as $comment) {
108: $result['comments'][] = $this->_getAttributes($comment, 'invoice_comment');
109: }
110:
111: return $result;
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121: 122: 123:
124: public function create($orderIncrementId, $itemsQty, $comment = null, $email = false, $includeComment = false)
125: {
126: $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
127:
128:
129: 130: 131:
132: if (!$order->getId()) {
133: $this->_fault('order_not_exists');
134: }
135:
136: 137: 138:
139: if (!$order->canInvoice()) {
140: $this->_fault('data_invalid', Mage::helper('sales')->__('Cannot do invoice for order.'));
141: }
142:
143: $invoice = $order->prepareInvoice($itemsQty);
144:
145: $invoice->register();
146:
147: if ($comment !== null) {
148: $invoice->addComment($comment, $email);
149: }
150:
151: if ($email) {
152: $invoice->setEmailSent(true);
153: }
154:
155: $invoice->getOrder()->setIsInProcess(true);
156:
157: try {
158: $transactionSave = Mage::getModel('core/resource_transaction')
159: ->addObject($invoice)
160: ->addObject($invoice->getOrder())
161: ->save();
162:
163: $invoice->sendEmail($email, ($includeComment ? $comment : ''));
164: } catch (Mage_Core_Exception $e) {
165: $this->_fault('data_invalid', $e->getMessage());
166: }
167:
168: return $invoice->getIncrementId();
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178: 179:
180: public function ($invoiceIncrementId, $comment, $email = false, $includeComment = false)
181: {
182: $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
183:
184:
185:
186: if (!$invoice->getId()) {
187: $this->_fault('not_exists');
188: }
189:
190:
191: try {
192: $invoice->addComment($comment, $email);
193: $invoice->sendUpdateEmail($email, ($includeComment ? $comment : ''));
194: $invoice->save();
195: } catch (Mage_Core_Exception $e) {
196: $this->_fault('data_invalid', $e->getMessage());
197: }
198:
199: return true;
200: }
201:
202: 203: 204: 205: 206: 207:
208: public function capture($invoiceIncrementId)
209: {
210: $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
211:
212:
213:
214: if (!$invoice->getId()) {
215: $this->_fault('not_exists');
216: }
217:
218: if (!$invoice->canCapture()) {
219: $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice cannot be captured.'));
220: }
221:
222: try {
223: $invoice->capture();
224: $invoice->getOrder()->setIsInProcess(true);
225: $transactionSave = Mage::getModel('core/resource_transaction')
226: ->addObject($invoice)
227: ->addObject($invoice->getOrder())
228: ->save();
229: } catch (Mage_Core_Exception $e) {
230: $this->_fault('status_not_changed', $e->getMessage());
231: } catch (Exception $e) {
232: $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice capturing problem.'));
233: }
234:
235: return true;
236: }
237:
238: 239: 240: 241: 242: 243:
244: public function void($invoiceIncrementId)
245: {
246: $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
247:
248:
249:
250: if (!$invoice->getId()) {
251: $this->_fault('not_exists');
252: }
253:
254: if (!$invoice->canVoid()) {
255: $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice cannot be voided.'));
256: }
257:
258: try {
259: $invoice->void();
260: $invoice->getOrder()->setIsInProcess(true);
261: $transactionSave = Mage::getModel('core/resource_transaction')
262: ->addObject($invoice)
263: ->addObject($invoice->getOrder())
264: ->save();
265: } catch (Mage_Core_Exception $e) {
266: $this->_fault('status_not_changed', $e->getMessage());
267: } catch (Exception $e) {
268: $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice void problem'));
269: }
270:
271: return true;
272: }
273:
274: 275: 276: 277: 278: 279:
280: public function cancel($invoiceIncrementId)
281: {
282: $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
283:
284:
285:
286: if (!$invoice->getId()) {
287: $this->_fault('not_exists');
288: }
289:
290: if (!$invoice->canCancel()) {
291: $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice cannot be canceled.'));
292: }
293:
294: try {
295: $invoice->cancel();
296: $invoice->getOrder()->setIsInProcess(true);
297: $transactionSave = Mage::getModel('core/resource_transaction')
298: ->addObject($invoice)
299: ->addObject($invoice->getOrder())
300: ->save();
301: } catch (Mage_Core_Exception $e) {
302: $this->_fault('status_not_changed', $e->getMessage());
303: } catch (Exception $e) {
304: $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice canceling problem.'));
305: }
306:
307: return true;
308: }
309: }
310: