Merge pull request #36685 from shariquerik/api-fix

diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.json b/erpnext/accounts/doctype/journal_entry/journal_entry.json
index 80df0ff..2eb54a5 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.json
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.json
@@ -89,7 +89,7 @@
    "label": "Entry Type",
    "oldfieldname": "voucher_type",
    "oldfieldtype": "Select",
-   "options": "Journal Entry\nInter Company Journal Entry\nBank Entry\nCash Entry\nCredit Card Entry\nDebit Note\nCredit Note\nContra Entry\nExcise Entry\nWrite Off Entry\nOpening Entry\nDepreciation Entry\nExchange Rate Revaluation\nExchange Gain Or Loss\nDeferred Revenue\nDeferred Expense\nReversal Of ITC",
+   "options": "Journal Entry\nInter Company Journal Entry\nBank Entry\nCash Entry\nCredit Card Entry\nDebit Note\nCredit Note\nContra Entry\nExcise Entry\nWrite Off Entry\nOpening Entry\nDepreciation Entry\nExchange Rate Revaluation\nExchange Gain Or Loss\nDeferred Revenue\nDeferred Expense",
    "reqd": 1,
    "search_index": 1
   },
@@ -555,7 +555,45 @@
  "name": "Journal Entry",
  "naming_rule": "By \"Naming Series\" field",
  "owner": "Administrator",
- "permissions": [],
+ "permissions": [
+  {
+   "amend": 1,
+   "cancel": 1,
+   "create": 1,
+   "delete": 1,
+   "email": 1,
+   "print": 1,
+   "read": 1,
+   "report": 1,
+   "role": "Accounts User",
+   "share": 1,
+   "submit": 1,
+   "write": 1
+  },
+  {
+   "amend": 1,
+   "cancel": 1,
+   "create": 1,
+   "delete": 1,
+   "email": 1,
+   "export": 1,
+   "import": 1,
+   "print": 1,
+   "read": 1,
+   "report": 1,
+   "role": "Accounts Manager",
+   "share": 1,
+   "submit": 1,
+   "write": 1
+  },
+  {
+   "email": 1,
+   "print": 1,
+   "read": 1,
+   "report": 1,
+   "role": "Auditor"
+  }
+ ],
  "search_fields": "voucher_type,posting_date, due_date, cheque_no",
  "sort_field": "modified",
  "sort_order": "DESC",
diff --git a/erpnext/accounts/doctype/loyalty_program/loyalty_program.py b/erpnext/accounts/doctype/loyalty_program/loyalty_program.py
index a134f74..4f58579 100644
--- a/erpnext/accounts/doctype/loyalty_program/loyalty_program.py
+++ b/erpnext/accounts/doctype/loyalty_program/loyalty_program.py
@@ -145,8 +145,8 @@
 
 		loyalty_amount = flt(points_to_redeem * loyalty_program_details.conversion_factor)
 
-		if loyalty_amount > ref_doc.grand_total:
-			frappe.throw(_("You can't redeem Loyalty Points having more value than the Grand Total."))
+		if loyalty_amount > ref_doc.rounded_total:
+			frappe.throw(_("You can't redeem Loyalty Points having more value than the Rounded Total."))
 
 		if not ref_doc.loyalty_amount and ref_doc.loyalty_amount != loyalty_amount:
 			ref_doc.loyalty_amount = loyalty_amount
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index c6576f7..ac31e8a 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -230,84 +230,88 @@
 		return False
 
 	def validate_allocated_amount_with_latest_data(self):
-		latest_references = get_outstanding_reference_documents(
-			{
-				"posting_date": self.posting_date,
-				"company": self.company,
-				"party_type": self.party_type,
-				"payment_type": self.payment_type,
-				"party": self.party,
-				"party_account": self.paid_from if self.payment_type == "Receive" else self.paid_to,
-				"get_outstanding_invoices": True,
-				"get_orders_to_be_billed": True,
-			},
-			validate=True,
-		)
+		if self.references:
+			uniq_vouchers = set([(x.reference_doctype, x.reference_name) for x in self.references])
+			vouchers = [frappe._dict({"voucher_type": x[0], "voucher_no": x[1]}) for x in uniq_vouchers]
+			latest_references = get_outstanding_reference_documents(
+				{
+					"posting_date": self.posting_date,
+					"company": self.company,
+					"party_type": self.party_type,
+					"payment_type": self.payment_type,
+					"party": self.party,
+					"party_account": self.paid_from if self.payment_type == "Receive" else self.paid_to,
+					"get_outstanding_invoices": True,
+					"get_orders_to_be_billed": True,
+					"vouchers": vouchers,
+				},
+				validate=True,
+			)
 
-		# Group latest_references by (voucher_type, voucher_no)
-		latest_lookup = {}
-		for d in latest_references:
-			d = frappe._dict(d)
-			latest_lookup.setdefault((d.voucher_type, d.voucher_no), frappe._dict())[d.payment_term] = d
+			# Group latest_references by (voucher_type, voucher_no)
+			latest_lookup = {}
+			for d in latest_references:
+				d = frappe._dict(d)
+				latest_lookup.setdefault((d.voucher_type, d.voucher_no), frappe._dict())[d.payment_term] = d
 
-		for idx, d in enumerate(self.get("references"), start=1):
-			latest = latest_lookup.get((d.reference_doctype, d.reference_name)) or frappe._dict()
+			for idx, d in enumerate(self.get("references"), start=1):
+				latest = latest_lookup.get((d.reference_doctype, d.reference_name)) or frappe._dict()
 
