rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
| 6 | from frappe import _ |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 7 | from erpnext.controllers.website_list_for_contact import (get_customers_suppliers, |
| 8 | get_party_details) |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 9 | |
| 10 | def get_context(context): |
| 11 | context.no_cache = 1 |
Rushabh Mehta | 878dfec | 2016-06-28 17:11:32 +0530 | [diff] [blame] | 12 | context.show_sidebar = True |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 13 | 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() |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 16 | unauthorized_user(context.doc.supplier) |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 17 | update_supplier_details(context) |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 18 | context["title"] = frappe.form_dict.name |
| 19 | |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 20 | def get_supplier(): |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 21 | 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 '' |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 26 | |
| 27 | def check_supplier_has_docname_access(supplier): |
| 28 | status = True |
Rushabh Mehta | 7a1b5da | 2016-03-29 16:04:25 +0530 | [diff] [blame] | 29 | if frappe.form_dict.name not in frappe.db.sql_list("""select parent from `tabRequest for Quotation Supplier` |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 30 | where supplier = '{supplier}'""".format(supplier=supplier)): |
| 31 | status = False |
| 32 | return status |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 33 | |
| 34 | def unauthorized_user(supplier): |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 35 | status = check_supplier_has_docname_access(supplier) or False |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 36 | if status == False: |
| 37 | frappe.throw(_("Not Permitted"), frappe.PermissionError) |
| 38 | |
| 39 | def update_supplier_details(context): |
| 40 | supplier_doc = frappe.get_doc("Supplier", context.doc.supplier) |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 41 | context.doc.currency = supplier_doc.default_currency or frappe.db.get_value("Company", context.doc.company, "default_currency") |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 42 | 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 Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 44 | context.doc.buying_price_list = supplier_doc.default_price_list or '' |