Function #20: Custom calculation in item table of invoices

Function #20: Custom calculation in item table of invoices

When you create an invoice in Zoho Books, the 'Amount' of a line item is typically calculated as the product of the "Quantity" and the "Rate" of the item. For instance, if an item has a sales rate of $50 and a quantity of 5 is sold, then the amount would be calculated as follows: 
Amount = Quantity * Rate = $5 * $50 = $250.

There may be scenarios where you'd need to include a third variable to arrive at the item amount. Let's take the example of a rental business where you lend equipments and charge customers based on the duration of usage. In this case, the computation for the Amount field should be as follows:
Amount = Duration * Quantity of equipment lent * Rate.
For instance, if you rented out 2 pieces of equipment for 5 days at the rate of $10 per day, the amount should be calculated as 5 * 2 * $10=$100. 

In cases when you need to include a third variable for calculation, you can use today's custom function. We're providing you with a sample script that you can adjust to fit your specific requirements.

Prerequisites: 

1. Create a Connection named "zbooks" to successfully execute the function. You can watch the GIF attached below to know how to create the connection. 



2. Create the third variable (Days in this example) as an item-level custom field to be displayed in the sales transactions. You can check the GIF below for the steps. 



3. Similarly, create another custom field called " Unit Rate" to be displayed in the item table. The purpose of this custom field is to store the original sales rate of the line item. 





Custom Function:

Navigate to Settings -> Automation -> Custom Functions -> +New Custom Function > Add the function code from this GitHub link.
 
Workflow Rule: 
 
Go to Settings -> Automation -> Workflow Rules -> +New Workflow Rule and set up the workflow rule as shown below:-





In the final step of this workflow rule, associate the custom function you created and then hit Save.

Here's how the function will work:

The custom function will be executed once the invoice is saved. Initially, it copies the original sales rate to the Unit Rate (Custom) field. Then, it multiplies Days * Unit Rate and populates the result in the Rate (Default) field. Following this, Zoho Books performs the calculation of Rate (Default field) * Quantity and fills the result in the Amount field.

Note: If you wish to hide the "Rate" - default field at the template level, go to Settings > Templates > Invoices > Edit > Table > Disable the checkbox for Rate field > Save. Now the invoice templates will only display the Unit Rate (Custom field), Third variable field (Days), Quantity field, and their resultant in the Amount field.



Should you require any help in tailoring this function to better suit your requirements, don't hesitate to reach out to us at support[at]zohobooks[dot]com. We will be happy to assist you!

Regards,
Shireen Farhana S
Zoho Books