blob: decae417a6c1925cf25b36f9fe7b368eb0398660 [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 Waghchaureae270d62016-04-04 18:49:26 +05307from erpnext.controllers.website_list_for_contact import (get_customers_suppliers,
8 get_party_details)
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()
rohitwaghchaure62fea032016-03-30 13:24:42 +053016 unauthorized_user(context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053017 update_supplier_details(context)
rohitwaghchaurea1064a62016-03-03 14:00:35 +053018 context["title"] = frappe.form_dict.name
19
rohitwaghchaurea1064a62016-03-03 14:00:35 +053020def get_supplier():
rohitwaghchaure62fea032016-03-30 13:24:42 +053021 doctype = frappe.form_dict.doctype
22 parties_doctype = 'Request for Quotation Supplier' if doctype == 'Request for Quotation' else doctype
23 customers, suppliers = get_customers_suppliers(parties_doctype, frappe.session.user)
24 key, parties = get_party_details(customers, suppliers)
25 return parties[0] if key == 'supplier' else ''
rohitwaghchaurea1064a62016-03-03 14:00:35 +053026
27def check_supplier_has_docname_access(supplier):
28 status = True
Rushabh Mehta7a1b5da2016-03-29 16:04:25 +053029 if frappe.form_dict.name not in frappe.db.sql_list("""select parent from `tabRequest for Quotation Supplier`
rohitwaghchaurea1064a62016-03-03 14:00:35 +053030 where supplier = '{supplier}'""".format(supplier=supplier)):
31 status = False
32 return status
rohitwaghchaure62fea032016-03-30 13:24:42 +053033
34def unauthorized_user(supplier):
Rohit Waghchaureae270d62016-04-04 18:49:26 +053035 status = check_supplier_has_docname_access(supplier) or False
rohitwaghchaure62fea032016-03-30 13:24:42 +053036 if status == False:
37 frappe.throw(_("Not Permitted"), frappe.PermissionError)
38
39def update_supplier_details(context):
40 supplier_doc = frappe.get_doc("Supplier", context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053041 context.doc.currency = supplier_doc.default_currency or frappe.db.get_value("Company", context.doc.company, "default_currency")
rohitwaghchaure62fea032016-03-30 13:24:42 +053042 context.doc.currency_symbol = frappe.db.get_value("Currency", context.doc.currency, "symbol")
43 context.doc.number_format = frappe.db.get_value("Currency", context.doc.currency, "number_format")
Rohit Waghchaureae270d62016-04-04 18:49:26 +053044 context.doc.buying_price_list = supplier_doc.default_price_list or ''