-			# If term based allocation is enabled, throw
-			if (
-				d.payment_term is None or d.payment_term == ""
-			) and self.term_based_allocation_enabled_for_reference(
-				d.reference_doctype, d.reference_name
-			):
-				frappe.throw(
-					_(
-						"{0} has Payment Term based allocation enabled. Select a Payment Term for Row #{1} in Payment References section"
-					).format(frappe.bold(d.reference_name), frappe.bold(idx))
-				)
-
-			# if no payment template is used by invoice and has a custom term(no `payment_term`), then invoice outstanding will be in 'None' key
-			latest = latest.get(d.payment_term) or latest.get(None)
-
-			# The reference has already been fully paid
-			if not latest:
-				frappe.throw(
-					_("{0} {1} has already been fully paid.").format(_(d.reference_doctype), d.reference_name)
-				)
-			# The reference has already been partly paid
-			elif latest.outstanding_amount < latest.invoice_amount and flt(
-				d.outstanding_amount, d.precision("outstanding_amount")
-			) != flt(latest.outstanding_amount, d.precision("outstanding_amount")):
-				frappe.throw(
-					_(
-						"{0} {1} has already been partly paid. Please use the 'Get Outstanding Invoice' or the 'Get Outstanding Orders' button to get the latest outstanding amounts."
-					).format(_(d.reference_doctype), d.reference_name)
-				)
-
-			fail_message = _("Row #{0}: Allocated Amount cannot be greater than outstanding amount.")
-
-			if (
-				d.payment_term
-				and (
-					(flt(d.allocated_amount)) > 0
-					and latest.payment_term_outstanding
-					and (flt(d.allocated_amount) > flt(latest.payment_term_outstanding))
-				)
-				and self.term_based_allocation_enabled_for_reference(d.reference_doctype, d.reference_name)
-			):
-				frappe.throw(
-					_(
-						"Row #{0}: Allocated amount:{1} is greater than outstanding amount:{2} for Payment Term {3}"
-					).format(
-						d.idx, d.allocated_amount, latest.payment_term_outstanding, d.payment_term
+				# If term based allocation is enabled, throw
+				if (
+					d.payment_term is None or d.payment_term == ""
+				) and self.term_based_allocation_enabled_for_reference(
+					d.reference_doctype, d.reference_name
+				):
+					frappe.throw(
+						_(
+							"{0} has Payment Term based allocation enabled. Select a Payment Term for Row #{1} in Payment References section"
+						).format(frappe.bold(d.reference_name), frappe.bold(idx))
 					)
-				)
 
-			if (flt(d.allocated_amount)) > 0 and flt(d.allocated_amount) > flt(latest.outstanding_amount):
-				frappe.throw(fail_message.format(d.idx))
+				# if no payment template is used by invoice and has a custom term(no `payment_term`), then invoice outstanding will be in 'None' key
+				latest = latest.get(d.payment_term) or latest.get(None)
 
-			# Check for negative outstanding invoices as well
-			if flt(d.allocated_amount) < 0 and flt(d.allocated_amount) < flt(latest.outstanding_amount):
-				frappe.throw(fail_message.format(d.idx))
+				# The reference has already been fully paid
+				if not latest:
+					frappe.throw(
+						_("{0} {1} has already been fully paid.").format(_(d.reference_doctype), d.reference_name)
+					)
+				# The reference has already been partly paid
+				elif latest.outstanding_amount < latest.invoice_amount and flt(
+					d.outstanding_amount, d.precision("outstanding_amount")
+				) != flt(latest.outstanding_amount, d.precision("outstanding_amount")):
+					frappe.throw(
+						_(
+							"{0} {1} has already been partly paid. Please use the 'Get Outstanding Invoice' or the 'Get Outstanding Orders' button to get the latest outstanding amounts."
+						).format(_(d.reference_doctype), d.reference_name)
+					)
+
+				fail_message = _("Row #{0}: Allocated Amount cannot be greater than outstanding amount.")
+
+				if (
+					d.payment_term
+					and (
+						(flt(d.allocated_amount)) > 0
+						and latest.payment_term_outstanding
+						and (flt(d.allocated_amount) > flt(latest.payment_term_outstanding))
+					)
+					and self.term_based_allocation_enabled_for_reference(d.reference_doctype, d.reference_name)
+				):
+					frappe.throw(
+						_(
+							"Row #{0}: Allocated amount:{1} is greater than outstanding amount:{2} for Payment Term {3}"
+						).format(
+							d.idx, d.allocated_amount, latest.payment_term_outstanding, d.payment_term
+						)
+					)
+
+				if (flt(d.allocated_amount)) > 0 and flt(d.allocated_amount) > flt(latest.outstanding_amount):
+					frappe.throw(fail_message.format(d.idx))
+
+				# Check for negative outstanding invoices as well
+				if flt(d.allocated_amount) < 0 and flt(d.allocated_amount) < flt(latest.outstanding_amount):
+					frappe.throw(fail_message.format(d.idx))
 
 	def delink_advance_entry_references(self):
 		for reference in self.references:
@@ -1587,6 +1591,7 @@
 			min_outstanding=args.get("outstanding_amt_greater_than"),
 			max_outstanding=args.get("outstanding_amt_less_than"),
 			accounting_dimensions=accounting_dimensions_filter,
+			vouchers=args.get("vouchers") or None,
 		)
 
 		outstanding_invoices = split_invoices_based_on_payment_terms(
diff --git a/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js b/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js
index a6c0102..faceaf3 100644
--- a/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js
+++ b/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js
@@ -153,7 +153,7 @@
 frappe.ui.form.on('POS Closing Entry Detail', {
 	closing_amount: (frm, cdt, cdn) => {
 		const row = locals[cdt][cdn];
-		frappe.model.set_value(cdt, cdn, "difference", flt(row.expected_amount - row.closing_amount));
+		frappe.model.set_value(cdt, cdn, "difference", flt(row.closing_amount - row.expected_amount));
 	}
 })
 
@@ -185,6 +185,7 @@
 		}
 		if (payment) {
 			payment.expected_amount += flt(p.amount);
+			payment.closing_amount = payment.expected_amount;
 			payment.difference = payment.closing_amount - payment.expected_amount;
 		} else {
 			frm.add_child("payment_reconciliation", {
diff --git a/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.json b/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.json
index 9d15e6c..a98a24c 100644
--- a/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.json
+++ b/erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.json
@@ -221,6 +221,7 @@
    "read_only": 1
   },
   {
+   "default": "Now",
    "fieldname": "posting_time",
    "fieldtype": "Time",
    "label": "Posting Time",
@@ -235,7 +236,7 @@
    "link_fieldname": "pos_closing_entry"
   }
  ],
- "modified": "2022-08-01 11:37:14.991228",
+ "modified": "2023-08-10 16:25:49.322697",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "POS Closing Entry",
diff --git a/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py b/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py
index 1deb3c5..93ba90a 100644
--- a/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py
+++ b/erpnext/accounts/doctype/pos_closing_entry/test_pos_closing_entry.py
@@ -8,9 +8,11 @@
 from erpnext.accounts.doctype.pos_closing_entry.pos_closing_entry import (
 	make_closing_entry_from_opening,
 )
+from erpnext.accounts.doctype.pos_invoice.pos_invoice import make_sales_return
 from erpnext.accounts.doctype.pos_invoice.test_pos_invoice import create_pos_invoice
 from erpnext.accounts.doctype.pos_opening_entry.test_pos_opening_entry import create_opening_entry
 from erpnext.accounts.doctype.pos_profile.test_pos_profile import make_pos_profile
+from erpnext.selling.page.point_of_sale.point_of_sale import get_items
 from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
 
 
@@ -67,6 +69,36 @@
 
 		self.assertTrue(pcv_doc.name)
 
+	def test_pos_qty_for_item(self):
+		"""
+		Test if quantity is calculated correctly for an item in POS Closing Entry
+		"""
+		test_user, pos_profile = init_user_and_profile()
+		opening_entry = create_opening_entry(pos_profile, test_user.name)
+
+		test_item_qty = get_test_item_qty(pos_profile)
+
+		pos_inv1 = create_pos_invoice(rate=3500, do_not_submit=1)
+		pos_inv1.append("payments", {"mode_of_payment": "Cash", "account": "Cash - _TC", "amount": 3500})
+		pos_inv1.submit()
+
+		pos_inv2 = create_pos_invoice(rate=3200, do_not_submit=1)
+		pos_inv2.append("payments", {"mode_of_payment": "Cash", "account": "Cash - _TC", "amount": 3200})
+		pos_inv2.submit()
+
+		# make return entry of pos_inv2
+		pos_return = make_sales_return(pos_inv2.name)
+		pos_return.paid_amount = pos_return.grand_total
+		pos_return.save()
+		pos_return.submit()
+
+		pcv_doc = make_closing_entry_from_opening(opening_entry)
+		pcv_doc.submit()
+
+		opening_entry = create_opening_entry(pos_profile, test_user.name)
+		test_item_qty_after_sales = get_test_item_qty(pos_profile)
+		self.assertEqual(test_item_qty_after_sales, test_item_qty - 1)
+
 	def test_cancelling_of_pos_closing_entry(self):
 		test_user, pos_profile = init_user_and_profile()
 		opening_entry = create_opening_entry(pos_profile, test_user.name)
@@ -123,3 +155,19 @@
 	pos_profile.save()
 
 	return test_user, pos_profile
+
+
+def get_test_item_qty(pos_profile):
+	test_item_pos = get_items(
+		start=0,
+		page_length=5,
+		price_list="Standard Selling",
+		pos_profile=pos_profile.name,
+		search_term="_Test Item",
+		item_group="All Item Groups",
+	)
+
+	test_item_qty = [item for item in test_item_pos["items"] if item["item_code"] == "_Test Item"][
+		0
+	].get("actual_qty")
+	return test_item_qty
diff --git a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py
index 4b2fcec..89a9611 100644
--- a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py
+++ b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py
@@ -542,6 +542,7 @@
 		is_stock_item = True
 		bin_qty = get_bin_qty(item_code, warehouse)
 		pos_sales_qty = get_pos_reserved_qty(item_code, warehouse)
+
 		return bin_qty - pos_sales_qty, is_stock_item
 	else:
 		is_stock_item = True
@@ -595,7 +596,6 @@
 		.where(
 			(p_inv.name == p_item.parent)
 			& (IfNull(p_inv.consolidated_invoice, "") == "")
-			& (p_inv.is_return == 0)
 			& (p_item.docstatus == 1)
 			& (p_item.item_code == item_code)
 			& (p_item.warehouse == warehouse)
diff --git a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py
index d8cbcc1..b587ce6 100644
--- a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py
+++ b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py
@@ -95,7 +95,6 @@
 			sales_invoice = self.process_merging_into_sales_invoice(sales)
 
 		self.save()  # save consolidated_sales_invoice & consolidated_credit_note ref in merge log
-
 		self.update_pos_invoices(pos_invoice_docs, sales_invoice, credit_note)
 
 	def on_cancel(self):
@@ -108,7 +107,6 @@
 
 	def process_merging_into_sales_invoice(self, data):
 		sales_invoice = self.get_new_sales_invoice()
-
 		sales_invoice = self.merge_pos_invoice_into(sales_invoice, data)
 
 		sales_invoice.is_consolidated = 1
@@ -165,8 +163,7 @@
 				for i in items:
 					if (
 						i.item_code == item.item_code
-						and not i.serial_no
-						and not i.batch_no
+						and not i.serial_and_batch_bundle
 						and i.uom == item.uom
 						and i.net_rate == item.net_rate
 						and i.warehouse == item.warehouse
@@ -385,6 +382,7 @@
 		for d in invoices
 		if d.is_return and d.return_against
 	]
+
 	for pos_invoice in pos_return_docs:
 		for item in pos_invoice.items:
 			if not item.serial_no and not item.serial_and_batch_bundle:
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 63c0c45..f9cfe5a 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -3376,6 +3376,7 @@
 
 		set_advance_flag(company="_Test Company", flag=0, default_account="")
 
+	@change_settings("Selling Settings", {"allow_negative_rates_for_items": 0})
 	def test_sales_return_negative_rate(self):
 		si = create_sales_invoice(is_return=1, qty=-2, rate=-10, do_not_save=True)
 		self.assertRaises(frappe.ValidationError, si.save)
diff --git a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py
index e66a886..d17ca08 100644
--- a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py
+++ b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py
@@ -262,14 +262,20 @@
 		if tax_deducted:
 			net_total = inv.tax_withholding_net_total
 			if ldc:
-				tax_amount = get_tds_amount_from_ldc(ldc, parties, tax_details, posting_date, net_total)
+				limit_consumed = get_limit_consumed(ldc, parties)
+				if is_valid_certificate(ldc, posting_date, limit_consumed):
+					tax_amount = get_lower_deduction_amount(
+						net_total, limit_consumed, ldc.certificate_limit, ldc.rate, tax_details
+					)
+				else:
+					tax_amount = net_total * tax_details.rate / 100 if net_total > 0 else 0
 			else:
 				tax_amount = net_total * tax_details.rate / 100 if net_total > 0 else 0
 
 			# once tds is deducted, not need to add vouchers in the invoice
 			voucher_wise_amount = {}
 		else:
-			tax_amount = get_tds_amount(ldc, parties, inv, tax_details, tax_deducted, vouchers)
+			tax_amount = get_tds_amount(ldc, parties, inv, tax_details, vouchers)
 
 	elif party_type == "Customer":
 		if tax_deducted:
@@ -416,7 +422,7 @@
 	return sum(entries)
 
 
-def get_tds_amount(ldc, parties, inv, tax_details, tax_deducted, vouchers):
+def get_tds_amount(ldc, parties, inv, tax_details, vouchers):
 	tds_amount = 0
 	invoice_filters = {"name": ("in", vouchers), "docstatus": 1, "apply_tds": 1}
 
@@ -496,15 +502,10 @@
 			net_total += inv.tax_withholding_net_total
 			supp_credit_amt = net_total - cumulative_threshold
 
-		if ldc and is_valid_certificate(
-			ldc.valid_from,
-			ldc.valid_upto,
-			inv.get("posting_date") or inv.get("transaction_date"),
-			tax_deducted,
-			inv.tax_withholding_net_total,
-			ldc.certificate_limit,
-		):
-			tds_amount = get_ltds_amount(supp_credit_amt, 0, ldc.certificate_limit, ldc.rate, tax_details)
+		if ldc and is_valid_certificate(ldc, inv.get("posting_date") or inv.get("transaction_date"), 0):
+			tds_amount = get_lower_deduction_amount(
+				supp_credit_amt, 0, ldc.certificate_limit, ldc.rate, tax_details
+			)
 		else:
 			tds_amount = supp_credit_amt * tax_details.rate / 100 if supp_credit_amt > 0 else 0
 
@@ -582,8 +583,7 @@
 	return inv.grand_total - tcs_tax_row_amount
 
 
-def get_tds_amount_from_ldc(ldc, parties, tax_details, posting_date, net_total):
-	tds_amount = 0
+def get_limit_consumed(ldc, parties):
 	limit_consumed = frappe.db.get_value(
 		"Purchase Invoice",
 		{
@@ -597,37 +597,29 @@
 		"sum(tax_withholding_net_total)",
 	)
 
-	if is_valid_certificate(
-		ldc.valid_from, ldc.valid_upto, posting_date, limit_consumed, net_total, ldc.certificate_limit
-	):
-		tds_amount = get_ltds_amount(
-			net_total, limit_consumed, ldc.certificate_limit, ldc.rate, tax_details
-		)
-
-	return tds_amount
+	return limit_consumed
 
 
-def get_ltds_amount(current_amount, deducted_amount, certificate_limit, rate, tax_details):
-	if certificate_limit - flt(deducted_amount) - flt(current_amount) >= 0:
+def get_lower_deduction_amount(
+	current_amount, limit_consumed, certificate_limit, rate, tax_details
+):
+	if certificate_limit - flt(limit_consumed) - flt(current_amount) >= 0:
 		return current_amount * rate / 100
 	else:
-		ltds_amount = certificate_limit - flt(deducted_amount)
+		ltds_amount = certificate_limit - flt(limit_consumed)
 		tds_amount = current_amount - ltds_amount
 
 		return ltds_amount * rate / 100 + tds_amount * tax_details.rate / 100
 
 
-def is_valid_certificate(
-	valid_from, valid_upto, posting_date, deducted_amount, current_amount, certificate_limit
-):
-	valid = False
+def is_valid_certificate(ldc, posting_date, limit_consumed):
+	available_amount = flt(ldc.certificate_limit) - flt(limit_consumed)
+	if (
+		getdate(ldc.valid_from) <= getdate(posting_date) <= getdate(ldc.valid_upto)
+	) and available_amount > 0:
+		return True
 
-	available_amount = flt(certificate_limit) - flt(deducted_amount)
-
-	if (getdate(valid_from) <= getdate(posting_date) <= getdate(valid_upto)) and available_amount > 0:
-		valid = True
-
-	return valid
+	return False
 
 
 def normal_round(number):
diff --git a/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py b/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py
index 80220e4..0fbaf23 100644
--- a/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py
+++ b/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py
@@ -4,6 +4,7 @@
 import unittest
 
 import frappe
+from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
 from frappe.utils import today
 
 from erpnext.accounts.utils import get_fiscal_year
@@ -17,6 +18,7 @@
 		# create relevant supplier, etc
 		create_records()
 		create_tax_withholding_category_records()
+		make_pan_no_field()
 
 	def tearDown(self):
 		cancel_invoices()
@@ -451,6 +453,40 @@
 		pe2.cancel()
 		pe3.cancel()
 
+	def test_lower_deduction_certificate_application(self):
+		frappe.db.set_value(
+			"Supplier",
+			"Test LDC Supplier",
+			{
+				"tax_withholding_category": "Test Service Category",
+				"pan": "ABCTY1234D",
+			},
+		)
+
+		create_lower_deduction_certificate(
+			supplier="Test LDC Supplier",
+			certificate_no="1AE0423AAJ",
+			tax_withholding_category="Test Service Category",
+			tax_rate=2,
+			limit=50000,
+		)
+
+		pi1 = create_purchase_invoice(supplier="Test LDC Supplier", rate=35000)
+		pi1.submit()
+		self.assertEqual(pi1.taxes[0].tax_amount, 700)
+
+		pi2 = create_purchase_invoice(supplier="Test LDC Supplier", rate=35000)
+		pi2.submit()
+		self.assertEqual(pi2.taxes[0].tax_amount, 2300)
+
+		pi3 = create_purchase_invoice(supplier="Test LDC Supplier", rate=35000)
+		pi3.submit()
+		self.assertEqual(pi3.taxes[0].tax_amount, 3500)
+
+		pi1.cancel()
+		pi2.cancel()
+		pi3.cancel()
+
 
 def cancel_invoices():
 	purchase_invoices = frappe.get_all(
@@ -610,6 +646,7 @@
 		"Test TDS Supplier6",
 		"Test TDS Supplier7",
 		"Test TDS Supplier8",
+		"Test LDC Supplier",
 	]:
 		if frappe.db.exists("Supplier", name):
 			continue
@@ -806,3 +843,39 @@
 				"accounts": [{"company": "_Test Company", "account": account}],
 			}
 		).insert()
+
+
+def create_lower_deduction_certificate(
+	supplier, tax_withholding_category, tax_rate, certificate_no, limit
+):
+	fiscal_year = get_fiscal_year(today(), company="_Test Company")
+	if not frappe.db.exists("Lower Deduction Certificate", certificate_no):
+		frappe.get_doc(
+			{
+				"doctype": "Lower Deduction Certificate",
+				"company": "_Test Company",
+				"supplier": supplier,
+				"certificate_no": certificate_no,
+				"tax_withholding_category": tax_withholding_category,
+				"fiscal_year": fiscal_year[0],
+				"valid_from": fiscal_year[1],
+				"valid_upto": fiscal_year[2],
+				"rate": tax_rate,
+				"certificate_limit": limit,
+			}
+		).insert()
+
+
+def make_pan_no_field():
+	pan_field = {
+		"Supplier": [
+			{
+				"fieldname": "pan",
+				"label": "PAN",
+				"fieldtype": "Data",
+				"translatable": 0,
+			}
+		]
+	}
+
+	create_custom_fields(pan_field, update=1)
diff --git a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.js b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.js
index b66a555..8808165 100644
--- a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.js
+++ b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.js
@@ -33,7 +33,14 @@
 					frappe.throw(__("Please select Party Type first"));
 				}
 				return party_type;
