blob: 68a5a5ab45c42efc4d1cdb92c57efce1d4483e53 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Anand Doshi9d9aec12013-01-17 15:44:57 +05303
4from __future__ import unicode_literals
5import webnotes
Akhilesh Darjeee82eee52014-01-20 16:09:45 +05306from webnotes import _, msgprint, throw
Anand Doshif8f0c0d2013-01-17 20:20:56 +05307import json
Anand Doshi9d9aec12013-01-17 15:44:57 +05308
9def get_company_currency(company):
10 currency = webnotes.conn.get_value("Company", company, "default_currency")
11 if not currency:
12 currency = webnotes.conn.get_default("currency")
13 if not currency:
Akhilesh Darjeee82eee52014-01-20 16:09:45 +053014 throw(_('Please specify Default Currency in Company Master \
15 and Global Defaults'))
Anand Doshi9d9aec12013-01-17 15:44:57 +053016
Anand Doshif8f0c0d2013-01-17 20:20:56 +053017 return currency
18
Anand Doshi61a2f682013-06-21 17:55:31 +053019def get_root_of(doctype):
20 """Get root element of a DocType with a tree structure"""
21 result = webnotes.conn.sql_list("""select name from `tab%s`
Nabin Hait869913c2013-06-28 15:00:24 +053022 where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" %
23 (doctype, doctype))
Anand Doshi61a2f682013-06-21 17:55:31 +053024 return result[0] if result else None
25
26def get_ancestors_of(doctype, name):
27 """Get ancestor elements of a DocType with a tree structure"""
28 lft, rgt = webnotes.conn.get_value(doctype, name, ["lft", "rgt"])
29 result = webnotes.conn.sql_list("""select name from `tab%s`
Anand Doshi99100a42013-07-04 17:13:53 +053030 where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt))
31 return result or []
Anand Doshi61a2f682013-06-21 17:55:31 +053032
Anand Doshif8f0c0d2013-01-17 20:20:56 +053033@webnotes.whitelist()
Rushabh Mehta4a404e92013-08-09 18:11:35 +053034def get_price_list_currency(price_list):
Akhilesh Darjeee82eee52014-01-20 16:09:45 +053035 price_list_currency = webnotes.conn.get_value("Price List", {"name": price_list,
36 "enabled": 1}, "currency")
37
38 if not price_list_currency:
39 throw("{message}: {price_list} {disabled}".format(**{
40 "message": _("Price List"),
41 "price_list": price_list,
42 "disabled": _("is disabled.")
43 }))
44 else:
45 return {"price_list_currency": price_list_currency}