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
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-09-01 09:54:25 +00:00
|
|
|
import json
|
2022-03-31 08:51:01 +00:00
|
|
|
from typing import Dict, Optional
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-09-01 09:54:25 +00:00
|
|
|
import frappe
|
2017-09-28 05:26:27 +00:00
|
|
|
from frappe.utils.nestedset import get_root_of
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
from erpnext.accounts.doctype.pos_invoice.pos_invoice import get_stock_availability
|
2022-03-21 07:17:35 +00:00
|
|
|
from erpnext.accounts.doctype.pos_profile.pos_profile import get_child_nodes, get_item_groups
|
2022-04-01 09:50:40 +00:00
|
|
|
from erpnext.stock.utils import scan_barcode
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2017-08-23 06:45:10 +00:00
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
def search_by_term(search_term, warehouse, price_list):
|
2021-06-15 14:23:57 +00:00
|
|
|
result = search_for_serial_or_batch_or_barcode_number(search_term) or {}
|
2021-06-03 07:28:53 +00:00
|
|
|
|
|
|
|
item_code = result.get("item_code") or search_term
|
|
|
|
serial_no = result.get("serial_no") or ""
|
|
|
|
batch_no = result.get("batch_no") or ""
|
|
|
|
barcode = result.get("barcode") or ""
|
|
|
|
|
|
|
|
if result:
|
|
|
|
item_info = frappe.db.get_value(
|
|
|
|
"Item",
|
|
|
|
item_code,
|
|
|
|
[
|
|
|
|
"name as item_code",
|
|
|
|
"item_name",
|
|
|
|
"description",
|
|
|
|
"stock_uom",
|
|
|
|
"image as item_image",
|
|
|
|
"is_stock_item",
|
|
|
|
],
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
|
2022-01-17 14:08:05 +00:00
|
|
|
item_stock_qty, is_stock_item = get_stock_availability(item_code, warehouse)
|
2021-06-03 07:28:53 +00:00
|
|
|
price_list_rate, currency = frappe.db.get_value(
|
|
|
|
"Item Price",
|
|
|
|
{"price_list": price_list, "item_code": item_code},
|
2021-06-15 14:23:57 +00:00
|
|
|
["price_list_rate", "currency"],
|
|
|
|
) or [None, None]
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
item_info.update(
|
|
|
|
{
|
|
|
|
"serial_no": serial_no,
|
|
|
|
"batch_no": batch_no,
|
|
|
|
"barcode": barcode,
|
|
|
|
"price_list_rate": price_list_rate,
|
|
|
|
"currency": currency,
|
|
|
|
"actual_qty": item_stock_qty,
|
|
|
|
}
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
return {"items": [item_info]}
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-02-15 06:09:45 +00:00
|
|
|
|
2017-08-23 06:45:10 +00:00
|
|
|
@frappe.whitelist()
|
2021-06-03 07:28:53 +00:00
|
|
|
def get_items(start, page_length, price_list, item_group, pos_profile, search_term=""):
|
|
|
|
warehouse, hide_unavailable_items = frappe.db.get_value(
|
|
|
|
"POS Profile", pos_profile, ["warehouse", "hide_unavailable_items"]
|
|
|
|
)
|
|
|
|
|
2020-10-26 05:47:04 +00:00
|
|
|
result = []
|
2018-08-07 09:08:50 +00:00
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
if search_term:
|
2021-06-15 14:23:57 +00:00
|
|
|
result = search_by_term(search_term, warehouse, price_list) or []
|
2021-06-03 07:28:53 +00:00
|
|
|
if result:
|
|
|
|
return result
|
2018-08-07 09:08:50 +00:00
|
|
|
|
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
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
condition = get_conditions(search_term)
|
2020-10-26 05:47:04 +00:00
|
|
|
condition += get_item_group_condition(pos_profile)
|
2017-11-29 08:25:13 +00:00
|
|
|
|
2017-08-28 11:49:28 +00:00
|
|
|
lft, rgt = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"])
|
2018-08-07 09:08:50 +00:00
|
|
|
|
2020-10-26 05:47:04 +00:00
|
|
|
bin_join_selection, bin_join_condition = "", ""
|
2020-10-29 07:53:50 +00:00
|
|
|
if hide_unavailable_items:
|
2020-10-26 05:47:04 +00:00
|
|
|
bin_join_selection = ", `tabBin` bin"
|
|
|
|
bin_join_condition = (
|
|
|
|
"AND bin.warehouse = %(warehouse)s AND bin.item_code = item.name AND bin.actual_qty > 0"
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-04-11 14:22:00 +00:00
|
|
|
|
2020-04-14 08:35:14 +00:00
|
|
|
items_data = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT
|
2020-10-26 05:47:04 +00:00
|
|
|
item.name AS item_code,
|
|
|
|
item.item_name,
|
|
|
|
item.description,
|
|
|
|
item.stock_uom,
|
|
|
|
item.image AS item_image,
|
|
|
|
item.is_stock_item
|
2019-04-11 14:22:00 +00:00
|
|
|
FROM
|
2020-10-26 05:47:04 +00:00
|
|
|
`tabItem` item {bin_join_selection}
|
2019-04-11 14:22:00 +00:00
|
|
|
WHERE
|
2020-10-26 05:47:04 +00:00
|
|
|
item.disabled = 0
|
|
|
|
AND item.has_variants = 0
|
|
|
|
AND item.is_sales_item = 1
|
|
|
|
AND item.is_fixed_asset = 0
|
|
|
|
AND item.item_group in (SELECT name FROM `tabItem Group` WHERE lft >= {lft} AND rgt <= {rgt})
|
|
|
|
AND {condition}
|
|
|
|
{bin_join_condition}
|
2020-04-14 08:35:14 +00:00
|
|
|
ORDER BY
|
2020-10-26 05:47:04 +00:00
|
|
|
item.name asc
|
2020-04-14 08:35:14 +00:00
|
|
|
LIMIT
|
2022-06-14 05:19:07 +00:00
|
|
|
{page_length} offset {start}""".format(
|
2020-04-14 08:35:14 +00:00
|
|
|
start=start,
|
|
|
|
page_length=page_length,
|
|
|
|
lft=lft,
|
|
|
|
rgt=rgt,
|
2020-10-26 05:47:04 +00:00
|
|
|
condition=condition,
|
|
|
|
bin_join_selection=bin_join_selection,
|
|
|
|
bin_join_condition=bin_join_condition,
|
|
|
|
),
|
|
|
|
{"warehouse": warehouse},
|
|
|
|
as_dict=1,
|
|
|
|
)
|
2019-04-11 14:22:00 +00:00
|
|
|
|
|
|
|
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]},
|
|
|
|
)
|
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
item_prices = {}
|
2019-04-11 14:22:00 +00:00
|
|
|
for d in item_prices_data:
|
|
|
|
item_prices[d.item_code] = d
|
|
|
|
|
|
|
|
for item in items_data:
|
2020-03-26 13:46:36 +00:00
|
|
|
item_code = item.item_code
|
|
|
|
item_price = item_prices.get(item_code) or {}
|
2022-01-17 14:08:05 +00:00
|
|
|
item_stock_qty, is_stock_item = get_stock_availability(item_code, warehouse)
|
2020-10-26 05:47:04 +00:00
|
|
|
|
|
|
|
row = {}
|
|
|
|
row.update(item)
|
|
|
|
row.update(
|
|
|
|
{
|
|
|
|
"price_list_rate": item_price.get("price_list_rate"),
|
|
|
|
"currency": item_price.get("currency"),
|
|
|
|
"actual_qty": item_stock_qty,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result.append(row)
|
2019-04-11 14:22:00 +00:00
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
return {"items": result}
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-08-24 09:57:55 +00:00
|
|
|
|
2018-11-13 05:41:32 +00:00
|
|
|
@frappe.whitelist()
|
2022-03-31 08:51:01 +00:00
|
|
|
def search_for_serial_or_batch_or_barcode_number(search_value: str) -> Dict[str, Optional[str]]:
|
2022-04-01 09:50:40 +00:00
|
|
|
return scan_barcode(search_value)
|
2018-12-10 11:46:39 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
def get_conditions(search_term):
|
2021-04-26 17:18:13 +00:00
|
|
|
condition = "("
|
2021-06-03 07:28:53 +00:00
|
|
|
condition += """item.name like {search_term}
|
|
|
|
or item.item_name like {search_term}""".format(
|
|
|
|
search_term=frappe.db.escape("%" + search_term + "%")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2021-06-03 07:28:53 +00:00
|
|
|
condition += add_search_fields_condition(search_term)
|
2021-04-26 17:18:13 +00:00
|
|
|
condition += ")"
|
|
|
|
|
|
|
|
return condition
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-06-03 07:28:53 +00:00
|
|
|
def add_search_fields_condition(search_term):
|
2021-04-26 17:18:13 +00:00
|
|
|
condition = ""
|
|
|
|
search_fields = frappe.get_all("POS Search Fields", fields=["fieldname"])
|
|
|
|
if search_fields:
|
|
|
|
for field in search_fields:
|
2021-06-03 07:28:53 +00:00
|
|
|
condition += " or item.`{0}` like {1}".format(
|
|
|
|
field["fieldname"], frappe.db.escape("%" + search_term + "%")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2021-04-26 17:18:13 +00:00
|
|
|
return condition
|
2017-10-04 09:03:12 +00:00
|
|
|
|
2022-03-28 13:22:46 +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:
|
2020-10-26 05:47:04 +00:00
|
|
|
cond = "and item.item_group in (%s)" % (", ".join(["%s"] * len(item_groups)))
|
2017-11-29 08:25:13 +00:00
|
|
|
|
|
|
|
return cond % tuple(item_groups)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-02 07:05:41 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2017-11-29 08:25:13 +00:00
|
|
|
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`
|
2022-06-14 05:19:07 +00:00
|
|
|
where {condition} and (name like %(txt)s) limit {page_len} offset {start}""".format(
|
2017-11-29 08:25:13 +00:00
|
|
|
condition=cond, start=start, page_len=page_len
|
|
|
|
),
|
2018-08-07 09:08:50 +00:00
|
|
|
{"txt": "%%%s%%" % txt},
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-12-09 12:21:32 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2020-07-23 13:21:26 +00:00
|
|
|
def check_opening_entry(user):
|
2020-08-05 14:12:25 +00:00
|
|
|
open_vouchers = frappe.db.get_all(
|
|
|
|
"POS Opening Entry",
|
2020-07-23 13:21:26 +00:00
|
|
|
filters={"user": user, "pos_closing_entry": ["in", ["", None]], "docstatus": 1},
|
|
|
|
fields=["name", "company", "pos_profile", "period_start_date"],
|
|
|
|
order_by="period_start_date desc",
|
|
|
|
)
|
|
|
|
|
|
|
|
return open_vouchers
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def create_opening_voucher(pos_profile, company, balance_details):
|
|
|
|
balance_details = json.loads(balance_details)
|
|
|
|
|
|
|
|
new_pos_opening = frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "POS Opening Entry",
|
|
|
|
"period_start_date": frappe.utils.get_datetime(),
|
|
|
|
"posting_date": frappe.utils.getdate(),
|
|
|
|
"user": frappe.session.user,
|
|
|
|
"pos_profile": pos_profile,
|
|
|
|
"company": company,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
new_pos_opening.set("balance_details", balance_details)
|
|
|
|
new_pos_opening.submit()
|
|
|
|
|
|
|
|
return new_pos_opening.as_dict()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_past_order_list(search_term, status, limit=20):
|
|
|
|
fields = ["name", "grand_total", "currency", "customer", "posting_time", "posting_date"]
|
|
|
|
invoice_list = []
|
|
|
|
|
|
|
|
if search_term and status:
|
|
|
|
invoices_by_customer = frappe.db.get_all(
|
|
|
|
"POS Invoice",
|
|
|
|
filters={"customer": ["like", "%{}%".format(search_term)], "status": status},
|
|
|
|
fields=fields,
|
|
|
|
)
|
|
|
|
invoices_by_name = frappe.db.get_all(
|
|
|
|
"POS Invoice",
|
|
|
|
filters={"name": ["like", "%{}%".format(search_term)], "status": status},
|
|
|
|
fields=fields,
|
|
|
|
)
|
|
|
|
|
|
|
|
invoice_list = invoices_by_customer + invoices_by_name
|
|
|
|
elif status:
|
|
|
|
invoice_list = frappe.db.get_all("POS Invoice", filters={"status": status}, fields=fields)
|
2020-08-05 14:12:25 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
return invoice_list
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def set_customer_info(fieldname, customer, value=""):
|
|
|
|
if fieldname == "loyalty_program":
|
|
|
|
frappe.db.set_value("Customer", customer, "loyalty_program", value)
|
|
|
|
|
|
|
|
contact = frappe.get_cached_value("Customer", customer, "customer_primary_contact")
|
2020-11-03 05:21:09 +00:00
|
|
|
if not contact:
|
|
|
|
contact = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT parent FROM `tabDynamic Link`
|
|
|
|
WHERE
|
|
|
|
parenttype = 'Contact' AND
|
|
|
|
parentfield = 'links' AND
|
|
|
|
link_doctype = 'Customer' AND
|
|
|
|
link_name = %s
|
|
|
|
""",
|
|
|
|
(customer),
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
contact = contact[0].get("parent") if contact else None
|
|
|
|
|
|
|
|
if not contact:
|
|
|
|
new_contact = frappe.new_doc("Contact")
|
|
|
|
new_contact.is_primary_contact = 1
|
|
|
|
new_contact.first_name = customer
|
|
|
|
new_contact.set("links", [{"link_doctype": "Customer", "link_name": customer}])
|
|
|
|
new_contact.save()
|
|
|
|
contact = new_contact.name
|
|
|
|
frappe.db.set_value("Customer", customer, "customer_primary_contact", contact)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-11-03 05:21:09 +00:00
|
|
|
contact_doc = frappe.get_doc("Contact", contact)
|
|
|
|
if fieldname == "email_id":
|
|
|
|
contact_doc.set("email_ids", [{"email_id": value, "is_primary": 1}])
|
|
|
|
frappe.db.set_value("Customer", customer, "email_id", value)
|
|
|
|
elif fieldname == "mobile_no":
|
|
|
|
contact_doc.set("phone_nos", [{"phone": value, "is_primary_mobile_no": 1}])
|
|
|
|
frappe.db.set_value("Customer", customer, "mobile_no", value)
|
2021-04-26 17:18:13 +00:00
|
|
|
contact_doc.save()
|
2022-03-21 07:17:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-21 07:17:35 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_pos_profile_data(pos_profile):
|
|
|
|
pos_profile = frappe.get_doc("POS Profile", pos_profile)
|
|
|
|
pos_profile = pos_profile.as_dict()
|
|
|
|
|
|
|
|
_customer_groups_with_children = []
|
|
|
|
for row in pos_profile.customer_groups:
|
|
|
|
children = get_child_nodes("Customer Group", row.customer_group)
|
|
|
|
_customer_groups_with_children.extend(children)
|
|
|
|
|
|
|
|
pos_profile.customer_groups = _customer_groups_with_children
|
|
|
|
return pos_profile
|