fix: HRA Exemption calculation in case of multiple salary structure assignments
diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py
index 269e4aa..c730b19 100644
--- a/erpnext/hr/utils.py
+++ b/erpnext/hr/utils.py
@@ -439,20 +439,18 @@
 	return False
 
 
-def get_salary_assignment(employee, date):
-	assignment = frappe.db.sql(
-		"""
-		select * from `tabSalary Structure Assignment`
-		where employee=%(employee)s
-		and docstatus = 1
-		and %(on_date)s >= from_date order by from_date desc limit 1""",
-		{
-			"employee": employee,
-			"on_date": date,
-		},
-		as_dict=1,
+def get_salary_assignments(employee, payroll_period):
+	start_date, end_date = frappe.db.get_value(
+		"Payroll Period", payroll_period, ["start_date", "end_date"]
 	)
-	return assignment[0] if assignment else None
+	assignments = frappe.db.get_all(
+		"Salary Structure Assignment",
+		filters={"employee": employee, "docstatus": 1, "from_date": ["between", (start_date, end_date)]},
+		fields=["*"],
+		order_by="from_date",
+	)
+
+	return assignments
 
 
 def get_sal_slip_total_benefit_given(employee, payroll_period, component=False):
diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py
index 6a7e113..6aa1f1f 100644
--- a/erpnext/regional/india/utils.py
+++ b/erpnext/regional/india/utils.py
@@ -1,14 +1,24 @@
 import json
+import math
 import re
 
 import frappe
 from frappe import _
 from frappe.model.utils import get_fetch_values
-from frappe.utils import cint, cstr, date_diff, flt, getdate, nowdate
+from frappe.utils import (
+	add_days,
+	cint,
+	cstr,
+	date_diff,
+	flt,
+	get_link_to_form,
+	getdate,
+	month_diff,
+)
 
 from erpnext.controllers.accounts_controller import get_taxes_and_charges
 from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount
-from erpnext.hr.utils import get_salary_assignment
+from erpnext.hr.utils import get_salary_assignments
 from erpnext.payroll.doctype.salary_structure.salary_structure import make_salary_slip
 from erpnext.regional.india import number_state_mapping, state_numbers, states
 
@@ -360,44 +370,55 @@
 		"Company", doc.company, ["basic_component", "hra_component"]
 	)
 	if not (basic_component and hra_component):
-		frappe.throw(_("Please mention Basic and HRA component in Company"))
-	annual_exemption, monthly_exemption, hra_amount = 0, 0, 0
+		frappe.throw(
+			_("Please set Basic and HRA component in Company {0}").format(
+				get_link_to_form("Company", doc.company)
+			)
+		)
+
+	annual_exemption = monthly_exemption = hra_amount = basic_amount = 0
+
 	if hra_component and basic_component:
-		assignment = get_salary_assignment(doc.employee, nowdate())
-		if assignment:
-			hra_component_exists = frappe.db.exists(
-				"Salary Detail",
-				{
-					"parent": assignment.salary_structure,
-					"salary_component": hra_component,
-					"parentfield": "earnings",
-					"parenttype": "Salary Structure",
-				},
-			)
+		assignments = get_salary_assignments(doc.employee, doc.payroll_period)
 
-			if hra_component_exists:
-				basic_amount, hra_amount = get_component_amt_from_salary_slip(
-					doc.employee, assignment.salary_structure, basic_component, hra_component
-				)
-				if hra_amount:
-					if doc.monthly_house_rent:
-						annual_exemption = calculate_hra_exemption(
-							assignment.salary_structure,
-							basic_amount,
-							hra_amount,
-							doc.monthly_house_rent,
-							doc.rented_in_metro_city,
-						)
-						if annual_exemption > 0:
-							monthly_exemption = annual_exemption / 12
-						else:
-							annual_exemption = 0
-
-		elif doc.docstatus == 1:
+		if not assignments and doc.docstatus == 1:
 			frappe.throw(
-				_("Salary Structure must be submitted before submission of Tax Ememption Declaration")
+				_("Salary Structure must be submitted before submission of {0}").format(doc.doctype)
 			)
 
