Skip to content

Instantly share code, notes, and snippets.

@jeznag
Created July 13, 2018 01:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeznag/a47fefa0d491a8a73956a345d1a4c51e to your computer and use it in GitHub Desktop.
Save jeznag/a47fefa0d491a8a73956a345d1a4c51e to your computer and use it in GitHub Desktop.
Deluge code for creating purchase order based on sales order
sales_order_record = zoho.crm.getRecordById("Sales_Orders",sales_order_id.toLong());
products = sales_order_record.get("Product_Details");
account_id = sales_order_record.get("Account_Name").getJSON("id");
account_record = zoho.crm.getRecordById("Accounts", account_id.toLong());
products_list = products.toJSONList();
products_for_purchase_order = List:Map();
grand_total = 0.0;
total_without_tax = 0.0;
total_tax = 0.0;
for each product_json in products_list
{
product_id = product_json.get("product").getJSON("id");
product_record = zoho.crm.getRecordById("Products",product_id.toLong());
pack_details = product_record.get("Pack_Details");
if (pack_details != null) {
pack_details_map = pack_details.toMap();
for each item_key in pack_details_map.keys()
{
item_data = pack_details_map.get(item_key).toMap();
product_for_purchase_order = zoho.crm.getRecordById("Products",item_data.get("id").toLong());
total_for_line_item = item_data.get("quantity").toDecimal() * product_for_purchase_order.get("Unit_Price").toDecimal();
tax_type = product_for_purchase_order.get("Tax");
net_total = total_for_line_item;
line_item_map = Map:String(
{
"product": product_for_purchase_order.get("id"),
"quantity": item_data.get("quantity"),
"unit_price":product_for_purchase_order.get("Unit_Price"),
"list_price":product_for_purchase_order.get("Unit_Price"),
"total":total_for_line_item,
"total_after_discount":total_for_line_item,
"net_total":net_total
});
tax_for_line_item = 0;
if(tax_type.contains("Expenses"))
{
tax_for_line_item = total_for_line_item * 0.1;
line_tax = tax_type + "===10";
net_total = total_for_line_item * 1.1;
line_item_map.put("Tax",tax_for_line_item);
line_item_map.put("Line_Tax",line_tax);
line_item_map.put("Net_Total",net_total);
line_item_map.put("Line_Tax", line_tax);
}
should_add = false;
if(item_key == "tins" && account_record.get("Roof_Type") == "Tin")
{
should_add = true;
}
else if(item_key == "tiles" && account_record.get("Roof_Type").contains("Tile"))
{
should_add = true;
}
else if(item_key != "tiles" && item_key != "tins")
{
should_add = true;
}
if(should_add)
{
total_tax = total_tax + tax_for_line_item;
total_without_tax = total_without_tax + total_for_line_item;
grand_total = grand_total + net_total;
products_for_purchase_order.add(line_item_map);
}
}
}
}
new_purchase_order = Map:String({
"Subject":sales_order_record.get("Subject"),
"Account": account_id,
"PO_Number":sales_order_record.get("Contract_Number"),
"Tracking_Number":sales_order_record.get("Contract_Number"),
"Sales_Order":sales_order_record.get("id"),
"Opportunity_Information":sales_order_record.get("Deal_Name"),
"Billing_Street":sales_order_record.get("Billing_Street"),
"Billing_City":sales_order_record.get("Billing_City"),
"Billing_State":sales_order_record.get("Billing_State"),
"Billing_Code":sales_order_record.get("Billing_Code"),
"Billing_Country":sales_order_record.get("Billing_Country"),
"Shipping_City":account_record.get("Suburb"),
"Shipping_State":account_record.get("State"),
"Shipping_Code":account_record.get("Postcode").toString(),
"Product_Details":products_for_purchase_order,
"Sub_Total":total_without_tax,"Tax":total_tax,"Grand_Total":grand_total,
"Total_Amount_at_line_item_level":total_without_tax,
"Total_Tax_at_line_item_level":total_tax
});
create_resp = zoho.crm.create("Purchase_Orders",new_purchase_order);
url_for_purchase_order = "https://crm.zoho.com/crm/EntityInfo.do?module=PurchaseOrders&id=" + create_resp.get("id");
openUrl(url_for_purchase_order,"Same Window");
return url_for_purchase_order;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment