/app/code/core/Mage/Sales/Model/Order/Invoice/Api.php

https://bitbucket.org/spenna/alexoo_produzione · PHP · 322 lines · 178 code · 51 blank · 93 comment · 14 complexity · ccf13d580bccf5222bbb1d82b377489d MD5 · raw file

  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. * Invoice API
  28. *
  29. * @category Mage
  30. * @package Mage_Sales
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Sales_Model_Order_Invoice_Api extends Mage_Sales_Model_Api_Resource
  34. {
  35. public function __construct()
  36. {
  37. $this->_attributesMap['invoice'] = array(
  38. 'invoice_id' => 'entity_id'
  39. );
  40. $this->_attributesMap['invoice_item'] = array(
  41. 'item_id' => 'entity_id'
  42. );
  43. $this->_attributesMap['invoice_comment'] = array(
  44. 'comment_id' => 'entity_id'
  45. );
  46. }
  47. /**
  48. * Retrive invoices by filters
  49. *
  50. * @param array $filters
  51. * @return array
  52. */
  53. public function items($filters = null)
  54. {
  55. //TODO: add full name logic
  56. $collection = Mage::getResourceModel('sales/order_invoice_collection')
  57. ->addAttributeToSelect('order_id')
  58. ->addAttributeToSelect('increment_id')
  59. ->addAttributeToSelect('created_at')
  60. ->addAttributeToSelect('state')
  61. ->addAttributeToSelect('grand_total')
  62. ->addAttributeToSelect('order_currency_code')
  63. ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
  64. ->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
  65. ->joinAttribute('order_increment_id', 'order/increment_id', 'order_id', null, 'left')
  66. ->joinAttribute('order_created_at', 'order/created_at', 'order_id', null, 'left');
  67. if (is_array($filters)) {
  68. try {
  69. foreach ($filters as $field => $value) {
  70. if (isset($this->_attributesMap['invoice'][$field])) {
  71. $field = $this->_attributesMap['invoice'][$field];
  72. }
  73. $collection->addFieldToFilter($field, $value);
  74. }
  75. } catch (Mage_Core_Exception $e) {
  76. $this->_fault('filters_invalid', $e->getMessage());
  77. }
  78. }
  79. $result = array();
  80. foreach ($collection as $invoice) {
  81. $result[] = $this->_getAttributes($invoice, 'invoice');
  82. }
  83. return $result;
  84. }
  85. /**
  86. * Retrieve invoice information
  87. *
  88. * @param string $invoiceIncrementId
  89. * @return array
  90. */
  91. public function info($invoiceIncrementId)
  92. {
  93. $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
  94. /* @var Mage_Sales_Model_Order_Invoice $invoice */
  95. if (!$invoice->getId()) {
  96. $this->_fault('not_exists');
  97. }
  98. $result = $this->_getAttributes($invoice, 'invoice');
  99. $result['order_increment_id'] = $invoice->getOrderIncrementId();
  100. $result['items'] = array();
  101. foreach ($invoice->getAllItems() as $item) {
  102. $result['items'][] = $this->_getAttributes($item, 'invoice_item');
  103. }
  104. $result['comments'] = array();
  105. foreach ($invoice->getCommentsCollection() as $comment) {
  106. $result['comments'][] = $this->_getAttributes($comment, 'invoice_comment');
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Create new invoice for order
  112. *
  113. * @param string $orderIncrementId
  114. * @param array $itemsQty
  115. * @param string $comment
  116. * @param booleam $email
  117. * @param boolean $includeComment
  118. * @return string
  119. */
  120. public function create($orderIncrementId, $itemsQty, $comment = null, $email = false, $includeComment = false)
  121. {
  122. $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
  123. /* @var $order Mage_Sales_Model_Order */
  124. /**
  125. * Check order existing
  126. */
  127. if (!$order->getId()) {
  128. $this->_fault('order_not_exists');
  129. }
  130. /**
  131. * Check invoice create availability
  132. */
  133. if (!$order->canInvoice()) {
  134. $this->_fault('data_invalid', Mage::helper('sales')->__('Cannot do invoice for order.'));
  135. }
  136. $invoice = $order->prepareInvoice($itemsQty);
  137. $invoice->register();
  138. if ($comment !== null) {
  139. $invoice->addComment($comment, $email);
  140. }
  141. if ($email) {
  142. $invoice->setEmailSent(true);
  143. }
  144. $invoice->getOrder()->setIsInProcess(true);
  145. try {
  146. $transactionSave = Mage::getModel('core/resource_transaction')
  147. ->addObject($invoice)
  148. ->addObject($invoice->getOrder())
  149. ->save();
  150. $invoice->sendEmail($email, ($includeComment ? $comment : ''));
  151. } catch (Mage_Core_Exception $e) {
  152. $this->_fault('data_invalid', $e->getMessage());
  153. }
  154. return $invoice->getIncrementId();
  155. }
  156. /**
  157. * Add comment to invoice
  158. *
  159. * @param string $invoiceIncrementId
  160. * @param string $comment
  161. * @param boolean $email
  162. * @param boolean $includeComment
  163. * @return boolean
  164. */
  165. public function addComment($invoiceIncrementId, $comment, $email = false, $includeComment = false)
  166. {
  167. $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
  168. /* @var $invoice Mage_Sales_Model_Order_Invoice */
  169. if (!$invoice->getId()) {
  170. $this->_fault('not_exists');
  171. }
  172. try {
  173. $invoice->addComment($comment, $email);
  174. $invoice->sendUpdateEmail($email, ($includeComment ? $comment : ''));
  175. $invoice->save();
  176. } catch (Mage_Core_Exception $e) {
  177. $this->_fault('data_invalid', $e->getMessage());
  178. }
  179. return true;
  180. }
  181. /**
  182. * Capture invoice
  183. *
  184. * @param string $invoiceIncrementId
  185. * @return boolean
  186. */
  187. public function capture($invoiceIncrementId)
  188. {
  189. $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
  190. /* @var $invoice Mage_Sales_Model_Order_Invoice */
  191. if (!$invoice->getId()) {
  192. $this->_fault('not_exists');
  193. }
  194. if (!$invoice->canCapture()) {
  195. $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice cannot be captured.'));
  196. }
  197. try {
  198. $invoice->capture();
  199. $invoice->getOrder()->setIsInProcess(true);
  200. $transactionSave = Mage::getModel('core/resource_transaction')
  201. ->addObject($invoice)
  202. ->addObject($invoice->getOrder())
  203. ->save();
  204. } catch (Mage_Core_Exception $e) {
  205. $this->_fault('status_not_changed', $e->getMessage());
  206. } catch (Exception $e) {
  207. $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice capturing problem.'));
  208. }
  209. return true;
  210. }
  211. /**
  212. * Void invoice
  213. *
  214. * @param unknown_type $invoiceIncrementId
  215. * @return unknown
  216. */
  217. public function void($invoiceIncrementId)
  218. {
  219. $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
  220. /* @var $invoice Mage_Sales_Model_Order_Invoice */
  221. if (!$invoice->getId()) {
  222. $this->_fault('not_exists');
  223. }
  224. if (!$invoice->canVoid()) {
  225. $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice cannot be voided.'));
  226. }
  227. try {
  228. $invoice->void();
  229. $invoice->getOrder()->setIsInProcess(true);
  230. $transactionSave = Mage::getModel('core/resource_transaction')
  231. ->addObject($invoice)
  232. ->addObject($invoice->getOrder())
  233. ->save();
  234. } catch (Mage_Core_Exception $e) {
  235. $this->_fault('status_not_changed', $e->getMessage());
  236. } catch (Exception $e) {
  237. $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice void problem'));
  238. }
  239. return true;
  240. }
  241. /**
  242. * Cancel invoice
  243. *
  244. * @param string $invoiceIncrementId
  245. * @return boolean
  246. */
  247. public function cancel($invoiceIncrementId)
  248. {
  249. $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
  250. /* @var $invoice Mage_Sales_Model_Order_Invoice */
  251. if (!$invoice->getId()) {
  252. $this->_fault('not_exists');
  253. }
  254. if (!$invoice->canCancel()) {
  255. $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice cannot be canceled.'));
  256. }
  257. try {
  258. $invoice->cancel();
  259. $invoice->getOrder()->setIsInProcess(true);
  260. $transactionSave = Mage::getModel('core/resource_transaction')
  261. ->addObject($invoice)
  262. ->addObject($invoice->getOrder())
  263. ->save();
  264. } catch (Mage_Core_Exception $e) {
  265. $this->_fault('status_not_changed', $e->getMessage());
  266. } catch (Exception $e) {
  267. $this->_fault('status_not_changed', Mage::helper('sales')->__('Invoice canceling problem.'));
  268. }
  269. return true;
  270. }
  271. } // Class Mage_Sales_Model_Order_Invoice_Api End