blob: 4c488d92423e79ff3a6ebe4c7652cd0135ab931e [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
12 context.doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name)
13 context.parents = frappe.form_dict.parents
14 context.doc.supplier = get_supplier()
rohitwaghchaure62fea032016-03-30 13:24:42 +053015 unauthorized_user(context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053016 update_supplier_details(context)
rohitwaghchaurea1064a62016-03-03 14:00:35 +053017 context["title"] = frappe.form_dict.name
18
rohitwaghchaurea1064a62016-03-03 14:00:35 +053019def get_supplier():
rohitwaghchaure62fea032016-03-30 13:24:42 +053020 doctype = frappe.form_dict.doctype
21 parties_doctype = 'Request for Quotation Supplier' if doctype == 'Request for Quotation' else doctype
22 customers, suppliers = get_customers_suppliers(parties_doctype, frappe.session.user)
23 key, parties = get_party_details(customers, suppliers)
24 return parties[0] if key == 'supplier' else ''
rohitwaghchaurea1064a62016-03-03 14:00:35 +053025
26def check_supplier_has_docname_access(supplier):
27 status = True
Rushabh Mehta7a1b5da2016-03-29 16:04:25 +053028 if frappe.form_dict.name not in frappe.db.sql_list("""select parent from `tabRequest for Quotation Supplier`
rohitwaghchaurea1064a62016-03-03 14:00:35 +053029 where supplier = '{supplier}'""".format(supplier=supplier)):
30 status = False
31 return status
rohitwaghchaure62fea032016-03-30 13:24:42 +053032
33def unauthorized_user(supplier):
Rohit Waghchaureae270d62016-04-04 18:49:26 +053034 status = check_supplier_has_docname_access(supplier) or False
rohitwaghchaure62fea032016-03-30 13:24:42 +053035 if status == False:
36 frappe.throw(_("Not Permitted"), frappe.PermissionError)
37
38def update_supplier_details(context):
39 supplier_doc = frappe.get_doc("Supplier", context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053040 context.doc.currency = supplier_doc.default_currency or frappe.db.get_value("Company", context.doc.company, "default_currency")
rohitwaghchaure62fea032016-03-30 13:24:42 +053041 context.doc.currency_symbol = frappe.db.get_value("Currency", context.doc.currency, "symbol")
42 context.doc.number_format = frappe.db.get_value("Currency", context.doc.currency, "number_format")
Rohit Waghchaureae270d62016-04-04 18:49:26 +053043 context.doc.buying_price_list = supplier_doc.default_price_list or ''