Skip to content

Instantly share code, notes, and snippets.

@zirinisp
Created November 4, 2017 18:24
Show Gist options
  • Save zirinisp/81c3d25696730d4df5b6f526bdba8637 to your computer and use it in GitHub Desktop.
Save zirinisp/81c3d25696730d4df5b6f526bdba8637 to your computer and use it in GitHub Desktop.
Zoho Creator Script for End of Day Report on Zoho Books
// Slack Urls
debugSlackUrl = "https://hooks.slack.com/services/...";
eodReportSlack = "https://hooks.slack.com/services/...";
// Zoho Books
zohoBooksOrganisationId = "...";
cashCustomerId = ...;
creditCardCustomerId = ...;
otherCustomerId = ...;
zohoBooksPettyCashAccountId = ...;
// Zoho Invoice
zohoInvoiceOrganisationId = "...";
// Create Cash Sales Invoice
// Create an Item
cashItem = Map();
cashItem.put("name","Day Sales");
cashItem.put("rate",input.Cash_Sales);
cashItem.put("quantity",1);
// Create a list and add it there
cashItemsList = list();
cashItemsList.add(cashItem);
// Create the invoice
cashInvoiceValues = Map();
cashInvoiceValues.put("customer_id",cashCustomerId);
cashInvoiceValues.put("date",input.Date_field);
cashInvoiceValues.put("line_items",cashItemsList);
cashInvoiceValues.put("status","sent");
cashInvoiceValues.put("due_date",input.Date_field);
// Submit the invoice to books
cashResponse = zoho.invoice.create("Invoices", zohoBooksOrganisationId, cashInvoiceValues);
info cashResponse;
thisapp.debugSlack(debugSlackUrl,input.Date_field + " cashResponse",cashResponse);
if(cashResponse.containKey("invoice"))
{
info "Invoice Response = " + cashResponse.get("message");
invoiceMap = cashResponse.get("invoice").toMap();
invoiceID = invoiceMap.get("invoice_id");
invoiceTotal = invoiceMap.get("total");
appliedInvoice = Map();
appliedInvoice.put("invoice_id",invoiceID);
appliedInvoice.put("amount_applied",invoiceTotal);
appliedInvoices = list();
appliedInvoices.add(appliedInvoice);
paymentJSON = Map();
paymentJSON.put("customer_id",cashCustomerId);
paymentJSON.put("date",input.Date_field);
paymentJSON.put("amount",invoiceTotal);
paymentJSON.put("account_name","Petty Cash");
paymentJSON.put("account_id",zohoBooksPettyCashAccountId);
paymentJSON.put("invoices",appliedInvoices);
paymentResponse = zoho.books.createRecord("Customerpayments", zohoBooksOrganisationId, paymentJSON);
info "Payment Response = " + paymentResponse.get("message");
info paymentResponse;
thisapp.debugSlack(debugSlackUrl,input.Date_field + " paymentResponse",paymentResponse);
}
else
{
info "Error occured while creating invoice = " + cashResponse.get("message");
}
// Create Credit Card Sales Invoice
// Create an Item
creditCardItem = Map();
creditCardItem.put("name","Day Sales");
creditCardItem.put("rate",input.Credit_Card_Sales);
creditCardItem.put("quantity",1);
// Create a list and add it there
creditCardItemsList = list();
creditCardItemsList.add(creditCardItem);
// Create the invoice
creditCardInvoiceValues = Map();
creditCardInvoiceValues.put("customer_id",creditCardCustomerId);
creditCardInvoiceValues.put("date",input.Date_field);
creditCardInvoiceValues.put("line_items",creditCardItemsList);
creditCardInvoiceValues.put("status","sent");
creditCardInvoiceValues.put("due_date",input.Date_field);
// Submit the invoice to books
creditCardResponse = zoho.invoice.create("Invoices",zohoBooksOrganisationId,creditCardInvoiceValues);
info creditCardResponse;
thisapp.debugSlack(debugSlackUrl,input.Date_field + " creditCardResponse",creditCardResponse);
// Create an invoice for others
otherItem = Map();
otherItem.put("name","Day Sales");
otherItem.put("rate",input.Other);
otherItem.put("quantity",1);
// Create a list and add it there
otherItemsList = list();
otherItemsList.add(otherItem);
// Create the invoice
otherInvoiceValues = Map();
otherInvoiceValues.put("customer_id", otherCustomerId);
otherInvoiceValues.put("date",input.Date_field);
otherInvoiceValues.put("line_items",creditCardItemsList);
otherInvoiceValues.put("status","sent");
otherInvoiceValues.put("due_date",input.Date_field);
// Submit the invoice to books
otherResponse = zoho.invoice.create("Invoices", zohoInvoiceOrganisationId, otherInvoiceValues);
info otherResponse;
thisapp.debugSlack(debugSlackUrl,input.Date_field + " otherResponse",otherResponse);
// Post to slack
eodMessage = Map();
eodMessage.put("username",input.Date_field + " Sales: " + input.Total);
fieldsList = list();
item1 = Map();
item1.put("title","Cash");
item1.put("value",input.Cash_Sales);
item1.put("short",true);
fieldsList.add(item1);
item2 = Map();
item2.put("title","Credit Cards");
item2.put("value",input.Credit_Card_Sales);
item2.put("short",true);
fieldsList.add(item2);
item3 = Map();
item3.put("title","Other");
item3.put("value",input.Other);
item3.put("short",true);
fieldsList.add(item3);
item4 = Map();
item4.put("title","Deliveries");
item4.put("value",input.Delivery_Sales);
item4.put("short",true);
fieldsList.add(item4);
slackAttachments = Map();
slackAttachments.put("fields",fieldsList);
slackAttachmentsList = list();
slackAttachmentsList.add(slackAttachments);
eodMessage.put("attachments",slackAttachmentsList);
eodMessage.put("text","End of Day Report");
payload = Map();
payload.put("payload",eodMessage);
resp = postUrl(eodReportSlack,payload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment