2017-08-23 06:45:10 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe, json
|
2017-09-28 05:26:27 +00:00
|
|
|
from frappe.utils.nestedset import get_root_of
|
2018-01-23 12:58:05 +00:00
|
|
|
from frappe.utils import cint
|
2017-11-29 08:25:13 +00:00
|
|
|
from erpnext.accounts.doctype.pos_profile.pos_profile import get_item_groups
|
2017-08-23 06:45:10 +00:00
|
|
|
|
2018-02-15 06:09:45 +00:00
|
|
|
from six import string_types
|
|
|
|
|
2017-08-23 06:45:10 +00:00
|
|
|
@frappe.whitelist()
|
2017-11-29 08:25:13 +00:00
|
|
|
def get_items(start, page_length, price_list, item_group, search_value="", pos_profile=None):
|
2018-11-13 05:41:32 +00:00
|
|
|
data = dict()
|
2018-08-07 09:08:50 +00:00
|
|
|
warehouse = ""
|
|
|
|
display_items_in_stock = 0
|
|
|
|
|
|
|
|
if pos_profile:
|
|
|
|
warehouse, display_items_in_stock = frappe.db.get_value('POS Profile', pos_profile, ['warehouse', 'display_items_in_stock'])
|
|
|
|
|
2017-09-28 05:26:27 +00:00
|
|
|
if not frappe.db.exists('Item Group', item_group):
|
|
|
|
item_group = get_root_of('Item Group')
|
2017-08-23 06:45:10 +00:00
|
|
|
|
2017-08-23 10:55:16 +00:00
|
|
|
if search_value:
|
2018-11-13 05:41:32 +00:00
|
|
|
data = search_serial_or_batch_or_barcode_number(search_value)
|
2017-08-23 06:45:10 +00:00
|
|
|
|
2018-11-13 05:41:32 +00:00
|
|
|
item_code = data.get("item_code") if data.get("item_code") else search_value
|
|
|
|
serial_no = data.get("serial_no") if data.get("serial_no") else ""
|
|
|
|
batch_no = data.get("batch_no") if data.get("batch_no") else ""
|
|
|
|
barcode = data.get("barcode") if data.get("barcode") else ""
|
2017-10-10 20:37:09 +00:00
|
|
|
|
|
|
|
item_code, condition = get_conditions(item_code, serial_no, batch_no, barcode)
|
2017-10-04 09:03:12 +00:00
|
|
|
|
2017-11-29 08:25:13 +00:00
|
|
|
if pos_profile:
|
|
|
|
condition += get_item_group_condition(pos_profile)
|
|
|
|
|
2017-08-28 11:49:28 +00:00
|
|
|
lft, rgt = frappe.db.get_value('Item Group', item_group, ['lft', 'rgt'])
|
2017-08-23 06:45:10 +00:00
|
|
|
# locate function is used to sort by closest match from the beginning of the value
|
2018-08-07 09:08:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
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.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})
|
2018-09-27 10:09:34 +00:00
|
|
|
and {condition} limit {start}, {page_length}""".format(
|
|
|
|
start=start,
|
|
|
|
page_length=page_length,
|
|
|
|
lft=lft,
|
|
|
|
rgt=rgt,
|
|
|
|
condition=condition
|
|
|
|
), {
|
2018-08-07 09:08:50 +00:00
|
|
|
'price_list': price_list
|
2018-09-27 10:09:34 +00:00
|
|
|
}, as_dict=1)
|
2018-08-07 09:08:50 +00:00
|
|
|
|
|
|
|
res = {
|
|
|
|
'items': res
|
|
|
|
}
|
|
|
|
|
|
|
|
elif display_items_in_stock == 1:
|
|
|
|
query = """select i.name as item_code, i.item_name, i.image as item_image,
|
|
|
|
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 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"""
|
|
|
|
|
|
|
|
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} 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 = {
|
2017-08-23 10:55:16 +00:00
|
|
|
'items': res
|
2018-08-07 09:08:50 +00:00
|
|
|
}
|
2017-08-23 10:55:16 +00:00
|
|
|
|
|
|
|
if serial_no:
|
|
|
|
res.update({
|
|
|
|
'serial_no': serial_no
|
|
|
|
})
|
|
|
|
|
2017-08-29 09:57:17 +00:00
|
|
|
if batch_no:
|
|
|
|
res.update({
|
|
|
|
'batch_no': batch_no
|
|
|
|
})
|
|
|
|
|
2017-11-10 09:59:14 +00:00
|
|
|
if barcode:
|
|
|
|
res.update({
|
|
|
|
'barcode': barcode
|
|
|
|
})
|
|
|
|
|
2017-08-24 09:57:55 +00:00
|
|
|
return res
|
|
|
|
|
2018-11-13 05:41:32 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def search_serial_or_batch_or_barcode_number(search_value):
|
|
|
|
# search barcode no
|
|
|
|
barcode_data = frappe.db.get_value('Item Barcode', {'barcode': search_value}, ['barcode', 'parent as item_code'], as_dict=True)
|
|
|
|
if barcode_data:
|
|
|
|
return barcode_data
|
|
|
|
|
|
|
|
# search serial no
|
|
|
|
serial_no_data = frappe.db.get_value('Serial No', search_value, ['name as serial_no', 'item_code'], as_dict=True)
|
|
|
|
if serial_no_data:
|
|
|
|
return serial_no_data
|
|
|
|
|
|
|
|
# search batch no
|
|
|
|
batch_no_data = frappe.db.get_value('Batch', search_value, ['name as batch_no', 'item as item_code'], as_dict=True)
|
|
|
|
if batch_no_data:
|
|
|
|
return batch_no_data
|
|
|
|
|
2018-12-10 11:46:39 +00:00
|
|
|
return {}
|
|
|
|
|
2017-10-10 20:37:09 +00:00
|
|
|
def get_conditions(item_code, serial_no, batch_no, barcode):
|
|
|
|
if serial_no or batch_no or barcode:
|
2017-10-25 11:34:56 +00:00
|
|
|
return frappe.db.escape(item_code), "i.name = %(item_code)s"
|
2017-10-04 09:03:12 +00:00
|
|
|
|
2017-10-25 11:34:56 +00:00
|
|
|
condition = """(i.name like %(item_code)s
|
2017-10-10 20:37:09 +00:00
|
|
|
or i.item_name like %(item_code)s)"""
|
2017-10-04 09:03:12 +00:00
|
|
|
|
2018-09-21 04:50:52 +00:00
|
|
|
return frappe.db.escape('%' + item_code + '%'), condition
|
2017-10-04 09:03:12 +00:00
|
|
|
|
2017-11-29 08:25:13 +00:00
|
|
|
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)))
|
|
|
|
|
|
|
|
return cond % tuple(item_groups)
|
|
|
|
|
|
|
|
def item_group_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
item_groups = []
|
|
|
|
cond = "1=1"
|
|
|
|
pos_profile= filters.get('pos_profile')
|
|
|
|
|
|
|
|
if pos_profile:
|
|
|
|
item_groups = get_item_groups(pos_profile)
|
|
|
|
|
|
|
|
if item_groups:
|
|
|
|
cond = "name in (%s)"%(', '.join(['%s']*len(item_groups)))
|
|
|
|
cond = cond % tuple(item_groups)
|
|
|
|
|
|
|
|
return frappe.db.sql(""" select distinct name from `tabItem Group`
|
|
|
|
where {condition} and (name like %(txt)s) limit {start}, {page_len}"""
|
|
|
|
.format(condition = cond, start=start, page_len= page_len),
|
2018-08-07 09:08:50 +00:00
|
|
|
{'txt': '%%%s%%' % txt})
|