00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Tax_Model_Calculation extends Mage_Core_Model_Abstract
00033 {
00034 protected $_rates = array();
00035 protected $_ctc = array();
00036 protected $_ptc = array();
00037
00038 protected $_rateCache = array();
00039 protected $_rateCalculationProcess = array();
00040
00041 protected function _construct()
00042 {
00043 $this->_init('tax/calculation');
00044 }
00045
00046 public function deleteByRuleId($ruleId)
00047 {
00048 $this->getResource()->deleteByRuleId($ruleId);
00049 return $this;
00050 }
00051
00052 public function getRates($ruleId)
00053 {
00054 if (!isset($this->_rates[$ruleId])) {
00055 $this->_rates[$ruleId] = $this->getResource()->getDistinct('tax_calculation_rate_id', $ruleId);
00056 }
00057 return $this->_rates[$ruleId];
00058 }
00059
00060 public function getCustomerTaxClasses($ruleId)
00061 {
00062 if (!isset($this->_ctc[$ruleId])) {
00063 $this->_ctc[$ruleId] = $this->getResource()->getDistinct('customer_tax_class_id', $ruleId);
00064 }
00065 return $this->_ctc[$ruleId];
00066 }
00067
00068 public function getProductTaxClasses($ruleId)
00069 {
00070 if (!isset($this->_ptc[$ruleId])) {
00071 $this->_ptc[$ruleId] = $this->getResource()->getDistinct('product_tax_class_id', $ruleId);
00072 }
00073 return $this->_ptc[$ruleId];
00074 }
00075
00076 protected function _formCalculationProcess()
00077 {
00078 $title = $this->getRateTitle();
00079 $value = $this->getRateValue();
00080 $id = $this->getRateId();
00081
00082 $rate = array('code'=>$title, 'title'=>$title, 'percent'=>$value, 'position'=>1, 'priority'=>1);
00083
00084 $process = array();
00085 $process['percent'] = $value;
00086 $process['id'] = "{$id}-{$value}";
00087 $process['rates'][] = $rate;
00088
00089 return array($process);
00090 }
00091
00092 public function getRate($request)
00093 {
00094 if (!$request->getCountryId() || !$request->getCustomerClassId() || !$request->getProductClassId()) {
00095 return 0;
00096 }
00097
00098 $cacheKey = "{$request->getProductClassId()}|{$request->getCustomerClassId()}|{$request->getCountryId()}|{$request->getRegionId()}|{$request->getPostcode()}";
00099 if (!isset($this->_rateCache[$cacheKey])) {
00100 $this->unsRateValue();
00101 $this->unsCalculationProcess();
00102 $this->unsEventModuleId();
00103 Mage::dispatchEvent('tax_rate_data_fetch', array('request'=>$this));
00104 if (!$this->hasRateValue()) {
00105 $this->setCalculationProcess($this->_getResource()->getCalculationProcess($request));
00106 $this->setRateValue($this->_getResource()->getRate($request));
00107 } else {
00108 $this->setCalculationProcess($this->_formCalculationProcess());
00109 }
00110 $this->_rateCache[$cacheKey] = $this->getRateValue();
00111 $this->_rateCalculationProcess[$cacheKey] = $this->getCalculationProcess();
00112 }
00113 return $this->_rateCache[$cacheKey];
00114 }
00115
00116 public function getRateRequest($shippingAddress = null, $billingAddress = null, $customerTaxClass = null, $store = null)
00117 {
00118 $address = new Varien_Object();
00119 $session = Mage::getSingleton('customer/session');
00120 $basedOn = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_BASED_ON, $store);
00121 if (($shippingAddress === false && $basedOn == 'shipping') || ($billingAddress === false && $basedOn == 'billing')) {
00122 $basedOn = 'default';
00123 } else {
00124 if ((($billingAddress === false || is_null($billingAddress) || !$billingAddress->getCountryId()) && $basedOn == 'billing') || (($shippingAddress === false || is_null($shippingAddress) || !$shippingAddress->getCountryId()) && $basedOn == 'shipping')){
00125 if (!$session->isLoggedIn()) {
00126 $basedOn = 'default';
00127 } else {
00128 $defBilling = $session->getCustomer()->getDefaultBillingAddress();
00129 $defShipping = $session->getCustomer()->getDefaultShippingAddress();
00130
00131 if ($basedOn == 'billing' && $defBilling && $defBilling->getCountryId()) {
00132 $billingAddress = $defBilling;
00133 } else if ($basedOn == 'shipping' && $defShipping && $defShipping->getCountryId()) {
00134 $shippingAddress = $defShipping;
00135 } else {
00136 $basedOn = 'default';
00137 }
00138 }
00139 }
00140 }
00141
00142 switch ($basedOn) {
00143 case 'billing':
00144 $address = $billingAddress;
00145 break;
00146
00147 case 'shipping':
00148 $address = $shippingAddress;
00149 break;
00150
00151 case 'origin':
00152 $address
00153 ->setCountryId(Mage::getStoreConfig('shipping/origin/country_id', $store))
00154 ->setRegionId(Mage::getStoreConfig('shipping/origin/region_id', $store))
00155 ->setPostcode(Mage::getStoreConfig('shipping/origin/postcode', $store));
00156 break;
00157
00158 case 'default':
00159 $address
00160 ->setCountryId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, $store))
00161 ->setRegionId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_REGION, $store))
00162 ->setPostcode(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, $store));
00163 break;
00164 }
00165
00166 if (is_null($customerTaxClass) && $session->isLoggedIn()) {
00167 $customerTaxClass = $session->getCustomer()->getTaxClassId();
00168 } elseif (($customerTaxClass === false) || !$session->isLoggedIn()) {
00169 $defaultCustomerGroup = Mage::getStoreConfig('customer/create_account/default_group', $store);
00170 $customerTaxClass = Mage::getModel('customer/group')->getTaxClassId($defaultCustomerGroup);
00171 }
00172 $request = new Varien_Object();
00173 $request
00174 ->setCountryId($address->getCountryId())
00175 ->setRegionId($address->getRegionId())
00176 ->setPostcode($address->getPostcode())
00177 ->setStore($store)
00178 ->setCustomerClassId($customerTaxClass);
00179 return $request;
00180 }
00181
00182 protected function _getRates($request, $fieldName, $type)
00183 {
00184 $result = array();
00185 $classes = Mage::getModel('tax/class')->getCollection()
00186 ->addFieldToFilter('class_type', $type)
00187 ->load();
00188 foreach ($classes as $class) {
00189 $request->setData($fieldName, $class->getId());
00190 $result[$class->getId()] = $this->getRate($request);
00191 }
00192
00193 return $result;
00194 }
00195
00196 public function getRatesForAllProductTaxClasses($request)
00197 {
00198 return $this->_getRates($request, 'product_class_id', 'PRODUCT');
00199 }
00200 public function getRatesForAllCustomerTaxClasses($request)
00201 {
00202 return $this->_getRates($request, 'customer_class_id', 'CUSTOMER');
00203 }
00204
00205 public function getAppliedRates($request)
00206 {
00207 $cacheKey = "{$request->getStore()->getId()}|{$request->getProductClassId()}|{$request->getCustomerClassId()}|{$request->getCountryId()}|{$request->getRegionId()}|{$request->getPostcode()}";
00208 if (!isset($this->_rateCalculationProcess[$cacheKey])) {
00209 $this->_rateCalculationProcess[$cacheKey] = $this->_getResource()->getCalculationProcess($request);
00210 }
00211 return $this->_rateCalculationProcess[$cacheKey];
00212 }
00213
00214 public function reproduceProcess($rates)
00215 {
00216 return $this->getResource()->getCalculationProcess(null, $rates);
00217 }
00218
00219 public function getRatesByCustomerTaxClass($customerTaxClass)
00220 {
00221 return $this->getResource()->getRatesByCustomerTaxClass($customerTaxClass);
00222 }
00223
00224 public function getRatesByCustomerAndProductTaxClasses($customerTaxClass, $productTaxClass)
00225 {
00226 return $this->getResource()->getRatesByCustomerTaxClass($customerTaxClass, $productTaxClass);
00227 }
00228 }