Create SalesOrder in Books

PHOTO EMBED

Tue Jan 07 2025 14:56:59 GMT+0000 (Coordinated Universal Time)

Saved by @Pooja

void Books.Create_Sales_Order_in_Books(int soid)
{
	//Getting authtoken and organisation id
	books_access = thisapp.Books.Get_Books_Access();
	conn_tok = books_access.get("connection");
	org_id = books_access.get("organisation_id");
	//info auth_tok;
	//info org_id;
	//------------------------------------------------------------------------------
	fetch_so = Sales_Order[ID == input.soid];
	//info fetch_so;
	fetch_cust = Customers[ID == fetch_so.Customer_Name];
	//info ("fetchcust" + fetch_cust) + "  " + fetch_cust.Customer_Books_ID;
	fetch_delivery_method = Shipping_Mode[ID == fetch_so.Delivery_Mode].Shipping_Mode;
	fetch_warehouse = Warehouse[ID = fetch_so.Warehouse].Warehouse_Name;
	fettax = GST_Details[ID == fetch_so.Item_Details.Tax];
	fetemp = Employee_Details[ID == fetch_so.Created_By].Employee_Name;
	fetch_payment_trms = Payment_Terms[ID == fetch_so.Payment_Terms].Stages;
	shipment_date = fetch_so.Expected_Shipment_Date.toString("yyyy-MM-dd");
	so_date = zoho.currentdate.toString("yyyy-MM-dd");
	curr_code = Currency_Code[ID == fetch_cust.Currency_Code];
	curr_conv = Currency_Conversion[Convert_To == fetch_cust.Currency_Code];
	plc_of_supply = Destination[Destination == fetch_so.Place_of_Supply.Destination].Short_Name;
	fet_gst_trmt = GST_Treatment[ID == fetch_cust.GST_Treatment];
	mymap = Map();
	mymap.put("customer_id",fetch_cust.Customer_Books_ID);
	mymap.put("customer_name",fetch_cust.Contact_Name);
	mymap.put("date",so_date);
	//mymap.put("billing_address",fetch_so.Billing_Address);
	//mymap.put("shipping_address",fetch_so.Shipping_Address);
	mymap.put("shipment_date",shipment_date);
	mymap.put("delivery_method",fetch_delivery_method);
	mymap.put("place_of_supply",plc_of_supply);
	mymap.put("gst_treatment",fet_gst_trmt.Link_name);
	mymap.put("salesorder_number",fetch_so.Sale_Order_No);
	mymap.put("salesperson_name",fetemp);
	mymap.put("currency_id",curr_code.Zoho_Books_ID);
	mymap.put("exchange_rate",curr_conv.Exchange_Rate);
	mymap.put("status","Open");
	url_to_so = "https://creatorapp.zoho.in/carrierwheels/erp/#Page:SO_Print?ID=" + soid;
	custom_list = List();
	custom_map = Map();
	custom_map.put("api_name","cf_download_so_from_erp");
	custom_map.put("value",url_to_so);
	custom_list.add(custom_map);
	mymap.put("custom_fields",custom_list);
	item_list = List();
	tx_val = 0;
	totalqty = 0;
	for each  rec in fetch_so.Item_Details
	{
		fetchitem = Materials[ID == rec.Part_Description];
		line_item_map = Map();
		line_item_map.put("name",rec.Part_Description.Part_Description);
		line_item_map.put("item_id",fetchitem.Zoho_Books_ID);
		//	line_item_map.put("description",rec.Part_Description.Additional_Description);
		line_item_map.put("quantity",rec.Qty);
		totalqty = ifnull(totalqty,0.00) + ifnull(rec.Qty,0.00);
		line_item_map.put("rate",rec.Rate);
		line_item_map.put("warehouse_name",fetch_warehouse);
		//line_item_map.put("tax_type",fettax.Tax_Details.Tax_Type);
		line_item_map.put("tax_name",fettax.Tax_Name);
		line_item_map.put("tax_id",fettax.Zoho_Books_ID);
		line_item_map.put("tax_percentage",fettax.Total_Rate);
		item_list.add(line_item_map);
		tx_val = ifnull(tx_val,0.0) + ifnull(rec.Tax_Value,0.0);
	}
	if(fetch_so.Packing_Forwarding > 0)
	{
		fetchitem = Materials[Part_No == "900114"];
		line_item_map = Map();
		line_item_map.put("name",fetchitem.Part_Description);
		line_item_map.put("item_id",fetchitem.Zoho_Books_ID);
		//line_item_map.put("quantity",1);
		line_item_map.put("rate",fetch_so.Packing_Forwarding);
		line_item_map.put("warehouse_name",fetch_warehouse);
		line_item_map.put("tax_name",fettax.Tax_Name);
		line_item_map.put("tax_id",fettax.Zoho_Books_ID);
		line_item_map.put("tax_percentage",fettax.Total_Rate);
		item_list.add(line_item_map);
		tx_val = ifnull(tx_val,0.0) + ifnull(fetch_so.PF_Tax_value,0.0);
	}
	mymap.put("tax_total",tx_val);
	mymap.put("line_items",item_list);
	post_url = zoho.books.createRecord("salesorders",org_id,mymap,conn_tok);
	info post_url;
	res_code = post_url.get("code").toLong();
	resp_Map = Map();
	log_type = "Failure";
	if(res_code == 0)
	{
		log_type = "Success";
		books_id = post_url.toMap().get("salesorder").toMap().get("salesorder_id");
		fetch_so.Zoho_Books_ID=books_id;
		test_map = Map();
		mark_response = invokeurl
		[
			url :"https://www.zohoapis.in/books/v3/salesorders/" + books_id + "/status/open?organization_id=" + org_id + ""
			type :POST
			parameters:test_map
			connection:"books_con"
		];
	}
	if(res_code == 0)
	{
		getzbook = zoho.books.getRecordsByID("salesorders",org_id,books_id);
		get_line_item = getzbook.toMap().get("salesorder").toMap().get("line_items");
		for each  lineitem in get_line_item
		{
			lineitemid = lineitem.getjson("line_item_id");
			itemid = lineitem.getjson("item_id");
			fetso = Sales_Order[Zoho_Books_ID == books_id];
			fetsosf = Sale_Order_Subform[Sales_Order_Exis_ID == fetso.ID];
			getmat = Materials[Zoho_Books_ID == itemid];
			for each  sosfrec in fetsosf
			{
				if(sosfrec.Part_No == getmat.ID)
				{
					sosfrec.zb_line_item_id=lineitemid;
					fetso.pack_forw_lineitemid=lineitemid;
				}
			}
		}
	}
	ins_log = insert into Log_Files
	[
		Added_User=zoho.loginuser
		Log_Type=log_type
		Module_Name="Books"
		Form_Name="Sales Order"
		Log_Details=post_url
		Reference_NO=fetch_so.Sale_Order_No
	];
	//sending error log message
	if(log_type == "Failure")
	{
		resp_Map.put("Resp","Failure");
		resp_Map.put("log_msg",post_url.get("message"));
	}
	//status to changed 
	updtstatus = zoho.books.markStatus("salesorders",org_id,books_id,"open",conn_tok);
}
content_copyCOPY