-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmanage_emi.ds
More file actions
110 lines (102 loc) · 4.29 KB
/
Copy pathmanage_emi.ds
File metadata and controls
110 lines (102 loc) · 4.29 KB
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
/*
This is a sample Custom Function written to be used in Workflow automation of Zoho Books.
This function will help you to manage fixed installment payments using Zoho Books.
Please ensure to replace 'cf_number_of_installments' with invoice level custom field api name
Also, replace '419492XXX001877001' with template id of Retainer invoice.
Create a connection by the name "zbooks" for Zoho Books service.
Module - Invoice
Workflow Type - Event Based
When Invoice is - Edited
When any field is updated
Filter the triggers - Status is Sent AND Number Of instalment is not Empty AND Start Date is not Empty.
Actions - Type: Custom function; Name: Choose the custom function that you have created
*/
invoiceID = invoice.get("invoice_id");
invoicedate = invoice.get("date").toDate();
organizationID = organization.get("organization_id");
apiEndPoint = organization.get("api_root_endpoint");
PDFtemplateIdForRetainer = "419492XXX001877001";
installmentFieldApiName = "cf_number_of_installments";
numberofInstallMents = invoice.get("custom_field_hash").get(installmentFieldApiName);
installmentMonth = 0;
note = "Dear customer,\nTotal purchase amount = " + invoice.get("total") + "\nRemaining Balance = " + invoice.get("balance");
perMothEmi = invoice.get("total").toDecimal() / numberofInstallMents.toDecimal();
invoiceBalance = invoice.get("balance");
invoiceNumber = invoice.get("invoice_number");
retainerFetchUrl = apiEndPoint + "/retainerinvoices?reference_number_contains=" + encodeUrl(invoiceNumber) + "&organization_id=" + organizationID + "&response_option=2";
getRelatedRetainers = invokeurl
[
url :retainerFetchUrl
type :GET
connection:"zbooks"
];
// info getRelatedRetainers;
installmentMonth = getRelatedRetainers.get("page_context").toMap().get("total");
if(installmentMonth == 0)
{
thisEmi = 1;
}
else
{
thisEmi = installmentMonth.toDecimal() + 1;
}
remaining = numberofInstallMents.toDecimal() - thisEmi.toDecimal();
remaininginstallment = remaining.toDecimal() + 1;
note = "Dear customer,\n\nTotal purchase amount = " + invoice.get("total") + "\nYou have a remaining balance of " + invoice.get("balance") + " to be paid in " + remaininginstallment;
if(invoiceBalance >= 0)
{
retainerMap = Map();
retainerMap.put("customer_id",invoice.get("customer_id"));
retainerMap.put("reference_number",invoice.get("invoice_number"));
lineList = List();
itemMap = Map();
itemMap.put("description","Installment " + thisEmi + " - " + today);
itemMap.put("rate",perMothEmi);
lineList.add(itemMap);
retainerMap.put("line_items",lineList);
retainerMap.put("notes",note);
retainerMap.put("template_id",PDFtemplateIdForRetainer);
createRI = zoho.books.createRecord("retainerinvoices",organizationID,retainerMap,"zbooks");
info createRI.get("message");
info retainerMap;
if(createRI.get("code") == 0)
{
retainerInvoiceID = createRI.get("retainerinvoice").get("retainerinvoice_id");
customerID = invoice.get("customer_id");
contactDetails = invokeurl
[
url :apiEndPoint + "/contacts/" + customerID + "?organization_id=" + organizationID
type :GET
connection:"zbooks"
];
contactDetails = contactDetails.get("contact");
contactPersons = contactDetails.get("contact_persons").toList();
for each findCP in contactPersons
{
primaryContacts = findCP.get("is_primary_contact");
if(primaryContacts == true)
{
email = findCP.get("email");
}
}
subject = "Your Monthly Installment Payment is Due for " + organization.get("name");
body = "Dear Customer,\n\nKindly note that your installment payment for this month is pending. Please take a moment to review the payable amount enclosed in the attached invoice and settle it at your earliest convenience.\n\nShould you have any questions or need further clarification, feel free to get in touch with us. \n\nRegards,\n" + user.get("name") + "\n" + organization.get("name");
json = Map();
tlist = List();
tlist.add(email);
json.put("to_mail_ids",tlist);
json.put("subject",subject);
json.put("body",body);
params = Map();
params.put("JSONString",json);
params.put("attach_pdf",true);
response = invokeurl
[
url :apiEndPoint + "/retainerinvoices/" + retainerInvoiceID + "/email?organization_id=" + organizationID
type :POST
parameters:params
connection:"zbooks"
];
info response;
}
}