PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Weee/Model/Tax.php

https://gitlab.com/blingbang2016/shop
PHP | 318 lines | 176 code | 29 blank | 113 comment | 24 complexity | c48fa3fc37b885bad8d9404f9a3c769c 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_Weee
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Model to calculate Weee amount
  28. */
  29. class Mage_Weee_Model_Tax extends Mage_Core_Model_Abstract
  30. {
  31. /**
  32. * Including FPT only
  33. */
  34. const DISPLAY_INCL = 0;
  35. /**
  36. * Including FPT and FPT description
  37. */
  38. const DISPLAY_INCL_DESCR = 1;
  39. /**
  40. * Excluding FPT, FPT description, final price
  41. */
  42. const DISPLAY_EXCL_DESCR_INCL = 2;
  43. /**
  44. * Excluding FPT
  45. */
  46. const DISPLAY_EXCL = 3;
  47. /**
  48. * All weee attributes
  49. *
  50. * @var array
  51. */
  52. protected $_allAttributes = null;
  53. /**
  54. * Cache product discounts
  55. *
  56. * @var array
  57. */
  58. protected $_productDiscounts = array();
  59. /**
  60. * Tax helper
  61. *
  62. * @var Mage_Tax_Helper_Data
  63. */
  64. protected $_taxHelper;
  65. /**
  66. * Initialize resource
  67. */
  68. protected function _construct()
  69. {
  70. $this->_init('weee/tax', 'weee/tax');
  71. }
  72. /**
  73. * Initialize tax helper
  74. *
  75. * @param array $args
  76. */
  77. public function __construct(array $args = array())
  78. {
  79. parent::__construct();
  80. $this->_taxHelper = !empty($args['helper']) ? $args['helper'] : Mage::helper('tax');
  81. }
  82. /**
  83. * Calculate weee amount for a product
  84. *
  85. * @param Mage_Catalog_Model_Product $product
  86. * @param Mage_Customer_Model_Address_Abstract $shipping
  87. * @param Mage_Customer_Model_Address_Abstract $billing
  88. * @param mixed $website
  89. * @param boolean $calculateTax
  90. * @param boolean $ignoreDiscount
  91. * @return float
  92. */
  93. public function getWeeeAmount(
  94. $product,
  95. $shipping = null,
  96. $billing = null,
  97. $website = null,
  98. $calculateTax = false,
  99. $ignoreDiscount = false)
  100. {
  101. $amount = 0;
  102. $attributes = $this->getProductWeeeAttributes(
  103. $product,
  104. $shipping,
  105. $billing,
  106. $website,
  107. $calculateTax,
  108. $ignoreDiscount
  109. );
  110. foreach ($attributes as $attribute) {
  111. $amount += $attribute->getAmount();
  112. }
  113. return $amount;
  114. }
  115. /**
  116. * Get a list of Weee attribute codes
  117. *
  118. * @param boolean $forceEnabled
  119. * @return array
  120. */
  121. public function getWeeeAttributeCodes($forceEnabled = false)
  122. {
  123. return $this->getWeeeTaxAttributeCodes($forceEnabled);
  124. }
  125. /**
  126. * Retrieve Weee tax attribute codes
  127. *
  128. * @param bool $forceEnabled
  129. * @return array
  130. */
  131. public function getWeeeTaxAttributeCodes($forceEnabled = false)
  132. {
  133. if (!$forceEnabled && !Mage::helper('weee')->isEnabled()) {
  134. return array();
  135. }
  136. if (is_null($this->_allAttributes)) {
  137. $this->_allAttributes = Mage::getModel('eav/entity_attribute')->getAttributeCodesByFrontendType('weee');
  138. }
  139. return $this->_allAttributes;
  140. }
  141. /**
  142. * Get Weee amounts associated with a product
  143. *
  144. * @param Mage_Catalog_Model_Product $product
  145. * @param Mage_Customer_Model_Address_Abstract $shipping
  146. * @param Mage_Customer_Model_Address_Abstract $billing
  147. * @param mixed $website
  148. * @param boolean $calculateTax
  149. * @param boolean $ignoreDiscount
  150. * @return array|\Varien_Object
  151. */
  152. public function getProductWeeeAttributes(
  153. $product,
  154. $shipping = null,
  155. $billing = null,
  156. $website = null,
  157. $calculateTax = null,
  158. $ignoreDiscount = false)
  159. {
  160. $result = array();
  161. $allWeee = $this->getWeeeTaxAttributeCodes();
  162. if (!$allWeee) {
  163. return $result;
  164. }
  165. $websiteId = Mage::app()->getWebsite($website)->getId();
  166. $store = Mage::app()->getWebsite($website)->getDefaultGroup()->getDefaultStore();
  167. $customer = null;
  168. if ($shipping) {
  169. $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
  170. $customer = $shipping->getQuote()->getCustomer();
  171. } else {
  172. $customerTaxClass = null;
  173. }
  174. $calculator = Mage::getModel('tax/calculation');
  175. if ($customer) {
  176. $calculator->setCustomer($customer);
  177. }
  178. $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store);
  179. $currentPercent = $product->getTaxPercent();
  180. if (!$currentPercent) {
  181. $currentPercent = Mage::getSingleton('tax/calculation')->getRate(
  182. $rateRequest->setProductClassId($product->getTaxClassId()));
  183. }
  184. $discountPercent = 0;
  185. if (!$ignoreDiscount && Mage::helper('weee')->isDiscounted($store)) {
  186. $discountPercent = $this->_getDiscountPercentForProduct($product);
  187. }
  188. $productAttributes = $product->getTypeInstance(true)->getSetAttributes($product);
  189. foreach ($productAttributes as $code => $attribute) {
  190. if (in_array($code, $allWeee)) {
  191. $attributeSelect = $this->getResource()->getReadConnection()->select();
  192. $attributeSelect
  193. ->from($this->getResource()->getTable('weee/tax'), 'value')
  194. ->where('attribute_id = ?', (int)$attribute->getId())
  195. ->where('website_id IN(?)', array($websiteId, 0))
  196. ->where('country = ?', $rateRequest->getCountryId())
  197. ->where('state IN(?)', array($rateRequest->getRegionId(), '*'))
  198. ->where('entity_id = ?', (int)$product->getId())
  199. ->limit(1);
  200. $order = array('state ' . Varien_Db_Select::SQL_DESC, 'website_id ' . Varien_Db_Select::SQL_DESC);
  201. $attributeSelect->order($order);
  202. $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect);
  203. if ($value) {
  204. if ($discountPercent) {
  205. $value = Mage::app()->getStore()->roundPrice($value - ($value * $discountPercent / 100));
  206. }
  207. $taxAmount = 0;
  208. $amount = $value;
  209. if ($calculateTax && Mage::helper('weee')->isTaxable($store)) {
  210. if ($this->_taxHelper->isCrossBorderTradeEnabled($store)) {
  211. $defaultPercent = $currentPercent;
  212. } else {
  213. $defaultRateRequest = $calculator->getDefaultRateRequest($store);
  214. $defaultPercent = Mage::getModel('tax/calculation')
  215. ->getRate($defaultRateRequest
  216. ->setProductClassId($product->getTaxClassId()));
  217. }
  218. if (Mage::helper('weee')->isTaxIncluded($store)) {
  219. $taxAmount = Mage::app()->getStore()
  220. ->roundPrice($value / (100 + $defaultPercent) * $currentPercent);
  221. $amount = $amount - $taxAmount;
  222. } else {
  223. $appliedRates = Mage::getModel('tax/calculation')->getAppliedRates($rateRequest);
  224. if (count($appliedRates) > 1) {
  225. $taxAmount = 0;
  226. foreach ($appliedRates as $appliedRate) {
  227. $taxRate = $appliedRate['percent'];
  228. $taxAmount += Mage::app()->getStore()->roundPrice($value * $taxRate / 100);
  229. }
  230. } else {
  231. $taxAmount = Mage::app()->getStore()->roundPrice($value * $currentPercent / 100);
  232. }
  233. }
  234. }
  235. $one = new Varien_Object();
  236. $one->setName(Mage::helper('catalog')->__($attribute->getFrontend()->getLabel()))
  237. ->setAmount($amount)
  238. ->setTaxAmount($taxAmount)
  239. ->setCode($attribute->getAttributeCode());
  240. $result[] = $one;
  241. }
  242. }
  243. }
  244. return $result;
  245. }
  246. /**
  247. * Get discount percentage for a product
  248. *
  249. * @param Mage_Catalog_Model_Product $product
  250. * @return int
  251. */
  252. protected function _getDiscountPercentForProduct($product)
  253. {
  254. $website = Mage::app()->getStore()->getWebsiteId();
  255. $group = Mage::getSingleton('customer/session')->getCustomerGroupId();
  256. $key = implode('-', array($website, $group, $product->getId()));
  257. if (!isset($this->_productDiscounts[$key])) {
  258. $this->_productDiscounts[$key] = (int) $this->getResource()
  259. ->getProductDiscountPercent($product->getId(), $website, $group);
  260. }
  261. $value = $this->_productDiscounts[$key];
  262. if ($value) {
  263. return 100 - min(100, max(0, $value));
  264. } else {
  265. return 0;
  266. }
  267. }
  268. /**
  269. * Update discounts for FPT amounts of all products
  270. *
  271. * @return Mage_Weee_Model_Tax
  272. */
  273. public function updateDiscountPercents()
  274. {
  275. $this->getResource()->updateDiscountPercents();
  276. return $this;
  277. }
  278. /**
  279. * Update discounts for FPT amounts base on products condiotion
  280. *
  281. * @param mixed $products
  282. * @return Mage_Weee_Model_Tax
  283. */
  284. public function updateProductsDiscountPercent($products)
  285. {
  286. $this->getResource()->updateProductsDiscountPercent($products);
  287. return $this;
  288. }
  289. }