Skip to content

Commit

Permalink
[-] BO: Handle combinated taxes on AdminProducts
Browse files Browse the repository at this point in the history
  • Loading branch information
rGaillard committed Aug 12, 2014
1 parent c77a45a commit 51799ba
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,9 @@ $(document).ready(function () {
<div class="col-lg-8">
<script type="text/javascript">
noTax = {if $tax_exclude_taxe_option}true{else}false{/if};
taxesArray = new Array ();
taxesArray[0] = 0;
{foreach $tax_rules_groups as $tax_rules_group}
{if isset($taxesRatesByGroup[$tax_rules_group['id_tax_rules_group']])}
taxesArray[{$tax_rules_group.id_tax_rules_group}] = {$taxesRatesByGroup[$tax_rules_group['id_tax_rules_group']]};
{else}
taxesArray[{$tax_rules_group.id_tax_rules_group}] = 0;
{/if}
taxesArray = new Array();
{foreach $taxesRatesByGroup as $tax_by_group}
taxesArray[{$tax_by_group.id_tax_rules_group}] = {$tax_by_group|json_encode}
{/foreach}
ecotaxTaxRate = {$ecotaxTaxRate / 100};
</script>
Expand Down
32 changes: 30 additions & 2 deletions controllers/admin/AdminProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3088,13 +3088,41 @@ public function initFormPrices($obj)
$product->id_tax_rules_group = (int)Product::getIdTaxRulesGroupMostUsed();
$data->assign('ecotax_tax_excl', 0);
}

$address = new Address();
$address->id_country = (int)$this->context->country->id;
$tax_rules_groups = TaxRulesGroup::getTaxRulesGroups(true);
$tax_rates = array(
0 => array (
'id_tax_rules_group' => 0,
'rates' => array(0),
'computation_method' => 0
)
);

foreach ($tax_rules_groups as $tax_rules_group)
{
$id_tax_rules_group = (int)$tax_rules_group['id_tax_rules_group'];
$tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator();
$tax_rates[$id_tax_rules_group] = array(
'id_tax_rules_group' => $id_tax_rules_group,
'rates' => array(),
'computation_method' => (int)$tax_calculator->computation_method
);

if (isset($tax_calculator->taxes) && count($tax_calculator->taxes))
foreach ($tax_calculator->taxes as $tax)
$tax_rates[$id_tax_rules_group]['rates'][] = (float)$tax->rate;
else
$tax_rates[$id_tax_rules_group]['rates'][] = 0;
}

// prices part
$data->assign(array(
'link' => $this->context->link,
'currency' => $currency = $this->context->currency,
'tax_rules_groups' => TaxRulesGroup::getTaxRulesGroups(true),
'taxesRatesByGroup' => TaxRulesGroup::getAssociatedTaxRatesByIdCountry($this->context->country->id),
'tax_rules_groups' => $tax_rules_groups,
'taxesRatesByGroup' => $tax_rates,
'ecotaxTaxRate' => Tax::getProductEcotaxRate(),
'tax_exclude_taxe_option' => Tax::excludeTaxeOption(),
'ps_use_ecotax' => Configuration::get('PS_USE_ECOTAX'),
Expand Down
85 changes: 71 additions & 14 deletions js/price.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,74 @@ function getTax()
{
if (noTax)
return 0;

var selectedTax = document.getElementById('id_tax_rules_group');
var taxId = selectedTax.options[selectedTax.selectedIndex].value;
return taxesArray[taxId].rates[0];
}

function getTaxes()
{
if (noTax)
taxesArray[taxId];

var selectedTax = document.getElementById('id_tax_rules_group');
var taxId = selectedTax.options[selectedTax.selectedIndex].value;
return taxesArray[taxId];
}

function addTaxes(price)
{
var taxes = getTaxes();
var price_with_taxes = price;
if (taxes.computation_method == 0) {
for (i in taxes.rates) {
price_with_taxes *= (1 + taxes.rates[i] / 100);
break;
}
}
else if (taxes.computation_method == 1) {
var rate = 0;
for (i in taxes.rates) {
rate += taxes.rates[i];
}
price_with_taxes *= (1 + rate / 100);
}
else if (taxes.computation_method == 2) {
for (i in taxes.rates) {
price_with_taxes *= (1 + taxes.rates[i] / 100);
}
}

return price_with_taxes;
}

function removeTaxes(price)
{
var taxes = getTaxes();
var price_without_taxes = price;
if (taxes.computation_method == 0) {
for (i in taxes.rates) {
price_without_taxes /= (1 + taxes.rates[i] / 100);
break;
}
}
else if (taxes.computation_method == 1) {
var rate = 0;
for (i in taxes.rates) {
rate += taxes.rates[i];
}
price_without_taxes /= (1 + rate / 100);
}
else if (taxes.computation_method == 2) {
for (i in taxes.rates) {
price_without_taxes /= (1 + taxes.rates[i] / 100);
}
}

return price_without_taxes;
}

function getEcotaxTaxIncluded()
{
return ps_round(ecotax_tax_excl * (1 + ecotaxTaxRate), 2);
Expand Down Expand Up @@ -59,9 +122,9 @@ function calcPrice()

function calcPriceTI()
{
var tax = getTax();

var priceTE = parseFloat(document.getElementById('priceTEReal').value.replace(/,/g, '.'));
var newPrice = priceTE * ((tax / 100) + 1);
var newPrice = addTaxes(priceTE);

document.getElementById('priceTI').value = (isNaN(newPrice) == true || newPrice < 0) ? '' :
ps_round(newPrice, priceDisplayPrecision);
Expand All @@ -86,9 +149,8 @@ function calcPriceTI()
function calcPriceTE()
{
ecotax_tax_excl = $('#ecotax').val() / (1 + ecotaxTaxRate);
var tax = getTax();
var priceTI = parseFloat(document.getElementById('priceTI').value.replace(/,/g, '.'));
var newPrice = ps_round(priceTI - getEcotaxTaxIncluded(), priceDisplayPrecision) / ((tax / 100) + 1);
var newPrice = removeTaxes(ps_round(priceTI - getEcotaxTaxIncluded(), priceDisplayPrecision));
document.getElementById('priceTE').value = (isNaN(newPrice) == true || newPrice < 0) ? '' :
ps_round(newPrice, 6).toFixed(6);
document.getElementById('priceTEReal').value = (isNaN(newPrice) == true || newPrice < 0) ? 0 : ps_round(newPrice, 9);
Expand All @@ -101,9 +163,8 @@ function calcPriceTE()

function calcImpactPriceTI()
{
var tax = getTax();
var priceTE = parseFloat(document.getElementById('attribute_priceTEReal').value.replace(/,/g, '.'));
var newPrice = priceTE * ((tax / 100) + 1);
var newPrice = addTaxes(priceTE);
$('#attribute_priceTI').val((isNaN(newPrice) == true || newPrice < 0) ? '' : ps_round(newPrice, priceDisplayPrecision).toFixed(priceDisplayPrecision));
var total = ps_round((parseFloat($('#attribute_priceTI').val()) * parseInt($('#attribute_price_impact').val()) + parseFloat($('#finalPrice').html())), priceDisplayPrecision);
if (isNaN(total) || total < 0)
Expand All @@ -114,10 +175,9 @@ function calcImpactPriceTI()

function calcImpactPriceTE()
{
var tax = getTax();
var priceTI = parseFloat(document.getElementById('attribute_priceTI').value.replace(/,/g, '.'));
priceTI = (isNaN(priceTI)) ? 0 : ps_round(priceTI);
var newPrice = ps_round(priceTI, priceDisplayPrecision) / ((tax / 100) + 1);
var newPrice = removeTaxes(ps_round(priceTI, priceDisplayPrecision));
$('#attribute_price').val((isNaN(newPrice) == true || newPrice < 0) ? '' : ps_round(newPrice, 6).toFixed(6));
$('#attribute_priceTEReal').val((isNaN(newPrice) == true || newPrice < 0) ? 0 : ps_round(newPrice, 9));
var total = ps_round((parseFloat($('#attribute_priceTI').val()) * parseInt($('#attribute_price_impact').val()) + parseFloat($('#finalPrice').html())), priceDisplayPrecision);
Expand All @@ -137,7 +197,6 @@ function calcReduction()

function reductionPrice()
{
var tax = getTax();
var price = document.getElementById('priceTI');
var priceWhithoutTaxes = document.getElementById('priceTE');
var newprice = document.getElementById('finalPrice');
Expand All @@ -156,13 +215,12 @@ function reductionPrice()
}

newprice.innerHTML = (ps_round(parseFloat(curPrice), priceDisplayPrecision) + getEcotaxTaxIncluded()).toFixed(priceDisplayPrecision);
var rpriceWithoutTaxes = ps_round(rprice.value / ((tax / 100) + 1), priceDisplayPrecision);
var rpriceWithoutTaxes = ps_round(removeTaxes(rprice.value), priceDisplayPrecision);
newpriceWithoutTax.innerHTML = ps_round(priceWhithoutTaxes.value - rpriceWithoutTaxes, priceDisplayPrecision).toFixed(priceDisplayPrecision);
}

function reductionPercent()
{
var tax = getTax();
var price = document.getElementById('priceTI');
var newprice = document.getElementById('finalPrice');
var newpriceWithoutTax = document.getElementById('finalPriceWithoutTax');
Expand All @@ -182,7 +240,7 @@ function reductionPercent()
}

newprice.innerHTML = (ps_round(parseFloat(curPrice), priceDisplayPrecision) + getEcotaxTaxIncluded()).toFixed(priceDisplayPrecision);
newpriceWithoutTax.innerHTML = ps_round(parseFloat(ps_round(curPrice, priceDisplayPrecision) / ((tax / 100) + 1)), priceDisplayPrecision).toFixed(priceDisplayPrecision);
newpriceWithoutTax.innerHTML = ps_round(parseFloat(removeTaxes(ps_round(curPrice, priceDisplayPrecision))), priceDisplayPrecision).toFixed(priceDisplayPrecision);
}

function isInReductionPeriod()
Expand Down Expand Up @@ -210,9 +268,8 @@ function decimalTruncate(source, decimals)

function unitPriceWithTax(type)
{
var tax = getTax();
var priceWithTax = parseFloat(document.getElementById(type+'_price').value.replace(/,/g, '.'));
var newPrice = priceWithTax * ((tax / 100) + 1);
var newPrice = addTaxes(priceWithTax);
$('#'+type+'_price_with_tax').html((isNaN(newPrice) == true || newPrice < 0) ? '0.00' : ps_round(newPrice, priceDisplayPrecision).toFixed(priceDisplayPrecision));
}

Expand Down

0 comments on commit 51799ba

Please sign in to comment.