mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-15 03:01:22 +00:00
fix: tds for customer and supplier in Journal Entry
This commit is contained in:
@@ -353,56 +353,52 @@ class JournalEntry(AccountsController):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def apply_tax_withholding(self):
|
def apply_tax_withholding(self):
|
||||||
from erpnext.accounts.report.general_ledger.general_ledger import get_account_type_map
|
|
||||||
|
|
||||||
if not self.apply_tds or self.voucher_type not in ("Debit Note", "Credit Note"):
|
if not self.apply_tds or self.voucher_type not in ("Debit Note", "Credit Note"):
|
||||||
return
|
return
|
||||||
|
|
||||||
parties = [d.party for d in self.get("accounts") if d.party]
|
party = None
|
||||||
parties = list(set(parties))
|
party_type = None
|
||||||
|
party_account = None
|
||||||
|
|
||||||
if len(parties) > 1:
|
for d in self.get("accounts"):
|
||||||
frappe.throw(_("Cannot apply TDS against multiple parties in one entry"))
|
if d.party and party and d.party != party:
|
||||||
|
frappe.throw(_("Cannot apply TDS against multiple parties in one entry"))
|
||||||
|
|
||||||
account_type_map = get_account_type_map(self.company)
|
if d.party_type in ("Customer", "Supplier") and d.party:
|
||||||
party_type = "supplier" if self.voucher_type == "Credit Note" else "customer"
|
party = d.party
|
||||||
doctype = "Purchase Invoice" if self.voucher_type == "Credit Note" else "Sales Invoice"
|
party_type = d.party_type
|
||||||
debit_or_credit = (
|
party_account = d.account
|
||||||
"debit_in_account_currency"
|
break
|
||||||
if self.voucher_type == "Credit Note"
|
|
||||||
else "credit_in_account_currency"
|
# debit or credit based on party type
|
||||||
|
dr_or_cr = "credit_in_account_currency" if party_type == "Supplier" else "debit_in_account_currency"
|
||||||
|
rev_dr_or_cr = (
|
||||||
|
"debit_in_account_currency" if party_type == "Supplier" else "credit_in_account_currency"
|
||||||
)
|
)
|
||||||
rev_debit_or_credit = (
|
|
||||||
"credit_in_account_currency"
|
|
||||||
if debit_or_credit == "debit_in_account_currency"
|
|
||||||
else "debit_in_account_currency"
|
|
||||||
)
|
|
||||||
|
|
||||||
party_account = get_party_account(party_type.title(), parties[0], self.company)
|
|
||||||
|
|
||||||
net_total = sum(
|
net_total = sum(
|
||||||
d.get(debit_or_credit)
|
d.get(dr_or_cr) - d.get(rev_dr_or_cr)
|
||||||
for d in self.get("accounts")
|
for d in self.get("accounts")
|
||||||
if account_type_map.get(d.account) not in ("Tax", "Chargeable")
|
if d.party == party and d.party_type == party_type
|
||||||
)
|
)
|
||||||
|
|
||||||
party_amount = sum(
|
# only apply tds if net total is positive
|
||||||
d.get(rev_debit_or_credit) for d in self.get("accounts") if d.account == party_account
|
if net_total <= 0:
|
||||||
)
|
return
|
||||||
|
|
||||||
inv = frappe._dict(
|
inv = frappe._dict(
|
||||||
{
|
{
|
||||||
party_type: parties[0],
|
"party_type": party_type,
|
||||||
"doctype": doctype,
|
"party": party,
|
||||||
|
"doctype": self.doctype,
|
||||||
"company": self.company,
|
"company": self.company,
|
||||||
"posting_date": self.posting_date,
|
"posting_date": self.posting_date,
|
||||||
"net_total": net_total,
|
"tax_withholding_net_total": net_total,
|
||||||
|
"base_tax_withholding_net_total": net_total,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
tax_withholding_details, advance_taxes, voucher_wise_amount = get_party_tax_withholding_details(
|
tax_withholding_details = get_party_tax_withholding_details(inv, self.tax_withholding_category)
|
||||||
inv, self.tax_withholding_category
|
|
||||||
)
|
|
||||||
|
|
||||||
if not tax_withholding_details:
|
if not tax_withholding_details:
|
||||||
return
|
return
|
||||||
@@ -413,29 +409,36 @@ class JournalEntry(AccountsController):
|
|||||||
d.update(
|
d.update(
|
||||||
{
|
{
|
||||||
"account": tax_withholding_details.get("account_head"),
|
"account": tax_withholding_details.get("account_head"),
|
||||||
debit_or_credit: tax_withholding_details.get("tax_amount"),
|
dr_or_cr: tax_withholding_details.get("tax_amount"),
|
||||||
|
rev_dr_or_cr: 0,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
accounts.append(d.get("account"))
|
accounts.append(d.get("account"))
|
||||||
|
|
||||||
if d.get("account") == party_account:
|
if d.get("account") == party_account:
|
||||||
d.update({rev_debit_or_credit: party_amount - tax_withholding_details.get("tax_amount")})
|
party_field = dr_or_cr
|
||||||
|
amount = net_total - tax_withholding_details.get("tax_amount")
|
||||||
|
if not d.get(party_field):
|
||||||
|
party_field = rev_dr_or_cr
|
||||||
|
amount = -1 * amount
|
||||||
|
|
||||||
|
d.update({party_field: amount})
|
||||||
|
|
||||||
if not accounts or tax_withholding_details.get("account_head") not in accounts:
|
if not accounts or tax_withholding_details.get("account_head") not in accounts:
|
||||||
self.append(
|
self.append(
|
||||||
"accounts",
|
"accounts",
|
||||||
{
|
{
|
||||||
"account": tax_withholding_details.get("account_head"),
|
"account": tax_withholding_details.get("account_head"),
|
||||||
rev_debit_or_credit: tax_withholding_details.get("tax_amount"),
|
dr_or_cr: tax_withholding_details.get("tax_amount"),
|
||||||
"against_account": parties[0],
|
"against_account": party,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
to_remove = [
|
to_remove = [
|
||||||
d
|
d
|
||||||
for d in self.get("accounts")
|
for d in self.get("accounts")
|
||||||
if not d.get(rev_debit_or_credit) and d.account == tax_withholding_details.get("account_head")
|
if not d.get(dr_or_cr) and d.account == tax_withholding_details.get("account_head")
|
||||||
]
|
]
|
||||||
|
|
||||||
for d in to_remove:
|
for d in to_remove:
|
||||||
|
|||||||
@@ -85,6 +85,9 @@ def get_party_details(inv):
|
|||||||
if inv.doctype == "Sales Invoice":
|
if inv.doctype == "Sales Invoice":
|
||||||
party_type = "Customer"
|
party_type = "Customer"
|
||||||
party = inv.customer
|
party = inv.customer
|
||||||
|
elif inv.doctype == "Journal Entry":
|
||||||
|
party_type = inv.party_type
|
||||||
|
party = inv.party
|
||||||
else:
|
else:
|
||||||
party_type = "Supplier"
|
party_type = "Supplier"
|
||||||
party = inv.supplier
|
party = inv.supplier
|
||||||
|
|||||||
Reference in New Issue
Block a user