Zoho FSM | Calculate Work Order Profit

Calculate Work Order Profit

Alert
Heads up! Zoho FSM now includes a native Job Costing feature! This custom implementation guide was originally provided as a workaround before the built-in feature was available.

If you have implemented this (or similar custom solutions), it is advisable to remove them before enabling the Job Costing feature. This will prevent any conflicts or unnecessary overlap between your custom setup and the new built-in functionality.

Use case: Calculate the profit earned for a work order.
 
Profit = Grand Total of the work order - Total cost price of the work order

where,
Total cost price of the work order = Total cost price of the services used + Total cost price of the parts used
 
Total cost price of the services used = (Cost Price of the service1 X Quantity used) + (Cost Price of the service2 X Quantity used) +....+ (Cost Price of the serviceN X Quantity used)
 
Total cost price of the parts used = (Cost Price of the part1 X Quantity used) + (Cost Price of the part2 X Quantity used) +....+ (Cost Price of the partN X Quantity used)

Follow the steps below to implement this use case:
Step 1: Add custom fields
Step 2: Create a custom function
Step 3: Create a workflow rule

Step 1: Add custom fields 

Add the following custom fields:
 
Module
Field Name
Field Type
Description
Services And Parts (Services layout)
Cost Price
Decimal
For a service, enter the cost/purchase price of the service in this field.
Services And Parts (Parts layout)
Cost Price
Decimal
For a part, enter the cost/purchase price of the part in this field.
Work Orders
Total Cost Price
Decimal
Whenever the workflow is triggered, this field will be populated with the total cost price of the work order.
Work Orders
Total Profit
Decimal
Whenever the workflow is triggered, this field will be populated with the profit earned for the work order.
 
When you add a new field to the Services module, that field will automatically get added to the Unused Fields of the Parts module and vice versa. You can drag and drop the field from the Unused Fields section to the module layout.
 

Step 2: Create a custom function   

Create a custom function to calculate the Total Cost Price and Total Profit of a work order and to populate these values in the Total Cost Price, and Total Profit fields of a work order.
  1. Navigate to Setup > Automation > Functions and click Create Function.
  2. Enter the following details and click Save:
    1. Function Name: CalculateWOProfit
    2. Module: Work Order
    3. In the Deluge Script Editor, enter the following script:

serviceLineitems = work_order.get("Service_Line_Items");
//info serviceLineitems;
servicepricemap = Map();
totalPurchased = 0;
for each  sitem in serviceLineitems
{
 serviceId = sitem.get("Service").get("id");
 sitemquantity = sitem.get("Quantity");
 info sitemquantity;
 if(!servicepricemap.containKey(serviceId))
 {
  sinfo = zoho.fsm.getRecordById("Service_And_Parts",serviceId);
  //info sinfo;
  servicepurchased = sinfo.get("data").get(0).get("Cost_Price__C");
  //info servicepurchased;
  if(servicepurchased != null && servicepurchased != "")
  {
   servicepricemap.put(serviceId,servicepurchased);
  }
  else
  {
   servicepricemap.put(serviceId,sinfo.get("data").get(0).get("Unit_Price"));
  }
 }
 if(servicepricemap.get(serviceId) != null)
 {
  purchseval = servicepricemap.get(serviceId);
  totalPurchased = totalPurchased + sitemquantity * purchseval;
  //info "totalPurchased: " + totalPurchased;
 }
}
partlineitems = work_order.get("Part_Line_Items");
partmap = Map();
totalPartsPurchased = 0;
for each  pitem in partlineitems
{
 partId = pitem.get("Part").get("id");
 pitemquantity = pitem.get("Quantity");
 if(!partmap.containKey(partId))
 {
  pinfo = zoho.fsm.getRecordById("Service_And_Parts",partId);
  //info sinfo;
  partpurchased = pinfo.get("data").get(0).get("Cost_Price__C");
  if(partpurchased != null && partpurchased != "")
  {
   partmap.put(partId,partpurchased);
  }
  else
  {
   partmap.put(partId,pinfo.get("data").get(0).get("Unit_Price"));
  }
 }
 if(partmap.get(partId) != null)
 {
  partpurchaseVale = partmap.get(partId);
  totalPartsPurchased = totalPartsPurchased + pitemquantity * partpurchaseVale;
  //info "totalPartsPurchased: " + totalPartsPurchased;
 }
}
//info "totalPartsPurchased: " + totalPartsPurchased;
//info "totalPurchased: " + totalPurchased;
totalpurchasedWo = totalPartsPurchased + totalPurchased;
info "totalpurchasedWo: " + totalpurchasedWo.toDecimal();
wototal = work_order.get("Grand_Total");
info "wototal:" + wototal;
totalprofit = wototal - totalpurchasedWo;
info "Profit: " + totalprofit.toDecimal();
info zoho.fsm.updateRecord("Work_Orders",work_order.get("id"),{"Total_Cost_Price__C":totalpurchasedWo.toDecimal(),"Total_Profit__C":totalprofit.toDecimal()});

In the above code snippet, the API names of the custom fields used are highlighted in blue.  Replace these with the API names of the custom fields ypu have created. You can find the API names of the custom fields in the module builder (Setup > Customization > Module and Fields) of the respective modules.
 

Step 3: Create a workflow rule

Create a workflow rule in Zoho FSM that will update a work order with the Total Cost Price and Total Profit of the work order.
  1. Go to Setup > Automation > Workflow Rules and click Create Workflow.
  2. Enter the following details, then click Next:
    1. Module: Work Orders
    2. Rule Name: CalculateWOProfit
    3. Description: Calculate the profit earned for a work order



  3. Select the rule trigger as Created or Edited and click Next.
    Select the checkbox Repeat this workflow whenever a Work Order is edited.



  4. Select the rule criteria as To all Work Orders and click Next.



  5. Click +Action and select Function.



  6. Select Existing Functions and click Next.



  7. Select the custom function created in the previous step.



  8. Click Save.


Testing the use case

Provide the Cost Price for the Services and Parts.


 
Create or edit a work order. The Total Cost Price and Total Profit of the work order.will be populated.