Calculating based on multiple fields input

mkusens 18 Sep, 2012
I have a customer signup form (http://www.eservicefl.com/sign-up) and need to be able to calculate two values based on two of the fields. Basically, I will be charging clients a signup fee and a monthly usage fee that will be based on the values in the referral code (referralcode) and # of Attorney's (numattorneys) fields. These values are then going to be passed into the HTML to PDF action so that a completely filled out contract will be sent to the client via email as a PDF attachment. I have the rest of the form already working with the HTML to PDF action but just need help in filling out these two fields automatically. I saw several posts on using php code to do math based on fields but mine needs a bit more logic then that. Basically the logic I need is:

If numattorneys = 1 to 5 then monthlyfee = ($20 x numattorneys x (if (referralcode = BAR25 or SERVE25) .75 otherwise 1)), If numattorneys = 6 to 15 then monthlyfee = ($18 x numattorneys x (if (referralcode = BAR25 or SERVE25) .75 otherwise 1)), If numattorneys = 16 to 30 then monthlyfee = ($15 x numattorneys x (if (referralcode = BAR25 or SERVE25) .75 otherwise 1)).

Also I would need to perform this calculation:

If numattorneys = 1 to 5 then setupfee = ($150 x numattorneys x (if (referralcode = BAR25 or SERVE25) .75 otherwise 1)), If numattorneys = 6 to 15 then setupfee = ($225 x numattorneys x (if (referralcode = BAR25 or SERVE25) .75 otherwise 1)), If numattorneys = 16 to 30 then setupfee = ($300 x numattorneys x (if (referralcode = BAR25 or SERVE25) .75 otherwise 1)).

I am more then happy to buy as much beer as you want if you can assist me with writing this code. Please let me know if you can assist and I will PM or email you super admin credentials.
GreyHead 18 Sep, 2012
Hi mkusens,

I'm assuming that you are using CFV4 and that you want this calculation after the form is submitted.
<?php
$numattorneys =& $form->data['numattorneys'];
if ( $numattorneys > 0 && $numattorneys <=5 ) {
  $mrate = 20;
  $srate = 150;
} elseif ( $numattorneys > 5 && $numattorneys <= 15 ) {
  $mrate = 18;
  $srate = 225;
} elseif ( $numattorneys > 15 && $numattorneys <= 30 ) {
  $mrate = 15;
  $srate = 300;
}
$discount = 0;
$referralcode =& $form->data['referralcode'];
if ( $referralcode == 'BAR25' || $referralcode == 'SERVE25' ) {
  $discount = .25;
}

$form->data['monthlyfee'] = $numattorneys * $mrate * (1 - $discount);
$form->data['setupfee']   = $numattorneys * $srate * (1 - $discount);
?>

Note: this doesn't handle the cases where numattorneys is < 1 or > 30 - I assume that some other validation is used to check this.

Bob
mkusens 18 Sep, 2012
Thanks Bob. I will try out the code tonight and let you know how it works. In the meantime, I bought you some beer. See attached file.
GreyHead 18 Sep, 2012
Hi mkusens,

Thank you, very much appreciated. Let me know if it doesn't all work smoothly.

Bob
mkusens 18 Sep, 2012
Bob - I tried the code in a custom Code event on submit (in CFv4), before the email and HTML to PDF events. I also made sure there are hidden fields on my form for the fields {setupfee} and {monthlyfee} but neither shows up on my PDF attached to the email or in the email body when I use those fields. I am also getting an error upon submit that shows up in the same page as the thanks message. I've attached a screenshot.

Also, yes the validation for the numattorneys is that the values are in a drop down list that forces them to select a number between 1 and 30.

The URL for the form on the frontend is:

http://www.eservicefl.com/sign-up

Thanks,

Michael
GreyHead 19 Sep, 2012
Hi Michael,

Sorry, I missed a ; at the end of this line
  $srate = 225;


Bob
mkusens 19 Sep, 2012
That got rid of the error and passed the values to the HTML to PDF action now. One tweak I made is in the last line, because the setup fee is per client, not attorney so I just removed the "$numattorneys *" from that line and now all calculations are done properly. Thanks a ton and below is the final code for anyone else who needs it.

<?php
    $numattorneys =& $form->data['numattorneys'];
    if ( $numattorneys > 0 && $numattorneys <=5 ) {
      $mrate = 20;
      $srate = 150;
    } elseif ( $numattorneys > 5 && $numattorneys <= 15 ) {
      $mrate = 18;
      $srate = 225;
    } elseif ( $numattorneys > 15 && $numattorneys <= 30 ) {
      $mrate = 15;
      $srate = 300;
    }
    $discount = 0;
    $referralcode =& $form->data['referralcode'];
    if ( $referralcode == 'BAR25' || $referralcode == 'SERVE25' ) {
      $discount = .25;
    }

    $form->data['monthlyfee'] = $numattorneys * $mrate * (1 - $discount);
    $form->data['setupfee']   = $srate * (1 - $discount);
    ?>
Shelbourne 18 Oct, 2012
I feel like I am missing something really obvious on this thread but I can't figure it out. I am trying to calculate a value for a hidden field using PHP in the custom code just like this, but the hidden field won't update. Even a simple piece of code used for testing purposes won't work:

For example, in the custom code on submit I have:

<?php
$form->data['total'] = Dog;
?>

In the email that sends after, the "total" field is blank (as it is by default). If I move this piece of custom code to the On Load area, the value is set appropriately.

Any assistance would be appreciated.
GreyHead 18 Oct, 2012
Hi Shelbourne,

It should work provided that the Custom Code action is before the Email action and you add the quotes round 'Dog'.

Bob
This topic is locked and no more replies can be posted.