Merge pull request #22887 from deepeshgarg007/subscription_test

fix: Test case for subscription
diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.json b/erpnext/accounts/doctype/accounts_settings/accounts_settings.json
index 8ca8b71..b2e8b09 100644
--- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.json
+++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.json
@@ -225,7 +225,7 @@
  "idx": 1,
  "issingle": 1,
  "links": [],
- "modified": "2020-06-22 20:13:26.043092",
+ "modified": "2020-08-03 20:13:26.043092",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Accounts Settings",
diff --git a/erpnext/hr/dashboard_chart/job_application_status/job_application_status.json b/erpnext/hr/dashboard_chart/job_application_status/job_application_status.json
index bfcfa96..42a8309 100644
--- a/erpnext/hr/dashboard_chart/job_application_status/job_application_status.json
+++ b/erpnext/hr/dashboard_chart/job_application_status/job_application_status.json
@@ -7,14 +7,14 @@
  "doctype": "Dashboard Chart",
  "document_type": "Job Applicant",
  "dynamic_filters_json": "",
- "filters_json": "[[\"Job Applicant\",\"creation\",\"Previous\",\"1 month\"]]",
+ "filters_json": "[[\"Job Applicant\",\"creation\",\"Timespan\",\"last month\",false]]",
  "group_by_based_on": "status",
  "group_by_type": "Count",
  "idx": 0,
  "is_public": 1,
  "is_standard": 1,
- "last_synced_on": "2020-07-22 14:27:40.118498",
- "modified": "2020-07-22 14:33:00.404144",
+ "last_synced_on": "2020-07-28 16:19:12.109979",
+ "modified": "2020-07-28 16:19:45.279490",
  "modified_by": "Administrator",
  "module": "HR",
  "name": "Job Application Status",
diff --git a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js
index 4b9b928..42f7cdb 100644
--- a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js
+++ b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js
@@ -35,7 +35,15 @@
 			"fieldname":"employee",
 			"label": __("Employee"),
 			"fieldtype": "Link",
-			"options": "Employee"
+			"options": "Employee",
+			get_query: () => {
+				var company = frappe.query_report.get_filter_value('company');
+				return {
+					filters: {
+						'company': company
+					}
+				};
+			}
 		},
 		{
 			"fieldname":"company",
diff --git a/erpnext/loan_management/doctype/loan/loan.json b/erpnext/loan_management/doctype/loan/loan.json
index 192beee..aa5e21b 100644
--- a/erpnext/loan_management/doctype/loan/loan.json
+++ b/erpnext/loan_management/doctype/loan/loan.json
@@ -20,8 +20,8 @@
   "section_break_8",
   "loan_type",
   "loan_amount",
-  "is_secured_loan",
   "rate_of_interest",
+  "is_secured_loan",
   "disbursement_date",
   "disbursed_amount",
   "column_break_11",
@@ -334,7 +334,7 @@
  ],
  "is_submittable": 1,
  "links": [],
- "modified": "2020-07-02 20:46:40.128142",
+ "modified": "2020-08-01 12:36:11.255233",
  "modified_by": "Administrator",
  "module": "Loan Management",
  "name": "Loan",
diff --git a/erpnext/loan_management/doctype/loan/test_loan.py b/erpnext/loan_management/doctype/loan/test_loan.py
index c65996e..23815d598 100644
--- a/erpnext/loan_management/doctype/loan/test_loan.py
+++ b/erpnext/loan_management/doctype/loan/test_loan.py
@@ -269,7 +269,7 @@
 		self.assertTrue(loan_security_shortfall)
 
 		self.assertEquals(loan_security_shortfall.loan_amount, 1000000.00)
