chore: keep back code to be a part of other apps / to be ported later
diff --git a/erpnext/regional/india/__init__.py b/erpnext/regional/india/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/regional/india/__init__.py
diff --git a/erpnext/regional/india/setup.py b/erpnext/regional/india/setup.py
new file mode 100644
index 0000000..33f8cb2
--- /dev/null
+++ b/erpnext/regional/india/setup.py
@@ -0,0 +1,321 @@
+# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+
+import frappe
+from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
+
+
+def setup(company=None, patch=True):
+	# Company independent fixtures should be called only once at the first company setup
+	if patch or frappe.db.count("Company", {"country": "India"}) <= 1:
+		make_custom_fields()
+		create_gratuity_rule()
+		create_salary_components()
+
+
+def make_custom_fields():
+	create_custom_fields(
+		{
+			"Salary Component": [
+				{
+					"fieldname": "component_type",
+					"label": "Component Type",
+					"fieldtype": "Select",
+					"insert_after": "description",
+					"options": (
+						"\nProvident Fund\nAdditional Provident Fund\nProvident Fund" " Loan\nProfessional Tax"
+					),
+					"depends_on": 'eval:doc.type == "Deduction"',
+					"translatable": 0,
+				},
+			],
+			"Employee": [
+				{
+					"fieldname": "ifsc_code",
+					"label": "IFSC Code",
+					"fieldtype": "Data",
+					"insert_after": "bank_ac_no",
+					"print_hide": 1,
+					"depends_on": 'eval:doc.salary_mode == "Bank"',
+					"translatable": 0,
+				},
+				{
+					"fieldname": "pan_number",
+					"label": "PAN Number",
+					"fieldtype": "Data",
+					"insert_after": "payroll_cost_center",
+					"print_hide": 1,
+					"translatable": 0,
+				},
+				{
+					"fieldname": "micr_code",
+					"label": "MICR Code",
+					"fieldtype": "Data",
+					"insert_after": "ifsc_code",
+					"print_hide": 1,
+					"depends_on": 'eval:doc.salary_mode == "Bank"',
+					"translatable": 0,
+				},
+				{
+					"fieldname": "provident_fund_account",
+					"label": "Provident Fund Account",
+					"fieldtype": "Data",
+					"insert_after": "pan_number",
+					"translatable": 0,
+				},
+			],
+			"Company": [
+				{
+					"fieldname": "hra_section",
+					"label": "HRA Settings",
+					"fieldtype": "Section Break",
+					"insert_after": "asset_received_but_not_billed",
+					"collapsible": 1,
+				},
+				{
+					"fieldname": "basic_component",
+					"label": "Basic Component",
+					"fieldtype": "Link",
+					"options": "Salary Component",
+					"insert_after": "hra_section",
+				},
+				{
+					"fieldname": "hra_component",
+					"label": "HRA Component",
+					"fieldtype": "Link",
+					"options": "Salary Component",
+					"insert_after": "basic_component",
+				},
+				{
+					"fieldname": "hra_column_break",
+					"fieldtype": "Column Break",
+					"insert_after": "hra_component",
+				},
+				{
+					"fieldname": "arrear_component",
+					"label": "Arrear Component",
+					"fieldtype": "Link",
+					"options": "Salary Component",
+					"insert_after": "hra_column_break",
+				},
+				{
+					"fieldname": "non_profit_section",
+					"label": "Non Profit Settings",
+					"fieldtype": "Section Break",
+					"insert_after": "arrear_component",
+					"collapsible": 1,
+				},
+				{
+					"fieldname": "company_80g_number",
+					"label": "80G Number",
+					"fieldtype": "Data",
+					"insert_after": "non_profit_section",
+					"translatable": 0,
+				},
+				{
+					"fieldname": "with_effect_from",
+					"label": "80G With Effect From",
+					"fieldtype": "Date",
+					"insert_after": "company_80g_number",
+				},
+			],
+			"Employee Tax Exemption Declaration": [
+				{
+					"fieldname": "hra_section",
+					"label": "HRA Exemption",
+					"fieldtype": "Section Break",
+					"insert_after": "declarations",
+				},
+				{
+					"fieldname": "monthly_house_rent",
+					"label": "Monthly House Rent",
+					"fieldtype": "Currency",
+					"insert_after": "hra_section",
+				},
+				{
+					"fieldname": "rented_in_metro_city",
+					"label": "Rented in Metro City",
+					"fieldtype": "Check",
+					"insert_after": "monthly_house_rent",
+					"depends_on": "monthly_house_rent",
+				},
+				{
+					"fieldname": "salary_structure_hra",
+					"label": "HRA as per Salary Structure",
+					"fieldtype": "Currency",
+					"insert_after": "rented_in_metro_city",
+					"read_only": 1,
+					"depends_on": "monthly_house_rent",
+				},
+				{
+					"fieldname": "hra_column_break",
+					"fieldtype": "Column Break",
+					"insert_after": "salary_structure_hra",
+					"depends_on": "monthly_house_rent",
+				},
+				{
+					"fieldname": "annual_hra_exemption",
+					"label": "Annual HRA Exemption",
+					"fieldtype": "Currency",
+					"insert_after": "hra_column_break",
+					"read_only": 1,
+					"depends_on": "monthly_house_rent",
+				},
+				{
+					"fieldname": "monthly_hra_exemption",
+					"label": "Monthly HRA Exemption",
+					"fieldtype": "Currency",
+					"insert_after": "annual_hra_exemption",
+					"read_only": 1,
+					"depends_on": "monthly_house_rent",
+				},
+			],
+			"Employee Tax Exemption Proof Submission": [
+				{
+					"fieldname": "hra_section",
+					"label": "HRA Exemption",
+					"fieldtype": "Section Break",
+					"insert_after": "tax_exemption_proofs",
+				},
+				{
+					"fieldname": "house_rent_payment_amount",
+					"label": "House Rent Payment Amount",
+					"fieldtype": "Currency",
+					"insert_after": "hra_section",
+				},
+				{
+					"fieldname": "rented_in_metro_city",
+					"label": "Rented in Metro City",
+					"fieldtype": "Check",
+					"insert_after": "house_rent_payment_amount",
+					"depends_on": "house_rent_payment_amount",
+				},
+				{
+					"fieldname": "rented_from_date",
+					"label": "Rented From Date",
+					"fieldtype": "Date",
+					"insert_after": "rented_in_metro_city",
+					"depends_on": "house_rent_payment_amount",
+				},
+				{
+					"fieldname": "rented_to_date",
+					"label": "Rented To Date",
+					"fieldtype": "Date",
+					"insert_after": "rented_from_date",
+					"depends_on": "house_rent_payment_amount",
+				},
+				{
+					"fieldname": "hra_column_break",
+					"fieldtype": "Column Break",
+					"insert_after": "rented_to_date",
+					"depends_on": "house_rent_payment_amount",
+				},
+				{
+					"fieldname": "monthly_house_rent",
+					"label": "Monthly House Rent",
+					"fieldtype": "Currency",
+					"insert_after": "hra_column_break",
+					"read_only": 1,
+					"depends_on": "house_rent_payment_amount",
+				},
+				{
+					"fieldname": "monthly_hra_exemption",
+					"label": "Monthly Eligible Amount",
+					"fieldtype": "Currency",
+					"insert_after": "monthly_house_rent",
+					"read_only": 1,
+					"depends_on": "house_rent_payment_amount",
+				},
+				{
+					"fieldname": "total_eligible_hra_exemption",
+					"label": "Total Eligible HRA Exemption",
+					"fieldtype": "Currency",
+					"insert_after": "monthly_hra_exemption",
+					"read_only": 1,
+					"depends_on": "house_rent_payment_amount",
+				},
+			],
+		}
+	)
+
+
+def create_gratuity_rule():
+	# Standard Indain Gratuity Rule
+	if frappe.db.exists("Gratuity Rule", "Indian Standard Gratuity Rule"):
+		return
+
+	rule = frappe.new_doc("Gratuity Rule")
+	rule.update(
+		{
+			"name": "Indian Standard Gratuity Rule",
+			"calculate_gratuity_amount_based_on": "Current Slab",
+			"work_experience_calculation_method": "Round Off Work Experience",
+			"minimum_year_for_gratuity": 5,
+			"gratuity_rule_slabs": [
+				{
+					"from_year": 0,
+					"to_year": 0,
+					"fraction_of_applicable_earnings": 15 / 26,
+				}
+			],
+		}
+	)
+	rule.flags.ignore_mandatory = True
+	rule.save()
+
+
+def create_salary_components():
+	docs = [
+		{
+			"doctype": "Salary Component",
+			"salary_component": "Professional Tax",
+			"description": "Professional Tax",
+			"type": "Deduction",
+			"exempted_from_income_tax": 1,
+		},
+		{
+			"doctype": "Salary Component",
+			"salary_component": "Provident Fund",
+			"description": "Provident fund",
+			"type": "Deduction",
+			"is_tax_applicable": 1,
+		},
+		{
+			"doctype": "Salary Component",
+			"salary_component": "House Rent Allowance",
+			"description": "House Rent Allowance",
+			"type": "Earning",
+			"is_tax_applicable": 1,
+		},
+		{
+			"doctype": "Salary Component",
+			"salary_component": "Basic",
+			"description": "Basic",
+			"type": "Earning",
+			"is_tax_applicable": 1,
+		},
+		{
+			"doctype": "Salary Component",
+			"salary_component": "Arrear",
+			"description": "Arrear",
+			"type": "Earning",
+			"is_tax_applicable": 1,
+		},
+		{
+			"doctype": "Salary Component",
+			"salary_component": "Leave Encashment",
+			"description": "Leave Encashment",
+			"type": "Earning",
+			"is_tax_applicable": 1,
+		},
+	]
+
+	for d in docs:
+		try:
+			doc = frappe.get_doc(d)
+			doc.flags.ignore_permissions = True
+			doc.insert(ignore_if_duplicate=True)
+
+		except frappe.DuplicateEntryError:
+			frappe.clear_messages()
diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py
new file mode 100644
index 0000000..23de075
--- /dev/null
+++ b/erpnext/regional/india/utils.py
@@ -0,0 +1,200 @@
+import math
+
+import frappe
+from frappe import _
+from frappe.utils import add_days, date_diff, flt, get_link_to_form, month_diff
+
+from erpnext.hr.utils import get_salary_assignments
+from erpnext.payroll.doctype.salary_structure.salary_structure import make_salary_slip
+
+
+def calculate_annual_eligible_hra_exemption(doc):
+	basic_component, hra_component = frappe.db.get_value(
+		"Company", doc.company, ["basic_component", "hra_component"]
+	)
+
+	if not (basic_component and hra_component):
+		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:
+		assignments = get_salary_assignments(doc.employee, doc.payroll_period)
+
+		if not assignments and doc.docstatus == 1:
+			frappe.throw(
+				_("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(
+					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,
+			"annual_exemption": annual_exemption,
+			"monthly_exemption": monthly_exemption,
+		}
+	)
+
+
+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,
+		posting_date=from_date,
+	)
+
+	basic_amt, hra_amt = 0, 0
+	for earning in salary_slip.earnings:
+		if earning.salary_component == basic_component:
+			basic_amt = earning.amount
+		elif earning.salary_component == hra_component:
+			hra_amt = earning.amount
+		if basic_amt and hra_amt:
+			return basic_amt, hra_amt
+	return basic_amt, hra_amt
+
+
+def calculate_hra_exemption(annual_basic, annual_hra, monthly_house_rent, rented_in_metro_city):
+	# TODO make this configurable
+	exemptions = []
+	# case 1: The actual amount allotted by the employer as the HRA.
+	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_component_pay(frequency, amount, from_date, to_date):
+	days = date_diff(to_date, from_date) + 1
+
+	if frequency == "Daily":
+		return amount * days
+	elif frequency == "Weekly":
+		return amount * math.floor(days / 7)
+	elif frequency == "Fortnightly":
+		return amount * math.floor(days / 14)
+	elif frequency == "Monthly":
+		return amount * month_diff(to_date, from_date)
+	elif frequency == "Bimonthly":
+		return amount * (month_diff(to_date, from_date) / 2)
+
+
+def calculate_hra_exemption_for_period(doc):
+	monthly_rent, eligible_hra = 0, 0
+	if doc.house_rent_payment_amount:
+		validate_house_rent_dates(doc)
+		# TODO receive rented months or validate dates are start and end of months?
+		# Calc monthly rent, round to nearest .5
+		factor = flt(date_diff(doc.rented_to_date, doc.rented_from_date) + 1) / 30
+		factor = round(factor * 2) / 2
+		monthly_rent = doc.house_rent_payment_amount / factor
+		# update field used by calculate_annual_eligible_hra_exemption
+		doc.monthly_house_rent = monthly_rent
+		exemptions = calculate_annual_eligible_hra_exemption(doc)
+
+		if exemptions["monthly_exemption"]:
+			# calc total exemption amount
+			eligible_hra = exemptions["monthly_exemption"] * factor
+		exemptions["monthly_house_rent"] = monthly_rent
+		exemptions["total_eligible_hra_exemption"] = eligible_hra
+		return exemptions
+
+
+def validate_house_rent_dates(doc):
+	if not doc.rented_to_date or not doc.rented_from_date:
+		frappe.throw(_("House rented dates required for exemption calculation"))
+
+	if date_diff(doc.rented_to_date, doc.rented_from_date) < 14:
+		frappe.throw(_("House rented dates should be atleast 15 days apart"))
+
+	proofs = frappe.db.sql(
+		"""
+        select name
+        from `tabEmployee Tax Exemption Proof Submission`
+        where
+            docstatus=1 and employee=%(employee)s and payroll_period=%(payroll_period)s
+            and (rented_from_date between %(from_date)s and %(to_date)s or rented_to_date between %(from_date)s and %(to_date)s)
+    """,
+		{
+			"employee": doc.employee,
+			"payroll_period": doc.payroll_period,
+			"from_date": doc.rented_from_date,
+			"to_date": doc.rented_to_date,
+		},
+	)
+
+	if proofs:
+		frappe.throw(_("House rent paid days overlapping with {0}").format(proofs[0][0]))