2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2015-02-23 16:44:12 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2015-02-23 16:44:12 +00:00
|
|
|
import json
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2015-02-23 16:44:12 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2021-09-14 07:19:08 +00:00
|
|
|
from frappe.modules.utils import get_module_app
|
2017-09-10 06:34:28 +00:00
|
|
|
from frappe.utils import flt, has_common
|
2015-02-23 16:44:12 +00:00
|
|
|
from frappe.utils.user import is_website_user
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2015-02-23 16:44:12 +00:00
|
|
|
def get_list_context(context=None):
|
|
|
|
return {
|
|
|
|
"global_number_format": frappe.db.get_default("number_format") or "#,###.##",
|
|
|
|
"currency": frappe.db.get_default("currency"),
|
|
|
|
"currency_symbols": json.dumps(
|
|
|
|
dict(
|
|
|
|
frappe.db.sql(
|
|
|
|
"""select name, symbol
|
2015-11-16 13:35:46 +00:00
|
|
|
from tabCurrency where enabled=1"""
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
)
|
2015-11-16 13:35:46 +00:00
|
|
|
),
|
2015-02-23 16:44:12 +00:00
|
|
|
"row_template": "templates/includes/transaction_row.html",
|
|
|
|
"get_list": get_transaction_list,
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-09-14 07:19:08 +00:00
|
|
|
def get_webform_list_context(module):
|
|
|
|
if get_module_app(module) != "erpnext":
|
|
|
|
return
|
|
|
|
return {"get_list": get_webform_transaction_list}
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-09-14 07:19:08 +00:00
|
|
|
|
|
|
|
def get_webform_transaction_list(
|
|
|
|
doctype, txt=None, filters=None, limit_start=0, limit_page_length=20, order_by="modified"
|
|
|
|
):
|
|
|
|
"""Get List of transactions for custom doctypes"""
|
|
|
|
from frappe.www.list import get_list
|
|
|
|
|
|
|
|
if not filters:
|
|
|
|
filters = []
|
|
|
|
|
|
|
|
meta = frappe.get_meta(doctype)
|
2017-08-08 11:42:50 +00:00
|
|
|
|
2021-09-14 07:19:08 +00:00
|
|
|
for d in meta.fields:
|
|
|
|
if d.fieldtype == "Link" and d.fieldname != "amended_from":
|
|
|
|
allowed_docs = [d.name for d in get_transaction_list(doctype=d.options, custom=True)]
|
|
|
|
allowed_docs.append("")
|
|
|
|
filters.append((d.fieldname, "in", allowed_docs))
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-09-14 07:19:08 +00:00
|
|
|
return get_list(
|
|
|
|
doctype,
|
|
|
|
txt,
|
|
|
|
filters,
|
|
|
|
limit_start,
|
|
|
|
limit_page_length,
|
|
|
|
ignore_permissions=False,
|
|
|
|
fields=None,
|
|
|
|
order_by="modified",
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-14 07:19:08 +00:00
|
|
|
def get_transaction_list(
|
|
|
|
doctype,
|
|
|
|
txt=None,
|
|
|
|
filters=None,
|
|
|
|
limit_start=0,
|
|
|
|
limit_page_length=20,
|
|
|
|
order_by="modified",
|
|
|
|
custom=False,
|
|
|
|
):
|
2015-02-23 16:44:12 +00:00
|
|
|
user = frappe.session.user
|
2019-08-12 06:21:27 +00:00
|
|
|
ignore_permissions = False
|
2015-09-11 13:19:59 +00:00
|
|
|
|
|
|
|
if not filters:
|
2023-03-09 16:42:35 +00:00
|
|
|
filters = {}
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2023-03-09 16:42:35 +00:00
|
|
|
filters["docstatus"] = ["<", "2"] if doctype in ["Supplier Quotation", "Purchase Invoice"] else 1
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2019-08-12 06:21:27 +00:00
|
|
|
if (user != "Guest" and is_website_user()) or doctype == "Request for Quotation":
|
2016-03-29 10:34:25 +00:00
|
|
|
parties_doctype = (
|
|
|
|
"Request for Quotation Supplier" if doctype == "Request for Quotation" else doctype
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-23 16:44:12 +00:00
|
|
|
# find party for this contact
|
2016-03-03 08:30:35 +00:00
|
|
|
customers, suppliers = get_customers_suppliers(parties_doctype, user)
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2019-08-12 06:21:27 +00:00
|
|
|
if customers:
|
|
|
|
if doctype == "Quotation":
|
2023-03-09 16:42:35 +00:00
|
|
|
filters["quotation_to"] = "Customer"
|
|
|
|
filters["party_name"] = ["in", customers]
|
2019-08-12 06:21:27 +00:00
|
|
|
else:
|
2023-03-09 16:42:35 +00:00
|
|
|
filters["customer"] = ["in", customers]
|
2019-08-12 06:21:27 +00:00
|
|
|
elif suppliers:
|
2023-03-09 16:42:35 +00:00
|
|
|
filters["supplier"] = ["in", suppliers]
|
2021-09-14 07:19:08 +00:00
|
|
|
elif not custom:
|
2019-08-12 06:21:27 +00:00
|
|
|
return []
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2016-03-03 08:30:35 +00:00
|
|
|
if doctype == "Request for Quotation":
|
2019-08-12 06:21:27 +00:00
|
|
|
parties = customers or suppliers
|
2016-03-03 08:30:35 +00:00
|
|
|
return rfq_transaction_list(parties_doctype, doctype, parties, limit_start, limit_page_length)
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2019-08-12 06:21:27 +00:00
|
|
|
# Since customers and supplier do not have direct access to internal doctypes
|
|
|
|
ignore_permissions = True
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2021-09-14 07:19:08 +00:00
|
|
|
if not customers and not suppliers and custom:
|
|
|
|
ignore_permissions = False
|
2023-03-09 16:42:35 +00:00
|
|
|
filters = {}
|
2021-09-14 07:19:08 +00:00
|
|
|
|
2019-08-12 06:21:27 +00:00
|
|
|
transactions = get_list_for_transactions(
|
|
|
|
doctype,
|
|
|
|
txt,
|
|
|
|
filters,
|
|
|
|
limit_start,
|
|
|
|
limit_page_length,
|
|
|
|
fields="name",
|
|
|
|
ignore_permissions=ignore_permissions,
|
|
|
|
order_by="modified desc",
|
|
|
|
)
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2021-09-14 07:19:08 +00:00
|
|
|
if custom:
|
|
|
|
return transactions
|
|
|
|
|
2019-08-12 06:21:27 +00:00
|
|
|
return post_process(doctype, transactions)
|
2017-08-08 11:42:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-09-10 06:34:28 +00:00
|
|
|
def get_list_for_transactions(
|
|
|
|
doctype,
|
|
|
|
txt,
|
|
|
|
filters,
|
|
|
|
limit_start,
|
|
|
|
limit_page_length=20,
|
2019-08-12 06:21:27 +00:00
|
|
|
ignore_permissions=False,
|
|
|
|
fields=None,
|
|
|
|
order_by=None,
|
|
|
|
):
|
2017-09-10 06:34:28 +00:00
|
|
|
"""Get List of transactions like Invoices, Orders"""
|
2017-08-08 11:42:50 +00:00
|
|
|
from frappe.www.list import get_list
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-08-08 11:42:50 +00:00
|
|
|
meta = frappe.get_meta(doctype)
|
|
|
|
data = []
|
|
|
|
or_filters = []
|
|
|
|
|
|
|
|
for d in get_list(
|
|
|
|
doctype,
|
|
|
|
txt,
|
|
|
|
filters=filters,
|
|
|
|
fields="name",
|
|
|
|
limit_start=limit_start,
|
2017-09-10 06:34:28 +00:00
|
|
|
limit_page_length=limit_page_length,
|
|
|
|
ignore_permissions=ignore_permissions,
|
|
|
|
order_by="modified desc",
|
|
|
|
):
|
2017-08-08 11:42:50 +00:00
|
|
|
data.append(d)
|
|
|
|
|
|
|
|
if txt:
|
|
|
|
if meta.get_field("items"):
|
|
|
|
if meta.get_field("items").options:
|
2017-08-28 12:47:36 +00:00
|
|
|
child_doctype = meta.get_field("items").options
|
|
|
|
for item in frappe.get_all(child_doctype, {"item_name": ["like", "%" + txt + "%"]}):
|
2017-08-08 11:42:50 +00:00
|
|
|
child = frappe.get_doc(child_doctype, item.name)
|
|
|
|
or_filters.append([doctype, "name", "=", child.parent])
|
|
|
|
|
|
|
|
if or_filters:
|
2017-09-10 06:34:28 +00:00
|
|
|
for r in frappe.get_list(
|
|
|
|
doctype,
|
|
|
|
fields=fields,
|
|
|
|
filters=filters,
|
|
|
|
or_filters=or_filters,
|
2019-08-05 04:38:35 +00:00
|
|
|
limit_start=limit_start,
|
|
|
|
limit_page_length=limit_page_length,
|
2017-09-10 06:34:28 +00:00
|
|
|
ignore_permissions=ignore_permissions,
|
|
|
|
order_by=order_by,
|
|
|
|
):
|
2017-08-08 11:42:50 +00:00
|
|
|
data.append(r)
|
|
|
|
|
|
|
|
return data
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-03-03 08:30:35 +00:00
|
|
|
def rfq_transaction_list(parties_doctype, doctype, parties, limit_start, limit_page_length):
|
|
|
|
data = frappe.db.sql(
|
|
|
|
"""select distinct parent as name, supplier from `tab{doctype}`
|
|
|
|
where supplier = '{supplier}' and docstatus=1 order by modified desc limit {start}, {len}""".format(
|
|
|
|
doctype=parties_doctype, supplier=parties[0], start=limit_start, len=limit_page_length
|
|
|
|
),
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
return post_process(doctype, data)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
def post_process(doctype, data):
|
|
|
|
result = []
|
|
|
|
for d in data:
|
|
|
|
doc = frappe.get_doc(doctype, d.name)
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
doc.status_percent = 0
|
|
|
|
doc.status_display = []
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
if doc.get("per_billed"):
|
|
|
|
doc.status_percent += flt(doc.per_billed)
|
|
|
|
doc.status_display.append(
|
|
|
|
_("Billed") if doc.per_billed == 100 else _("{0}% Billed").format(doc.per_billed)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
if doc.get("per_delivered"):
|
2023-07-17 14:45:11 +00:00
|
|
|
doc.status_percent += flt(doc.per_delivered, 2)
|
2015-09-11 13:19:59 +00:00
|
|
|
doc.status_display.append(
|
2023-07-17 14:45:11 +00:00
|
|
|
_("Delivered")
|
|
|
|
if flt(doc.per_delivered, 2) == 100
|
|
|
|
else _("{0}% Delivered").format(doc.per_delivered)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2015-09-16 13:22:52 +00:00
|
|
|
if hasattr(doc, "set_indicator"):
|
|
|
|
doc.set_indicator()
|
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
doc.status_display = ", ".join(doc.status_display)
|
2021-06-11 13:10:22 +00:00
|
|
|
doc.items_preview = ", ".join(d.item_name for d in doc.items if d.item_name)
|
2015-09-11 13:19:59 +00:00
|
|
|
result.append(doc)
|
2015-02-23 16:44:12 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-02-23 16:44:12 +00:00
|
|
|
def get_customers_suppliers(doctype, user):
|
2017-09-10 06:34:28 +00:00
|
|
|
customers = []
|
|
|
|
suppliers = []
|
2015-02-23 16:44:12 +00:00
|
|
|
meta = frappe.get_meta(doctype)
|
|
|
|
|
2019-08-05 04:38:35 +00:00
|
|
|
customer_field_name = get_customer_field_name(doctype)
|
|
|
|
|
|
|
|
has_customer_field = meta.has_field(customer_field_name)
|
|
|
|
has_supplier_field = meta.has_field("supplier")
|
|
|
|
|
2017-09-10 06:34:28 +00:00
|
|
|
if has_common(["Supplier", "Customer"], frappe.get_roles(user)):
|
2023-06-28 05:52:40 +00:00
|
|
|
suppliers = get_parents_for_user("Supplier")
|
|
|
|
customers = get_parents_for_user("Customer")
|
2017-09-10 06:34:28 +00:00
|
|
|
elif frappe.has_permission(doctype, "read", user=user):
|
2019-08-05 04:38:35 +00:00
|
|
|
customer_list = frappe.get_list("Customer")
|
|
|
|
customers = suppliers = [customer.name for customer in customer_list]
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2019-08-05 04:38:35 +00:00
|
|
|
return customers if has_customer_field else None, suppliers if has_supplier_field else None
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2023-06-28 05:52:40 +00:00
|
|
|
def get_parents_for_user(parenttype: str) -> list[str]:
|
|
|
|
portal_user = frappe.qb.DocType("Portal User")
|
|
|
|
|
|
|
|
return (
|
|
|
|
frappe.qb.from_(portal_user)
|
|
|
|
.select(portal_user.parent)
|
|
|
|
.where(portal_user.user == frappe.session.user)
|
|
|
|
.where(portal_user.parenttype == parenttype)
|
|
|
|
).run(pluck="name")
|
|
|
|
|
|
|
|
|
2015-02-23 16:44:12 +00:00
|
|
|
def has_website_permission(doc, ptype, user, verbose=False):
|
|
|
|
doctype = doc.doctype
|
|
|
|
customers, suppliers = get_customers_suppliers(doctype, user)
|
|
|
|
if customers:
|
2019-08-12 06:21:27 +00:00
|
|
|
return frappe.db.exists(doctype, get_customer_filter(doc, customers))
|
2015-02-23 16:44:12 +00:00
|
|
|
elif suppliers:
|
2016-09-21 11:19:58 +00:00
|
|
|
fieldname = "suppliers" if doctype == "Request for Quotation" else "supplier"
|
2020-05-27 08:06:35 +00:00
|
|
|
return frappe.db.exists(doctype, {"name": doc.name, fieldname: ["in", suppliers]})
|
2015-02-23 16:44:12 +00:00
|
|
|
else:
|
|
|
|
return False
|
2019-08-05 04:38:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-08-05 04:38:35 +00:00
|
|
|
def get_customer_filter(doc, customers):
|
|
|
|
doctype = doc.doctype
|
|
|
|
filters = frappe._dict()
|
|
|
|
filters.name = doc.name
|
|
|
|
filters[get_customer_field_name(doctype)] = ["in", customers]
|
|
|
|
if doctype == "Quotation":
|
2019-08-12 06:21:27 +00:00
|
|
|
filters.quotation_to = "Customer"
|
2019-08-05 04:38:35 +00:00
|
|
|
return filters
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-08-05 04:38:35 +00:00
|
|
|
def get_customer_field_name(doctype):
|
|
|
|
if doctype == "Quotation":
|
|
|
|
return "party_name"
|
|
|
|
else:
|
2019-10-09 06:11:33 +00:00
|
|
|
return "customer"
|
2023-06-28 05:52:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_role_for_portal_user(portal_user, role):
|
|
|
|
"""When a new portal user is added, give appropriate roles to user if
|
|
|
|
posssible, else warn user to add roles."""
|
|
|
|
if not portal_user.is_new():
|
|
|
|
return
|
|
|
|
|
|
|
|
user_doc = frappe.get_doc("User", portal_user.user)
|
|
|
|
roles = {r.role for r in user_doc.roles}
|
|
|
|
|
|
|
|
if role in roles:
|
|
|
|
return
|
|
|
|
|
|
|
|
if "System Manager" not in frappe.get_roles():
|
|
|
|
frappe.msgprint(
|
|
|
|
_("Please add {1} role to user {0}.").format(portal_user.user, role),
|
|
|
|
alert=True,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
user_doc.add_roles(role)
|
|
|
|
frappe.msgprint(
|
|
|
|
_("Added {1} Role to User {0}.").format(frappe.bold(user_doc.name), role), alert=True
|
|
|
|
)
|