/app/code/core/Mage/Sales/Block/Order/Totals.php

https://github.com/OpenMage/magento-lts · PHP · 316 lines · 184 code · 18 blank · 114 comment · 31 complexity · 603b13b30375e7b4affb824ff0d5f776 MD5 · raw file

  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Sales_Block_Order_Totals extends Mage_Core_Block_Template
  27. {
  28. /**
  29. * Associated array of totals
  30. * array(
  31. * $totalCode => $totalObject
  32. * )
  33. *
  34. * @var array
  35. */
  36. protected $_totals;
  37. protected $_order = null;
  38. /**
  39. * Initialize self totals and children blocks totals before html building
  40. *
  41. * @inheritDoc
  42. */
  43. protected function _beforeToHtml()
  44. {
  45. $this->_initTotals();
  46. foreach ($this->getChild() as $child) {
  47. if (method_exists($child, 'initTotals')) {
  48. $child->initTotals();
  49. }
  50. }
  51. return parent::_beforeToHtml();
  52. }
  53. /**
  54. * Get order object
  55. *
  56. * @return Mage_Sales_Model_Order
  57. */
  58. public function getOrder()
  59. {
  60. if ($this->_order === null) {
  61. if ($this->hasData('order')) {
  62. $this->_order = $this->_getData('order');
  63. } elseif (Mage::registry('current_order')) {
  64. $this->_order = Mage::registry('current_order');
  65. } elseif ($this->getParentBlock()->getOrder()) {
  66. $this->_order = $this->getParentBlock()->getOrder();
  67. }
  68. }
  69. return $this->_order;
  70. }
  71. /**
  72. * @param Mage_Sales_Model_Order $order
  73. * @return $this
  74. */
  75. public function setOrder($order)
  76. {
  77. $this->_order = $order;
  78. return $this;
  79. }
  80. /**
  81. * Get totals source object
  82. *
  83. * @return Mage_Sales_Model_Order
  84. */
  85. public function getSource()
  86. {
  87. return $this->getOrder();
  88. }
  89. /**
  90. * Initialize order totals array
  91. *
  92. * @return $this
  93. */
  94. protected function _initTotals()
  95. {
  96. $source = $this->getSource();
  97. $this->_totals = array();
  98. $this->_totals['subtotal'] = new Varien_Object(array(
  99. 'code' => 'subtotal',
  100. 'value' => $source->getSubtotal(),
  101. 'label' => $this->__('Subtotal')
  102. ));
  103. /**
  104. * Add shipping
  105. */
  106. if (!$source->getIsVirtual() && ((float) $source->getShippingAmount() || $source->getShippingDescription())) {
  107. $this->_totals['shipping'] = new Varien_Object(array(
  108. 'code' => 'shipping',
  109. 'field' => 'shipping_amount',
  110. 'value' => $this->getSource()->getShippingAmount(),
  111. 'label' => $this->__('Shipping & Handling')
  112. ));
  113. }
  114. /**
  115. * Add discount
  116. */
  117. if (((float)$this->getSource()->getDiscountAmount()) != 0) {
  118. if ($this->getSource()->getDiscountDescription()) {
  119. $discountLabel = $this->__('Discount (%s)', $source->getDiscountDescription());
  120. } else {
  121. $discountLabel = $this->__('Discount');
  122. }
  123. $this->_totals['discount'] = new Varien_Object(array(
  124. 'code' => 'discount',
  125. 'field' => 'discount_amount',
  126. 'value' => $source->getDiscountAmount(),
  127. 'label' => $discountLabel
  128. ));
  129. }
  130. $this->_totals['grand_total'] = new Varien_Object(array(
  131. 'code' => 'grand_total',
  132. 'field' => 'grand_total',
  133. 'strong'=> true,
  134. 'value' => $source->getGrandTotal(),
  135. 'label' => $this->__('Grand Total')
  136. ));
  137. /**
  138. * Base grandtotal
  139. */
  140. if ($this->getOrder()->isCurrencyDifferent()) {
  141. $this->_totals['base_grandtotal'] = new Varien_Object(array(
  142. 'code' => 'base_grandtotal',
  143. 'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
  144. 'label' => $this->__('Grand Total to be Charged'),
  145. 'is_formated' => true,
  146. ));
  147. }
  148. return $this;
  149. }
  150. /**
  151. * Add new total to totals array after specific total or before last total by default
  152. *
  153. * @param Varien_Object $total
  154. * @param null|string $after accepted values: 'first', 'last'
  155. * @return Mage_Sales_Block_Order_Totals
  156. */
  157. public function addTotal(Varien_Object $total, $after = null)
  158. {
  159. if ($after !== null && $after != 'last' && $after != 'first') {
  160. $totals = array();
  161. $added = false;
  162. foreach ($this->_totals as $code => $item) {
  163. $totals[$code] = $item;
  164. if ($code == $after) {
  165. $added = true;
  166. $totals[$total->getCode()] = $total;
  167. }
  168. }
  169. if (!$added) {
  170. $last = array_pop($totals);
  171. $totals[$total->getCode()] = $total;
  172. $totals[$last->getCode()] = $last;
  173. }
  174. $this->_totals = $totals;
  175. } elseif ($after=='last') {
  176. $this->_totals[$total->getCode()] = $total;
  177. } elseif ($after=='first') {
  178. $totals = array($total->getCode()=>$total);
  179. $this->_totals = array_merge($totals, $this->_totals);
  180. } else {
  181. $last = array_pop($this->_totals);
  182. $this->_totals[$total->getCode()] = $total;
  183. $this->_totals[$last->getCode()] = $last;
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Add new total to totals array before specific total or after first total by default
  189. *
  190. * @param Varien_Object $total
  191. * @param null|array|string $before
  192. * @return Mage_Sales_Block_Order_Totals
  193. */
  194. public function addTotalBefore(Varien_Object $total, $before = null)
  195. {
  196. if ($before !== null) {
  197. if (!is_array($before)) {
  198. $before = array($before);
  199. }
  200. foreach ($before as $beforeTotals) {
  201. if (isset($this->_totals[$beforeTotals])) {
  202. $totals = array();
  203. foreach ($this->_totals as $code => $item) {
  204. if ($code == $beforeTotals) {
  205. $totals[$total->getCode()] = $total;
  206. }
  207. $totals[$code] = $item;
  208. }
  209. $this->_totals = $totals;
  210. return $this;
  211. }
  212. }
  213. }
  214. $totals = array();
  215. $first = array_shift($this->_totals);
  216. $totals[$first->getCode()] = $first;
  217. $totals[$total->getCode()] = $total;
  218. foreach ($this->_totals as $code => $item) {
  219. $totals[$code] = $item;
  220. }
  221. $this->_totals = $totals;
  222. return $this;
  223. }
  224. /**
  225. * Get Total object by code
  226. *
  227. * @param string $code
  228. * @return Varien_Object|false
  229. */
  230. public function getTotal($code)
  231. {
  232. if (isset($this->_totals[$code])) {
  233. return $this->_totals[$code];
  234. }
  235. return false;
  236. }
  237. /**
  238. * Delete total by specific
  239. *
  240. * @param string $code
  241. * @return Mage_Sales_Block_Order_Totals
  242. */
  243. public function removeTotal($code)
  244. {
  245. unset($this->_totals[$code]);
  246. return $this;
  247. }
  248. /**
  249. * Apply sort orders to totals array.
  250. * Array should have next structure
  251. * array(
  252. * $totalCode => $totalSortOrder
  253. * )
  254. *
  255. *
  256. * @param array $order
  257. * @return Mage_Sales_Block_Order_Totals
  258. */
  259. public function applySortOrder($order)
  260. {
  261. return $this;
  262. }
  263. /**
  264. * get totals array for visualization
  265. *
  266. * @param null|string $area
  267. * @return array
  268. */
  269. public function getTotals($area = null)
  270. {
  271. $totals = array();
  272. if ($area === null) {
  273. $totals = $this->_totals;
  274. } else {
  275. $area = (string)$area;
  276. foreach ($this->_totals as $total) {
  277. $totalArea = (string) $total->getArea();
  278. if ($totalArea == $area) {
  279. $totals[] = $total;
  280. }
  281. }
  282. }
  283. return $totals;
  284. }
  285. /**
  286. * Format total value based on order currency
  287. *
  288. * @param Varien_Object $total
  289. * @return string
  290. */
  291. public function formatValue($total)
  292. {
  293. if (!$total->getIsFormated()) {
  294. return $this->getOrder()->formatPrice($total->getValue());
  295. }
  296. return $total->getValue();
  297. }
  298. }