blob: bdbf3f4ec2b8c72eb664648c7d4104d0b58ac1ff [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 Mehtacc8b2b22017-03-31 12:44:29 +05306from frappe import _
Anand Doshidffec8f2014-07-01 17:45:15 +05307from frappe.utils import flt
Nabin Haitad4f1c72016-12-09 12:14:47 +05308from frappe.utils import get_datetime_str, nowdate
Rushabh Mehtacf99dc02017-02-09 18:06:11 +05309
Anand Doshi61a2f682013-06-21 17:55:31 +053010def get_root_of(doctype):
Nabin Hait87d70272016-12-08 14:43:11 +053011 """Get root element of a DocType with a tree structure"""
12 result = frappe.db.sql_list("""select name from `tab%s`
13 where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" %
14 (doctype, doctype))
15 return result[0] if result else None
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053016
Anand Doshi61a2f682013-06-21 17:55:31 +053017def get_ancestors_of(doctype, name):
Nabin Hait87d70272016-12-08 14:43:11 +053018 """Get ancestor elements of a DocType with a tree structure"""
19 lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"])
20 result = frappe.db.sql_list("""select name from `tab%s`
21 where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt))
22 return result or []
Anand Doshi61a2f682013-06-21 17:55:31 +053023
Rushabh Mehtaa1516472014-04-30 19:38:28 +053024def before_tests():
Nabin Hait87d70272016-12-08 14:43:11 +053025 frappe.clear_cache()
26 # complete setup if missing
27 from frappe.desk.page.setup_wizard.setup_wizard import setup_complete
28 if not frappe.get_list("Company"):
29 setup_complete({
30 "currency" :"USD",
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053031 "full_name" :"Test User",
Nabin Hait87d70272016-12-08 14:43:11 +053032 "company_name" :"Wind Power LLC",
33 "timezone" :"America/New_York",
34 "company_abbr" :"WP",
35 "industry" :"Manufacturing",
36 "country" :"United States",
37 "fy_start_date" :"2011-01-01",
38 "fy_end_date" :"2011-12-31",
39 "language" :"english",
40 "company_tagline" :"Testing",
41 "email" :"test@erpnext.com",
42 "password" :"test",
43 "chart_of_accounts" : "Standard",
Rushabh Mehta43ef4e92017-07-03 11:53:07 +053044 "domain" : "Manufacturing"
Nabin Hait87d70272016-12-08 14:43:11 +053045 })
Rushabh Mehtaa1516472014-04-30 19:38:28 +053046
Nabin Hait87d70272016-12-08 14:43:11 +053047 frappe.db.sql("delete from `tabLeave Allocation`")
48 frappe.db.sql("delete from `tabLeave Application`")
49 frappe.db.sql("delete from `tabSalary Slip`")
50 frappe.db.sql("delete from `tabItem Price`")
Rushabh Mehta5bd39422015-08-03 15:09:48 +053051
Nabin Hait87d70272016-12-08 14:43:11 +053052 frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0)
Makarand Bauskard0109a62017-07-26 16:29:22 +053053 enable_all_roles_and_domains()
Rushabh Mehta5bd39422015-08-03 15:09:48 +053054
Nabin Hait87d70272016-12-08 14:43:11 +053055 frappe.db.commit()
Anand Doshidffec8f2014-07-01 17:45:15 +053056
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053057@frappe.whitelist()
Nabin Haitad4f1c72016-12-09 12:14:47 +053058def get_exchange_rate(from_currency, to_currency, transaction_date=None):
59 if not transaction_date:
60 transaction_date = nowdate()
61 if not (from_currency and to_currency):
Nabin Hait87d70272016-12-08 14:43:11 +053062 # manqala 19/09/2016: Should this be an empty return or should it throw and exception?
63 return
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053064
Nabin Hait87d70272016-12-08 14:43:11 +053065 if from_currency == to_currency:
66 return 1
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053067
Nabin Hait288a18e2016-12-08 15:36:23 +053068 # cksgb 19/09/2016: get last entry in Currency Exchange with from_currency and to_currency.
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053069 entries = frappe.get_all("Currency Exchange", fields = ["exchange_rate"],
Nabin Hait87d70272016-12-08 14:43:11 +053070 filters=[
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053071 ["date", "<=", get_datetime_str(transaction_date)],
72 ["from_currency", "=", from_currency],
Nabin Hait87d70272016-12-08 14:43:11 +053073 ["to_currency", "=", to_currency]
74 ], order_by="date desc", limit=1)
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053075
Nabin Hait87d70272016-12-08 14:43:11 +053076 if entries:
77 return flt(entries[0].exchange_rate)
Rushabh Mehta746fd902015-10-15 11:50:38 +053078
Nabin Hait87d70272016-12-08 14:43:11 +053079 try:
80 cache = frappe.cache()
81 key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
82 value = cache.get(key)
Rushabh Mehta9f436a72015-10-15 11:57:46 +053083
Nabin Hait87d70272016-12-08 14:43:11 +053084 if not value:
85 import requests
tundebabzy7f8a2592017-08-31 10:00:15 +010086 api_url = "http://api.fixer.io/{0}".format(transaction_date)
87 response = requests.get(api_url, params={
Nabin Hait87d70272016-12-08 14:43:11 +053088 "base": from_currency,
89 "symbols": to_currency
90 })
91 # expire in 6 hours
92 response.raise_for_status()
93 value = response.json()["rates"][to_currency]
94 cache.setex(key, value, 6 * 60 * 60)
Nabin Hait87d70272016-12-08 14:43:11 +053095 return flt(value)
96 except:
Nabin Hait7a9bd412017-05-24 16:18:27 +053097 frappe.msgprint(_("Unable to find exchange rate for {0} to {1} for key date {2}. Please create a Currency Exchange record manually").format(from_currency, to_currency, transaction_date))
Makarand Bauskarddd48452017-07-06 11:09:34 +053098 return 0.0
Makarand Bauskard0109a62017-07-26 16:29:22 +053099
100def enable_all_roles_and_domains():
101 """ enable all roles and domain for testing """
102 roles = frappe.get_list("Role", filters={"disabled": 1})
103 for role in roles:
104 _role = frappe.get_doc("Role", role.get("name"))
105 _role.disabled = 0
106 _role.flags.ignore_mandatory = True
107 _role.flags.ignore_permissions = True
108 _role.save()
109
110 # add all roles to users
Zarrarc5451062017-07-27 17:19:06 +0530111 user = frappe.get_doc("User", "Administrator")
Makarand Bauskard0109a62017-07-26 16:29:22 +0530112 user.add_roles(*[role.get("name") for role in roles])
113
114 domains = frappe.get_list("Domain")
115 if not domains:
116 return
117
118 domain_settings = frappe.get_doc("Domain Settings", "Domain Settings")
119 domain_settings.set("active_domains", [])
120 for domain in domains:
121 row = domain_settings.append("active_domains", {})
122 row.domain=domain.get("name")
123
124 domain_settings.save()