blob: 5480613b4eb033879459eeb848010d77d0dec565 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe 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
Rushabh Mehtaa1516472014-04-30 19:38:28 +05306from frappe import _, throw
Anand Doshidffec8f2014-07-01 17:45:15 +05307from frappe.utils import flt
Anand Doshi9d9aec12013-01-17 15:44:57 +05308
9def get_company_currency(company):
Rushabh Mehta50ce9752015-04-27 13:13:38 +053010 currency = frappe.db.get_value("Company", company, "default_currency", cache=True)
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 Mehtaa1516472014-04-30 19:38:28 +053032def before_tests():
Nabin Hait7c9d0032015-03-13 08:14:31 +053033 frappe.clear_cache()
Rushabh Mehtaa1516472014-04-30 19:38:28 +053034 # complete setup if missing
35 from erpnext.setup.page.setup_wizard.setup_wizard import setup_account
Nabin Hait7c9d0032015-03-13 08:14:31 +053036 if not frappe.get_list("Company"):
Rushabh Mehtaa1516472014-04-30 19:38:28 +053037 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 Hait249bbbc2014-11-26 15:35:08 +053051 "password" :"test",
52 "chart_of_accounts" : "Standard"
Rushabh Mehtaa1516472014-04-30 19:38:28 +053053 })
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`")
Rushabh Mehta5bd39422015-08-03 15:09:48 +053059
60 frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0)
61
Rushabh Mehtaa1516472014-04-30 19:38:28 +053062 frappe.db.commit()
Anand Doshidffec8f2014-07-01 17:45:15 +053063
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053064@frappe.whitelist()
Anand Doshidffec8f2014-07-01 17:45:15 +053065def get_exchange_rate(from_currency, to_currency):
Pratik Vyasa0f25102015-06-30 12:36:17 +053066 try:
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053067 cache = frappe.cache()
68 key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
69 value = cache.get(key)
70 if not value:
71 import requests
Pratik Vyasa0f25102015-06-30 12:36:17 +053072 response = requests.get("http://api.fixer.io/latest", params={
73 "base": from_currency,
74 "symbols": to_currency
75 })
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053076 # expire in 24 hours
Pratik Vyasa0f25102015-06-30 12:36:17 +053077 response.raise_for_status()
78 value = response.json()["rates"][to_currency]
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053079 cache.setex(key, value, 24 * 60 * 60)
80 return flt(value)
Pratik Vyasa0f25102015-06-30 12:36:17 +053081 except:
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053082 exchange = "%s-%s" % (from_currency, to_currency)
83 return flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate"))