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

Gross Profit calculator issue

New Here ,
Apr 03, 2025 Apr 03, 2025

Trying to determine how to make a gross profit percentage calculation populate in field usgp.

Screen Shot 2025-04-03 at 2.56.49 PM.png

I'm trying to divide usppu/usdcost, multiply by 10, and have the result populate in field usgp. Unfortunately, I'm not getting an error message and the field remains blank.

 

Screen Shot 2025-04-03 at 2.50.36 PM.png

Note that field usppu populates once uscog (cost of goods) is manually entered and subtracted from usdcost ( i.e. usppu=usdcost-uscog).

 

Here's what I have so far:

var oFld=this.getField("usppu");
var discount=1;
if(oFld.value!="Off")
{usppu=oFld.value}
if(!this.getField("usppu").value)
{event.value=""}
else
{
event.value=this.getField("usdcost").value/usppu*10;
}

 

Thank you.

TOPICS
JavaScript , PDF forms
206
Translate
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 ,
Apr 03, 2025 Apr 03, 2025

You did not define usppu so you probably have an error.

Translate
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 ,
Apr 03, 2025 Apr 03, 2025

The "usppu" field is not a checkbox.   I think you must mean for one of the "disco" fields to be used in the "if" statement? 

You did not say, but I'm assuming that the script you provided a custom calculation on the "usgp" field?  Since this is where you say the result should go.  These details are important. Assumptions are bad. 

 

So I would suggest you start out by just doing the straight forward calculation.  Then add the discount code. 

 

var oFld=this.getField("usppu");

if(oFld.value && ((Number)oFld.value > 0))

      event.value=(this.getField("usdcost").value/(Number)oFld.value)*10;

else

    event.value = 0;

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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 ,
Apr 08, 2025 Apr 08, 2025

usgp is a percentage determined with the equation of (usdcost - uscog) / usdcost * 10. 

 

usdcost is determined with usmsrp * discount [checkbox].

 

How would (Number), which you ecoded, be treated?

 

Thank you.

Translate
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 ,
Apr 08, 2025 Apr 08, 2025

In the example below, when populated, usgp should result in 60%

 

Screen Shot 2025-04-03 at 2.50.36 PM.png

Translate
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 ,
Apr 08, 2025 Apr 08, 2025

Just enter the code for the calculation you've already stated above.

Put this code in the custom calculation script for the "usgp" field.

var oFld = this.getField("usdcost");
if(!isNaN(oFld.value) && ((Number)oFld.value > 0)){
    var nUSDCost = (Number)oFld.value;
    event.value=(nUSDCost - this.getField("uscog").value/nUSDCost) *10;
}
else
    event.value = 0;

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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 ,
Apr 08, 2025 Apr 08, 2025

For the cost with discount, use this code in the custom calculatin script for "usdcost"

NOTE:  I don't know what the export values are for the "disco" checkbox fields, so I'm just guessing. You'll need to fill in the correct values. 

 

var oFld = this.getField("usmsrp");
if(!isNaN(oFld.value) && ((Number)oFld.value > 0)){
    var cDisco = this.getField("disco").value;
    if(cDisco = "50") // 50% off
         event.value= oFld.value*.5;
    if(cDisco = "40")  // 40% off
         event.value= oFld.value*.4;
    else  // full value
         event.value= oFld.value;
}
else
    event.value = 0;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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 ,
Apr 08, 2025 Apr 08, 2025

Hi Thom,  I have never seen the Number method like you have written it:  (Number)oFld.value.  It's usually Number(oFld.value).  I ran a test in the console and I'm getting a syntax error:

var oFld=this.getField("usmsrp");

(Number)oFld.value;

Translate
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 ,
Apr 08, 2025 Apr 08, 2025
LATEST

I've got C++ on the brain. JS typing uses a different syntax. 

Here's the correction

 

var oFld = this.getField("usmsrp");
if(!isNaN(oFld.value) && (Number(oFld.value) > 0)){
    var cDisco = this.getField("disco").value;
    if(cDisco = "50") // 50% off
         event.value= oFld.value*.5;
    if(cDisco = "40")  // 40% off
         event.value= oFld.value*.4;
    else  // full value
         event.value= oFld.value;
}
else
    event.value = 0;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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