forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice.js
302 lines (266 loc) · 9.92 KB
/
price.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
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);
}
function getEcotaxTaxExcluded()
{
return ecotax_tax_excl;
}
function formatPrice(price)
{
var fixedToSix = (Math.round(price * 1000000) / 1000000);
return (Math.round(fixedToSix) == fixedToSix + 0.000001 ? fixedToSix + 0.000001 : fixedToSix);
}
function calcPrice()
{
var priceType = $('#priceType').val();
if (priceType == 'TE')
calcPriceTI();
else
calcPriceTE();
}
function calcPriceTI()
{
var priceTE = parseFloat(document.getElementById('priceTEReal').value.replace(/,/g, '.'));
var newPrice = addTaxes(priceTE);
document.getElementById('priceTI').value = (isNaN(newPrice) == true || newPrice < 0) ? '' :
ps_round(newPrice, priceDisplayPrecision);
document.getElementById('finalPrice').innerHTML = (isNaN(newPrice) == true || newPrice < 0) ? '' :
ps_round(newPrice, priceDisplayPrecision).toFixed(priceDisplayPrecision);
document.getElementById('finalPriceWithoutTax').innerHTML = (isNaN(priceTE) == true || priceTE < 0) ? '' :
(ps_round(priceTE, 6) + getEcotaxTaxExcluded()).toFixed(6);
calcReduction();
if (isNaN(parseFloat($('#priceTI').val())))
{
$('#priceTI').val('');
$('#finalPrice').html('');
}
else
{
$('#priceTI').val((parseFloat($('#priceTI').val()) + getEcotaxTaxIncluded()).toFixed(priceDisplayPrecision));
$('#finalPrice').html(parseFloat($('#priceTI').val()).toFixed(priceDisplayPrecision));
}
}
function calcPriceTE()
{
ecotax_tax_excl = $('#ecotax').val() / (1 + ecotaxTaxRate);
var priceTI = parseFloat(document.getElementById('priceTI').value.replace(/,/g, '.'));
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);
document.getElementById('finalPrice').innerHTML = (isNaN(newPrice) == true || newPrice < 0) ? '' :
ps_round(priceTI, priceDisplayPrecision).toFixed(priceDisplayPrecision);
document.getElementById('finalPriceWithoutTax').innerHTML = (isNaN(newPrice) == true || newPrice < 0) ? '' :
(ps_round(newPrice, 6) + getEcotaxTaxExcluded()).toFixed(6);
calcReduction();
}
function calcImpactPriceTI()
{
var priceTE = parseFloat(document.getElementById('attribute_priceTEReal').value.replace(/,/g, '.'));
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)
$('#attribute_new_total_price').html('0.00');
else
$('#attribute_new_total_price').html(total);
}
function calcImpactPriceTE()
{
var priceTI = parseFloat(document.getElementById('attribute_priceTI').value.replace(/,/g, '.'));
priceTI = (isNaN(priceTI)) ? 0 : ps_round(priceTI);
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);
if (isNaN(total) || total < 0)
$('#attribute_new_total_price').html('0.00');
else
$('#attribute_new_total_price').html(total);
}
function calcReduction()
{
if (parseFloat($('#reduction_price').val()) > 0)
reductionPrice();
else if (parseFloat($('#reduction_percent').val()) > 0)
reductionPercent();
}
function reductionPrice()
{
var price = document.getElementById('priceTI');
var priceWhithoutTaxes = document.getElementById('priceTE');
var newprice = document.getElementById('finalPrice');
var newpriceWithoutTax = document.getElementById('finalPriceWithoutTax');
var curPrice = price.value;
document.getElementById('reduction_percent').value = 0;
if (isInReductionPeriod())
{
var rprice = document.getElementById('reduction_price');
if (parseFloat(curPrice) <= parseFloat(rprice.value))
rprice.value = curPrice;
if (parseFloat(rprice.value) < 0 || isNaN(parseFloat(curPrice)))
rprice.value = 0;
curPrice = curPrice - rprice.value;
}
newprice.innerHTML = (ps_round(parseFloat(curPrice), priceDisplayPrecision) + getEcotaxTaxIncluded()).toFixed(priceDisplayPrecision);
var rpriceWithoutTaxes = ps_round(removeTaxes(rprice.value), priceDisplayPrecision);
newpriceWithoutTax.innerHTML = ps_round(priceWhithoutTaxes.value - rpriceWithoutTaxes, priceDisplayPrecision).toFixed(priceDisplayPrecision);
}
function reductionPercent()
{
var price = document.getElementById('priceTI');
var newprice = document.getElementById('finalPrice');
var newpriceWithoutTax = document.getElementById('finalPriceWithoutTax');
var curPrice = price.value;
document.getElementById('reduction_price').value = 0;
if (isInReductionPeriod())
{
var newprice = document.getElementById('finalPrice');
var rpercent = document.getElementById('reduction_percent');
if (parseFloat(rpercent.value) >= 100)
rpercent.value = 100;
if (parseFloat(rpercent.value) < 0)
rpercent.value = 0;
curPrice = price.value * (1 - (rpercent.value / 100));
}
newprice.innerHTML = (ps_round(parseFloat(curPrice), priceDisplayPrecision) + getEcotaxTaxIncluded()).toFixed(priceDisplayPrecision);
newpriceWithoutTax.innerHTML = ps_round(parseFloat(removeTaxes(ps_round(curPrice, priceDisplayPrecision))), priceDisplayPrecision).toFixed(priceDisplayPrecision);
}
function isInReductionPeriod()
{
var start = document.getElementById('reduction_from').value;
var end = document.getElementById('reduction_to').value;
if (start == end && start != "" && start != "0000-00-00 00:00:00") return true;
var sdate = new Date(start.replace(/-/g,'/'));
var edate = new Date(end.replace(/-/g,'/'));
var today = new Date();
return (sdate <= today && edate >= today);
}
function decimalTruncate(source, decimals)
{
if (typeof(decimals) == 'undefined')
decimals = 6;
source = source.toString();
var pos = source.indexOf('.');
return parseFloat(source.substr(0, pos + decimals + 1));
}
function unitPriceWithTax(type)
{
var priceWithTax = parseFloat(document.getElementById(type+'_price').value.replace(/,/g, '.'));
var newPrice = addTaxes(priceWithTax);
$('#'+type+'_price_with_tax').html((isNaN(newPrice) == true || newPrice < 0) ? '0.00' : ps_round(newPrice, priceDisplayPrecision).toFixed(priceDisplayPrecision));
}
function unitySecond()
{
$('#unity_second').html($('#unity').val());
if ($('#unity').get(0).value.length > 0)
{
$('#unity_third').html($('#unity').val());
$('#tr_unit_impact').show();
}
else
$('#tr_unit_impact').hide();
}
function changeCurrencySpecificPrice(index)
{
var id_currency = $('#spm_currency_' + index).val();
if (currencies[id_currency]["format"] == 2 || currencies[id_currency]["format"] == 4)
{
$('#spm_currency_sign_pre_' + index).html('');
$('#spm_currency_sign_post_' + index).html(' ' + currencies[id_currency]["sign"]);
}
else if (currencies[id_currency]["format"] == 1 || currencies[id_currency]["format"] == 3)
{
$('#spm_currency_sign_post_' + index).html('');
$('#spm_currency_sign_pre_' + index).html(currencies[id_currency]["sign"] + ' ');
}
}