blob: 4b8364249114ca0fbb93929c51137acef85a2739 [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
Ankush Menat494bd9e2022-03-28 18:52:46 +053026 parties_doctype = (
27 "Request for Quotation Supplier" if doctype == "Request for Quotation" else doctype
28 )
rohitwaghchaure62fea032016-03-30 13:24:42 +053029 customers, suppliers = get_customers_suppliers(parties_doctype, frappe.session.user)
Suraj Shetty83705af2019-08-12 11:51:27 +053030
Ankush Menat494bd9e2022-03-28 18:52:46 +053031 return suppliers[0] if suppliers else ""
32
rohitwaghchaurea1064a62016-03-03 14:00:35 +053033
34def check_supplier_has_docname_access(supplier):
35 status = True
Ankush Menat494bd9e2022-03-28 18:52:46 +053036 if frappe.form_dict.name not in frappe.db.sql_list(
37 """select parent from `tabRequest for Quotation Supplier`
38 where supplier = %s""",
39 (supplier,),
40 ):
rohitwaghchaurea1064a62016-03-03 14:00:35 +053041 status = False
42 return status
rohitwaghchaure62fea032016-03-30 13:24:42 +053043
Ankush Menat494bd9e2022-03-28 18:52:46 +053044
rohitwaghchaure62fea032016-03-30 13:24:42 +053045def unauthorized_user(supplier):
Rohit Waghchaureae270d62016-04-04 18:49:26 +053046 status = check_supplier_has_docname_access(supplier) or False
rohitwaghchaure62fea032016-03-30 13:24:42 +053047 if status == False:
48 frappe.throw(_("Not Permitted"), frappe.PermissionError)
49
Ankush Menat494bd9e2022-03-28 18:52:46 +053050
rohitwaghchaure62fea032016-03-30 13:24:42 +053051def update_supplier_details(context):
52 supplier_doc = frappe.get_doc("Supplier", context.doc.supplier)
Ankush Menat494bd9e2022-03-28 18:52:46 +053053 context.doc.currency = supplier_doc.default_currency or frappe.get_cached_value(
54 "Company", context.doc.company, "default_currency"
55 )
56 context.doc.currency_symbol = frappe.db.get_value(
57 "Currency", context.doc.currency, "symbol", cache=True
58 )
59 context.doc.number_format = frappe.db.get_value(
60 "Currency", context.doc.currency, "number_format", cache=True
61 )
62 context.doc.buying_price_list = supplier_doc.default_price_list or ""
63
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053064
65def get_link_quotation(supplier, rfq):
Ankush Menat494bd9e2022-03-28 18:52:46 +053066 quotation = frappe.db.sql(
67 """ select distinct `tabSupplier Quotation Item`.parent as name,
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053068 `tabSupplier Quotation`.status, `tabSupplier Quotation`.transaction_date from
69 `tabSupplier Quotation Item`, `tabSupplier Quotation` where `tabSupplier Quotation`.docstatus < 2 and
70 `tabSupplier Quotation Item`.request_for_quotation =%(name)s and
71 `tabSupplier Quotation Item`.parent = `tabSupplier Quotation`.name and
72 `tabSupplier Quotation`.supplier = %(supplier)s order by `tabSupplier Quotation`.creation desc""",
Ankush Menat494bd9e2022-03-28 18:52:46 +053073 {"name": rfq, "supplier": supplier},
74 as_dict=1,
75 )
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053076
77 for data in quotation:
78 data.transaction_date = formatdate(data.transaction_date)
79
80 return quotation or None