fix: Add tests
diff --git a/erpnext/accounts/doctype/advance_taxes_and_charges/advance_taxes_and_charges.json b/erpnext/accounts/doctype/advance_taxes_and_charges/advance_taxes_and_charges.json
index 4d63499..05b284a 100644
--- a/erpnext/accounts/doctype/advance_taxes_and_charges/advance_taxes_and_charges.json
+++ b/erpnext/accounts/doctype/advance_taxes_and_charges/advance_taxes_and_charges.json
@@ -25,8 +25,7 @@
   "allocated_amount",
   "column_break_13",
   "base_tax_amount",
-  "base_total",
-  "base_allocated_amount"
+  "base_total"
  ],
  "fields": [
   {
@@ -169,12 +168,6 @@
    "options": "currency"
   },
   {
-   "fieldname": "base_allocated_amount",
-   "fieldtype": "Currency",
-   "label": "Allocated Amount (Company Currency)",
-   "options": "Company:company:default_currency"
-  },
-  {
    "fetch_from": "account_head.account_currency",
    "fieldname": "currency",
    "fieldtype": "Link",
@@ -186,7 +179,7 @@
  "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2021-06-09 11:46:58.373170",
+ "modified": "2021-11-25 11:10:10.945027",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Advance Taxes and Charges",
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index e524592..7aeb872 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -435,24 +435,16 @@
 
 		net_total = self.paid_amount
 
-		for reference in self.get("references"):
-			net_total_for_tds = 0
-			if reference.reference_doctype == 'Purchase Order':
-				net_total_for_tds += flt(frappe.db.get_value('Purchase Order', reference.reference_name, 'net_total'))
-
-			if net_total_for_tds:
-				net_total = net_total_for_tds
-
 		# Adding args as purchase invoice to get TDS amount
 		args = frappe._dict({
 			'company': self.company,
-			'doctype': 'Purchase Invoice',
+			'doctype': 'Payment Entry',
 			'supplier': self.party,
 			'posting_date': self.posting_date,
 			'net_total': net_total
 		})
 
-		tax_withholding_details, tax_deducted_on_advances = get_party_tax_withholding_details(args, self.tax_withholding_category)
+		tax_withholding_details = get_party_tax_withholding_details(args, self.tax_withholding_category)
 
 		if not tax_withholding_details:
 			return
@@ -1593,10 +1585,6 @@
 			})
 			pe.set_difference_amount()
 
-	if doc.doctype == 'Purchase Order' and doc.apply_tds:
-		pe.apply_tax_withholding_amount = 1
-		pe.tax_withholding_category = doc.tax_withholding_category
-
 	return pe
 
 def get_bank_cash_account(doc, bank_account):
diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index 78396a5..aa2408e 100644
--- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -1160,25 +1160,21 @@
 		# Create Purchase Order with TDS applied
 		po = create_purchase_order(do_not_save=1, supplier=supplier.name, rate=3000, item='_Test Non Stock Item',
 			posting_date='2021-09-15')
-		po.apply_tds = 1
-		po.tax_withholding_category = 'TDS - 194 - Dividends - Individual'
 		po.save()
 		po.submit()
 
-		# Update Unrealized Profit / Loss Account which is used as default advance tax account
-		frappe.db.set_value('Company', '_Test Company', 'unrealized_profit_loss_account', '_Test Account Excise Duty - _TC')
-
 		# Create Payment Entry Against the order
 		payment_entry = get_payment_entry(dt='Purchase Order', dn=po.name)
 		payment_entry.paid_from = 'Cash - _TC'
+		payment_entry.apply_tax_withholding_amount = 1
+		payment_entry.tax_withholding_category = 'TDS - 194 - Dividends - Individual'
 		payment_entry.save()
 		payment_entry.submit()
 
 		# Check GLE for Payment Entry
 		expected_gle = [
-			['_Test Account Excise Duty - _TC', 3000, 0],
 			['Cash - _TC', 0, 27000],
-			['Creditors - _TC', 27000, 0],
+			['Creditors - _TC', 30000, 0],
 			['TDS Payable - _TC', 0, 3000],
 		]
 
@@ -1204,9 +1200,7 @@
 		# Zero net effect on final TDS Payable on invoice
 		expected_gle = [
 			['_Test Account Cost for Goods Sold - _TC', 30000],
-			['_Test Account Excise Duty - _TC', -3000],
-			['Creditors - _TC', -27000],
-			['TDS Payable - _TC', 0]
+			['Creditors - _TC', -30000]
 		]
 
 		gl_entries = frappe.db.sql("""select account, sum(debit - credit) as amount
@@ -1219,6 +1213,14 @@
 			self.assertEqual(expected_gle[i][0], gle.account)
 			self.assertEqual(expected_gle[i][1], gle.amount)
 
+		payment_entry.load_from_db()
+		self.assertEqual(payment_entry.taxes[0].allocated_amount, 3000)
+
+		purchase_invoice.cancel()
+
+		payment_entry.load_from_db()
+		self.assertEqual(payment_entry.taxes[0].allocated_amount, 0)
+
 def check_gl_entries(doc, voucher_no, expected_gle, posting_date):
 	gl_entries = frappe.db.sql("""select account, debit, credit, posting_date
 		from `tabGL Entry`
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 fe156cb..5bb9b93 100644
--- a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py
+++ b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py
@@ -197,7 +197,10 @@
 	advance_vouchers = get_advance_vouchers(parties, company=inv.company, from_date=tax_details.from_date,
 		to_date=tax_details.to_date, party_type=party_type)
 	taxable_vouchers = vouchers + advance_vouchers
-	tax_deducted_on_advances = get_taxes_deducted_on_advances_allocated(inv, tax_details)
+	tax_deducted_on_advances = 0
+
+	if inv.doctype == 'Purchase Invoice':
+		tax_deducted_on_advances = get_taxes_deducted_on_advances_allocated(inv, tax_details)
 
 	tax_deducted = 0
 	if taxable_vouchers:
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 9cf2e33..c526029 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -145,15 +145,16 @@
 		self.validate_party()
 		self.validate_currency()
 
+		if self.doctype in ['Purchase Invoice', 'Sales Invoice']:
+			pos_check_field = "is_pos" if self.doctype=="Sales Invoice" else "is_paid"
+			if cint(self.allocate_advances_automatically) and not cint(self.get(pos_check_field)):
+				self.set_advances()
+
 		if self.doctype == 'Purchase Invoice':
 			self.calculate_paid_amount()
 			# apply tax withholding only if checked and applicable
 			self.set_tax_withholding()
 
-		if self.doctype in ['Purchase Invoice', 'Sales Invoice']:
-			pos_check_field = "is_pos" if self.doctype=="Sales Invoice" else "is_paid"
-			if cint(self.allocate_advances_automatically) and not cint(self.get(pos_check_field)):
-				self.set_advances()
 
 			self.set_advance_gain_or_loss()