blob: 9a8ae071136cce1fed82ad71fa492e5ba2ffe5db [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
Chude Osiegbu7be3aa32016-09-05 23:35:00 +01008from frappe.utils import get_datetime, get_datetime_str
Nabin Hait87d70272016-12-08 14:43:11 +05309
Anand Doshi9d9aec12013-01-17 15:44:57 +053010def get_company_currency(company):
Nabin Hait87d70272016-12-08 14:43:11 +053011 currency = frappe.db.get_value("Company", company, "default_currency", cache=True)
12 if not currency:
13 currency = frappe.db.get_default("currency")
14 if not currency:
15 throw(_('Please specify Default Currency in Company Master and Global Defaults'))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053016
Nabin Hait87d70272016-12-08 14:43:11 +053017 return currency
Anand Doshif8f0c0d2013-01-17 20:20:56 +053018
Anand Doshi61a2f682013-06-21 17:55:31 +053019def get_root_of(doctype):
Nabin Hait87d70272016-12-08 14:43:11 +053020 """Get root element of a DocType with a tree structure"""
21 result = frappe.db.sql_list("""select name from `tab%s`
22 where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" %
23 (doctype, doctype))
24 return result[0] if result else None
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053025
Anand Doshi61a2f682013-06-21 17:55:31 +053026def get_ancestors_of(doctype, name):
Nabin Hait87d70272016-12-08 14:43:11 +053027 """Get ancestor elements of a DocType with a tree structure"""
28 lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"])
29 result = frappe.db.sql_list("""select name from `tab%s`
30 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
Rushabh Mehtaa1516472014-04-30 19:38:28 +053033def before_tests():
Nabin Hait87d70272016-12-08 14:43:11 +053034 frappe.clear_cache()
35 # complete setup if missing
36 from frappe.desk.page.setup_wizard.setup_wizard import setup_complete
37 if not frappe.get_list("Company"):
38 setup_complete({
39 "currency" :"USD",
40 "first_name" :"Test",
41 "last_name" :"User",
42 "company_name" :"Wind Power LLC",
43 "timezone" :"America/New_York",
44 "company_abbr" :"WP",
45 "industry" :"Manufacturing",
46 "country" :"United States",
47 "fy_start_date" :"2011-01-01",
48 "fy_end_date" :"2011-12-31",
49 "language" :"english",
50 "company_tagline" :"Testing",
51 "email" :"test@erpnext.com",
52 "password" :"test",
53 "chart_of_accounts" : "Standard",
54 "domain" : "Manufacturing",
55
56 })
Rushabh Mehtaa1516472014-04-30 19:38:28 +053057
Nabin Hait87d70272016-12-08 14:43:11 +053058 frappe.db.sql("delete from `tabLeave Allocation`")
59 frappe.db.sql("delete from `tabLeave Application`")
60 frappe.db.sql("delete from `tabSalary Slip`")
61 frappe.db.sql("delete from `tabItem Price`")
Rushabh Mehta5bd39422015-08-03 15:09:48 +053062
Nabin Hait87d70272016-12-08 14:43:11 +053063 frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0)
Rushabh Mehta5bd39422015-08-03 15:09:48 +053064
Nabin Hait87d70272016-12-08 14:43:11 +053065 frappe.db.commit()
Anand Doshidffec8f2014-07-01 17:45:15 +053066
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053067@frappe.whitelist()
Nabin Hait288a18e2016-12-08 15:36:23 +053068def get_exchange_rate(transaction_date, from_currency, to_currency):
69 if not (transaction_date and from_currency and to_currency):
Nabin Hait87d70272016-12-08 14:43:11 +053070 # manqala 19/09/2016: Should this be an empty return or should it throw and exception?
71 return
72
73 if from_currency == to_currency:
74 return 1
75
Nabin Hait288a18e2016-12-08 15:36:23 +053076 # cksgb 19/09/2016: get last entry in Currency Exchange with from_currency and to_currency.
Nabin Hait87d70272016-12-08 14:43:11 +053077 entries = frappe.get_all("Currency Exchange", fields = ["exchange_rate"],
78 filters=[
Nabin Hait288a18e2016-12-08 15:36:23 +053079 ["date", "<=", get_datetime_str(transaction_date)],
Nabin Hait87d70272016-12-08 14:43:11 +053080 ["from_currency", "=", from_currency],
81 ["to_currency", "=", to_currency]
82 ], order_by="date desc", limit=1)
Nabin Hait288a18e2016-12-08 15:36:23 +053083
Nabin Hait87d70272016-12-08 14:43:11 +053084 if entries:
85 return flt(entries[0].exchange_rate)
Rushabh Mehta746fd902015-10-15 11:50:38 +053086
Nabin Hait87d70272016-12-08 14:43:11 +053087 try:
88 cache = frappe.cache()
89 key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
90 value = cache.get(key)
Rushabh Mehta9f436a72015-10-15 11:57:46 +053091
Nabin Hait87d70272016-12-08 14:43:11 +053092 if not value:
93 import requests
94 response = requests.get("http://api.fixer.io/latest", params={
95 "base": from_currency,
96 "symbols": to_currency
97 })
98 # expire in 6 hours
99 response.raise_for_status()
100 value = response.json()["rates"][to_currency]
101 cache.setex(key, value, 6 * 60 * 60)
Anand Doshi70f57eb2015-11-27 14:37:40 +0530102
Nabin Hait87d70272016-12-08 14:43:11 +0530103 return flt(value)
104 except:
Nabin Hait288a18e2016-12-08 15:36:23 +0530105 frappe.msgprint(_("Unable to find exchange rate for {0} to {1} for key date {2}").format(from_currency, to_currency, transaction_date))
Nabin Hait87d70272016-12-08 14:43:11 +0530106 return 0.0