Profit margins on Estimates, Sales order & Invoices

Profit margins on Estimates, Sales order & Invoices

I've written a little script for a custom button if anyone is interested.
Just go to "Preferences" | Sales orders | Custom Buttons, click "New"
If you want it on your Invoices or estimates, then do this in the Invoices or estimates preferences & change any mention of salesorder below to invoice or estimate
Enter Custom button name = "Margins" - or whatever you prefer.
Delete the sample function.
Copy & paste the code below.....
Enter your organization id (bold / red)
Click "Save"
Refresh your browser & then go to the Sales order module, select a sales order, click "More" | "More Actions" | Select "Margins" or whatever you named the custom button !
Update: As related lists are now available in sales orders, invoices & estimates, this looks better in a "Related list".

orgId = "Your organization Id"; // enter your organization id here
resultMap = Map();
marginHeader = List();
listData = List();
marginHeader.add({"key":"itemName","value":"Name"});
marginHeader.add({"key":"qty","value":"Qty","align":"right"});
marginHeader.add({"key":"cost","value":"Cost","align":"right"});
marginHeader.add({"key":"lineValue","value":"Line Cost","align":"right"});
marginHeader.add({"key":"lineSale","value":"Line Sale","align":"right"});
marginHeader.add({"key":"profit","value":"Profit","align":"right"});
marginHeader.add({"key":"margin","value":"Margin","align":"right"});
items = salesorder.get("line_items");
totalCost = 0;
totalProfit = 0;
totalMargin = 0;
for each  line in items
{
try
{
id = line.get("item_id");
details = zoho.books.getRecordsByID("items",orgId,id);
details = details.get("item");
name = details.get("name");
marginDetails = Map();
qty = line.get("quantity").round(2);
sale = line.get("item_total").round(2);
costPrice = details.get("purchase_rate").round(2);
lineValue = (qty * costPrice).round(2);
itemCost = qty * costPrice;
totalCost = totalCost + lineValue;
lineTotal = line.get("item_total");
profit = sale - itemCost;
totalProfit = totalProfit + profit;
margin = (profit / lineTotal * 100).round(2);
totalMargin = totalMargin + margin;
marginDetails.put("margin",margin.round(0) + "%");
marginDetails.put("itemName",name);
marginDetails.put("qty",qty.round(2) + "");
marginDetails.put("cost",costPrice.round(2) + "");
marginDetails.put("lineValue",lineValue.round(2) + "");
marginDetails.put("lineSale",sale.round(2) + "");
marginDetails.put("profit",profit.round(2) + "");
listData.add(marginDetails);
} catch(e) {}
}
seperator = Map();
seperator.put("seperator","");
listData.add(seperator);
saleValue = salesorder.get("sub_total");
saleValue = toDecimal(saleValue);
totalMargin = totalProfit / saleValue * 100;
totalDetails = Map();
totalDetails.put("itemName","Nett : ");
totalDetails.put("lineValue",totalCost.text("##,###.00") + "");
totalDetails.put("profit",totalProfit.text("##,###.00"));
totalDetails.put("lineSale",salesorder.get("sub_total").text("##,###.00"));
totalDetails.put("margin",totalMargin.round(0) + "%");
listData.add(totalDetails);
resultMap.put("header_context",marginHeader);
resultMap.put("data",listData);
gross = toDecimal(salesorder.get("total"));
gtotalCost = totalCost;
gprofit = totalProfit;
gmargin = gprofit / gross * 100;
gtotalDetails = Map();
gtotalDetails.put("itemName","Gross : ");
gtotalDetails.put("profit",gprofit.text("##,###.00"));
gtotalDetails.put("lineValue",gtotalCost.text("##,###.00") + "");
gtotalDetails.put("lineSale",gross.text("##,###.00"));
gtotalDetails.put("margin",gmargin.round(0) + "%");
listData.add(gtotalDetails);
resultMap.put("data",listData);
return resultMap;





                            Zoho Desk Resources

                            • Desk Community Learning Series


                            • Digest


                            • Functions


                            • Meetups


                            • Kbase


                            • Resources


                            • Glossary


                            • Desk Marketplace


                            • MVP Corner


                            • Word of the Day



                                Zoho Marketing Automation


                                        Manage your brands on social media



                                                Zoho TeamInbox Resources

                                                  Zoho DataPrep Resources



                                                    Zoho CRM Plus Resources

                                                      Zoho Books Resources


                                                        Zoho Subscriptions Resources

                                                          Zoho Projects Resources


                                                            Zoho Sprints Resources


                                                              Qntrl Resources


                                                                Zoho Creator Resources


                                                                  Zoho WorkDrive Resources



                                                                    Zoho Campaigns Resources

                                                                      Zoho CRM Resources

                                                                      • CRM Community Learning Series

                                                                        CRM Community Learning Series


                                                                      • Tips

                                                                        Tips

                                                                      • Functions

                                                                        Functions

                                                                      • Meetups

                                                                        Meetups

                                                                      • Kbase

                                                                        Kbase

                                                                      • Resources

                                                                        Resources

                                                                      • Digest

                                                                        Digest

                                                                      • CRM Marketplace

                                                                        CRM Marketplace

                                                                      • MVP Corner

                                                                        MVP Corner

                                                                      





                                                                      




                                                                          Design. Discuss. Deliver.

                                                                          Create visually engaging stories with Zoho Show.

                                                                          Get Started Now