PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/gryzz/crystal_magento
PHP | 300 lines | 210 code | 22 blank | 68 comment | 21 complexity | 937160cc5a61eb5cf608ef8e4a55facf 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) 2010 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. $parent = $this->getParentBlock();
  72. $this->_order = $parent->getOrder();
  73. $this->_source = $parent->getSource();
  74. $store = $this->getStore();
  75. $allowTax = ($this->_source->getTaxAmount() > 0) || ($this->_config->displaySalesZeroTax($store));
  76. $grandTotal = (float) $this->_source->getGrandTotal();
  77. if (!$grandTotal || ($allowTax && !$this->_config->displaySalesTaxWithGrandTotal($store))) {
  78. $this->_addTax();
  79. }
  80. $this->_initSubtotal();
  81. $this->_initShipping();
  82. $this->_initDiscount();
  83. $this->_initGrandTotal();
  84. return $this;
  85. }
  86. /**
  87. * Add tax total string
  88. *
  89. * @param string $after
  90. * @return Mage_Tax_Block_Sales_Order_Tax
  91. */
  92. protected function _addTax($after='discount')
  93. {
  94. $taxTotal = new Varien_Object(array(
  95. 'code' => 'tax',
  96. 'block_name'=> $this->getNameInLayout()
  97. ));
  98. $this->getParentBlock()->addTotal($taxTotal, $after);
  99. return $this;
  100. }
  101. /**
  102. * Get order store object
  103. *
  104. * @return Mage_Core_Model_Store
  105. */
  106. public function getStore()
  107. {
  108. return $this->_order->getStore();
  109. }
  110. protected function _initSubtotal()
  111. {
  112. $store = $this->getStore();
  113. $parent = $this->getParentBlock();
  114. $subtotal = $parent->getTotal('subtotal');
  115. if (!$subtotal) {
  116. return $this;
  117. }
  118. if ($this->_config->displaySalesSubtotalBoth($store)) {
  119. $subtotal = (float) $this->_source->getSubtotal();
  120. $baseSubtotal = (float) $this->_source->getBaseSubtotal();
  121. $subtotalIncl = (float) $this->_source->getSubtotalInclTax();
  122. $baseSubtotalIncl= (float) $this->_source->getBaseSubtotalInclTax();
  123. if (!$subtotalIncl) {
  124. $subtotalIncl = $subtotal+ $this->_source->getTaxAmount()
  125. - $this->_source->getShippingTaxAmount();
  126. }
  127. if (!$baseSubtotalIncl) {
  128. $baseSubtotalIncl = $baseSubtotal + $this->_source->getBaseTaxAmount()
  129. - $this->_source->getBaseShippingTaxAmount();
  130. }
  131. $subtotalIncl = max(0, $subtotalIncl);
  132. $baseSubtotalIncl = max(0, $baseSubtotalIncl);
  133. $totalExcl = new Varien_Object(array(
  134. 'code' => 'subtotal_excl',
  135. 'value' => $subtotal,
  136. 'base_value'=> $baseSubtotal,
  137. 'label' => $this->__('Subtotal (Excl.Tax)')
  138. ));
  139. $totalIncl = new Varien_Object(array(
  140. 'code' => 'subtotal_incl',
  141. 'value' => $subtotalIncl,
  142. 'base_value'=> $baseSubtotalIncl,
  143. 'label' => $this->__('Subtotal (Incl.Tax)')
  144. ));
  145. $parent->addTotal($totalExcl, 'subtotal');
  146. $parent->addTotal($totalIncl, 'subtotal_excl');
  147. $parent->removeTotal('subtotal');
  148. } elseif ($this->_config->displaySalesSubtotalInclTax($store)) {
  149. $subtotalIncl = (float) $this->_source->getSubtotalInclTax();
  150. $baseSubtotalIncl= (float) $this->_source->getBaseSubtotalInclTax();
  151. if (!$subtotalIncl) {
  152. $subtotalIncl = $this->_source->getSubtotal()
  153. + $this->_source->getTaxAmount()
  154. - $this->_source->getShippingTaxAmount();
  155. }
  156. if (!$baseSubtotalIncl) {
  157. $baseSubtotalIncl = $this->_source->getBaseSubtotal()
  158. + $this->_source->getBaseTaxAmount()
  159. - $this->_source->getBaseShippingTaxAmount();
  160. }
  161. $total = $parent->getTotal('subtotal');
  162. if ($total) {
  163. $total->setValue($subtotalIncl);
  164. $total->setBaseValue($baseSubtotalIncl);
  165. }
  166. }
  167. return $this;
  168. }
  169. protected function _initShipping()
  170. {
  171. $store = $this->getStore();
  172. $parent = $this->getParentBlock();
  173. $shipping = $parent->getTotal('shipping');
  174. if (!$shipping) {
  175. return $this;
  176. }
  177. if ($this->_config->displaySalesShippingBoth($store)) {
  178. $shipping = (float) $this->_source->getShippingAmount();
  179. $baseShipping = (float) $this->_source->getBaseShippingAmount();
  180. $shippingIncl = (float) $this->_source->getShippingInclTax();
  181. if (!$shippingIncl) {
  182. $shippingIncl = $shipping + (float) $this->_source->getShippingTaxAmount();
  183. }
  184. $baseShippingIncl = (float) $this->_source->getBaseShippingInclTax();
  185. if (!$baseShippingIncl) {
  186. $baseShippingIncl = $baseShipping + (float) $this->_source->getBaseShippingTaxAmount();
  187. }
  188. $totalExcl = new Varien_Object(array(
  189. 'code' => 'shipping',
  190. 'value' => $shipping,
  191. 'base_value'=> $baseShipping,
  192. 'label' => $this->__('Shipping & Handling (Excl.Tax)')
  193. ));
  194. $totalIncl = new Varien_Object(array(
  195. 'code' => 'shipping_incl',
  196. 'value' => $shippingIncl,
  197. 'base_value'=> $baseShippingIncl,
  198. 'label' => $this->__('Shipping & Handling (Incl.Tax)')
  199. ));
  200. $parent->addTotal($totalExcl, 'shipping');
  201. $parent->addTotal($totalIncl, 'shipping');
  202. } elseif ($this->_config->displaySalesShippingInclTax($store)) {
  203. $shippingIncl = $this->_source->getShippingInclTax();
  204. if (!$shippingIncl) {
  205. $shippingIncl = $this->_source->getShippingAmount()
  206. + $this->_source->getShippingTaxAmount();
  207. }
  208. $baseShippingIncl = $this->_source->getBaseShippingInclTax();
  209. if (!$baseShippingIncl) {
  210. $baseShippingIncl = $this->_source->getBaseShippingAmount()
  211. + $this->_source->getBaseShippingTaxAmount();
  212. }
  213. $total = $parent->getTotal('shipping');
  214. if ($total) {
  215. $total->setValue($shippingIncl);
  216. $total->setBaseValue($baseShippingIncl);
  217. }
  218. }
  219. return $this;
  220. }
  221. protected function _initDiscount()
  222. {
  223. // $store = $this->getStore();
  224. // $parent = $this->getParentBlock();
  225. // if ($this->_config->displaySales) {
  226. //
  227. // } elseif ($this->_config->displaySales) {
  228. //
  229. // }
  230. }
  231. protected function _initGrandTotal()
  232. {
  233. $store = $this->getStore();
  234. $parent = $this->getParentBlock();
  235. $grandototal = $parent->getTotal('grand_total');
  236. if (!$grandototal || !(float)$this->_source->getGrandTotal()) {
  237. return $this;
  238. }
  239. if ($this->_config->displaySalesTaxWithGrandTotal($store)) {
  240. $grandtotal = $this->_source->getGrandTotal();
  241. $baseGrandtotal = $this->_source->getBaseGrandTotal();
  242. $grandtotalExcl = $grandtotal - $this->_source->getTaxAmount();
  243. $baseGrandtotalExcl = $baseGrandtotal - $this->_source->getBaseTaxAmount();
  244. $grandtotalExcl = max($grandtotalExcl, 0);
  245. $baseGrandtotalExcl = max($baseGrandtotalExcl, 0);
  246. $totalExcl = new Varien_Object(array(
  247. 'code' => 'grand_total',
  248. 'strong' => true,
  249. 'value' => $grandtotalExcl,
  250. 'base_value'=> $baseGrandtotalExcl,
  251. 'label' => $this->__('Grand Total (Excl.Tax)')
  252. ));
  253. $totalIncl = new Varien_Object(array(
  254. 'code' => 'grand_total_incl',
  255. 'strong' => true,
  256. 'value' => $grandtotal,
  257. 'base_value'=> $baseGrandtotal,
  258. 'label' => $this->__('Grand Total (Incl.Tax)')
  259. ));
  260. $parent->addTotal($totalExcl, 'grand_total');
  261. $this->_addTax('grand_total');
  262. $parent->addTotal($totalIncl, 'tax');
  263. }
  264. return $this;
  265. }
  266. public function getOrder()
  267. {
  268. return $this->_order;
  269. }
  270. public function getLabelProperties()
  271. {
  272. return $this->getParentBlock()->getLabelProperties();
  273. }
  274. public function getValueProperties()
  275. {
  276. return $this->getParentBlock()->getValueProperties();
  277. }
  278. }