-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit-quotediscount-form.js
More file actions
244 lines (205 loc) · 10.2 KB
/
edit-quotediscount-form.js
File metadata and controls
244 lines (205 loc) · 10.2 KB
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
$(document).ready(function (e) {
//Created By: Leslie Baliguat, Created On: 03/14/16
$('#UpdateButton').click(function (event) {
event.preventDefault();
setTimeout(function () {
window.parent.$('#btnRecalculate').click();
}, 500);
});
var isFinancing = true;
var paymentMode = window.parent.$('#gsc_paymentmode').val();
if (paymentMode != '100000001')
isFinancing = false;
if (paymentMode == '100000000') {
$('#gsc_applypercentagetodp').attr('readonly', true);
$('#gsc_applyamounttodp').attr('readonly', true);
}
if (!isFinancing) {
$('#gsc_applypercentagetoaf').attr('readonly', true);
$('#gsc_applyamounttoaf').attr('readonly', true);
}
//set page validators
if (typeof (Page_Validators) == 'undefined') return;
// Validator when the discounts are not equal to 100%
var discountValidator = document.createElement('span');
discountValidator.style.display = "none";
discountValidator.id = "RequiredFieldValidatordiscounts";
discountValidator.errormessage = "Discounts to be applied should total to 100%.";
discountValidator.validationGroup = "";
discountValidator.initialvalue = "";
discountValidator.evaluationfunction = function () {
var af = $("#gsc_applypercentagetoaf").val() == "" ? 0 : $("#gsc_applypercentagetoaf").val();
var dp = $("#gsc_applypercentagetodp").val() == "" ? 0 : $("#gsc_applypercentagetodp").val();
var up = $("#gsc_applypercentagetoup").val() == "" ? 0 : $("#gsc_applypercentagetoup").val();
var total = parseFloat(af) + parseFloat(dp) + parseFloat(up);
if (af < 1 && dp < 1 && up < 1) {
return true;
} else {
var total = parseFloat(af) + parseFloat(dp) + parseFloat(up);
if (total < 100) {
return false;
} else {
return true;
}
}
};
Page_Validators.push(discountValidator);
//Variables for discount computation
var dscnt_dppercent = $("#gsc_applypercentagetodp").val();
var dscnt_dpamount = $("#gsc_applyamounttodp").val().replace(/,/g, '');
var dscnt_afpercent = $("#gsc_applypercentagetoaf").val();
var dscnt_afamount = $("#gsc_applyamounttoaf").val().replace(/,/g, '');
var dscnt_uppercent = $("#gsc_applypercentagetoup").val();
var dscnt_upamount = $("#gsc_applyamounttoup").val().replace(/,/g, '');
var totaldiscount = $("#gsc_discountamount").val().replace(/,/g, '');
dscnt_dppercent = dscnt_dppercent == "" ? 0 : dscnt_dppercent;
dscnt_afpercent = dscnt_afpercent == "" ? 0 : dscnt_afpercent;
dscnt_uppercent = dscnt_uppercent == "" ? 0 : dscnt_uppercent;
dscnt_dpamount = dscnt_dpamount == "" ? 0 : dscnt_dpamount;
dscnt_afamount = dscnt_afamount == "" ? 0 : dscnt_afamount;
dscnt_upamount = dscnt_upamount == "" ? 0 : dscnt_upamount;
totaldiscount = totaldiscount == "" ? 0 : totaldiscount;
setTimeout(function () {
$('#gsc_applyamounttodp').val(parseFloat(dscnt_dpamount).toFixed(2));
$('#gsc_applyamounttoaf').val(parseFloat(dscnt_afamount).toFixed(2));
$('#gsc_applyamounttoup').val(parseFloat(dscnt_upamount).toFixed(2));
$("#gsc_discountamount").val(parseFloat(totaldiscount).toFixed(2));
//change type from text to number; only allow numbers in textbox
$('#gsc_applypercentagetodp').click(function () {
$(this).get(0).type = 'number';
});
$('#gsc_applyamounttodp').click(function () {
$(this).get(0).type = 'number';
});
$('#gsc_applypercentagetoaf').click(function () {
$(this).get(0).type = 'number';
});
$('#gsc_applyamounttoaf').click(function () {
$(this).get(0).type = 'number';
});
$('#gsc_applypercentagetoup').click(function () {
$(this).get(0).type = 'number';
});
$('#gsc_applyamounttoup').click(function () {
$(this).get(0).type = 'number';
});
$('#gsc_discountamount').click(function () {
$(this).get(0).type = 'number';
});
//do not allow less than 0 input
$('#gsc_applypercentagetodp').blur(function () {
if (this.value < 0) {
this.value = 0;
$("#gsc_applypercentagetodp").trigger('change');
}
else if ((parseFloat(this.value) + parseFloat(dscnt_afpercent) + parseFloat(dscnt_uppercent)) > 100) {
this.value = 100 - dscnt_afpercent - dscnt_uppercent;
$("#gsc_applypercentagetodp").trigger('change');
}
});
$('#gsc_applyamounttodp').blur(function () {
if (this.value < 0) {
this.value = 0;
$("#gsc_applyamounttodp").trigger('change');
}
else if ((parseFloat(this.value) + parseFloat(dscnt_afamount) + parseFloat(dscnt_upamount)) > totaldiscount) {
this.value = totaldiscount - dscnt_afamount - dscnt_upamount;
$("#gsc_applyamounttodp").trigger('change');
}
});
$('#gsc_applypercentagetoaf').blur(function () {
if (this.value < 0) {
this.value = 0;
$("#gsc_applypercentagetoaf").trigger('change');
}
else if ((parseFloat(this.value) + parseFloat(dscnt_dppercent) + parseFloat(dscnt_uppercent)) > 100) {
this.value = 100 - dscnt_dppercent - dscnt_uppercent;
$("#gsc_applypercentagetoaf").trigger('change');
}
});
$('#gsc_applyamounttoaf').blur(function () {
if (this.value < 0) {
this.value = 0;
$("#gsc_applyamounttoaf").trigger('change');
}
else if ((parseFloat(this.value) + parseFloat(dscnt_dpamount) + parseFloat(dscnt_upamount)) > totaldiscount) {
this.value = totaldiscount - dscnt_dpamount - dscnt_upamount;
$("#gsc_applyamounttoaf").trigger('change');
}
});
$('#gsc_applypercentagetoup').blur(function () {
if (this.value < 0) {
this.value = 0;
$("#gsc_applypercentagetoup").trigger('change');
}
else if ((parseFloat(this.value) + parseFloat(dscnt_dppercent) + parseFloat(dscnt_afpercent)) > 100) {
this.value = 100 - dscnt_dppercent - dscnt_afpercent;
$("#gsc_applypercentagetoup").trigger('change');
}
});
$('#gsc_applyamounttoup').blur(function () {
if (this.value < 0) {
this.value = 0;
$("#gsc_applyamounttoup").trigger('change');
}
else if ((parseFloat(this.value) + parseFloat(dscnt_dpamount) + parseFloat(dscnt_afamount)) > totaldiscount) {
this.value = totaldiscount - dscnt_dpamount - dscnt_afamount;
$("#gsc_applyamounttoup").trigger('change');
}
});
$('#gsc_discountamount').blur(function () {
if (this.value < 0)
this.value = 0;
$("#gsc_discountamount").trigger('change');
});
$("#gsc_applypercentagetodp").on('change', function () {
dscnt_dppercent = $('#gsc_applypercentagetodp').val() == "" ? 0 : $('#gsc_applypercentagetodp').val();
dscnt_dpamount = computeAmount(dscnt_dppercent);
$("#gsc_applyamounttodp").val(dscnt_dpamount == 0 ? "" : dscnt_dpamount);
});
$("#gsc_applyamounttodp").on('change', function () {
dscnt_dpamount = $('#gsc_applyamounttodp').val() == "" ? 0 : $('#gsc_applyamounttodp').val();
dscnt_dppercent = computePercentage(dscnt_dpamount);
$("#gsc_applypercentagetodp").val(dscnt_dppercent == 0 ? "" : dscnt_dppercent);
});
$("#gsc_applypercentagetoaf").on('change', function () {
dscnt_afpercent = $('#gsc_applypercentagetoaf').val() == "" ? 0 : $('#gsc_applypercentagetoaf').val();
dscnt_afamount = computeAmount(dscnt_afpercent);
$("#gsc_applyamounttoaf").val(dscnt_afamount == 0 ? "" : dscnt_afamount);
});
$("#gsc_applyamounttoaf").on('change', function () {
dscnt_afamount = $('#gsc_applyamounttoaf').val() == "" ? 0 : $('#gsc_applyamounttoaf').val();
dscnt_afpercent = computePercentage(dscnt_afamount);
$("#gsc_applypercentagetoaf").val(dscnt_afpercent == 0 ? "" : dscnt_afpercent);
});
$("#gsc_applypercentagetoup").on('change', function () {
dscnt_uppercent = $('#gsc_applypercentagetoup').val() == "" ? 0 : $('#gsc_applypercentagetoup').val();
dscnt_upamount = computeAmount(dscnt_uppercent);
$("#gsc_applyamounttoup").val(dscnt_upamount == 0 ? "" : dscnt_upamount);
});
$("#gsc_applyamounttoup").on('change', function () {
dscnt_upamount = $('#gsc_applyamounttoup').val() == "" ? 0 : $('#gsc_applyamounttoup').val();
dscnt_uppercent = computePercentage(dscnt_upamount);
$("#gsc_applypercentagetoup").val(dscnt_uppercent == 0 ? "" : dscnt_uppercent);
});
$("#gsc_discountamount").on('change', function () {
$("#gsc_applyamounttodp").val(dscnt_dpamount = computeAmount(dscnt_dppercent));
$("#gsc_applyamounttoaf").val(dscnt_afamount = computeAmount(dscnt_afpercent));
$("#gsc_applyamounttoup").val(dscnt_upamount = computeAmount(dscnt_uppercent));
});
}, 100);
function computePercentage(amount) {
totaldiscount = $("#gsc_discountamount").val() == "" ? 0 : $("#gsc_discountamount").val().replace(/,/g, '');
if (totaldiscount != 0) {
var percent = (parseFloat(amount) / parseFloat(totaldiscount)) * 100;
return parseFloat(percent).toFixed(2);
}
}
function computeAmount(percent) {
totaldiscount = $("#gsc_discountamount").val() == "" ? 0 : $("#gsc_discountamount").val().replace(/,/g, '');
if (totaldiscount != 0) {
var amount = (parseFloat(totaldiscount) * parseFloat(percent)) / 100;
return parseFloat(amount).toFixed(2);
}
}
});