From 73746e2c71a3be32af9157eb324f324a7e853352 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 6 Jun 2025 19:40:12 +0530 Subject: [PATCH] perf: use cached selling settings (#47949) * perf: Use cached stock repost settings * perf: Use cached selling settings --- .../doctype/sales_invoice/sales_invoice.py | 14 +++++++------- .../tax_withholding_details.py | 2 +- .../tds_computation_summary.py | 2 +- .../trial_balance_for_party.py | 2 +- erpnext/controllers/accounts_controller.py | 2 +- erpnext/controllers/selling_controller.py | 6 ++---- erpnext/controllers/status_updater.py | 2 +- erpnext/controllers/stock_controller.py | 6 ++---- erpnext/projects/doctype/project/project.py | 2 +- erpnext/selling/doctype/quotation/quotation.py | 2 +- erpnext/selling/doctype/sales_order/sales_order.py | 12 +++++------- .../customer_credit_balance.py | 2 +- erpnext/startup/boot.py | 8 ++++---- .../stock/doctype/delivery_note/delivery_note.py | 4 ++-- erpnext/stock/doctype/item/item.py | 6 +++--- erpnext/stock/doctype/packed_item/packed_item.py | 2 +- erpnext/stock/doctype/price_list/price_list.py | 2 +- 17 files changed, 35 insertions(+), 41 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 4e2f2f99c2d..4e75d92306e 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -495,7 +495,7 @@ class SalesInvoice(SellingController): self.update_time_sheet(self.name) - if frappe.db.get_single_value("Selling Settings", "sales_update_frequency") == "Each Transaction": + if frappe.get_settings("Selling Settings", "sales_update_frequency") == "Each Transaction": update_company_current_month_sales(self.company) self.update_project() update_linked_doc(self.doctype, self.name, self.inter_company_invoice_reference) @@ -611,7 +611,7 @@ class SalesInvoice(SellingController): if self.coupon_code: update_coupon_code_count(self.coupon_code, "cancelled") - if frappe.db.get_single_value("Selling Settings", "sales_update_frequency") == "Each Transaction": + if frappe.get_settings("Selling Settings", "sales_update_frequency") == "Each Transaction": update_company_current_month_sales(self.company) self.update_project() if not self.is_return and not self.is_consolidated and self.loyalty_program: @@ -1016,7 +1016,7 @@ class SalesInvoice(SellingController): ) if ( - cint(frappe.db.get_single_value("Selling Settings", "maintain_same_sales_rate")) + cint(frappe.get_settings("Selling Settings", "maintain_same_sales_rate")) and not self.is_return and not self.is_internal_customer ): @@ -1063,7 +1063,7 @@ class SalesInvoice(SellingController): "Delivery Note": ["dn_required", "update_stock"], } for key, value in prev_doc_field_map.items(): - if frappe.db.get_single_value("Selling Settings", value[0]) == "Yes": + if frappe.get_settings("Selling Settings", value[0]) == "Yes": if frappe.get_value("Customer", self.customer, value[0]): continue @@ -1466,7 +1466,7 @@ class SalesInvoice(SellingController): def make_tax_gl_entries(self, gl_entries): enable_discount_accounting = cint( - frappe.db.get_single_value("Selling Settings", "enable_discount_accounting") + frappe.get_settings("Selling Settings", "enable_discount_accounting") ) for tax in self.get("taxes"): @@ -1516,7 +1516,7 @@ class SalesInvoice(SellingController): def make_item_gl_entries(self, gl_entries): # income account gl entries enable_discount_accounting = cint( - frappe.db.get_single_value("Selling Settings", "enable_discount_accounting") + frappe.get_settings("Selling Settings", "enable_discount_accounting") ) for item in self.get("items"): @@ -1591,7 +1591,7 @@ class SalesInvoice(SellingController): def enable_discount_accounting(self): if not hasattr(self, "_enable_discount_accounting"): self._enable_discount_accounting = cint( - frappe.db.get_single_value("Selling Settings", "enable_discount_accounting") + frappe.get_settings("Selling Settings", "enable_discount_accounting") ) return self._enable_discount_accounting diff --git a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py index 0da5fcfc7ef..f51e4152e56 100644 --- a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py +++ b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py @@ -9,7 +9,7 @@ from frappe.utils import getdate def execute(filters=None): if filters.get("party_type") == "Customer": - party_naming_by = frappe.db.get_single_value("Selling Settings", "cust_master_name") + party_naming_by = frappe.get_settings("Selling Settings", "cust_master_name") else: party_naming_by = frappe.db.get_single_value("Buying Settings", "supp_master_name") diff --git a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py index e14d9320fa2..6b49f12a524 100644 --- a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py +++ b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py @@ -10,7 +10,7 @@ from erpnext.accounts.utils import get_fiscal_year def execute(filters=None): if filters.get("party_type") == "Customer": - party_naming_by = frappe.db.get_single_value("Selling Settings", "cust_master_name") + party_naming_by = frappe.get_settings("Selling Settings", "cust_master_name") else: party_naming_by = frappe.db.get_single_value("Buying Settings", "supp_master_name") diff --git a/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py b/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py index f6c79eb6c45..d2587a78572 100644 --- a/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py +++ b/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py @@ -256,7 +256,7 @@ def is_party_name_visible(filters): if filters.get("party_type") in ["Customer", "Supplier"]: if filters.get("party_type") == "Customer": - party_naming_by = frappe.db.get_single_value("Selling Settings", "cust_master_name") + party_naming_by = frappe.get_settings("Selling Settings", "cust_master_name") else: party_naming_by = frappe.db.get_single_value("Buying Settings", "supp_master_name") diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 2e9be24d278..7970b43c7f0 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1962,7 +1962,7 @@ class AccountsController(TransactionBase): def make_discount_gl_entries(self, gl_entries): enable_discount_accounting = cint( - frappe.db.get_single_value("Selling Settings", "enable_discount_accounting") + frappe.get_settings("Selling Settings", "enable_discount_accounting") ) if enable_discount_accounting: diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 2d2d163b737..77e54c5747b 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -255,9 +255,7 @@ class SellingController(StockController): title=_("Invalid Selling Price"), ) - if self.get("is_return") or not frappe.db.get_single_value( - "Selling Settings", "validate_selling_price" - ): + if self.get("is_return") or not frappe.get_settings("Selling Settings", "validate_selling_price"): return is_internal_customer = self.get("is_internal_customer") @@ -717,7 +715,7 @@ class SellingController(StockController): def validate_for_duplicate_items(self): check_list, chk_dupl_itm = [], [] - if cint(frappe.db.get_single_value("Selling Settings", "allow_multiple_items")): + if cint(frappe.get_settings("Selling Settings", "allow_multiple_items")): return if self.doctype == "Sales Invoice" and self.is_consolidated: return diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py index e105acd05ec..f98d61f221e 100644 --- a/erpnext/controllers/status_updater.py +++ b/erpnext/controllers/status_updater.py @@ -266,7 +266,7 @@ class StatusUpdater(Document): if hasattr(d, "qty") and d.qty > 0 and self.get("is_return"): frappe.throw(_("For an item {0}, quantity must be negative number").format(d.item_code)) - if not frappe.db.get_single_value("Selling Settings", "allow_negative_rates_for_items"): + if not frappe.get_settings("Selling Settings", "allow_negative_rates_for_items"): if hasattr(d, "item_code") and hasattr(d, "rate") and flt(d.rate) < 0: frappe.throw( _( diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index cb23f0ab7f6..e791a2ea329 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -1379,9 +1379,7 @@ class StockController(AccountsController): force = True if force or future_sle_exists(args) or repost_required_for_queue(self): - item_based_reposting = cint( - frappe.db.get_single_value("Stock Reposting Settings", "item_based_reposting") - ) + item_based_reposting = frappe.get_settings("Stock Reposting Settings", "item_based_reposting") if item_based_reposting: create_item_wise_repost_entries( voucher_type=self.doctype, @@ -1673,7 +1671,7 @@ def is_reposting_pending(): def future_sle_exists(args, sl_entries=None, allow_force_reposting=True): from erpnext.stock.utils import get_combine_datetime - if allow_force_reposting and frappe.db.get_single_value( + if allow_force_reposting and frappe.get_settings( "Stock Reposting Settings", "do_reposting_for_each_stock_transaction" ): return True diff --git a/erpnext/projects/doctype/project/project.py b/erpnext/projects/doctype/project/project.py index 959b34f77a3..64781e0f212 100644 --- a/erpnext/projects/doctype/project/project.py +++ b/erpnext/projects/doctype/project/project.py @@ -680,7 +680,7 @@ def send_project_status_email_to_users(): def update_project_sales_billing(): - sales_update_frequency = frappe.db.get_single_value("Selling Settings", "sales_update_frequency") + sales_update_frequency = frappe.get_settings("Selling Settings", "sales_update_frequency") if sales_update_frequency == "Each Transaction": return elif sales_update_frequency == "Monthly" and frappe.utils.now_datetime().day != 1: diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py index 0ad6bf6bf2a..17a2ff07617 100644 --- a/erpnext/selling/doctype/quotation/quotation.py +++ b/erpnext/selling/doctype/quotation/quotation.py @@ -167,7 +167,7 @@ class Quotation(SellingController): """ If permitted in settings and any item has 0 qty, the SO has unit price items. """ - if not frappe.db.get_single_value("Selling Settings", "allow_zero_qty_in_quotation"): + if not frappe.get_settings("Selling Settings", "allow_zero_qty_in_quotation"): return self.has_unit_price_items = any( diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index 26f6d7026c0..77a28db827b 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -253,7 +253,7 @@ class SalesOrder(SellingController): """ If permitted in settings and any item has 0 qty, the SO has unit price items. """ - if not frappe.db.get_single_value("Selling Settings", "allow_zero_qty_in_sales_order"): + if not frappe.get_settings("Selling Settings", "allow_zero_qty_in_sales_order"): return self.has_unit_price_items = any( @@ -279,9 +279,7 @@ class SalesOrder(SellingController): (self.po_no, self.name, self.customer), ) if so and so[0][0]: - if cint( - frappe.db.get_single_value("Selling Settings", "allow_against_multiple_purchase_orders") - ): + if cint(frappe.get_settings("Selling Settings", "allow_against_multiple_purchase_orders")): frappe.msgprint( _( "Warning: Sales Order {0} already exists against Customer's Purchase Order {1}" @@ -405,7 +403,7 @@ class SalesOrder(SellingController): } ) - if cint(frappe.db.get_single_value("Selling Settings", "maintain_same_sales_rate")): + if cint(frappe.get_settings("Selling Settings", "maintain_same_sales_rate")): self.validate_rate_with_reference_doc([["Quotation", "prevdoc_docname", "quotation_item"]]) def update_enquiry_status(self, prevdoc, flag): @@ -483,7 +481,7 @@ class SalesOrder(SellingController): update_coupon_code_count(self.coupon_code, "cancelled") def update_project(self): - if frappe.db.get_single_value("Selling Settings", "sales_update_frequency") != "Each Transaction": + if frappe.get_settings("Selling Settings", "sales_update_frequency") != "Each Transaction": return if self.project: @@ -815,7 +813,7 @@ def get_list_context(context=None): @frappe.whitelist() def is_enable_cutoff_date_on_bulk_delivery_note_creation(): - return frappe.db.get_single_value("Selling Settings", "enable_cutoff_date_on_bulk_delivery_note_creation") + return frappe.get_settings("Selling Settings", "enable_cutoff_date_on_bulk_delivery_note_creation") @frappe.whitelist() diff --git a/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py b/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py index d64c89c8f2b..24570391224 100644 --- a/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py +++ b/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py @@ -13,7 +13,7 @@ def execute(filters=None): if not filters: filters = {} # Check if customer id is according to naming series or customer name - customer_naming_type = frappe.db.get_single_value("Selling Settings", "cust_master_name") + customer_naming_type = frappe.get_settings("Selling Settings", "cust_master_name") columns = get_columns(customer_naming_type) data = [] diff --git a/erpnext/startup/boot.py b/erpnext/startup/boot.py index 2e7d5939815..a312d0d5499 100644 --- a/erpnext/startup/boot.py +++ b/erpnext/startup/boot.py @@ -15,9 +15,9 @@ def boot_session(bootinfo): if frappe.session["user"] != "Guest": update_page_info(bootinfo) - bootinfo.sysdefaults.territory = frappe.db.get_single_value("Selling Settings", "territory") - bootinfo.sysdefaults.customer_group = frappe.db.get_single_value("Selling Settings", "customer_group") - bootinfo.sysdefaults.use_server_side_reactivity = frappe.db.get_single_value( + bootinfo.sysdefaults.territory = frappe.get_settings("Selling Settings", "territory") + bootinfo.sysdefaults.customer_group = frappe.get_settings("Selling Settings", "customer_group") + bootinfo.sysdefaults.use_server_side_reactivity = frappe.get_settings( "Selling Settings", "use_server_side_reactivity" ) bootinfo.sysdefaults.allow_stale = cint(frappe.get_settings("Accounts Settings", "allow_stale")) @@ -30,7 +30,7 @@ def boot_session(bootinfo): ) bootinfo.sysdefaults.allow_sales_order_creation_for_expired_quotation = cint( - frappe.db.get_single_value("Selling Settings", "allow_sales_order_creation_for_expired_quotation") + frappe.get_settings("Selling Settings", "allow_sales_order_creation_for_expired_quotation") ) # if no company, show a dialog box to create a new company diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py index 31525de213f..1a81d34fad1 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.py +++ b/erpnext/stock/doctype/delivery_note/delivery_note.py @@ -247,7 +247,7 @@ class DeliveryNote(SellingController): def so_required(self): """check in manage account if sales order required or not""" - if frappe.db.get_single_value("Selling Settings", "so_required") == "Yes": + if frappe.get_settings("Selling Settings", "so_required") == "Yes": for d in self.get("items"): if not d.against_sales_order: frappe.throw(_("Sales Order required for Item {0}").format(d.item_code)) @@ -314,7 +314,7 @@ class DeliveryNote(SellingController): ) if ( - cint(frappe.db.get_single_value("Selling Settings", "maintain_same_sales_rate")) + cint(frappe.get_settings("Selling Settings", "maintain_same_sales_rate")) and not self.is_return and not self.is_internal_customer ): diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index 3789514589a..a0decbd3a86 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -243,9 +243,9 @@ class Item(Document): def add_price(self, price_list=None): """Add a new price""" if not price_list: - price_list = frappe.db.get_single_value( - "Selling Settings", "selling_price_list" - ) or frappe.db.get_value("Price List", _("Standard Selling")) + price_list = frappe.get_settings("Selling Settings", "selling_price_list") or frappe.db.get_value( + "Price List", _("Standard Selling") + ) if price_list: item_price = frappe.get_doc( { diff --git a/erpnext/stock/doctype/packed_item/packed_item.py b/erpnext/stock/doctype/packed_item/packed_item.py index 2dc28b1f04b..b6702379590 100644 --- a/erpnext/stock/doctype/packed_item/packed_item.py +++ b/erpnext/stock/doctype/packed_item/packed_item.py @@ -69,7 +69,7 @@ def make_packing_list(doc): return parent_items_price, reset = {}, False - set_price_from_children = frappe.db.get_single_value("Selling Settings", "editable_bundle_item_rates") + set_price_from_children = frappe.get_settings("Selling Settings", "editable_bundle_item_rates") stale_packed_items_table = get_indexed_packed_items_table(doc) diff --git a/erpnext/stock/doctype/price_list/price_list.py b/erpnext/stock/doctype/price_list/price_list.py index 882c3f5c9cb..f4772c33fff 100644 --- a/erpnext/stock/doctype/price_list/price_list.py +++ b/erpnext/stock/doctype/price_list/price_list.py @@ -39,7 +39,7 @@ class PriceList(Document): def set_default_if_missing(self): if cint(self.selling): - if not frappe.db.get_single_value("Selling Settings", "selling_price_list"): + if not frappe.get_settings("Selling Settings", "selling_price_list"): frappe.set_value("Selling Settings", "Selling Settings", "selling_price_list", self.name) elif cint(self.buying):