These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

help with form calculations

fossey
Registered: Feb 27 2009
Posts: 13
Answered

I have a form where I am trying to calculate the unit * unit price to get a total.
There are 23 lines altogether. From the totals, I need to get a subtotal, then apply taxes to the subtotal, and finally a grand total.

My problems are these: If I only fill in line 1, the "P.S.T." and "G.S.T." fields remain blank. If I fill in more than line 1, the taxes are calculated, but if I change either the unit value or the unit price value, the taxes are not calculating to the correct value.

Any help would be appreciated.

Here are my scripts:

var a = this.getField("QO1");//quantity
var b = this.getField("UnitPrice1");//unit price

event.value = a.value*b.value;//quantity * unit price

if(event.value==0)event.value=""

//my form has a total of 23 lines as above

////////////////////////////////////

my subtotal script is as follows:

var a = this.getField("Total1").value;
var b = this.getField("Total2").value;
var c = this.getField("Total3").value;
var d = this.getField("Total4").value;
var e = this.getField("Total5").value;
var f = this.getField("Total6").value;
var g = this.getField("Total7").value;
var h = this.getField("Total8").value;
var i = this.getField("Total9").value;
var j = this.getField("Total10").value;
var k = this.getField("Total11").value;
var l = this.getField("Total12").value;
var m = this.getField("Total13").value;
var n = this.getField("Total14").value;
var o = this.getField("Total15").value;
var p = this.getField("Total16").value;
var q = this.getField("Total17").value;
var r = this.getField("Total18").value;
var s = this.getField("Total19").value;
var t = this.getField("Total20").value;
var u = this.getField("Total21").value;
var v = this.getField("Total22").value;
var w = this.getField("Total23").value;

event.value = a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w;

if(event.value==0)event.value="";

////////////////////////////////////////////

I then have two scripts for calculating taxes as follows:

var a = this.getField("Subtotal").value;
var b = .08;

event.value = a * b;

if(event.value==0)event.value="";

///////////

var a = this.getField("Subtotal").value;
var b = .05;

event.value = a * b;

if(event.value==0)event.value="";

////////////////

Then my script to get the value of subtotal and the two tax fields as follows:

var a = this.getField("Subtotal").value;
var b = this.getField("P.S.T.").value;
var c = this.getField("G.S.T.").value;

event.value = a+b+c;

if(event.value==0)event.value="";

My Product Information:
Acrobat Pro 9.2, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
First of all, the subtotal calculation script looks quite bad. You can really improve it, like so:
subTotal = 0;for (i=1; i<=23; i++) {subTotal += this.getField("Total"+i).value;}if (subTotal==0) {event.value="";} else event.value = subTotal;

About your problem: check the field calculation order. It sounds like there's a problem with it.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

fossey
Registered: Feb 27 2009
Posts: 13
Thank you for your reply.
I used this script in the subtotal calculation and now get string in subtotal rather than a value, ie:
Quantity 1 at $100.00 = $100.00
Quantity 2 at $100.00 = $200.00
Subtotal is showing $100,200.00
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You have to realize that JavaScript may automatically change a number to a character string and the '+' operator is either an add operation for numbers or a concatenation action for a text string. You can force the '+' action to addition by using the 'Number()' constrictor.
event.value = ''; // clear the fieldsubTotal = 0; // variable for subtotal// loop through the fieldsfor (i=1; i<=23; i++) {// add a field's numeric value - treat blanks or nulls as a zerosubTotal += Number(this.getField("Total"+i).value);}if (subTotal !=0) {// fill in field value if subtotal is not zeroevent.value = subTotal;}

You should try to use the correct format for all numeric fields or you may need to add some additional code to eliminate values that are not spaces or null strings.

George Kaiser

fossey
Registered: Feb 27 2009
Posts: 13
Thank you very much!
fossey
Registered: Feb 27 2009
Posts: 13
Hi again, I have another question.
My form runs horizontal so I have quantity ordered x unit price = total
There are 23 rows altogether.
When I fill in the quantity of 1 say and the unit price of $10.00, the total column show $10.00.
My tax fields remain empty until I fill in the quantity on the next line, also if I have more than one row filled in, it will only tabulate the taxes on the lines where I have actually entered a value in the quantity column.
How can I get the taxes to calculate as the price is calculated?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
That sounds like a form calculation order problem. You will have to arrange the form's field calculation oder, using Acrobat's menu bar "Forms => Edit form in Acrobat" then again (dynamic menu options) "Forms => Edit Fields => Set Field Calculation order..." and arrange the order in which the fields should calculate form fist - top to last - bottom.Of you create one field with all the calculations.

George Kaiser

fossey
Registered: Feb 27 2009
Posts: 13
Yes, the subtotal was under the gst and pst in calculation order. Works perfectly now.
Thanks again.
leet235
Registered: Jun 11 2009
Posts: 2
I need help doing some calculations in Adobe Acrobat X Pro. Here is what I need: Field 1 x Field 2 = into Field 3, Field 3 needs to - with Field 4 and = into field 5. I hope this makes sense. Please let me know if you can help me out.

Thanks
Tammy

Tammy Lee

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Exactly what are the field names?

The space in the names will cause an solvable issue in the Simplified Field Notation.

For Field 3 you can use the following Simplified Field Notation:

Field\ 1 * Field\ 2

For field 5 the Simplified Field Notation:

Field\ 3 - Field\ 4

Or you you can put it all in a Custom Calculation Script in field 5:

var Field1 = Number(this.getField("Field 1").value);
var Field2 = Number(this.getField("Field 2").value);
this.getField("Field 3").value = Field1 * Field2;
var Field3 = Number(this.getField("Field 3").value);
var Field4 = Number(this.getField("Field 4").value);
event.value = Field3 - Field4;

Note that capitalization and white space is important in field names and variable names.

George Kaiser

leet235
Registered: Jun 11 2009
Posts: 2
Thank you George. It is working great!


Tammy Lee