Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Calculated merge fields allow you to use a scripting language (PHP) to perform calculations. Creating calculated merge fields is an advanced topic and requires a some understanding of computer programming and in particular PHP.  However it is fairly easy to create simple calculations by performing simple math on variables. 

Info

Note for security reasons only a limited subset of the PHP commands are available.

A calculated merge field is typically used when you need to add a new field to a document that is based on a calculation of existing fields. For example if you have designed a workflow for a property settlement you may need to calculate the rates apportionment on settlement day by dividing the amount already paid by how many days the new buyer will benefit from those rates after settlement day.  This amount usually gets added to the final amount payable at settlement.  This is easily achieved with a calculated merge field.

Creating a new calculated merge field

From the top navigation bar go to Admin / "Document Templates" / "List of Merge Fields". This will show a list of all available merge fields in the system.  

Tip
Tip: If you just want to see existing calculated merge fields then filter the list on "Data Source" = "Calculated Field".

...

As you saw in the previous examples you can use custom data fields in your expressions. To make this even easier there are two special variable names which give you direct access to single-row and multi-row data collection fields.

Accessing single-row data fields

Code Block
$singleRowData['data-collection-name']['data-field-name']

So in the date example above we could use this alternative syntax to access the settlement date:

Code Block
$settlement = date('Y-m-d', strtotime($singleRowData['conv_settlement']['settlement_date']));

Accessing multi-row data fields

Code Block
$multiRowData['data-collection-name'][0…n]['data-field-name']

...

You can use calculated fields inside invoice templates. Just remember to treat each merge field as a string and enclose it in quotes. However, keep in mind that many merge fields used in invoice templates will give a result as a currency and when trying to do a calculation where the numbers are in currency will not work.

For example

...

First you will have to strip out the '$' symbol and any commas that appear so that you just get the numbers (ie, you want to turn '$1,234.00' into '1234'.

Use the following to convert a currencey into a plain number:

Code Block
floatval(str_replace(array('$', ','), '', '[[SP_TotalInclusive]]'));

It would would be easier to follow if you defined each variable that you are going to use in your calculation then run the calculation. In the example below we are trying to calculate the tax on an invoice but taking the invoice total inclusive of tax:

[[SP_TotalInclusive]]

Less the total of the invoice excluding tax:

[[SP_TotalExclusive]]

The first two lines strip the dollar symbol, commas and decimal points from the value of each and defines them so they can be used in the calculation on the third line.

The last line displays the result of the calculation but also converts it back to being displayed as a currency.

Code Block
$Total_Inc = (floatval(str_replace(array('$', ','), '', '[[SP_TotalInclusive]]' -)));
$Total_Exl = (floatval(str_replace(array('$', ','), '', '[[SP_TotalExclusive]]')));
echo $x;$calculation = $Total_Inc - $Total_Exl;
echo '$' . number_format($calculation, 2);