Skip to main content
Participant
August 12, 2020
Question

PDF Forms - Calculating the discount / JavaScript

  • August 12, 2020
  • 7 replies
  • 3552 views

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!

This topic has been closed for replies.

7 replies

Anna5D2FAuthor
Participant
August 13, 2020

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!

ls_rbls
Community Expert
Community Expert
August 12, 2020

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:

 

Bernd Alheit
Community Expert
Community Expert
August 13, 2020

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

Nesa Nurani
Community Expert
Community Expert
August 12, 2020

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;

jctremblay
Community Expert
Community Expert
August 12, 2020

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

jctremblay
Community Expert
Community Expert
August 12, 2020

@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
Bernd Alheit
Community Expert
Community Expert
August 12, 2020

This will not when 2 different products are odered.

jctremblay
Community Expert
Community Expert
August 12, 2020

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.

ls_rbls
Community Expert
Community Expert
August 12, 2020

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.

jctremblay
Community Expert
Community Expert
August 12, 2020

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.

Bernd Alheit
Community Expert
Community Expert
August 12, 2020

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