string button.SendInvoieToBooks(String ID)
{
info ID;
// Получаем инвойс из CRM
Invoice = invokeurl
[
url :"https://www.zohoapis.com/crm/v8/Invoices/" + ID
type :GET
connection:"crm"
];
Invoice = Invoice.get("data").get(0);
AccountName = Invoice.get("Account_Name").get("name");
// Ищем контакт в Zoho Books
organization_id = "ВАШ_ORG_ID";
response = invokeurl
[
url :"https://www.zohoapis.com/books/v3/contacts?organization_id=" + organization_id + "&contact_name=" + AccountName
type :GET
connection:"books"
];
response = response.get("contacts");
for each rec in response
{
if(rec.get("status").contains("inactive") == false)
{
contactID = rec.get("contact_id");
}
}
// Подготовка данных
invoice_number = Invoice.get("Invoice_Number");
date = Invoice.get("Invoice_Date");
due_date = Invoice.get("Due_Date");
Product_Details = Invoice.get("Invoiced_Items");
Currency = Invoice.get("Currency");
if(Currency == "USD") currency_id = "ID_ДЛЯ_USD";
if(Currency == "RUB") currency_id = "ID_ДЛЯ_RUB";
if(Currency == "EUR") currency_id = "ID_ДЛЯ_EUR";
if(Currency == "AED") currency_id = "ID_ДЛЯ_AED";
listVar = List();
for each rec in Product_Details
{
ProductName = rec.get("Product_Name").get("name");
response = invokeurl
[
url :"https://www.zohoapis.com/books/v3/items?organization_id=" + organization_id + "&name_startswith=" + ProductName
type :GET
connection:"books"
];
ProductID = response.get("item_id");
mapVar = Map();
mapVar.put("item_id",ProductID);
mapVar.put("name",ProductName);
mapVar.put("rate",rec.get("List_Price"));
mapVar.put("quantity",rec.get("Quantity"));
listVar.add(mapVar);
}
// Создаём инвойс в Zoho Books
values = Map();
values.put("invoice_number",invoice_number);
values.put("customer_id",contactID);
values.put("currency_id",currency_id);
values.put("date",date);
values.put("due_date",due_date);
values.put("line_items",listVar);
response = zoho.books.createRecord("Invoices",organization_id,values,"books");
return response.get("message");
}