-			}
+			},
+			"get_query": function() {
+				return {
+					"filters": {
+						"tax_withholding_category": ["!=",""],
+					}
+				}
+			},
 		},
 		{
 			"fieldname":"from_date",
diff --git a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py
index ddd049a..7d16661 100644
--- a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py
+++ b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py
@@ -7,19 +7,26 @@
 
 
 def execute(filters=None):
+	if filters.get("party_type") == "Customer":
+		party_naming_by = frappe.db.get_single_value("Selling Settings", "cust_master_name")
+	else:
+		party_naming_by = frappe.db.get_single_value("Buying Settings", "supp_master_name")
+
+	filters.update({"naming_series": party_naming_by})
+
 	validate_filters(filters)
 	(
 		tds_docs,
 		tds_accounts,
 		tax_category_map,
 		journal_entry_party_map,
-		invoice_net_total_map,
+		net_total_map,
 	) = get_tds_docs(filters)
 
 	columns = get_columns(filters)
 
 	res = get_result(
-		filters, tds_docs, tds_accounts, tax_category_map, journal_entry_party_map, invoice_net_total_map
+		filters, tds_docs, tds_accounts, tax_category_map, journal_entry_party_map, net_total_map
 	)
 	return columns, res
 
@@ -31,7 +38,7 @@
 
 
 def get_result(
-	filters, tds_docs, tds_accounts, tax_category_map, journal_entry_party_map, invoice_net_total_map
+	filters, tds_docs, tds_accounts, tax_category_map, journal_entry_party_map, net_total_map
 ):
 	party_map = get_party_pan_map(filters.get("party_type"))
 	tax_rate_map = get_tax_rate_map(filters)
@@ -39,7 +46,7 @@
 
 	out = []
 	for name, details in gle_map.items():
-		tax_amount, total_amount = 0, 0
+		tax_amount, total_amount, grand_total, base_total = 0, 0, 0, 0
 		tax_withholding_category = tax_category_map.get(name)
 		rate = tax_rate_map.get(tax_withholding_category)
 
@@ -60,8 +67,8 @@
 			if entry.account in tds_accounts:
 				tax_amount += entry.credit - entry.debit
 
-			if invoice_net_total_map.get(name):
-				total_amount = invoice_net_total_map.get(name)
+			if net_total_map.get(name):
+				total_amount, grand_total, base_total = net_total_map.get(name)
 			else:
 				total_amount += entry.credit
 
@@ -69,15 +76,13 @@
 			if party_map.get(party, {}).get("party_type") == "Supplier":
 				party_name = "supplier_name"
 				party_type = "supplier_type"
-				table_name = "Supplier"
 			else:
 				party_name = "customer_name"
 				party_type = "customer_type"
-				table_name = "Customer"
 
 			row = {
 				"pan"
-				if frappe.db.has_column(table_name, "pan")
+				if frappe.db.has_column(filters.party_type, "pan")
 				else "tax_id": party_map.get(party, {}).get("pan"),
 				"party": party_map.get(party, {}).get("name"),
 			}
@@ -91,6 +96,8 @@
 					"entity_type": party_map.get(party, {}).get(party_type),
 					"rate": rate,
 					"total_amount": total_amount,
+					"grand_total": grand_total,
+					"base_total": base_total,
 					"tax_amount": tax_amount,
 					"transaction_date": posting_date,
 					"transaction_type": voucher_type,
@@ -144,9 +151,9 @@
 
 
 def get_columns(filters):
-	pan = "pan" if frappe.db.has_column("Supplier", "pan") else "tax_id"
+	pan = "pan" if frappe.db.has_column(filters.party_type, "pan") else "tax_id"
 	columns = [
-		{"label": _(frappe.unscrub(pan)), "fieldname": pan, "fieldtype": "Data", "width": 90},
+		{"label": _(frappe.unscrub(pan)), "fieldname": pan, "fieldtype": "Data", "width": 60},
 		{
 			"label": _(filters.get("party_type")),
 			"fieldname": "party",
@@ -158,25 +165,30 @@
 
 	if filters.naming_series == "Naming Series":
 		columns.append(
-			{"label": _("Party Name"), "fieldname": "party_name", "fieldtype": "Data", "width": 180}
+			{
+				"label": _(filters.party_type + " Name"),
+				"fieldname": "party_name",
+				"fieldtype": "Data",
+				"width": 180,
+			}
 		)
 
 	columns.extend(
 		[
 			{
+				"label": _("Date of Transaction"),
+				"fieldname": "transaction_date",
+				"fieldtype": "Date",
+				"width": 100,
+			},
+			{
 				"label": _("Section Code"),
 				"options": "Tax Withholding Category",
 				"fieldname": "section_code",
 				"fieldtype": "Link",
-				"width": 180,
-			},
-			{"label": _("Entity Type"), "fieldname": "entity_type", "fieldtype": "Data", "width": 120},
-			{
-				"label": _("TDS Rate %") if filters.get("party_type") == "Supplier" else _("TCS Rate %"),
-				"fieldname": "rate",
-				"fieldtype": "Percent",
 				"width": 90,
 			},
+			{"label": _("Entity Type"), "fieldname": "entity_type", "fieldtype": "Data", "width": 100},
 			{
 				"label": _("Total Amount"),
 				"fieldname": "total_amount",
@@ -184,15 +196,27 @@
 				"width": 90,
 			},
 			{
-				"label": _("TDS Amount") if filters.get("party_type") == "Supplier" else _("TCS Amount"),
+				"label": _("TDS Rate %") if filters.get("party_type") == "Supplier" else _("TCS Rate %"),
+				"fieldname": "rate",
+				"fieldtype": "Percent",
+				"width": 90,
+			},
+			{
+				"label": _("Tax Amount"),
 				"fieldname": "tax_amount",
 				"fieldtype": "Float",
 				"width": 90,
 			},
 			{
-				"label": _("Date of Transaction"),
-				"fieldname": "transaction_date",
-				"fieldtype": "Date",
+				"label": _("Grand Total"),
+				"fieldname": "grand_total",
+				"fieldtype": "Float",
+				"width": 90,
+			},
+			{
+				"label": _("Base Total"),
+				"fieldname": "base_total",
+				"fieldtype": "Float",
 				"width": 90,
 			},
 			{"label": _("Transaction Type"), "fieldname": "transaction_type", "width": 100},
@@ -216,7 +240,7 @@
 	payment_entries = []
 	journal_entries = []
 	tax_category_map = frappe._dict()
-	invoice_net_total_map = frappe._dict()
+	net_total_map = frappe._dict()
 	or_filters = frappe._dict()
 	journal_entry_party_map = frappe._dict()
 	bank_accounts = frappe.get_all("Account", {"is_group": 0, "account_type": "Bank"}, pluck="name")
@@ -260,13 +284,13 @@
 		tds_documents.append(d.voucher_no)
 
 	if purchase_invoices:
-		get_doc_info(purchase_invoices, "Purchase Invoice", tax_category_map, invoice_net_total_map)
+		get_doc_info(purchase_invoices, "Purchase Invoice", tax_category_map, net_total_map)
 
 	if sales_invoices:
-		get_doc_info(sales_invoices, "Sales Invoice", tax_category_map, invoice_net_total_map)
+		get_doc_info(sales_invoices, "Sales Invoice", tax_category_map, net_total_map)
 
 	if payment_entries:
-		get_doc_info(payment_entries, "Payment Entry", tax_category_map)
+		get_doc_info(payment_entries, "Payment Entry", tax_category_map, net_total_map)
 
 	if journal_entries:
 		journal_entry_party_map = get_journal_entry_party_map(journal_entries)
@@ -277,7 +301,7 @@
 		tds_accounts,
 		tax_category_map,
 		journal_entry_party_map,
-		invoice_net_total_map,
+		net_total_map,
 	)
 
 
@@ -295,11 +319,25 @@
 	return journal_entry_party_map
 
 
-def get_doc_info(vouchers, doctype, tax_category_map, invoice_net_total_map=None):
+def get_doc_info(vouchers, doctype, tax_category_map, net_total_map=None):
 	if doctype == "Purchase Invoice":
-		fields = ["name", "tax_withholding_category", "base_tax_withholding_net_total"]
-	if doctype == "Sales Invoice":
-		fields = ["name", "base_net_total"]
+		fields = [
+			"name",
+			"tax_withholding_category",
+			"base_tax_withholding_net_total",
+			"grand_total",
+			"base_total",
+		]
+	elif doctype == "Sales Invoice":
+		fields = ["name", "base_net_total", "grand_total", "base_total"]
+	elif doctype == "Payment Entry":
+		fields = [
+			"name",
+			"tax_withholding_category",
+			"paid_amount",
+			"paid_amount_after_tax",
+			"base_paid_amount",
+		]
 	else:
 		fields = ["name", "tax_withholding_category"]
 
@@ -308,9 +346,15 @@
 	for entry in entries:
 		tax_category_map.update({entry.name: entry.tax_withholding_category})
 		if doctype == "Purchase Invoice":
-			invoice_net_total_map.update({entry.name: entry.base_tax_withholding_net_total})
-		if doctype == "Sales Invoice":
-			invoice_net_total_map.update({entry.name: entry.base_net_total})
+			net_total_map.update(
+				{entry.name: [entry.base_tax_withholding_net_total, entry.grand_total, entry.base_total]}
+			)
+		elif doctype == "Sales Invoice":
+			net_total_map.update({entry.name: [entry.base_net_total, entry.grand_total, entry.base_total]})
+		elif doctype == "Payment Entry":
+			net_total_map.update(
+				{entry.name: [entry.paid_amount, entry.paid_amount_after_tax, entry.base_paid_amount]}
+			)
 
 
 def get_tax_rate_map(filters):
diff --git a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.js b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.js
index d334846..a0be1b5 100644
--- a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.js
+++ b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.js
@@ -12,17 +12,35 @@
 			"default": frappe.defaults.get_default('company')
 		},
 		{
-			"fieldname":"supplier",
-			"label": __("Supplier"),
-			"fieldtype": "Link",
-			"options": "Supplier",
+			"fieldname":"party_type",
+			"label": __("Party Type"),
+			"fieldtype": "Select",
+			"options": ["Supplier", "Customer"],
+			"reqd": 1,
+			"default": "Supplier",
+			"on_change": function(){
+				frappe.query_report.set_filter_value("party", "");
+			}
+		},
+		{
+			"fieldname":"party",
+			"label": __("Party"),
+			"fieldtype": "Dynamic Link",
+			"get_options": function() {
+				var party_type = frappe.query_report.get_filter_value('party_type');
+				var party = frappe.query_report.get_filter_value('party');
+				if(party && !party_type) {
+					frappe.throw(__("Please select Party Type first"));
+				}
+				return party_type;
+			},
 			"get_query": function() {
 				return {
 					"filters": {
 						"tax_withholding_category": ["!=",""],
 					}
 				}
-			}
+			},
 		},
 		{
 			"fieldname":"from_date",
diff --git a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py
index c6aa21c..82f97f1 100644
--- a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py
+++ b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py
@@ -9,9 +9,14 @@
 
 
 def execute(filters=None):
-	validate_filters(filters)
+	if filters.get("party_type") == "Customer":
+		party_naming_by = frappe.db.get_single_value("Selling Settings", "cust_master_name")
+	else:
+		party_naming_by = frappe.db.get_single_value("Buying Settings", "supp_master_name")
 
-	filters.naming_series = frappe.db.get_single_value("Buying Settings", "supp_master_name")
+	filters.update({"naming_series": party_naming_by})
+
+	validate_filters(filters)
 
 	columns = get_columns(filters)
 	(
@@ -25,7 +30,7 @@
 	res = get_result(
 		filters, tds_docs, tds_accounts, tax_category_map, journal_entry_party_map, invoice_total_map
 	)
-	final_result = group_by_supplier_and_category(res)
+	final_result = group_by_party_and_category(res, filters)
 
 	return columns, final_result
 
@@ -43,60 +48,67 @@
 	filters["fiscal_year"] = from_year
 
 
-def group_by_supplier_and_category(data):
-	supplier_category_wise_map = {}
+def group_by_party_and_category(data, filters):
+	party_category_wise_map = {}
 
 	for row in data:
-		supplier_category_wise_map.setdefault(
-			(row.get("supplier"), row.get("section_code")),
+		party_category_wise_map.setdefault(
+			(row.get("party"), row.get("section_code")),
 			{
 				"pan": row.get("pan"),
-				"supplier": row.get("supplier"),
-				"supplier_name": row.get("supplier_name"),
+				"tax_id": row.get("tax_id"),
+				"party": row.get("party"),
+				"party_name": row.get("party_name"),
 				"section_code": row.get("section_code"),
 				"entity_type": row.get("entity_type"),
-				"tds_rate": row.get("tds_rate"),
-				"total_amount_credited": 0.0,
-				"tds_deducted": 0.0,
+				"rate": row.get("rate"),
+				"total_amount": 0.0,
+				"tax_amount": 0.0,
 			},
 		)
 
-		supplier_category_wise_map.get((row.get("supplier"), row.get("section_code")))[
-			"total_amount_credited"
-		] += row.get("total_amount_credited", 0.0)
+		party_category_wise_map.get((row.get("party"), row.get("section_code")))[
+			"total_amount"
+		] += row.get("total_amount", 0.0)
 
-		supplier_category_wise_map.get((row.get("supplier"), row.get("section_code")))[
-			"tds_deducted"
-		] += row.get("tds_deducted", 0.0)
+		party_category_wise_map.get((row.get("party"), row.get("section_code")))[
+			"tax_amount"
+		] += row.get("tax_amount", 0.0)
 
-	final_result = get_final_result(supplier_category_wise_map)
+	final_result = get_final_result(party_category_wise_map)
 
 	return final_result
 
 
-def get_final_result(supplier_category_wise_map):
+def get_final_result(party_category_wise_map):
 	out = []
-	for key, value in supplier_category_wise_map.items():
+	for key, value in party_category_wise_map.items():
 		out.append(value)
 
 	return out
 
 
 def get_columns(filters):
+	pan = "pan" if frappe.db.has_column(filters.party_type, "pan") else "tax_id"
 	columns = [
-		{"label": _("PAN"), "fieldname": "pan", "fieldtype": "Data", "width": 90},
+		{"label": _(frappe.unscrub(pan)), "fieldname": pan, "fieldtype": "Data", "width": 90},
 		{
-			"label": _("Supplier"),
-			"options": "Supplier",
-			"fieldname": "supplier",
-			"fieldtype": "Link",
+			"label": _(filters.get("party_type")),
+			"fieldname": "party",
+			"fieldtype": "Dynamic Link",
+			"options": "party_type",
 			"width": 180,
 		},
 	]
 
 	if filters.naming_series == "Naming Series":
 		columns.append(
-			{"label": _("Supplier Name"), "fieldname": "supplier_name", "fieldtype": "Data", "width": 180}
+			{
+				"label": _(filters.party_type + " Name"),
+				"fieldname": "party_name",
+				"fieldtype": "Data",
+				"width": 180,
+			}
 		)
 
 	columns.extend(
@@ -109,18 +121,23 @@
 				"width": 180,
 			},
 			{"label": _("Entity Type"), "fieldname": "entity_type", "fieldtype": "Data", "width": 180},
-			{"label": _("TDS Rate %"), "fieldname": "tds_rate", "fieldtype": "Percent", "width": 90},
 			{
-				"label": _("Total Amount Credited"),
-				"fieldname": "total_amount_credited",
-				"fieldtype": "Float",
-				"width": 90,
+				"label": _("TDS Rate %") if filters.get("party_type") == "Supplier" else _("TCS Rate %"),
+				"fieldname": "rate",
+				"fieldtype": "Percent",
+				"width": 120,
 			},
 			{
-				"label": _("Amount of TDS Deducted"),
-				"fieldname": "tds_deducted",
+				"label": _("Total Amount"),
+				"fieldname": "total_amount",
 				"fieldtype": "Float",
-				"width": 90,
+				"width": 120,
+			},
+			{
+				"label": _("Tax Amount"),
+				"fieldname": "tax_amount",
+				"fieldtype": "Float",
+				"width": 120,
 			},
 		]
 	)
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index c24442e..bccf6f1 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -908,6 +908,7 @@
 	min_outstanding=None,
 	max_outstanding=None,
 	accounting_dimensions=None,
+	vouchers=None,
 ):
 
 	ple = qb.DocType("Payment Ledger Entry")
@@ -933,6 +934,7 @@
 
 	ple_query = QueryPaymentLedger()
 	invoice_list = ple_query.get_voucher_outstandings(
+		vouchers=vouchers,
 		common_filter=common_filter,
 		posting_date=posting_date,
 		min_outstanding=min_outstanding,
diff --git a/erpnext/assets/doctype/asset_category/asset_category.js b/erpnext/assets/doctype/asset_category/asset_category.js
index c702687..7dde14e 100644
--- a/erpnext/assets/doctype/asset_category/asset_category.js
+++ b/erpnext/assets/doctype/asset_category/asset_category.js
@@ -33,6 +33,7 @@
 			var d  = locals[cdt][cdn];
 			return {
 				"filters": {
+					"account_type": "Depreciation",
 					"root_type": ["in", ["Expense", "Income"]],
 					"is_group": 0,
 					"company": d.company_name
diff --git a/erpnext/assets/doctype/asset_category/asset_category.py b/erpnext/assets/doctype/asset_category/asset_category.py
index 2e1def9..8d35141 100644
--- a/erpnext/assets/doctype/asset_category/asset_category.py
+++ b/erpnext/assets/doctype/asset_category/asset_category.py
@@ -53,7 +53,7 @@
 		account_type_map = {
 			"fixed_asset_account": {"account_type": ["Fixed Asset"]},
 			"accumulated_depreciation_account": {"account_type": ["Accumulated Depreciation"]},
-			"depreciation_expense_account": {"root_type": ["Expense", "Income"]},
+			"depreciation_expense_account": {"account_type": ["Depreciation"]},
 			"capital_work_in_progress_account": {"account_type": ["Capital Work in Progress"]},
 		}
 		for d in self.accounts:
diff --git a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py
index e938577..56840c1 100644
--- a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py
+++ b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py
@@ -193,7 +193,7 @@
 				"supplier": data.get("supplier"),
 				"supplier_name": data.get("supplier_name"),
 				"update_password_link": f'<a href="{update_password_link}" class="btn btn-default btn-xs" target="_blank">{_("Set Password")}</a>',
-				"portal_link": f'<a href="{rfq_link}" class="btn btn-default btn-sm" target="_blank"> {_("Submit your Quotation")} </a>',
+				"portal_link": f'<a href="{rfq_link}" class="btn btn-default btn-xs" target="_blank"> {_("Submit your Quotation")} </a>',
 				"user_fullname": full_name,
 			}
 		)
diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py
index f3663cc..73a248f 100644
--- a/erpnext/controllers/status_updater.py
+++ b/erpnext/controllers/status_updater.py
@@ -5,7 +5,7 @@
 import frappe
 from frappe import _
 from frappe.model.document import Document
-from frappe.utils import comma_or, flt, getdate, now, nowdate
+from frappe.utils import comma_or, flt, get_link_to_form, getdate, now, nowdate
 
 
 class OverAllowanceError(frappe.ValidationError):
@@ -233,8 +233,17 @@
 				if hasattr(d, "qty") and d.qty > 0 and self.get("is_return"):
 					frappe.throw(_("For an item {0}, quantity must be negative number").format(d.item_code))
 
-				if hasattr(d, "item_code") and hasattr(d, "rate") and flt(d.rate) < 0:
-					frappe.throw(_("For an item {0}, rate must be a positive number").format(d.item_code))
+				if not frappe.db.get_single_value("Selling Settings", "allow_negative_rates_for_items"):
+					if hasattr(d, "item_code") and hasattr(d, "rate") and flt(d.rate) < 0:
+						frappe.throw(
+							_(
+								"For item {0}, rate must be a positive number. To Allow negative rates, enable {1} in {2}"
+							).format(
+								frappe.bold(d.item_code),
+								frappe.bold(_("`Allow Negative rates for Items`")),
+								get_link_to_form("Selling Settings", "Selling Settings"),
+							),
+						)
 
 				if d.doctype == args["source_dt"] and d.get(args["join_field"]):
 					args["name"] = d.get(args["join_field"])
diff --git a/erpnext/e_commerce/web_template/hero_slider/hero_slider.html b/erpnext/e_commerce/web_template/hero_slider/hero_slider.html
index e560f4a..fe4fee3 100644
--- a/erpnext/e_commerce/web_template/hero_slider/hero_slider.html
+++ b/erpnext/e_commerce/web_template/hero_slider/hero_slider.html
@@ -1,7 +1,7 @@
 {%- macro slide(image, title, subtitle, action, label, index, align="Left", theme="Dark") -%}
 {%- set align_class = resolve_class({
 	'text-right': align == 'Right',
-	'text-centre': align == 'Centre',
+	'text-center': align == 'Centre',
 	'text-left': align == 'Left',
 }) -%}
 
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index d035ad6..a25c7c2 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -322,8 +322,6 @@
 execute:frappe.db.set_single_value("Accounts Settings", "merge_similar_account_heads", 0)
 erpnext.patches.v14_0.update_reference_type_in_journal_entry_accounts
 erpnext.patches.v14_0.update_subscription_details
-# below migration patches should always run last
-erpnext.patches.v14_0.migrate_gl_to_payment_ledger
 execute:frappe.delete_doc_if_exists("Report", "Tax Detail")
 erpnext.patches.v15_0.enable_all_leads
 erpnext.patches.v14_0.update_company_in_ldc
@@ -340,3 +338,6 @@
 execute:frappe.defaults.clear_default("fiscal_year")
 erpnext.patches.v15_0.remove_exotel_integration
 erpnext.patches.v14_0.single_to_multi_dunning
+execute:frappe.db.set_single_value('Selling Settings', 'allow_negative_rates_for_items', 0)
+# below migration patch should always run last
+erpnext.patches.v14_0.migrate_gl_to_payment_ledger
diff --git a/erpnext/projects/report/billing_summary.py b/erpnext/projects/report/billing_summary.py
index bc8f2af..ac1524a 100644
--- a/erpnext/projects/report/billing_summary.py
+++ b/erpnext/projects/report/billing_summary.py
@@ -98,9 +98,11 @@
 	record_filters = [
 		["start_date", "<=", filters.to_date],
 		["end_date", ">=", filters.from_date],
-		["docstatus", "=", 1],
 	]
-
+	if not filters.get("include_draft_timesheets"):
+		record_filters.append(["docstatus", "=", 1])
+	else:
+		record_filters.append(["docstatus", "!=", 2])
 	if "employee" in filters:
 		record_filters.append(["employee", "=", filters.employee])
 
diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js
index 8566b1f..2c25465 100644
--- a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js
+++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js
@@ -25,5 +25,10 @@
 			default: frappe.datetime.add_days(frappe.datetime.month_start(), -1),
 			reqd: 1
 		},
+		{
+			fieldname:"include_draft_timesheets",
+			label: __("Include Timesheets in Draft Status"),
+			fieldtype: "Check",
+		},
 	]
 }
