PDF Scripting Function Calculation on Document Script Java

Howard_Smith1
Howard_Smith1 used Ask the Experts™
on
To begin with, I have very little knowledge on PDF scripting.  However I have managed myself by doing some Google searching.

I have a form that has three rows with the following fields:
TEXT FIELD:
Item1.Quantity  
Item2.Quantity
Item3.Quantity        

TEXT FIELD
Item1.UnitPrice
Item2.UnitPrice
Item3.UnitPrice
      
CHECK BOX                               
Item1.DiscountYes            
Item2.DiscountYes                  
Item3.DiscountYes
            
TEXT FIELD
Item1.Discount
Item2.Discount
Item3.Discount

Value of check box
“Yes” = checked, discount applies and function runs.
“No” = unchecked, discount does not apply function does not run, but leaves discount field blank.

Form allows the user to enter the Quantity amount, UnitPrice amount and to check or leave blank a check box if discount applies depending on discount policy.  If checked a 10% discount will apply.  If discount does not apply check box is left blank.  

Code below contains a function that works and one that does not work for me.
I need the code that does not work, to be able to call the function if the Check Box is checked no matter in which row the fields are placed.  

I have tried to the same code calling specific fields and the code works.

Further more I need to add an additional condition to be added:
When the box is being checked and the Item#.Quantity and/or Item#.UnitPrice fields are left blank, then user gets the following warning message:
If Item#.Quantity  == ""
 App.alert(“Please enter QUANTITY amount in order to get DISCOUNT PRICE.”)
If Item#.Unit Price  == ""
App.alert(“Please enter UNIT PRICE amount in order to get DISCOUNT PRICE.”)

Any suggestions on making this work will be appreciated.


****THIS FUNCTION IS NOT WORKING FOR ME BECAUSE OF: var cItemName = event.target.name.split(".").shift(); I NEED FORMULA TO WORK NO MATTER IN WHICH ROW FIELDS ARE PLACED ****

//Formula to calculate (Quantity*UnitPrice)*10%, if check box is checked or not.  
//
function SetDiscount()
{
var cItemName = event.target.name.split(".").shift();
var a = this.getField(cItemName + ".Quantity").value;
var b = this.getField(cItemName + ".UnitPrice").value;
var c = this.getField(cItemName + ".DiscountYes").value;
var d = Math.round(a.value * b.value)* (10.00/100);

if (c.value == "Yes")
    event.value = d

else
    event.value = "";
  
}


