Merge pull request #25190 from ruchamahabal/approver-perms

feat(HR): share doc with employee approvers if they don't have access
diff --git a/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py b/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py
index 7a9727f..aa5a67f 100644
--- a/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py
+++ b/erpnext/hr/doctype/compensatory_leave_request/compensatory_leave_request.py
@@ -5,7 +5,7 @@
 from __future__ import unicode_literals
 import frappe
 from frappe import _
-from frappe.utils import date_diff, add_days, getdate, cint
+from frappe.utils import date_diff, add_days, getdate, cint, format_date
 from frappe.model.document import Document
 from erpnext.hr.utils import validate_dates, validate_overlap, get_leave_period, \
 	get_holidays_for_employee, create_additional_leave_ledger_entry
@@ -40,7 +40,12 @@
 	def validate_holidays(self):
 		holidays = get_holidays_for_employee(self.employee, self.work_from_date, self.work_end_date)
 		if len(holidays) < date_diff(self.work_end_date, self.work_from_date) + 1:
-			frappe.throw(_("Compensatory leave request days not in valid holidays"))
+			if date_diff(self.work_end_date, self.work_from_date):
+				msg = _("The days between {0} to {1} are not valid holidays.").format(frappe.bold(format_date(self.work_from_date)), frappe.bold(format_date(self.work_end_date)))
+			else:
+				msg = _("{0} is not a holiday.").format(frappe.bold(format_date(self.work_from_date)))
+
+			frappe.throw(msg)
 
 	def on_submit(self):
 		company = frappe.db.get_value("Employee", self.employee, "company")
@@ -63,7 +68,7 @@
 				leave_allocation = self.create_leave_allocation(leave_period, date_difference)
 			self.leave_allocation=leave_allocation.name
 		else:
-			frappe.throw(_("There is no leave period in between {0} and {1}").format(self.work_from_date, self.work_end_date))
+			frappe.throw(_("There is no leave period in between {0} and {1}").format(format_date(self.work_from_date), format_date(self.work_end_date)))
 
 	def on_cancel(self):
 		if self.leave_allocation:
diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.json b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.json
index 2b5df4b..86ea59d 100644
--- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.json
+++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.json
@@ -21,6 +21,7 @@
   "interest_payable",
   "payable_amount",
   "column_break_9",
+  "shortfall_amount",
   "payable_principal_amount",
   "penalty_amount",
   "amount_paid",
@@ -31,6 +32,7 @@
   "column_break_21",
   "reference_date",
   "principal_amount_paid",
+  "total_penalty_paid",
   "total_interest_paid",
   "repayment_details",
   "amended_from"
@@ -226,12 +228,25 @@
    "fieldtype": "Percent",
    "label": "Rate Of Interest",
    "read_only": 1
+  },
+  {
+   "fieldname": "shortfall_amount",
+   "fieldtype": "Currency",
+   "label": "Shortfall Amount",
+   "options": "Company:company:default_currency",
+   "read_only": 1
+  },
+  {
+   "fieldname": "total_penalty_paid",
+   "fieldtype": "Currency",
+   "label": "Total Penalty Paid",
+   "options": "Company:company:default_currency"
   }
  ],
  "index_web_pages_for_search": 1,
  "is_submittable": 1,
  "links": [],
- "modified": "2020-11-05 10:06:58.792841",
+ "modified": "2021-04-05 13:45:19.137896",
  "modified_by": "Administrator",
  "module": "Loan Management",
  "name": "Loan Repayment",
diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
index bac06c4..a88e183 100644
--- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
+++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
@@ -60,6 +60,12 @@
 		if not self.payable_amount:
 			self.payable_amount = flt(amounts['payable_amount'], precision)
 
+		shortfall_amount = flt(frappe.db.get_value('Loan Security Shortfall', {'loan': self.against_loan, 'status': 'Pending'},
+			'shortfall_amount'))
+
+		if shortfall_amount:
+			self.shortfall_amount = shortfall_amount
+
 		if amounts.get('due_date'):
 			self.due_date = amounts.get('due_date')
 
@@ -69,7 +75,7 @@
 		if not self.amount_paid:
 			frappe.throw(_("Amount paid cannot be zero"))
 
-		if self.amount_paid < self.penalty_amount:
+		if not self.shortfall_amount and self.amount_paid < self.penalty_amount:
 			msg = _("Paid amount cannot be less than {0}").format(self.penalty_amount)
 			frappe.throw(msg)
 
