Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit c0da46d

Browse files
committed
Taxcalc refactor
1 parent 13a4628 commit c0da46d

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/lib/taxcalc.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
export function updateProductPrices (product, rate, sourcePriceInclTax = false) {
2+
const rateFactor = parseFloat(rate.rate) / 100
23
product.price = parseFloat(product.price)
4+
product.special_price = parseFloat(product.special_price)
35

46
let priceExclTax = product.price
57
if (sourcePriceInclTax) {
6-
priceExclTax = product.price / (1 + (rate.rate / 100))
8+
priceExclTax = product.price / (1 + rateFactor)
79
product.price = priceExclTax
810
}
9-
product.priceInclTax = (priceExclTax + priceExclTax * (parseFloat(rate.rate) / 100))
10-
product.priceTax = (priceExclTax * (parseFloat(rate.rate) / 100))
1111

12-
product.special_price = parseFloat(product.special_price)
12+
product.priceTax = priceExclTax * rateFactor
13+
product.priceInclTax = priceExclTax + product.priceTax
1314

1415
let specialPriceExclTax = product.special_price
1516
if (sourcePriceInclTax) {
16-
specialPriceExclTax = product.special_price / (1 + (rate.rate / 100))
17+
specialPriceExclTax = product.special_price / (1 + rateFactor)
1718
product.special_price = specialPriceExclTax
1819
}
1920

20-
product.specialPriceInclTax = (specialPriceExclTax + specialPriceExclTax * (parseFloat(rate.rate) / 100))
21-
product.specialPriceTax = (specialPriceExclTax * (parseFloat(rate.rate) / 100))
21+
product.specialPriceTax = specialPriceExclTax * rateFactor
22+
product.specialPriceInclTax = specialPriceExclTax + product.specialPriceTax
2223

2324
if (product.special_price && (product.special_price < product.price)) {
2425
if ((product.special_to_date && new Date(product.special_to_date) < new Date()) || (product.special_from_date && new Date(product.special_from_date) > new Date())) {
@@ -44,23 +45,25 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false)
4445
}
4546
}
4647
configurableChild.price = parseFloat(configurableChild.price)
48+
configurableChild.special_price = parseFloat(configurableChild.special_price)
49+
4750
let priceExclTax = configurableChild.price
4851
if (sourcePriceInclTax) {
49-
priceExclTax = configurableChild.price / (1 + (rate.rate / 100))
52+
priceExclTax = configurableChild.price / (1 + rateFactor)
5053
configurableChild.price = priceExclTax
5154
}
5255

53-
configurableChild.priceInclTax = (priceExclTax + priceExclTax * (parseFloat(rate.rate) / 100))
54-
configurableChild.priceTax = (priceExclTax * (parseFloat(rate.rate) / 100))
56+
configurableChild.priceTax = priceExclTax * rateFactor
57+
configurableChild.priceInclTax = priceExclTax + configurableChild.priceTax
5558

5659
let specialPriceExclTax = configurableChild.special_price
5760
if (sourcePriceInclTax) {
58-
specialPriceExclTax = configurableChild.special_price / (1 + (rate.rate / 100))
61+
specialPriceExclTax = configurableChild.special_price / (1 + rateFactor)
5962
configurableChild.special_price = specialPriceExclTax
6063
}
6164

62-
configurableChild.specialPriceInclTax = (specialPriceExclTax + specialPriceExclTax * (parseFloat(rate.rate) / 100))
63-
configurableChild.specialPriceTax = (specialPriceExclTax * (parseFloat(rate.rate) / 100))
65+
configurableChild.specialPriceTax = specialPriceExclTax * rateFactor
66+
configurableChild.specialPriceInclTax = specialPriceExclTax + configurableChild.specialPriceTax
6467

6568
if (configurableChild.special_price && (configurableChild.special_price < configurableChild.price)) {
6669
if ((configurableChild.special_to_date && new Date(configurableChild.special_to_date) < new Date()) || (configurableChild.special_from_date && new Date(configurableChild.special_from_date) > new Date())) {
@@ -79,10 +82,10 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false)
7982
}
8083

8184
if (configurableChild.priceInclTax < product.priceInclTax || product.price === 0) { // always show the lowest price
82-
product.priceInclTax = parseFloat(configurableChild.priceInclTax)
83-
product.priceTax = parseFloat(configurableChild.priceTax)
84-
product.price = parseFloat(configurableChild.price)
85-
product.special_price = parseFloat(configurableChild.special_price)
85+
product.priceInclTax = configurableChild.priceInclTax
86+
product.priceTax = configurableChild.priceTax
87+
product.price = configurableChild.price
88+
product.special_price = configurableChild.special_price
8689
product.specialPriceInclTax = configurableChild.specialPriceInclTax
8790
product.specialPriceTax = configurableChild.specialPriceTax
8891
product.originalPrice = configurableChild.originalPrice
@@ -92,6 +95,7 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false)
9295
}
9396
}
9497
}
98+
9599
export function calculateProductTax (product, taxClasses, taxCountry = 'PL', taxRegion = '', sourcePriceInclTax = false) {
96100
let rateFound = false
97101
let taxClass = taxClasses.find((el) => el.product_tax_class_ids.indexOf(parseInt(product.tax_class_id) >= 0))
@@ -107,7 +111,7 @@ export function calculateProductTax (product, taxClasses, taxCountry = 'PL', tax
107111
}
108112
if (!rateFound) {
109113
console.log('No such tax class id: ' + product.tax_class_id + ' or rate not found for ' + taxCountry + ' / ' + taxRegion)
110-
updateProductPrices(product, { rate: 0 })
114+
updateProductPrices(product, {rate: 0})
111115

112116
product.priceInclTax = product.price
113117
product.priceTax = 0

0 commit comments

Comments
 (0)