From 50238c3049135280606d25b68531eeaaa88c6eb7 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 8 Aug 2018 18:43:04 +0530 Subject: [PATCH] [optimize] tree conditions for pricing rule --- .../doctype/pricing_rule/pricing_rule.py | 16 ++++++++++++---- erpnext/accounts/party.py | 4 ++-- erpnext/controllers/accounts_controller.py | 2 +- erpnext/stock/get_item_details.py | 17 ++++++++++++----- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py index a4e39dfb72..96cd747960 100644 --- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py @@ -76,7 +76,7 @@ class PricingRule(Document): def validate_price_list_with_currency(self): if self.currency and self.for_price_list: - price_list_currency = frappe.db.get_value("Price List", self.for_price_list, "currency") + price_list_currency = frappe.db.get_value("Price List", self.for_price_list, "currency", True) if not self.currency == price_list_currency: throw(_("Currency should be same as Price List Currency: {0}").format(price_list_currency)) @@ -167,14 +167,14 @@ def get_pricing_rule_for_item(args): if args.transaction_type=="selling": if args.customer and not (args.customer_group and args.territory): - customer = frappe.db.get_value("Customer", args.customer, ["customer_group", "territory"]) + customer = frappe.db.get_cached_value("Customer", args.customer, ["customer_group", "territory"]) if customer: args.customer_group, args.territory = customer args.supplier = args.supplier_group = None elif args.supplier and not args.supplier_group: - args.supplier_group = frappe.db.get_value("Supplier", args.supplier, "supplier_group") + args.supplier_group = frappe.db.get_cached_value("Supplier", args.supplier, "supplier_group") args.customer = args.customer_group = args.territory = None pricing_rules = get_pricing_rules(args) @@ -209,7 +209,7 @@ def get_pricing_rule_for_item(args): return item_details def remove_pricing_rule_for_item(pricing_rule, item_details): - pricing_rule = frappe.db.get_value('Pricing Rule', pricing_rule, + pricing_rule = frappe.db.get_cached_value('Pricing Rule', pricing_rule, ['price_or_discount', 'margin_type'], as_dict=1) if pricing_rule and pricing_rule.price_or_discount == 'Discount Percentage': item_details.discount_percentage = 0.0 @@ -237,6 +237,12 @@ def remove_pricing_rules(item_list): def get_pricing_rules(args): def _get_tree_conditions(parenttype, allow_blank=True): field = frappe.scrub(parenttype) + if not frappe.flags.tree_conditions: + frappe.flags.tree_conditions = {} + key = (parenttype, args[field], ) + if key in frappe.flags.tree_conditions: + return frappe.flags.tree_conditions[key] + condition = "" if args.get(field): try: @@ -251,6 +257,8 @@ def get_pricing_rules(args): if allow_blank: parent_groups.append('') condition = " ifnull("+field+", '') in ('" + \ "', '".join([frappe.db.escape(d) for d in parent_groups])+"')" + frappe.flags.tree_conditions[key] = condition + return condition diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py index 114ad868a9..2619cd5049 100644 --- a/erpnext/accounts/party.py +++ b/erpnext/accounts/party.py @@ -143,7 +143,7 @@ def get_default_price_list(party): if party.doctype == "Customer": price_list = frappe.db.get_value("Customer Group", - party.customer_group, "default_price_list") + party.customer_group, "default_price_list", cache=True) if price_list: return price_list @@ -162,7 +162,7 @@ def set_price_list(out, party, party_type, given_price_list): price_list = get_default_price_list(party) or given_price_list if price_list: - out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency") + out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency", cache=True) out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 449ea36eff..dc8c1e4218 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -605,7 +605,7 @@ class AccountsController(TransactionBase): @property def company_abbr(self): - if not hasattr(self, "_abbr"): + if not hasattr(self, "_abbr") and self.company: self._abbr = frappe.get_cached_value('Company', self.company, "abbr") return self._abbr diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 5c61035273..198940c18c 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -103,7 +103,7 @@ def update_stock(args, out): def set_valuation_rate(out, args): - if frappe.db.exists("Product Bundle", args.item_code): + if frappe.db.exists("Product Bundle", args.item_code, cache=True): valuation_rate = 0.0 bundled_items = frappe.get_doc("Product Bundle", args.item_code) @@ -330,10 +330,17 @@ def get_default_deferred_revenue_account(args, item): return None def get_default_cost_center(args, item, item_group): - return (frappe.db.get_value("Project", args.get("project"), "cost_center", cache=True) - or (item.get("selling_cost_center") if args.get("customer") else item.get("buying_cost_center")) - or (item_group.get("selling_cost_center") if args.get("customer") else item_group.get("buying_cost_center")) - or args.get("cost_center")) + cost_center = None + if args.get('project'): + cost_center = frappe.db.get_value("Project", args.get("project"), "cost_center", cache=True) + + if not cost_center: + if args.get('customer'): + cost_center = item.get('selling_cost_center') or item_group.get('selling_cost_center') + else: + cost_center = item.get('buying_cost_center') or item_group.get('buying_cost_center') + + return cost_center or args.get("cost_center") def get_default_supplier(args, item, item_group): return (item.get("default_supplier")