Example #1
0
 public function tax_calculation()
 {
     $items = Mage::getModel('tax/calculation')->getCollection()->getItems();
     foreach ($items as $item) {
         $item->delete();
     }
     $this->conn->query('ALTER TABLE `tax_calculation` AUTO_INCREMENT=1');
     //mage::D($this->objects['tax_calculation']); die;
     foreach ($this->objects['tax_calculation'] as $item) {
         $item['tax_calculation_rule_id'] = Mage::getModel('tax/calculation_rule')->getCollection()->addFieldToFilter('code', $item['tax_calculation_rule_id'])->getFirstItem()->getTaxCalculationRuleId();
         $item['tax_calculation_rate_id'] = Mage::getModel('tax/calculation_rate')->getCollection()->addFieldToFilter('code', $item['tax_calculation_rate_id'])->getFirstItem()->getTaxCalculationRateId();
         $item['customer_tax_class_id'] = Mage::getModel('tax/class')->getCollection()->addFieldToFilter('class_type', 'CUSTOMER')->addFieldToFilter('class_name', $item['customer_tax_class_id'])->getFirstItem()->getClassId();
         $item['product_tax_class_id'] = Mage::getModel('tax/class')->getCollection()->addFieldToFilter('class_type', 'PRODUCT')->addFieldToFilter('class_name', $item['product_tax_class_id'])->getFirstItem()->getClassId();
         //mage::d($item); die;
         $model = new Mage_Tax_Model_Calculation();
         foreach ($item as $key => $value) {
             $model->setData($key, $value);
         }
         try {
             $model->save();
         } catch (Exception $e) {
             mage::d($e->getMessage());
             //die;
         }
     }
 }
Example #2
0
 /**
  * Get information about tax rates applied to request
  *
  * @param   Varien_Object $request
  * @return  array
  */
 public function getAppliedRates($request)
 {
     if ($this->_getDataHelper()->isServiceEnabled($this->_getStore($request))) {
         return array();
     }
     return parent::getAppliedRates($request);
 }
Example #3
0
 /**
  * Round price based on tax rounding settings
  *
  * @param float $price
  * @param string $rate
  * @param bool $direction
  * @param string $type
  * @return float
  */
 protected function _round($price, $rate, $direction, $type = 'regular')
 {
     if (!$price) {
         return $this->_calculator->round($price);
     }
     $deltas = $this->_address->getRoundingDeltas();
     $key = $type . $direction;
     $rate = (string) $rate;
     $delta = isset($deltas[$key][$rate]) ? $deltas[$key][$rate] : 0;
     return $this->_calculator->round($price + $delta);
 }
Example #4
0
 /**
  * Round price based on previous rounding operation delta
  *
  * @param float $price
  * @param string $rate
  * @param bool $direction price including or excluding tax
  * @param string $type
  * @return float
  */
 protected function _deltaRound($price, $rate, $direction, $type = 'regular')
 {
     if ($price) {
         $rate = (string) $rate;
         $type = $type . $direction;
         $delta = isset($this->_roundingDeltas[$type][$rate]) ? $this->_roundingDeltas[$type][$rate] : 0;
         $price += $delta;
         $this->_roundingDeltas[$type][$rate] = $price - $this->_calculator->round($price);
         $price = $this->_calculator->round($price);
     }
     return $price;
 }
Example #5
0
 /**
  * Round price based on previous rounding operation delta
  *
  * @param float $price
  * @param string $rate
  * @param bool $direction price including or excluding tax
  * @param string $type
  * @return float
  */
 protected function _deltaRound($price, $rate, $direction, $type = 'regular')
 {
     if ($price) {
         $rate = (string) $rate;
         $type = $type . $direction;
         // initialize the delta to a small number to avoid non-deterministic behavior with rounding of 0.5
         $delta = isset($this->_roundingDeltas[$type][$rate]) ? $this->_roundingDeltas[$type][$rate] : 1.0E-6;
         $price += $delta;
         $this->_roundingDeltas[$type][$rate] = $price - $this->_calculator->round($price);
         $price = $this->_calculator->round($price);
     }
     return $price;
 }
