Skip to content

Commit

Permalink
fix: updated logic for calculating tax_withholding_net_total in payme…
Browse files Browse the repository at this point in the history
…nt entry
  • Loading branch information
ljain112 committed Jul 2, 2024
1 parent eca3e02 commit 575dbfe
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions erpnext/accounts/doctype/payment_entry/payment_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def validate(self):
self.set_exchange_rate()
self.validate_mandatory()
self.validate_reference_documents()
self.set_tax_withholding()
self.set_amounts()
self.validate_amounts()
self.apply_taxes()
Expand All @@ -178,6 +177,7 @@ def validate(self):
self.validate_allocated_amount()
self.validate_paid_invoices()
self.ensure_supplier_is_not_blocked()
self.set_tax_withholding()
self.set_status()
self.set_total_in_words()

Expand Down Expand Up @@ -846,9 +846,7 @@ def set_tax_withholding(self):
if not self.apply_tax_withholding_amount:
return

order_amount = self.get_order_net_total()

net_total = flt(order_amount) + flt(self.unallocated_amount)
net_total = self.calculate_tax_withholding_net_total()

# Adding args as purchase invoice to get TDS amount
args = frappe._dict(
Expand Down Expand Up @@ -892,20 +890,39 @@ def set_tax_withholding(self):
for d in to_remove:
self.remove(d)

def get_order_net_total(self):
def calculate_tax_withholding_net_total(self):
net_total = 0
order_details = self.get_order_wise_tax_withholding_net_total()

for d in self.references:
tax_withholding_net_total = order_details.get(d.reference_name)
if not tax_withholding_net_total:
continue

net_taxable_outstanding = max(
0, d.outstanding_amount - (d.total_amount - tax_withholding_net_total)
)

net_total += min(net_taxable_outstanding, d.allocated_amount)

net_total += self.unallocated_amount

return net_total

def get_order_wise_tax_withholding_net_total(self):
if self.party_type == "Supplier":
doctype = "Purchase Order"
else:
doctype = "Sales Order"

docnames = [d.reference_name for d in self.references if d.reference_doctype == doctype]

tax_withholding_net_total = frappe.db.get_value(
doctype, {"name": ["in", docnames]}, ["sum(base_tax_withholding_net_total)"]
return frappe._dict(
frappe.db.get_all(
doctype, filters={"name": ["in", docnames]}, fields=["name", "base_tax_withholding_net_total"]
)
)

return tax_withholding_net_total

def apply_taxes(self):
self.initialize_taxes()
self.determine_exclusive_rate()
Expand Down

0 comments on commit 575dbfe

Please sign in to comment.