Dynamic calculation of price based on the calculated distance

Dynamic calculation of price based on the calculated distance

Requirement

Calculate the cost of a taxi or hired car based on distance.

Use Case

A cab booking application calculates and displays the approximate cost of a trip based on pickup and drop-off addresses, after which the user can choose to book the ride in the Rent Requisition form by clicking a button.

See how it works

Steps to follow

1. Create two forms with the following details:
Form
Form Link Name
Field Type
Field Name
Field Link Name
Rent Requisition
Rent_Requisition
Name
Name
Name
Phone
Phone
Phone_Number
Date-Time
Travel Date & Time
Travel_Date_Time
Single Line
Starting Point
Starting_Point
Single Line
Drop
Drop
Booking
Booking
Single Line
Starting Point
Starting_Point
Single Line
Drop
Drop
Decimal
Distance
Distance
Currency
Price
Price

2. Duplicate the Booking form to create a stateless form. Rename it Booking Estimate. We are using a stateless form because we are just using it to display the estimate. Some customers may just browse or check for the price.
 
3. Click on the Form Properties icon in the form builder header of the Booking Estimate form.
 
4. In the slider, remove the default buttons: Submit and Reset.


5. Create a button by clicking Add Button.


6. A new button named Button is created. Rename it to Book Now! as shown below, by clicking the three dots to the right of the button name.


7. In the same way, create a button named Calculate Price.
 
8. Create a workflow on the Booking Estimate form, to be executed upon loading the form, to disable the Price and Distance fields.

9. Click Add New Action. Add the below code to disable the Price field:
  1. disable Price;
  2. disable Distance;

10. To calculate an estimate for the trip based on the distance traveled, create another workflow to execute when the Calculate Price button is clicked.
 
11. Click Add New Action and add the below code to calculate Distance and Price. We are using the Zoho Maps Deluge method to find the distance between two locations.
  1. //Calculate the distance between Start and Drop in km. We are using trim() to remove any spaces in the Starting Point and Drop
  2. dist = round(zoho.map.distanceBetween(input.Starting_Point.trim(),input.Drop.trim(),"KILOMETRE"), 2);
  3. //Assuming driver cost to be Rs.350
  4. driverCost = 350;
  5. //Assuming the driving cost to be Rs.17/km
  6. ratePKm = 17;
  7. //Calculating the prime estimate
  8. price = round(driverCost + (dist * ratePKm) , 2);
  9. //Since clicking the button refreshes the form, we are reopening the form after calculation¯with the values.
  10. openUrl("#Form:Booking_Estimate?Starting_Point=" + input.Starting_Point + "&Drop=" + input.Drop + "&Distance=" + dist + "&Price=" + price , "same window");

This will display the estimate of the cost of the journey.

12. Create a workflow for booking the cab when the Book Now! button is clicked after the estimate is shown.
 
13. Click Add New Action and click Deluge Script. Add the below script to open the Rent Requisition form with the Starting_Point and Drop fields already populated.
  1. openUrl("#Form:Rent_Requisition?Starting_Point=" + Starting_Point + "&Drop=" + Drop, "same window"); 
The customer will enter the Travel Date & Time and rider details.

See how it works

 

Points to Note

  • The distance-between method can be used on two address fields with the following Deluge code:
  1. zoho.map.distanceBetween(Starting_Point, Drop, "KILOMETRE");