2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-06-03 14:32:58 +00:00
|
|
|
import json
|
2017-01-18 10:05:01 +00:00
|
|
|
from collections import defaultdict
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-01-06 10:04:15 +00:00
|
|
|
import frappe
|
2021-09-14 08:28:18 +00:00
|
|
|
from frappe import scrub
|
2017-01-13 13:23:11 +00:00
|
|
|
from frappe.desk.reportview import get_filters_cond, get_match_cond
|
2020-01-06 10:04:15 +00:00
|
|
|
from frappe.utils import nowdate, unique
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-03-31 05:15:32 +00:00
|
|
|
import erpnext
|
2020-01-06 10:04:15 +00:00
|
|
|
from erpnext.stock.get_item_details import _get_item_tax_template
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
# searches for active employees
|
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2013-07-08 13:15:55 +00:00
|
|
|
def employee_query(doctype, txt, searchfield, start, page_len, filters):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Employee"
|
2016-11-16 09:59:01 +00:00
|
|
|
conditions = []
|
2022-08-03 05:42:30 +00:00
|
|
|
fields = get_fields(doctype, ["name", "employee_name"])
|
2020-05-18 08:56:26 +00:00
|
|
|
|
|
|
|
return frappe.db.sql(
|
|
|
|
"""select {fields} from `tabEmployee`
|
2021-06-23 05:51:38 +00:00
|
|
|
where status in ('Active', 'Suspended')
|
2014-04-11 11:21:27 +00:00
|
|
|
and docstatus < 2
|
2014-07-09 07:45:03 +00:00
|
|
|
and ({key} like %(txt)s
|
|
|
|
or employee_name like %(txt)s)
|
2016-11-16 09:59:01 +00:00
|
|
|
{fcond} {mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2022-06-17 15:47:48 +00:00
|
|
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
|
|
|
(case when locate(%(_txt)s, employee_name) > 0 then locate(%(_txt)s, employee_name) else 99999 end),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
2014-04-16 09:51:46 +00:00
|
|
|
name, employee_name
|
2022-06-14 05:19:07 +00:00
|
|
|
limit %(page_len)s offset %(start)s""".format(
|
2014-07-09 07:45:03 +00:00
|
|
|
**{
|
2020-05-18 08:56:26 +00:00
|
|
|
"fields": ", ".join(fields),
|
2014-07-09 07:45:03 +00:00
|
|
|
"key": searchfield,
|
2016-11-16 09:59:01 +00:00
|
|
|
"fcond": get_filters_cond(doctype, filters, conditions),
|
2014-07-09 07:45:03 +00:00
|
|
|
"mcond": get_match_cond(doctype),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
{"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
|
|
|
|
)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
|
|
|
# searches for leads which are not converted
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2014-04-11 11:21:27 +00:00
|
|
|
def lead_query(doctype, txt, searchfield, start, page_len, filters):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Lead"
|
|
|
|
fields = get_fields(doctype, ["name", "lead_name", "company_name"])
|
2020-05-18 08:56:26 +00:00
|
|
|
|
|
|
|
return frappe.db.sql(
|
|
|
|
"""select {fields} from `tabLead`
|
2014-04-11 11:21:27 +00:00
|
|
|
where docstatus < 2
|
|
|
|
and ifnull(status, '') != 'Converted'
|
2014-07-09 07:45:03 +00:00
|
|
|
and ({key} like %(txt)s
|
|
|
|
or lead_name like %(txt)s
|
|
|
|
or company_name like %(txt)s)
|
|
|
|
{mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2022-06-17 15:47:48 +00:00
|
|
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
|
|
|
(case when locate(%(_txt)s, lead_name) > 0 then locate(%(_txt)s, lead_name) else 99999 end),
|
|
|
|
(case when locate(%(_txt)s, company_name) > 0 then locate(%(_txt)s, company_name) else 99999 end),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
2014-04-16 09:51:46 +00:00
|
|
|
name, lead_name
|
2022-06-14 05:19:07 +00:00
|
|
|
limit %(page_len)s offset %(start)s""".format(
|
2014-07-09 07:45:03 +00:00
|
|
|
**{"fields": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
|
|
|
|
),
|
|
|
|
{"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
# searches for customer
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2022-10-24 10:40:47 +00:00
|
|
|
def customer_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Customer"
|
2017-07-25 08:33:01 +00:00
|
|
|
conditions = []
|
2014-02-14 10:17:51 +00:00
|
|
|
cust_master_name = frappe.defaults.get_user_default("cust_master_name")
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2022-10-24 10:18:34 +00:00
|
|
|
fields = ["name"]
|
|
|
|
if cust_master_name != "Customer Name":
|
2022-11-03 05:54:58 +00:00
|
|
|
fields.append("customer_name")
|
2017-01-13 13:23:11 +00:00
|
|
|
|
2022-08-03 05:42:30 +00:00
|
|
|
fields = get_fields(doctype, fields)
|
|
|
|
searchfields = frappe.get_meta(doctype).get_search_fields()
|
2021-06-11 10:30:48 +00:00
|
|
|
searchfields = " or ".join(field + " like %(txt)s" for field in searchfields)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2014-07-09 07:45:03 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select {fields} from `tabCustomer`
|
2014-04-11 11:21:27 +00:00
|
|
|
where docstatus < 2
|
2017-06-23 17:32:52 +00:00
|
|
|
and ({scond}) and disabled=0
|
2017-07-25 08:33:01 +00:00
|
|
|
{fcond} {mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2022-06-17 15:47:48 +00:00
|
|
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
|
|
|
(case when locate(%(_txt)s, customer_name) > 0 then locate(%(_txt)s, customer_name) else 99999 end),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
2014-04-11 11:21:27 +00:00
|
|
|
name, customer_name
|
2022-06-14 05:19:07 +00:00
|
|
|
limit %(page_len)s offset %(start)s""".format(
|
2014-07-09 07:45:03 +00:00
|
|
|
**{
|
2020-05-18 08:56:26 +00:00
|
|
|
"fields": ", ".join(fields),
|
2017-06-23 17:32:52 +00:00
|
|
|
"scond": searchfields,
|
2017-07-25 08:33:01 +00:00
|
|
|
"mcond": get_match_cond(doctype),
|
|
|
|
"fcond": get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
|
2014-07-09 07:45:03 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
{"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
|
2022-10-24 10:40:47 +00:00
|
|
|
as_dict=as_dict,
|
2014-07-09 07:45:03 +00:00
|
|
|
)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
# searches for supplier
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2022-10-24 10:40:47 +00:00
|
|
|
def supplier_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Supplier"
|
2014-02-14 10:17:51 +00:00
|
|
|
supp_master_name = frappe.defaults.get_user_default("supp_master_name")
|
2020-08-05 14:12:25 +00:00
|
|
|
|
2022-10-24 10:18:34 +00:00
|
|
|
fields = ["name"]
|
|
|
|
if supp_master_name != "Supplier Name":
|
2022-11-03 05:54:58 +00:00
|
|
|
fields.append("supplier_name")
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2022-08-03 05:42:30 +00:00
|
|
|
fields = get_fields(doctype, fields)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2014-07-09 07:45:03 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select {field} from `tabSupplier`
|
2014-04-11 11:21:27 +00:00
|
|
|
where docstatus < 2
|
2014-07-09 07:45:03 +00:00
|
|
|
and ({key} like %(txt)s
|
2021-10-27 13:45:44 +00:00
|
|
|
or supplier_name like %(txt)s) and disabled=0
|
2022-06-17 15:47:48 +00:00
|
|
|
and (on_hold = 0 or (on_hold = 1 and CURRENT_DATE > release_date))
|
2014-07-09 07:45:03 +00:00
|
|
|
{mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2022-06-17 15:47:48 +00:00
|
|
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
|
|
|
(case when locate(%(_txt)s, supplier_name) > 0 then locate(%(_txt)s, supplier_name) else 99999 end),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
2014-04-11 11:21:27 +00:00
|
|
|
name, supplier_name
|
2022-06-14 05:19:07 +00:00
|
|
|
limit %(page_len)s offset %(start)s""".format(
|
2014-07-09 07:45:03 +00:00
|
|
|
**{"field": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
|
|
|
|
),
|
|
|
|
{"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
|
2022-10-24 10:40:47 +00:00
|
|
|
as_dict=as_dict,
|
2014-07-09 07:45:03 +00:00
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2013-07-16 11:54:17 +00:00
|
|
|
def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Account"
|
2020-03-31 05:15:32 +00:00
|
|
|
company_currency = erpnext.get_company_currency(filters.get("company"))
|
|
|
|
|
2020-08-05 14:12:25 +00:00
|
|
|
def get_accounts(with_account_type_filter):
|
|
|
|
account_type_condition = ""
|
|
|
|
if with_account_type_filter:
|
|
|
|
account_type_condition = "AND account_type in %(account_types)s"
|
|
|
|
|
|
|
|
accounts = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT name, parent_account
|
|
|
|
FROM `tabAccount`
|
|
|
|
WHERE `tabAccount`.docstatus!=2
|
|
|
|
{account_type_condition}
|
|
|
|
AND is_group = 0
|
|
|
|
AND company = %(company)s
|
2022-05-11 07:31:06 +00:00
|
|
|
AND disabled = %(disabled)s
|
2022-03-22 12:56:58 +00:00
|
|
|
AND (account_currency = %(currency)s or ifnull(account_currency, '') = '')
|
2020-08-05 14:12:25 +00:00
|
|
|
AND `{searchfield}` LIKE %(txt)s
|
2020-09-21 08:27:04 +00:00
|
|
|
{mcond}
|
2020-08-05 14:12:25 +00:00
|
|
|
ORDER BY idx DESC, name
|
2022-06-14 05:19:07 +00:00
|
|
|
LIMIT %(limit)s offset %(offset)s
|
2020-09-21 08:27:04 +00:00
|
|
|
""".format(
|
|
|
|
account_type_condition=account_type_condition,
|
|
|
|
searchfield=searchfield,
|
|
|
|
mcond=get_match_cond(doctype),
|
|
|
|
),
|
2020-08-05 14:12:25 +00:00
|
|
|
dict(
|
|
|
|
account_types=filters.get("account_type"),
|
|
|
|
company=filters.get("company"),
|
2022-05-11 07:31:06 +00:00
|
|
|
disabled=filters.get("disabled", 0),
|
2020-08-05 14:12:25 +00:00
|
|
|
currency=company_currency,
|
|
|
|
txt="%{}%".format(txt),
|
|
|
|
offset=start,
|
|
|
|
limit=page_len,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return accounts
|
|
|
|
|
|
|
|
tax_accounts = get_accounts(True)
|
|
|
|
|
2014-03-21 05:44:49 +00:00
|
|
|
if not tax_accounts:
|
2020-08-05 14:12:25 +00:00
|
|
|
tax_accounts = get_accounts(False)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-03-21 05:44:49 +00:00
|
|
|
return tax_accounts
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2016-04-07 09:55:43 +00:00
|
|
|
def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Item"
|
2013-07-08 13:15:55 +00:00
|
|
|
conditions = []
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2021-06-03 14:32:58 +00:00
|
|
|
if isinstance(filters, str):
|
|
|
|
filters = json.loads(filters)
|
|
|
|
|
2019-10-28 10:18:10 +00:00
|
|
|
# Get searchfields from meta and use in Item Link field query
|
2022-08-03 05:42:30 +00:00
|
|
|
meta = frappe.get_meta(doctype, cached=True)
|
2019-10-28 10:18:10 +00:00
|
|
|
searchfields = meta.get_search_fields()
|
|
|
|
|
2019-11-19 13:35:23 +00:00
|
|
|
columns = ""
|
2022-09-28 17:30:45 +00:00
|
|
|
extra_searchfields = [field for field in searchfields if not field in ["name", "description"]]
|
2019-11-19 13:35:23 +00:00
|
|
|
|
|
|
|
if extra_searchfields:
|
2022-09-28 17:30:45 +00:00
|
|
|
columns += ", " + ", ".join(extra_searchfields)
|
|
|
|
|
|
|
|
if "description" in searchfields:
|
|
|
|
columns += """, if(length(tabItem.description) > 40, \
|
|
|
|
concat(substr(tabItem.description, 1, 40), "..."), description) as description"""
|
2019-10-28 10:18:10 +00:00
|
|
|
|
2019-10-30 13:03:44 +00:00
|
|
|
searchfields = searchfields + [
|
|
|
|
field
|
|
|
|
for field in [searchfield or "name", "item_code", "item_group", "item_name"]
|
|
|
|
if not field in searchfields
|
|
|
|
]
|
2019-10-28 10:18:10 +00:00
|
|
|
searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
|
|
|
|
|
2021-09-14 08:28:18 +00:00
|
|
|
if filters and isinstance(filters, dict):
|
|
|
|
if filters.get("customer") or filters.get("supplier"):
|
|
|
|
party = filters.get("customer") or filters.get("supplier")
|
|
|
|
item_rules_list = frappe.get_all(
|
|
|
|
"Party Specific Item", filters={"party": party}, fields=["restrict_based_on", "based_on_value"]
|
|
|
|
)
|
2021-06-02 08:43:09 +00:00
|
|
|
|
2021-09-14 08:28:18 +00:00
|
|
|
filters_dict = {}
|
|
|
|
for rule in item_rules_list:
|
|
|
|
if rule["restrict_based_on"] == "Item":
|
|
|
|
rule["restrict_based_on"] = "name"
|
|
|
|
filters_dict[rule.restrict_based_on] = []
|
2021-05-12 10:55:07 +00:00
|
|
|
|
2021-09-14 08:28:18 +00:00
|
|
|
for rule in item_rules_list:
|
|
|
|
filters_dict[rule.restrict_based_on].append(rule.based_on_value)
|
|
|
|
|
|
|
|
for filter in filters_dict:
|
|
|
|
filters[scrub(filter)] = ["in", filters_dict[filter]]
|
|
|
|
|
|
|
|
if filters.get("customer"):
|
|
|
|
del filters["customer"]
|
|
|
|
else:
|
|
|
|
del filters["supplier"]
|
2022-02-03 07:32:13 +00:00
|
|
|
else:
|
|
|
|
filters.pop("customer", None)
|
|
|
|
filters.pop("supplier", None)
|
2021-06-02 08:43:09 +00:00
|
|
|
|
2018-04-02 18:07:33 +00:00
|
|
|
description_cond = ""
|
2022-08-03 05:42:30 +00:00
|
|
|
if frappe.db.count(doctype, cache=True) < 50000:
|
2018-04-02 18:07:33 +00:00
|
|
|
# scan description only if items are less than 50000
|
|
|
|
description_cond = "or tabItem.description LIKE %(txt)s"
|
2022-09-28 17:30:45 +00:00
|
|
|
|
2021-11-09 06:25:33 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select
|
2022-09-28 17:30:45 +00:00
|
|
|
tabItem.name {columns}
|
2014-04-11 11:21:27 +00:00
|
|
|
from tabItem
|
2013-11-04 10:53:04 +00:00
|
|
|
where tabItem.docstatus < 2
|
2015-10-29 06:51:41 +00:00
|
|
|
and tabItem.disabled=0
|
2020-05-21 12:40:13 +00:00
|
|
|
and tabItem.has_variants=0
|
2014-06-23 06:50:12 +00:00
|
|
|
and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00')
|
2019-10-28 10:18:10 +00:00
|
|
|
and ({scond} or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s)
|
2019-03-02 16:17:55 +00:00
|
|
|
{description_cond})
|
2013-11-04 10:53:04 +00:00
|
|
|
{fcond} {mcond}
|
2014-04-16 09:51:46 +00:00
|
|
|
order by
|
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
2014-04-16 09:51:46 +00:00
|
|
|
name, item_name
|
2017-10-17 07:00:34 +00:00
|
|
|
limit %(start)s, %(page_len)s """.format(
|
2019-10-30 13:03:44 +00:00
|
|
|
columns=columns,
|
2019-10-28 10:18:10 +00:00
|
|
|
scond=searchfields,
|
2016-03-30 07:40:25 +00:00
|
|
|
fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
|
2018-04-02 18:07:33 +00:00
|
|
|
mcond=get_match_cond(doctype).replace("%", "%%"),
|
|
|
|
description_cond=description_cond,
|
|
|
|
),
|
2013-11-04 10:53:04 +00:00
|
|
|
{
|
|
|
|
"today": nowdate(),
|
|
|
|
"txt": "%%%s%%" % txt,
|
2014-04-16 09:51:46 +00:00
|
|
|
"_txt": txt.replace("%", ""),
|
2013-11-04 10:53:04 +00:00
|
|
|
"start": start,
|
|
|
|
"page_len": page_len,
|
2016-04-07 09:55:43 +00:00
|
|
|
},
|
|
|
|
as_dict=as_dict,
|
|
|
|
)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2017-11-10 09:36:02 +00:00
|
|
|
def bom(doctype, txt, searchfield, start, page_len, filters):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "BOM"
|
2014-04-11 11:21:27 +00:00
|
|
|
conditions = []
|
2022-08-03 05:42:30 +00:00
|
|
|
fields = get_fields(doctype, ["name", "item"])
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select {fields}
|
2022-06-17 15:47:48 +00:00
|
|
|
from `tabBOM`
|
|
|
|
where `tabBOM`.docstatus=1
|
|
|
|
and `tabBOM`.is_active=1
|
|
|
|
and `tabBOM`.`{key}` like %(txt)s
|
2016-03-16 10:52:03 +00:00
|
|
|
{fcond} {mcond}
|
|
|
|
order by
|
2022-06-17 15:47:48 +00:00
|
|
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc, name
|
2022-06-17 15:47:48 +00:00
|
|
|
limit %(page_len)s offset %(start)s""".format(
|
2020-05-18 08:56:26 +00:00
|
|
|
fields=", ".join(fields),
|
2019-07-08 05:10:40 +00:00
|
|
|
fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
|
2019-07-19 17:19:21 +00:00
|
|
|
mcond=get_match_cond(doctype).replace("%", "%%"),
|
|
|
|
key=searchfield,
|
|
|
|
),
|
2019-07-08 05:10:40 +00:00
|
|
|
{
|
2019-07-19 17:19:21 +00:00
|
|
|
"txt": "%" + txt + "%",
|
2016-03-11 09:03:04 +00:00
|
|
|
"_txt": txt.replace("%", ""),
|
2017-11-10 09:36:02 +00:00
|
|
|
"start": start or 0,
|
|
|
|
"page_len": page_len or 20,
|
2016-03-11 09:03:04 +00:00
|
|
|
},
|
|
|
|
)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2013-07-08 13:15:55 +00:00
|
|
|
def get_project_name(doctype, txt, searchfield, start, page_len, filters):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Project"
|
2013-07-08 13:15:55 +00:00
|
|
|
cond = ""
|
2021-09-10 07:16:35 +00:00
|
|
|
if filters and filters.get("customer"):
|
2018-09-26 12:45:53 +00:00
|
|
|
cond = """(`tabProject`.customer = %s or
|
2018-08-27 06:13:57 +00:00
|
|
|
ifnull(`tabProject`.customer,"")="") and""" % (
|
|
|
|
frappe.db.escape(filters.get("customer"))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2022-08-03 05:42:30 +00:00
|
|
|
fields = get_fields(doctype, ["name", "project_name"])
|
|
|
|
searchfields = frappe.get_meta(doctype).get_search_fields()
|
2022-06-17 11:31:27 +00:00
|
|
|
searchfields = " or ".join(["`tabProject`." + field + " like %(txt)s" for field in searchfields])
|
2020-05-18 08:56:26 +00:00
|
|
|
|
|
|
|
return frappe.db.sql(
|
|
|
|
"""select {fields} from `tabProject`
|
2021-05-07 08:01:14 +00:00
|
|
|
where
|
2022-06-17 11:31:27 +00:00
|
|
|
`tabProject`.status not in ('Completed', 'Cancelled')
|
2021-06-22 10:56:19 +00:00
|
|
|
and {cond} {scond} {match_cond}
|
2016-03-11 09:03:04 +00:00
|
|
|
order by
|
2022-06-17 15:47:48 +00:00
|
|
|
(case when locate(%(_txt)s, `tabProject`.name) > 0 then locate(%(_txt)s, `tabProject`.name) else 99999 end),
|
|
|
|
`tabProject`.idx desc,
|
2016-03-11 09:03:04 +00:00
|
|
|
`tabProject`.name asc
|
2022-06-14 05:19:07 +00:00
|
|
|
limit {page_len} offset {start}""".format(
|
2020-05-18 08:56:26 +00:00
|
|
|
fields=", ".join(["`tabProject`.{0}".format(f) for f in fields]),
|
2016-03-11 09:03:04 +00:00
|
|
|
cond=cond,
|
2021-05-07 08:01:14 +00:00
|
|
|
scond=searchfields,
|
2016-03-11 09:03:04 +00:00
|
|
|
match_cond=get_match_cond(doctype),
|
|
|
|
start=start,
|
|
|
|
page_len=page_len,
|
|
|
|
),
|
|
|
|
{"txt": "%{0}%".format(txt), "_txt": txt.replace("%", "")},
|
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2017-09-18 11:40:09 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2017-05-17 08:22:21 +00:00
|
|
|
def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Delivery Note"
|
|
|
|
fields = get_fields(doctype, ["name", "customer", "posting_date"])
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2017-05-17 08:22:21 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""
|
2020-05-18 08:56:26 +00:00
|
|
|
select %(fields)s
|
2014-04-11 11:21:27 +00:00
|
|
|
from `tabDelivery Note`
|
|
|
|
where `tabDelivery Note`.`%(key)s` like %(txt)s and
|
2017-09-18 11:40:09 +00:00
|
|
|
`tabDelivery Note`.docstatus = 1
|
2022-06-17 11:31:27 +00:00
|
|
|
and status not in ('Stopped', 'Closed') %(fcond)s
|
2017-09-18 11:40:09 +00:00
|
|
|
and (
|
|
|
|
(`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100)
|
2021-04-14 05:51:11 +00:00
|
|
|
or (`tabDelivery Note`.grand_total = 0 and `tabDelivery Note`.per_billed < 100)
|
2017-09-18 11:40:09 +00:00
|
|
|
or (
|
|
|
|
`tabDelivery Note`.is_return = 1
|
|
|
|
and return_against in (select name from `tabDelivery Note` where per_billed < 100)
|
|
|
|
)
|
|
|
|
)
|
2022-06-14 05:19:07 +00:00
|
|
|
%(mcond)s order by `tabDelivery Note`.`%(key)s` asc limit %(page_len)s offset %(start)s
|
2017-05-17 08:22:21 +00:00
|
|
|
"""
|
|
|
|
% {
|
2020-05-18 08:56:26 +00:00
|
|
|
"fields": ", ".join(["`tabDelivery Note`.{0}".format(f) for f in fields]),
|
2017-05-17 08:22:21 +00:00
|
|
|
"key": searchfield,
|
|
|
|
"fcond": get_filters_cond(doctype, filters, []),
|
|
|
|
"mcond": get_match_cond(doctype),
|
2019-05-15 02:16:28 +00:00
|
|
|
"start": start,
|
|
|
|
"page_len": page_len,
|
2017-05-17 08:22:21 +00:00
|
|
|
"txt": "%(txt)s",
|
2017-09-18 11:40:09 +00:00
|
|
|
},
|
|
|
|
{"txt": ("%%%s%%" % txt)},
|
|
|
|
as_dict=as_dict,
|
|
|
|
)
|
|
|
|
|
2013-10-18 06:59:11 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2013-10-18 06:59:11 +00:00
|
|
|
def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Batch"
|
2015-07-08 09:06:09 +00:00
|
|
|
cond = ""
|
|
|
|
if filters.get("posting_date"):
|
2018-01-31 10:00:03 +00:00
|
|
|
cond = "and (batch.expiry_date is null or batch.expiry_date >= %(posting_date)s)"
|
2015-08-26 05:20:16 +00:00
|
|
|
|
2015-03-20 09:36:30 +00:00
|
|
|
batch_nos = None
|
|
|
|
args = {
|
|
|
|
"item_code": filters.get("item_code"),
|
|
|
|
"warehouse": filters.get("warehouse"),
|
|
|
|
"posting_date": filters.get("posting_date"),
|
2015-04-06 07:29:34 +00:00
|
|
|
"txt": "%{0}%".format(txt),
|
2015-03-20 09:36:30 +00:00
|
|
|
"start": start,
|
|
|
|
"page_len": page_len,
|
|
|
|
}
|
2013-10-18 06:59:11 +00:00
|
|
|
|
2019-10-22 08:33:27 +00:00
|
|
|
having_clause = "having sum(sle.actual_qty) > 0"
|
|
|
|
if filters.get("is_return"):
|
|
|
|
having_clause = ""
|
|
|
|
|
2022-08-03 05:42:30 +00:00
|
|
|
meta = frappe.get_meta(doctype, cached=True)
|
2020-09-22 08:24:07 +00:00
|
|
|
searchfields = meta.get_search_fields()
|
|
|
|
|
|
|
|
search_columns = ""
|
2020-10-05 08:25:53 +00:00
|
|
|
search_cond = ""
|
|
|
|
|
2020-09-22 08:24:07 +00:00
|
|
|
if searchfields:
|
|
|
|
search_columns = ", " + ", ".join(searchfields)
|
2020-10-05 07:08:54 +00:00
|
|
|
search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
|
2020-09-22 08:24:07 +00:00
|
|
|
|
2015-04-06 07:29:34 +00:00
|
|
|
if args.get("warehouse"):
|
2020-09-22 08:24:07 +00:00
|
|
|
searchfields = ["batch." + field for field in searchfields]
|
|
|
|
if searchfields:
|
|
|
|
search_columns = ", " + ", ".join(searchfields)
|
2020-10-05 07:08:54 +00:00
|
|
|
search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
|
2020-09-22 08:24:07 +00:00
|
|
|
|
2019-10-22 08:33:27 +00:00
|
|
|
batch_nos = frappe.db.sql(
|
|
|
|
"""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom,
|
|
|
|
concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date)
|
2020-09-22 08:24:07 +00:00
|
|
|
{search_columns}
|
2019-10-22 08:33:27 +00:00
|
|
|
from `tabStock Ledger Entry` sle
|
|
|
|
INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
|
|
|
|
where
|
|
|
|
batch.disabled = 0
|
2021-07-20 12:49:15 +00:00
|
|
|
and sle.is_cancelled = 0
|
2019-10-22 08:33:27 +00:00
|
|
|
and sle.item_code = %(item_code)s
|
|
|
|
and sle.warehouse = %(warehouse)s
|
|
|
|
and (sle.batch_no like %(txt)s
|
2019-12-24 06:59:25 +00:00
|
|
|
or batch.expiry_date like %(txt)s
|
2020-10-05 08:25:53 +00:00
|
|
|
or batch.manufacturing_date like %(txt)s
|
|
|
|
{search_cond})
|
2019-10-22 08:33:27 +00:00
|
|
|
and batch.docstatus < 2
|
|
|
|
{cond}
|
|
|
|
{match_conditions}
|
|
|
|
group by batch_no {having_clause}
|
|
|
|
order by batch.expiry_date, sle.batch_no desc
|
2022-06-14 05:19:07 +00:00
|
|
|
limit %(page_len)s offset %(start)s""".format(
|
2020-09-22 08:24:07 +00:00
|
|
|
search_columns=search_columns,
|
2019-10-22 08:33:27 +00:00
|
|
|
cond=cond,
|
|
|
|
match_conditions=get_match_cond(doctype),
|
2020-10-05 07:08:54 +00:00
|
|
|
having_clause=having_clause,
|
|
|
|
search_cond=search_cond,
|
2019-10-22 08:33:27 +00:00
|
|
|
),
|
|
|
|
args,
|
|
|
|
)
|
2015-03-20 09:36:30 +00:00
|
|
|
|
|
|
|
return batch_nos
|
2013-10-18 06:59:11 +00:00
|
|
|
else:
|
2020-09-22 08:24:07 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date)
|
|
|
|
{search_columns}
|
|
|
|
from `tabBatch` batch
|
2018-08-06 09:15:18 +00:00
|
|
|
where batch.disabled = 0
|
|
|
|
and item = %(item_code)s
|
2018-02-12 09:03:40 +00:00
|
|
|
and (name like %(txt)s
|
2019-12-24 06:59:25 +00:00
|
|
|
or expiry_date like %(txt)s
|
2020-10-05 08:25:53 +00:00
|
|
|
or manufacturing_date like %(txt)s
|
|
|
|
{search_cond})
|
2015-03-20 09:36:30 +00:00
|
|
|
and docstatus < 2
|
2015-07-08 09:06:09 +00:00
|
|
|
{0}
|
2015-04-06 07:29:34 +00:00
|
|
|
{match_conditions}
|
2020-10-05 08:25:53 +00:00
|
|
|
|
2015-04-06 07:29:34 +00:00
|
|
|
order by expiry_date, name desc
|
2022-06-14 05:19:07 +00:00
|
|
|
limit %(page_len)s offset %(start)s""".format(
|
2020-10-05 07:08:54 +00:00
|
|
|
cond,
|
|
|
|
search_columns=search_columns,
|
|
|
|
search_cond=search_cond,
|
|
|
|
match_conditions=get_match_cond(doctype),
|
2022-03-28 13:22:46 +00:00
|
|
|
),
|
2020-10-05 07:08:54 +00:00
|
|
|
args,
|
|
|
|
)
|
2014-05-28 07:26:28 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2014-05-28 07:26:28 +00:00
|
|
|
def get_account_list(doctype, txt, searchfield, start, page_len, filters):
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Account"
|
2014-06-14 09:56:10 +00:00
|
|
|
filter_list = []
|
|
|
|
|
2014-05-28 07:26:28 +00:00
|
|
|
if isinstance(filters, dict):
|
2014-06-14 09:56:10 +00:00
|
|
|
for key, val in filters.items():
|
|
|
|
if isinstance(val, (list, tuple)):
|
|
|
|
filter_list.append([doctype, key, val[0], val[1]])
|
|
|
|
else:
|
|
|
|
filter_list.append([doctype, key, "=", val])
|
2015-04-14 16:45:24 +00:00
|
|
|
elif isinstance(filters, list):
|
|
|
|
filter_list.extend(filters)
|
2014-06-14 09:56:10 +00:00
|
|
|
|
2015-04-23 07:44:17 +00:00
|
|
|
if "is_group" not in [d[1] for d in filter_list]:
|
|
|
|
filter_list.append(["Account", "is_group", "=", "0"])
|
2014-06-14 09:56:10 +00:00
|
|
|
|
|
|
|
if searchfield and txt:
|
|
|
|
filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
|
2014-05-28 07:26:28 +00:00
|
|
|
|
2014-09-09 10:45:35 +00:00
|
|
|
return frappe.desk.reportview.execute(
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype,
|
2014-09-09 10:45:35 +00:00
|
|
|
filters=filter_list,
|
2014-05-28 07:26:28 +00:00
|
|
|
fields=["name", "parent_account"],
|
|
|
|
limit_start=start,
|
|
|
|
limit_page_length=page_len,
|
|
|
|
as_list=True,
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-06-24 13:23:04 +00:00
|
|
|
|
2020-07-01 15:44:32 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2020-04-28 07:30:04 +00:00
|
|
|
def get_blanket_orders(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
return frappe.db.sql(
|
|
|
|
"""select distinct bo.name, bo.blanket_order_type, bo.to_date
|
|
|
|
from `tabBlanket Order` bo, `tabBlanket Order Item` boi
|
|
|
|
where
|
|
|
|
boi.parent = bo.name
|
|
|
|
and boi.item_code = {item_code}
|
|
|
|
and bo.blanket_order_type = '{blanket_order_type}'
|
|
|
|
and bo.company = {company}
|
|
|
|
and bo.docstatus = 1""".format(
|
|
|
|
item_code=frappe.db.escape(filters.get("item")),
|
|
|
|
blanket_order_type=filters.get("blanket_order_type"),
|
|
|
|
company=frappe.db.escape(filters.get("company")),
|
|
|
|
)
|
|
|
|
)
|
2015-10-19 06:25:28 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2015-10-19 06:25:28 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2015-10-19 06:25:28 +00:00
|
|
|
def get_income_account(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
from erpnext.controllers.queries import get_match_cond
|
|
|
|
|
|
|
|
# income account can be any Credit account,
|
|
|
|
# but can also be a Asset account with account_type='Income Account' in special circumstances.
|
|
|
|
# Hence the first condition is an "OR"
|
|
|
|
if not filters:
|
|
|
|
filters = {}
|
|
|
|
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Account"
|
2015-10-29 06:51:41 +00:00
|
|
|
condition = ""
|
2015-10-19 06:25:28 +00:00
|
|
|
if filters.get("company"):
|
|
|
|
condition += "and tabAccount.company = %(company)s"
|
2015-10-29 06:51:41 +00:00
|
|
|
|
2015-10-19 06:25:28 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select tabAccount.name from `tabAccount`
|
|
|
|
where (tabAccount.report_type = "Profit and Loss"
|
|
|
|
or tabAccount.account_type in ("Income Account", "Temporary"))
|
|
|
|
and tabAccount.is_group=0
|
|
|
|
and tabAccount.`{key}` LIKE %(txt)s
|
2016-03-11 09:03:04 +00:00
|
|
|
{condition} {match_condition}
|
|
|
|
order by idx desc, name""".format(
|
2015-10-19 06:25:28 +00:00
|
|
|
condition=condition, match_condition=get_match_cond(doctype), key=searchfield
|
|
|
|
),
|
|
|
|
{"txt": "%" + txt + "%", "company": filters.get("company", "")},
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-03-04 07:00:46 +00:00
|
|
|
|
2020-11-15 17:13:01 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2023-04-25 13:03:31 +00:00
|
|
|
def get_filtered_dimensions(
|
|
|
|
doctype, txt, searchfield, start, page_len, filters, reference_doctype=None
|
|
|
|
):
|
2020-11-15 17:13:01 +00:00
|
|
|
from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import (
|
|
|
|
get_dimension_filter_map,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-11-15 17:13:01 +00:00
|
|
|
dimension_filters = get_dimension_filter_map()
|
|
|
|
dimension_filters = dimension_filters.get((filters.get("dimension"), filters.get("account")))
|
2020-11-25 08:12:16 +00:00
|
|
|
query_filters = []
|
2021-11-30 13:15:20 +00:00
|
|
|
or_filters = []
|
|
|
|
fields = ["name"]
|
|
|
|
|
|
|
|
searchfields = frappe.get_meta(doctype).get_search_fields()
|
2020-11-15 17:13:01 +00:00
|
|
|
|
|
|
|
meta = frappe.get_meta(doctype)
|
|
|
|
if meta.is_tree:
|
2020-11-25 08:12:16 +00:00
|
|
|
query_filters.append(["is_group", "=", 0])
|
2020-11-15 17:13:01 +00:00
|
|
|
|
2021-08-18 10:47:54 +00:00
|
|
|
if meta.has_field("disabled"):
|
|
|
|
query_filters.append(["disabled", "!=", 1])
|
|
|
|
|
2020-11-15 17:13:01 +00:00
|
|
|
if meta.has_field("company"):
|
2020-11-25 08:12:16 +00:00
|
|
|
query_filters.append(["company", "=", filters.get("company")])
|
|
|
|
|
2021-11-30 13:15:20 +00:00
|
|
|
for field in searchfields:
|
|
|
|
or_filters.append([field, "LIKE", "%%%s%%" % txt])
|
|
|
|
fields.append(field)
|
2020-11-15 17:13:01 +00:00
|
|
|
|
|
|
|
if dimension_filters:
|
|
|
|
if dimension_filters["allow_or_restrict"] == "Allow":
|
|
|
|
query_selector = "in"
|
|
|
|
else:
|
|
|
|
query_selector = "not in"
|
|
|
|
|
|
|
|
if len(dimension_filters["allowed_dimensions"]) == 1:
|
|
|
|
dimensions = tuple(dimension_filters["allowed_dimensions"] * 2)
|
|
|
|
else:
|
|
|
|
dimensions = tuple(dimension_filters["allowed_dimensions"])
|
|
|
|
|
2020-11-25 08:12:16 +00:00
|
|
|
query_filters.append(["name", query_selector, dimensions])
|
2020-11-15 17:13:01 +00:00
|
|
|
|
2021-11-30 13:15:20 +00:00
|
|
|
output = frappe.get_list(
|
2023-04-25 13:03:31 +00:00
|
|
|
doctype,
|
|
|
|
fields=fields,
|
|
|
|
filters=query_filters,
|
|
|
|
or_filters=or_filters,
|
|
|
|
as_list=1,
|
|
|
|
reference_doctype=reference_doctype,
|
2021-11-30 13:15:20 +00:00
|
|
|
)
|
2020-11-25 08:12:16 +00:00
|
|
|
|
2021-11-30 13:15:20 +00:00
|
|
|
return [tuple(d) for d in set(output)]
|
2016-03-04 07:00:46 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-03-04 07:00:46 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2016-03-04 07:00:46 +00:00
|
|
|
def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
from erpnext.controllers.queries import get_match_cond
|
2016-04-07 09:55:43 +00:00
|
|
|
|
2016-03-04 07:00:46 +00:00
|
|
|
if not filters:
|
|
|
|
filters = {}
|
|
|
|
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Account"
|
2016-03-04 07:00:46 +00:00
|
|
|
condition = ""
|
|
|
|
if filters.get("company"):
|
|
|
|
condition += "and tabAccount.company = %(company)s"
|
2016-04-07 09:55:43 +00:00
|
|
|
|
2016-03-04 07:00:46 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select tabAccount.name from `tabAccount`
|
|
|
|
where (tabAccount.report_type = "Profit and Loss"
|
2019-08-21 09:19:24 +00:00
|
|
|
or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed", "Capital Work in Progress"))
|
2016-03-04 07:00:46 +00:00
|
|
|
and tabAccount.is_group=0
|
|
|
|
and tabAccount.docstatus!=2
|
|
|
|
and tabAccount.{key} LIKE %(txt)s
|
|
|
|
{condition} {match_condition}""".format(
|
|
|
|
condition=condition, key=searchfield, match_condition=get_match_cond(doctype)
|
|
|
|
),
|
2016-03-31 17:40:13 +00:00
|
|
|
{"company": filters.get("company", ""), "txt": "%" + txt + "%"},
|
2016-07-22 02:42:59 +00:00
|
|
|
)
|
2017-01-12 12:19:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2017-01-12 12:19:37 +00:00
|
|
|
def warehouse_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
# Should be used when item code is passed in filters.
|
2022-08-03 05:42:30 +00:00
|
|
|
doctype = "Warehouse"
|
2017-01-18 10:05:01 +00:00
|
|
|
conditions, bin_conditions = [], []
|
|
|
|
filter_dict = get_doctype_wise_filters(filters)
|
|
|
|
|
2017-08-24 09:53:33 +00:00
|
|
|
query = """select `tabWarehouse`.name,
|
2022-06-17 11:31:27 +00:00
|
|
|
CONCAT_WS(' : ', 'Actual Qty', ifnull(round(`tabBin`.actual_qty, 2), 0 )) actual_qty
|
2020-08-17 19:05:04 +00:00
|
|
|
from `tabWarehouse` left join `tabBin`
|
|
|
|
on `tabBin`.warehouse = `tabWarehouse`.name {bin_conditions}
|
2017-01-31 09:44:44 +00:00
|
|
|
where
|
2020-08-17 19:05:04 +00:00
|
|
|
`tabWarehouse`.`{key}` like {txt}
|
2017-01-31 09:44:44 +00:00
|
|
|
{fcond} {mcond}
|
2020-08-17 19:05:04 +00:00
|
|
|
order by ifnull(`tabBin`.actual_qty, 0) desc
|
2017-01-31 09:44:44 +00:00
|
|
|
limit
|
2022-06-14 05:19:07 +00:00
|
|
|
{page_len} offset {start}
|
2017-01-31 09:44:44 +00:00
|
|
|
""".format(
|
2020-08-17 19:05:04 +00:00
|
|
|
bin_conditions=get_filters_cond(
|
|
|
|
doctype, filter_dict.get("Bin"), bin_conditions, ignore_permissions=True
|
|
|
|
),
|
2018-09-21 04:50:52 +00:00
|
|
|
key=searchfield,
|
2017-01-31 09:44:44 +00:00
|
|
|
fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions),
|
2017-08-24 09:53:33 +00:00
|
|
|
mcond=get_match_cond(doctype),
|
|
|
|
start=start,
|
|
|
|
page_len=page_len,
|
|
|
|
txt=frappe.db.escape("%{0}%".format(txt)),
|
|
|
|
)
|
|
|
|
|
|
|
|
return frappe.db.sql(query)
|
2017-01-18 10:05:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_doctype_wise_filters(filters):
|
|
|
|
# Helper function to seperate filters doctype_wise
|
|
|
|
filter_dict = defaultdict(list)
|
|
|
|
for row in filters:
|
|
|
|
filter_dict[row[0]].append(row)
|
|
|
|
return filter_dict
|
2017-11-29 05:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2017-11-29 05:23:09 +00:00
|
|
|
def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters):
|
2018-01-12 10:52:33 +00:00
|
|
|
query = """select batch_id from `tabBatch`
|
2018-08-06 09:15:18 +00:00
|
|
|
where disabled = 0
|
2022-06-15 06:37:33 +00:00
|
|
|
and (expiry_date >= CURRENT_DATE or expiry_date IS NULL)
|
2018-09-21 04:50:52 +00:00
|
|
|
and name like {txt}""".format(
|
|
|
|
txt=frappe.db.escape("%{0}%".format(txt))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2017-11-29 05:23:09 +00:00
|
|
|
|
2018-01-12 10:52:33 +00:00
|
|
|
if filters and filters.get("item"):
|
2018-09-21 04:50:52 +00:00
|
|
|
query += " and item = {item}".format(item=frappe.db.escape(filters.get("item")))
|
2017-11-29 05:23:09 +00:00
|
|
|
|
2018-01-08 12:27:32 +00:00
|
|
|
return frappe.db.sql(query, filters)
|
2019-06-02 10:33:05 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2019-06-02 10:33:05 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2019-06-02 10:33:05 +00:00
|
|
|
def item_manufacturer_query(doctype, txt, searchfield, start, page_len, filters):
|
2019-09-16 14:14:28 +00:00
|
|
|
item_filters = [
|
|
|
|
["manufacturer", "like", "%" + txt + "%"],
|
|
|
|
["item_code", "=", filters.get("item_code")],
|
|
|
|
]
|
|
|
|
|
|
|
|
item_manufacturers = frappe.get_all(
|
|
|
|
"Item Manufacturer",
|
|
|
|
fields=["manufacturer", "manufacturer_part_no"],
|
|
|
|
filters=item_filters,
|
2019-06-02 10:33:05 +00:00
|
|
|
limit_start=start,
|
|
|
|
limit_page_length=page_len,
|
|
|
|
as_list=1,
|
|
|
|
)
|
2019-09-16 14:14:28 +00:00
|
|
|
return item_manufacturers
|
2019-11-18 06:16:55 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2019-11-18 06:16:55 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2019-11-18 06:16:55 +00:00
|
|
|
def get_purchase_receipts(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
query = """
|
2020-01-06 10:04:15 +00:00
|
|
|
select pr.name
|
2019-11-18 06:16:55 +00:00
|
|
|
from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pritem
|
|
|
|
where pr.docstatus = 1 and pritem.parent = pr.name
|
|
|
|
and pr.name like {txt}""".format(
|
|
|
|
txt=frappe.db.escape("%{0}%".format(txt))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-11-18 06:16:55 +00:00
|
|
|
|
|
|
|
if filters and filters.get("item_code"):
|
|
|
|
query += " and pritem.item_code = {item_code}".format(
|
|
|
|
item_code=frappe.db.escape(filters.get("item_code"))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-11-18 06:16:55 +00:00
|
|
|
|
|
|
|
return frappe.db.sql(query, filters)
|
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2019-11-18 06:16:55 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2019-11-18 06:16:55 +00:00
|
|
|
def get_purchase_invoices(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
query = """
|
2020-01-06 10:04:15 +00:00
|
|
|
select pi.name
|
2019-11-18 06:16:55 +00:00
|
|
|
from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` piitem
|
|
|
|
where pi.docstatus = 1 and piitem.parent = pi.name
|
|
|
|
and pi.name like {txt}""".format(
|
|
|
|
txt=frappe.db.escape("%{0}%".format(txt))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-11-18 06:16:55 +00:00
|
|
|
|
|
|
|
if filters and filters.get("item_code"):
|
|
|
|
query += " and piitem.item_code = {item_code}".format(
|
|
|
|
item_code=frappe.db.escape(filters.get("item_code"))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-11-18 06:16:55 +00:00
|
|
|
|
|
|
|
return frappe.db.sql(query, filters)
|
2020-01-06 10:04:15 +00:00
|
|
|
|
2020-05-18 08:56:26 +00:00
|
|
|
|
2020-01-06 10:04:15 +00:00
|
|
|
@frappe.whitelist()
|
2020-08-05 14:12:25 +00:00
|
|
|
@frappe.validate_and_sanitize_search_inputs
|
2020-01-06 10:04:15 +00:00
|
|
|
def get_tax_template(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
|
|
|
|
item_doc = frappe.get_cached_doc("Item", filters.get("item_code"))
|
|
|
|
item_group = filters.get("item_group")
|
2022-01-18 20:15:27 +00:00
|
|
|
company = filters.get("company")
|
2020-01-06 10:04:15 +00:00
|
|
|
taxes = item_doc.taxes or []
|
|
|
|
|
|
|
|
while item_group:
|
|
|
|
item_group_doc = frappe.get_cached_doc("Item Group", item_group)
|
|
|
|
taxes += item_group_doc.taxes or []
|
|
|
|
item_group = item_group_doc.parent_item_group
|
|
|
|
|
|
|
|
if not taxes:
|
2022-01-18 20:15:27 +00:00
|
|
|
return frappe.get_all(
|
|
|
|
"Item Tax Template", filters={"disabled": 0, "company": company}, as_list=True
|
|
|
|
)
|
2020-01-06 10:04:15 +00:00
|
|
|
else:
|
2020-08-10 09:18:13 +00:00
|
|
|
valid_from = filters.get("valid_from")
|
|
|
|
valid_from = valid_from[1] if isinstance(valid_from, list) else valid_from
|
|
|
|
|
2020-01-06 10:04:15 +00:00
|
|
|
args = {
|
|
|
|
"item_code": filters.get("item_code"),
|
2020-08-10 09:18:13 +00:00
|
|
|
"posting_date": valid_from,
|
2020-06-18 06:18:44 +00:00
|
|
|
"tax_category": filters.get("tax_category"),
|
2022-01-18 20:15:27 +00:00
|
|
|
"company": company,
|
2020-01-06 10:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
taxes = _get_item_tax_template(args, taxes, for_validate=True)
|
|
|
|
return [(d,) for d in set(taxes)]
|
2020-05-18 08:56:26 +00:00
|
|
|
|
|
|
|
|
2021-04-19 05:03:39 +00:00
|
|
|
def get_fields(doctype, fields=None):
|
|
|
|
if fields is None:
|
|
|
|
fields = []
|
2020-05-18 08:56:26 +00:00
|
|
|
meta = frappe.get_meta(doctype)
|
|
|
|
fields.extend(meta.get_search_fields())
|
|
|
|
|
|
|
|
if meta.title_field and not meta.title_field.strip() in fields:
|
|
|
|
fields.insert(1, meta.title_field.strip())
|
|
|
|
|
|
|
|
return unique(fields)
|