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: * Invoice API V2
29: *
30: * @category Mage
31: * @package Mage_Sales
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Sales_Model_Order_Invoice_Api_V2 extends Mage_Sales_Model_Order_Invoice_Api
35: {
36: /**
37: * Create new invoice for order
38: *
39: * @param string $invoiceIncrementId
40: * @param array $itemsQty
41: * @param string $comment
42: * @param bool $email
43: * @param bool $includeComment
44: * @return string
45: */
46: public function create($invoiceIncrementId, $itemsQty, $comment = null, $email = false, $includeComment = false)
47: {
48: $order = Mage::getModel('sales/order')->loadByIncrementId($invoiceIncrementId);
49: $itemsQty = $this->_prepareItemQtyData($itemsQty);
50: /* @var $order Mage_Sales_Model_Order */
51: /**
52: * Check order existing
53: */
54: if (!$order->getId()) {
55: $this->_fault('order_not_exists');
56: }
57:
58: /**
59: * Check invoice create availability
60: */
61: if (!$order->canInvoice()) {
62: $this->_fault('data_invalid', Mage::helper('sales')->__('Cannot do invoice for order.'));
63: }
64:
65: $invoice = $order->prepareInvoice($itemsQty);
66:
67: $invoice->register();
68:
69: if ($comment !== null) {
70: $invoice->addComment($comment, $email);
71: }
72:
73: if ($email) {
74: $invoice->setEmailSent(true);
75: }
76:
77: $invoice->getOrder()->setIsInProcess(true);
78:
79: try {
80: Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder())->save();
81: $invoice->sendEmail($email, ($includeComment ? $comment : ''));
82: } catch (Mage_Core_Exception $e) {
83: $this->_fault('data_invalid', $e->getMessage());
84: }
85:
86: return $invoice->getIncrementId();
87: }
88:
89: /**
90: * Prepare items quantity data
91: *
92: * @param array $data
93: * @return array
94: */
95: protected function _prepareItemQtyData($data)
96: {
97: $quantity = array();
98: foreach ($data as $item) {
99: if (isset($item->order_item_id) && isset($item->qty)) {
100: $quantity[$item->order_item_id] = $item->qty;
101: }
102: }
103: return $quantity;
104: }
105: }
106: