Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 6 | from frappe import _, throw |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 7 | from frappe.utils import flt |
Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 8 | |
| 9 | def get_company_currency(company): |
Rushabh Mehta | 50ce975 | 2015-04-27 13:13:38 +0530 | [diff] [blame] | 10 | currency = frappe.db.get_value("Company", company, "default_currency", cache=True) |
Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 11 | if not currency: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 12 | currency = frappe.db.get_default("currency") |
Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 13 | if not currency: |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 14 | throw(_('Please specify Default Currency in Company Master and Global Defaults')) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 15 | |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 16 | return currency |
| 17 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 18 | def get_root_of(doctype): |
| 19 | """Get root element of a DocType with a tree structure""" |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 20 | 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 Hait | 869913c | 2013-06-28 15:00:24 +0530 | [diff] [blame] | 22 | (doctype, doctype)) |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 23 | return result[0] if result else None |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 24 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 25 | def get_ancestors_of(doctype, name): |
| 26 | """Get ancestor elements of a DocType with a tree structure""" |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 27 | lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"]) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 28 | result = frappe.db.sql_list("""select name from `tab%s` |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 29 | where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt)) |
| 30 | return result or [] |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 31 | |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 32 | def before_tests(): |
Nabin Hait | 7c9d003 | 2015-03-13 08:14:31 +0530 | [diff] [blame] | 33 | frappe.clear_cache() |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 34 | # complete setup if missing |
| 35 | from erpnext.setup.page.setup_wizard.setup_wizard import setup_account |
Nabin Hait | 7c9d003 | 2015-03-13 08:14:31 +0530 | [diff] [blame] | 36 | if not frappe.get_list("Company"): |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 37 | setup_account({ |
| 38 | "currency" :"USD", |
| 39 | "first_name" :"Test", |
| 40 | "last_name" :"User", |
| 41 | "company_name" :"Wind Power LLC", |
| 42 | "timezone" :"America/New_York", |
| 43 | "company_abbr" :"WP", |
| 44 | "industry" :"Manufacturing", |
| 45 | "country" :"United States", |
| 46 | "fy_start_date" :"2014-01-01", |
| 47 | "fy_end_date" :"2014-12-31", |
| 48 | "language" :"english", |
| 49 | "company_tagline" :"Testing", |
| 50 | "email" :"test@erpnext.com", |
Nabin Hait | 249bbbc | 2014-11-26 15:35:08 +0530 | [diff] [blame] | 51 | "password" :"test", |
| 52 | "chart_of_accounts" : "Standard" |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 53 | }) |
| 54 | |
| 55 | frappe.db.sql("delete from `tabLeave Allocation`") |
| 56 | frappe.db.sql("delete from `tabLeave Application`") |
| 57 | frappe.db.sql("delete from `tabSalary Slip`") |
| 58 | frappe.db.sql("delete from `tabItem Price`") |
| 59 | frappe.db.commit() |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 60 | |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 61 | @frappe.whitelist() |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 62 | def get_exchange_rate(from_currency, to_currency): |
Pratik Vyas | a0f2510 | 2015-06-30 12:36:17 +0530 | [diff] [blame] | 63 | try: |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 64 | cache = frappe.cache() |
| 65 | key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency) |
| 66 | value = cache.get(key) |
| 67 | if not value: |
| 68 | import requests |
Pratik Vyas | a0f2510 | 2015-06-30 12:36:17 +0530 | [diff] [blame] | 69 | response = requests.get("http://api.fixer.io/latest", params={ |
| 70 | "base": from_currency, |
| 71 | "symbols": to_currency |
| 72 | }) |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 73 | # expire in 24 hours |
Pratik Vyas | a0f2510 | 2015-06-30 12:36:17 +0530 | [diff] [blame] | 74 | response.raise_for_status() |
| 75 | value = response.json()["rates"][to_currency] |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 76 | cache.setex(key, value, 24 * 60 * 60) |
| 77 | return flt(value) |
Pratik Vyas | a0f2510 | 2015-06-30 12:36:17 +0530 | [diff] [blame] | 78 | except: |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 79 | exchange = "%s-%s" % (from_currency, to_currency) |
| 80 | return flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate")) |