blob: efbc12f6f8bdc864e137e76300935bc106645dda [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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe 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):
Anand Doshie9baaa62014-02-26 12:35:33 +053010 currency = frappe.db.get_value("Company", company, "default_currency")
Anand Doshi9d9aec12013-01-17 15:44:57 +053011 if not currency:
Anand Doshie9baaa62014-02-26 12:35:33 +053012 currency = frappe.db.get_default("currency")
Anand Doshi9d9aec12013-01-17 15:44:57 +053013 if not currency:
Rushabh Mehtaff938022014-04-15 18:40:00 +053014 throw(_('Please specify Default Currency in Company Master and Global Defaults'))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053015
Anand Doshif8f0c0d2013-01-17 20:20:56 +053016 return currency
17
Anand Doshi61a2f682013-06-21 17:55:31 +053018def get_root_of(doctype):
19 """Get root element of a DocType with a tree structure"""
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053020 result = frappe.db.sql_list("""select name from `tab%s`
21 where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" %
Nabin Hait869913c2013-06-28 15:00:24 +053022 (doctype, doctype))
Anand Doshi61a2f682013-06-21 17:55:31 +053023 return result[0] if result else None
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053024
Anand Doshi61a2f682013-06-21 17:55:31 +053025def get_ancestors_of(doctype, name):
26 """Get ancestor elements of a DocType with a tree structure"""
Anand Doshie9baaa62014-02-26 12:35:33 +053027 lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"])
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053028 result = frappe.db.sql_list("""select name from `tab%s`
Anand Doshi99100a42013-07-04 17:13:53 +053029 where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt))
30 return result or []
Anand Doshi61a2f682013-06-21 17:55:31 +053031
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053032@frappe.whitelist()
Rushabh Mehta4a404e92013-08-09 18:11:35 +053033def get_price_list_currency(price_list):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053034 price_list_currency = frappe.db.get_value("Price List", {"name": price_list,
Akhilesh Darjeee82eee52014-01-20 16:09:45 +053035 "enabled": 1}, "currency")
36
37 if not price_list_currency:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053038 throw(_("Price List {0} is disabled").format(price_list))
Akhilesh Darjeee82eee52014-01-20 16:09:45 +053039 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053040 return {"price_list_currency": price_list_currency}