diff --git a/erpnext/projects/report/project_billing_summary/project_billing_summary.js b/erpnext/projects/report/project_billing_summary/project_billing_summary.js
index 0242036..fce0c68 100644
--- a/erpnext/projects/report/project_billing_summary/project_billing_summary.js
+++ b/erpnext/projects/report/project_billing_summary/project_billing_summary.js
@@ -25,5 +25,10 @@
 			default: frappe.datetime.add_days(frappe.datetime.month_start(),-1),
 			reqd: 1
 		},
+		{
+			fieldname:"include_draft_timesheets",
+			label: __("Include Timesheets in Draft Status"),
+			fieldtype: "Check",
+		},
 	]
 }
diff --git a/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js b/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js
index cbb64ca..52fa8ab 100644
--- a/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js
+++ b/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js
@@ -104,6 +104,9 @@
 				name: __("Document Name"),
 				editable: false,
 				width: 1,
+				format: (value, row) => {
+					return frappe.form.formatters.Link(value, {options: row[2].content});
+				},
 			},
 			{
 				name: __("Reference Date"),
@@ -132,7 +135,7 @@
 	format_row(row) {
 		return [
 			row[1], // Document Type
-			frappe.form.formatters.Link(row[2], {options: row[1]}), // Document Name
+			row[2], // Document Name
 			row[5] || row[8], // Reference Date
 			format_currency(row[3], row[9]), // Remaining
 			row[4], // Reference Number
diff --git a/erpnext/public/js/controllers/stock_controller.js b/erpnext/public/js/controllers/stock_controller.js
index 720423b..e9c409e 100644
--- a/erpnext/public/js/controllers/stock_controller.js
+++ b/erpnext/public/js/controllers/stock_controller.js
@@ -57,7 +57,8 @@
 					from_date: me.frm.doc.posting_date,
 					to_date: moment(me.frm.doc.modified).format('YYYY-MM-DD'),
 					company: me.frm.doc.company,
-					show_cancelled_entries: me.frm.doc.docstatus === 2
+					show_cancelled_entries: me.frm.doc.docstatus === 2,
+					ignore_prepared_report: true
 				};
 				frappe.set_route("query-report", "Stock Ledger");
 			}, __("View"));
