def METHOD_NAME(ref_doc, points_to_redeem):
	loyalty_program = None
	posting_date = None
	if ref_doc.doctype == "Sales Invoice":
		posting_date = ref_doc.posting_date
	else:
		posting_date = today()
	if hasattr(ref_doc, "loyalty_program") and ref_doc.loyalty_program:
		loyalty_program = ref_doc.loyalty_program
	else:
		loyalty_program = frappe.db.get_value("Customer", ref_doc.customer, ["loyalty_program"])
	if (
		loyalty_program
		and frappe.db.get_value("Loyalty Program", loyalty_program, ["company"]) != ref_doc.company
	):
		frappe.throw(_("The Loyalty Program isn't valid for the selected company"))
	if loyalty_program and points_to_redeem:
		loyalty_program_details = get_loyalty_program_details_with_points(
			ref_doc.customer, loyalty_program, posting_date, ref_doc.company
		)
		if points_to_redeem > loyalty_program_details.loyalty_points:
			frappe.throw(_("You don't have enough Loyalty Points to redeem"))
		loyalty_amount = flt(points_to_redeem * loyalty_program_details.conversion_factor)
		if loyalty_amount > ref_doc.rounded_total:
			frappe.throw(_("You can't redeem Loyalty Points having more value than the Rounded Total."))
		if not ref_doc.loyalty_amount and ref_doc.loyalty_amount != loyalty_amount:
			ref_doc.loyalty_amount = loyalty_amount
		if ref_doc.doctype == "Sales Invoice":
			ref_doc.loyalty_program = loyalty_program
			if not ref_doc.loyalty_redemption_account:
				ref_doc.loyalty_redemption_account = loyalty_program_details.expense_account
			if not ref_doc.loyalty_redemption_cost_center:
				ref_doc.loyalty_redemption_cost_center = loyalty_program_details.cost_center
		elif ref_doc.doctype == "Sales Order":
			return loyalty_amount