[optimize] tree conditions for pricing rule

This commit is contained in:
Nabin Hait 2018-08-08 18:43:04 +05:30
parent 859d942b22
commit 50238c3049
4 changed files with 27 additions and 12 deletions

View File

@ -76,7 +76,7 @@ class PricingRule(Document):
def validate_price_list_with_currency(self): def validate_price_list_with_currency(self):
if self.currency and self.for_price_list: 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: if not self.currency == price_list_currency:
throw(_("Currency should be same as Price List Currency: {0}").format(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.transaction_type=="selling":
if args.customer and not (args.customer_group and args.territory): 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: if customer:
args.customer_group, args.territory = customer args.customer_group, args.territory = customer
args.supplier = args.supplier_group = None args.supplier = args.supplier_group = None
elif args.supplier and not args.supplier_group: 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 args.customer = args.customer_group = args.territory = None
pricing_rules = get_pricing_rules(args) pricing_rules = get_pricing_rules(args)
@ -209,7 +209,7 @@ def get_pricing_rule_for_item(args):
return item_details return item_details
def remove_pricing_rule_for_item(pricing_rule, 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) ['price_or_discount', 'margin_type'], as_dict=1)
if pricing_rule and pricing_rule.price_or_discount == 'Discount Percentage': if pricing_rule and pricing_rule.price_or_discount == 'Discount Percentage':
item_details.discount_percentage = 0.0 item_details.discount_percentage = 0.0
@ -237,6 +237,12 @@ def remove_pricing_rules(item_list):
def get_pricing_rules(args): def get_pricing_rules(args):
def _get_tree_conditions(parenttype, allow_blank=True): def _get_tree_conditions(parenttype, allow_blank=True):
field = frappe.scrub(parenttype) 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 = "" condition = ""
if args.get(field): if args.get(field):
try: try:
@ -251,6 +257,8 @@ def get_pricing_rules(args):
if allow_blank: parent_groups.append('') if allow_blank: parent_groups.append('')
condition = " ifnull("+field+", '') in ('" + \ condition = " ifnull("+field+", '') in ('" + \
"', '".join([frappe.db.escape(d) for d in parent_groups])+"')" "', '".join([frappe.db.escape(d) for d in parent_groups])+"')"
frappe.flags.tree_conditions[key] = condition
return condition return condition

View File

@ -143,7 +143,7 @@ def get_default_price_list(party):
if party.doctype == "Customer": if party.doctype == "Customer":
price_list = frappe.db.get_value("Customer Group", 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: if price_list:
return 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 price_list = get_default_price_list(party) or given_price_list
if 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 out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list

View File

@ -605,7 +605,7 @@ class AccountsController(TransactionBase):
@property @property
def company_abbr(self): 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") self._abbr = frappe.get_cached_value('Company', self.company, "abbr")
return self._abbr return self._abbr

View File

@ -103,7 +103,7 @@ def update_stock(args, out):
def set_valuation_rate(out, args): 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 valuation_rate = 0.0
bundled_items = frappe.get_doc("Product Bundle", args.item_code) bundled_items = frappe.get_doc("Product Bundle", args.item_code)
@ -330,10 +330,17 @@ def get_default_deferred_revenue_account(args, item):
return None return None
def get_default_cost_center(args, item, item_group): def get_default_cost_center(args, item, item_group):
return (frappe.db.get_value("Project", args.get("project"), "cost_center", cache=True) cost_center = None
or (item.get("selling_cost_center") if args.get("customer") else item.get("buying_cost_center")) if args.get('project'):
or (item_group.get("selling_cost_center") if args.get("customer") else item_group.get("buying_cost_center")) cost_center = frappe.db.get_value("Project", args.get("project"), "cost_center", cache=True)
or args.get("cost_center"))
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): def get_default_supplier(args, item, item_group):
return (item.get("default_supplier") return (item.get("default_supplier")