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: * Sales order shippment 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_Shipment_Api_V2 extends Mage_Sales_Model_Order_Shipment_Api
35: {
36: protected function _prepareItemQtyData($data)
37: {
38: $_data = array();
39: foreach ($data as $item) {
40: if (isset($item->order_item_id) && isset($item->qty)) {
41: $_data[$item->order_item_id] = $item->qty;
42: }
43: }
44: return $_data;
45: }
46:
47: /**
48: * Create new shipment for order
49: *
50: * @param string $orderIncrementId
51: * @param array $itemsQty
52: * @param string $comment
53: * @param boolean $email
54: * @param boolean $includeComment
55: * @return string
56: */
57: public function create($orderIncrementId, $itemsQty = array(), $comment = null, $email = false,
58: $includeComment = false
59: ) {
60: $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
61: $itemsQty = $this->_prepareItemQtyData($itemsQty);
62: /**
63: * Check order existing
64: */
65: if (!$order->getId()) {
66: $this->_fault('order_not_exists');
67: }
68:
69: /**
70: * Check shipment create availability
71: */
72: if (!$order->canShip()) {
73: $this->_fault('data_invalid', Mage::helper('sales')->__('Cannot do shipment for order.'));
74: }
75:
76: /* @var $shipment Mage_Sales_Model_Order_Shipment */
77: $shipment = $order->prepareShipment($itemsQty);
78: if ($shipment) {
79: $shipment->register();
80: $shipment->addComment($comment, $email && $includeComment);
81: if ($email) {
82: $shipment->setEmailSent(true);
83: }
84: $shipment->getOrder()->setIsInProcess(true);
85: try {
86: $transactionSave = Mage::getModel('core/resource_transaction')
87: ->addObject($shipment)
88: ->addObject($shipment->getOrder())
89: ->save();
90: $shipment->sendEmail($email, ($includeComment ? $comment : ''));
91: } catch (Mage_Core_Exception $e) {
92: $this->_fault('data_invalid', $e->getMessage());
93: }
94: return $shipment->getIncrementId();
95: }
96: return null;
97: }
98:
99: /**
100: * Retrieve allowed shipping carriers for specified order
101: *
102: * @param string $orderIncrementId
103: * @return array
104: */
105: public function getCarriers($orderIncrementId)
106: {
107: $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
108:
109: /**
110: * Check order existing
111: */
112: if (!$order->getId()) {
113: $this->_fault('order_not_exists');
114: }
115: $carriers = array();
116: foreach ($this->_getCarriers($order) as $key => $value) {
117: $carriers[] = array('key' => $key, 'value' => $value);
118: }
119:
120: return $carriers;
121: }
122: }
123: