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: /**
29: * Sales Quote Address Total abstract model
30: *
31: * @category Mage
32: * @package Mage_Sales
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_Sales_Model_Quote_Address_Total_Abstract
36: {
37: /**
38: * Total Code name
39: *
40: * @var string
41: */
42: protected $_code;
43: protected $_address = null;
44:
45: /**
46: * Various abstract abilities
47: * @var bool
48: */
49: protected $_canAddAmountToAddress = true;
50: protected $_canSetAddressAmount = true;
51:
52: /**
53: * Key for item row total getting
54: *
55: * @var string
56: */
57: protected $_itemRowTotalKey = null;
58:
59: /**
60: * Set total code code name
61: *
62: * @param string $code
63: * @return Mage_Sales_Model_Quote_Address_Total_Abstract
64: */
65: public function setCode($code)
66: {
67: $this->_code = $code;
68: return $this;
69: }
70:
71: /**
72: * Retrieve total code name
73: *
74: * @return unknown
75: */
76: public function getCode()
77: {
78: return $this->_code;
79: }
80:
81: /**
82: * Label getter
83: *
84: * @return string
85: */
86: public function getLabel()
87: {
88: return '';
89: }
90:
91: /**
92: * Collect totals process.
93: *
94: * @param Mage_Sales_Model_Quote_Address $address
95: * @return Mage_Sales_Model_Quote_Address_Total_Abstract
96: */
97: public function collect(Mage_Sales_Model_Quote_Address $address)
98: {
99: $this->_setAddress($address);
100: /**
101: * Reset amounts
102: */
103: $this->_setAmount(0);
104: $this->_setBaseAmount(0);
105: return $this;
106: }
107:
108: /**
109: * Fetch (Retrieve data as array)
110: *
111: * @param Mage_Sales_Model_Quote_Address $address
112: * @return array
113: */
114: public function fetch(Mage_Sales_Model_Quote_Address $address)
115: {
116: $this->_setAddress($address);
117: return array();
118: }
119:
120: /**
121: * Set address shich can be used inside totals calculation
122: *
123: * @param Mage_Sales_Model_Quote_Address $address
124: * @return Mage_Sales_Model_Quote_Address_Total_Abstract
125: */
126: protected function _setAddress(Mage_Sales_Model_Quote_Address $address)
127: {
128: $this->_address = $address;
129: return $this;
130: }
131:
132: /**
133: * Get quote address object
134: *
135: * @throw Mage_Core_Exception if address not declared
136: * @return Mage_Sales_Model_Quote_Address
137: */
138: protected function _getAddress()
139: {
140: if ($this->_address === null) {
141: Mage::throwException(
142: Mage::helper('sales')->__('Address model is not defined.')
143: );
144: }
145: return $this->_address;
146: }
147:
148: /**
149: * Set total model amount value to address
150: *
151: * @param float $amount
152: * @return Mage_Sales_Model_Quote_Address_Total_Abstract
153: */
154: protected function _setAmount($amount)
155: {
156: if ($this->_canSetAddressAmount) {
157: $this->_getAddress()->setTotalAmount($this->getCode(), $amount);
158: }
159: return $this;
160: }
161:
162: /**
163: * Set total model base amount value to address
164: *
165: * @param float $amount
166: * @return Mage_Sales_Model_Quote_Address_Total_Abstract
167: */
168: protected function _setBaseAmount($baseAmount)
169: {
170: if ($this->_canSetAddressAmount) {
171: $this->_getAddress()->setBaseTotalAmount($this->getCode(), $baseAmount);
172: }
173: return $this;
174: }
175:
176: /**
177: * Add total model amount value to address
178: *
179: * @param float $amount
180: * @return Mage_Sales_Model_Quote_Address_Total_Abstract
181: */
182: protected function _addAmount($amount)
183: {
184: if ($this->_canAddAmountToAddress) {
185: $this->_getAddress()->addTotalAmount($this->getCode(),$amount);
186: }
187: return $this;
188: }
189:
190: /**
191: * Add total model base amount value to address
192: *
193: * @param float $amount
194: * @return Mage_Sales_Model_Quote_Address_Total_Abstract
195: */
196: protected function _addBaseAmount($baseAmount)
197: {
198: if ($this->_canAddAmountToAddress) {
199: $this->_getAddress()->addBaseTotalAmount($this->getCode(), $baseAmount);
200: }
201: return $this;
202: }
203:
204: /**
205: * Get all items except nominals
206: *
207: * @param Mage_Sales_Model_Quote_Address $address
208: * @return array
209: */
210: protected function _getAddressItems(Mage_Sales_Model_Quote_Address $address)
211: {
212: return $address->getAllNonNominalItems();
213: }
214:
215: /**
216: * Getter for row default total
217: *
218: * @param Mage_Sales_Model_Quote_Item_Abstract $item
219: * @return float
220: */
221: public function getItemRowTotal(Mage_Sales_Model_Quote_Item_Abstract $item)
222: {
223: if (!$this->_itemRowTotalKey) {
224: return 0;
225: }
226: return $item->getDataUsingMethod($this->_itemRowTotalKey);
227: }
228:
229: /**
230: * Getter for row default base total
231: *
232: * @param Mage_Sales_Model_Quote_Item_Abstract $item
233: * @return float
234: */
235: public function getItemBaseRowTotal(Mage_Sales_Model_Quote_Item_Abstract $item)
236: {
237: if (!$this->_itemRowTotalKey) {
238: return 0;
239: }
240: return $item->getDataUsingMethod('base_' . $this->_itemRowTotalKey);
241: }
242:
243: /**
244: * Whether the item row total may be compouded with others
245: *
246: * @param Mage_Sales_Model_Quote_Item_Abstract $item
247: * @return bool
248: */
249: public function getIsItemRowTotalCompoundable(Mage_Sales_Model_Quote_Item_Abstract $item)
250: {
251: if ($item->getData("skip_compound_{$this->_itemRowTotalKey}")) {
252: return false;
253: }
254: return true;
255: }
256:
257: /**
258: * Process model configuration array.
259: * This method can be used for changing models apply sort order
260: *
261: * @param array $config
262: * @param store $store
263: * @return array
264: */
265: public function processConfigArray($config, $store)
266: {
267: return $config;
268: }
269: }
270: