12 Replies Latest reply on Aug 14, 2015 12:52 PM by try67

    I am stuck on a javascript calculation. Any help?

    Perkymon

      I am stuck on a javascript calculation on an Adobe form I designed. It has to do with tax calculations. I have the javascript code that is giving me trouble. I could email or post...please advise. Just looking for some expertise. I would be forever grateful!

        • 1. Re: I am stuck on a javascript calculation. Any help?
          try67 MVP & Adobe Community Professional

          Explain what you're trying to achieve, post the code you're using, and describe what goes wrong.

          • 2. Re: I am stuck on a javascript calculation. Any help?
            gkaiseril MVP & Adobe Community Professional

            Is this a logic issue?

             

            Is this the form computing wrong value?

            If so, is the calculated amount off by a small amount - possible error from not rounding values.

            If the values are behind when a field is changed, then check the calculation order.

             

            Have you checked the JavaScript console for errors?

            • 3. Re: I am stuck on a javascript calculation. Any help?
              Perkymon Level 1

              Thanks so much for responding! First, let me post the criteria:

               

              The way sales tax is figured in Tennessee is as follows:

               

              Labor/Installation and Optional Accessories are taxed at 9.25%

               

              A boat package is (boat, motor, trailer) is taxed as follows:

               

              90% of the purchase price goes to the boat and motor

              10% of the purchase price goes to the trailer

              After the package is broken down –

               

              7% of the boat/motor portion is the state tax

              7% of the trailer portion is the state tax

               

              2.25% of the first $1600 of the boat/motor portion is the local tax

              2.25% of the first $1600 of the trailer portion is the local tax

               

              2.75% of the next $1600 of the boat/motor portion is the state single article tax

              2.75% of the next $1600 of the trailer portion is the state single article tax

               

               

              Here is an example of the tax structure:

               

              A package is sold for $65,000.00

               

              90% of the package would be applied to the boat/motor   $58,500.00

              10% of the package would be applied to the trailer                              $ 6,500.00

               

              7% State Tax would be charged to the boat motor portion               $4,095.00

              7% State Tax would be charged to the trailer portion                         $455.00

               

              2.25% local tax would be charged to the first $1600.00 of the boat portion               $36.00  (2.25% x $1600.00)

              2.25% local tax would be charged to the first $1600.00 of the trailer portion            $36.00 (2.25% x $1600.00)

               

              2.75% state single article tax would be charged to the next $1600.00 of the boat portion                   $44.00 (2.75% X $1600.00)

              2.75% state single article tax would be charged to the next $1600.00 of the trailer portion                $44.00 (2.75% X $1600.00)

               

              So the total boat tax would be     $4,175.00

              Total Trailer tax would be             $535.00

              Total Taxes would be                     $4,710.00

               

              There are times in which the portions of the boat/trailer will fall below the $1600 and $3200 threshold. At that point, you just tax the portion that falls within the threshold.

               

              Here is the Javascript code:

               

              To calculate the boat local tax, you can use the following script:

              var boatMotorTotal = Number(this.getField("total_purchase").value) * 0.9;

               

              var localTax = 0;

              var amount;

               

              if (boatMotorTotal > 1600) {

                  amount = 1600;

              } else {

                  amount = boatMotorTotal;

              }

              event.value = amount * 0.0225;

              The boat single tax can be calculated as follows:

              var boatMotorTotal = Number(this.getField("total_purchase").value) * 0.9;

               

              var amount;

               

              boatMotorTotal = boatMotorTotal - 1600;

               

              if (boatMotorTotal > 1600) {

                  amount = 1600;

              } else {

                  amount = boatMotorTotal;

              }

              event.value = amount * 0.0275;

              And, the same for the trailer local/single tax:

              var trailerTotal = Number(this.getField("total_purchase").value) * 0.1;

               

              var localTax = 0;

              var amount;

               

              if (trailerTotal > 1600) {

                  amount = 1600;

              } else {

                  amount = trailerTotal;

              }

              event.value = amount * 0.0225;

              and

              var trailerTotal = Number(this.getField("total_purchase").value) * 0.1;

               

              var amount;

               

              trailerTotal = trailerTotal - 1600;

               

              if (trailerTotal > 1600) {

                  amount = 1600;

              } else {

                  amount = trailerTotal;

              }

              event.value = amount * 0.0275;


              i would like to post the actual .pdf and show what is happening, but I am not sure how to do this. It is a small file (under 1mb)


              Thank you in advance for your help

              • 4. Re: I am stuck on a javascript calculation. Any help?
                try67 MVP & Adobe Community Professional

                What exactly is the issue, then?

                 

                You can't post a file to this forum directly. You can upload it to a file-sharing website (Google Drive, Dropbox, etc.) and post a link to it here, though.

                • 5. Re: I am stuck on a javascript calculation. Any help?
                  gkaiseril MVP & Adobe Community Professional

                  Since there is a general 90% boat and 10% trailer split how do you plan on handling the exceptions?

                   

                  You code hard codes that split factor and has no allowance for overriding the split proportions.

                   

                  Most states calculate the sales tax to the nearest cent but you script does not do this. I am talking about the actual field value and not the displayed value. The actual value will go to over 5 decimal places. It is possible your tax calculation at the end will be off by as much as $ 0.01. This might not be much in your eyes, but if you have several hundreds of dollars this could result a much larger difference at the end of the month. This is the type of error that state revenuers can easily spot and find fault with along with fines and interest.


                  • 6. Re: I am stuck on a javascript calculation. Any help?
                    Perkymon Level 1

                    Here is a Dropbox link:     Dropbox - Nashville Marine Master Purchase Agreement work copy.pdf

                     

                    One issue is that the form cannot display negative numbers. Also, the calculations are not working properly. Please let me know if there is any other info you need.

                    • 7. Re: I am stuck on a javascript calculation. Any help?
                      try67 MVP & Adobe Community Professional

                      Well, the form is secured so we can't really do much with it...

                       

                      You're getting negative results because you're not checking for empty (or zero) values in your code, especially in the last piece of code you posted.

                      You need to make sure that the trailerTotal variable is not zero before continuing with the rest of the calculation in that one.

                      • 8. Re: I am stuck on a javascript calculation. Any help?
                        Perkymon Level 1

                        Sorry about that. I've added a form that is not secured for you. Here's the link:

                         

                        Dropbox - Nashville Marine Master Purchase Agreement work copy.pdf

                        • 10. Re: I am stuck on a javascript calculation. Any help?
                          try67 MVP & Adobe Community Professional

                          OK, but the issue is the same. If the total purchase amount is below a certain number the result of your calculation will be negative.

                          You need to define what should happen in such a case.

                          • 11. Re: I am stuck on a javascript calculation. Any help?
                            Perkymon Level 1

                            I plan on hiding the  boat and trailer state tax, boat and trailer local tax and boat and trailer single item tax fields so they don't show in the final version. I can't have a negative item showing in any of the total boat tax and total trailer tax fields.

                            • 12. Re: I am stuck on a javascript calculation. Any help?
                              try67 MVP & Adobe Community Professional

                              Instead of hiding the fields you can just adjust the code slightly so it doesn't display negative results.

                              Add at the end of it this line to do that:

                               

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