blob: b9f646b8a5d58ae61b5f9ba4faa1f57098b17fab [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
4from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05305
rohitwaghchaurea1064a62016-03-03 14:00:35 +05306import frappe
7from frappe import _
Rohit Waghchaurea26f6852016-10-03 19:05:18 +05308from frappe.utils import formatdate
Chillar Anand915b3432021-09-02 16:44:59 +05309
Suraj Shetty83705af2019-08-12 11:51:27 +053010from erpnext.controllers.website_list_for_contact import get_customers_suppliers
rohitwaghchaurea1064a62016-03-03 14:00:35 +053011
Chillar Anand915b3432021-09-02 16:44:59 +053012
rohitwaghchaurea1064a62016-03-03 14:00:35 +053013def get_context(context):
14 context.no_cache = 1
Rushabh Mehta878dfec2016-06-28 17:11:32 +053015 context.show_sidebar = True
rohitwaghchaurea1064a62016-03-03 14:00:35 +053016 context.doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name)
17 context.parents = frappe.form_dict.parents
18 context.doc.supplier = get_supplier()
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053019 context.doc.rfq_links = get_link_quotation(context.doc.supplier, context.doc.name)
rohitwaghchaure62fea032016-03-30 13:24:42 +053020 unauthorized_user(context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053021 update_supplier_details(context)
rohitwaghchaurea1064a62016-03-03 14:00:35 +053022 context["title"] = frappe.form_dict.name
23
rohitwaghchaurea1064a62016-03-03 14:00:35 +053024def get_supplier():
rohitwaghchaure62fea032016-03-30 13:24:42 +053025 doctype = frappe.form_dict.doctype
26 parties_doctype = 'Request for Quotation Supplier' if doctype == 'Request for Quotation' else doctype
27 customers, suppliers = get_customers_suppliers(parties_doctype, frappe.session.user)
Suraj Shetty83705af2019-08-12 11:51:27 +053028
29 return suppliers[0] if suppliers else ''
rohitwaghchaurea1064a62016-03-03 14:00:35 +053030
31def check_supplier_has_docname_access(supplier):
32 status = True
Rushabh Mehta7a1b5da2016-03-29 16:04:25 +053033 if frappe.form_dict.name not in frappe.db.sql_list("""select parent from `tabRequest for Quotation Supplier`
tundedcd54202017-06-22 11:02:19 +010034 where supplier = %s""", (supplier,)):
rohitwaghchaurea1064a62016-03-03 14:00:35 +053035 status = False
36 return status
rohitwaghchaure62fea032016-03-30 13:24:42 +053037
38def unauthorized_user(supplier):
Rohit Waghchaureae270d62016-04-04 18:49:26 +053039 status = check_supplier_has_docname_access(supplier) or False
rohitwaghchaure62fea032016-03-30 13:24:42 +053040 if status == False:
41 frappe.throw(_("Not Permitted"), frappe.PermissionError)
42
43def update_supplier_details(context):
44 supplier_doc = frappe.get_doc("Supplier", context.doc.supplier)
Rushabh Mehta708e47a2018-08-08 16:37:31 +053045 context.doc.currency = supplier_doc.default_currency or frappe.get_cached_value('Company', context.doc.company, "default_currency")
46 context.doc.currency_symbol = frappe.db.get_value("Currency", context.doc.currency, "symbol", cache=True)
47 context.doc.number_format = frappe.db.get_value("Currency", context.doc.currency, "number_format", cache=True)
Rohit Waghchaurea26f6852016-10-03 19:05:18 +053048 context.doc.buying_price_list = supplier_doc.default_price_list or ''
49
50def get_link_quotation(supplier, rfq):
51 quotation = frappe.db.sql(""" select distinct `tabSupplier Quotation Item`.parent as name,
52 `tabSupplier Quotation`.status, `tabSupplier Quotation`.transaction_date from
53 `tabSupplier Quotation Item`, `tabSupplier Quotation` where `tabSupplier Quotation`.docstatus < 2 and
54 `tabSupplier Quotation Item`.request_for_quotation =%(name)s and
55 `tabSupplier Quotation Item`.parent = `tabSupplier Quotation`.name and
56 `tabSupplier Quotation`.supplier = %(supplier)s order by `tabSupplier Quotation`.creation desc""",
57 {'name': rfq, 'supplier': supplier}, as_dict=1)
58
59 for data in quotation:
60 data.transaction_date = formatdate(data.transaction_date)
61
62 return quotation or None