Merge pull request #20781 from deepeshgarg007/loan_shortfall_enhance

fix: Add loan to value ratio in loan security type
diff --git a/erpnext/accounts/report/asset_depreciations_and_balances/asset_depreciations_and_balances.py b/erpnext/accounts/report/asset_depreciations_and_balances/asset_depreciations_and_balances.py
index 7854660..d7efbad 100644
--- a/erpnext/accounts/report/asset_depreciations_and_balances/asset_depreciations_and_balances.py
+++ b/erpnext/accounts/report/asset_depreciations_and_balances/asset_depreciations_and_balances.py
@@ -29,7 +29,7 @@
 
 		row.update(next(asset for asset in assets if asset["asset_category"] == asset_category.get("asset_category", "")))
 		row.accumulated_depreciation_as_on_to_date = (flt(row.accumulated_depreciation_as_on_from_date) +
-				flt(row.depreciation_amount_during_the_period) - flt(row.depreciation_eliminated))
+				flt(row.depreciation_amount_during_the_period) - flt(row.depreciation_eliminated_during_the_period))
 
 		row.net_asset_value_as_on_from_date = (flt(row.cost_as_on_from_date) -
 				flt(row.accumulated_depreciation_as_on_from_date))
@@ -86,7 +86,6 @@
 		group by asset_category
 	""", {"to_date": filters.to_date, "from_date": filters.from_date, "company": filters.company}, as_dict=1)
 
-
 def get_assets(filters):
 	return frappe.db.sql("""
 		SELECT results.asset_category,
