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 c28d19cf7f
commit 4d34b1ead7
618 changed files with 4188 additions and 6384 deletions

View File

@@ -95,7 +95,7 @@ force_item_fields = (
class AccountsController(TransactionBase):
def __init__(self, *args, **kwargs):
super(AccountsController, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
def get_print_settings(self):
print_setting_fields = []
@@ -200,14 +200,13 @@ class AccountsController(TransactionBase):
self.validate_return_against_account()
if self.doctype in ["Purchase Invoice", "Sales Invoice"]:
if invalid_advances := [
x for x in self.advances if not x.reference_type or not x.reference_name
]:
if invalid_advances := [x for x in self.advances if not x.reference_type or not x.reference_name]:
frappe.throw(
_(
"Rows: {0} in {1} section are Invalid. Reference Name should point to a valid Payment Entry or Journal Entry."
).format(
frappe.bold(comma_and([x.idx for x in invalid_advances])), frappe.bold(_("Advance Payments"))
frappe.bold(comma_and([x.idx for x in invalid_advances])),
frappe.bold(_("Advance Payments")),
)
)
@@ -352,9 +351,7 @@ class AccountsController(TransactionBase):
)
def validate_return_against_account(self):
if (
self.doctype in ["Sales Invoice", "Purchase Invoice"] and self.is_return and self.return_against
):
if self.doctype in ["Sales Invoice", "Purchase Invoice"] and self.is_return and self.return_against:
cr_dr_account_field = "debit_to" if self.doctype == "Sales Invoice" else "credit_to"
cr_dr_account_label = "Debit To" if self.doctype == "Sales Invoice" else "Credit To"
cr_dr_account = self.get(cr_dr_account_field)
@@ -387,11 +384,7 @@ class AccountsController(TransactionBase):
item.set(field_map.get(self.doctype), default_deferred_account)
def validate_auto_repeat_subscription_dates(self):
if (
self.get("from_date")
and self.get("to_date")
and getdate(self.from_date) > getdate(self.to_date)
):
if self.get("from_date") and self.get("to_date") and getdate(self.from_date) > getdate(self.to_date):
frappe.throw(_("To Date cannot be before From Date"), title=_("Invalid Auto Repeat Date"))
def validate_deferred_start_and_end_date(self):
@@ -399,11 +392,15 @@ class AccountsController(TransactionBase):
if d.get("enable_deferred_revenue") or d.get("enable_deferred_expense"):
if not (d.service_start_date and d.service_end_date):
frappe.throw(
_("Row #{0}: Service Start and End Date is required for deferred accounting").format(d.idx)
_("Row #{0}: Service Start and End Date is required for deferred accounting").format(
d.idx
)
)
elif getdate(d.service_start_date) > getdate(d.service_end_date):
frappe.throw(
_("Row #{0}: Service Start Date cannot be greater than Service End Date").format(d.idx)
_("Row #{0}: Service Start Date cannot be greater than Service End Date").format(
d.idx
)
)
elif getdate(self.posting_date) > getdate(d.service_end_date):
frappe.throw(
@@ -462,7 +459,9 @@ class AccountsController(TransactionBase):
if not self.cash_bank_account:
# show message that the amount is not paid
frappe.throw(
_("Note: Payment Entry will not be created since 'Cash or Bank Account' was not specified")
_(
"Note: Payment Entry will not be created since 'Cash or Bank Account' was not specified"
)
)
if cint(self.is_return) and self.grand_total > self.paid_amount:
@@ -506,7 +505,11 @@ class AccountsController(TransactionBase):
if date_field and self.get(date_field):
validate_fiscal_year(
self.get(date_field), self.fiscal_year, self.company, self.meta.get_label(date_field), self
self.get(date_field),
self.fiscal_year,
self.company,
self.meta.get_label(date_field),
self,
)
def validate_party_accounts(self):
@@ -576,7 +579,9 @@ class AccountsController(TransactionBase):
if tax_updated:
frappe.msgprint(
_("Disabled tax included prices since this {} is an internal transfer").format(self.doctype),
_("Disabled tax included prices since this {} is an internal transfer").format(
self.doctype
),
alert=1,
)
@@ -667,7 +672,7 @@ class AccountsController(TransactionBase):
parent_dict[fieldname] = self.get(fieldname)
if self.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
document_type = "{} Item".format(self.doctype)
document_type = f"{self.doctype} Item"
parent_dict.update({"document_type": document_type})
# party_name field used for customer in quotation
@@ -708,7 +713,9 @@ class AccountsController(TransactionBase):
if item.get(fieldname) is None or fieldname in force_item_fields:
item.set(fieldname, value)
elif fieldname in ["cost_center", "conversion_factor"] and not item.get(fieldname):
elif fieldname in ["cost_center", "conversion_factor"] and not item.get(
fieldname
):
item.set(fieldname, value)
elif fieldname == "serial_no":
@@ -744,7 +751,8 @@ class AccountsController(TransactionBase):
# Items add via promotional scheme may not have cost center set
if hasattr(item, "cost_center") and not item.get("cost_center"):
item.set(
"cost_center", self.get("cost_center") or erpnext.get_default_cost_center(self.company)
"cost_center",
self.get("cost_center") or erpnext.get_default_cost_center(self.company),
)
if ret.get("pricing_rules"):
@@ -871,9 +879,7 @@ class AccountsController(TransactionBase):
if self.taxes_and_charges and frappe.get_cached_value(
taxes_and_charges_doctype, self.taxes_and_charges, "disabled"
):
frappe.throw(
_("{0} '{1}' is disabled").format(taxes_and_charges_doctype, self.taxes_and_charges)
)
frappe.throw(_("{0} '{1}' is disabled").format(taxes_and_charges_doctype, self.taxes_and_charges))
def validate_tax_account_company(self):
for d in self.get("taxes"):
@@ -998,9 +1004,8 @@ class AccountsController(TransactionBase):
self.set(parentfield, self.get(parentfield, {"allocated_amount": ["not in", [0, None, ""]]}))
frappe.db.sql(
"""delete from `tab%s` where parentfield=%s and parent = %s
and allocated_amount = 0"""
% (childtype, "%s", "%s"),
"""delete from `tab{}` where parentfield={} and parent = {}
and allocated_amount = 0""".format(childtype, "%s", "%s"),
(parentfield, self.name),
)
@@ -1088,9 +1093,7 @@ class AccountsController(TransactionBase):
return res
def is_inclusive_tax(self):
is_inclusive = cint(
frappe.db.get_single_value("Accounts Settings", "show_inclusive_tax_in_print")
)
is_inclusive = cint(frappe.db.get_single_value("Accounts Settings", "show_inclusive_tax_in_print"))
if is_inclusive:
is_inclusive = 0
@@ -1133,7 +1136,6 @@ class AccountsController(TransactionBase):
for d in self.get("advances"):
advance_exchange_rate = d.ref_exchange_rate
if d.allocated_amount and self.conversion_rate != advance_exchange_rate:
base_allocated_amount_in_ref_rate = advance_exchange_rate * d.allocated_amount
base_allocated_amount_in_inv_rate = self.conversion_rate * d.allocated_amount
difference = base_allocated_amount_in_ref_rate - base_allocated_amount_in_inv_rate
@@ -1179,7 +1181,7 @@ class AccountsController(TransactionBase):
return False
def make_exchange_gain_loss_journal(
self, args: dict = None, dimensions_dict: dict = None
self, args: dict | None = None, dimensions_dict: dict | None = None
) -> None:
"""
Make Exchange Gain/Loss journal for Invoices and Payments
@@ -1199,7 +1201,6 @@ class AccountsController(TransactionBase):
flt(arg.get("difference_amount", 0), precision) != 0
or flt(arg.get("exchange_gain_loss", 0), precision) != 0
) and arg.get("difference_account"):
party_account = arg.get("account")
gain_loss_account = arg.get("difference_account")
difference_amount = arg.get("difference_amount") or arg.get("exchange_gain_loss")
@@ -1250,8 +1251,8 @@ class AccountsController(TransactionBase):
gain_loss_to_book = [x for x in self.references if x.exchange_gain_loss != 0]
booked = []
if gain_loss_to_book:
vtypes = [x.reference_doctype for x in gain_loss_to_book]
vnames = [x.reference_name for x in gain_loss_to_book]
[x.reference_doctype for x in gain_loss_to_book]
[x.reference_name for x in gain_loss_to_book]
je = qb.DocType("Journal Entry")
jea = qb.DocType("Journal Entry Account")
parents = (
@@ -1391,7 +1392,9 @@ class AccountsController(TransactionBase):
"allocated_amount": flt(d.allocated_amount),
"precision": d.precision("advance_amount"),
"exchange_rate": (
self.conversion_rate if self.party_account_currency != self.company_currency else 1
self.conversion_rate
if self.party_account_currency != self.company_currency
else 1
),
"grand_total": (
self.base_grand_total
@@ -1574,9 +1577,12 @@ class AccountsController(TransactionBase):
"account": item.discount_account,
"against": supplier_or_customer,
dr_or_cr: flt(
discount_amount * self.get("conversion_rate"), item.precision("discount_amount")
discount_amount * self.get("conversion_rate"),
item.precision("discount_amount"),
),
dr_or_cr + "_in_account_currency": flt(
discount_amount, item.precision("discount_amount")
),
dr_or_cr + "_in_account_currency": flt(discount_amount, item.precision("discount_amount")),
"cost_center": item.cost_center,
"project": item.project,
},
@@ -1592,10 +1598,12 @@ class AccountsController(TransactionBase):
"account": income_or_expense_account,
"against": supplier_or_customer,
rev_dr_cr: flt(
discount_amount * self.get("conversion_rate"), item.precision("discount_amount")
discount_amount * self.get("conversion_rate"),
item.precision("discount_amount"),
),
rev_dr_cr + "_in_account_currency": flt(
discount_amount, item.precision("discount_amount")
),
rev_dr_cr
+ "_in_account_currency": flt(discount_amount, item.precision("discount_amount")),
"cost_center": item.cost_center,
"project": item.project or self.project,
},
@@ -1635,9 +1643,7 @@ class AccountsController(TransactionBase):
total_overbilled_amt = 0.0
reference_names = [d.get(item_ref_dn) for d in self.get("items") if d.get(item_ref_dn)]
reference_details = self.get_billing_reference_details(
reference_names, ref_dt + " Item", based_on
)
reference_details = self.get_billing_reference_details(reference_names, ref_dt + " Item", based_on)
for item in self.get("items"):
if not item.get(item_ref_dn):
@@ -1823,17 +1829,13 @@ class AccountsController(TransactionBase):
def raise_missing_debit_credit_account_error(self, party_type, party):
"""Raise an error if debit to/credit to account does not exist."""
db_or_cr = (
frappe.bold("Debit To") if self.doctype == "Sales Invoice" else frappe.bold("Credit To")
)
db_or_cr = frappe.bold("Debit To") if self.doctype == "Sales Invoice" else frappe.bold("Credit To")
rec_or_pay = "Receivable" if self.doctype == "Sales Invoice" else "Payable"
link_to_party = frappe.utils.get_link_to_form(party_type, party)
link_to_company = frappe.utils.get_link_to_form("Company", self.company)
message = _("{0} Account not found against Customer {1}.").format(
db_or_cr, frappe.bold(party) or ""
)
message = _("{0} Account not found against Customer {1}.").format(db_or_cr, frappe.bold(party) or "")
message += "<br>" + _("Please set one of the following:") + "<br>"
message += (
"<br><ul><li>"
@@ -1886,7 +1888,6 @@ class AccountsController(TransactionBase):
and party_account_currency != self.company_currency
and self.currency != party_account_currency
):
frappe.throw(
_("Accounting Entry for {0}: {1} can only be made in currency: {2}").format(
party_type, party, party_account_currency
@@ -1907,9 +1908,7 @@ class AccountsController(TransactionBase):
party_type, party = self.get_party()
party_gle_currency = get_party_gle_currency(party_type, party, self.company)
party_account = (
self.get("debit_to") if self.doctype == "Sales Invoice" else self.get("credit_to")
)
party_account = self.get("debit_to") if self.doctype == "Sales Invoice" else self.get("credit_to")
party_account_currency = get_account_currency(party_account)
allow_multi_currency_invoices_against_single_party_account = frappe.db.get_singles_value(
"Accounts Settings", "allow_multi_currency_invoices_against_single_party_account"
@@ -1932,10 +1931,8 @@ class AccountsController(TransactionBase):
consider_for_total_advance = True
if adv.reference_name == linked_doc_name:
frappe.db.sql(
"""delete from `tab{0} Advance`
where name = %s""".format(
self.doctype
),
f"""delete from `tab{self.doctype} Advance`
where name = %s""",
adv.name,
)
consider_for_total_advance = False
@@ -2152,7 +2149,9 @@ class AccountsController(TransactionBase):
for d in self.get("payment_schedule"):
if self.doctype == "Sales Order" and getdate(d.due_date) < getdate(self.transaction_date):
frappe.throw(
_("Row {0}: Due Date in the Payment Terms table cannot be before Posting Date").format(d.idx)
_("Row {0}: Due Date in the Payment Terms table cannot be before Posting Date").format(
d.idx
)
)
elif d.due_date in dates:
li.append(_("{0} in row {1}").format(d.due_date, d.idx))
@@ -2160,9 +2159,7 @@ class AccountsController(TransactionBase):
if li:
duplicates = "<br>" + "<br>".join(li)
frappe.throw(
_("Rows with duplicate due dates in other rows were found: {0}").format(duplicates)
)
frappe.throw(_("Rows with duplicate due dates in other rows were found: {0}").format(duplicates))
def validate_payment_schedule_amount(self):
if self.doctype == "Sales Invoice" and self.is_pos:
@@ -2285,7 +2282,7 @@ class AccountsController(TransactionBase):
jv.voucher_type = "Journal Entry"
jv.posting_date = self.posting_date
jv.company = self.company
jv.remark = "Adjustment for {} {}".format(self.doctype, self.name)
jv.remark = f"Adjustment for {self.doctype} {self.name}"
reconcilation_entry = frappe._dict()
advance_entry = frappe._dict()
@@ -2295,9 +2292,7 @@ class AccountsController(TransactionBase):
reconcilation_entry.party = secondary_party
reconcilation_entry.reference_type = self.doctype
reconcilation_entry.reference_name = self.name
reconcilation_entry.cost_center = self.cost_center or erpnext.get_default_cost_center(
self.company
)
reconcilation_entry.cost_center = self.cost_center or erpnext.get_default_cost_center(self.company)
advance_entry.account = primary_account
advance_entry.party_type = primary_party_type
@@ -2343,7 +2338,7 @@ class AccountsController(TransactionBase):
def check_if_fields_updated(self, fields_to_check, child_tables):
# Check if any field affecting accounting entry is altered
doc_before_update = self.get_doc_before_save()
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
accounting_dimensions = [*get_accounting_dimensions(), "cost_center", "project"]
# Check if opening entry check updated
needs_repost = doc_before_update.get("is_opening") != self.is_opening
@@ -2383,9 +2378,7 @@ class AccountsController(TransactionBase):
@frappe.whitelist()
def get_tax_rate(account_head):
return frappe.get_cached_value(
"Account", account_head, ["tax_rate", "account_name"], as_dict=True
)
return frappe.get_cached_value("Account", account_head, ["tax_rate", "account_name"], as_dict=True)
@frappe.whitelist()
@@ -2415,7 +2408,7 @@ def get_taxes_and_charges(master_doctype, master_name):
tax_master = frappe.get_doc(master_doctype, master_name)
taxes_and_charges = []
for i, tax in enumerate(tax_master.get("taxes")):
for _i, tax in enumerate(tax_master.get("taxes")):
tax = tax.as_dict()
for fieldname in default_fields + child_table_fields:
@@ -2534,9 +2527,7 @@ def set_balance_in_account_currency(
):
if (not conversion_rate) and (account_currency != company_currency):
frappe.throw(
_("Account: {0} with currency: {1} can not be selected").format(
gl_dict.account, account_currency
)
_("Account: {0} with currency: {1} can not be selected").format(gl_dict.account, account_currency)
)
gl_dict["account_currency"] = (
@@ -2546,9 +2537,7 @@ def set_balance_in_account_currency(
# set debit/credit in account currency if not provided
if flt(gl_dict.debit) and not flt(gl_dict.debit_in_account_currency):
gl_dict.debit_in_account_currency = (
gl_dict.debit
if account_currency == company_currency
else flt(gl_dict.debit / conversion_rate, 2)
gl_dict.debit if account_currency == company_currency else flt(gl_dict.debit / conversion_rate, 2)
)
if flt(gl_dict.credit) and not flt(gl_dict.credit_in_account_currency):
@@ -2568,9 +2557,7 @@ def get_advance_journal_entries(
order_list,
include_unallocated=True,
):
dr_or_cr = (
"credit_in_account_currency" if party_type == "Customer" else "debit_in_account_currency"
)
dr_or_cr = "credit_in_account_currency" if party_type == "Customer" else "debit_in_account_currency"
conditions = []
if include_unallocated:
@@ -2579,7 +2566,7 @@ def get_advance_journal_entries(
if order_list:
order_condition = ", ".join(["%s"] * len(order_list))
conditions.append(
" (t2.reference_type = '{0}' and ifnull(t2.reference_name, '') in ({1}))".format(
" (t2.reference_type = '{}' and ifnull(t2.reference_name, '') in ({}))".format(
order_doctype, order_condition
)
)
@@ -2588,10 +2575,10 @@ def get_advance_journal_entries(
# nosemgrep
journal_entries = frappe.db.sql(
"""
f"""
select
'Journal Entry' as reference_type, t1.name as reference_name,
t1.remark as remarks, t2.{0} as amount, t2.name as reference_row,
t1.remark as remarks, t2.{amount_field} as amount, t2.name as reference_row,
t2.reference_name as against_order, t2.exchange_rate
from
`tabJournal Entry` t1, `tabJournal Entry Account` t2
@@ -2599,11 +2586,9 @@ def get_advance_journal_entries(
t1.name = t2.parent and t2.account = %s
and t2.party_type = %s and t2.party = %s
and t2.is_advance = 'Yes' and t1.docstatus = 1
and {1} > 0 {2}
order by t1.posting_date""".format(
amount_field, dr_or_cr, reference_condition
),
[party_account, party_type, party] + order_list,
and {dr_or_cr} > 0 {reference_condition}
order by t1.posting_date""",
[party_account, party_type, party, *order_list],
as_dict=1,
)
@@ -2631,13 +2616,9 @@ def get_advance_payment_entries(
per = qb.DocType("Payment Entry Reference")
party_account_field = "paid_from" if party_type == "Customer" else "paid_to"
currency_field = (
"paid_from_account_currency" if party_type == "Customer" else "paid_to_account_currency"
)
currency_field = "paid_from_account_currency" if party_type == "Customer" else "paid_to_account_currency"
payment_type = "Receive" if party_type == "Customer" else "Pay"
exchange_rate_field = (
"source_exchange_rate" if payment_type == "Receive" else "target_exchange_rate"
)
exchange_rate_field = "source_exchange_rate" if payment_type == "Receive" else "target_exchange_rate"
payment_entries_against_order, unallocated_payment_entries = [], []
@@ -2783,9 +2764,7 @@ def get_payment_terms(
schedule = []
for d in terms_doc.get("terms"):
term_details = get_payment_term_details(
d, posting_date, grand_total, base_grand_total, bill_date
)
term_details = get_payment_term_details(d, posting_date, grand_total, base_grand_total, bill_date)
schedule.append(term_details)
return schedule
@@ -2903,9 +2882,7 @@ def add_taxes_from_tax_template(child_item, parent_doc, db_insert=True):
tax_row.db_insert()
def set_order_defaults(
parent_doctype, parent_doctype_name, child_doctype, child_docname, trans_item
):
def set_order_defaults(parent_doctype, parent_doctype_name, child_doctype, child_docname, trans_item):
"""
Returns a Sales/Purchase Order Item child item containing the default values
"""
@@ -2921,9 +2898,7 @@ def set_order_defaults(
child_item.stock_uom = item.stock_uom
child_item.uom = trans_item.get("uom") or item.stock_uom
child_item.warehouse = get_item_warehouse(item, p_doc, overwrite_warehouse=True)
conversion_factor = flt(
get_conversion_factor(item.item_code, child_item.uom).get("conversion_factor")
)
conversion_factor = flt(get_conversion_factor(item.item_code, child_item.uom).get("conversion_factor"))
child_item.conversion_factor = flt(trans_item.get("conversion_factor")) or conversion_factor
if child_doctype == "Purchase Order Item":
@@ -3070,9 +3045,7 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
def get_new_child_item(item_row):
child_doctype = "Sales Order Item" if parent_doctype == "Sales Order" else "Purchase Order Item"
return set_order_defaults(
parent_doctype, parent_doctype_name, child_doctype, child_docname, item_row
)
return set_order_defaults(parent_doctype, parent_doctype_name, child_doctype, child_docname, item_row)
def validate_quantity(child_item, new_data):
if not flt(new_data.get("qty")):
@@ -3086,9 +3059,7 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
if parent_doctype == "Sales Order" and flt(new_data.get("qty")) < flt(child_item.delivered_qty):
frappe.throw(_("Cannot set quantity less than delivered quantity"))
if parent_doctype == "Purchase Order" and flt(new_data.get("qty")) < flt(
child_item.received_qty
):
if parent_doctype == "Purchase Order" and flt(new_data.get("qty")) < flt(child_item.received_qty):
frappe.throw(_("Cannot set quantity less than received quantity"))
def should_update_supplied_items(doc) -> bool:
@@ -3103,9 +3074,7 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
item.supplied_qty or item.consumed_qty or item.returned_qty for item in doc.supplied_items
)
update_supplied_items = (
any_qty_changed or items_added_or_removed or any_conversion_factor_changed
)
update_supplied_items = any_qty_changed or items_added_or_removed or any_conversion_factor_changed
if update_supplied_items and supplied_items_processed:
frappe.throw(_("Item qty can not be updated as raw materials are already processed."))
@@ -3142,8 +3111,9 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
prev_rate, new_rate = flt(child_item.get("rate")), flt(d.get("rate"))
prev_qty, new_qty = flt(child_item.get("qty")), flt(d.get("qty"))
prev_con_fac, new_con_fac = flt(child_item.get("conversion_factor")), flt(
d.get("conversion_factor")
prev_con_fac, new_con_fac = (
flt(child_item.get("conversion_factor")),
flt(d.get("conversion_factor")),
)
prev_uom, new_uom = child_item.get("uom"), d.get("uom")
@@ -3222,7 +3192,8 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
if parent_doctype in sales_doctypes:
child_item.margin_type = "Amount"
child_item.margin_rate_or_amount = flt(
child_item.rate - child_item.price_list_rate, child_item.precision("margin_rate_or_amount")
child_item.rate - child_item.price_list_rate,
child_item.precision("margin_rate_or_amount"),
)
child_item.rate_with_margin = child_item.rate
else:
@@ -3302,10 +3273,8 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
parent.set_status()
def check_if_child_table_updated(
child_table_before_update, child_table_after_update, fields_to_check
):
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
def check_if_child_table_updated(child_table_before_update, child_table_after_update, fields_to_check):
accounting_dimensions = [*get_accounting_dimensions(), "cost_center", "project"]
# Check if any field affecting accounting entry is altered
for index, item in enumerate(child_table_after_update):
for field in fields_to_check: