fix: Allocate advance taxes only for payment entry
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index a4bac35..f925bcf 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -903,42 +903,43 @@
def allocate_advance_taxes(self, gl_entries):
tax_map = self.get_tax_map()
for pe in self.get('advances'):
- pe = frappe.get_doc('Payment Entry', pe.reference_name)
- for tax in pe.get('taxes'):
- account_currency = get_account_currency(tax.account_head)
- dr_or_cr = "credit" if tax.add_deduct_tax == "Add" else "debit"
- rev_dr_cr = "debit" if tax.add_deduct_tax == "Add" else "credit"
+ if pe.reference_type == 'Payment Entry':
+ pe = frappe.get_doc('Payment Entry', pe.reference_name)
+ for tax in pe.get('taxes'):
+ account_currency = get_account_currency(tax.account_head)
+ dr_or_cr = "credit" if tax.add_deduct_tax == "Add" else "debit"
+ rev_dr_cr = "debit" if tax.add_deduct_tax == "Add" else "credit"
- unallocated_amount = tax.tax_amount - tax.allocated_amount
- if tax_map.get(tax.account_head):
- amount = tax_map.get(tax.account_head)
- if amount < unallocated_amount:
- unallocated_amount = amount
+ unallocated_amount = tax.tax_amount - tax.allocated_amount
+ if tax_map.get(tax.account_head):
+ amount = tax_map.get(tax.account_head)
+ if amount < unallocated_amount:
+ unallocated_amount = amount
- gl_entries.append(
- self.get_gl_dict({
- "account": tax.account_head,
- "against": self.supplier,
- dr_or_cr: unallocated_amount,
- dr_or_cr + "_in_account_currency": unallocated_amount
- if account_currency==self.company_currency
- else unallocated_amount,
- 'cost_center': tax.cost_center
- }, account_currency, item=tax))
+ gl_entries.append(
+ self.get_gl_dict({
+ "account": tax.account_head,
+ "against": self.supplier,
+ dr_or_cr: unallocated_amount,
+ dr_or_cr + "_in_account_currency": unallocated_amount
+ if account_currency==self.company_currency
+ else unallocated_amount,
+ 'cost_center': tax.cost_center
+ }, account_currency, item=tax))
- gl_entries.append(
- self.get_gl_dict({
- "account": pe.advance_tax_account,
- "against": self.supplier,
- rev_dr_cr: unallocated_amount,
- rev_dr_cr + "_in_account_currency": unallocated_amount
- if account_currency==self.company_currency
- else unallocated_amount,
- 'cost_center': tax.cost_center or self.cost_center
- }, account_currency, item=tax))
+ gl_entries.append(
+ self.get_gl_dict({
+ "account": pe.advance_tax_account,
+ "against": self.supplier,
+ rev_dr_cr: unallocated_amount,
+ rev_dr_cr + "_in_account_currency": unallocated_amount
+ if account_currency==self.company_currency
+ else unallocated_amount,
+ 'cost_center': tax.cost_center or self.cost_center
+ }, account_currency, item=tax))
- frappe.db.set_value('Advance Taxes and Charges', tax.name, 'allocated_amount', tax.allocated_amount + unallocated_amount)
- tax_map[tax.account_head] -= unallocated_amount
+ frappe.db.set_value('Advance Taxes and Charges', tax.name, 'allocated_amount', tax.allocated_amount + unallocated_amount)
+ tax_map[tax.account_head] -= unallocated_amount
def make_payment_gl_entries(self, gl_entries):
# Make Cash GL Entries
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 9a596f2..664b3ab 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -916,42 +916,43 @@
def allocate_advance_taxes(self, gl_entries):
tax_map = self.get_tax_map()
for pe in self.get('advances'):
- pe = frappe.get_doc('Payment Entry', pe.reference_name)
- for tax in pe.get('taxes'):
- account_currency = get_account_currency(tax.account_head)
- dr_or_cr = "debit" if tax.add_deduct_tax == "Add" else "credit"
- rev_dr_cr = "credit" if tax.add_deduct_tax == "Add" else "debit"
+ if pe.reference_type == 'Payment Entry':
+ pe = frappe.get_doc('Payment Entry', pe.reference_name)
+ for tax in pe.get('taxes'):
+ account_currency = get_account_currency(tax.account_head)
+ dr_or_cr = "debit" if tax.add_deduct_tax == "Add" else "credit"
+ rev_dr_cr = "credit" if tax.add_deduct_tax == "Add" else "debit"
- unallocated_amount = tax.tax_amount - tax.allocated_amount
- if tax_map.get(tax.account_head):
- amount = tax_map.get(tax.account_head)
- if amount < unallocated_amount:
- unallocated_amount = amount
+ unallocated_amount = tax.tax_amount - tax.allocated_amount
+ if tax_map.get(tax.account_head):
+ amount = tax_map.get(tax.account_head)
+ if amount < unallocated_amount:
+ unallocated_amount = amount
- gl_entries.append(
- self.get_gl_dict({
- "account": tax.account_head,
- "against": self.customer,
- dr_or_cr: unallocated_amount,
- dr_or_cr + "_in_account_currency": unallocated_amount
- if account_currency==self.company_currency
- else unallocated_amount,
- 'cost_center': tax.cost_center
- }, account_currency, item=tax))
+ gl_entries.append(
+ self.get_gl_dict({
+ "account": tax.account_head,
+ "against": self.customer,
+ dr_or_cr: unallocated_amount,
+ dr_or_cr + "_in_account_currency": unallocated_amount
+ if account_currency==self.company_currency
+ else unallocated_amount,
+ 'cost_center': tax.cost_center
+ }, account_currency, item=tax))
- gl_entries.append(
- self.get_gl_dict({
- "account": pe.advance_tax_account,
- "against": self.customer,
- rev_dr_cr: unallocated_amount,
- rev_dr_cr + "_in_account_currency": unallocated_amount
- if account_currency==self.company_currency
- else unallocated_amount,
- 'cost_center': tax.cost_center
- }, account_currency, item=tax))
+ gl_entries.append(
+ self.get_gl_dict({
+ "account": pe.advance_tax_account,
+ "against": self.customer,
+ rev_dr_cr: unallocated_amount,
+ rev_dr_cr + "_in_account_currency": unallocated_amount
+ if account_currency==self.company_currency
+ else unallocated_amount,
+ 'cost_center': tax.cost_center
+ }, account_currency, item=tax))
- frappe.db.set_value('Advance Taxes and Charges', tax.name, 'allocated_amount', tax.allocated_amount + unallocated_amount)
- tax_map[tax.account_head] -= unallocated_amount
+ frappe.db.set_value('Advance Taxes and Charges', tax.name, 'allocated_amount', tax.allocated_amount + unallocated_amount)
+ tax_map[tax.account_head] -= unallocated_amount
def make_item_gl_entries(self, gl_entries):
# income account gl entries
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index da88853..11a9d90 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -726,16 +726,17 @@
tax_map = self.get_tax_map()
for pe in self.get('advances'):
- pe = frappe.get_doc('Payment Entry', pe.reference_name)
- for tax in pe.get('taxes'):
- allocated_amount = tax_map.get(tax.account_head) - allocated_tax_map.get(tax.account_head)
- if allocated_amount > tax.tax_amount:
- allocated_amount = tax.tax_amount
+ if pe.reference_type == 'Payment Entry':
+ pe = frappe.get_doc('Payment Entry', pe.reference_name)
+ for tax in pe.get('taxes'):
+ allocated_amount = tax_map.get(tax.account_head) - allocated_tax_map.get(tax.account_head)
+ if allocated_amount > tax.tax_amount:
+ allocated_amount = tax.tax_amount
- if allocated_amount:
- frappe.db.set_value('Advance Taxes and Charges', tax.name, 'allocated_amount', tax.allocated_amount - allocated_amount)
- tax_map[tax.account_head] -= allocated_amount
- allocated_tax_map[tax.account_head] -= allocated_amount
+ if allocated_amount:
+ frappe.db.set_value('Advance Taxes and Charges', tax.name, 'allocated_amount', tax.allocated_amount - allocated_amount)
+ tax_map[tax.account_head] -= allocated_amount
+ allocated_tax_map[tax.account_head] -= allocated_amount
def validate_multiple_billing(self, ref_dt, item_ref_dn, based_on, parentfield):
from erpnext.controllers.status_updater import get_allowance_for