****THIS FUNCTION IS WORKING FOR ME, BUT CALLING UNIQUE FIELDS.  HOWEVER NEED FUNCTION TO WORK FOR ALL ROW FIELDS****
//Formula to calculate (Quantity*UnitPrice)*10%, if check box is checked or not
//
function SetDiscount()
{
var a = this.getField("Item1.Quantity");
var b = this.getField("Item1.UnitPrice");
var c = this.getField("Item1.DiscountYes");
var d = Math.round(a.value * b.value)* (10.00/100);

if (c.value == "Yes")
    event.value = d;

else
    event.value = "";
  
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Can you upload the PDF?

Author

Commented:
Sure, file attached.
form-402a-experts-exchange.pdf
I am not sure exactly what you mean by 'need function to work for all row fields'. Do you want one function that goes through the complete form and processes the discount? You could do something like the following code:

function SetDiscount(theItem)
{
var a = this.getField(theItem + ".Quantity");
var b = this.getField(theItem + ".UnitPrice");
var c = this.getField(theItem + ".DiscountYes");
var d = Math.round(a.value * b.value)* (10.00/100);

if (c.value == "Yes")
    this.getField(theItem + ".Discount").value = d;

else
    this.getField(theItem + ".Discount").value = "";
  
}

SetDiscount("Item1");
SetDiscount("Item2");
SetDiscount("Item3");

Open in new window

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

We can talk about the warning once I know that I'm actually on the right track.

Author

Commented:
Yes,
I would like for one function to be able to calculate the discount based on user's input for all rows on the same form.  My discount amount will vary from month to month.  Therefore having a formula that works with all rows on the form makes it easier for future updates on the discount amount.  

Example:
If I enter in row one the quantity of 2 and Unit Price of $200.00, and the Discount Check box is checked, then the formula will calculate the discount of $40.00
ON THE SAME FORM:
in row two the quantity of 3 and Unit Price of $250.00, and the Discount Check box is checked, then the formula will calculate the discount of $75.00
ON THE SAME FORM:
in row three quantity of 4 and Unit Price of $5.00, and the Discount Check box has been left unchecked, then the formula will not calculate the discount and leave the Discount field blank.
There will be a total of 12 rows on this form and the user will have the ability to submit the form with inputs of one row or up twelve rows.
Did you look at the code that I posted? Does it solve your problem?

Author

Commented:
Yes, I did looked at the code and implemented the code into my form.  However, form does not calculate the discount when the user enters the quantity amount and unit price amount and selects the check box to become = to "yes".

How does the formula know, which set of fields to get to perform calculation for the discount.  The user enters the item quantity, the unit price and selectes if discount applies, if not is left blank.  If discount applies then the formula should run and set the discount amount.  If the discount does not apply then is left blank  and the formula does not run.  Each row will have a different discount calculation depending on the user's input.

My fields in the form are now named:
ROW 1          Item1.Quantity           Item1.UnitPrice            Item1.DiscountYes            Item1.Discount
ROW 2          Item2.Quantity           Item2.UnitPrice            Item2.DiscountYes            Item2.Discount
ROW 3          Item3.Quantity           Item3.UnitPrice            Item3.DiscountYes            Item3.Discount

Do I need to change my field names on the form for the code to work as metion above?
You need to trigger the calculation of the discount somehow. Is there a submit button?
You could also add a call to the function into the event handler that handles the checkbox event. Just call the function with the argument.

Modify the properties for your first checkbox (Item1.DiscountYes) and use the following program as a MouseUp
var cItemName = event.target.name.split(".").shift();    // that's from the original script
if (event.target.value == "Yes")
{
     SetDiscount(cItemName);
}

Open in new window

Author

Commented:
I have taken My first Script and added, your if statement and now it is working like it needed to work.  I now need add'l help.  Modifiying the if statement.

I need the If statement to give a warning message if the check box is checked == "Yes" and the quantiy & Unit Price fields are left blank == "".

app.alert("Enter Quantity Amount in order  to get Discount Amount.")
app.alert("Enter Unit Price Amount in order  to get Discount Amount.")

function SetDiscount() 
{ 
var cItemName = event.target.name.split(".").shift(); 
var a = this.getField(cItemName + ".Quantity"); 
var b = this.getField(cItemName + ".UnitPrice"); 
var c = this.getField(cItemName + ".DiscountYes"); 
var d = Math.round(a.value * b.value)* (10.00/100); 
 
if (c.value == "Yes") 
    this.getField(cItemName + ".Discount").value = d; 
 
else 
    this.getField(cItemName + ".Discount").value = ""; 
   
}

Open in new window

if (c.value == "Yes" && a.value == "")
{
    app.alert("Enter Quantity Amount in order to get Discount Amount.");
}
else if (c.value == "Yes" && b.value == "")
{
    app.alert("Enter Unit Price in order to get Discount Amount.");
}

Author

Commented:
Thansk so much, this has worked perfect.  I have not finished with the rest of the fields on the form, but should be able to close off solution pretty soon.  Will leave open, just in case I need your help with the rest of it.

Author

Commented:
Ok, working with Discount Total for fields Items "1.Discount thru Item12.Discount".
I'm trying to use an array to get the value of fields "Item1.Dscount thru Item12.Discount".
Then use "sum" to get the TotalDiscount and place it on field named "TotalDiscount".

I have tried attached code, but is not working.  I do not get error message, is just not getting the value of the array "Item1.Dscount thru Item12.Discount".

Any sugestion on to make it work?
function TotalDiscount(){
var cItemName = event.target.name.split(".").shift();
var f = this.getField(cItemName + ".Discount");
var a = f.getArray();
var sum = 0;
for (i =0; i < a.length; i++)
sum += a[i].value;
this.getField("TotalDiscount").value = sum;
}

Open in new window

I don't have an opportunity to test the code right now, but your problem is that you are not getting the fields in the loop. Try something like this (your Array approach is not supported by Acrobat):


function TotalDiscount()
{
    var cItemName = event.target.name.split(".").shift();

    var sum = 0;
    for (i =1; i <= 12; i++)
    {
        sum += = this.getField("Item" + i + ".Discount").value;
    }
    this.getField("TotalDiscount").value = sum;
}

Open in new window

Author

Commented:
} //GETTING SYNTAX ERROR 8: AT LINE 9


