blob: 8431486d1a3773f921b902344a4f603b1ec89e06 [file] [log] [blame]
rohitwaghchaurea1064a62016-03-03 14:00:35 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
Chillar Anand915b3432021-09-02 16:44:59 +05304
rohitwaghchaurea1064a62016-03-03 14:00:35 +05305import frappe
6from frappe import _
Rohit Waghchaurea26f6852016-10-03 19:05:18 +05307from frappe.utils import formatdate
Chillar Anand915b3432021-09-02 16:44:59 +05308
Suraj Shetty83705af2019-08-12 11:51:27 +05309from erpnext.controllers.website_list_for_contact import get_customers_suppliers
rohitwaghchaurea1064a62016-03-03 14:00:35 +053010
Chillar Anand915b3432021-09-02 16:44:59 +053011
rohitwaghchaurea1064a62016-03-03 14:00:35 +053012def get_context(context):
13 context.no_cache = 1
Rushabh Mehta878dfec2016-06-28 17:11:32 +053014 context.show_sidebar = True
rohitwaghchaurea1064a62016-03-03 14:00:35 +053015 context.doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name)
16 context.parents = frappe.form_dict.parents
17 context.doc.supplier = get_supplier()
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053018 context.doc.rfq_links = get_link_quotation(context.doc.supplier, context.doc.name)
rohitwaghchaure62fea032016-03-30 13:24:42 +053019 unauthorized_user(context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053020 update_supplier_details(context)
rohitwaghchaurea1064a62016-03-03 14:00:35 +053021 context["title"] = frappe.form_dict.name
22
Ankush Menat494bd9e2022-03-28 18:52:46 +053023
rohitwaghchaurea1064a62016-03-03 14:00:35 +053024def get_supplier():
rohitwaghchaure62fea032016-03-30 13:24:42 +053025 doctype = frappe.form_dict.doctype
Akhil Narang3effaf22024-03-27 11:37:26 +053026 parties_doctype = "Request for Quotation Supplier" if doctype == "Request for Quotation" else doctype
rohitwaghchaure62fea032016-03-30 13:24:42 +053027 customers, suppliers = get_customers_suppliers(parties_doctype, frappe.session.user)
Suraj Shetty83705af2019-08-12 11:51:27 +053028
Ankush Menat494bd9e2022-03-28 18:52:46 +053029 return suppliers[0] if suppliers else ""
30
rohitwaghchaurea1064a62016-03-03 14:00:35 +053031
32def check_supplier_has_docname_access(supplier):
33 status = True
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 if frappe.form_dict.name not in frappe.db.sql_list(
35 """select parent from `tabRequest for Quotation Supplier`
36 where supplier = %s""",
37 (supplier,),
38 ):
rohitwaghchaurea1064a62016-03-03 14:00:35 +053039 status = False
40 return status
rohitwaghchaure62fea032016-03-30 13:24:42 +053041
Ankush Menat494bd9e2022-03-28 18:52:46 +053042
rohitwaghchaure62fea032016-03-30 13:24:42 +053043def unauthorized_user(supplier):
Rohit Waghchaureae270d62016-04-04 18:49:26 +053044 status = check_supplier_has_docname_access(supplier) or False
Akhil Narang3effaf22024-03-27 11:37:26 +053045 if status is False:
rohitwaghchaure62fea032016-03-30 13:24:42 +053046 frappe.throw(_("Not Permitted"), frappe.PermissionError)
47
Ankush Menat494bd9e2022-03-28 18:52:46 +053048
rohitwaghchaure62fea032016-03-30 13:24:42 +053049def update_supplier_details(context):
50 supplier_doc = frappe.get_doc("Supplier", context.doc.supplier)
Ankush Menat494bd9e2022-03-28 18:52:46 +053051 context.doc.currency = supplier_doc.default_currency or frappe.get_cached_value(
52 "Company", context.doc.company, "default_currency"
53 )
Akhil Narang3effaf22024-03-27 11:37:26 +053054 context.doc.currency_symbol = frappe.db.get_value("Currency", context.doc.currency, "symbol", cache=True)
Ankush Menat494bd9e2022-03-28 18:52:46 +053055 context.doc.number_format = frappe.db.get_value(
56 "Currency", context.doc.currency, "number_format", cache=True
57 )
58 context.doc.buying_price_list = supplier_doc.default_price_list or ""
59
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053060
61def get_link_quotation(supplier, rfq):
Ankush Menat494bd9e2022-03-28 18:52:46 +053062 quotation = frappe.db.sql(
63 """ select distinct `tabSupplier Quotation Item`.parent as name,
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053064 `tabSupplier Quotation`.status, `tabSupplier Quotation`.transaction_date from
65 `tabSupplier Quotation Item`, `tabSupplier Quotation` where `tabSupplier Quotation`.docstatus < 2 and
66 `tabSupplier Quotation Item`.request_for_quotation =%(name)s and
67 `tabSupplier Quotation Item`.parent = `tabSupplier Quotation`.name and
68 `tabSupplier Quotation`.supplier = %(supplier)s order by `tabSupplier Quotation`.creation desc""",
Ankush Menat494bd9e2022-03-28 18:52:46 +053069 {"name": rfq, "supplier": supplier},
70 as_dict=1,
71 )
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053072
73 for data in quotation:
74 data.transaction_date = formatdate(data.transaction_date)
75
76 return quotation or None