Skip to main content
Participating Frequently
June 19, 2018
Answered

PDF Calculation Script for challenging sales tax

  • June 19, 2018
  • 4 replies
  • 1548 views

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!

This topic has been closed for replies.
Correct answer BarlaeDC

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

4 replies

BarlaeDC
Community Expert
Community Expert
June 21, 2018

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

BarlaeDC
Community Expert
BarlaeDCCommunity ExpertCorrect answer
Community Expert
June 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; 

     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

Participating Frequently
June 21, 2018

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

BarlaeDC
Community Expert
Community Expert
June 21, 2018

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

Participating Frequently
June 21, 2018
BarlaeDC
Community Expert
Community Expert
June 19, 2018

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

Participating Frequently
June 19, 2018

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

try67
Community Expert
Community Expert
June 19, 2018

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".

try67
Community Expert
Community Expert
June 19, 2018

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?

Participating Frequently
June 19, 2018

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!