From 7d4139e9f9595aefea1de5f46ea31637260ebddf Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 23 Jun 2021 19:47:25 +0530 Subject: [PATCH] perf: don't query unless required, and use cache - added cache for querying values which are rarely changed. - Reordering conditionals so queries are not performed unless absolutely required. Essentially, both option below are semantically correct but one will always hit the DB before before evaulating to `False`. - `False and frappe.db.sql("select * from tabItem")` - `frappe.db.sql("select * from tabItem") and False` --- erpnext/controllers/sales_and_purchase_return.py | 9 +++++---- erpnext/controllers/selling_controller.py | 2 +- erpnext/stock/doctype/batch/batch.py | 7 +++---- erpnext/stock/doctype/delivery_note/delivery_note.py | 5 ++--- .../stock/doctype/material_request/material_request.py | 2 +- .../doctype/stock_ledger_entry/stock_ledger_entry.json | 5 +++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py index 5f759b43bc6f..f2e299d7b618 100644 --- a/erpnext/controllers/sales_and_purchase_return.py +++ b/erpnext/controllers/sales_and_purchase_return.py @@ -99,9 +99,10 @@ def validate_returned_items(doc): frappe.throw(_("Row # {0}: Serial No {1} does not match with {2} {3}") .format(d.idx, s, doc.doctype, doc.return_against)) - if warehouse_mandatory and frappe.db.get_value("Item", d.item_code, "is_stock_item") \ - and not d.get("warehouse"): - frappe.throw(_("Warehouse is mandatory")) + if (warehouse_mandatory and not d.get("warehouse") and + frappe.db.get_value("Item", d.item_code, "is_stock_item", cache=True) + ): + frappe.throw(_("Warehouse is mandatory")) items_returned = True @@ -462,4 +463,4 @@ def get_returned_serial_nos(child_doc, parent_doc): for row in frappe.get_all(parent_doc.doctype, fields = fields, filters=filters): serial_nos.extend(get_serial_nos(row.serial_no)) - return serial_nos \ No newline at end of file + return serial_nos diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 7f28289760c2..b3e6f94b5ea1 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -469,7 +469,7 @@ def validate_for_duplicate_items(self): stock_items = [d.item_code, d.description, d.warehouse, ''] non_stock_items = [d.item_code, d.description] - if frappe.db.get_value("Item", d.item_code, "is_stock_item") == 1: + if frappe.db.get_value("Item", d.item_code, "is_stock_item", cache=True) == 1: duplicate_items_msg = _("Item {0} entered multiple times.").format(frappe.bold(d.item_code)) duplicate_items_msg += "

" duplicate_items_msg += _("Please enable {} in {} to allow same item in multiple rows").format( diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 508e17c3409a..8790391a36d2 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -113,7 +113,7 @@ def validate(self): self.item_has_batch_enabled() def item_has_batch_enabled(self): - if frappe.db.get_value("Item", self.item, "has_batch_no") == 0: + if frappe.db.get_value("Item", self.item, "has_batch_no", cache=True) == 0: frappe.throw(_("The selected item cannot have Batch")) def before_save(self): @@ -230,9 +230,8 @@ def set_batch_nos(doc, warehouse_field, throw=False): """Automatically select `batch_no` for outgoing items in item table""" for d in doc.items: qty = d.get('stock_qty') or d.get('transfer_qty') or d.get('qty') or 0 - has_batch_no = frappe.db.get_value('Item', d.item_code, 'has_batch_no') warehouse = d.get(warehouse_field, None) - if has_batch_no and warehouse and qty > 0: + if warehouse and qty > 0 and frappe.db.get_value('Item', d.item_code, 'has_batch_no'): if not d.batch_no: d.batch_no = get_batch_no(d.item_code, warehouse, qty, throw, d.serial_no) else: @@ -308,4 +307,4 @@ def validate_serial_no_with_batch(serial_nos, item_code): message = "Serial Nos" if len(serial_nos) > 1 else "Serial No" frappe.throw(_("There is no batch found against the {0}: {1}") - .format(message, serial_no_link)) \ No newline at end of file + .format(message, serial_no_link)) diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py index dd31965facee..fe9153679bbd 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.py +++ b/erpnext/stock/doctype/delivery_note/delivery_note.py @@ -181,9 +181,8 @@ def validate_warehouse(self): super(DeliveryNote, self).validate_warehouse() for d in self.get_item_list(): - if frappe.db.get_value("Item", d['item_code'], "is_stock_item") == 1: - if not d['warehouse']: - frappe.throw(_("Warehouse required for stock Item {0}").format(d["item_code"])) + if not d['warehouse'] and frappe.db.get_value("Item", d['item_code'], "is_stock_item", cache=True) == 1: + frappe.throw(_("Warehouse required for stock Item {0}").format(d["item_code"])) def update_current_stock(self): diff --git a/erpnext/stock/doctype/material_request/material_request.py b/erpnext/stock/doctype/material_request/material_request.py index 335175f21d1c..901971b1f245 100644 --- a/erpnext/stock/doctype/material_request/material_request.py +++ b/erpnext/stock/doctype/material_request/material_request.py @@ -189,7 +189,7 @@ def update_requested_qty(self, mr_item_rows=None): item_wh_list = [] for d in self.get("items"): if (not mr_item_rows or d.name in mr_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") == 1 and d.warehouse: + and d.warehouse and frappe.db.get_value("Item", d.item_code, "is_stock_item", cache=True) == 1 : item_wh_list.append([d.item_code, d.warehouse]) for item_code, warehouse in item_wh_list: diff --git a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json index 2463a21ed619..ce40678c3431 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json +++ b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json @@ -266,7 +266,8 @@ "fieldname": "is_cancelled", "fieldtype": "Check", "label": "Is Cancelled", - "report_hide": 1 + "report_hide": 1, + "search_index": 1 }, { "default": "1", @@ -316,7 +317,7 @@ "in_create": 1, "index_web_pages_for_search": 1, "links": [], - "modified": "2020-09-07 11:10:35.318872", + "modified": "2021-06-23 20:45:54.140217", "modified_by": "Administrator", "module": "Stock", "name": "Stock Ledger Entry",