mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-27 08:54:45 +00:00
refactor: proper variable naming
This commit is contained in:
@@ -360,36 +360,39 @@ class JournalEntry(AccountsController):
|
|||||||
|
|
||||||
party = None
|
party = None
|
||||||
party_type = None
|
party_type = None
|
||||||
party_account = None
|
|
||||||
party_row = None
|
party_row = None
|
||||||
|
|
||||||
for d in self.get("accounts"):
|
for row in self.get("accounts"):
|
||||||
if d.party and party and d.party != party:
|
if row.party_type in ("Customer", "Supplier") and row.party:
|
||||||
frappe.throw(_("Cannot apply TDS against multiple parties in one entry"))
|
if party and row.party != party:
|
||||||
|
frappe.throw(_("Cannot apply TDS against multiple parties in one entry"))
|
||||||
|
|
||||||
if d.party_type in ("Customer", "Supplier") and d.party:
|
if not party:
|
||||||
party = d.party
|
party = row.party
|
||||||
party_type = d.party_type
|
party_type = row.party_type
|
||||||
party_account = d.account
|
party_row = row
|
||||||
party_row = d
|
|
||||||
break
|
|
||||||
|
|
||||||
if not (party and party_type):
|
if not party:
|
||||||
return
|
return
|
||||||
|
|
||||||
# debit or credit based on party type
|
# debit or credit based on party type
|
||||||
dr_or_cr = "credit" if party_type == "Supplier" else "debit"
|
dr_cr = "credit" if party_type == "Supplier" else "debit"
|
||||||
rev_dr_or_cr = "debit" if party_type == "Supplier" else "credit"
|
rev_dr_cr = "debit" if party_type == "Supplier" else "credit"
|
||||||
|
|
||||||
|
precision = self.precision(dr_cr, party_row)
|
||||||
|
|
||||||
# net total in company currency.
|
# net total in company currency.
|
||||||
net_total_in_company_currency = sum(
|
net_total = flt(
|
||||||
d.get(dr_or_cr) - d.get(rev_dr_or_cr)
|
sum(
|
||||||
for d in self.get("accounts")
|
d.get(dr_cr) - d.get(rev_dr_cr)
|
||||||
if d.party == party and d.party_type == party_type
|
for d in self.get("accounts")
|
||||||
|
if d.party == party and d.party_type == party_type
|
||||||
|
),
|
||||||
|
precision,
|
||||||
)
|
)
|
||||||
|
|
||||||
# only apply tds if net total is positive
|
# only apply tds if net total is positive
|
||||||
if net_total_in_company_currency <= 0:
|
if net_total <= 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
inv = frappe._dict(
|
inv = frappe._dict(
|
||||||
@@ -399,87 +402,94 @@ class JournalEntry(AccountsController):
|
|||||||
"doctype": self.doctype,
|
"doctype": self.doctype,
|
||||||
"company": self.company,
|
"company": self.company,
|
||||||
"posting_date": self.posting_date,
|
"posting_date": self.posting_date,
|
||||||
"tax_withholding_net_total": net_total_in_company_currency,
|
"tax_withholding_net_total": net_total,
|
||||||
"base_tax_withholding_net_total": net_total_in_company_currency,
|
"base_tax_withholding_net_total": net_total,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
tax_withholding_details = get_party_tax_withholding_details(inv, self.tax_withholding_category)
|
tax_details = get_party_tax_withholding_details(inv, self.tax_withholding_category)
|
||||||
|
|
||||||
if not tax_withholding_details:
|
if not tax_details:
|
||||||
return
|
return
|
||||||
|
|
||||||
tds_account = tax_withholding_details.get("account_head")
|
tax_acc = tax_details.get("account_head")
|
||||||
company_default_currency = frappe.get_cached_value("Company", self.company, "default_currency")
|
acc_curr = get_account_currency(tax_acc)
|
||||||
tds_account_currency = get_account_currency(tds_account)
|
comp_curr = frappe.get_cached_value("Company", self.company, "default_currency")
|
||||||
|
|
||||||
tds_exch_rate = get_exchange_rate(tds_account_currency, company_default_currency, self.posting_date)
|
exch_rate = get_exchange_rate(acc_curr, comp_curr, self.posting_date)
|
||||||
|
|
||||||
tds_amt_in_company_currency = tax_withholding_details.get("tax_amount")
|
tax_amt_in_comp_curr = flt(tax_details.get("tax_amount"), precision)
|
||||||
tds_amt_in_account_currency = tds_amt_in_company_currency / tds_exch_rate
|
tax_amt_in_acc_curr = flt(tax_amt_in_comp_curr / exch_rate, precision)
|
||||||
tds_amt_in_party_currency = tds_amt_in_company_currency / party_row.get("exchange_rate", 1)
|
tax_amt_in_party_curr = flt(tax_amt_in_comp_curr / party_row.get("exchange_rate", 1), precision)
|
||||||
|
|
||||||
# Update or create tax account row
|
# Update or create tax account row
|
||||||
tax_row = None
|
tax_row = None
|
||||||
for d in self.get("accounts"):
|
|
||||||
if d.account == tds_account:
|
for row in self.get("accounts"):
|
||||||
tax_row = d
|
if row.account == tax_acc:
|
||||||
|
tax_row = row
|
||||||
break
|
break
|
||||||
|
|
||||||
if not tax_row:
|
if not tax_row:
|
||||||
tax_row = self.append(
|
tax_row = self.append(
|
||||||
"accounts",
|
"accounts",
|
||||||
{
|
{
|
||||||
"account": tds_account,
|
"account": tax_acc,
|
||||||
"account_currency": tds_account_currency,
|
"account_currency": acc_curr,
|
||||||
"exchange_rate": tds_exch_rate,
|
"exchange_rate": exch_rate,
|
||||||
"is_tax_withholding_account": 1,
|
"is_tax_withholding_account": 1,
|
||||||
"against_account": party_account,
|
"debit": 0,
|
||||||
|
"credit": 0,
|
||||||
|
"debit_in_account_currency": 0,
|
||||||
|
"credit_in_account_currency": 0,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# TDS will always be credit
|
# TDS will always be credit
|
||||||
tax_row.update(
|
tax_row.update(
|
||||||
{
|
{
|
||||||
"credit": flt(tds_amt_in_company_currency, self.precision("credit")),
|
"credit": tax_amt_in_comp_curr,
|
||||||
"credit_in_account_currency": flt(
|
"credit_in_account_currency": tax_amt_in_acc_curr,
|
||||||
tds_amt_in_account_currency, self.precision("credit_in_account_currency")
|
|
||||||
),
|
|
||||||
"debit": 0,
|
"debit": 0,
|
||||||
"debit_in_account_currency": 0,
|
"debit_in_account_currency": 0,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
party_field = dr_or_cr
|
# update party row
|
||||||
|
party_field = dr_cr
|
||||||
|
|
||||||
|
# sometime user may enter amount in opposite field as negative value
|
||||||
if not party_row.get(party_field):
|
if not party_row.get(party_field):
|
||||||
party_field = rev_dr_or_cr
|
party_field = rev_dr_cr
|
||||||
tds_amt_in_company_currency = -1 * tds_amt_in_company_currency
|
tax_amt_in_comp_curr *= -1
|
||||||
tds_amt_in_party_currency = -1 * tds_amt_in_party_currency
|
tax_amt_in_party_curr *= -1
|
||||||
|
|
||||||
if dr_or_cr == "debit":
|
# for customer amount will be added.
|
||||||
# for customer,increase the receivable amount
|
if dr_cr == "debit":
|
||||||
party_row.update(
|
tax_amt_in_comp_curr *= -1
|
||||||
{
|
tax_amt_in_party_curr *= -1
|
||||||
party_field: flt(party_row.get(party_field, 0)) + tds_amt_in_company_currency,
|
|
||||||
f"{party_field}_in_account_currency": flt(
|
|
||||||
party_row.get(f"{party_field}_in_account_currency", 0)
|
|
||||||
)
|
|
||||||
+ tds_amt_in_party_currency,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# for supplier,decrease the payable amount
|
|
||||||
party_row.update(
|
|
||||||
{
|
|
||||||
party_field: flt(party_row.get(party_field, 0)) - tds_amt_in_company_currency,
|
|
||||||
f"{party_field}_in_account_currency": flt(
|
|
||||||
party_row.get(f"{party_field}_in_account_currency", 0)
|
|
||||||
)
|
|
||||||
- tds_amt_in_party_currency,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Recalculate totals
|
party_field_in_acc_curr = f"{party_field}_in_account_currency"
|
||||||
|
party_amt_in_comp_curr = flt(party_row.get(party_field) - tax_amt_in_comp_curr, precision)
|
||||||
|
party_amt_in_acc_curr = flt(party_row.get(party_field_in_acc_curr) - tax_amt_in_party_curr, precision)
|
||||||
|
|
||||||
|
party_row.update(
|
||||||
|
{
|
||||||
|
party_field: party_amt_in_comp_curr,
|
||||||
|
party_field_in_acc_curr: party_amt_in_acc_curr,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# remove other tds rows if any
|
||||||
|
dup_rows = []
|
||||||
|
for row in self.get("accounts"):
|
||||||
|
if row.account == tax_acc and row != tax_row:
|
||||||
|
dup_rows.append(row)
|
||||||
|
|
||||||
|
for row in dup_rows:
|
||||||
|
self.remove(row)
|
||||||
|
|
||||||
|
# recalculate totals
|
||||||
self.set_amounts_in_company_currency()
|
self.set_amounts_in_company_currency()
|
||||||
self.set_total_debit_credit()
|
self.set_total_debit_credit()
|
||||||
self.set_against_account()
|
self.set_against_account()
|
||||||
|
|||||||
Reference in New Issue
Block a user