From 859d942b22a22f3c13b99f14fa206b0018982c15 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Wed, 8 Aug 2018 18:33:50 +0530 Subject: [PATCH] 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 --- erpnext/accounts/doctype/pricing_rule/pricing_rule.py | 6 +++--- .../accounts/doctype/sales_invoice/sales_invoice.py | 6 +++--- erpnext/assets/doctype/asset/asset.py | 4 ++-- .../buying/doctype/purchase_order/purchase_order.py | 8 ++++---- erpnext/controllers/selling_controller.py | 6 +++--- erpnext/selling/doctype/sales_order/sales_order.py | 10 +++++----- erpnext/utilities/transaction_base.py | 2 +- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py index 6c2ae7ab75..a4e39dfb72 100644 --- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py @@ -70,7 +70,7 @@ class PricingRule(Document): def validate_max_discount(self): 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): 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): 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: # invalid item_code return item_details @@ -284,7 +284,7 @@ def get_pricing_rules(args): # load variant of if not defined 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: item_variant_condition = ' or item_code=%(variant_of)s ' diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 19c86d51f2..2a17d80bf3 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -161,7 +161,7 @@ class SalesInvoice(SellingController): self.update_project() update_linked_invoice(self.doctype, self.name, self.inter_company_invoice_reference) - # create the loyalty point ledger entry if the customer is enrolled in any loyalty program + # create the loyalty point ledger entry if the customer is enrolled in any loyalty program if not self.is_return and self.loyalty_program: self.make_loyalty_point_entry() elif self.is_return and self.return_against and self.loyalty_program: @@ -491,7 +491,7 @@ class SalesInvoice(SellingController): super(SalesInvoice, self).validate_warehouse() 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)) def validate_delivery_note(self): @@ -1006,7 +1006,7 @@ class SalesInvoice(SellingController): where redeem_against=%s''', (lp_entry.name), as_dict=1) if against_lp_entry: invoice_list = ", ".join([d.sales_invoice for d in against_lp_entry]) - frappe.throw(_('''Sales Invoice can't be cancelled since the Loyalty Points earned has been redeemed. + frappe.throw(_('''Sales Invoice can't be cancelled since the Loyalty Points earned has been redeemed. First cancel the Sales Invoice No {0}''').format(invoice_list)) else: frappe.db.sql('''delete from `tabLoyalty Point Entry` where sales_invoice=%s''', (self.name)) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 024dd620f3..04fa8dcb06 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -44,7 +44,7 @@ class Asset(AccountsController): self.db_set('booked_fixed_asset', 0) 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) if not item: frappe.throw(_("Item {0} does not exist").format(self.item_code)) @@ -61,7 +61,7 @@ class Asset(AccountsController): def set_missing_values(self): 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'): finance_books = get_item_details(self.item_code, self.asset_category) diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py index b63c256d6a..a543c20066 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -134,7 +134,7 @@ class PurchaseOrder(BuyingController): d.last_purchase_rate = d.rate 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: d.base_price_list_rate = d.base_rate = d.price_list_rate \ = d.rate = d.last_purchase_rate = item_last_purchase_rate @@ -168,7 +168,7 @@ class PurchaseOrder(BuyingController): for d in self.get("items"): if (not po_item_rows or d.name in po_item_rows) \ 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: item_wh_list.append([d.item_code, d.warehouse]) for item_code, warehouse in item_wh_list: @@ -286,7 +286,7 @@ class PurchaseOrder(BuyingController): if d.rm_item_code: stock_bin = get_bin(d.rm_item_code, d.reserve_warehouse) stock_bin.update_reserved_qty_for_sub_contracting() - + def update_receiving_percentage(self): total_qty, received_qty = 0.0, 0.0 for item in self.items: @@ -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 return last_purchase_rate 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: return item_last_purchase_rate diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 052ea03567..b9efe53819 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -139,7 +139,7 @@ class SellingController(StockController): def validate_max_discount(self): 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: 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: 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) 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") @@ -283,7 +283,7 @@ class SellingController(StockController): sl_entries = [] 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: d.conversion_factor = get_conversion_factor(d.item_code, d.uom).get("conversion_factor") or 1.0 return_rate = 0 diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index b860f7dedc..f69b59f36f 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -116,7 +116,7 @@ class SalesOrder(SellingController): d.delivery_date = self.delivery_date if getdate(self.transaction_date) > getdate(d.delivery_date): frappe.msgprint(_("Expected Delivery Date should be after Sales Order Date"), - indicator='orange', title=_('Warning')) + indicator='orange', title=_('Warning')) if getdate(self.delivery_date) != getdate(max_delivery_date): self.delivery_date = max_delivery_date else: @@ -136,7 +136,7 @@ class SalesOrder(SellingController): super(SalesOrder, self).validate_warehouse() 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))) \ and not d.warehouse and not cint(d.delivered_by_supplier): 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): # 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 - 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) def check_nextdoc_docstatus(self): @@ -281,7 +281,7 @@ class SalesOrder(SellingController): item_wh_list = [] def _valid_for_reserve(item_code, warehouse): 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]) for d in self.get("items"): @@ -412,7 +412,7 @@ class SalesOrder(SellingController): Item {0} is added with and without Ensure Delivery by \ Serial No.").format(item.item_code)) 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 \ can have delivery based on Serial No").format(item.item_code)) if not frappe.db.exists("BOM", {"item": item.item_code, "is_active": 1}): diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py index d3769a23bd..6ca3d5bf88 100644 --- a/erpnext/utilities/transaction_base.py +++ b/erpnext/utilities/transaction_base.py @@ -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()])) 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: return