Example #6
0
 public function getRateRequest($shippingAddress = null, $billingAddress = null, $customerTaxClass = null, $store = null)
 {
     $request = parent::getRateRequest($shippingAddress, $billingAddress, $customerTaxClass, $store);
     $countryCode = Mage::getSingleton('geoip/country')->getCountry();
     if ($countryCode) {
         if (is_null($shippingAddress) && is_null($billingAddress) || $shippingAddress === false && $billingAddress === false) {
             $request->setCountryId($countryCode);
             $request->setRegionId(0);
             $request->setPostcode('*');
         } elseif (!$shippingAddress->getCountryId() && !$billingAddress->getCountryId()) {
             $request->setCountryId($countryCode);
             $request->setRegionId(0);
             $request->setPostcode('*');
         }
     }
     return $request;
 }
Example #7
0
 /**                                    
  * Calculates tax amounts for the row item using $this->_calculator
  *
  * @param Mage_Sales_Model_Quote_Item $item
  * @return $this
  */
 protected function _calcTaxAmounts(&$item)
 {
     //@nelkaake -a 16/11/10: Calculator only works in magento 1.4 and up.
     if (!Mage::helper('rewards/version')->isMageVersionAtLeast('1.4.2')) {
         return $this;
     }
     // @nelkaake -a 16/11/10: Tax calculation methods
     $tax = $this->_calculator->calcTaxAmount($item->getRowTotal(), $item->getTaxPercent(), false);
     $baseTax = $this->_calculator->calcTaxAmount($item->getBaseRowTotal(), $item->getTaxPercent(), false);
     $item->setTaxAmount($tax);
     $item->setBaseTaxAmount($baseTax);
     $item->setTaxableAmount($item->getRowTotal());
     $item->setBaseTaxableAmount($item->getBaseRowTotal());
     //@nelkaake -a 26/01/11: We don't set this so that we can later use the row total including tax to find out
     // how much of the order was discounted due to catalog redemption rules.
     $item->setRowTotalInclTax($item->getRowTotal() + $tax);
     $item->setBaseRowTotalInclTax($item->getBaseRowTotal() + $baseTax);
     return $this;
 }
