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_Shipping
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: class Mage_Shipping_Model_Rate_Result
29: {
30: /**
31: * Shippin method rates
32: *
33: * @var array
34: */
35: protected $_rates = array();
36:
37: /**
38: * Shipping errors
39: *
40: * @var null|bool
41: */
42: protected $_error = null;
43:
44: /**
45: * Reset result
46: *
47: * @return Mage_Shipping_Model_Rate_Result
48: */
49: public function reset()
50: {
51: $this->_rates = array();
52: return $this;
53: }
54:
55: /**
56: * Set Error
57: *
58: * @param bool $error
59: * @return void
60: */
61: public function setError($error)
62: {
63: $this->_error = $error;
64: }
65:
66: /**
67: * Get Error
68: *
69: * @return null|bool;
70: */
71: public function getError()
72: {
73: return $this->_error;
74: }
75:
76: /**
77: * Add a rate to the result
78: *
79: * @param Mage_Shipping_Model_Rate_Result_Abstract|Mage_Shipping_Model_Rate_Result $result
80: * @return Mage_Shipping_Model_Rate_Result
81: */
82: public function append($result)
83: {
84: if ($result instanceof Mage_Shipping_Model_Rate_Result_Error) {
85: $this->setError(true);
86: }
87: if ($result instanceof Mage_Shipping_Model_Rate_Result_Abstract) {
88: $this->_rates[] = $result;
89: }
90: elseif ($result instanceof Mage_Shipping_Model_Rate_Result) {
91: $rates = $result->getAllRates();
92: foreach ($rates as $rate) {
93: $this->append($rate);
94: }
95: }
96: return $this;
97: }
98:
99: /**
100: * Return all quotes in the result
101: *
102: * @return array
103: */
104: public function getAllRates()
105: {
106: return $this->_rates;
107: }
108:
109: /**
110: * Return rate by id in array
111: *
112: * @param int $id
113: * @return Mage_Shipping_Model_Rate_Result_Method|null
114: */
115: public function getRateById($id)
116: {
117: return isset($this->_rates[$id]) ? $this->_rates[$id] : null;
118: }
119:
120: /**
121: * Return quotes for specified type
122: *
123: * @param string $carrier
124: * @return array
125: */
126: public function getRatesByCarrier($carrier)
127: {
128: $result = array();
129: foreach ($this->_rates as $rate) {
130: if ($rate->getCarrier() === $carrier) {
131: $result[] = $rate;
132: }
133: }
134: return $result;
135: }
136:
137: /**
138: * Converts object to array
139: *
140: * @return array
141: */
142: public function asArray()
143: {
144: $currencyFilter = Mage::app()->getStore()->getPriceFilter();
145: $rates = array();
146: $allRates = $this->getAllRates();
147: foreach ($allRates as $rate) {
148: $rates[$rate->getCarrier()]['title'] = $rate->getCarrierTitle();
149: $rates[$rate->getCarrier()]['methods'][$rate->getMethod()] = array(
150: 'title' => $rate->getMethodTitle(),
151: 'price' => $rate->getPrice(),
152: 'price_formatted' => $currencyFilter->filter($rate->getPrice()),
153: );
154: }
155: return $rates;
156: }
157:
158: /**
159: * Get cheapest rate
160: *
161: * @return null|Mage_Shipping_Model_Rate_Result_Method
162: */
163: public function getCheapestRate()
164: {
165: $cheapest = null;
166: $minPrice = 100000;
167: foreach ($this->getAllRates() as $rate) {
168: if (is_numeric($rate->getPrice()) && $rate->getPrice() < $minPrice) {
169: $cheapest = $rate;
170: $minPrice = $rate->getPrice();
171: }
172: }
173: return $cheapest;
174: }
175:
176: /**
177: * Sort rates by price from min to max
178: *
179: * @return Mage_Shipping_Model_Rate_Result
180: */
181: public function sortRatesByPrice()
182: {
183: if (!is_array($this->_rates) || !count($this->_rates)) {
184: return $this;
185: }
186: /* @var $rate Mage_Shipping_Model_Rate_Result_Method */
187: foreach ($this->_rates as $i => $rate) {
188: $tmp[$i] = $rate->getPrice();
189: }
190:
191: natsort($tmp);
192:
193: foreach ($tmp as $i => $price) {
194: $result[] = $this->_rates[$i];
195: }
196:
197: $this->reset();
198: $this->_rates = $result;
199: return $this;
200: }
201:
202: /**
203: * Set price for each rate according to count of packages
204: *
205: * @param int $packageCount
206: * @return Mage_Shipping_Model_Rate_Result
207: */
208: public function updateRatePrice($packageCount)
209: {
210: if ($packageCount > 1) {
211: foreach ($this->_rates as $rate) {
212: $rate->setPrice($rate->getPrice() * $packageCount);
213: }
214: }
215:
216: return $this;
217: }
218: }
219: