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

PDF Forms - Calculating the discount / JavaScript

New Here ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Hello my friends,

 

I've a problem which I am hoping will be solved with your help:)

I made a pdf form with calculating net and gross price where customer can type how many of products wants to buy and it works all fine (attachment). 

 

The problem is when I want to make a discount (for example 10%) of total price if customer order 2 or more products and I'm stuck.  Do you have any ideas how I can make it happen? I can write JavaScript code but I dont know how to implement it into pdf.

 

Is it possible to use the total net price as a variable? 
Any ideas will be very valuable, cheers!

TOPICS
Acrobat SDK and JavaScript

Views

2.6K

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Calculate the net price with Javascript code and substract the discount when 2 or more products are ordered.

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Why do you need JavaScript code when this can be done very easily with Simplified Field Notation, and  the same question has been answer before in the forums up to 4 times in the past two weeks.

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Price value is at 100%, but at 90% if 2 or more quantity. I don’t think this can be done with Simplified Field Notation.

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

How does you count the ordered products with Simplified Field Notation?

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

@Anna5D2F for all indivual total price add this line in a custom calculation script. Make sure you change the field name for each.

var qp_price = getField("kwota_val").value;
var qp_prod = getField("ilosc_val").value; if (qp_prod > "1") qp_price = qp_price * 0.9;

event.value = qp_price * qp_prod

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

This will not when 2 different products are odered.

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Indeed, only when the same products is ordered 2 or more time.
So we need to calculate the sum of all quantity, than act on the results.

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

She can add a hidden field, than SUM the quantities, than eveluate that resuts for the price.

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

If you need this for 2 or more differents products, create an hidden field [I call mine "ilosc_total" ] that will sum all of the quantities fields. Than for all sub-total line use this in a custom calculation script:

var q_total = getField("ilosc_total").value;;
var q_price1 = getField("kwota_val").value;
var q_1 = getField("ilosc_val").value;
if (q_total > "1") q_price1 = q_price1 * 0.9;
event.value = q_1 * q_price1;

 

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

You have to drop the quotes around "1" for it to work...

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Whatever you do, you will need to fix the Calculation order... 

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Use this code as "Custom Calculation Script" of "suma_brutto" field:

 

var sumanetto = Number(this.getField("suma_netto").value);
var iloscval = Number(this.getField("ilosc_val").value);
var ilosclol = Number(this.getField("ilosc_lol").value);
var ilosccsgo = Number(this.getField("ilosc_csgo").value);
var iloscfifa = Number(this.getField("ilosc_fifa").value);
var iloscf1 = Number(this.getField("ilosc_f1").value);
var iloscclash = Number(this.getField("ilosc_clash").value);
var x = iloscval+ilosclol+ilosccsgo+iloscfifa+iloscf1+iloscclash;
var y = sumanetto*1.23;
if(x > 1){
event.value = y*.9;
}
else event.value = y;

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

I'm sorry guys but if I understood Anna's question correctly she has an PDF invoice that itemizes the Net Price for each individual item in the table.

 

Therefore, she needs to illustrate a breakdown of the Net Price for each product before it is totalize as the Total Net Price and Grand Total.

 

I think that we all missed the fact that Grand Total in an invoice is not the same as Gross Price in this context. 

 

For example, the cutomer who will be receiving the invoice from the seller would like to see what are they paying for exactly.

 

As for the person who prepares the invoice should take care in providing a clear breakdown of these costs to avoid discrepancies during audits and inspections.

 

That said, Net Price is defined as the total price after comissions and other discounts are applied to the Gross Price of an item. So it is not a good business practice to hide a percent discount in the Total Net Price and expect that those number are open to interpretation.  

 

For this purpose, below is a breakdown of how I worked around this PDF.

 

I still used Simplified Field Notation in the Net Price coulumn. I also added  hidden checkboxes next to each quantity field and set an export value of ".1" to each Check Box. 

 

In the Quantity fields I added a very simple custom calculation script, so when the user enters a value greater than one (1) it will check the checkbox and the Simplified Field Notation will reflect the correct arithmetic.

 

Here is what I did :

 

  • The slide below shows how the 10 percent is applied automatically when the user enters a value greater than 1 in the quantity field(s)
  • To make the form easier to understand and avoid confusion I also specified the appropriate terms for each column

 

discount.png

 

See below the scripts that I used in this example:

 

 