@@ -75,7 +76,8 @@
 					to_date: moment(me.frm.doc.modified).format('YYYY-MM-DD'),
 					company: me.frm.doc.company,
 					group_by: "Group by Voucher (Consolidated)",
-					show_cancelled_entries: me.frm.doc.docstatus === 2
+					show_cancelled_entries: me.frm.doc.docstatus === 2,
+					ignore_prepared_report: true
 				};
 				frappe.set_route("query-report", "General Ledger");
 			}, __("View"));
diff --git a/erpnext/public/js/utils/demo.js b/erpnext/public/js/utils/demo.js
index b59c476..3ebc5ef 100644
--- a/erpnext/public/js/utils/demo.js
+++ b/erpnext/public/js/utils/demo.js
@@ -11,7 +11,7 @@
 
 function render_clear_demo_button() {
 	let wait_for_onboaring_tours = setInterval(() => {
-		if ($("#driver-page-overlay").length) {
+		if ($("#driver-page-overlay").length || $("#show-dialog").length) {
 			return;
 		}
 		setup_clear_demo_button();
diff --git a/erpnext/selling/doctype/selling_settings/selling_settings.json b/erpnext/selling/doctype/selling_settings/selling_settings.json
index f3b9f6f..6855012 100644
--- a/erpnext/selling/doctype/selling_settings/selling_settings.json
+++ b/erpnext/selling/doctype/selling_settings/selling_settings.json
@@ -20,6 +20,7 @@
   "editable_price_list_rate",
   "validate_selling_price",
   "editable_bundle_item_rates",
+  "allow_negative_rates_for_items",
   "sales_transactions_settings_section",
   "so_required",
   "dn_required",
@@ -193,6 +194,12 @@
    "fieldname": "dont_reserve_sales_order_qty_on_sales_return",
    "fieldtype": "Check",
    "label": "Don't Reserve Sales Order Qty on Sales Return"
+  },
+  {
+   "default": "0",
+   "fieldname": "allow_negative_rates_for_items",
+   "fieldtype": "Check",
+   "label": "Allow Negative rates for Items"
   }
  ],
  "icon": "fa fa-cog",
@@ -200,7 +207,7 @@
  "index_web_pages_for_search": 1,
  "issingle": 1,
  "links": [],
- "modified": "2023-08-09 15:35:42.914354",
+ "modified": "2023-08-14 20:33:05.693667",
  "modified_by": "Administrator",
  "module": "Selling",
  "name": "Selling Settings",
diff --git a/erpnext/selling/page/point_of_sale/pos_controller.js b/erpnext/selling/page/point_of_sale/pos_controller.js
index 720d142..db6255a 100644
--- a/erpnext/selling/page/point_of_sale/pos_controller.js
+++ b/erpnext/selling/page/point_of_sale/pos_controller.js
@@ -225,6 +225,7 @@
 		voucher.pos_opening_entry = this.pos_opening;
 		voucher.period_end_date = frappe.datetime.now_datetime();
 		voucher.posting_date = frappe.datetime.now_date();
+		voucher.posting_time = frappe.datetime.now_time();
 		frappe.set_route('Form', 'POS Closing Entry', voucher.name);
 	}
 
diff --git a/erpnext/setup/demo.py b/erpnext/setup/demo.py
index 1c19974..926283f 100644
--- a/erpnext/setup/demo.py
+++ b/erpnext/setup/demo.py
@@ -114,7 +114,7 @@
 	if document_type == "Purchase Order":
 		posting_date = get_random_date(start_date, 1, 30)
 	else:
-		posting_date = get_random_date(start_date, 31, 365)
+		posting_date = get_random_date(start_date, 31, 364)
 
 	doctype.update(
 		{
@@ -180,8 +180,16 @@
 def clear_demo_record(document):
 	document_type = document.get("doctype")
 	del document["doctype"]
-	doc = frappe.get_doc(document_type, document)
-	frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True)
+
+	valid_columns = frappe.get_meta(document_type).get_valid_columns()
+
+	filters = document
+	for key in list(filters):
+		if key not in valid_columns:
+			filters.pop(key, None)
+
+	doc = frappe.get_doc(document_type, filters)
+	doc.delete(ignore_permissions=True)
 
 
 def delete_company(company):
diff --git a/erpnext/setup/demo_data/item_group.json b/erpnext/setup/demo_data/item_group.json
index acb261f..f96944d 100644
--- a/erpnext/setup/demo_data/item_group.json
+++ b/erpnext/setup/demo_data/item_group.json
@@ -1,7 +1,6 @@
 [
     {
         "doctype": "Item Group",
-        "item_group_name": "Demo Item Group",
-        "parent_item_group": "All Item Groups"
+        "item_group_name": "Demo Item Group"
     }
-]
\ No newline at end of file
+]
diff --git a/erpnext/setup/demo_data/purchase_order.json b/erpnext/setup/demo_data/purchase_order.json
index 42ffa88..318a865 100644
--- a/erpnext/setup/demo_data/purchase_order.json
+++ b/erpnext/setup/demo_data/purchase_order.json
@@ -4,6 +4,7 @@
         "supplier": "Zuckerman Security Ltd.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -20,6 +21,7 @@
         "supplier": "MA Inc.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -36,6 +38,7 @@
         "supplier": "Summit Traders Ltd.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -52,6 +55,7 @@
         "supplier": "Zuckerman Security Ltd.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -68,6 +72,7 @@
         "supplier": "MA Inc.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -84,6 +89,7 @@
         "supplier": "Summit Traders Ltd.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -100,6 +106,7 @@
         "supplier": "Zuckerman Security Ltd.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -116,6 +123,7 @@
         "supplier": "MA Inc.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -132,6 +140,7 @@
         "supplier": "Summit Traders Ltd.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
@@ -148,6 +157,7 @@
         "supplier": "Zuckerman Security Ltd.",
         "doctype": "Purchase Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Purchase Order Item",
diff --git a/erpnext/setup/demo_data/sales_order.json b/erpnext/setup/demo_data/sales_order.json
index d390637..29bffc3 100644
--- a/erpnext/setup/demo_data/sales_order.json
+++ b/erpnext/setup/demo_data/sales_order.json
@@ -4,6 +4,7 @@
         "customer": "Grant Plastics Ltd.",
         "doctype": "Sales Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Sales Order Item",
@@ -20,6 +21,7 @@
         "customer": "West View Software Ltd.",
         "doctype": "Sales Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Sales Order Item",
@@ -44,6 +46,7 @@
         "customer": "West View Software Ltd.",
         "doctype": "Sales Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Sales Order Item",
@@ -76,6 +79,7 @@
         "customer": "Palmer Productions Ltd.",
         "doctype": "Sales Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Sales Order Item",
@@ -92,6 +96,7 @@
         "customer": "Grant Plastics Ltd.",
         "doctype": "Sales Order",
         "update_stock": 1,
+        "disable_rounded_total": 1,
         "items": [
             {
                 "doctype": "Sales Order Item",
diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py
index 43bd7ac..1f90c5b 100644
--- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py
+++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py
@@ -3,7 +3,7 @@
 
 import collections
 import csv
-from collections import defaultdict
+from collections import Counter, defaultdict
 from typing import Dict, List
 
 import frappe
@@ -1197,6 +1197,7 @@
 		filters=[
 			["POS Invoice", "consolidated_invoice", "is", "not set"],
 			["POS Invoice", "docstatus", "=", 1],
+			["POS Invoice", "is_return", "=", 0],
 			["POS Invoice Item", "item_code", "=", kwargs.item_code],
 			["POS Invoice", "name", "!=", kwargs.ignore_voucher_no],
 		],
@@ -1214,7 +1215,6 @@
 	for d in get_serial_batch_ledgers(kwargs.item_code, docstatus=1, name=ids):
 		ignore_serial_nos.append(d.serial_no)
 
-	# Will be deprecated in v16
 	returned_serial_nos = []
 	for pos_invoice in pos_invoices:
 		if pos_invoice.serial_no:
@@ -1242,8 +1242,13 @@
 				child_doc, parent_doc, ignore_voucher_detail_no=kwargs.get("ignore_voucher_detail_no")
 			)
 		)
+	# Counter is used to create a hashmap of serial nos, which contains count of each serial no
+	# so we subtract returned serial nos from ignore serial nos after creating a counter of each to get the items which we need 	to ignore(which are sold)
 
-	return list(set(ignore_serial_nos) - set(returned_serial_nos))
+	ignore_serial_nos_counter = Counter(ignore_serial_nos)
+	returned_serial_nos_counter = Counter(returned_serial_nos)
+
+	return list(ignore_serial_nos_counter - returned_serial_nos_counter)
 
 
 def get_reserved_batches_for_pos(kwargs):
diff --git a/erpnext/templates/includes/order/order_taxes.html b/erpnext/templates/includes/order/order_taxes.html
index 0060ab3..d7b9620 100644
--- a/erpnext/templates/includes/order/order_taxes.html
+++ b/erpnext/templates/includes/order/order_taxes.html
@@ -19,7 +19,7 @@
 					{{ d.description }}
 				</div>
 				<div class="item-grand-total col-4 text-right pr-0">
-					{{ doc.get_formatted("net_total") }}
+					{{ d.get_formatted("base_tax_amount") }}
 				</div>
 			</div>
 		</div>