• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

PDF Calculation Script for challenging sales tax

Community Beginner ,
Jun 18, 2018 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!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.1K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 21, 2018 Jun 21, 2018

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;

...

Votes

Translate

Translate
Community Expert ,
Jun 19, 2018 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?

Votes

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
community guidelines
Community Beginner ,
Jun 19, 2018 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!

Votes

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
community guidelines
Community Expert ,
Jun 19, 2018 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

Votes

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
community guidelines
Community Beginner ,
Jun 19, 2018 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

Votes

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
community guidelines
Community Expert ,
Jun 19, 2018 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".

Votes

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
community guidelines
Community Expert ,
Jun 19, 2018 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

Votes

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
community guidelines
Community Beginner ,
Jun 21, 2018 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?

Votes

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
community guidelines
Community Expert ,
Jun 21, 2018 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") 

Votes

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
community guidelines
Community Expert ,
Jun 21, 2018 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

Votes

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
community guidelines
Community Beginner ,
Jun 21, 2018 Jun 21, 2018

Copy link to clipboard

Copied

Votes

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
community guidelines
Community Expert ,
Jun 21, 2018 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

Votes

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
community guidelines
Community Expert ,
Jun 21, 2018 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

Votes

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
community guidelines
Community Beginner ,
Jun 21, 2018 Jun 21, 2018

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines