Calculation function dead end

No - line 163 is the calculate function.

What you are looking for is in the makeCalcUpdateCallback function at line 21.

Thanks Paul, posted this in but still showing a minus figure for me. It’s in the live form as above.
Looking at it again the figure it produces is weird as even when they should be getting commission it is negative eg actual gross of 2k is giving -200

It looks like you accidentally removed the function declaration for the setCalcFieldValue function.

1 Like

I tested your code substituting with
$('form#Commission #Commission_On').formCalc("minu( Nett_Earnings, Actual_Takings )", { currency_format:true, mirror:'sfm_Commission_On_parsed' });

And I did not get blank. I believe I got the same result as Paul’s amended code.

Sample values of 40 hours, 3.75 pay rate, 3.5 target, and 900 gross are used and the commission on value is £0.00

Change actual gross to say -900 and commission changes to £150.00

Are you sure you replaced the code correctly?

Taken from my editor

$(function(){
   $('form#Commission #Base_Pay').formCalc(" Hours_Worked * Pay_Rate",{ currency_format:true, mirror:'sfm_Base_Pay_parsed'});

   $('form#Commission #Target').formCalc(" (Base_Pay * Target_Multiplier)*1.2",{ currency_format:true, mirror:'sfm_Target_parsed'});

   $('form#Commission #Nett_Earnings').formCalc(" Actual_Takings /1.2",{ currency_format:true, mirror:'sfm_Nett_Earnings_parsed'});

   $('form#Commission #Commission_On').formCalc("minu( Nett_Earnings, Actual_Takings )", { currency_format:true, mirror:'sfm_Commission_On_parsed' });

   $('form#Commission #Commission_Earned').formCalc("perc_of( Commission_On , Commission_Rate )",{ currency_format:true, mirror:'sfm_Commission_Earned_parsed'});

   $('form#Commission #Wage').formCalc(" Base_Pay + Commission_Earned",{ currency_format:true, mirror:'sfm_Wage_parsed'});

   $('form#Commission #Holiday_Pay_Accrued').formCalc("((12.07/100)* Hours_Worked )* Pay_Rate",{ currency_format:true, mirror:'sfm_Holiday_Pay_Accrued_parsed'});

   $('form#Commission #Emp_Pension_Contribution').formCalc("perc_of( Wage , Employer_Percentage )",{ currency_format:true, mirror:'sfm_Emp_Pension_Contribution_parsed'});

   $('form#Commission #Employers_NI_Contribution').formCalc(" perc_of( Wage ,13.8)",{ currency_format:true, mirror:'sfm_Employers_NI_Contribution_parsed'});

   $('form#Commission #Total_Payroll_Cost').formCalc(" Wage + Holiday_Pay_Accrued + Emp_Pension_Contribution + Employers_NI_Contribution",{ currency_format:true, mirror:'sfm_Total_Payroll_Cost_parsed'});
});
Globalize.culture('en-GB')

Edit: It looks like Paul has this, so will back away now:)

2 Likes

It must be me as still getting a negative. I’ll keep trying incase I have entered your code wrong.

This is what I see

function makeCalcUpdateCallback(calc_input)
            {
               return function(value)
                        {
                           if (calc_input.id === "Commission_On" && value < 0) {
                               value = 0;
                           }
                           setCalcFieldValue(calc_input, value);
            {
                if(typeof value == 'number' && isNaN(value))

Between line 27 and 28 of the code at https://www.salonlogic.co.uk/forms/Commission/scripts/jquery.sim.FormCalc.js

The anonymous function needs to be closed. The makeCalcUpdateCallback function needs to be closed, and the setCalcFieldValue function needs to be defined.

You accidentally removed those when updating the code.

I will separate the code where the problem is.

function makeCalcUpdateCallback(calc_input)
            {
               return function(value)
                        {
                           if (calc_input.id === "Commission_On" && value < 0) {
                               value = 0;
                           }
                           setCalcFieldValue(calc_input, value);

You need to put back in the code that used to be here, between the line above, and the line below.

            {
                if(typeof value == 'number' && isNaN(value))

Thanks Paul, no longer negative. Success I think. :slight_smile:

Guys, I need to ask for your help again, I wasn’t convinced with one of the sums and edited it in the formmaker an didn’t think of the repurcussions of publishing it, however it’s back to nothing working, even though I think I have made all the amendments you gave me last night. Any chance you could have a quick look. I wont be updating it in the same fashion again. Sorry and thanks in advance. :slight_smile:

Need to double check, but believe you are missing a closing parentheses ‘}’ on approx line 27. After setCalcFieldValue(calc_input, value);

It’s kind of why I would argue that using the built in ‘minu’ function, is a better way to go than editing the formCalc code. I think it’s potentially a bit of dodgy path to go down.

Regardless it would be an idea to make a back up, once you do get it working:)

In other words this needs to be closed e.g.

 return function(value)
     { <- opening
         if (calc_input.id === "Commission_On" && value < 0) {
             value = 0;
         }
         setCalcFieldValue(calc_input, value);
     } <- closing

Thanks, I have tried both the things you suggested but to no avail. I wonder if it would be easier just to start again with an form and go from there to get it finished? Then learning more of how to build forms as I have time.

This was how it looked with your first suggestion https://www.salonlogic.co.uk/form.php
And this was one with Bootstrap that I was still using the functions from the form above in (unsuccessfully as no formcalc I assume) https://www.salonlogic.co.uk/scribble2.php

Scotty55 I have just looked at https://www.salonlogic.co.uk/forms/Commission/scripts/jquery.sim.FormCalc.js

You need to add the closing bracket as I showed you

On that page, copying the JavaScript code into beautifier.io I see that the syntax error is because the }) has been close off at the wrong place just before the targetMultiplier event listener, , and needs to be moved to the end of the code instead.

The browser console says:

Bootstrap's JavaScript requires jQuery. jQuery must be included before Bootstrap's JavaScript.

You need to fix that before any further progress can be made.

I think I have done that now. It works but the commission on remains at zero constantly.
756 actual gross = 630 nett so zero commission
900 actual should show a commission on of 150

On which page? There is still a syntax error at https://www.salonlogic.co.uk/form.php
And https://www.salonlogic.co.uk/scribble2.php still complains about no jQuery.

Sorry on the salonlogic.co.uk/forms/Commission/Commission.php

In the scribble one I have linked to Jquery

 <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js" integrity="sha384-1CmrxMRARb6aLqgBO7yyAxTOQE2AKb9GfXnEo760AUcUmFx3ibVJJAzGytlQcNXd" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>	      <title>Calculate Commission and Total Payroll Cost</title>
  </head>

I see that the browser console reports exactly what problem your code is having. I recommend that you investigate that as it helps you to fix most problems.

You have linked it in the wrong place though. Please reread the error message as it gives the details there. The browser console is where you find that too.

Sorry Paul, I’m just not getting it. I forgot I had saved your code from last night so I have pasted it in but still an error. (commission form) On the other form I see the script in the head but bootstrap says at bottom of page

 function makeCalcUpdateCallback(calc_input)
            {
               return function(value)
                        {
                           if (calc_input.id === "Commission_On" && value < 0) {
                               value = 0;
                           }
                           setCalcFieldValue(calc_input, value);
            }
            function setCalcFieldValue(calc_field,value)
			}
                if(typeof value == 'number' && isNaN(value))

The code is still wrong Scotty55

This is taken from formCalc before it was amended

$.fn.formCalc = function(formula,options_param)
    {
        var calcobjs = [];
        var options = options_param || {value_maps:false,currency_format:false};

        this.filter('input').each(function()
        {
            function makeCalcUpdateCallback(calc_input)
            {
                return function(value)
                        {
                           setCalcFieldValue(calc_input,value);
                        }
            }
            function setCalcFieldValue(calc_field,value)
            {
                if(typeof value == 'number' && isNaN(value))

This is how it should look be with the amended code

$.fn.formCalc = function(formula,options_param)
    {
        var calcobjs = [];
        var options = options_param || {value_maps:false,currency_format:false};

        this.filter('input').each(function()
        {
            function makeCalcUpdateCallback(calc_input)
            {
               return function(value)
                        {
                           if (calc_input.id === "Commission_On" && value < 0) {
                               value = 0;
                           }
                           setCalcFieldValue(calc_input, value);
                        }
            }
            function setCalcFieldValue(calc_field,value)
            {
                if(typeof value == 'number' && isNaN(value))

Can you see the difference to your code?

A newline needs adding after setCalcFieldValue(calc_input, value);
and a closing bracket ‘}’

The closing bracket under function setCalcFieldValue(calc_field,value)
needs changing to an open bracket ‘{’

Just compare yours and the above examples

Edit: A bit odd that this has now been removed from the current formCalc script

minu:function(total_p,perc_p)
{
  var total = convfx.getNumber(total_p);
  var perc  = convfx.getNumber(perc_p);
  if((total-perc)>=0){
     return(total-perc);
  } else { return(0); }
},
1 Like