Skip to content
  • There are no suggestions because the search field is empty.

How to: Incorporate Calculations in PDF Templates

PDF Templates can be used from anything to generating a check-out agreement, to even creating invoices for rentals that clients can e-sign.

Some of the more important functions that can be used with PDF templates are the following:

  • Sum
    • Sum()
  • For loops
    • [for item in items]
  • Calculation of number of days for a booking
    • ((due-started).days)
  • Formatting displayed values to two decimal places
    • "%.2f" %


Additionally, you can combine these in different ways to yield different values that can be used to generate invoices, or other type of PDF files. Below is an example PDF template incorporating all of these functions:

 

Incorporating Functions For Calculations

The perfect example of how to start using these functions is to multiply a custom field for value, or rental rate, by the number of days the item is to be out for the duration of the booking. This would look something like the following:

${(item.fields.Daily_Rental_Rate or 0.00) * ((due-started).days or 1)}

Note: The above example uses a custom field called "Daily Rental Rate." Please don't forget to include the symbol for the currency you wish to display before the block of code.

The addition of "or 0.00" and "or 1" are to account for items that may not have a value in the custom field, and for a booking that is returned on the same day, you would want that to count as 1 day instead of 0.


You can then combine the sum and for loop functions to generate a grand total for the above example. We will need to re-use the above calculation to find the individual "total" value for each item, while then adding the value of each line item in the booking to a sum. The code would look something like this:

$${sum([((item.fields.Daily_Rental_Rate or 0) * ((due-started).days or 1)) for item in items])}

Note: First we call the sum function that has logic to pull the total value of each item for the booking, and then we repeat this for each item that is part of the booking by using "for item in items."

 

Calculation Discounts or Taxes

You are also able to incorporate taxes or discounts by grabbing those totals and multiplying them by specific values. For example, if you'd like to find the 6% tax rate for the entire booking, you can multiply each value that is part of the sum by 0.06, as follows:

Total Tax (6% rate): $${sum([(((item.fields.Daily_Rental_Rate or 0) * 0.06 ) * ((due-started).days or 1)) for item in items])}

If you would like to give discounts, or include taxes in a grand total, you can change the value multiplied. For example to include a 20% discount, you will want to multiply each value by 0.8 (80% of the total value of the item). Or if you want to include the tax in the grand total, you will want to multiply it by 1.06 (assuming a 6% tax rate). The reason 1.06 is used as the value, is because multiplying by 1 gives you the 100% value of the item, and then we're adding 0.06 to it for representing the 6% tax rate on the value of each item.

The above calculation for a grand total including taxes or for giving a flat 20% discount, would then be displayed as follows:

Grand Total (incl. 6% tax): $${sum([(((item.fields.Daily_Rental_Rate or 0) * 1.06 ) * ((due-started).days or 1)) for item in items])}

Sub total with 20% discount: $${sum([(((item.fields.Daily_Rental_Rate or 0) * 0.8) * ((due-started).days or 1)) for item in items])}

 

Formatting Totals For Two Decimal Points

Calculations can, at times, be displayed as $299.982 instead of $299.98, which doesn't really represent currency as it is used. To address this, you can insert a final bit of additional code at the very front of your calculation: "%.2f" %. This used to format the result shown to two decimal points, to properly display as currency is used. The final version of our Grand Total calculation that includes a 6% tax, would then look as follows:

Grand Total (incl. 6% tax):  $${"%.2f" % sum([(((item.fields.Daily_Rental_Rate or 0) * 1.06 ) * ((due-started).days or 1)) for item in items])}