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