blob: abc28904c328cd4da95b0761ac7c5a04144b839f [file] [log] [blame]
rohitwaghchaurea1064a62016-03-03 14:00:35 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import frappe
6from frappe import _
Rohit Waghchaurea26f6852016-10-03 19:05:18 +05307from frappe.utils import formatdate
Rohit Waghchaureae270d62016-04-04 18:49:26 +05308from erpnext.controllers.website_list_for_contact import (get_customers_suppliers,
9 get_party_details)
rohitwaghchaurea1064a62016-03-03 14:00:35 +053010
11def get_context(context):
12 context.no_cache = 1
Rushabh Mehta878dfec2016-06-28 17:11:32 +053013 context.show_sidebar = True
rohitwaghchaurea1064a62016-03-03 14:00:35 +053014 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 Waghchaurea26f6852016-10-03 19:05:18 +053017 context.doc.rfq_links = get_link_quotation(context.doc.supplier, context.doc.name)
rohitwaghchaure62fea032016-03-30 13:24:42 +053018 unauthorized_user(context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053019 update_supplier_details(context)
rohitwaghchaurea1064a62016-03-03 14:00:35 +053020 context["title"] = frappe.form_dict.name
21
rohitwaghchaurea1064a62016-03-03 14:00:35 +053022def get_supplier():
rohitwaghchaure62fea032016-03-30 13:24:42 +053023 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 ''
rohitwaghchaurea1064a62016-03-03 14:00:35 +053028
29def check_supplier_has_docname_access(supplier):
30 status = True
Rushabh Mehta7a1b5da2016-03-29 16:04:25 +053031 if frappe.form_dict.name not in frappe.db.sql_list("""select parent from `tabRequest for Quotation Supplier`
rohitwaghchaurea1064a62016-03-03 14:00:35 +053032 where supplier = '{supplier}'""".format(supplier=supplier)):
33 status = False
34 return status
rohitwaghchaure62fea032016-03-30 13:24:42 +053035
36def unauthorized_user(supplier):
Rohit Waghchaureae270d62016-04-04 18:49:26 +053037 status = check_supplier_has_docname_access(supplier) or False
rohitwaghchaure62fea032016-03-30 13:24:42 +053038 if status == False:
39 frappe.throw(_("Not Permitted"), frappe.PermissionError)
40
41def update_supplier_details(context):
42 supplier_doc = frappe.get_doc("Supplier", context.doc.supplier)
Rohit Waghchaureae270d62016-04-04 18:49:26 +053043 context.doc.currency = supplier_doc.default_currency or frappe.db.get_value("Company", context.doc.company, "default_currency")
rohitwaghchaure62fea032016-03-30 13:24:42 +053044 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 Waghchaurea26f6852016-10-03 19:05:18 +053046 context.doc.buying_price_list = supplier_doc.default_price_list or ''
47
48def 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