1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class Mage_Sales_Block_Order_Totals extends Mage_Core_Block_Template
27: {
28: 29: 30: 31: 32: 33: 34: 35:
36: protected $_totals;
37: protected $_order = null;
38:
39: 40: 41: 42: 43:
44: protected function _beforeToHtml()
45: {
46: $this->_initTotals();
47: foreach ($this->getChild() as $child) {
48: if (method_exists($child, 'initTotals')) {
49: $child->initTotals();
50: }
51: }
52: return parent::_beforeToHtml();
53: }
54:
55: 56: 57: 58: 59:
60: public function getOrder()
61: {
62: if ($this->_order === null) {
63: if ($this->hasData('order')) {
64: $this->_order = $this->_getData('order');
65: } elseif (Mage::registry('current_order')) {
66: $this->_order = Mage::registry('current_order');
67: } elseif ($this->getParentBlock()->getOrder()) {
68: $this->_order = $this->getParentBlock()->getOrder();
69: }
70: }
71: return $this->_order;
72: }
73:
74: public function setOrder($order)
75: {
76: $this->_order = $order;
77: return $this;
78: }
79:
80: 81: 82: 83: 84:
85: public function getSource()
86: {
87: return $this->getOrder();
88: }
89:
90: 91: 92: 93: 94:
95: protected function _initTotals()
96: {
97: $source = $this->getSource();
98:
99: $this->_totals = array();
100: $this->_totals['subtotal'] = new Varien_Object(array(
101: 'code' => 'subtotal',
102: 'value' => $source->getSubtotal(),
103: 'label' => $this->__('Subtotal')
104: ));
105:
106:
107: 108: 109:
110: if (!$source->getIsVirtual() && ((float) $source->getShippingAmount() || $source->getShippingDescription()))
111: {
112: $this->_totals['shipping'] = new Varien_Object(array(
113: 'code' => 'shipping',
114: 'field' => 'shipping_amount',
115: 'value' => $this->getSource()->getShippingAmount(),
116: 'label' => $this->__('Shipping & Handling')
117: ));
118: }
119:
120: 121: 122:
123: if (((float)$this->getSource()->getDiscountAmount()) != 0) {
124: if ($this->getSource()->getDiscountDescription()) {
125: $discountLabel = $this->__('Discount (%s)', $source->getDiscountDescription());
126: } else {
127: $discountLabel = $this->__('Discount');
128: }
129: $this->_totals['discount'] = new Varien_Object(array(
130: 'code' => 'discount',
131: 'field' => 'discount_amount',
132: 'value' => $source->getDiscountAmount(),
133: 'label' => $discountLabel
134: ));
135: }
136:
137: $this->_totals['grand_total'] = new Varien_Object(array(
138: 'code' => 'grand_total',
139: 'field' => 'grand_total',
140: 'strong'=> true,
141: 'value' => $source->getGrandTotal(),
142: 'label' => $this->__('Grand Total')
143: ));
144:
145: 146: 147:
148: if ($this->getOrder()->isCurrencyDifferent()) {
149: $this->_totals['base_grandtotal'] = new Varien_Object(array(
150: 'code' => 'base_grandtotal',
151: 'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
152: 'label' => $this->__('Grand Total to be Charged'),
153: 'is_formated' => true,
154: ));
155: }
156: return $this;
157: }
158:
159: 160: 161: 162: 163: 164: 165:
166: public function addTotal(Varien_Object $total, $after=null)
167: {
168: if ($after !== null && $after != 'last' && $after != 'first') {
169: $totals = array();
170: $added = false;
171: foreach ($this->_totals as $code => $item) {
172: $totals[$code] = $item;
173: if ($code == $after) {
174: $added = true;
175: $totals[$total->getCode()] = $total;
176: }
177: }
178: if (!$added) {
179: $last = array_pop($totals);
180: $totals[$total->getCode()] = $total;
181: $totals[$last->getCode()] = $last;
182: }
183: $this->_totals = $totals;
184: } elseif ($after=='last') {
185: $this->_totals[$total->getCode()] = $total;
186: } elseif ($after=='first') {
187: $totals = array($total->getCode()=>$total);
188: $this->_totals = array_merge($totals, $this->_totals);
189: } else {
190: $last = array_pop($this->_totals);
191: $this->_totals[$total->getCode()] = $total;
192: $this->_totals[$last->getCode()] = $last;
193: }
194: return $this;
195: }
196:
197: 198: 199: 200: 201: 202: 203:
204: public function addTotalBefore(Varien_Object $total, $before=null)
205: {
206: if ($before !== null) {
207: if (!is_array($before)) {
208: $before = array($before);
209: }
210: foreach ($before as $beforeTotals) {
211: if (isset($this->_totals[$beforeTotals])) {
212: $totals = array();
213: foreach ($this->_totals as $code => $item) {
214: if ($code == $beforeTotals) {
215: $totals[$total->getCode()] = $total;
216: }
217: $totals[$code] = $item;
218: }
219: $this->_totals = $totals;
220: return $this;
221: }
222: }
223: }
224: $totals = array();
225: $first = array_shift($this->_totals);
226: $totals[$first->getCode()] = $first;
227: $totals[$total->getCode()] = $total;
228: foreach ($this->_totals as $code => $item) {
229: $totals[$code] = $item;
230: }
231: $this->_totals = $totals;
232: return $this;
233: }
234:
235: 236: 237: 238: 239:
240: public function getTotal($code)
241: {
242: if (isset($this->_totals[$code])) {
243: return $this->_totals[$code];
244: }
245: return false;
246: }
247:
248: 249: 250: 251: 252: 253:
254: public function removeTotal($code)
255: {
256: unset($this->_totals[$code]);
257: return $this;
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270:
271: public function applySortOrder($order)
272: {
273: return $this;
274: }
275:
276: 277: 278: 279: 280:
281: public function getTotals($area=null)
282: {
283: $totals = array();
284: if ($area === null) {
285: $totals = $this->_totals;
286: } else {
287: $area = (string)$area;
288: foreach ($this->_totals as $total) {
289: $totalArea = (string) $total->getArea();
290: if ($totalArea == $area) {
291: $totals[] = $total;
292: }
293: }
294: }
295: return $totals;
296: }
297:
298: 299: 300: 301: 302: 303:
304: public function formatValue($total)
305: {
306: if (!$total->getIsFormated()) {
307: return $this->getOrder()->formatPrice($total->getValue());
308: }
309: return $total->getValue();
310: }
311: }
312: