PDF Calculation Script for challenging sales tax

Jun 18, 2018

Copy link to clipboard

Copied

Here's my dilemma:

I'm making an unique invoice.

Sales tax is 1% for county and 6% for state. County is capped at $50. And Total sales tax is capped at $18,000.

I have no experience in JavaScript. How can I set this to run automatically based on the total?

And then is there a way to turn it off for when sales tax is not needed?

Thanks in advanced!

Adobe Community Professional
Correct answer by BarlaeDC | Adobe Community Professional

HI,

I have checked with the code provided - included below  - and it works in your form as expected.

if ( this.getField ("ApplySalesTax").valueAsString=="Off") 

     event.value = 0; 

else 

     var tempTotal = 0; 

     var subTotalValue = Number ( this.getField ("SubTotal").valueAsString); 

     var salesTotal = subTotalValue * 0.01 

     if ( salesTotal<= 50) tempTotal = salesTotal; 

     else salesTotal = 50; 

     // add sales tax on before state tax 

     subTotalValue += salesTotal; 

     var stateTotal = subTotalValue * 0.06; 

     if ( stateTotal <= 18000) tempTotal += stateTotal; 

     else tempTotal +=18000; 

 

     event.value = tempTotal; 

I have correct the equals, and sorted the parentheses.

Hope this helps

Malcolm

TOPICS
Acrobat SDK and JavaScript, Windows

Views

291

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

PDF Calculation Script for challenging sales tax

Jun 18, 2018

Copy link to clipboard

Copied

Here's my dilemma:

I'm making an unique invoice.

Sales tax is 1% for county and 6% for state. County is capped at $50. And Total sales tax is capped at $18,000.

I have no experience in JavaScript. How can I set this to run automatically based on the total?

And then is there a way to turn it off for when sales tax is not needed?

Thanks in advanced!

Adobe Community Professional
Correct answer by BarlaeDC | Adobe Community Professional

HI,

I have checked with the code provided - included below  - and it works in your form as expected.

if ( this.getField ("ApplySalesTax").valueAsString=="Off") 

     event.value = 0; 

else 

     var tempTotal = 0; 

     var subTotalValue = Number ( this.getField ("SubTotal").valueAsString); 

     var salesTotal = subTotalValue * 0.01 

     if ( salesTotal<= 50) tempTotal = salesTotal; 

     else salesTotal = 50; 

     // add sales tax on before state tax 

     subTotalValue += salesTotal; 

     var stateTotal = subTotalValue * 0.06; 

     if ( stateTotal <= 18000) tempTotal += stateTotal; 

     else tempTotal +=18000; 

 

     event.value = tempTotal; 

I have correct the equals, and sorted the parentheses.

Hope this helps

Malcolm

TOPICS
Acrobat SDK and JavaScript, Windows

Views

292

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 18, 2018 0
Most Valuable Participant ,
Jun 19, 2018

Copy link to clipboard

Copied

Sure, that's all possible, but you need to better explain how this needs to work.

For example, based on what should there be a sales tax at all?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 19, 2018 0
Jun 19, 2018

Copy link to clipboard

Copied

The sales tax would be based on the sub-total. This is the basic equation I figured out last night:

Sub-Total x 0.01 = A

A <= 50 = A

A > 50 = 50

Sub-Total x 0.06 = B

(A or 50) + B = C

C <= 18000 = C

C > 18000 = 18000

Does this help? Thanks for responding!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 19, 2018 0
Adobe Community Professional ,
Jun 19, 2018

Copy link to clipboard

Copied

Hi,

Thanks for the response, you mention that sales tax might not always be applied, we need to know how that would be worked out.

Is the sales tax specific to entries in other fields?

Is it controlled by a checkbox, "Apply sales tax?" if checked, add tax, if not checked, don't add tax?

Is it controlled by something else?

Regards

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 19, 2018 0
Jun 19, 2018

Copy link to clipboard

Copied

Hi Barlae,

I've never done that kind of conditional before. I guess the easiest would be a check box for apply sales tax.

Thanks for replying!

Carolyn

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 19, 2018 0
Most Valuable Participant ,
Jun 19, 2018

Copy link to clipboard

Copied

You can use this code as the custom calculation script of the Sales Tax field:

if (this.getField("ApplySalesTax").valueAsString=="Off") {

     event.value = 0;

} else {

    var subTotal = Number(this.getField("SubTotal").valueAsString) * 0.01;

    if (subTotal<=50) event.value = subTotal;

    else event.value = 50;

}

Note it assumes there's a check-box called "ApplySalesTax" and a text field called "SubTotal".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 19, 2018 1
Adobe Community Professional ,
Jun 19, 2018

Copy link to clipboard

Copied

Hi,

Just building the answer above to include the state tax as well.

Assuming state tax is applied with the same rules as sales tax, and applied after sales tax, the code would need to be something like

if ( this.getField ("ApplySalesTax").valueAsString="Off")

{

     event.value = 0;

}

else

{

     var tempTotal = 0;

     var subTotalValue = Number ( this.getField ("SubTotal").valueAsString);

     var salesTotal = subTotalValue * 0.01

     if ( salesTotal<= 50) tempTotal = salesTotal;

     else salesTotal = 50;

     // add sales tax on before state tax

     subTotalValue += salesTotal;

     var stateTotal = subTotalValue * 0.06;

     if ( stateTotal <= 18000) tempTotal += stateTotal;

     else ( tempTotal +=18000;

     event.value = tempTotal;

}

Same assumptions as before, hope this helps

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 19, 2018 1
Jun 21, 2018

Copy link to clipboard

Copied

Thank you both for your contributions!

I can't seem to get it to work though.

BarlaeDC, yours has a missing parentheses mark on line 16. Where should it be?

I tried just running just the checkbox portion to see if it was working, this is what I put:

if (this.getField ("ApplySalesTax").valueAsString="Off")

{

     event.value = 0;

}

else

{

     event.value=50;

}

I have the checkbox set as "ApplySalesTax", put this equation in Sales Tax's custom calculation script, close the box, exit the form editor, and tested checking and unchecking the box. I tried saving it, exiting Adobe PDF, and then coming back in. Still no luck in getting it to work. Here's a screenshot of my form.

What am I doing wrong?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2018 0
Most Valuable Participant ,
Jun 21, 2018

Copy link to clipboard

Copied

You're using the wrong operator. Change the first line to:

if (this.getField ("ApplySalesTax").valueAsString=="Off") 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2018 1
Adobe Community Professional ,
Jun 21, 2018

Copy link to clipboard

Copied

Hi,

Can you post the file through a file sharing site, as it would be easier to find the problem by looking at your form?

as to the missing parentheses, that if should maybe be changed to

if ( stateTotal <= 18000)

{

  •     tempTotal += stateTotal; 

}

else

{

    tempTotal +=18000;     

}

That might make it easier to understand.

Regards

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2018 0
Jun 21, 2018

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2018 0
Adobe Community Professional ,
Jun 21, 2018

Copy link to clipboard

Copied

HI,

I have looked at the document and try67​ is correct, you are missing a equals sign, once you add that it works as expected.

I will plug in the code we provided to check that it works as expected.

Regards

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2018 1
Adobe Community Professional ,
Jun 21, 2018

Copy link to clipboard

Copied

HI,

I have checked with the code provided - included below  - and it works in your form as expected.

if ( this.getField ("ApplySalesTax").valueAsString=="Off") 

     event.value = 0; 

else 

     var tempTotal = 0; 

     var subTotalValue = Number ( this.getField ("SubTotal").valueAsString); 

     var salesTotal = subTotalValue * 0.01 

     if ( salesTotal<= 50) tempTotal = salesTotal; 

     else salesTotal = 50; 

     // add sales tax on before state tax 

     subTotalValue += salesTotal; 

     var stateTotal = subTotalValue * 0.06; 

     if ( stateTotal <= 18000) tempTotal += stateTotal; 

     else tempTotal +=18000; 

 

     event.value = tempTotal; 

I have correct the equals, and sorted the parentheses.

Hope this helps

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2018 1
Jun 21, 2018

Copy link to clipboard

Copied

Thank you so much! It works perfectly! Y'all are awesome. BarlaeDCtry67

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2018 0