Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Exit

- Home
- Acrobat SDK
- Discussions
- Re: PDF Forms - Calculating the discount / JavaScr...

- Re: PDF Forms - Calculating the discount / JavaScr...

0

PDF Forms - Calculating the discount / JavaScript

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/td-p/11355065
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

Community guidelines

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

17
Replies
17

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355178#M3506
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

Community guidelines

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

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355190#M3507
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

Community guidelines

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

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355221#M3508
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355223#M3509
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

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

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355225#M3510
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
```

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355236#M3511
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

This will not when 2 different products are odered.

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355246#M3512
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

So we need to calculate the sum of all quantity, than act on the results.

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355259#M3513
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.

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355307#M3514
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;
```

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355349#M3517
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

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

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355311#M3515
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

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

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11355337#M3516
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;

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11356305#M3518
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

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.

- 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:

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11356949#M3519
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

Community guidelines

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11357596#M3521
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.

Community guidelines

Community Expert
,

LATEST
/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11357621#M3522
Aug 13, 2020
Aug 13, 2020

Copy link to clipboard

Copied

+++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?

Community guidelines

Anna5D2F
AUTHOR

New Here
,

/t5/acrobat-sdk-discussions/pdf-forms-calculating-the-discount-javascript/m-p/11357268#M3520
Aug 13, 2020
Aug 13, 2020

Copy link to clipboard

Copied

Community guidelines

Copyright © 2024 Adobe. All rights reserved.