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_Checkout
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: class Mage_Checkout_Block_Cart_Totals extends Mage_Checkout_Block_Cart_Abstract
28: {
29: protected $_totalRenderers;
30: protected $_defaultRenderer = 'checkout/total_default';
31: protected $_totals = null;
32:
33: public function getTotals()
34: {
35: if (is_null($this->_totals)) {
36: return parent::getTotals();
37: }
38: return $this->_totals;
39: }
40:
41: public function setTotals($value)
42: {
43: $this->_totals = $value;
44: return $this;
45: }
46:
47: protected function _getTotalRenderer($code)
48: {
49: $blockName = $code.'_total_renderer';
50: $block = $this->getLayout()->getBlock($blockName);
51: if (!$block) {
52: $block = $this->_defaultRenderer;
53: $config = Mage::getConfig()->getNode("global/sales/quote/totals/{$code}/renderer");
54: if ($config) {
55: $block = (string) $config;
56: }
57:
58: $block = $this->getLayout()->createBlock($block, $blockName);
59: }
60: /**
61: * Transfer totals to renderer
62: */
63: $block->setTotals($this->getTotals());
64: return $block;
65: }
66:
67: public function renderTotal($total, $area = null, $colspan = 1)
68: {
69: $code = $total->getCode();
70: if ($total->getAs()) {
71: $code = $total->getAs();
72: }
73: return $this->_getTotalRenderer($code)
74: ->setTotal($total)
75: ->setColspan($colspan)
76: ->setRenderingArea(is_null($area) ? -1 : $area)
77: ->toHtml();
78: }
79:
80: /**
81: * Render totals html for specific totals area (footer, body)
82: *
83: * @param null|string $area
84: * @param int $colspan
85: * @return string
86: */
87: public function renderTotals($area = null, $colspan = 1)
88: {
89: $html = '';
90: foreach($this->getTotals() as $total) {
91: if ($total->getArea() != $area && $area != -1) {
92: continue;
93: }
94: $html .= $this->renderTotal($total, $area, $colspan);
95: }
96: return $html;
97: }
98:
99: /**
100: * Check if we have display grand total in base currency
101: *
102: * @return bool
103: */
104: public function needDisplayBaseGrandtotal()
105: {
106: $quote = $this->getQuote();
107: if ($quote->getBaseCurrencyCode() != $quote->getQuoteCurrencyCode()) {
108: return true;
109: }
110: return false;
111: }
112:
113: /**
114: * Get formated in base currency base grand total value
115: *
116: * @return string
117: */
118: public function displayBaseGrandtotal()
119: {
120: $firstTotal = reset($this->_totals);
121: if ($firstTotal) {
122: $total = $firstTotal->getAddress()->getBaseGrandTotal();
123: return Mage::app()->getStore()->getBaseCurrency()->format($total, array(), true);
124: }
125: return '-';
126: }
127:
128: /**
129: * Get active or custom quote
130: *
131: * @return Mage_Sales_Model_Quote
132: */
133: public function getQuote()
134: {
135: if ($this->getCustomQuote()) {
136: return $this->getCustomQuote();
137: }
138:
139: if (null === $this->_quote) {
140: $this->_quote = $this->getCheckout()->getQuote();
141: }
142: return $this->_quote;
143: }
144: }
145: