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
|
2017-08-23 06:45:10 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2017-08-28 11:49:28 +00:00
|
|
|
def get_items(start, page_length, price_list, item_group, search_value=""):
|
2017-08-23 10:55:16 +00:00
|
|
|
serial_no = ""
|
2017-08-29 09:57:17 +00:00
|
|
|
batch_no = ""
|
2017-10-10 20:37:09 +00:00
|
|
|
barcode = ""
|
|
|
|
|
2017-08-23 10:55:16 +00:00
|
|
|
item_code = search_value
|
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:
|
2017-08-23 06:45:10 +00:00
|
|
|
# search serial no
|
2017-08-23 10:55:16 +00:00
|
|
|
serial_no_data = frappe.db.get_value('Serial No', search_value, ['name', 'item_code'])
|
|
|
|
if serial_no_data:
|
|
|
|
serial_no, item_code = serial_no_data
|
2017-08-23 06:45:10 +00:00
|
|
|
|
2017-08-29 09:57:17 +00:00
|
|
|
if not serial_no:
|
|
|
|
batch_no_data = frappe.db.get_value('Batch', search_value, ['name', 'item'])
|
|
|
|
if batch_no_data:
|
|
|
|
batch_no, item_code = batch_no_data
|
|
|
|
|
2017-10-10 20:37:09 +00:00
|
|
|
if not serial_no and not batch_no:
|
|
|
|
barcode_data = frappe.db.get_value('Item', {'barcode': search_value}, ['name', 'barcode'])
|
|
|
|
if barcode_data:
|
|
|
|
item_code, barcode = barcode_data
|
|
|
|
|
|
|
|
item_code, condition = get_conditions(item_code, serial_no, batch_no, barcode)
|
2017-10-04 09:03:12 +00:00
|
|
|
|
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
|
2017-08-23 10:55:16 +00:00
|
|
|
res = frappe.db.sql("""select i.name as item_code, i.item_name, i.image as item_image,
|
2017-10-25 08:29:29 +00:00
|
|
|
i.is_stock_item, item_det.price_list_rate, item_det.currency
|
2017-08-23 06:45:10 +00:00
|
|
|
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
|
2017-09-27 06:46:58 +00:00
|
|
|
i.disabled = 0 and i.has_variants = 0 and i.is_sales_item = 1
|
2017-08-28 11:49:28 +00:00
|
|
|
and i.item_group in (select name from `tabItem Group` where lft >= {lft} and rgt <= {rgt})
|
2017-10-04 09:03:12 +00:00
|
|
|
and {condition}
|
|
|
|
limit {start}, {page_length}""".format(start=start,
|
|
|
|
page_length=page_length, lft=lft, rgt=rgt, condition=condition),
|
2017-08-23 10:55:16 +00:00
|
|
|
{
|
2017-10-04 09:03:12 +00:00
|
|
|
'item_code': item_code,
|
2017-08-23 10:55:16 +00:00
|
|
|
'price_list': price_list
|
|
|
|
} , as_dict=1)
|
|
|
|
|
|
|
|
res = {
|
|
|
|
'items': res
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-24 09:57:55 +00:00
|
|
|
return res
|
|
|
|
|
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
|
|
|
|
|
|
|
return '%%%s%%'%(frappe.db.escape(item_code)), condition
|
|
|
|
|
2017-08-24 09:57:55 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def submit_invoice(doc):
|
|
|
|
if isinstance(doc, basestring):
|
|
|
|
args = json.loads(doc)
|
|
|
|
|
|
|
|
doc = frappe.new_doc('Sales Invoice')
|
|
|
|
doc.update(args)
|
|
|
|
doc.run_method("set_missing_values")
|
|
|
|
doc.run_method("calculate_taxes_and_totals")
|
|
|
|
doc.submit()
|
|
|
|
|
|
|
|
return doc
|