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