Example #8
0
 /**
  * Get request for fetching address tax rate
  *
  * @deprecated after 1.4.0.0
  * @param   Mage_Sales_Model_Quote_Address $address
  * @return  Varien_Object
  */
 protected function _getAddressTaxRequest($address)
 {
     $addressTaxRequest = $this->_calculator->getRateRequest($address, $address->getQuote()->getBillingAddress(), $address->getQuote()->getCustomerTaxClassId(), $address->getQuote()->getStore());
     return $addressTaxRequest;
 }
 /**
  * Recollect item price and row total using after taxes subtract.
  * Declare item price including tax attributes
  *
  * @deprecated after 1.4.1
  *
  * @param   Mage_Sales_Model_Quote_Address $address
  * @param   Mage_Sales_Model_Quote_Item_Abstract $item
  *
  * @return  Mage_Tax_Model_Sales_Total_Quote_Subtotal
  */
 protected function _recollectItem($address, Mage_Sales_Model_Quote_Item_Abstract $item)
 {
     $store = $address->getQuote()->getStore();
     $request = $this->_getStoreTaxRequest($address);
     $request->setProductClassId($item->getProduct()->getTaxClassId());
     $rate = $this->_calculator->getRate($request);
     $qty = $item->getTotalQty();
     $price = $taxPrice = $item->getCalculationPriceOriginal();
     $basePrice = $baseTaxPrice = $item->getBaseCalculationPriceOriginal();
     $subtotal = $taxSubtotal = $item->getRowTotal();
     $baseSubtotal = $baseTaxSubtotal = $item->getBaseRowTotal();
     if ($this->_config->discountTax($store)) {
         $item->setDiscountCalculationPrice($price);
         $item->setBaseDiscountCalculationPrice($basePrice);
     }
     /**
      * Use original price for tax calculation
      */
     if ($item->hasCustomPrice() && !$this->_helper->applyTaxOnCustomPrice($store)) {
         $taxPrice = $item->getOriginalPrice();
         $baseTaxPrice = $item->getBaseOriginalPrice();
         $taxSubtotal = $taxPrice * $qty;
         $baseTaxSubtotal = $baseTaxPrice * $qty;
     }
     if ($this->_areTaxRequestsSimilar) {
         $item->setRowTotalInclTax($subtotal);
         $item->setBaseRowTotalInclTax($baseSubtotal);
         $item->setPriceInclTax($price);
         $item->setBasePriceInclTax($basePrice);
         $item->setTaxCalcPrice($taxPrice);
         $item->setBaseTaxCalcPrice($baseTaxPrice);
         $item->setTaxCalcRowTotal($taxSubtotal);
         $item->setBaseTaxCalcRowTotal($baseTaxSubtotal);
     }
     $this->_subtotalInclTax += $subtotal;
     $this->_baseSubtotalInclTax += $baseSubtotal;
     if ($this->_config->getAlgorithm($store) == Mage_Tax_Model_Calculation::CALC_UNIT_BASE) {
         $taxAmount = $this->_calculator->calcTaxAmount($taxPrice, $rate, true);
         $baseTaxAmount = $this->_calculator->calcTaxAmount($baseTaxPrice, $rate, true);
         $unitPrice = $this->_calculator->round($price - $taxAmount);
         $baseUnitPrice = $this->_calculator->round($basePrice - $baseTaxAmount);
         $subtotal = $this->_calculator->round($unitPrice * $qty);
         $baseSubtotal = $this->_calculator->round($baseUnitPrice * $qty);
     } else {
         $taxAmount = $this->_calculator->calcTaxAmount($taxSubtotal, $rate, true, false);
         $baseTaxAmount = $this->_calculator->calcTaxAmount($baseTaxSubtotal, $rate, true, false);
         $unitPrice = ($subtotal - $taxAmount) / $qty;
         $baseUnitPrice = ($baseSubtotal - $baseTaxAmount) / $qty;
         $subtotal = $this->_calculator->round($subtotal - $taxAmount);
         $baseSubtotal = $this->_calculator->round($baseSubtotal - $baseTaxAmount);
     }
     if ($item->hasCustomPrice()) {
         $item->setCustomPrice($unitPrice);
         $item->setBaseCustomPrice($baseUnitPrice);
     }
     $item->setPrice($baseUnitPrice);
     $item->setOriginalPrice($unitPrice);
     $item->setBasePrice($baseUnitPrice);
     $item->setRowTotal($subtotal);
     $item->setBaseRowTotal($baseSubtotal);
     return $this;
 }
