Cached calls (#15109)

* [optimize]: bin updates

* [optimise]: use frappe.get_cached_value instead of get_value for item

* [fix]: change db.get_cached_value to get_cached_value
This commit is contained in:
Himanshu Mishra
2018-08-08 18:33:50 +05:30
committed by Nabin Hait
parent f56aac65e1
commit 859d942b22
7 changed files with 21 additions and 21 deletions

View File

@@ -70,7 +70,7 @@ class PricingRule(Document):
def validate_max_discount(self): def validate_max_discount(self):
if self.rate_or_discount == "Discount Percentage" and self.item_code: if self.rate_or_discount == "Discount Percentage" and self.item_code:
max_discount = frappe.db.get_value("Item", self.item_code, "max_discount") max_discount = frappe.get_cached_value("Item", self.item_code, "max_discount")
if max_discount and flt(self.discount_percentage) > flt(max_discount): if max_discount and flt(self.discount_percentage) > flt(max_discount):
throw(_("Max discount allowed for item: {0} is {1}%").format(self.item_code, max_discount)) throw(_("Max discount allowed for item: {0} is {1}%").format(self.item_code, max_discount))
@@ -158,7 +158,7 @@ def get_pricing_rule_for_item(args):
if not (args.item_group and args.brand): if not (args.item_group and args.brand):
try: try:
args.item_group, args.brand = frappe.db.get_value("Item", args.item_code, ["item_group", "brand"]) args.item_group, args.brand = frappe.get_cached_value("Item", args.item_code, ["item_group", "brand"])
except TypeError: except TypeError:
# invalid item_code # invalid item_code
return item_details return item_details
@@ -284,7 +284,7 @@ def get_pricing_rules(args):
# load variant of if not defined # load variant of if not defined
if "variant_of" not in args: if "variant_of" not in args:
args.variant_of = frappe.db.get_value("Item", args.item_code, "variant_of") args.variant_of = frappe.get_cached_value("Item", args.item_code, "variant_of")
if args.variant_of: if args.variant_of:
item_variant_condition = ' or item_code=%(variant_of)s ' item_variant_condition = ' or item_code=%(variant_of)s '

View File

@@ -491,7 +491,7 @@ class SalesInvoice(SellingController):
super(SalesInvoice, self).validate_warehouse() super(SalesInvoice, self).validate_warehouse()
for d in self.get_item_list(): for d in self.get_item_list():
if not d.warehouse and frappe.db.get_value("Item", d.item_code, "is_stock_item"): if not d.warehouse and frappe.get_cached_value("Item", d.item_code, "is_stock_item"):
frappe.throw(_("Warehouse required for stock Item {0}").format(d.item_code)) frappe.throw(_("Warehouse required for stock Item {0}").format(d.item_code))
def validate_delivery_note(self): def validate_delivery_note(self):

View File

@@ -44,7 +44,7 @@ class Asset(AccountsController):
self.db_set('booked_fixed_asset', 0) self.db_set('booked_fixed_asset', 0)
def validate_item(self): def validate_item(self):
item = frappe.db.get_value("Item", self.item_code, item = frappe.get_cached_value("Item", self.item_code,
["is_fixed_asset", "is_stock_item", "disabled"], as_dict=1) ["is_fixed_asset", "is_stock_item", "disabled"], as_dict=1)
if not item: if not item:
frappe.throw(_("Item {0} does not exist").format(self.item_code)) frappe.throw(_("Item {0} does not exist").format(self.item_code))
@@ -61,7 +61,7 @@ class Asset(AccountsController):
def set_missing_values(self): def set_missing_values(self):
if not self.asset_category: if not self.asset_category:
self.asset_category = frappe.db.get_value("Item", self.item_code, "asset_category") self.asset_category = frappe.get_cached_value("Item", self.item_code, "asset_category")
if self.item_code and not self.get('finance_books'): if self.item_code and not self.get('finance_books'):
finance_books = get_item_details(self.item_code, self.asset_category) finance_books = get_item_details(self.item_code, self.asset_category)

View File

@@ -134,7 +134,7 @@ class PurchaseOrder(BuyingController):
d.last_purchase_rate = d.rate d.last_purchase_rate = d.rate
else: else:
item_last_purchase_rate = frappe.db.get_value("Item", d.item_code, "last_purchase_rate") item_last_purchase_rate = frappe.get_cached_value("Item", d.item_code, "last_purchase_rate")
if item_last_purchase_rate: if item_last_purchase_rate:
d.base_price_list_rate = d.base_rate = d.price_list_rate \ d.base_price_list_rate = d.base_rate = d.price_list_rate \
= d.rate = d.last_purchase_rate = item_last_purchase_rate = d.rate = d.last_purchase_rate = item_last_purchase_rate
@@ -168,7 +168,7 @@ class PurchaseOrder(BuyingController):
for d in self.get("items"): for d in self.get("items"):
if (not po_item_rows or d.name in po_item_rows) \ if (not po_item_rows or d.name in po_item_rows) \
and [d.item_code, d.warehouse] not in item_wh_list \ and [d.item_code, d.warehouse] not in item_wh_list \
and frappe.db.get_value("Item", d.item_code, "is_stock_item") \ and frappe.get_cached_value("Item", d.item_code, "is_stock_item") \
and d.warehouse and not d.delivered_by_supplier: and d.warehouse and not d.delivered_by_supplier:
item_wh_list.append([d.item_code, d.warehouse]) item_wh_list.append([d.item_code, d.warehouse])
for item_code, warehouse in item_wh_list: for item_code, warehouse in item_wh_list:
@@ -304,7 +304,7 @@ def item_last_purchase_rate(name, conversion_rate, item_code, conversion_factor=
last_purchase_rate = (last_purchase_details['base_rate'] * (flt(conversion_factor) or 1.0)) / conversion_rate last_purchase_rate = (last_purchase_details['base_rate'] * (flt(conversion_factor) or 1.0)) / conversion_rate
return last_purchase_rate return last_purchase_rate
else: else:
item_last_purchase_rate = frappe.db.get_value("Item", item_code, "last_purchase_rate") item_last_purchase_rate = frappe.get_cached_value("Item", item_code, "last_purchase_rate")
if item_last_purchase_rate: if item_last_purchase_rate:
return item_last_purchase_rate return item_last_purchase_rate

View File

@@ -139,7 +139,7 @@ class SellingController(StockController):
def validate_max_discount(self): def validate_max_discount(self):
for d in self.get("items"): for d in self.get("items"):
discount = flt(frappe.db.get_value("Item", d.item_code, "max_discount")) discount = flt(frappe.get_cached_value("Item", d.item_code, "max_discount"))
if discount and flt(d.discount_percentage) > discount: if discount and flt(d.discount_percentage) > discount:
frappe.throw(_("Maximum discount for Item {0} is {1}%").format(d.item_code, discount)) frappe.throw(_("Maximum discount for Item {0} is {1}%").format(d.item_code, discount))
@@ -166,7 +166,7 @@ class SellingController(StockController):
if not it.item_code: if not it.item_code:
continue continue
last_purchase_rate, is_stock_item = frappe.db.get_value("Item", it.item_code, ["last_purchase_rate", "is_stock_item"]) last_purchase_rate, is_stock_item = frappe.get_cached_value("Item", it.item_code, ["last_purchase_rate", "is_stock_item"])
last_purchase_rate_in_sales_uom = last_purchase_rate / (it.conversion_factor or 1) last_purchase_rate_in_sales_uom = last_purchase_rate / (it.conversion_factor or 1)
if flt(it.base_rate) < flt(last_purchase_rate_in_sales_uom): if flt(it.base_rate) < flt(last_purchase_rate_in_sales_uom):
throw_message(it.item_name, last_purchase_rate_in_sales_uom, "last purchase rate") throw_message(it.item_name, last_purchase_rate_in_sales_uom, "last purchase rate")
@@ -283,7 +283,7 @@ class SellingController(StockController):
sl_entries = [] sl_entries = []
for d in self.get_item_list(): for d in self.get_item_list():
if frappe.db.get_value("Item", d.item_code, "is_stock_item") == 1 and flt(d.qty): if frappe.get_cached_value("Item", d.item_code, "is_stock_item") == 1 and flt(d.qty):
if flt(d.conversion_factor)==0.0: if flt(d.conversion_factor)==0.0:
d.conversion_factor = get_conversion_factor(d.item_code, d.uom).get("conversion_factor") or 1.0 d.conversion_factor = get_conversion_factor(d.item_code, d.uom).get("conversion_factor") or 1.0
return_rate = 0 return_rate = 0

View File

@@ -136,7 +136,7 @@ class SalesOrder(SellingController):
super(SalesOrder, self).validate_warehouse() super(SalesOrder, self).validate_warehouse()
for d in self.get("items"): for d in self.get("items"):
if (frappe.db.get_value("Item", d.item_code, "is_stock_item") == 1 or if (frappe.get_cached_value("Item", d.item_code, "is_stock_item") == 1 or
(self.has_product_bundle(d.item_code) and self.product_bundle_has_stock_item(d.item_code))) \ (self.has_product_bundle(d.item_code) and self.product_bundle_has_stock_item(d.item_code))) \
and not d.warehouse and not cint(d.delivered_by_supplier): and not d.warehouse and not cint(d.delivered_by_supplier):
frappe.throw(_("Delivery warehouse required for stock item {0}").format(d.item_code), frappe.throw(_("Delivery warehouse required for stock item {0}").format(d.item_code),
@@ -208,7 +208,7 @@ class SalesOrder(SellingController):
def check_credit_limit(self): def check_credit_limit(self):
# if bypass credit limit check is set to true (1) at sales order level, # if bypass credit limit check is set to true (1) at sales order level,
# then we need not to check credit limit and vise versa # then we need not to check credit limit and vise versa
if not cint(frappe.db.get_value("Customer", self.customer, "bypass_credit_limit_check_at_sales_order")): if not cint(frappe.get_cached_value("Customer", self.customer, "bypass_credit_limit_check_at_sales_order")):
check_credit_limit(self.customer, self.company) check_credit_limit(self.customer, self.company)
def check_nextdoc_docstatus(self): def check_nextdoc_docstatus(self):
@@ -281,7 +281,7 @@ class SalesOrder(SellingController):
item_wh_list = [] item_wh_list = []
def _valid_for_reserve(item_code, warehouse): def _valid_for_reserve(item_code, warehouse):
if item_code and warehouse and [item_code, warehouse] not in item_wh_list \ if item_code and warehouse and [item_code, warehouse] not in item_wh_list \
and frappe.db.get_value("Item", item_code, "is_stock_item"): and frappe.get_cached_value("Item", item_code, "is_stock_item"):
item_wh_list.append([item_code, warehouse]) item_wh_list.append([item_code, warehouse])
for d in self.get("items"): for d in self.get("items"):
@@ -412,7 +412,7 @@ class SalesOrder(SellingController):
Item {0} is added with and without Ensure Delivery by \ Item {0} is added with and without Ensure Delivery by \
Serial No.").format(item.item_code)) Serial No.").format(item.item_code))
if item.item_code not in reserved_items: if item.item_code not in reserved_items:
if not frappe.db.get_value("Item", item.item_code, "has_serial_no"): if not frappe.get_cached_value("Item", item.item_code, "has_serial_no"):
frappe.throw(_("Item {0} has no Serial No. Only serilialized items \ frappe.throw(_("Item {0} has no Serial No. Only serilialized items \
can have delivery based on Serial No").format(item.item_code)) can have delivery based on Serial No").format(item.item_code))
if not frappe.db.exists("BOM", {"item": item.item_code, "is_active": 1}): if not frappe.db.exists("BOM", {"item": item.item_code, "is_active": 1}):

View File

@@ -144,7 +144,7 @@ def validate_uom_is_integer(doc, uom_field, qty_fields, child_dt=None):
distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()])) distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()]))
integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom, integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom,
"must_be_whole_number") or None, distinct_uoms) "must_be_whole_number", cache=True) or None, distinct_uoms)
if not integer_uoms: if not integer_uoms:
return return