feat: auto leave encashment
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 4f89533..3a73e23 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -271,11 +271,12 @@
 		"erpnext.projects.doctype.project.project.update_project_sales_billing",
 		"erpnext.projects.doctype.project.project.send_project_status_email_to_users",
 		"erpnext.quality_management.doctype.quality_review.quality_review.review",
-		"erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry.process_expired_allocation"
 		"erpnext.support.doctype.service_level_agreement.service_level_agreement.check_agreement_status",
 	],
 	"daily_long": [
-		"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_latest_price_in_all_boms"
+		"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_latest_price_in_all_boms",
+		"erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry.process_expired_allocation",
+		"erpnext.hr.utils.generate_leave_encashment"
 	],
 	"monthly_long": [
 		"erpnext.accounts.deferred_revenue.convert_deferred_revenue_to_income",
diff --git a/erpnext/hr/doctype/hr_settings/hr_settings.json b/erpnext/hr/doctype/hr_settings/hr_settings.json
index 8dd0acf..a41c887 100644
--- a/erpnext/hr/doctype/hr_settings/hr_settings.json
+++ b/erpnext/hr/doctype/hr_settings/hr_settings.json
@@ -24,6 +24,7 @@
   "column_break_18",
   "leave_approver_mandatory_in_leave_application",
   "show_leaves_of_all_department_members_in_calendar",
+  "auto_leave_encashment",
   "hiring_settings",
   "check_vacancies"
  ],
@@ -153,12 +154,18 @@
    "fieldname": "check_vacancies",
    "fieldtype": "Check",
    "label": "Check Vacancies On Job Offer Creation"
+  },
+  {
+   "default": "0",
+   "fieldname": "auto_leave_encashment",
+   "fieldtype": "Check",
+   "label": "Auto Leave Encashment"
   }
  ],
  "icon": "fa fa-cog",
  "idx": 1,
  "issingle": 1,
- "modified": "2019-07-01 18:59:55.256878",
+ "modified": "2019-08-05 13:07:17.993968",
  "modified_by": "Administrator",
  "module": "HR",
  "name": "HR Settings",
diff --git a/erpnext/hr/doctype/leave_encashment/leave_encashment.py b/erpnext/hr/doctype/leave_encashment/leave_encashment.py
index c47ff85..d256c7f 100644
--- a/erpnext/hr/doctype/leave_encashment/leave_encashment.py
+++ b/erpnext/hr/doctype/leave_encashment/leave_encashment.py
@@ -64,7 +64,7 @@
 
 		allocation = self.get_leave_allocation()
 
-		self.leave_balance = allocation.total_leaves_allocated - get_unused_leaves(self.employee, self.leave_type, allocation.from_date, self.encashment_date)
+		self.leave_balance = allocation.total_leaves_allocated - get_unused_leaves(self.employee, self.leave_type, self.encashment_date)
 
 		encashable_days = self.leave_balance - frappe.db.get_value('Leave Type', self.leave_type, 'encashment_threshold_days')
 		self.encashable_days = encashable_days if encashable_days > 0 else 0
@@ -102,3 +102,17 @@
 			)
 			create_leave_ledger_entry(self, args, submit)
 
+
+def create_leave_encashment(leave_allocation):
+	''' Creates leave encashment for the given allocations '''
+	for allocation in leave_allocation:
+		if not get_assigned_salary_structure(allocation.employee, allocation.to_date):
+			continue
+		leave_encashment = frappe.get_doc(dict(
+			doctype="Leave Encashment",
+			leave_period=allocation.leave_period,
+			employee=allocation.employee,
+			leave_type=allocation.leave_type,
+			encashment_date=allocation.to_date
+		))
+		leave_encashment.insert(ignore_permissions=True)
\ No newline at end of file
diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py
index 1d9db07..bf09de8 100644
--- a/erpnext/hr/utils.py
+++ b/erpnext/hr/utils.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import frappe, erpnext
 from frappe import _
-from frappe.utils import formatdate, format_datetime, getdate, get_datetime, nowdate, flt, cstr
+from frappe.utils import formatdate, format_datetime, getdate, get_datetime, nowdate, flt, cstr, add_days, today
 from frappe.model.document import Document
 from frappe.desk.form import assign_to
 from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
@@ -263,6 +263,19 @@
 	if leave_period:
 		return leave_period
 
+def generate_leave_encashment():
+	''' Generates a draft leave encashment on allocation expiry '''
+	from erpnext.hr.doctype.leave_encashment.leave_encashment import create_leave_encashment
+	if frappe.db.get_single_value('HR Settings', 'auto_leave_encashment'):
+		leave_type = frappe.db.sql_list("SELECT name FROM `tabLeave Type` WHERE `allow_encashment`=1")
+
+		leave_allocation = frappe.get_all("Leave Allocation", filters={
+			'to_date': add_days(today(), -1),
+			'leave_type': ('in', leave_type)
+		}, fields=['employee', 'leave_period', 'leave_type', 'to_date', 'total_leaves_allocated', 'new_leaves_allocated'])
+
+		create_leave_encashment(leave_allocation=leave_allocation)
+
 def allocate_earned_leaves():
 	'''Allocate earned leaves to Employees'''
 	e_leave_types = frappe.get_all("Leave Type",