Example #10
0
 /**
  * Calculate tax for amount
  *
  * @param   float $price
  * @param   float $taxRate
  * @return  float
  */
 protected function _calcTaxAmount($price)
 {
     return $this->_taxCalculationModel->calcTaxAmount($price, $this->_rate);
 }
 /**
  * Aggregate row totals per tax rate in array
  *
  * @param   Mage_Sales_Model_Quote_Item_Abstract $item
  * @param   float $rate
  * @param   array $taxGroups
  * @return  Mage_Tax_Model_Sales_Total_Quote
  */
 protected function _aggregateTaxPerRate($item, $rate, &$taxGroups)
 {
     $store = $item->getStore();
     $inclTax = $this->_usePriceIncludeTax($store);
     if ($inclTax) {
         $subtotal = $item->getTaxCalcRowTotal();
         $baseSubtotal = $item->getBaseTaxCalcRowTotal();
     } else {
         if ($item->hasCustomPrice() && $this->_helper->applyTaxOnCustomPrice($store)) {
             $subtotal = $item->getRowTotal();
             $baseSubtotal = $item->getBaseRowTotal();
         } else {
             $subtotal = $item->getTotalQty() * $item->getOriginalPrice();
             $baseSubtotal = $item->getTotalQty() * $item->getBaseOriginalPrice();
         }
     }
     $discountAmount = $item->getDiscountAmount();
     $baseDiscountAmount = $item->getBaseDiscountAmount();
     $qty = $item->getTotalQty();
     $rateKey = (string) $rate;
     /**
      * Add extra amounts which can be taxable too
      */
     $calcTotal = $subtotal + $item->getExtraRowTaxableAmount();
     $baseCalcTotal = $baseSubtotal + $item->getBaseExtraRowTaxableAmount();
     $item->setTaxPercent($rate);
     if (!isset($taxGroups[$rateKey]['totals'])) {
         $taxGroups[$rateKey]['totals'] = array();
     }
     if (!isset($taxGroups[$rateKey]['totals'])) {
         $taxGroups[$rateKey]['base_totals'] = array();
     }
     $calculationSequence = $this->_helper->getCalculationSequence($store);
     switch ($calculationSequence) {
         case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL:
             $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false);
             $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false);
             break;
         case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL:
             $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false);
             $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false);
             $discountPrice = $inclTax ? $subtotal / $qty : ($subtotal + $rowTax) / $qty;
             $baseDiscountPrice = $inclTax ? $baseSubtotal / $qty : ($baseSubtotal + $baseRowTax) / $qty;
             $item->setDiscountCalculationPrice($discountPrice);
             $item->setBaseDiscountCalculationPrice($baseDiscountPrice);
             break;
         case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL:
         case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL:
             $calcTotal = $calcTotal - $discountAmount;
             $baseCalcTotal = $baseCalcTotal - $baseDiscountAmount;
             $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false);
             $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false);
             break;
     }
     /**
      * "Delta" rounding
      */
     $delta = isset($this->_roundingDeltas[$rateKey]) ? $this->_roundingDeltas[$rateKey] : 0;
     $baseDelta = isset($this->_baseRoundingDeltas[$rateKey]) ? $this->_baseRoundingDeltas[$rateKey] : 0;
     $rowTax += $delta;
     $baseRowTax += $baseDelta;
     $this->_roundingDeltas[$rateKey] = $rowTax - $this->_calculator->round($rowTax);
     $this->_baseRoundingDeltas[$rateKey] = $baseRowTax - $this->_calculator->round($baseRowTax);
     $rowTax = $this->_calculator->round($rowTax);
     $baseRowTax = $this->_calculator->round($baseRowTax);
     /**
      * Renew item amounts in case if we are working with price include tax
      */
     if ($inclTax) {
         $unitTax = $this->_calculator->round($rowTax / $qty);
         $baseUnitTax = $this->_calculator->round($baseRowTax / $qty);
         if ($item->hasCustomPrice()) {
             $item->setCustomPrice($item->getPriceInclTax() - $unitTax);
             $item->setBaseCustomPrice($item->getBasePriceInclTax() - $baseUnitTax);
         } else {
             $item->setOriginalPrice($item->getPriceInclTax() - $unitTax);
             $item->setPrice($item->getBasePriceInclTax() - $baseUnitTax);
             $item->setBasePrice($item->getBasePriceInclTax() - $baseUnitTax);
         }
         $item->setRowTotal($item->getRowTotalInclTax() - $rowTax);
         $item->setBaseRowTotal($item->getBaseRowTotalInclTax() - $baseRowTax);
     }
     $item->setTaxAmount($rowTax);
     $item->setBaseTaxAmount($baseRowTax);
     $taxGroups[$rateKey]['totals'][] = $calcTotal;
     $taxGroups[$rateKey]['base_totals'][] = $baseCalcTotal;
     return $this;
 }