Merge pull request #28162 from deepeshgarg007/pan_field_missing_error

fix: Error for missing PAN no field
diff --git a/erpnext/accounts/doctype/account/chart_of_accounts/chart_of_accounts.py b/erpnext/accounts/doctype/account/chart_of_accounts/chart_of_accounts.py
index 05caafe..3596c34 100644
--- a/erpnext/accounts/doctype/account/chart_of_accounts/chart_of_accounts.py
+++ b/erpnext/accounts/doctype/account/chart_of_accounts/chart_of_accounts.py
@@ -81,7 +81,7 @@
 def identify_is_group(child):
 	if child.get("is_group"):
 		is_group = child.get("is_group")
-	elif len(set(child.keys()) - set(["account_type", "root_type", "is_group", "tax_rate", "account_number"])):
+	elif len(set(child.keys()) - set(["account_name", "account_type", "root_type", "is_group", "tax_rate", "account_number"])):
 		is_group = 1
 	else:
 		is_group = 0
diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
index 412833b..ad5a840 100644
--- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
+++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
@@ -4,9 +4,14 @@
 frappe.provide("erpnext.accounts");
 erpnext.accounts.PaymentReconciliationController = class PaymentReconciliationController extends frappe.ui.form.Controller {
 	onload() {
-		var me = this;
+		const default_company = frappe.defaults.get_default('company');
+		this.frm.set_value('company', default_company);
 
-		this.frm.set_query("party_type", function() {
+		this.frm.set_value('party_type', '');
+		this.frm.set_value('party', '');
+		this.frm.set_value('receivable_payable_account', '');
+
+		this.frm.set_query("party_type", () => {
 			return {
 				"filters": {
 					"name": ["in", Object.keys(frappe.boot.party_account_types)],
@@ -14,44 +19,30 @@
 			}
 		});
 
-		this.frm.set_query('receivable_payable_account', function() {
-			check_mandatory(me.frm);
+		this.frm.set_query('receivable_payable_account', () => {
 			return {
 				filters: {
-					"company": me.frm.doc.company,
+					"company": this.frm.doc.company,
 					"is_group": 0,
-					"account_type": frappe.boot.party_account_types[me.frm.doc.party_type]
+					"account_type": frappe.boot.party_account_types[this.frm.doc.party_type]
 				}
 			};
 		});
 
-		this.frm.set_query('bank_cash_account', function() {
-			check_mandatory(me.frm, true);
+		this.frm.set_query('bank_cash_account', () => {
 			return {
 				filters:[
-					['Account', 'company', '=', me.frm.doc.company],
+					['Account', 'company', '=', this.frm.doc.company],
 					['Account', 'is_group', '=', 0],
 					['Account', 'account_type', 'in', ['Bank', 'Cash']]
 				]
 			};
 		});
-
-		this.frm.set_value('party_type', '');
-		this.frm.set_value('party', '');
-		this.frm.set_value('receivable_payable_account', '');
-
-		var check_mandatory = (frm, only_company=false) => {
-			var title = __("Mandatory");
-			if (only_company && !frm.doc.company) {
-				frappe.throw({message: __("Please Select a Company First"), title: title});
-			} else if (!frm.doc.company || !frm.doc.party_type) {
-				frappe.throw({message: __("Please Select Both Company and Party Type First"), title: title});
-			}
-		};
 	}
 
 	refresh() {
 		this.frm.disable_save();
+
 		this.frm.set_df_property('invoices', 'cannot_delete_rows', true);
 		this.frm.set_df_property('payments', 'cannot_delete_rows', true);
 		this.frm.set_df_property('allocation', 'cannot_delete_rows', true);
@@ -85,76 +76,92 @@
 	}
 
 	company() {
-		var me = this;
+		this.frm.set_value('party', '');
 		this.frm.set_value('receivable_payable_account', '');
-		me.frm.clear_table("allocation");
-		me.frm.clear_table("invoices");
-		me.frm.clear_table("payments");
-		me.frm.refresh_fields();
-		me.frm.trigger('party');
+	}
+
+	party_type() {
+		this.frm.set_value('party', '');
 	}
 
 	party() {
-		var me = this;
-		if (!me.frm.doc.receivable_payable_account && me.frm.doc.party_type && me.frm.doc.party) {
+		this.frm.set_value('receivable_payable_account', '');
+		this.frm.trigger("clear_child_tables");
+
+		if (!this.frm.doc.receivable_payable_account && this.frm.doc.party_type && this.frm.doc.party) {
 			return frappe.call({
 				method: "erpnext.accounts.party.get_party_account",
 				args: {
-					company: me.frm.doc.company,
-					party_type: me.frm.doc.party_type,
-					party: me.frm.doc.party
+					company: this.frm.doc.company,
+					party_type: this.frm.doc.party_type,
+					party: this.frm.doc.party
 				},
-				callback: function(r) {
+				callback: (r) => {
 					if (!r.exc && r.message) {
-						me.frm.set_value("receivable_payable_account", r.message);
+						this.frm.set_value("receivable_payable_account", r.message);
 					}
-					me.frm.refresh();
+					this.frm.refresh();
+
 				}
 			});
 		}
 	}
 
+	receivable_payable_account() {
+		this.frm.trigger("clear_child_tables");
+		this.frm.refresh();
+	}
+
+	clear_child_tables() {
+		this.frm.clear_table("invoices");
+		this.frm.clear_table("payments");
+		this.frm.clear_table("allocation");
+		this.frm.refresh_fields();
+	}
+
 	get_unreconciled_entries() {
-		var me = this;
+		this.frm.clear_table("allocation");
 		return this.frm.call({
-			doc: me.frm.doc,
+			doc: this.frm.doc,
 			method: 'get_unreconciled_entries',
-			callback: function(r, rt) {
-				if (!(me.frm.doc.payments.length || me.frm.doc.invoices.length)) {
-					frappe.throw({message: __("No invoice and payment records found for this party")});
+			callback: () => {
+				if (!(this.frm.doc.payments.length || this.frm.doc.invoices.length)) {
+					frappe.throw({message: __("No Unreconciled Invoices and Payments found for this party and account")});
+				} else if (!(this.frm.doc.invoices.length)) {
+					frappe.throw({message: __("No Outstanding Invoices found for this party")});
+				} else if (!(this.frm.doc.payments.length)) {
+					frappe.throw({message: __("No Unreconciled Payments found for this party")});
 				}
-				me.frm.refresh();
+				this.frm.refresh();
 			}
 		});
 
 	}
 
 	allocate() {
-		var me = this;
-		let payments = me.frm.fields_dict.payments.grid.get_selected_children();
+		let payments = this.frm.fields_dict.payments.grid.get_selected_children();
 		if (!(payments.length)) {
-			payments = me.frm.doc.payments;
+			payments = this.frm.doc.payments;
 		}
-		let invoices = me.frm.fields_dict.invoices.grid.get_selected_children();
+		let invoices = this.frm.fields_dict.invoices.grid.get_selected_children();
 		if (!(invoices.length)) {
-			invoices = me.frm.doc.invoices;
+			invoices = this.frm.doc.invoices;
 		}
-		return me.frm.call({
-			doc: me.frm.doc,
+		return this.frm.call({
+			doc: this.frm.doc,
 			method: 'allocate_entries',
 			args: {
 				payments: payments,
 				invoices: invoices
 			},
-			callback: function() {
-				me.frm.refresh();
+			callback: () => {
+				this.frm.refresh();
 			}
 		});
 	}
 
 	reconcile() {
-		var me = this;
-		var show_dialog = me.frm.doc.allocation.filter(d => d.difference_amount && !d.difference_account);
+		var show_dialog = this.frm.doc.allocation.filter(d => d.difference_amount && !d.difference_account);
 
 		if (show_dialog && show_dialog.length) {
 
@@ -186,10 +193,10 @@
 							label: __("Difference Account"),
 							fieldname: 'difference_account',
 							reqd: 1,
-							get_query: function() {
+							get_query: () => {
 								return {
 									filters: {
-										company: me.frm.doc.company,
+										company: this.frm.doc.company,
 										is_group: 0
 									}
 								}
@@ -203,7 +210,7 @@
 						}]
 					},
 				],
-				primary_action: function() {
+				primary_action: () => {
 					const args = dialog.get_values()["allocation"];
 
 					args.forEach(d => {
@@ -211,7 +218,7 @@
 							"difference_account", d.difference_account);
 					});
 
-					me.reconcile_payment_entries();
+					this.reconcile_payment_entries();
 					dialog.hide();
 				},
 				primary_action_label: __('Reconcile Entries')
@@ -237,15 +244,12 @@
 	}
 
 	reconcile_payment_entries() {
-		var me = this;
-
 		return this.frm.call({
-			doc: me.frm.doc,
+			doc: this.frm.doc,
 			method: 'reconcile',
-			callback: function(r, rt) {
-				me.frm.clear_table("allocation");
-				me.frm.refresh_fields();
-				me.frm.refresh();
+			callback: () => {
+				this.frm.clear_table("allocation");
+				this.frm.refresh();
 			}
 		});
 	}
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 4f26ed4..28bd102 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
@@ -114,6 +114,8 @@
 	def merge_pos_invoice_into(self, invoice, data):
 		items, payments, taxes = [], [], []
 		loyalty_amount_sum, loyalty_points_sum = 0, 0
+		rounding_adjustment, base_rounding_adjustment = 0, 0
+		rounded_total, base_rounded_total = 0, 0
 		for doc in data:
 			map_doc(doc, invoice, table_map={ "doctype": invoice.doctype })
 
@@ -162,6 +164,11 @@
 						found = True
 				if not found:
 					payments.append(payment)
+			rounding_adjustment += doc.rounding_adjustment
+			rounded_total += doc.rounded_total
+			base_rounding_adjustment += doc.rounding_adjustment
+			base_rounded_total += doc.rounded_total
+
 
 		if loyalty_points_sum:
 			invoice.redeem_loyalty_points = 1
@@ -171,6 +178,10 @@
 		invoice.set('items', items)
 		invoice.set('payments', payments)
 		invoice.set('taxes', taxes)
+		invoice.set('rounding_adjustment',rounding_adjustment)
+		invoice.set('rounding_adjustment',base_rounding_adjustment)
+		invoice.set('base_rounded_total',base_rounded_total)
+		invoice.set('rounded_total',rounded_total)
 		invoice.additional_discount_percentage = 0
 		invoice.discount_amount = 0.0
 		invoice.taxes_and_charges = None
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 6ab0f96..5162182 100644
--- a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py
+++ b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py
@@ -183,6 +183,7 @@
 	ldc_name = frappe.db.get_value('Lower Deduction Certificate',
 		{
 			'pan_no': pan_no,
+			'tax_withholding_category': tax_details.tax_withholding_category,
 			'valid_from': ('>=', tax_details.from_date),
 			'valid_upto': ('<=', tax_details.to_date)
 		}, 'name')
diff --git a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py
index 0de2a98..0475231 100644
--- a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py
+++ b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py
@@ -114,8 +114,9 @@
 
 		# opening_value = Aseet - liability - equity
 		for data in [asset_data, liability_data, equity_data]:
-			account_name = get_root_account_name(data[0].root_type, company)
-			opening_value += (get_opening_balance(account_name, data, company) or 0.0)
+			if data:
+				account_name = get_root_account_name(data[0].root_type, company)
+				opening_value += (get_opening_balance(account_name, data, company) or 0.0)
 
 		opening_balance[company] = opening_value
 
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index fdd8d09..fb23d6f 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -450,7 +450,8 @@
 
 	# new row with references
 	new_row = journal_entry.append("accounts")
-	new_row.update(jv_detail.as_dict().copy())
+
+	new_row.update((frappe.copy_doc(jv_detail)).as_dict())
 
 	new_row.set(d["dr_or_cr"], d["allocated_amount"])
 	new_row.set('debit' if d['dr_or_cr'] == 'debit_in_account_currency' else 'credit',
diff --git a/erpnext/assets/report/fixed_asset_register/fixed_asset_register.js b/erpnext/assets/report/fixed_asset_register/fixed_asset_register.js
index 75f42a9..06989a9 100644
--- a/erpnext/assets/report/fixed_asset_register/fixed_asset_register.js
+++ b/erpnext/assets/report/fixed_asset_register/fixed_asset_register.js
@@ -16,9 +16,8 @@
 			fieldname:"status",
 			label: __("Status"),
 			fieldtype: "Select",
-			options: "In Location\nDisposed",
-			default: 'In Location',
-			reqd: 1
+			options: "\nIn Location\nDisposed",
+			default: 'In Location'
 		},
 		{
 			"fieldname":"filter_based_on",
diff --git a/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py b/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py
index e370b9d..63685fe 100644
--- a/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py
+++ b/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py
@@ -45,12 +45,13 @@
 	if filters.get('cost_center'):
 		conditions["cost_center"] = filters.get('cost_center')
 
-	# In Store assets are those that are not sold or scrapped
-	operand = 'not in'
-	if status not in 'In Location':
-		operand = 'in'
+	if status:
+		# In Store assets are those that are not sold or scrapped
+		operand = 'not in'
+		if status not in 'In Location':
+			operand = 'in'
 
-	conditions['status'] = (operand, ['Sold', 'Scrapped'])
+		conditions['status'] = (operand, ['Sold', 'Scrapped'])
 
 	return conditions
 
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 2486012..37b8f9d 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -815,6 +815,38 @@
 			if frappe.db.get_single_value('Accounts Settings', 'unlink_advance_payment_on_cancelation_of_order'):
 				unlink_ref_doc_from_payment_entries(self)
 
+			if self.doctype == "Sales Order":
+				self.unlink_ref_doc_from_po()
+
+	def unlink_ref_doc_from_po(self):
+		so_items = []
+		for item in self.items:
+			so_items.append(item.name)
+
+		linked_po = list(set(frappe.get_all(
+			'Purchase Order Item',
+			filters = {
+				'sales_order': self.name,
+				'sales_order_item': ['in', so_items],
+				'docstatus': ['<', 2]
+			},
+			pluck='parent'
+		)))
+
+		if linked_po:
+			frappe.db.set_value(
+				'Purchase Order Item', {
+					'sales_order': self.name,
+					'sales_order_item': ['in', so_items],
+					'docstatus': ['<', 2]
+				},{
+					'sales_order': None,
+					'sales_order_item': None
+				}
+			)
+
+			frappe.msgprint(_("Purchase Orders {0} are un-linked").format("\n".join(linked_po)))
+
 	def get_tax_map(self):
 		tax_map = {}
 		for tax in self.get('taxes'):
diff --git a/erpnext/controllers/queries.py b/erpnext/controllers/queries.py
index eeb659d..05ece4d 100644
--- a/erpnext/controllers/queries.py
+++ b/erpnext/controllers/queries.py
@@ -566,7 +566,7 @@
 
 		query_filters.append(['name', query_selector, dimensions])
 
-	output = frappe.get_all(doctype, filters=query_filters)
+	output = frappe.get_list(doctype, filters=query_filters)
 	result = [d.name for d in output]
 
 	return [(d,) for d in set(result)]
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index 70cc8a5..7e7f598 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -260,7 +260,9 @@
 		self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
 
 	def calculate_taxes(self):
-		self.doc.rounding_adjustment = 0
+		if not self.doc.get('is_consolidated'):
+			self.doc.rounding_adjustment = 0
+
 		# maintain actual tax rate based on idx
 		actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
 			for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
@@ -312,7 +314,9 @@
 
 					# adjust Discount Amount loss in last tax iteration
 					if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
-						and self.doc.discount_amount and self.doc.apply_discount_on == "Grand Total":
+						and self.doc.discount_amount \
+						and self.doc.apply_discount_on == "Grand Total" \
+						and not self.doc.get('is_consolidated'):
 							self.doc.rounding_adjustment = flt(self.doc.grand_total
 								- flt(self.doc.discount_amount) - tax.total,
 								self.doc.precision("rounding_adjustment"))
@@ -405,11 +409,16 @@
 				self.doc.rounding_adjustment = diff
 
 	def calculate_totals(self):
-		self.doc.grand_total = flt(self.doc.get("taxes")[-1].total) + flt(self.doc.rounding_adjustment) \
-			if self.doc.get("taxes") else flt(self.doc.net_total)
+		if self.doc.get("taxes"):
+			self.doc.grand_total = flt(self.doc.get("taxes")[-1].total) + flt(self.doc.rounding_adjustment)
+		else:
+			self.doc.grand_total = flt(self.doc.net_total)
 
-		self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total
+		if self.doc.get("taxes"):
+			self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total
 			- flt(self.doc.rounding_adjustment), self.doc.precision("total_taxes_and_charges"))
+		else:
+			self.doc.total_taxes_and_charges = 0.0
 
 		self._set_in_company_currency(self.doc, ["total_taxes_and_charges", "rounding_adjustment"])
 
@@ -446,19 +455,20 @@
 					self.doc.total_net_weight += d.total_weight
 
 	def set_rounded_total(self):
-		if self.doc.meta.get_field("rounded_total"):
-			if self.doc.is_rounded_total_disabled():
-				self.doc.rounded_total = self.doc.base_rounded_total = 0
-				return
+		if not self.doc.get('is_consolidated'):
+			if self.doc.meta.get_field("rounded_total"):
+				if self.doc.is_rounded_total_disabled():
+					self.doc.rounded_total = self.doc.base_rounded_total = 0
+					return
 
-			self.doc.rounded_total = round_based_on_smallest_currency_fraction(self.doc.grand_total,
-				self.doc.currency, self.doc.precision("rounded_total"))
+				self.doc.rounded_total = round_based_on_smallest_currency_fraction(self.doc.grand_total,
+					self.doc.currency, self.doc.precision("rounded_total"))
 
-			#if print_in_rate is set, we would have already calculated rounding adjustment
-			self.doc.rounding_adjustment += flt(self.doc.rounded_total - self.doc.grand_total,
-				self.doc.precision("rounding_adjustment"))
+				#if print_in_rate is set, we would have already calculated rounding adjustment
+				self.doc.rounding_adjustment += flt(self.doc.rounded_total - self.doc.grand_total,
+					self.doc.precision("rounding_adjustment"))
 
-			self._set_in_company_currency(self.doc, ["rounding_adjustment", "rounded_total"])
+				self._set_in_company_currency(self.doc, ["rounding_adjustment", "rounded_total"])
 
 	def _cleanup(self):
 		if not self.doc.get('is_consolidated'):
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 232e3a0..2cd8f8c 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -307,6 +307,9 @@
 		existing_bom_cost = self.total_cost
 
 		for d in self.get("items"):
+			if not d.item_code:
+				continue
+
 			rate = self.get_rm_rate({
 				"company": self.company,
 				"item_code": d.item_code,
@@ -599,7 +602,7 @@
 		for d in self.get('items'):
 			if d.bom_no:
 				self.get_child_exploded_items(d.bom_no, d.stock_qty)
-			else:
+			elif d.item_code:
 				self.add_to_cur_exploded_items(frappe._dict({
 					'item_code'		: d.item_code,
 					'item_name'		: d.item_name,
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index c533d48..2c21ab6 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -306,5 +306,6 @@
 erpnext.patches.v12_0.update_production_plan_status
 erpnext.patches.v13_0.healthcare_deprecation_warning
 erpnext.patches.v14_0.delete_healthcare_doctypes
+erpnext.patches.v13_0.update_category_in_ltds_certificate
 erpnext.patches.v13_0.create_pan_field_for_india #2
 erpnext.patches.v14_0.delete_hub_doctypes
diff --git a/erpnext/patches/v13_0/update_category_in_ltds_certificate.py b/erpnext/patches/v13_0/update_category_in_ltds_certificate.py
new file mode 100644
index 0000000..4d46452
--- /dev/null
+++ b/erpnext/patches/v13_0/update_category_in_ltds_certificate.py
@@ -0,0 +1,18 @@
+import frappe
+
+
+def execute():
+	company = frappe.get_all('Company', filters = {'country': 'India'})
+	if not company:
+		return
+
+	ldc = frappe.qb.DocType("Lower Deduction Certificate").as_("ldc")
+	supplier = frappe.qb.DocType("Supplier")
+
+	frappe.qb.update(ldc).inner_join(supplier).on(
+		ldc.supplier == supplier.name
+	).set(
+		ldc.tax_withholding_category, supplier.tax_withholding_category
+	).where(
+		ldc.tax_withholding_category.isnull()
+	).run()
\ No newline at end of file
diff --git a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.json b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.json
index f48fe6f..c32ab6b 100644
--- a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.json
+++ b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.json
@@ -7,7 +7,7 @@
  "engine": "InnoDB",
  "field_order": [
   "certificate_details_section",
-  "section_code",
+  "tax_withholding_category",
   "fiscal_year",
   "column_break_3",
   "certificate_no",
@@ -34,13 +34,6 @@
    "unique": 1
   },
   {
-   "fieldname": "section_code",
-   "fieldtype": "Select",
-   "label": "Section Code",
-   "options": "192\n193\n194\n194A\n194C\n194D\n194H\n194I\n194J\n194LA\n194LBB\n194LBC\n195",
-   "reqd": 1
-  },
-  {
    "fieldname": "section_break_3",
    "fieldtype": "Section Break",
    "label": "Deductee Details"
@@ -123,13 +116,22 @@
    "label": "Fiscal Year",
    "options": "Fiscal Year",
    "reqd": 1
+  },
+  {
+   "fieldname": "tax_withholding_category",
+   "fieldtype": "Link",
+   "label": "Tax Withholding Category",
+   "options": "Tax Withholding Category",
+   "reqd": 1
   }
  ],
+ "index_web_pages_for_search": 1,
  "links": [],
- "modified": "2020-04-23 23:04:41.203721",
+ "modified": "2021-10-23 18:33:38.962622",
  "modified_by": "Administrator",
  "module": "Regional",
  "name": "Lower Deduction Certificate",
+ "naming_rule": "By fieldname",
  "owner": "Administrator",
  "permissions": [],
  "sort_field": "modified",
diff --git a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py
index d8553f1..7afbc00 100644
--- a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py
+++ b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py
@@ -15,7 +15,7 @@
 class LowerDeductionCertificate(Document):
 	def validate(self):
 		self.validate_dates()
-		self.validate_supplier_against_section_code()
+		self.validate_supplier_against_tax_category()
 
 	def validate_dates(self):
 		if getdate(self.valid_upto) < getdate(self.valid_from):
@@ -31,12 +31,14 @@
 			<= fiscal_year.year_end_date):
 			frappe.throw(_("Valid Upto date not in Fiscal Year {0}").format(frappe.bold(self.fiscal_year)))
 
-	def validate_supplier_against_section_code(self):
-		duplicate_certificate = frappe.db.get_value('Lower Deduction Certificate', {'supplier': self.supplier, 'section_code': self.section_code}, ['name', 'valid_from', 'valid_upto'], as_dict=True)
+	def tax_withholding_category(self):
+		duplicate_certificate = frappe.db.get_value('Lower Deduction Certificate',
+			{'supplier': self.supplier, 'tax_withholding_category': self.tax_withholding_category, 'name': ("!=", self.name)},
+			['name', 'valid_from', 'valid_upto'], as_dict=True)
 		if duplicate_certificate and self.are_dates_overlapping(duplicate_certificate):
 			certificate_link = get_link_to_form('Lower Deduction Certificate', duplicate_certificate.name)
-			frappe.throw(_("There is already a valid Lower Deduction Certificate {0} for Supplier {1} against Section Code {2} for this time period.")
-				.format(certificate_link, frappe.bold(self.supplier), frappe.bold(self.section_code)))
+			frappe.throw(_("There is already a valid Lower Deduction Certificate {0} for Supplier {1} against category {2} for this time period.")
+				.format(certificate_link, frappe.bold(self.supplier), frappe.bold(self.tax_withholding_category)))
 
 	def are_dates_overlapping(self,duplicate_certificate):
 		valid_from = duplicate_certificate.valid_from
diff --git a/erpnext/regional/report/uae_vat_201/uae_vat_201.py b/erpnext/regional/report/uae_vat_201/uae_vat_201.py
index f4c049d..2b5ecc3 100644
--- a/erpnext/regional/report/uae_vat_201/uae_vat_201.py
+++ b/erpnext/regional/report/uae_vat_201/uae_vat_201.py
@@ -122,7 +122,7 @@
 	try:
 		return frappe.db.sql("""
 			select
-				s.vat_emirate as emirate, sum(i.base_amount) as total, sum(s.total_taxes_and_charges)
+				s.vat_emirate as emirate, sum(i.base_amount) as total, sum(i.tax_amount)
 			from
 				`tabSales Invoice Item` i inner join `tabSales Invoice` s
 			on
diff --git a/erpnext/selling/doctype/sales_order/sales_order.js b/erpnext/selling/doctype/sales_order/sales_order.js
index f692690..d46c46f 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.js
+++ b/erpnext/selling/doctype/sales_order/sales_order.js
@@ -78,6 +78,8 @@
 		});
 
 		erpnext.queries.setup_warehouse_query(frm);
+
+		frm.ignore_doctypes_on_cancel_all = ['Purchase Order'];
 	},
 
 	delivery_date: function(frm) {
diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py
index c4a0497..e1d5a89 100644
--- a/erpnext/stock/utils.py
+++ b/erpnext/stock/utils.py
@@ -101,11 +101,7 @@
 
 	if with_valuation_rate:
 		if with_serial_no:
-			serial_nos = last_entry.get("serial_no")
-
-			if (serial_nos and
-				len(get_serial_nos_data(serial_nos)) < last_entry.qty_after_transaction):
-				serial_nos = get_serial_nos_data_after_transactions(args)
+			serial_nos = get_serial_nos_data_after_transactions(args)
 
 			return ((last_entry.qty_after_transaction, last_entry.valuation_rate, serial_nos)
 				if last_entry else (0.0, 0.0, 0.0))
@@ -115,19 +111,32 @@
 		return last_entry.qty_after_transaction if last_entry else 0.0
 
 def get_serial_nos_data_after_transactions(args):
-	serial_nos = []
-	data = frappe.db.sql(""" SELECT serial_no, actual_qty
-		FROM `tabStock Ledger Entry`
-		WHERE
-			item_code = %(item_code)s and warehouse = %(warehouse)s
-			and timestamp(posting_date, posting_time) < timestamp(%(posting_date)s, %(posting_time)s)
-			order by posting_date, posting_time asc """, args, as_dict=1)
+	from pypika import CustomFunction
 
-	for d in data:
-		if d.actual_qty > 0:
-			serial_nos.extend(get_serial_nos_data(d.serial_no))
+	serial_nos = set()
+	args = frappe._dict(args)
+	sle = frappe.qb.DocType('Stock Ledger Entry')
+	Timestamp = CustomFunction('timestamp', ['date', 'time'])
+
+	stock_ledger_entries = frappe.qb.from_(
+		sle
+	).select(
+		'serial_no','actual_qty'
+	).where(
+		(sle.item_code == args.item_code)
+		& (sle.warehouse == args.warehouse)
+		& (Timestamp(sle.posting_date, sle.posting_time) < Timestamp(args.posting_date, args.posting_time))
+		& (sle.is_cancelled == 0)
+	).orderby(
+		sle.posting_date, sle.posting_time, sle.creation
+	).run(as_dict=1)
+
+	for stock_ledger_entry in stock_ledger_entries:
+		changed_serial_no = get_serial_nos_data(stock_ledger_entry.serial_no)
+		if stock_ledger_entry.actual_qty > 0:
+			serial_nos.update(changed_serial_no)
 		else:
-			serial_nos = list(set(serial_nos) - set(get_serial_nos_data(d.serial_no)))
+			serial_nos.difference_update(changed_serial_no)
 
 	return '\n'.join(serial_nos)
 
diff --git a/erpnext/tests/test_woocommerce.py b/erpnext/tests/test_woocommerce.py
index 881f286..3ce68d8 100644
--- a/erpnext/tests/test_woocommerce.py
+++ b/erpnext/tests/test_woocommerce.py
@@ -12,12 +12,6 @@
 
 class TestWoocommerce(unittest.TestCase):
 	def setUp(self):
-		if not frappe.db.exists('Company', 'Woocommerce'):
-			company = frappe.new_doc("Company")
-			company.company_name = "Woocommerce"
-			company.abbr = "W"
-			company.default_currency = "INR"
-			company.save()
 
 		woo_settings = frappe.get_doc("Woocommerce Settings")
 		if not woo_settings.secret:
@@ -26,14 +20,14 @@
 			woo_settings.api_consumer_key = "ck_fd43ff5756a6abafd95fadb6677100ce95a758a1"
 			woo_settings.api_consumer_secret = "cs_94360a1ad7bef7fa420a40cf284f7b3e0788454e"
 			woo_settings.enable_sync = 1
-			woo_settings.company = "Woocommerce"
-			woo_settings.tax_account = "Sales Expenses - W"
-			woo_settings.f_n_f_account = "Expenses - W"
+			woo_settings.company = "_Test Company"
+			woo_settings.tax_account = "Sales Expenses - _TC"
+			woo_settings.f_n_f_account = "Expenses - _TC"
 			woo_settings.creation_user = "Administrator"
 			woo_settings.save(ignore_permissions=True)
 
 	def test_sales_order_for_woocommerce(self):
-		frappe.flags.woocomm_test_order_data = {"id":75,"parent_id":0,"number":"74","order_key":"wc_order_5aa1281c2dacb","created_via":"checkout","version":"3.3.3","status":"processing","currency":"INR","date_created":"2018-03-08T12:10:04","date_created_gmt":"2018-03-08T12:10:04","date_modified":"2018-03-08T12:10:04","date_modified_gmt":"2018-03-08T12:10:04","discount_total":"0.00","discount_tax":"0.00","shipping_total":"150.00","shipping_tax":"0.00","cart_tax":"0.00","total":"649.00","total_tax":"0.00","prices_include_tax":False,"customer_id":12,"customer_ip_address":"103.54.99.5","customer_user_agent":"mozilla\\/5.0 (x11; linux x86_64) applewebkit\\/537.36 (khtml, like gecko) chrome\\/64.0.3282.186 safari\\/537.36","customer_note":"","billing":{"first_name":"Tony","last_name":"Stark","company":"Woocommerce","address_1":"Mumbai","address_2":"","city":"Dadar","state":"MH","postcode":"123","country":"IN","email":"tony@gmail.com","phone":"123457890"},"shipping":{"first_name":"Tony","last_name":"Stark","company":"","address_1":"Mumbai","address_2":"","city":"Dadar","state":"MH","postcode":"123","country":"IN"},"payment_method":"cod","payment_method_title":"Cash on delivery","transaction_id":"","date_paid":"","date_paid_gmt":"","date_completed":"","date_completed_gmt":"","cart_hash":"8e76b020d5790066496f244860c4703f","meta_data":[],"line_items":[{"id":80,"name":"Marvel","product_id":56,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"499.00","subtotal_tax":"0.00","total":"499.00","total_tax":"0.00","taxes":[],"meta_data":[],"sku":"","price":499}],"tax_lines":[],"shipping_lines":[{"id":81,"method_title":"Flat rate","method_id":"flat_rate:1","total":"150.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":623,"key":"Items","value":"Marvel &times; 1"}]}],"fee_lines":[],"coupon_lines":[],"refunds":[]}
+		frappe.flags.woocomm_test_order_data = {"id":75,"parent_id":0,"number":"74","order_key":"wc_order_5aa1281c2dacb","created_via":"checkout","version":"3.3.3","status":"processing","currency":"INR","date_created":"2018-03-08T12:10:04","date_created_gmt":"2018-03-08T12:10:04","date_modified":"2018-03-08T12:10:04","date_modified_gmt":"2018-03-08T12:10:04","discount_total":"0.00","discount_tax":"0.00","shipping_total":"150.00","shipping_tax":"0.00","cart_tax":"0.00","total":"649.00","total_tax":"0.00","prices_include_tax":False,"customer_id":12,"customer_ip_address":"103.54.99.5","customer_user_agent":"mozilla\\/5.0 (x11; linux x86_64) applewebkit\\/537.36 (khtml, like gecko) chrome\\/64.0.3282.186 safari\\/537.36","customer_note":"","billing":{"first_name":"Tony","last_name":"Stark","company":"_Test Company","address_1":"Mumbai","address_2":"","city":"Dadar","state":"MH","postcode":"123","country":"IN","email":"tony@gmail.com","phone":"123457890"},"shipping":{"first_name":"Tony","last_name":"Stark","company":"","address_1":"Mumbai","address_2":"","city":"Dadar","state":"MH","postcode":"123","country":"IN"},"payment_method":"cod","payment_method_title":"Cash on delivery","transaction_id":"","date_paid":"","date_paid_gmt":"","date_completed":"","date_completed_gmt":"","cart_hash":"8e76b020d5790066496f244860c4703f","meta_data":[],"line_items":[{"id":80,"name":"Marvel","product_id":56,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"499.00","subtotal_tax":"0.00","total":"499.00","total_tax":"0.00","taxes":[],"meta_data":[],"sku":"","price":499}],"tax_lines":[],"shipping_lines":[{"id":81,"method_title":"Flat rate","method_id":"flat_rate:1","total":"150.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":623,"key":"Items","value":"Marvel &times; 1"}]}],"fee_lines":[],"coupon_lines":[],"refunds":[]}
 		order()
 
 		self.assertTrue(frappe.get_value("Customer",{"woocommerce_email":"tony@gmail.com"}))