Merge pull request #18864 from rohitwaghchaure/v11_optimized_code_to_get_items_for_pos

fix: optimized the code to fix slow loading of items in the POS
This commit is contained in:
rohitwaghchaure
2019-08-29 12:22:30 +05:30
committed by GitHub
2 changed files with 64 additions and 56 deletions

View File

@@ -1763,10 +1763,18 @@ erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
this.email_queue_list = this.get_email_queue() || {};
this.customers_list = this.get_customers_details() || {};
if (this.si_docs.length || this.email_queue_list || this.customers_list) {
if(this.customer_doc) {
this.freeze = this.customer_doc.display
}
freeze_screen = this.freeze_screen || false;
if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
this.freeze = true;
frappe.call({
method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
freeze: true,
freeze: freeze_screen,
args: {
doc_list: me.si_docs,
email_queue_list: me.email_queue_list,

View File

@@ -29,7 +29,7 @@ def get_items(start, page_length, price_list, item_group, search_value="", pos_p
batch_no = data.get("batch_no") if data.get("batch_no") else ""
barcode = data.get("barcode") if data.get("barcode") else ""
item_code, condition = get_conditions(item_code, serial_no, batch_no, barcode)
condition = get_conditions(item_code, serial_no, batch_no, barcode)
if pos_profile:
condition += get_item_group_condition(pos_profile)
@@ -38,58 +38,60 @@ def get_items(start, page_length, price_list, item_group, search_value="", pos_p
# locate function is used to sort by closest match from the beginning of the value
if display_items_in_stock == 0:
res = frappe.db.sql("""select i.name as item_code, i.item_name, i.image as item_image, i.idx as idx,
i.is_stock_item, item_det.price_list_rate, item_det.currency
from `tabItem` i LEFT JOIN
(select item_code, price_list_rate, currency from
`tabItem Price` where price_list=%(price_list)s) item_det
ON
(item_det.item_code=i.name or item_det.item_code=i.variant_of)
where
i.disabled = 0 and i.has_variants = 0 and i.is_sales_item = 1
and i.item_group in (select name from `tabItem Group` where lft >= {lft} and rgt <= {rgt})
and {condition} order by idx desc limit {start}, {page_length}""".format(start=start,page_length=page_length,lft=lft, rgt=rgt, condition=condition),
{
'item_code': item_code,
'price_list': price_list
} , as_dict=1)
result = []
res = {
'items': res
}
items_data = frappe.db.sql(""" SELECT name as item_code,
item_name, image as item_image, idx as idx,is_stock_item
FROM
`tabItem`
WHERE
disabled = 0 and has_variants = 0 and is_sales_item = 1
and item_group in (select name from `tabItem Group` where lft >= {lft} and rgt <= {rgt})
and {condition} order by idx desc limit {start}, {page_length}"""
.format(
start=start, page_length=page_length,
lft=lft, rgt=rgt,
condition=condition
), as_dict=1)
elif display_items_in_stock == 1:
query = """select i.name as item_code, i.item_name, i.image as item_image, i.idx as idx,
i.is_stock_item, item_det.price_list_rate, item_det.currency
from `tabItem` i LEFT JOIN
(select item_code, price_list_rate, currency from
`tabItem Price` where price_list=%(price_list)s) item_det
ON
(item_det.item_code=i.name or item_det.item_code=i.variant_of) INNER JOIN"""
if items_data:
items = [d.item_code for d in items_data]
item_prices_data = frappe.get_all("Item Price",
fields = ["item_code", "price_list_rate", "currency"],
filters = {'price_list': price_list, 'item_code': ['in', items]})
if warehouse is not None:
query = query + """ (select item_code,actual_qty from `tabBin` where warehouse=%(warehouse)s and actual_qty > 0 group by item_code) item_se"""
else:
query = query + """ (select item_code,sum(actual_qty) as actual_qty from `tabBin` group by item_code) item_se"""
item_prices, bin_data = {}, {}
for d in item_prices_data:
item_prices[d.item_code] = d
res = frappe.db.sql(query + """
ON
((item_se.item_code=i.name or item_det.item_code=i.variant_of) and item_se.actual_qty>0)
where
i.disabled = 0 and i.has_variants = 0 and i.is_sales_item = 1
and i.item_group in (select name from `tabItem Group` where lft >= {lft} and rgt <= {rgt})
and {condition} order by idx desc limit {start}, {page_length}""".format
(start=start,page_length=page_length,lft=lft, rgt=rgt, condition=condition),
{
'item_code': item_code,
'price_list': price_list,
'warehouse': warehouse
} , as_dict=1)
res = {
'items': res
}
if display_items_in_stock:
filters = {'actual_qty': [">", 0], 'item_code': ['in', items]}
if warehouse:
filters['warehouse'] = warehouse
bin_data = frappe._dict(
frappe.get_all("Bin", fields = ["item_code", "sum(actual_qty) as actual_qty"],
filters = filters, group_by = "item_code")
)
for item in items_data:
row = {}
row.update(item)
item_price = item_prices.get(item.item_code) or {}
row.update({
'price_list_rate': item_price.get('price_list_rate'),
'currency': item_price.get('currency'),
'actual_qty': bin_data.get('actual_qty')
})
result.append(row)
res = {
'items': result
}
if serial_no:
res.update({
@@ -129,18 +131,16 @@ def search_serial_or_batch_or_barcode_number(search_value):
def get_conditions(item_code, serial_no, batch_no, barcode):
if serial_no or batch_no or barcode:
return frappe.db.escape(item_code), "i.name = %(item_code)s"
return "name = '{0}'".format(frappe.db.escape(item_code))
condition = """(i.name like %(item_code)s
or i.item_name like %(item_code)s)"""
return '%%%s%%'%(frappe.db.escape(item_code)), condition
return """(name like '{item_code}'
or item_name like '{item_code}')""".format(item_code = frappe.db.escape('%' + item_code + '%'))
def get_item_group_condition(pos_profile):
cond = "and 1=1"
item_groups = get_item_groups(pos_profile)
if item_groups:
cond = "and i.item_group in (%s)"%(', '.join(['%s']*len(item_groups)))
cond = "and item_group in (%s)"%(', '.join(['%s']*len(item_groups)))
return cond % tuple(item_groups)