var_org = organization.get("organization_id");
aaa = vendor_payment.get("payment_id");
amount = vendor_payment.get("amount");
paymentnumber = vendor_payment.get("payment_number");
dateformatted = vendor_payment.getJSON("date_formatted");
branch = vendor_payment.getJSON("branch_name");
refno = vendor_payment.getJSON("reference_number");
//billID = vendor_payment.get("bills").get(0).get("bill_id");
tbill = vendor_payment.getJSON("bills");
resp = invokeurl
[
	url :"https://www.zohoapis.in/books/v3/vendorpayments/" + aaa + "?organization_id=" + var_org
	type :GET
	connection:"books"
];
// info resp;
item_list = List();
for each  rec in tbill
{
	billID = rec.get("bill_id");
	billnum = rec.get("bill_number");
	amtapp = rec.get("amount_applied");
	// Get amount from the bill
	paymentid = rec.get("bill_payment_id");
	info "PAYMENTID: " + paymentid;
	info "AmtApp: " + amtapp;
	// Store amount in item_map for tracking
	item_map = Map();
	item_map.put("Bill_ID",billID);
	item_map.put("Payment_Amount",amtapp);
	item_map.put("Payment_Utr",zoho.currentdate);
	item_map.put("Payment_Number",paymentnumber);
	item_map.put("Branch",branch);
	item_map.put("Reference_Number",refno);
	item_list.add(item_map);
	response = invokeurl
	[
		url :"https://www.zohoapis.in/creator/v2.1/data/centralisedprocurement_usdcglobal/usdc1/report/All_Bills?Zoho_Books_Id=" + billID
		type :GET
		connection:"creator"
	];
	payres = invokeurl
	[
		url :"https://www.zohoapis.in/creator/v2.1/data/centralisedprocurement_usdcglobal/usdc1/report/All_Payments?Bill_Payment_ID=" + paymentid
		type :GET
		connection:"creator"
	];
	payvar = payres.get("data");
	varre = response.get("data");
	booksbilldata = zoho.books.getRecordsByID("bills","60036667486",billID,"books");
	crtrid = booksbilldata.getJSON("bill").getJSON("custom_fields");
	//info "BooksData"+booksbilldata;
	amount = booksbilldata.getJSON("payments");
	//info "Amount"+billID;
	for each  cfid in crtrid
	{
		if(cfid.getJSON("field_id") == "2293182000002730100")
		{
			crtrinvid = cfid.getJSON("value_formatted");
			info "INVID" + crtrinvid;
		}
	}
	// If payment record does not exist, create a new one	
	if(payvar.size() == 0)
	{
		creator_id1 = varre.getJSON("ID");
		paymap = Map();
		paymap.put("Payment_Amount",amtapp);
		paymap.put("Bill_ID",billID);
		paymap.put("Bill_No",billnum);
		paymap.put("Payment_Utr",zoho.currentdate);
		paymap.put("Payment_Number",paymentnumber);
		paymap.put("Branch",branch);
		paymap.put("Bill_Payment_ID",paymentid);
		paymap.put("Reference_Number",refno);
		paymap.put("Zoho_book_id",aaa);
		paymap.put("Bills",creator_id1);
		paymap.put("Zoho_book_id",aaa);
		paymap.put("CP_Internal_Invoice_ID",crtrinvid);
		info "Creating Payment Record: " + paymap;
		otherParams = Map();
		createPaymentres = zoho.creator.createRecord("centralisedprocurement_usdcglobal","usdc1","Payment_form",paymap,otherParams,"creator");
	}
	else
	{
		upmap = Map();
		upothermap = Map();
		getpaymentResponse1 = zoho.creator.getRecords("centralisedprocurement_usdcglobal","usdc1","All_Payments","Bill_Payment_ID ==\"" + paymentid + "\"",1,200,"creator");
		newresponse1 = getpaymentResponse1.getJson("data");
		upmap.put("Payment_Amount",amtapp);
		upmap.put("Bill_ID",billID);
		upmap.put("CP_Internal_Invoice_ID",crtrinvid);
		updatePayment1 = zoho.creator.updateRecord("centralisedprocurement_usdcglobal","usdc1","All_Payments",newresponse1.getJson("ID"),upmap,upothermap,"creator");
		info "Updated Payment Record: " + updatePayment1;
	}
	resp1 = invokeurl
	[
		url :"https://www.zohoapis.in/creator/v2.1/data/centralisedprocurement_usdcglobal/usdc1/report/Associated_Bill?ID=" + crtrinvid
		type :GET
		connection:"creator"
	];
	if(resp1.get("code") == 3000)
	{
		info "RESP1" + resp1;
		pdamt = resp1.getJSON("data").getJSON("Paid_Amount").toNumber();
		pdamt = pdamt + amtapp.toDecimal();
		info "PaidAMOUNT" + pdamt;
		mps = Map();
		Other = Map();
		//mps.put("Paid_Amount",amtapp.toDecimal().round(2));
		// 	info mps;
		upcreatorrec = zoho.creator.updateRecord("centralisedprocurement_usdcglobal","usdc1","Admin_Associated_Bill",crtrinvid,mps,Other,"creator");
		//info "CRT" + upcreatorrec;
	}
}
sendmail
[
	from :zoho.adminuserid
	to :"pooja.s@techvaria.com"
	subject :"Payment Test"
	message :upcreatorrec + "-" + mps
]