Calculating the sum of the fields with values and adding a discount.
Copy link to clipboard
Copied
I greet you cordially.
I'll say right away that I'm just getting started with javascripts.
I have to add the values from Price1,2,3 fields to the SUM field.
Even if it is 0.
I used the Properities / Calculate / Value is the / sum + of the following fields
option to calculate the sum. But now I want to write it down to use "Custom Calculation Script".
I have tried many things and nothing is working properly for me. I was creating hidden fields,
but I'd rather not want to make a workaround. Summing up, I don't know how to get
the values into the SUM fields from the Price1, 2, 3. I don't know how to write
a script that will calculate the discount in the SUM field. Before, I did it differently
but was not allowed to do so. It looked like this:
this.getField ("Discount"). value = 0;
if (this.getField ("BoxDiscount"). value! = "Off") this.getField ("PriceDiscount").
value = this.getField ("SUM"). value -this.getField ("Percent"). value * this.getField ("SUM"). value / 100;
And it works, but the need is different.


Copy link to clipboard
Copied
Your description is not very clear... Do you want to basically add up all the Price fields, if their corresponding check-box is selected, and deduct the Discount at the end?
Copy link to clipboard
Copied
Hi, dont bother the checkboxes, it working well. Checboxes work fine. All I need is to add up the values from the fields Price1, 2, 3 to field SUM. And subtract from them the percentage given in the field on the left next to the field SUM. Maybe you don't understand me well because I don't know English very well. Sorry for that.
Copy link to clipboard
Copied
Hi, you didn't say how do you enter discount is it static number or user input and what formatting do you use.
I assume for example you want to enter whole numbers like 20 instead of .2 and it will show 20% , if you enter 35 it shows 35% ...etc if I'm wrong sry.
In the case I described enter this code in your "Discount" field under properties->format->custom ->custom format script:
event.value = util.printf("%,0.0f%",event.value);
In your "SUM" field use this as custom calculation script:
var p1 = this.getField("Price1").value;
var p2 = this.getField("Price2").value;
var p3 = this.getField("Price3").value;
var dsc = this.getField("Discount").value;
event.value = (p1+p2+p3)*(1-dsc/100);
Copy link to clipboard
Copied
Thank you very much. It works! You are great ... I hope there are no problems along the way.
Copy link to clipboard
Copied
I have one more question, I wanted to make 3 groups where the checkboxes are exclusive. It works. Additionally, I wanted
to assign some checkboxes to invisible values. And it doesn't work anymore. There are no Syntax errors. Do You have any idea how to solve it?
The script looks:
var firstGroup = [this.getField("Box1"),this.getField("Box2"),this.getField("Box3"),this.getField("Box4")];
var secondGroup = [this.getField("Box5"),this.getField("Box6"),this.getField("Box7")];
var thirdGroup = [this.getField("Box8"),this.getField("Box9"),this.getField("Box10")];
function checkFirstGroup(box)
{
for(var i=0;i<firstGroup.length;i++)
{
if(firstGroup[i] != box)
{
firstGroup[i].value = "Off";
}
}
}
function checkSecondGroup(box)
{
for(var i=0;i<secondGroup.length;i++)
{
if(secondGroup[i] != box)
{
secondGroup[i].value = "Off";
}
}
}
function checkThirdGroup(box)
{
for(var i=0;i<thirdGroup.length;i++)
{
if(thirdGroup[i] != box)
{
thirdGroup[i].value = "Off";
}
}
}
var RedGroup = [this.getField("Box8"),this.getField("Box9"),this.getField("Box10")];
var YellowGroup = [this.getField("Box8")];
function showBoxes(){
// loop on all boxes entered in RedGroup
for(var i=0;i<RedGroup.length;i++){
if (this.getField("Box2").value!="Off" || this.getField("Box3").value!="Off" || this.getField("Box4").value!="Off") {
// if any of the red boxes is turned on, we turn on the appropriate boxes from the RedGroup
RedGroup[i].display = display.visible;
}
else {
// when all red boxes are turned off, we turn off the boxes
RedGroup[i].value = "Off";
RedGroup[i].display = display.hidden;
}
}
// loop for all boxes entered in YellowGroup
for(var i=0;i<YellowGroup.length;i++){
if (this.getField("Box7").value!="Off") {
// if any of the red boxes is turned on, we turn on the appropriate boxes from the YellowGroup group
YellowGroup[i].display = display.visible;
}
else {
// when all red boxes are turned off, we turn off the boxes
YellowGroup[i].value = "Off";
YellowGroup[i].display = display.hidden;
}
}
// here separately for boxes that have several colors
if (this.getField("Box2").value!="Off" || this.getField("Box3").value!="Off" || this.getField("Box4").value!="Off" || this.getField("Box7").value!="Off") {
}
}