@@ -94,9 +93,7 @@
 			   sum(results.depreciation_eliminated_during_the_period) as depreciation_eliminated_during_the_period,
 			   sum(results.depreciation_amount_during_the_period) as depreciation_amount_during_the_period
 		from (SELECT a.asset_category,
-				   ifnull(sum(a.opening_accumulated_depreciation +
-							  case when ds.schedule_date < %(from_date)s and
-										(ifnull(a.disposal_date, 0) = 0 or a.disposal_date >= %(from_date)s) then
+				   ifnull(sum(case when ds.schedule_date < %(from_date)s then
 								   ds.depreciation_amount
 							  else
 								   0
@@ -107,7 +104,6 @@
 							  else
 								   0
 							  end), 0) as depreciation_eliminated_during_the_period,
-
 				   ifnull(sum(case when ds.schedule_date >= %(from_date)s and ds.schedule_date <= %(to_date)s
 										and (ifnull(a.disposal_date, 0) = 0 or ds.schedule_date <= a.disposal_date) then
 								   ds.depreciation_amount
@@ -120,7 +116,8 @@
 			union
 			SELECT a.asset_category,
 				   ifnull(sum(case when ifnull(a.disposal_date, 0) != 0
-										and (a.disposal_date < %(from_date)s or a.disposal_date > %(to_date)s) then
+										and (a.disposal_date < %(from_date)s or a.disposal_date > %(to_date)s) 
+										then
 									0
 							   else
 									a.opening_accumulated_depreciation
@@ -133,7 +130,6 @@
 				   0 as depreciation_amount_during_the_period
 			from `tabAsset` a
 			where a.docstatus=1 and a.company=%(company)s and a.purchase_date <= %(to_date)s
-			and not exists(select * from `tabDepreciation Schedule` ds where a.name = ds.parent)
 			group by a.asset_category) as results
 		group by results.asset_category
 		""", {"to_date": filters.to_date, "from_date": filters.from_date, "company": filters.company}, as_dict=1)
diff --git a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
index 8b6359c..4523f66 100644
--- a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
+++ b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
@@ -306,10 +306,6 @@
 
 def get_items(filters, additional_query_columns):
 	conditions = get_conditions(filters)
-	match_conditions = frappe.build_match_conditions("Purchase Invoice")
-
-	if match_conditions:
-		match_conditions = " and {0} ".format(match_conditions)
 
 	if additional_query_columns:
 		additional_query_columns = ', ' + ', '.join(additional_query_columns)
@@ -327,8 +323,8 @@
 			`tabPurchase Invoice`.supplier_name, `tabPurchase Invoice`.mode_of_payment {0}
 		from `tabPurchase Invoice`, `tabPurchase Invoice Item`
 		where `tabPurchase Invoice`.name = `tabPurchase Invoice Item`.`parent` and
-		`tabPurchase Invoice`.docstatus = 1 %s %s
-	""".format(additional_query_columns) % (conditions, match_conditions), filters, as_dict=1)
+		`tabPurchase Invoice`.docstatus = 1 %s
+	""".format(additional_query_columns) % (conditions), filters, as_dict=1)
 
 def get_aii_accounts():
 	return dict(frappe.db.sql("select name, stock_received_but_not_billed from tabCompany"))
diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
index 2cc2db6..786e04d 100644
--- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
+++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
@@ -119,7 +119,7 @@
 		add_sub_total_row(total_row, total_row_map, 'total_row', tax_columns)
 		data.append(total_row_map.get('total_row'))
 		skip_total_row = 1
-	
+
 	return columns, data, None, None, None, skip_total_row
 
 def get_columns(additional_table_columns, filters):
@@ -370,10 +370,6 @@
 
 def get_items(filters, additional_query_columns):
 	conditions = get_conditions(filters)
-	match_conditions = frappe.build_match_conditions("Sales Invoice")
-
-	if match_conditions:
-		match_conditions = " and {0} ".format(match_conditions)
 
 	if additional_query_columns:
 		additional_query_columns = ', ' + ', '.join(additional_query_columns)
@@ -394,8 +390,8 @@
 			`tabSales Invoice`.update_stock, `tabSales Invoice Item`.uom, `tabSales Invoice Item`.qty {0}
 		from `tabSales Invoice`, `tabSales Invoice Item`
 		where `tabSales Invoice`.name = `tabSales Invoice Item`.parent
-			and `tabSales Invoice`.docstatus = 1 {1} {2}
-		""".format(additional_query_columns or '', conditions, match_conditions), filters, as_dict=1) #nosec
+			and `tabSales Invoice`.docstatus = 1 {1}
+		""".format(additional_query_columns or '', conditions), filters, as_dict=1) #nosec
 
 def get_delivery_notes_against_sales_order(item_list):
 	so_dn_map = frappe._dict()
diff --git a/erpnext/hr/doctype/shift_type/shift_type.py b/erpnext/hr/doctype/shift_type/shift_type.py
index 4988410..d56080e 100644
--- a/erpnext/hr/doctype/shift_type/shift_type.py
+++ b/erpnext/hr/doctype/shift_type/shift_type.py
@@ -75,7 +75,7 @@
 		for date in dates:
 			shift_details = get_employee_shift(employee, date, True)
 			if shift_details and shift_details.shift_type.name == self.name:
-				mark_attendance(employee, date, self.name, 'Absent')
+				mark_attendance(employee, date, 'Absent', self.name)
 
 	def get_assigned_employee(self, from_date=None, consider_default_shift=False):
 		filters = {'date':('>=', from_date), 'shift_type': self.name, 'docstatus': '1'}
diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py
index 1503adb..8ecc13b 100644
--- a/erpnext/setup/doctype/company/delete_company_transactions.py
+++ b/erpnext/setup/doctype/company/delete_company_transactions.py
@@ -108,8 +108,8 @@
 def delete_communications(doctype, company_name, company_fieldname):
 		reference_docs = frappe.get_all(doctype, filters={company_fieldname:company_name})
 		reference_doc_names = [r.name for r in reference_docs]
-		
+
 		communications = frappe.get_all("Communication", filters={"reference_doctype":doctype,"reference_name":["in", reference_doc_names]})
 		communication_names = [c.name for c in communications]
 
-		frappe.delete_doc("Communication", communication_names)
+		frappe.delete_doc("Communication", communication_names, ignore_permissions=True)