refactor(treewide): formatting and ruff fixes, + manually enabled F401

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
This commit is contained in:
Akhil Narang
2024-03-27 11:37:26 +05:30
parent 9eeedd8515
commit 960ef14b7a
575 changed files with 4086 additions and 6211 deletions

View File

@@ -3,7 +3,7 @@
from json import loads
from typing import TYPE_CHECKING, List, Optional, Tuple
from typing import TYPE_CHECKING, Optional
import frappe
import frappe.defaults
@@ -31,7 +31,7 @@ from pypika.terms import ExistsCriterion
import erpnext
# imported to enable erpnext.accounts.utils.get_account_currency
from erpnext.accounts.doctype.account.account import get_account_currency # noqa
from erpnext.accounts.doctype.account.account import get_account_currency
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions
from erpnext.stock import get_warehouse_account_map
from erpnext.stock.utils import get_stock_value_on
@@ -83,9 +83,7 @@ def get_fiscal_years(
FY = DocType("Fiscal Year")
query = (
frappe.qb.from_(FY)
.select(FY.name, FY.year_start_date, FY.year_end_date)
.where(FY.disabled == 0)
frappe.qb.from_(FY).select(FY.name, FY.year_start_date, FY.year_end_date).where(FY.disabled == 0)
)
if fiscal_year:
@@ -132,9 +130,7 @@ def get_fiscal_years(
else:
return ((fy.name, fy.year_start_date, fy.year_end_date),)
error_msg = _("""{0} {1} is not in any active Fiscal Year""").format(
label, formatdate(transaction_date)
)
error_msg = _("""{0} {1} is not in any active Fiscal Year""").format(label, formatdate(transaction_date))
if company:
error_msg = _("""{0} for {1}""").format(error_msg, frappe.bold(company))
@@ -211,12 +207,12 @@ def get_balance_on(
acc = frappe.get_doc("Account", account)
try:
year_start_date = get_fiscal_year(date, company=company, verbose=0)[1]
get_fiscal_year(date, company=company, verbose=0)[1]
except FiscalYearError:
if getdate(date) > getdate(nowdate()):
# if fiscal year not found and the date is greater than today
# get fiscal year for today's date and its corresponding year start date
year_start_date = get_fiscal_year(nowdate(), verbose=1)[1]
get_fiscal_year(nowdate(), verbose=1)[1]
else:
# this indicates that it is a date older than any existing fiscal year.
# hence, assuming balance as 0.0
@@ -231,15 +227,14 @@ def get_balance_on(
cc = frappe.get_doc("Cost Center", cost_center)
if cc.is_group:
cond.append(
""" exists (
f""" exists (
select 1 from `tabCost Center` cc where cc.name = gle.cost_center
and cc.lft >= %s and cc.rgt <= %s
and cc.lft >= {cc.lft} and cc.rgt <= {cc.rgt}
)"""
% (cc.lft, cc.rgt)
)
else:
cond.append("""gle.cost_center = %s """ % (frappe.db.escape(cost_center),))
cond.append(f"""gle.cost_center = {frappe.db.escape(cost_center)} """)
if account:
if not (frappe.flags.ignore_account_permission or ignore_account_permission):
@@ -248,11 +243,10 @@ def get_balance_on(
# different filter for group and ledger - improved performance
if acc.is_group:
cond.append(
"""exists (
f"""exists (
select name from `tabAccount` ac where ac.name = gle.account
and ac.lft >= %s and ac.rgt <= %s
and ac.lft >= {acc.lft} and ac.rgt <= {acc.rgt}
)"""
% (acc.lft, acc.rgt)
)
# If group and currency same as company,
@@ -260,7 +254,7 @@ def get_balance_on(
if acc.account_currency == frappe.get_cached_value("Company", acc.company, "default_currency"):
in_account_currency = False
else:
cond.append("""gle.account = %s """ % (frappe.db.escape(account),))
cond.append(f"""gle.account = {frappe.db.escape(account)} """)
if account_type:
accounts = frappe.db.get_all(
@@ -279,8 +273,7 @@ def get_balance_on(
if party_type and party:
cond.append(
"""gle.party_type = %s and gle.party = %s """
% (frappe.db.escape(party_type), frappe.db.escape(party))
f"""gle.party_type = {frappe.db.escape(party_type)} and gle.party = {frappe.db.escape(party)} """
)
if company:
@@ -297,11 +290,9 @@ def get_balance_on(
bal = frappe.db.sql(
"""
SELECT {0}
SELECT {}
FROM `tabGL Entry` gle
WHERE {1}""".format(
select_field, " and ".join(cond)
),
WHERE {}""".format(select_field, " and ".join(cond)),
(precision, precision),
)[0][0]
# if bal is None, return 0
@@ -336,30 +327,25 @@ def get_count_on(account, fieldname, date):
# for pl accounts, get balance within a fiscal year
if acc.report_type == "Profit and Loss":
cond.append(
"posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % year_start_date
)
cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % year_start_date)
# different filter for group and ledger - improved performance
if acc.is_group:
cond.append(
"""exists (
f"""exists (
select name from `tabAccount` ac where ac.name = gle.account
and ac.lft >= %s and ac.rgt <= %s
and ac.lft >= {acc.lft} and ac.rgt <= {acc.rgt}
)"""
% (acc.lft, acc.rgt)
)
else:
cond.append("""gle.account = %s """ % (frappe.db.escape(account),))
cond.append(f"""gle.account = {frappe.db.escape(account)} """)
entries = frappe.db.sql(
"""
SELECT name, posting_date, account, party_type, party,debit,credit,
voucher_type, voucher_no, against_voucher_type, against_voucher
FROM `tabGL Entry` gle
WHERE {0}""".format(
" and ".join(cond)
),
WHERE {}""".format(" and ".join(cond)),
as_dict=True,
)
@@ -382,13 +368,11 @@ def get_count_on(account, fieldname, date):
or (gle.against_voucher == gle.voucher_no and gle.get(dr_or_cr) > 0)
):
payment_amount = frappe.db.sql(
"""
SELECT {0}
f"""
SELECT {select_fields}
FROM `tabGL Entry` gle
WHERE docstatus < 2 and posting_date <= %(date)s and against_voucher = %(voucher_no)s
and party = %(party)s and name != %(name)s""".format(
select_fields
),
and party = %(party)s and name != %(name)s""",
{"date": date, "voucher_no": gle.voucher_no, "party": gle.party, "name": gle.name},
)[0][0]
@@ -443,7 +427,7 @@ def add_cc(args=None):
args = make_tree_args(**args)
if args.parent_cost_center == args.company:
args.parent_cost_center = "{0} - {1}".format(
args.parent_cost_center = "{} - {}".format(
args.parent_cost_center, frappe.get_cached_value("Company", args.company, "abbr")
)
@@ -459,7 +443,7 @@ def add_cc(args=None):
def _build_dimensions_dict_for_exc_gain_loss(
entry: dict | object = None, active_dimensions: list = None
entry: dict | object = None, active_dimensions: list | None = None
):
dimensions_dict = frappe._dict()
if entry and active_dimensions:
@@ -537,7 +521,11 @@ def reconcile_against_document(
# Only update outstanding for newly linked vouchers
for entry in entries:
update_voucher_outstanding(
entry.against_voucher_type, entry.against_voucher, entry.account, entry.party_type, entry.party
entry.against_voucher_type,
entry.against_voucher,
entry.account,
entry.party_type,
entry.party,
)
# update advance paid in Advance Receivable/Payable doctypes
if update_advance_paid:
@@ -568,15 +556,15 @@ def check_if_advance_entry_modified(args):
.select(journal_acc[args.get("dr_or_cr")])
.where(
(journal_acc.account == args.get("account"))
& ((journal_acc.party_type == args.get("party_type")))
& ((journal_acc.party == args.get("party")))
& (journal_acc.party_type == args.get("party_type"))
& (journal_acc.party == args.get("party"))
& (
(journal_acc.reference_type.isnull())
| (journal_acc.reference_type.isin(["", "Sales Order", "Purchase Order"]))
)
& ((journal_entry.name == args.get("voucher_no")))
& ((journal_acc.name == args.get("voucher_detail_no")))
& ((journal_entry.docstatus == 1))
& (journal_entry.name == args.get("voucher_no"))
& (journal_acc.name == args.get("voucher_detail_no"))
& (journal_entry.docstatus == 1)
)
)
@@ -611,9 +599,7 @@ def check_if_advance_entry_modified(args):
def validate_allocated_amount(args):
precision = args.get("precision") or frappe.db.get_single_value(
"System Settings", "currency_precision"
)
precision = args.get("precision") or frappe.db.get_single_value("System Settings", "currency_precision")
if args.get("allocated_amount") < 0:
throw(_("Allocated amount cannot be negative"))
elif flt(args.get("allocated_amount"), precision) > flt(args.get("unadjusted_amount"), precision):
@@ -749,7 +735,7 @@ def update_reference_in_payment_entry(
def cancel_exchange_gain_loss_journal(
parent_doc: dict | object, referenced_dt: str = None, referenced_dn: str = None
parent_doc: dict | object, referenced_dt: str | None = None, referenced_dn: str | None = None
) -> None:
"""
Cancel Exchange Gain/Loss for Sales/Purchase Invoice, if they have any.
@@ -792,7 +778,7 @@ def cancel_exchange_gain_loss_journal(
def update_accounting_ledgers_after_reference_removal(
ref_type: str = None, ref_no: str = None, payment_name: str = None
ref_type: str | None = None, ref_no: str | None = None, payment_name: str | None = None
):
# General Ledger
gle = qb.DocType("GL Entry")
@@ -818,9 +804,7 @@ def update_accounting_ledgers_after_reference_removal(
.set(ple.modified, now())
.set(ple.modified_by, frappe.session.user)
.where(
(ple.against_voucher_type == ref_type)
& (ple.against_voucher_no == ref_no)
& (ple.delinked == 0)
(ple.against_voucher_type == ref_type) & (ple.against_voucher_no == ref_no) & (ple.delinked == 0)
)
)
@@ -837,7 +821,7 @@ def remove_ref_from_advance_section(ref_doc: object = None):
qb.from_(adv_type).delete().where(adv_type.parent == ref_doc.name).run()
def unlink_ref_doc_from_payment_entries(ref_doc: object = None, payment_name: str = None):
def unlink_ref_doc_from_payment_entries(ref_doc: object = None, payment_name: str | None = None):
remove_ref_doc_link_from_jv(ref_doc.doctype, ref_doc.name, payment_name)
remove_ref_doc_link_from_pe(ref_doc.doctype, ref_doc.name, payment_name)
update_accounting_ledgers_after_reference_removal(ref_doc.doctype, ref_doc.name, payment_name)
@@ -845,7 +829,7 @@ def unlink_ref_doc_from_payment_entries(ref_doc: object = None, payment_name: st
def remove_ref_doc_link_from_jv(
ref_type: str = None, ref_no: str = None, payment_name: str = None
ref_type: str | None = None, ref_no: str | None = None, payment_name: str | None = None
):
jea = qb.DocType("Journal Entry Account")
@@ -885,7 +869,7 @@ def convert_to_list(result):
def remove_ref_doc_link_from_pe(
ref_type: str = None, ref_no: str = None, payment_name: str = None
ref_type: str | None = None, ref_no: str | None = None, payment_name: str | None = None
):
per = qb.DocType("Payment Entry Reference")
pay = qb.DocType("Payment Entry")
@@ -893,9 +877,7 @@ def remove_ref_doc_link_from_pe(
linked_pe = (
qb.from_(per)
.select(per.parent)
.where(
(per.reference_doctype == ref_type) & (per.reference_name == ref_no) & (per.docstatus.lt(2))
)
.where((per.reference_doctype == ref_type) & (per.reference_name == ref_no) & (per.docstatus.lt(2)))
.run(as_list=1)
)
linked_pe = convert_to_list(linked_pe)
@@ -908,9 +890,7 @@ def remove_ref_doc_link_from_pe(
.set(per.allocated_amount, 0)
.set(per.modified, now())
.set(per.modified_by, frappe.session.user)
.where(
(per.docstatus.lt(2) & (per.reference_doctype == ref_type) & (per.reference_name == ref_no))
)
.where(per.docstatus.lt(2) & (per.reference_doctype == ref_type) & (per.reference_name == ref_no))
)
if payment_name:
@@ -925,13 +905,15 @@ def remove_ref_doc_link_from_pe(
# Call cancel on only removed reference
references = [
x for x in pe_doc.references if x.reference_doctype == ref_type and x.reference_name == ref_no
x
for x in pe_doc.references
if x.reference_doctype == ref_type and x.reference_name == ref_no
]
[pe_doc.make_advance_gl_entries(x, cancel=1) for x in references]
pe_doc.clear_unallocated_reference_document_rows()
pe_doc.validate_payment_type_with_outstanding()
except Exception as e:
except Exception:
msg = _("There were issues unlinking payment entry {0}.").format(pe_doc.name)
msg += "<br>"
msg += _("Please cancel payment entry manually first")
@@ -941,9 +923,7 @@ def remove_ref_doc_link_from_pe(
pay.base_total_allocated_amount, pe_doc.base_total_allocated_amount
).set(pay.unallocated_amount, pe_doc.unallocated_amount).set(pay.modified, now()).set(
pay.modified_by, frappe.session.user
).where(
pay.name == pe
).run()
).where(pay.name == pe).run()
frappe.msgprint(_("Payment Entries {0} are un-linked").format("\n".join(linked_pe)))
@@ -977,9 +957,10 @@ def fix_total_debit_credit():
dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit"
frappe.db.sql(
"""update `tabGL Entry` set %s = %s + %s
where voucher_type = %s and voucher_no = %s and %s > 0 limit 1"""
% (dr_or_cr, dr_or_cr, "%s", "%s", "%s", dr_or_cr),
"""update `tabGL Entry` set {} = {} + {}
where voucher_type = {} and voucher_no = {} and {} > 0 limit 1""".format(
dr_or_cr, dr_or_cr, "%s", "%s", "%s", dr_or_cr
),
(d.diff, d.voucher_type, d.voucher_no),
)
@@ -1022,7 +1003,6 @@ def get_outstanding_invoices(
limit=None, # passed by reconciliation tool
voucher_no=None, # filter passed by reconciliation tool
):
ple = qb.DocType("Payment Ledger Entry")
outstanding_invoices = []
precision = frappe.get_precision("Sales Invoice", "outstanding_amount") or 2
@@ -1085,15 +1065,11 @@ def get_outstanding_invoices(
)
)
outstanding_invoices = sorted(
outstanding_invoices, key=lambda k: k["due_date"] or getdate(nowdate())
)
outstanding_invoices = sorted(outstanding_invoices, key=lambda k: k["due_date"] or getdate(nowdate()))
return outstanding_invoices
def get_account_name(
account_type=None, root_type=None, is_group=None, account_currency=None, company=None
):
def get_account_name(account_type=None, root_type=None, is_group=None, account_currency=None, company=None):
"""return account based on matching conditions"""
return frappe.db.get_value(
"Account",
@@ -1122,7 +1098,7 @@ def get_children(doctype, parent, company, is_root=False):
fields = ["name as value", "is_group as expandable"]
filters = [["docstatus", "<", 2]]
filters.append(['ifnull(`{0}`,"")'.format(parent_fieldname), "=", "" if is_root else parent])
filters.append([f'ifnull(`{parent_fieldname}`,"")', "=", "" if is_root else parent])
if is_root:
fields += ["root_type", "report_type", "account_currency"] if doctype == "Account" else []
@@ -1142,7 +1118,6 @@ def get_children(doctype, parent, company, is_root=False):
@frappe.whitelist()
def get_account_balances(accounts, company):
if isinstance(accounts, str):
accounts = loads(accounts)
@@ -1153,9 +1128,7 @@ def get_account_balances(accounts, company):
for account in accounts:
account["company_currency"] = company_currency
account["balance"] = flt(
get_balance_on(account["value"], in_account_currency=False, company=company)
)
account["balance"] = flt(get_balance_on(account["value"], in_account_currency=False, company=company))
if account["account_currency"] and account["account_currency"] != company_currency:
account["balance_in_account_currency"] = flt(get_balance_on(account["value"], company=company))
@@ -1305,20 +1278,17 @@ def update_gl_entries_after(
warehouse_account=None,
company=None,
):
stock_vouchers = get_future_stock_vouchers(
posting_date, posting_time, for_warehouses, for_items, company
)
stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items, company)
repost_gle_for_stock_vouchers(stock_vouchers, posting_date, company, warehouse_account)
def repost_gle_for_stock_vouchers(
stock_vouchers: List[Tuple[str, str]],
stock_vouchers: list[tuple[str, str]],
posting_date: str,
company: Optional[str] = None,
company: str | None = None,
warehouse_account=None,
repost_doc: Optional["RepostItemValuation"] = None,
):
from erpnext.accounts.general_ledger import toggle_debit_credit_if_negative
if not stock_vouchers:
@@ -1364,16 +1334,12 @@ def repost_gle_for_stock_vouchers(
def _delete_pl_entries(voucher_type, voucher_no):
ple = qb.DocType("Payment Ledger Entry")
qb.from_(ple).delete().where(
(ple.voucher_type == voucher_type) & (ple.voucher_no == voucher_no)
).run()
qb.from_(ple).delete().where((ple.voucher_type == voucher_type) & (ple.voucher_no == voucher_no)).run()
def _delete_gl_entries(voucher_type, voucher_no):
gle = qb.DocType("GL Entry")
qb.from_(gle).delete().where(
(gle.voucher_type == voucher_type) & (gle.voucher_no == voucher_no)
).run()
qb.from_(gle).delete().where((gle.voucher_type == voucher_type) & (gle.voucher_no == voucher_no)).run()
def _delete_accounting_ledger_entries(voucher_type, voucher_no):
@@ -1384,9 +1350,7 @@ def _delete_accounting_ledger_entries(voucher_type, voucher_no):
_delete_pl_entries(voucher_type, voucher_no)
def sort_stock_vouchers_by_posting_date(
stock_vouchers: List[Tuple[str, str]]
) -> List[Tuple[str, str]]:
def sort_stock_vouchers_by_posting_date(stock_vouchers: list[tuple[str, str]]) -> list[tuple[str, str]]:
sle = frappe.qb.DocType("Stock Ledger Entry")
voucher_nos = [v[1] for v in stock_vouchers]
@@ -1407,10 +1371,7 @@ def sort_stock_vouchers_by_posting_date(
return sorted_vouchers
def get_future_stock_vouchers(
posting_date, posting_time, for_warehouses=None, for_items=None, company=None
):
def get_future_stock_vouchers(posting_date, posting_time, for_warehouses=None, for_items=None, company=None):
values = []
condition = ""
if for_items:
@@ -1426,16 +1387,14 @@ def get_future_stock_vouchers(
values.append(company)
future_stock_vouchers = frappe.db.sql(
"""select distinct sle.voucher_type, sle.voucher_no
f"""select distinct sle.voucher_type, sle.voucher_no
from `tabStock Ledger Entry` sle
where
timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s)
and is_cancelled = 0
{condition}
order by timestamp(sle.posting_date, sle.posting_time) asc, creation asc for update""".format(
condition=condition
),
tuple([posting_date, posting_time] + values),
order by timestamp(sle.posting_date, sle.posting_time) asc, creation asc for update""",
tuple([posting_date, posting_time, *values]),
as_dict=True,
)
@@ -1462,9 +1421,8 @@ def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
select name, account, credit, debit, cost_center, project, voucher_type, voucher_no
from `tabGL Entry`
where
posting_date >= %s and voucher_no in (%s)"""
% ("%s", ", ".join(["%s"] * len(voucher_nos))),
tuple([posting_date] + voucher_nos),
posting_date >= {} and voucher_no in ({})""".format("%s", ", ".join(["%s"] * len(voucher_nos))),
tuple([posting_date, *voucher_nos]),
as_dict=1,
)
@@ -1503,16 +1461,16 @@ def compare_existing_and_expected_gle(existing_gle, expected_gle, precision):
def get_stock_accounts(company, voucher_type=None, voucher_no=None):
stock_accounts = [
d.name
for d in frappe.db.get_all(
"Account", {"account_type": "Stock", "company": company, "is_group": 0}
)
for d in frappe.db.get_all("Account", {"account_type": "Stock", "company": company, "is_group": 0})
]
if voucher_type and voucher_no:
if voucher_type == "Journal Entry":
stock_accounts = [
d.account
for d in frappe.db.get_all(
"Journal Entry Account", {"parent": voucher_no, "account": ["in", stock_accounts]}, "account"
"Journal Entry Account",
{"parent": voucher_no, "account": ["in", stock_accounts]},
"account",
)
]
@@ -1521,7 +1479,11 @@ def get_stock_accounts(company, voucher_type=None, voucher_no=None):
d.account
for d in frappe.db.get_all(
"GL Entry",
{"voucher_type": voucher_type, "voucher_no": voucher_no, "account": ["in", stock_accounts]},
{
"voucher_type": voucher_type,
"voucher_no": voucher_no,
"account": ["in", stock_accounts],
},
"account",
)
]
@@ -1552,9 +1514,7 @@ def get_stock_and_account_balance(account=None, posting_date=None, company=None)
def get_journal_entry(account, stock_adjustment_account, amount):
db_or_cr_warehouse_account = (
"credit_in_account_currency" if amount < 0 else "debit_in_account_currency"
)
db_or_cr_warehouse_account = "credit_in_account_currency" if amount < 0 else "debit_in_account_currency"
db_or_cr_stock_adjustment_account = (
"debit_in_account_currency" if amount < 0 else "credit_in_account_currency"
)
@@ -1575,7 +1535,7 @@ def check_and_delete_linked_reports(report):
frappe.delete_doc("Desktop Icon", icon)
def create_err_and_its_journals(companies: list = None) -> None:
def create_err_and_its_journals(companies: list | None = None) -> None:
if companies:
for company in companies:
err = frappe.new_doc("Exchange Rate Revaluation")
@@ -1632,9 +1592,7 @@ def get_payment_ledger_entries(gl_entries, cancel=0):
accounts_with_types = (
qb.from_(account)
.select(account.name, account.account_type)
.where(
(account.account_type.isin(["Receivable", "Payable"]) & (account.company.isin(companies)))
)
.where(account.account_type.isin(["Receivable", "Payable"]) & (account.company.isin(companies)))
.run(as_dict=True)
)
receivable_or_payable_accounts = [y.name for y in accounts_with_types]
@@ -1701,7 +1659,6 @@ def create_payment_ledger_entry(
ple_map = get_payment_ledger_entries(gl_entries, cancel=cancel)
for entry in ple_map:
ple = frappe.get_doc(entry)
if cancel:
@@ -1779,7 +1736,7 @@ def delink_original_entry(pl_entry, partial_cancel=False):
query.run()
class QueryPaymentLedger(object):
class QueryPaymentLedger:
"""
Helper Class for Querying Payment Ledger Entry
"""
@@ -1947,7 +1904,8 @@ class QueryPaymentLedger(object):
Table("outstanding").amount_in_account_currency.as_("outstanding_in_account_currency"),
(Table("vouchers").amount - Table("outstanding").amount).as_("paid_amount"),
(
Table("vouchers").amount_in_account_currency - Table("outstanding").amount_in_account_currency
Table("vouchers").amount_in_account_currency
- Table("outstanding").amount_in_account_currency
).as_("paid_amount_in_account_currency"),
Table("vouchers").due_date,
Table("vouchers").currency,