Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix & Feat: optional direct fetch, filter & limit to get_items() #317

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 108 additions & 2 deletions posawesome/fixtures/custom_field.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
"dt": "Batch",
"fetch_from": null,
"fetch_if_empty": 0,
"fieldname": "posa_btach_price",
"fieldname": "posa_batch_price",
"fieldtype": "Float",
"hidden": 0,
"hide_border": 0,
Expand All @@ -455,7 +455,7 @@
"length": 0,
"mandatory_depends_on": null,
"modified": "2020-10-26 02:31:58.913688",
"name": "Batch-posa_btach_price",
"name": "Batch-posa_batch_price",
"no_copy": 0,
"non_negative": 0,
"options": null,
Expand Down Expand Up @@ -3550,6 +3550,112 @@
"unique": 0,
"width": null
},
{
"allow_in_quick_entry": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"collapsible_depends_on": null,
"columns": 0,
"default": null,
"depends_on": null,
"description": null,
"docstatus": 0,
"doctype": "Custom Field",
"dt": "POS Profile",
"fetch_from": null,
"fetch_if_empty": 0,
"fieldname": "posa_limit_search",
"fieldtype": "Check",
"hidden": 0,
"hide_border": 0,
"hide_days": 0,
"hide_seconds": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_preview": 0,
"in_standard_filter": 0,
"insert_after": "posa_search_serial_no",
"label": "Limit Search Result",
"length": 0,
"mandatory_depends_on": null,
"modified": "2020-10-30 03:54:32.270370",
"name": "POS Profile-posa_limit_search",
"no_copy": 0,
"non_negative": 0,
"options": null,
"parent": null,
"parentfield": null,
"parenttype": null,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"print_width": null,
"read_only": 0,
"read_only_depends_on": null,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"translatable": 0,
"unique": 0,
"width": null
},
{
"allow_in_quick_entry": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"collapsible_depends_on": null,
"columns": 0,
"default": "500",
"depends_on": "eval:(doc.posa_limit_search)",
"description": "For best performance keep this under 1500",
"docstatus": 0,
"doctype": "Custom Field",
"dt": "POS Profile",
"fetch_from": null,
"fetch_if_empty": 0,
"fieldname": "posa_search_limit",
"fieldtype": "Int",
"hidden": 0,
"hide_border": 0,
"hide_days": 0,
"hide_seconds": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_preview": 0,
"in_standard_filter": 0,
"insert_after": "posa_limit_search",
"label": "Set the limit number for the search result",
"length": 0,
"mandatory_depends_on": null,
"modified": "2020-10-30 03:54:32.270370",
"name": "POS Profile-posa_search_limit",
"no_copy": 0,
"non_negative": 0,
"options": null,
"parent": null,
"parentfield": null,
"parenttype": null,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"print_width": null,
"read_only": 0,
"read_only_depends_on": null,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"translatable": 0,
"unique": 0,
"width": null
},
{
"allow_in_quick_entry": 0,
"allow_on_submit": 0,
Expand Down
2 changes: 1 addition & 1 deletion posawesome/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"POS Profile-posa_allow_partial_payment",
"POS Profile-posa_allow_credit_sale",
"POS Profile-posa_pos_awesome_advance_settings",
"Batch-posa_btach_price",
"Batch-posa_batch_price",
"POS Profile-posa_max_discount_allowed",
"POS Profile-posa_allow_return",
"POS Profile-posa_col_1",
Expand Down
91 changes: 72 additions & 19 deletions posawesome/posawesome/api/posapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,45 @@ def update_opening_shift_data(data, pos_profile):


@frappe.whitelist()
def get_items(pos_profile, price_list=None):
def get_items(pos_profile, price_list=None, item_group="", search_value=""):
pos_profile = json.loads(pos_profile)
data = dict()

posa_display_items_in_stock = pos_profile.get("posa_display_items_in_stock")
search_serial_no = pos_profile.get("posa_search_serial_no")
posa_show_template_items = pos_profile.get("posa_show_template_items")
warehouse = pos_profile.get("warehouse")
limit_search = pos_profile.get("posa_limit_search")
search_limit = pos_profile.get("posa_search_limit")

if not price_list:
price_list = pos_profile.get("selling_price_list")


condition = ""
limit = ""
condition += get_item_group_condition(pos_profile.get("name"))
if not pos_profile.get("posa_show_template_items"):

if(limit_search):
if search_value:
data = search_serial_or_batch_or_barcode_number(search_value, search_serial_no)

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 ""

condition += get_conditions(item_code, serial_no, batch_no, barcode)
if(item_group):
condition += " AND item_group like '%{item_group}%'".format(item_group=item_group)
limit = " LIMIT {search_limit}".format(search_limit=search_limit)

if not posa_show_template_items:
condition += " AND has_variants = 0"

result = []

items_data = frappe.db.sql(
"""

items_data = frappe.db.sql("""
SELECT
name AS item_code,
item_name,
Expand All @@ -152,12 +178,15 @@ def get_items(pos_profile, price_list=None):
disabled = 0
AND is_sales_item = 1
AND is_fixed_asset = 0
{0}
{condition}
ORDER BY
name asc
item_name asc
{limit}
""".format(
condition
),
condition=condition,
limit=limit
)
,
as_dict=1,
)

Expand Down Expand Up @@ -194,15 +223,15 @@ def get_items(pos_profile, price_list=None):
fields=["barcode", "posa_uom"],
)
serial_no_data = []
if pos_profile.get("posa_search_serial_no"):
if search_serial_no:
serial_no_data = frappe.get_all(
"Serial No",
filters={"item_code": item_code, "status": "Active"},
filters={"item_code": item_code, "status": "Active", "warehouse": warehouse},
fields=["name as serial_no"],
)
if pos_profile.get("posa_display_items_in_stock"):
if posa_display_items_in_stock:
item_stock_qty = get_stock_availability(
item_code, pos_profile.get("warehouse")
item_code, warehouse
)
attributes = ""
if pos_profile.get("posa_show_template_items") and item.has_variants:
Expand All @@ -214,7 +243,7 @@ def get_items(pos_profile, price_list=None):
fields=["attribute", "attribute_value"],
filters={"parent": item.item_code, "parentfield": "attributes"},
)
if pos_profile.get("posa_display_items_in_stock") and (
if posa_display_items_in_stock and (
not item_stock_qty or item_stock_qty < 0
):
pass
Expand All @@ -227,7 +256,7 @@ def get_items(pos_profile, price_list=None):
"currency": item_price.get("currency")
or pos_profile.get("currency"),
"item_barcode": item_barcode or [],
"actual_qty": 0,
"actual_qty": item_stock_qty or 0,
"serial_no_data": serial_no_data or [],
"attributes": attributes or "",
"item_attributes": item_attributes or "",
Expand All @@ -239,10 +268,10 @@ def get_items(pos_profile, price_list=None):


def get_item_group_condition(pos_profile):
cond = "and 1=1"
cond = " and 1=1"
item_groups = get_item_groups(pos_profile)
if item_groups:
cond = "and 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)

Expand Down Expand Up @@ -758,7 +787,7 @@ def get_items_details(pos_profile, items_data):

serial_no_data = frappe.get_all(
"Serial No",
filters={"item_code": item_code, "status": "Active"},
filters={"item_code": item_code, "status": "Active", "warehouse": warehouse},
fields=["name as serial_no"],
)

Expand All @@ -780,7 +809,7 @@ def get_items_details(pos_profile, items_data):
"batch_no": batch.batch_no,
"batch_qty": batch.qty,
"expiry_date": batch_doc.expiry_date,
"btach_price": batch_doc.posa_btach_price,
"batch_price": batch_doc.posa_batch_price,
}
)

Expand Down Expand Up @@ -1537,3 +1566,27 @@ def set_payment_schedule(doc):
d.base_payment_amount = flt(
d.payment_amount * doc.get("conversion_rate"), d.precision("base_payment_amount")
)

@frappe.whitelist()
def search_serial_or_batch_or_barcode_number(search_value, search_serial_no):
# 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
if 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
return {}

def get_conditions(item_code, serial_no, batch_no, barcode):

if serial_no or batch_no or barcode:
return " and name = {0}".format(frappe.db.escape(item_code))
return (""" and (name like {item_code} or item_name like {item_code})"""
.format(item_code=frappe.db.escape('%' + item_code + '%')))
Loading