Preview:
void Invoice.Get_Invoice_from_Books(int recid)
{
	try 
	{
		getdata = Get_Invoice_ID_from_Books[ID == input.recid];
		// 	info getdata;
		zbinvoiceid = getdata.Invoice_Books_ID;
		//info zbinvoiceid;
		books_access = thisapp.Books.Get_Books_Access();
		conn_tok = books_access.get("connection");
		org_id = books_access.get("organisation_id");
		invoice_resp = zoho.books.getRecordsByID("invoices",org_id,zbinvoiceid,conn_tok);
		// 		info "invresp" + invoice_resp;
		invoice_data = invoice_resp.tomap().get("invoice");
		saleno = "";
		vehicleno = "";
		trans = "";
		for each  rec1 in invoice_data.get("custom_fields")
		{
			if(rec1.get("label") == "Sales Order No")
			{
				saleno = rec1.get("value_formatted");
			}
			else if(rec1.get("label") == "Vehicle No")
			{
				vehicleno = rec1.get("value_formatted");
			}
			else if(rec1.get("label") == "Transporter")
			{
				trans = rec1.get("value_formatted");
			}
		}
		// 	info "invdata" + invoice_data;
		custname = invoice_data.get("customer_name");
		invid = invoice_data.get("invoice_id");
		custid = invoice_data.get("customer_id");
		customer_id = Customers[Customer_Books_ID == custid].ID;
		//info customer_id;
		invdate = invoice_data.get("date").todate();
		//info invdate;
		invduedate = invoice_data.get("due_date").todate();
		//info invduedate;
		refno = ifnull(invoice_data.get("reference_number"),"");
		// 	info refno;
		custgst = ifnull(invoice_data.get("gst_no"),"");
		//info custgst;
		invno = invoice_data.get("invoice_number");
		Payment_Terms = invoice_data.get("payment_terms_label");
		payment_terms_id = Payment_Terms[Stages == Payment_Terms];
		if(payment_terms_id.count(ID) == 0)
		{
			pmtterms_map = Map();
			pmtterms_map.put("Stages",Payment_Terms);
			dummy_map33 = Map();
			zoho.creator.createRecord("carrierwheels","erp","Payment_Terms",pmtterms_map,dummy_map33,"vj_creator");
		}
		currency_code = invoice_data.get("currency_code");
		currency_code_id = Currency_Code[Currency_Code == currency_code].ID;
		//info currency_code;
		Place_of_Supply = invoice_data.get("place_of_supply");
		place_of_supply_id = Destination[Short_Name == Place_of_Supply].ID;
		//info Place_of_Supply;
		Source_of_Supply_id = Destination[Short_Name == "UP"].ID;
		//info Source_of_Supply_id;
		billingaddress = invoice_data.tomap().get("billing_address");
		address_line_1 = billingaddress.get("address");
		address_line_2 = billingaddress.get("street2");
		district_city = billingaddress.get("city");
		state_province = billingaddress.get("state");
		postal_code = billingaddress.get("zip");
		country = billingaddress.get("country");
		fax_number = billingaddress.get("fax");
		//info billingaddress;
		shippingaddress = invoice_data.toMap().get("shipping_address");
		address_line_11 = shippingaddress.get("address");
		address_line_12 = shippingaddress.get("street2");
		district_city1 = shippingaddress.get("city");
		state_province1 = shippingaddress.get("state");
		postal_code1 = shippingaddress.get("zip");
		country1 = shippingaddress.get("country");
		itemtotal = invoice_data.get("sub_total");
		taxtotal = invoice_data.get("tax_total");
		grandtotal = invoice_data.get("total");
		gettcs = invoice_data.get("is_tcs_amount_in_percent");
		if(gettcs == "true")
		{
			applytcs = gettcs;
			tcs = invoice_data.get("tcs_tax_id");
			tcs_id = Bill_TCS[Zoho_Books_ID == tcs].ID;
			tcsvalue = invoice_data.get("tcs_amount");
		}
		//get the sales order details
		getsomain = Sales_Order[Sale_Order_No == saleno];
		info getsomain + "somain";
		if(getsomain.count() > 0)
		{
			getsosub = Sale_Order_Subform[Sales_Order_Exis_ID == getsomain.ID];
			cwstat = "Closed";
			sostat = "Closed";
			for each  sosubfrm in Sale_Order_Subform[Sales_Order_Exis_ID == getsomain.ID]
			{
				if(sosubfrm.Balance_Qty > 0)
				{
					cwstat = "Closed Partially";
					sostat = "Open";
				}
			}
			getsomain.CWPL_SO_Status=cwstat;
			getsomain.SO_Status=sostat;
			if(getsomain.Sales_Type == "Contract" && getsomain.CWPL_SO_Status == "Shipped Completed")
			{
				getsomain.CWPL_SO_Status="Closed";
			}
			buyerordno = getsomain.PO_Reference;
			buyrerorddt = getsomain.PO_Date;
			dispatchnoteno = "";
			if(getsomain.Dispatch_Note_No != null)
			{
				//dispatchnoteno = ifnull(getsomain.Dispatch_Note_No.CDNF_ID,"");
			}
			get_cfs = invoice_resp.get("invoice").toMap().get("custom_field_hash");
			dispatchnoteno = get_cfs.get("cf_disp_note_no");
			// 	info getsomain;
			// 	info getsosub;
			dnid = null;
			getdnmain = Create_Dispatch_Note_Format[Sales_Order_No == getsomain.ID];
			info getdnmain.count();
			getdnmain.Draft_Invoice_No=invoice_data.get("invoice_number");
			if(getdnmain.count() > 0)
			{
				dnid = getdnmain.ID;
				VehicleNo = getdnmain.Vehicle_No;
				DriverContactName = getdnmain.Driver_Name;
				DriverContactNo = getdnmain.Driver_Mobile_Number;
				NameofTransport1 = getdnmain.Transport_Name;
				dispatchnoteno = getdnmain.ID;
				dispatchnotedt = getdnmain.Dispatch_Note_Date;
				grno = getdnmain.GR_No;
			}
		}
		// 	sendmail
		// 	[
		// 		from :"erp@carrierwheels.com"
		// 		to :"parthasarathy.m@synprosoft.com"
		// 		subject :"getinvoicefrombooks function " + getdnmain.count() + "--" + getsomain.ID + " recid " + recid
		// 		message :"getinvoicefrombooks function " + getdnmain.count() + "--" + getsomain.ID + " recid " + recid
		// 	]
		str = thisapp.Common.Number_Function_New("Invoice");
		//info str;
		invoiceins = insert into Invoice
		[
			Customer_Name=customer_id
			Invoice_No=invno
			Invoice_Date=invdate
			Invoice_Due_Date=invduedate
			Reference_No=refno
			Invoice_Status="open"
			GST_No=custgst
			Payment_Terms=payment_terms_id.ID
			Currency_Code=currency_code_id
			Place_of_Supply=place_of_supply_id
			Source_of_Supply=Source_of_Supply_id
			Billing_Address.address_line_1=address_line_1
			Billing_Address.address_line_2=address_line_2
			Billing_Address.district_city=district_city
			Billing_Address.state_province=state_province
			Billing_Address.postal_Code=postal_code
			Billing_Address.country=country
			Shipping_Address.address_line_1=address_line_11
			Shipping_Address.address_line_2=address_line_12
			Shipping_Address.district_city=district_city1
			Shipping_Address.state_province=state_province1
			Shipping_Address.postal_Code=postal_code1
			Shipping_Address.country=country1
			Vehicle_no1=vehicleno
			Driver_Contact_Name=DriverContactName
			Driver_Contact_No=DriverContactNo
			Name_of_Transport=trans
			Sale_Order_No=getsomain.ID
			Sale_Order_Date=getsomain.Sale_Order_Date
			Approval_Status="Approved"
			Item_Total=itemtotal
			Tax_Total=taxtotal
			Grand_Total=grandtotal
			Zoho_Books_ID=zbinvoiceid
			Invoice_Type="Domestic Invoice"
			Apply_TCS=applytcs
			TCS=tcs_id
			TCS_Value=tcsvalue
			Create_Dispatch_Note_RECID=dnid
			Buyer_Order_No=buyerordno
			Buyer_Order_Date=buyrerorddt
			Added_User=zoho.loginuser
			Dispatch_Note_No=dispatchnoteno
			Dispatch_Note_Date=dispatchnotedt
			GR_No=grno
			Sales_Type=getsomain.Sales_Type
		];
		if(getsomain.count() > 0)
		{
			solist = List();
			solist.addAll(getsomain.Invoice_No);
			solist.add(invoiceins);
			getsomain.Invoice_No=solist;
			info invoiceins;
			info getsomain.Sale_Order_No;
			info getsomain.Sale_Order_Date;
		}
		sno = 0;
		lineitems = invoice_data.get("line_items");
		taxperc = 0;
		for each  var in lineitems
		{
			taxvalue = 0;
			taxperc = var.getjson("tax_percentage");
			taxvalue = var.getjson("item_total") * taxperc / 100;
			Totalamt = var.getjson("item_total") + taxvalue;
			sno = sno + 1;
			partno = Materials[Zoho_Books_ID == var.getjson("item_id")].ID;
			custpartcode = Sale_Order_Subform[Sales_Order_Exis_ID == getsomain.ID && Part_No == partno].Customer_Part_Code;
			invoicesub = insert into Invoice_Subform
			[
				S_No=sno
				Part_No=Materials[Zoho_Books_ID == var.getjson("item_id")].ID
				Part_Description=Materials[Zoho_Books_ID == var.getJSON("item_id")].ID
				Dispatch_Plan_Date_time=getdnmain.Plan_Date_Time
				Dispatch_Actual_Date_Time=getdnmain.Actual_Date_Time
				Vehicle_Gate_in_Time=getdnmain.In_Time
				Vehicle_Gate_out_time=getdnmain.Out_Time
				HSN_Code=var.getjson("hsn_or_sac")
				UoM=Unit_of_Measurement[UOM == var.getjson("unit")].ID
				Qty=var.getjson("quantity")
				Rate=var.getjson("rate")
				Sub_Total=var.getjson("item_total")
				Tax=GST_Details[Tax_Name == var.getjson("tax_name")].ID
				Tax_Value=taxvalue
				Total=Totalamt
				Invoice_Exist_ID=invoiceins
				Sales_Order_RECID=getsomain.ID
				Added_User=zoho.loginuser
				Customer_Part_Code=custpartcode
			];
		}
		//Update the total Invoice Qty
		totalinvqty = 0;
		totalinvqty = Invoice_Subform[Invoice_Exist_ID == invoiceins].sum(Qty);
		if(totalinvqty > 0)
		{
			getmaininv = Invoice[ID == invoiceins];
			if(getmaininv.count() > 0)
			{
				getmaininv.Total_Invoice_Qty=ifnull(totalinvqty,"");
			}
		}
		//Insert into Log Form
		ins_log = insert into Log_Files
		[
			Added_User=zoho.loginuser
			Module_Name="Invoice"
			Form_Name="GetInvoicefromBooks"
			Log_Details=invoice_resp
			Log_Type="success"
			Reference_NO=getsomain.Sale_Order_No
		];
		//update the E-Way Bill No.
		invoice_resp = zoho.books.getRecordsByID("invoices",org_id,zbinvoiceid,conn_tok);
		//info "invoice resp code " + invoice_resp.get("code") + "---" + zbid;
		ewaybill = invoice_resp.get("invoice").toMap().get("eway_bill_details");
		//info ewaybill;
		sendmail
		[
			from :"erp@carrierwheels.com"
			to :"parthasarathy.m@synprosoft.com"
			subject :"Function Invoice.Getinvoicefrombooks ewaybill details " + zbinvoiceid
			message :"ewaybill " + ewaybill
		]
		if(!isempty(ewaybill))
		{
			bno = ewaybill.get("eway_bill_number");
			if(!isnull(bno))
			{
				getinv = Invoice[Zoho_Books_ID == zbinvoiceid];
				if(getinv.count() > 0)
				{
					getinv.E_Way_Bill_No=ewaybill.get("eway_bill_number");
				}
			}
			if(!isnull(ewaybill.get("eway_bill_date")))
			{
				getinv = Invoice[Zoho_Books_ID == zbinvoiceid];
				if(getinv.count() > 0)
				{
					getinv.E_Way_Bill_Date=ewaybill.get("eway_bill_date").toString("dd-MMM-yyyy");
				}
			}
		}
	}
	catch (e)
	{
		sendmail
		[
			from :"erp@carrierwheels.com"
			to :"parthasarathy.m@synprosoft.com"
			subject :"Invoice.Get Invoice from books try & catch" + zbinvoiceid
			message :e
		]
	}
	//update the Invoice number in Dispatch Note format
	// 	if(getdnmain.count() > 0)
	// 	{
	// 		getdnmain.Invoice_Bill_No=str;
	// 	}
	//update the inventory , Check Inventory Forms
	//lineitemsin = invoice_data.get("line_items");
	// 	for each  rec in lineitemsin
	// 	{
	// 		Part_No_id = Materials[Zoho_Books_ID == rec.getjson("item_id")].ID;
	// 		Part_Description_id = Materials[Zoho_Books_ID == rec.getJSON("item_id")].ID;
	// 		qty = rec.getjson("quantity");
	// 		fetinvent = Inventory[Part_No == Part_No_id];
	// 		//Subtracting available Stock
	// 		if(fetinvent.count() > 0)
	// 		{
	// 			if(fetinvent.Total_Qty < fetinvent.Available_Qty)
	// 			{
	// 				fetinvent.Total_Qty=fetinvent.Available_Qty;
	// 			}
	// 			//blck_quantity = ifnull(fetinvent.Available_Qty,0.0) - ifnull(qty,0.0);
	// 			tot_inv_qty = ifnull(fetinvent.Total_Qty,0.0) - ifnull(qty,0.0);
	// 			//fetinvent.Block_Qty=fetinvent.Block_Qty - ifnull(qty,0.0);
	// 			fetinvent.Total_Qty=tot_inv_qty;
	// 			sendmail
	// 			[
	// 				from :zoho.loginuserid
	// 				to :"parthasarathy.m@certifytechnologies.com"
	// 				subject :"Invoice.Get_Invoice_from_Books"
	// 				message :"Part no " + fetinvent.Part_No.Part_No + "Total Qty " + fetinvent.Total_Qty + "Availabel Qty " + fetinvent.Available_Qty
	// 			]
	// 		}
	//Subtract the quantity in check Inventory form
	// 		fetchkinvent = Check_Inventory[Sales_Order_No == getsomain.ID && Part_No == Part_No_id && Customer_Name == customer_id];
	// 		if(fetchkinvent.count() > 0)
	// 		{
	// 			if(fetchkinvent.Quantity_to_be_Blocked > 0)
	// 			{
	// 				fetchkinvent.Quantity_to_be_Blocked=fetchkinvent.Quantity_to_be_Blocked - ifnull(qty,0.0);
	// 				if(fetchkinvent.Quantity_to_be_Blocked == 0)
	// 				{
	// 					fetchkinvent.Status="Closed";
	// 				}
	// 			}
	// 		}
	//getsosub.Invoiced_so_far_Qty=ifnull(getsosub.Invoiced_so_far_Qty,0.00) + ifnull(qty,0.0);
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter