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 |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 6 | import frappe |
| 7 | from frappe import _ |
Rohit Waghchaure | a26f685 | 2016-10-03 19:05:18 +0530 | [diff] [blame] | 8 | from frappe.utils import formatdate |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Suraj Shetty | 83705af | 2019-08-12 11:51:27 +0530 | [diff] [blame] | 10 | from erpnext.controllers.website_list_for_contact import get_customers_suppliers |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 11 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 12 | |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 13 | def get_context(context): |
| 14 | context.no_cache = 1 |
Rushabh Mehta | 878dfec | 2016-06-28 17:11:32 +0530 | [diff] [blame] | 15 | context.show_sidebar = True |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 16 | context.doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name) |
| 17 | context.parents = frappe.form_dict.parents |
| 18 | context.doc.supplier = get_supplier() |
Rohit Waghchaure | a26f685 | 2016-10-03 19:05:18 +0530 | [diff] [blame] | 19 | context.doc.rfq_links = get_link_quotation(context.doc.supplier, context.doc.name) |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 20 | unauthorized_user(context.doc.supplier) |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 21 | update_supplier_details(context) |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 22 | context["title"] = frappe.form_dict.name |
| 23 | |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 24 | def get_supplier(): |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 25 | doctype = frappe.form_dict.doctype |
| 26 | parties_doctype = 'Request for Quotation Supplier' if doctype == 'Request for Quotation' else doctype |
| 27 | customers, suppliers = get_customers_suppliers(parties_doctype, frappe.session.user) |
Suraj Shetty | 83705af | 2019-08-12 11:51:27 +0530 | [diff] [blame] | 28 | |
| 29 | return suppliers[0] if suppliers else '' |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 30 | |
| 31 | def check_supplier_has_docname_access(supplier): |
| 32 | status = True |
Rushabh Mehta | 7a1b5da | 2016-03-29 16:04:25 +0530 | [diff] [blame] | 33 | if frappe.form_dict.name not in frappe.db.sql_list("""select parent from `tabRequest for Quotation Supplier` |
tunde | dcd5420 | 2017-06-22 11:02:19 +0100 | [diff] [blame] | 34 | where supplier = %s""", (supplier,)): |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 35 | status = False |
| 36 | return status |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 37 | |
| 38 | def unauthorized_user(supplier): |
Rohit Waghchaure | ae270d6 | 2016-04-04 18:49:26 +0530 | [diff] [blame] | 39 | status = check_supplier_has_docname_access(supplier) or False |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 40 | if status == False: |
| 41 | frappe.throw(_("Not Permitted"), frappe.PermissionError) |
| 42 | |
| 43 | def update_supplier_details(context): |
| 44 | supplier_doc = frappe.get_doc("Supplier", context.doc.supplier) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 45 | context.doc.currency = supplier_doc.default_currency or frappe.get_cached_value('Company', context.doc.company, "default_currency") |
| 46 | context.doc.currency_symbol = frappe.db.get_value("Currency", context.doc.currency, "symbol", cache=True) |
| 47 | context.doc.number_format = frappe.db.get_value("Currency", context.doc.currency, "number_format", cache=True) |
Rohit Waghchaure | a26f685 | 2016-10-03 19:05:18 +0530 | [diff] [blame] | 48 | context.doc.buying_price_list = supplier_doc.default_price_list or '' |
| 49 | |
| 50 | def get_link_quotation(supplier, rfq): |
| 51 | quotation = frappe.db.sql(""" select distinct `tabSupplier Quotation Item`.parent as name, |
| 52 | `tabSupplier Quotation`.status, `tabSupplier Quotation`.transaction_date from |
| 53 | `tabSupplier Quotation Item`, `tabSupplier Quotation` where `tabSupplier Quotation`.docstatus < 2 and |
| 54 | `tabSupplier Quotation Item`.request_for_quotation =%(name)s and |
| 55 | `tabSupplier Quotation Item`.parent = `tabSupplier Quotation`.name and |
| 56 | `tabSupplier Quotation`.supplier = %(supplier)s order by `tabSupplier Quotation`.creation desc""", |
| 57 | {'name': rfq, 'supplier': supplier}, as_dict=1) |
| 58 | |
| 59 | for data in quotation: |
| 60 | data.transaction_date = formatdate(data.transaction_date) |
| 61 | |
| 62 | return quotation or None |