@@ -148,11 +154,28 @@
 	def allocate_amounts(self, repayment_details):
 		self.set('repayment_details', [])
 		self.principal_amount_paid = 0
-		total_interest_paid = 0
-		interest_paid = self.amount_paid - self.penalty_amount
+		self.total_penalty_paid = 0
+		interest_paid = self.amount_paid
 
-		if self.amount_paid - self.penalty_amount > 0:
-			interest_paid = self.amount_paid - self.penalty_amount
+		if self.shortfall_amount and self.amount_paid > self.shortfall_amount:
+			self.principal_amount_paid = self.shortfall_amount
+		elif self.shortfall_amount:
+			self.principal_amount_paid = self.amount_paid
+
+		interest_paid -= self.principal_amount_paid
+
+		if interest_paid > 0:
+			if self.penalty_amount and interest_paid > self.penalty_amount:
+				self.total_penalty_paid = self.penalty_amount
+			elif self.penalty_amount:
+				self.total_penalty_paid = interest_paid
+
+			interest_paid -= self.total_penalty_paid
+
+		total_interest_paid = 0
+		# interest_paid = self.amount_paid - self.principal_amount_paid - self.penalty_amount
+
+		if interest_paid > 0:
 			for lia, amounts in iteritems(repayment_details.get('pending_accrual_entries', [])):
 				if amounts['interest_amount'] + amounts['payable_principal_amount'] <= interest_paid:
 					interest_amount = amounts['interest_amount']
@@ -177,7 +200,7 @@
 					'paid_principal_amount': paid_principal
 				})
 
-		if repayment_details['unaccrued_interest'] and interest_paid:
+		if repayment_details['unaccrued_interest'] and interest_paid > 0:
 			# no of days for which to accrue interest
 			# Interest can only be accrued for an entire day and not partial
 			if interest_paid > repayment_details['unaccrued_interest']:
@@ -193,20 +216,20 @@
 				interest_paid -= no_of_days * per_day_interest
 
 		self.total_interest_paid = total_interest_paid
-		if interest_paid:
+		if interest_paid > 0:
 			self.principal_amount_paid += interest_paid
 
 	def make_gl_entries(self, cancel=0, adv_adj=0):
 		gle_map = []
 		loan_details = frappe.get_doc("Loan", self.against_loan)
 
-		if self.penalty_amount:
+		if self.total_penalty_paid:
 			gle_map.append(
 				self.get_gl_dict({
 					"account": loan_details.loan_account,
 					"against": loan_details.payment_account,
-					"debit": self.penalty_amount,
-					"debit_in_account_currency": self.penalty_amount,
+					"debit": self.total_penalty_paid,
+					"debit_in_account_currency": self.total_penalty_paid,
 					"against_voucher_type": "Loan",
 					"against_voucher": self.against_loan,
 					"remarks": _("Penalty against loan:") + self.against_loan,
@@ -221,8 +244,8 @@
 				self.get_gl_dict({
 					"account": loan_details.penalty_income_account,
 					"against": loan_details.payment_account,
-					"credit": self.penalty_amount,
-					"credit_in_account_currency": self.penalty_amount,
+					"credit": self.total_penalty_paid,
+					"credit_in_account_currency": self.total_penalty_paid,
 					"against_voucher_type": "Loan",
 					"against_voucher": self.against_loan,
 					"remarks": _("Penalty against loan:") + self.against_loan,
diff --git a/erpnext/loan_management/doctype/loan_security_shortfall/loan_security_shortfall.py b/erpnext/loan_management/doctype/loan_security_shortfall/loan_security_shortfall.py
index 6539436..8233b7b 100644
--- a/erpnext/loan_management/doctype/loan_security_shortfall/loan_security_shortfall.py
+++ b/erpnext/loan_management/doctype/loan_security_shortfall/loan_security_shortfall.py
@@ -12,7 +12,7 @@
 class LoanSecurityShortfall(Document):
 	pass
 
-def update_shortfall_status(loan, security_value):
+def update_shortfall_status(loan, security_value, on_cancel=0):
 	loan_security_shortfall = frappe.db.get_value("Loan Security Shortfall",
 		{"loan": loan, "status": "Pending"}, ['name', 'shortfall_amount'], as_dict=1)