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
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import json
|
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
from frappe.utils import flt
|
|
|
|
from frappe.utils.user import is_website_user
|
|
|
|
|
|
|
|
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"""))),
|
2015-02-23 16:44:12 +00:00
|
|
|
"row_template": "templates/includes/transaction_row.html",
|
|
|
|
"get_list": get_transaction_list
|
|
|
|
}
|
|
|
|
|
2017-04-12 07:32:28 +00:00
|
|
|
def get_transaction_list(doctype, txt=None, filters=None, limit_start=0, limit_page_length=20, order_by="modified"):
|
2016-06-23 12:55:50 +00:00
|
|
|
from frappe.www.list import get_list
|
2015-02-23 16:44:12 +00:00
|
|
|
user = frappe.session.user
|
2015-09-11 13:19:59 +00:00
|
|
|
key = None
|
|
|
|
|
|
|
|
if not filters: filters = []
|
|
|
|
|
2016-09-21 11:19:58 +00:00
|
|
|
if doctype == 'Supplier Quotation':
|
|
|
|
filters.append((doctype, "docstatus", "<", 2))
|
|
|
|
else:
|
|
|
|
filters.append((doctype, "docstatus", "=", 1))
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2017-07-25 08:42:58 +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
|
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
|
|
|
|
|
|
|
if not customers and not suppliers: return []
|
|
|
|
|
2016-03-03 08:30:35 +00:00
|
|
|
key, parties = get_party_details(customers, suppliers)
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2016-03-03 08:30:35 +00:00
|
|
|
if doctype == 'Request for Quotation':
|
|
|
|
return rfq_transaction_list(parties_doctype, doctype, parties, limit_start, limit_page_length)
|
2015-09-11 13:19:59 +00:00
|
|
|
|
|
|
|
filters.append((doctype, key, "in", parties))
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
if key:
|
|
|
|
return post_process(doctype, get_list(doctype, txt,
|
|
|
|
filters=filters, fields = "name",
|
|
|
|
limit_start=limit_start, limit_page_length=limit_page_length,
|
|
|
|
ignore_permissions=True,
|
|
|
|
order_by = "modified desc"))
|
2015-02-23 16:44:12 +00:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
return post_process(doctype, get_list(doctype, txt, filters, limit_start, limit_page_length,
|
|
|
|
fields="name", order_by = "modified desc"))
|
|
|
|
|
2016-03-03 08:30:35 +00:00
|
|
|
def get_party_details(customers, suppliers):
|
|
|
|
if customers:
|
|
|
|
key, parties = "customer", customers
|
|
|
|
elif suppliers:
|
|
|
|
key, parties = "supplier", suppliers
|
|
|
|
else:
|
|
|
|
key, parties = "customer", []
|
|
|
|
|
|
|
|
return key, parties
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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))
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
if doc.get("per_delivered"):
|
|
|
|
doc.status_percent += flt(doc.per_delivered)
|
|
|
|
doc.status_display.append(_("Delivered") if doc.per_delivered==100 else _("{0}% Delivered").format(doc.per_delivered))
|
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)
|
2016-06-28 11:41:32 +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
|
|
|
|
|
|
|
|
def get_customers_suppliers(doctype, user):
|
|
|
|
meta = frappe.get_meta(doctype)
|
2017-03-14 07:56:10 +00:00
|
|
|
contacts = frappe.db.sql(""" select `tabContact`.email_id, `tabDynamic Link`.link_doctype, `tabDynamic Link`.link_name
|
|
|
|
from `tabContact`, `tabDynamic Link` where
|
|
|
|
`tabContact`.name = `tabDynamic Link`.parent and `tabContact`.email_id =%s """, user, as_dict=1)
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2017-03-14 07:56:10 +00:00
|
|
|
customers = [c.link_name for c in contacts if c.link_doctype == 'Customer'] if meta.get_field("customer") else None
|
|
|
|
suppliers = [c.link_name for c in contacts if c.link_doctype == 'Supplier'] if meta.get_field("supplier") else None
|
2015-02-23 16:44:12 +00:00
|
|
|
|
|
|
|
return customers, suppliers
|
|
|
|
|
|
|
|
def has_website_permission(doc, ptype, user, verbose=False):
|
|
|
|
doctype = doc.doctype
|
|
|
|
customers, suppliers = get_customers_suppliers(doctype, user)
|
|
|
|
if customers:
|
|
|
|
return frappe.get_all(doctype, filters=[(doctype, "customer", "in", customers),
|
|
|
|
(doctype, "name", "=", doc.name)]) and True or False
|
|
|
|
elif suppliers:
|
2016-09-21 11:19:58 +00:00
|
|
|
fieldname = 'suppliers' if doctype == 'Request for Quotation' else 'supplier'
|
|
|
|
return frappe.get_all(doctype, filters=[(doctype, fieldname, "in", suppliers),
|
2015-02-23 16:44:12 +00:00
|
|
|
(doctype, "name", "=", doc.name)]) and True or False
|
|
|
|
else:
|
|
|
|
return False
|