Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 19 | from webnotes import _, msgprint |
| 20 | import json |
Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 21 | |
| 22 | def get_company_currency(company): |
| 23 | currency = webnotes.conn.get_value("Company", company, "default_currency") |
| 24 | if not currency: |
| 25 | currency = webnotes.conn.get_default("currency") |
| 26 | if not currency: |
| 27 | msgprint(_('Please specify Default Currency in Company Master \ |
| 28 | and Global Defaults'), raise_exception=True) |
| 29 | |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 30 | return currency |
| 31 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 32 | def get_root_of(doctype): |
| 33 | """Get root element of a DocType with a tree structure""" |
| 34 | result = webnotes.conn.sql_list("""select name from `tab%s` |
Nabin Hait | 869913c | 2013-06-28 15:00:24 +0530 | [diff] [blame] | 35 | where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" % |
| 36 | (doctype, doctype)) |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 37 | return result[0] if result else None |
| 38 | |
| 39 | def get_ancestors_of(doctype, name): |
| 40 | """Get ancestor elements of a DocType with a tree structure""" |
| 41 | lft, rgt = webnotes.conn.get_value(doctype, name, ["lft", "rgt"]) |
| 42 | result = webnotes.conn.sql_list("""select name from `tab%s` |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 43 | where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt)) |
| 44 | return result or [] |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 45 | |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 46 | @webnotes.whitelist() |
| 47 | def get_price_list_currency(args): |
| 48 | """ |
| 49 | args = { |
| 50 | "price_list_name": "Something", |
Anand Doshi | 060d924 | 2013-06-12 17:40:36 +0530 | [diff] [blame] | 51 | "buying_or_selling": "Buying" or "Selling" |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 52 | } |
| 53 | """ |
| 54 | if isinstance(args, basestring): |
| 55 | args = json.loads(args) |
| 56 | |
Nabin Hait | 380f555 | 2013-02-28 18:41:49 +0530 | [diff] [blame] | 57 | result = webnotes.conn.sql("""select distinct ref_currency from `tabItem Price` |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 58 | where price_list_name=%s and buying_or_selling=%s""", |
| 59 | (args.get("price_list_name"), args.get("buying_or_selling"))) |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 60 | if result and len(result)==1: |
| 61 | return {"price_list_currency": result[0][0]} |
| 62 | else: |
Anand Doshi | 060d924 | 2013-06-12 17:40:36 +0530 | [diff] [blame] | 63 | return {} |