-		self.assertEquals(loan_security_shortfall.security_value, 400000.00)
+		self.assertEquals(loan_security_shortfall.security_value, 800000.00)
 		self.assertEquals(loan_security_shortfall.shortfall_amount, 600000.00)
 
 		frappe.db.sql(""" UPDATE `tabLoan Security Price` SET loan_security_price = 250
diff --git a/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py b/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py
index 961c05c..2bb6fd8 100644
--- a/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py
+++ b/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py
@@ -14,6 +14,7 @@
 	def validate(self):
 		self.set_pledge_amount()
 		self.validate_duplicate_securities()
+		self.validate_loan_security_type()
 
 	def on_submit(self):
 		if self.loan:
@@ -31,6 +32,27 @@
 				frappe.throw(_('Loan Security {0} added multiple times').format(frappe.bold(
 					security.loan_security)))
 
+	def validate_loan_security_type(self):
+		existing_pledge = ''
+
+		if self.loan:
+			existing_pledge = frappe.db.get_value('Loan Security Pledge', {'loan': self.loan}, ['name'])
+
+		if existing_pledge:
+			loan_security_type = frappe.db.get_value('Pledge', {'parent': existing_pledge}, ['loan_security_type'])
+		else:
+			loan_security_type = self.securities[0].loan_security_type
+
+		ltv_ratio_map = frappe._dict(frappe.get_all("Loan Security Type",
+			fields=["name", "loan_to_value_ratio"], as_list=1))
+
+		ltv_ratio = ltv_ratio_map.get(loan_security_type)
+
+		for security in self.securities:
+			if ltv_ratio_map.get(security.loan_security_type) != ltv_ratio:
+				frappe.throw(_("Loan Securities with different LTV ratio cannot be pledged against one loan"))
+
+
 	def set_pledge_amount(self):
 		total_security_value = 0
 		maximum_loan_value = 0
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 ffd9673..02efe24 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
@@ -7,6 +7,7 @@
 from frappe.utils import get_datetime
 from frappe.model.document import Document
 from six import iteritems
+from erpnext.loan_management.doctype.loan_security_unpledge.loan_security_unpledge import get_pledged_security_qty
 
 class LoanSecurityShortfall(Document):
 	pass
@@ -50,31 +51,30 @@
 			"valid_upto": (">=", update_time)
 		}, as_list=1))
 
-	ltv_ratio_map = frappe._dict(frappe.get_all("Loan Security Type",
-		fields=["name", "loan_to_value_ratio"], as_list=1))
-
-	loans = frappe.db.sql(""" SELECT l.name, l.loan_amount, l.total_principal_paid, lp.loan_security, lp.haircut, lp.qty, lp.loan_security_type
-		FROM `tabLoan` l, `tabPledge` lp , `tabLoan Security Pledge`p WHERE lp.parent = p.name and p.loan = l.name and l.docstatus = 1
-		and l.is_secured_loan and l.status = 'Disbursed' and p.status = 'Pledged'""", as_dict=1)
+	loans = frappe.get_all('Loan', fields=['name', 'loan_amount', 'total_principal_paid'],
+		filters={'status': 'Disbursed', 'is_secured_loan': 1})
 
 	loan_security_map = {}
 
 	for loan in loans:
-		loan_security_map.setdefault(loan.name, {
-			"loan_amount": loan.loan_amount - loan.total_principal_paid,
-			"security_value": 0.0
-		})
+		outstanding_amount = loan.loan_amount - loan.total_principal_paid
+		pledged_securities = get_pledged_security_qty(loan.name)
+		ltv_ratio = ''
+		security_value = 0.0
 
-		current_loan_security_amount = loan_security_price_map.get(loan.loan_security, 0) * loan.qty
-		ltv_ratio = ltv_ratio_map.get(loan.loan_security_type)
+		for security, qty in pledged_securities.items():
+			if not ltv_ratio:
+				ltv_ratio = get_ltv_ratio(security)
+			security_value += loan_security_price_map.get(security) * qty
 
-		loan_security_map[loan.name]['security_value'] += current_loan_security_amount - (current_loan_security_amount * loan.haircut/100)
+		current_ratio = (outstanding_amount/security_value) * 100
 
-	for loan, value in iteritems(loan_security_map):
-		if (value["loan_amount"]/value['security_value'] * 100) > ltv_ratio:
-			create_loan_security_shortfall(loan, value, process_loan_security_shortfall)
+		if current_ratio > ltv_ratio:
+			shortfall_amount = outstanding_amount - ((security_value * ltv_ratio) / 100)
+			create_loan_security_shortfall(loan.name, outstanding_amount, security_value, shortfall_amount,
+				process_loan_security_shortfall)
 
-def create_loan_security_shortfall(loan, value, process_loan_security_shortfall):
+def create_loan_security_shortfall(loan, loan_amount, security_value, shortfall_amount, process_loan_security_shortfall):
 
 	existing_shortfall = frappe.db.get_value("Loan Security Shortfall", {"loan": loan, "status": "Pending"}, "name")
 
@@ -85,9 +85,14 @@
 		ltv_shortfall.loan = loan
 
 	ltv_shortfall.shortfall_time = get_datetime()
-	ltv_shortfall.loan_amount = value["loan_amount"]
-	ltv_shortfall.security_value = value["security_value"]
-	ltv_shortfall.shortfall_amount = value["loan_amount"] - value["security_value"]
+	ltv_shortfall.loan_amount = loan_amount
+	ltv_shortfall.security_value = security_value
+	ltv_shortfall.shortfall_amount = shortfall_amount
 	ltv_shortfall.process_loan_security_shortfall = process_loan_security_shortfall
 	ltv_shortfall.save()
 
+def get_ltv_ratio(loan_security):
+	loan_security_type = frappe.db.get_value('Loan Security', loan_security, 'loan_security_type')
+	ltv_ratio = frappe.db.get_value('Loan Security Type', loan_security_type, 'loan_to_value_ratio')
+	return ltv_ratio
+
diff --git a/erpnext/portal/doctype/products_settings/products_settings.py b/erpnext/portal/doctype/products_settings/products_settings.py
index b984aeb..ae7dc68 100644
--- a/erpnext/portal/doctype/products_settings/products_settings.py
+++ b/erpnext/portal/doctype/products_settings/products_settings.py
@@ -11,9 +11,9 @@
 class ProductsSettings(Document):
 	def validate(self):
 		if self.home_page_is_products:
-			frappe.db.set_value("Website Settings", "home_page", "products")
+			frappe.db.set_value("Website Settings", None, "home_page", "products")
 		elif frappe.db.get_single_value("Website Settings", "home_page") == 'products':
-			frappe.db.set_value("Website Settings", "home_page", "home")
+			frappe.db.set_value("Website Settings", None, "home_page", "home")
 
 		self.validate_field_filters()
 		self.validate_attribute_filters()
diff --git a/erpnext/selling/doctype/lead_source/lead_source.json b/erpnext/selling/doctype/lead_source/lead_source.json
index 868f6d1..373e83a 100644
--- a/erpnext/selling/doctype/lead_source/lead_source.json
+++ b/erpnext/selling/doctype/lead_source/lead_source.json
@@ -1,7 +1,7 @@
 {
  "allow_copy": 0, 
  "allow_import": 0, 
- "allow_rename": 0, 
+ "allow_rename": 1, 
  "autoname": "field:source_name", 
  "beta": 0, 
  "creation": "2016-09-16 01:47:47.382372", 
@@ -74,7 +74,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-09-16 02:03:01.441622", 
+ "modified": "2020-09-16 02:03:01.441622", 
  "modified_by": "Administrator", 
  "module": "Selling", 
  "name": "Lead Source", 
@@ -128,4 +128,4 @@
  "sort_field": "modified", 
  "sort_order": "DESC", 
  "track_seen": 0
-}
\ No newline at end of file
+}