-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautomated_invoices.dg
119 lines (111 loc) · 4.39 KB
/
automated_invoices.dg
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
// Start by defining all the variables that you will need throughout the function, pulled from the CRM Contact record. Then call the Zoho Books Organizations API. You will have to do some sort of search based off name or something else if you have more than one organization.
contactRecord = zoho.crm.getRecordById("Contacts",CONTACT_ID);
info contactRecord;
email = contactRecord.get("Email");
firstName = contactRecord.get("First_Name");
lastName = contactRecord.get("Last_Name");
otherInfo = contactRecord.get("other_custom_field"); //other info as needed
getOrganizations = invokeurl
[
url :"https://books.zoho.com/api/v3/organizations"
type :GET
connection:"zohobooks"
];
info getOrganizations;
orgId = getOrganizations.get("organizations").get(0).get("organization_id");
info orgId;
// Now that we have our defined variables, we search the Contacts module for contacts with a matching email address. It returns a list of contacts (just one)
searchParam = {"email":email};
contactSearch = zoho.books.getRecords("Contacts",orgId,searchParam).get("contacts");
contactId = null;
contactPersonId = null;
// Check If Contact is Found. If (the list) is not empty, we query the primary contact's info to put on the invoice
if(!contactSearch.isEmpty())
{
// Get Contact Record
contact = contactSearch.get(0);
contactId = contact.get("contact_id");
contactDetails = zoho.books.getRecordsById("contacts",orgId,contactId).get("contact");
contactPersons = contactDetails.get("contact_persons").toList();
// Find Primary Contact
for each contactPerson in contactPersons
{
if(contactPerson.get("is_primary_contact"))
{
contactPersonId = contactPerson.get("contact_person_id");
}
}
}
else //(list is empty, create the Contact and Contact person in Books and get the info for the Invoice)
{
// Create Map for New Contact
newContactMap = Map();
newContactMap.put("email",email);
newContactMap.put("contact_name",firstName + " " + lastName);
newContactMap.put("contact_persons", {{"first_name":firstName,"last_name":lastName,"email":email}} );
// Create New Contact
createContact = zoho.books.createRecord("Contacts",orgId,newContactMap);
contact = createContact.get("contact");
contactId = contact.get("contact_id");
// Get Contact Person Info
contactPerson = contact.get("contact_persons").get(0);
contactPersonId = contactPerson.get("contact_person_id");
}
// Create New Invoice map and select a specific template if needed
invoice = Map();
invoice.put("customer_id",contactId);
invoice.put("date",today);
invoice.put("template_id","your_templateId");
contactPersonsList = List();
contactPersonsList.add(contactPersonId);
invoice.put("contact_persons",contactPersonsList);
invoice.put("line_items",{{"item_id":"2062836000120068237","quantity":"1","invoice_custom_field":otherInfo}});
//create a map for custom fields and add them to the invoice
customList = List();
customField1 = Map();
customField1.put("label","your_label");
customField1.put("value", ifnull(your_value, ""));
customList.add(customField1);
// // for second custom field
// customField2 = Map();
// customField2.put("label","your_2nd_label");
// customField2.put("value",ifnull(your_2nd_value,""));
// customList.add(customField2);
invoice.put("custom_fields", customList);
// Set Payment Options - this code enables both Paypal and Stripe, but you can of course customize it
paymentOptions = List();
paypalMap = Map();
paypalMap.put("gateway_name","paypal");
paypalMap.put("additional_field1","standard");
stripeMap = Map();
stripeMap.put("gateway_name","stripe");
paymentOptions.add(paypalMap);
paymentOptions.add(stripeMap);
gatewayMap = Map();
gatewayMap.put("payment_gateways",paymentOptions);
invoice.put("payment_options",gatewayMap);
// Create Invoice
createInvoice = zoho.books.createRecord("invoices",orgId,invoice);
info createInvoice;
//If the invoice was created successfully, we send the email with the invoice and payment options via a Zoho Books API call
code = createInvoice.get("code");
if(code == 0)
{
emailList = List();
emailList.add(email);
invoiceInfo = createInvoice.get("invoice");
invoiceId = invoiceInfo.get("invoice_id");
info invoiceId;
json = Map();
json.put("to_mail_ids",emailList);
params = Map();
params.put("JSONString",json);
emailInvoice = invokeurl
[
url :"https://books.zoho.com/api/v3/invoices/" + invoiceId + "/email?organization_id=" + orgId
type :POST
parameters:params
connection:"zohobooks"
];
info emailInvoice.get("message");
}