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