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 |
| 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() |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 15 | unauthorized_user(context.doc.supplier) |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 16 | update_supplier_details(context) |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 17 | context["title"] = frappe.form_dict.name |
| 18 | |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 19 | def get_supplier(): |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 20 | 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 '' |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 25 | |
| 26 | def check_supplier_has_docname_access(supplier): |
| 27 | status = True |
Rushabh Mehta | 7a1b5da | 2016-03-29 16:04:25 +0530 | [diff] [blame] | 28 | 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] | 29 | where supplier = '{supplier}'""".format(supplier=supplier)): |
| 30 | status = False |
| 31 | return status |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 32 | |
| 33 | def unauthorized_user(supplier): |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 34 | status = check_supplier_has_docname_access(supplier) or False |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 35 | if status == False: |
| 36 | frappe.throw(_("Not Permitted"), frappe.PermissionError) |
| 37 | |
| 38 | def update_supplier_details(context): |
| 39 | supplier_doc = frappe.get_doc("Supplier", context.doc.supplier) |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 40 | 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] | 41 | 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 Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 43 | context.doc.buying_price_list = supplier_doc.default_price_list or '' |