PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/sevenly/magento-ce
PHP | 301 lines | 210 code | 22 blank | 69 comment | 21 complexity | 81caac522aae79dde6752ce19f02fe9f 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@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_Tax
  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. * Tax totals modification block. Can be used just as subblock of Mage_Sales_Block_Order_Totals
  28. */
  29. class Mage_Tax_Block_Sales_Order_Tax extends Mage_Core_Block_Template
  30. {
  31. /**
  32. * Tax configuration model
  33. *
  34. * @var Mage_Tax_Model_Config
  35. */
  36. protected $_config;
  37. protected $_order;
  38. protected $_source;
  39. /**
  40. * Initialize configuration object
  41. */
  42. protected function _construct()
  43. {
  44. $this->_config = Mage::getSingleton('tax/config');
  45. }
  46. /**
  47. * Check if we nedd display full tax total info
  48. *
  49. * @return bool
  50. */
  51. public function displayFullSummary()
  52. {
  53. return $this->_config->displaySalesFullSummary($this->getOrder()->getStore());
  54. }
  55. /**
  56. * Get data (totals) source model
  57. *
  58. * @return Varien_Object
  59. */
  60. public function getSource()
  61. {
  62. return $this->_source;
  63. }
  64. /**
  65. * Initialize all order totals relates with tax
  66. *
  67. * @return Mage_Tax_Block_Sales_Order_Tax
  68. */
  69. public function initTotals()
  70. {
  71. /** @var $parent Mage_Adminhtml_Block_Sales_Order_Invoice_Totals */
  72. $parent = $this->getParentBlock();
  73. $this->_order = $parent->getOrder();
  74. $this->_source = $parent->getSource();
  75. $store = $this->getStore();
  76. $allowTax = ($this->_source->getTaxAmount() > 0) || ($this->_config->displaySalesZeroTax($store));
  77. $grandTotal = (float) $this->_source->getGrandTotal();
  78. if (!$grandTotal || ($allowTax && !$this->_config->displaySalesTaxWithGrandTotal($store))) {
  79. $this->_addTax();
  80. }
  81. $this->_initSubtotal();
  82. $this->_initShipping();
  83. $this->_initDiscount();
  84. $this->_initGrandTotal();
  85. return $this;
  86. }
  87. /**
  88. * Add tax total string
  89. *
  90. * @param string $after
  91. * @return Mage_Tax_Block_Sales_Order_Tax
  92. */
  93. protected function _addTax($after='discount')
  94. {
  95. $taxTotal = new Varien_Object(array(
  96. 'code' => 'tax',
  97. 'block_name'=> $this->getNameInLayout()
  98. ));
  99. $this->getParentBlock()->addTotal($taxTotal, $after);
  100. return $this;
  101. }
  102. /**
  103. * Get order store object
  104. *
  105. * @return Mage_Core_Model_Store
  106. */
  107. public function getStore()
  108. {
  109. return $this->_order->getStore();
  110. }
  111. protected function _initSubtotal()
  112. {
  113. $store = $this->getStore();
  114. $parent = $this->getParentBlock();
  115. $subtotal = $parent->getTotal('subtotal');
  116. if (!$subtotal) {
  117. return $this;
  118. }
  119. if ($this->_config->displaySalesSubtotalBoth($store)) {
  120. $subtotal = (float) $this->_source->getSubtotal();
  121. $baseSubtotal = (float) $this->_source->getBaseSubtotal();
  122. $subtotalIncl = (float) $this->_source->getSubtotalInclTax();
  123. $baseSubtotalIncl= (float) $this->_source->getBaseSubtotalInclTax();
  124. if (!$subtotalIncl) {
  125. $subtotalIncl = $subtotal+ $this->_source->getTaxAmount()
  126. - $this->_source->getShippingTaxAmount();
  127. }
  128. if (!$baseSubtotalIncl) {
  129. $baseSubtotalIncl = $baseSubtotal + $this->_source->getBaseTaxAmount()
  130. - $this->_source->getBaseShippingTaxAmount();
  131. }
  132. $subtotalIncl = max(0, $subtotalIncl);
  133. $baseSubtotalIncl = max(0, $baseSubtotalIncl);
  134. $totalExcl = new Varien_Object(array(
  135. 'code' => 'subtotal_excl',
  136. 'value' => $subtotal,
  137. 'base_value'=> $baseSubtotal,
  138. 'label' => $this->__('Subtotal (Excl.Tax)')
  139. ));
  140. $totalIncl = new Varien_Object(array(
  141. 'code' => 'subtotal_incl',
  142. 'value' => $subtotalIncl,
  143. 'base_value'=> $baseSubtotalIncl,
  144. 'label' => $this->__('Subtotal (Incl.Tax)')
  145. ));
  146. $parent->addTotal($totalExcl, 'subtotal');
  147. $parent->addTotal($totalIncl, 'subtotal_excl');
  148. $parent->removeTotal('subtotal');
  149. } elseif ($this->_config->displaySalesSubtotalInclTax($store)) {
  150. $subtotalIncl = (float) $this->_source->getSubtotalInclTax();
  151. $baseSubtotalIncl= (float) $this->_source->getBaseSubtotalInclTax();
  152. if (!$subtotalIncl) {
  153. $subtotalIncl = $this->_source->getSubtotal()
  154. + $this->_source->getTaxAmount()
  155. - $this->_source->getShippingTaxAmount();
  156. }
  157. if (!$baseSubtotalIncl) {
  158. $baseSubtotalIncl = $this->_source->getBaseSubtotal()
  159. + $this->_source->getBaseTaxAmount()
  160. - $this->_source->getBaseShippingTaxAmount();
  161. }
  162. $total = $parent->getTotal('subtotal');
  163. if ($total) {
  164. $total->setValue(max(0, $subtotalIncl));
  165. $total->setBaseValue(max(0, $baseSubtotalIncl));
  166. }
  167. }
  168. return $this;
  169. }
  170. protected function _initShipping()
  171. {
  172. $store = $this->getStore();
  173. $parent = $this->getParentBlock();
  174. $shipping = $parent->getTotal('shipping');
  175. if (!$shipping) {
  176. return $this;
  177. }
  178. if ($this->_config->displaySalesShippingBoth($store)) {
  179. $shipping = (float) $this->_source->getShippingAmount();
  180. $baseShipping = (float) $this->_source->getBaseShippingAmount();
  181. $shippingIncl = (float) $this->_source->getShippingInclTax();
  182. if (!$shippingIncl) {
  183. $shippingIncl = $shipping + (float) $this->_source->getShippingTaxAmount();
  184. }
  185. $baseShippingIncl = (float) $this->_source->getBaseShippingInclTax();
  186. if (!$baseShippingIncl) {
  187. $baseShippingIncl = $baseShipping + (float) $this->_source->getBaseShippingTaxAmount();
  188. }
  189. $totalExcl = new Varien_Object(array(
  190. 'code' => 'shipping',
  191. 'value' => $shipping,
  192. 'base_value'=> $baseShipping,
  193. 'label' => $this->__('Shipping & Handling (Excl.Tax)')
  194. ));
  195. $totalIncl = new Varien_Object(array(
  196. 'code' => 'shipping_incl',
  197. 'value' => $shippingIncl,
  198. 'base_value'=> $baseShippingIncl,
  199. 'label' => $this->__('Shipping & Handling (Incl.Tax)')
  200. ));
  201. $parent->addTotal($totalExcl, 'shipping');
  202. $parent->addTotal($totalIncl, 'shipping');
  203. } elseif ($this->_config->displaySalesShippingInclTax($store)) {
  204. $shippingIncl = $this->_source->getShippingInclTax();
  205. if (!$shippingIncl) {
  206. $shippingIncl = $this->_source->getShippingAmount()
  207. + $this->_source->getShippingTaxAmount();
  208. }
  209. $baseShippingIncl = $this->_source->getBaseShippingInclTax();
  210. if (!$baseShippingIncl) {
  211. $baseShippingIncl = $this->_source->getBaseShippingAmount()
  212. + $this->_source->getBaseShippingTaxAmount();
  213. }
  214. $total = $parent->getTotal('shipping');
  215. if ($total) {
  216. $total->setValue($shippingIncl);
  217. $total->setBaseValue($baseShippingIncl);
  218. }
  219. }
  220. return $this;
  221. }
  222. protected function _initDiscount()
  223. {
  224. // $store = $this->getStore();
  225. // $parent = $this->getParentBlock();
  226. // if ($this->_config->displaySales) {
  227. //
  228. // } elseif ($this->_config->displaySales) {
  229. //
  230. // }
  231. }
  232. protected function _initGrandTotal()
  233. {
  234. $store = $this->getStore();
  235. $parent = $this->getParentBlock();
  236. $grandototal = $parent->getTotal('grand_total');
  237. if (!$grandototal || !(float)$this->_source->getGrandTotal()) {
  238. return $this;
  239. }
  240. if ($this->_config->displaySalesTaxWithGrandTotal($store)) {
  241. $grandtotal = $this->_source->getGrandTotal();
  242. $baseGrandtotal = $this->_source->getBaseGrandTotal();
  243. $grandtotalExcl = $grandtotal - $this->_source->getTaxAmount();
  244. $baseGrandtotalExcl = $baseGrandtotal - $this->_source->getBaseTaxAmount();
  245. $grandtotalExcl = max($grandtotalExcl, 0);
  246. $baseGrandtotalExcl = max($baseGrandtotalExcl, 0);
  247. $totalExcl = new Varien_Object(array(
  248. 'code' => 'grand_total',
  249. 'strong' => true,
  250. 'value' => $grandtotalExcl,
  251. 'base_value'=> $baseGrandtotalExcl,
  252. 'label' => $this->__('Grand Total (Excl.Tax)')
  253. ));
  254. $totalIncl = new Varien_Object(array(
  255. 'code' => 'grand_total_incl',
  256. 'strong' => true,
  257. 'value' => $grandtotal,
  258. 'base_value'=> $baseGrandtotal,
  259. 'label' => $this->__('Grand Total (Incl.Tax)')
  260. ));
  261. $parent->addTotal($totalExcl, 'grand_total');
  262. $this->_addTax('grand_total');
  263. $parent->addTotal($totalIncl, 'tax');
  264. }
  265. return $this;
  266. }
  267. public function getOrder()
  268. {
  269. return $this->_order;
  270. }
  271. public function getLabelProperties()
  272. {
  273. return $this->getParentBlock()->getLabelProperties();
  274. }
  275. public function getValueProperties()
  276. {
  277. return $this->getParentBlock()->getValueProperties();
  278. }
  279. }