//CUSTOM CALCULATION SCRIPTS FOR THE NET QUANTITY FIELDS 
//CHECKBOXES ARE NAMED "discount.0", "discount.1", "discount.3", etc

// ilosc_val
var f = this.getField("discount.0");
if (event.target.value > 1) {
f.checkThisBox(0,true);
} else {
f.checkThisBox(0,false);
 }

// ilosc_lol
var f = this.getField("discount.1");
if (event.target.value > 1) {
f.checkThisBox(0,true);
} else {
f.checkThisBox(0,false);
 }

// ilosc_csgo
var f = this.getField("discount.2");
if (event.target.value > 1) {
f.checkThisBox(0,true);
} else {
f.checkThisBox(0,false);
 }

// ilosc_fifa
var f = this.getField("discount.3");
if (event.target.value > 1) {
f.checkThisBox(0,true);
} else {
f.checkThisBox(0,false);
 }

// ilosc_f1
var f = this.getField("discount.4");
if (event.target.value > 1) {
f.checkThisBox(0,true);
} else {
f.checkThisBox(0,false);
 }

// ilosc_clash
var f = this.getField("discount.5");
if (event.target.value > 1) {
f.checkThisBox(0,true);
} else {
f.checkThisBox(0,false);
 }

 

 

 

  • Once these scripts were added then set the export value to the Check Boxes.

discount2.png

 

  • And below is the  Simplified Field Notations used to calculate the  Net Price of each item:

 

 

// SIMPLIFIED FIELD NOTATIONS

// used this for suma_val
(ilosc\_val*kwota\_val)-((ilosc\_val*kwota\_val)*discount\.0)

// used this for suma_lol
(ilosc\_lol*kwota\_lol)-((ilosc\_lol*kwota\_lol)*discount\.1)

// used this for suma_csgo
(ilosc\_csgo*kwota\_csgo)-((ilosc\_csgo*kwota\_csgo)*discount\.2)

// used this for suma_fifa
(ilosc\_fifa*kwota\_fifa)-((ilosc\_fifa*kwota\_fifa)*discount\.3)

// used this for suma_f1
(ilosc\_f1*kwota\_f1)-((ilosc\_f1*kwota\_f1)*discount\.4)

// used this for suma_clash
(ilosc\_clash*kwota\_clash)-((ilosc\_clash*kwota\_clash)*discount\.5)

 

 

 

If you want to see this PDF in action with this method here's a link to it:

 

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 ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

With your sample you will not get a discount when you order 1 piece of product 1 and 1 piece of product 2.

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 ,
Aug 13, 2020 Aug 13, 2020

Copy link to clipboard

Copied

Beside Bernd comment, this look like many extras steps, fields and javascript that is not needed.

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 ,
Aug 13, 2020 Aug 13, 2020

Copy link to clipboard

Copied

LATEST

+++EDITED REPLY,  fixed typos and spelling

 

The hidden checkboxes was inspired from your suggestion actually. This is what you suggested:

 

  • she can add a hidden field, than SUM the quantities, than eveluate that resuts for the price.

 

So I thought that this was convenient because you still need to itemize the Net Price per product if , for example, the user doesn't selects more than one Product but chooses to order 2 or more quantities of a single Product type.

 

I was already taking into account that  NesaNurani's code answered what you and Bernd were observing.

 

Nesa's code,  for instance,  could be used as a validation script or as a  custom calculation script  with conditions in the " suma_neto " field. The small code in this field should evaluate if the sum of all the values in the Quantity fields equals to or is less than 6. If it meets this condition the 10 % is applied to the Total Net Price.

 

If the sum of all Quantity fields is greater than 6 , and, if the checked state of any of the checkboxes is ( !="Off" ) then just add up all the fields normally because they are already clacluated with the net price value (if applicable) and there is no further need to apply a percent discount.

 

In any case,  I see what you're saying.

 

If this was a big PDF this method is not convenient, but it is only 6 rows so I saw that idea to meet the intended goals partially.

 

Any additional thoughts?  

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
New Here ,
Aug 13, 2020 Aug 13, 2020

Copy link to clipboard

Copied

You all are awesome, didn't expect that you will write code for me I was hoping only for tips but it's so much helpfull for me, I love this community already. I'm going to test it now, reading your code boosted my knowledge in process of working pdf forms so much. Thanks a lot again, I will close topic after I test it out 🙂 Cheers!

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