1
1
export function updateProductPrices ( product , rate , sourcePriceInclTax = false ) {
2
+ const rateFactor = parseFloat ( rate . rate ) / 100
2
3
product . price = parseFloat ( product . price )
4
+ product . special_price = parseFloat ( product . special_price )
3
5
4
6
let priceExclTax = product . price
5
7
if ( sourcePriceInclTax ) {
6
- priceExclTax = product . price / ( 1 + ( rate . rate / 100 ) )
8
+ priceExclTax = product . price / ( 1 + rateFactor )
7
9
product . price = priceExclTax
8
10
}
9
- product . priceInclTax = ( priceExclTax + priceExclTax * ( parseFloat ( rate . rate ) / 100 ) )
10
- product . priceTax = ( priceExclTax * ( parseFloat ( rate . rate ) / 100 ) )
11
11
12
- product . special_price = parseFloat ( product . special_price )
12
+ product . priceTax = priceExclTax * rateFactor
13
+ product . priceInclTax = priceExclTax + product . priceTax
13
14
14
15
let specialPriceExclTax = product . special_price
15
16
if ( sourcePriceInclTax ) {
16
- specialPriceExclTax = product . special_price / ( 1 + ( rate . rate / 100 ) )
17
+ specialPriceExclTax = product . special_price / ( 1 + rateFactor )
17
18
product . special_price = specialPriceExclTax
18
19
}
19
20
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
22
23
23
24
if ( product . special_price && ( product . special_price < product . price ) ) {
24
25
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)
44
45
}
45
46
}
46
47
configurableChild . price = parseFloat ( configurableChild . price )
48
+ configurableChild . special_price = parseFloat ( configurableChild . special_price )
49
+
47
50
let priceExclTax = configurableChild . price
48
51
if ( sourcePriceInclTax ) {
49
- priceExclTax = configurableChild . price / ( 1 + ( rate . rate / 100 ) )
52
+ priceExclTax = configurableChild . price / ( 1 + rateFactor )
50
53
configurableChild . price = priceExclTax
51
54
}
52
55
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
55
58
56
59
let specialPriceExclTax = configurableChild . special_price
57
60
if ( sourcePriceInclTax ) {
58
- specialPriceExclTax = configurableChild . special_price / ( 1 + ( rate . rate / 100 ) )
61
+ specialPriceExclTax = configurableChild . special_price / ( 1 + rateFactor )
59
62
configurableChild . special_price = specialPriceExclTax
60
63
}
61
64
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
64
67
65
68
if ( configurableChild . special_price && ( configurableChild . special_price < configurableChild . price ) ) {
66
69
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)
79
82
}
80
83
81
84
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
86
89
product . specialPriceInclTax = configurableChild . specialPriceInclTax
87
90
product . specialPriceTax = configurableChild . specialPriceTax
88
91
product . originalPrice = configurableChild . originalPrice
@@ -92,6 +95,7 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false)
92
95
}
93
96
}
94
97
}
98
+
95
99
export function calculateProductTax ( product , taxClasses , taxCountry = 'PL' , taxRegion = '' , sourcePriceInclTax = false ) {
96
100
let rateFound = false
97
101
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
107
111
}
108
112
if ( ! rateFound ) {
109
113
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 } )
111
115
112
116
product . priceInclTax = product . price
113
117
product . priceTax = 0
0 commit comments