-
Notifications
You must be signed in to change notification settings - Fork 6
/
quotes_transaction_level_profitability.ds
121 lines (114 loc) · 3.9 KB
/
quotes_transaction_level_profitability.ds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*====================================================================
This is a sample Custom Function written to be used in the Automation section of Zoho Books.
The function calculates the profit margin of a quote and populates the value in a custom field.
The function calculates the profit margin of a quote and populates the value in a custom field.
Create a Quote-level Custom field with the name "Total Margin" and Data Type as "Decimal".
Copy the API Field Name of the custom field that you have created and update the value of the variable "totalMarginPlaceholder".
Create a connection by the name "zbooks" for the Zoho Books service.
Create Workflow as below
Workflow name: Enter a suitable name of your choice
Module: Quotes
Workflow type: Event Based
When a contact is :Created or Edited
Filter the triggers: (Optional)
Actions: Type: Custom function; Name: Enter the name of your choice.
====================================================================*/
estimateID = estimate.get("estimate_id");
estimatedate = estimate.get("date").toDate();
organizationID = organization.get("organization_id");
customerID = estimate.get("customer_id");
currencyID = estimate.get("currency_id");
dc = organization.get("data_center_extension");
apiUrl = organization.get("api_root_endpoint");
orgCurrencyID = organization.get("currency_id");
subTotal = estimate.get("sub_total").toDecimal();
totalMarginPlaceholder = "cf_total_margin"; //Replace cf_total_margin with api name of the estimate custom field.
if(currencyID != orgCurrencyID)
{
currentDate = zoho.currentdate;
currentDate = estimatedate.toString("yyyy-MM-dd");
url = apiUrl+"/settings/currencies/" + currencyID + "/exchangerates?from_date=" + currentDate + "&organization_id=" + organizationID;
exchangeRate = invokeurl
[
url :url
type :GET
connection:"zbooks"
];
exchangeRate = exchangeRate.get("exchange_rates").toMap();
exchangeRate = exchangeRate.get("rate").round(3);
exRate = exchangeRate.toDecimal();
exchangeFlag = true;
}
estimateUrl = apiUrl+"/estimates/editpage?estimate_id=" + estimateID + "&organization_id=" + organizationID;
estimateDetails = invokeurl
[
url :estimateUrl
type :GET
connection:"zbooks"
];
estimateDetails = estimateDetails.get("estimate");
lineItems = estimateDetails.get("line_items").toList();
totGmargin = 0;
for each findItem in lineItems
{
perUnitDiscount = 0;
itemSellingPrice = 0;
itemPurchasePrice = 0;
discountAmt = 0;
gMargin = 0;
iMap = Map();
item = findItem.toMap();
itemID = item.get("item_id");
discount = findItem.get("discount");
discountAmt = findItem.get("discount_amount");
quantity = findItem.get("quantity").toDecimal();
if(discountAmt.isNull())
{
discountAmt = 0;
}
else
{
perUnitDiscount = discountAmt / quantity;
perUnitDiscount = perUnitDiscount.round(2);
}
if(exchangeFlag == true)
{
perUnitDiscount = perUnitDiscount * exRate;
}
getItemPurchasePrice = if(findItem.get("purchase_rate") != "",findItem.get("purchase_rate"),0);
unitSalesRate = findItem.get("bcy_rate").toDecimal();
if(getItemPurchasePrice.isNull())
{
unitPurchaseRate = 0;
}
else
{
unitPurchaseRate = getItemPurchasePrice.toDecimal();
}
unitMargin = unitSalesRate.toDecimal() - unitPurchaseRate;
unitMargin = unitMargin - perUnitDiscount;
totItemMargin = unitMargin * quantity;
totGmargin = totGmargin + totItemMargin;
totalProfitPer = (totGmargin / subTotal) * 100;
}
cMap = Map();
cList = List();
cMap.put("api_name",totalMarginPlaceholder);
cMap.put("value",totalProfitPer);
cList.add(cMap);
json = Map();
json.put("custom_fields",cList);
json.put("customer_id",customerID);
json.put("reason","The custom field has been updated");
params = Map();
params.put("JSONString",json);
updateUrl = apiUrl+"/estimates/" + estimateID + "?organization_id=" + organizationID;
updateRecord = invokeurl
[
url :updateUrl
type :PUT
parameters:params
connection:"zbooks"
];
info updateRecord.get("message");
info params;