+		assignment_dates = [assignment.from_date for assignment in assignments]
+
+		for idx, assignment in enumerate(assignments):
+			if has_hra_component(assignment.salary_structure, hra_component):
+				basic_salary_amt, hra_salary_amt = get_component_amt_from_salary_slip(
+					doc.employee,
+					assignment.salary_structure,
+					basic_component,
+					hra_component,
+					assignment.from_date,
+				)
+				to_date = get_end_date_for_assignment(assignment_dates, idx, doc.payroll_period)
+
+				frequency = frappe.get_value(
+					"Salary Structure", assignment.salary_structure, "payroll_frequency"
+				)
+				basic_amount += get_component_pay(frequency, basic_salary_amt, assignment.from_date, to_date)
+				hra_amount += get_component_pay(frequency, hra_salary_amt, assignment.from_date, to_date)
+
+		if hra_amount:
+			if doc.monthly_house_rent:
+				annual_exemption = calculate_hra_exemption(
+					assignment.salary_structure,
+					basic_amount,
+					hra_amount,
+					doc.monthly_house_rent,
+					doc.rented_in_metro_city,
+				)
+				if annual_exemption > 0:
+					monthly_exemption = annual_exemption / 12
+				else:
+					annual_exemption = 0
+
 	return frappe._dict(
 		{
 			"hra_amount": hra_amount,
@@ -407,10 +428,44 @@
 	)
 
 
-def get_component_amt_from_salary_slip(employee, salary_structure, basic_component, hra_component):
+def has_hra_component(salary_structure, hra_component):
+	return frappe.db.exists(
+		"Salary Detail",
+		{
+			"parent": salary_structure,
+			"salary_component": hra_component,
+			"parentfield": "earnings",
+			"parenttype": "Salary Structure",
+		},
+	)
+
+
+def get_end_date_for_assignment(assignment_dates, idx, payroll_period):
+	end_date = None
+
+	try:
+		end_date = assignment_dates[idx + 1]
+		end_date = add_days(end_date, -1)
+	except IndexError:
+		pass
+
+	if not end_date:
+		end_date = frappe.db.get_value("Payroll Period", payroll_period, "end_date")
+
+	return end_date
+
+
+def get_component_amt_from_salary_slip(
+	employee, salary_structure, basic_component, hra_component, from_date
+):
 	salary_slip = make_salary_slip(
 		salary_structure, employee=employee, for_preview=1, ignore_permissions=True
 	)
+	# generate salary slip as per assignment on "from_date"
+	salary_slip.posting_date = from_date
+	salary_slip.start_date = salary_slip.end_date = None
+	salary_slip.run_method("process_salary_structure", for_preview=True)
+
 	basic_amt, hra_amt = 0, 0
 	for earning in salary_slip.earnings:
 		if earning.salary_component == basic_component:
@@ -423,36 +478,37 @@
 
 
 def calculate_hra_exemption(
-	salary_structure, basic, monthly_hra, monthly_house_rent, rented_in_metro_city
+	salary_structure, annual_basic, annual_hra, monthly_house_rent, rented_in_metro_city
 ):
 	# TODO make this configurable
 	exemptions = []
-	frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency")
 	# case 1: The actual amount allotted by the employer as the HRA.
-	exemptions.append(get_annual_component_pay(frequency, monthly_hra))
-
-	actual_annual_rent = monthly_house_rent * 12
-	annual_basic = get_annual_component_pay(frequency, basic)
+	exemptions.append(annual_hra)
 
 	# case 2: Actual rent paid less 10% of the basic salary.
+	actual_annual_rent = monthly_house_rent * 12
 	exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1))
+
 	# case 3: 50% of the basic salary, if the employee is staying in a metro city (40% for a non-metro city).
 	exemptions.append(annual_basic * 0.5 if rented_in_metro_city else annual_basic * 0.4)
+
 	# return minimum of 3 cases
 	return min(exemptions)
 
 
-def get_annual_component_pay(frequency, amount):
+def get_component_pay(frequency, amount, from_date, to_date):
+	days = date_diff(to_date, from_date) + 1
+
 	if frequency == "Daily":
-		return amount * 365
+		return amount * days
 	elif frequency == "Weekly":
-		return amount * 52
+		return amount * math.ceil(days / 7)
 	elif frequency == "Fortnightly":
-		return amount * 26
+		return amount * math.ceil(days / 15)
 	elif frequency == "Monthly":
-		return amount * 12
+		return amount * month_diff(to_date, from_date)
 	elif frequency == "Bimonthly":
-		return amount * 6
+		return amount * math.ceil(days / 60)
 
 
 def validate_house_rent_dates(doc):