function TotalDiscount()
{
    var cItemName = event.target.name.split(".").shift();
 
    var sum = 0;
    for (i =1; i <= 12; i++)
    {
        sum += = this.getField("Item" + i + ".Discount").value;
    } //GETTING SYNTAX ERROR 8: AT LINE 9
    this.getField("TotalDiscount").value = sum;
}
There is a syntax error in your original code (sorry, I did not catch that). The line that keeps track of the sum is wrong:

sum += this.getField("Item" + i + ".Discount").value;

BTW: You don't need the first line that sets the cItemName variable for this routine. It's not getting used, and not necessary.

Author

Commented:
Great!  That did it.  Thanks.

Author

Commented:
Need to add an additional if statement to the function SetDiscount().
The user has a column //this.getField(cItemName + ".CodeDescription");//
I need the if statement to look at this column and if any items selected are not either:
"09001  -  DESKTOP COMPUTER", "09002  -  TOUCH SCREEN DESKTOP COMPUTER", "09003  -  LAPTOP COMPUTER", "09004  -  NETBOOK"
then the user gets a:
//app.alert("Discount does not apply to selected item.");//
and discount value field = ""

HELP!
///THIS IS THE CURRENT CODE:

function SetDiscount() 
{ 
var cItemName = event.target.name.split(".").shift(); 
var a = this.getField(cItemName + ".Quantity"); 
var b = this.getField(cItemName + ".UnitPrice"); 
var c = this.getField(cItemName + ".DiscountYes"); 
var d = Math.round(a.value * b.value)* (10.00/100); 
 
if (c.value == "Yes" && a.value == "")
{
    app.alert("Enter ITEM QUANTITY in order to get DISCOUNT AMOUNT.");
} 
else if (c.value == "Yes" && b.value == "")
{
    app.alert("Enter UNIT PRICE in order to get DISCOUNT AMOUNT.");
}
if (c.value == "Yes") 
    this.getField(cItemName + ".Discount").value = d; 
 
else 
    this.getField(cItemName + ".Discount").value = "";    
}

//I HAVE TRIED THIS, NO SUCCESS:


function SetDiscount() 
{ 
var cItemName = event.target.name.split(".").shift(); 
var a = this.getField(cItemName + ".Quantity"); 
var b = this.getField(cItemName + ".UnitPrice"); 
var c = this.getField(cItemName + ".DiscountYes");
var d = Math.round(a.value * b.value)* (10.00/100); 
var e = this.getField(cItemName + ".CodeDescription"); 
var dApplies = true;

{
if (e.value =="09001  -  DESKTOP COMPUTER", "09002  -  TOUCH SCREEN DESKTOP COMPUTER", "09003  -  LAPTOP COMPUTER", "09004  -  NETBOOK")
dApplies = falce;
}

 
if (c.value == "Yes" && a.value == "")
{
    app.alert("Enter ITEM QUANTITY in order to get DISCOUNT AMOUNT.");
} 
else if (c.value == "Yes" && b.value == "")
{
    app.alert("Enter UNIT PRICE in order to get DISCOUNT AMOUNT.");
}
else if (c.value == "Yes" && dApplies == "falce")
{
    app.alert("Discount does not apply to selected item.");
}

if (c.value == "Yes")
    this.getField(cItemName + ".Discount").value = d; 
 
else 
    this.getField(cItemName + ".Discount").value = "";    
}

Open in new window

Would you mind opening a new question for this? It looks like your original question was resolved.
When you open a new question, please link it to this question, so that it's easy to find the original code. You won't have to repeat all the information. Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial