blob: 2302a66fa0308c7ea24a71d5f2d42597a9587cac [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
Chillar Anand915b3432021-09-02 16:44:59 +05305
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05306import frappe
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +05307from frappe import _
Chillar Anand915b3432021-09-02 16:44:59 +05308from frappe.utils import add_days, flt, get_datetime_str, nowdate
9
Neil Trini Lasrado771fbb82018-08-24 15:15:56 +053010from erpnext import get_default_company
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053011
Chillar Anand915b3432021-09-02 16:44:59 +053012
Anand Doshi61a2f682013-06-21 17:55:31 +053013def get_root_of(doctype):
Nabin Hait87d70272016-12-08 14:43:11 +053014 """Get root element of a DocType with a tree structure"""
15 result = frappe.db.sql_list("""select name from `tab%s`
16 where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" %
17 (doctype, doctype))
18 return result[0] if result else None
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053019
Anand Doshi61a2f682013-06-21 17:55:31 +053020def get_ancestors_of(doctype, name):
Nabin Hait87d70272016-12-08 14:43:11 +053021 """Get ancestor elements of a DocType with a tree structure"""
22 lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"])
23 result = frappe.db.sql_list("""select name from `tab%s`
24 where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt))
25 return result or []
Anand Doshi61a2f682013-06-21 17:55:31 +053026
Rushabh Mehtaa1516472014-04-30 19:38:28 +053027def before_tests():
Nabin Hait87d70272016-12-08 14:43:11 +053028 frappe.clear_cache()
29 # complete setup if missing
30 from frappe.desk.page.setup_wizard.setup_wizard import setup_complete
31 if not frappe.get_list("Company"):
32 setup_complete({
Ankush Menat22cb6422021-07-02 23:32:33 +053033 "currency" :"USD",
34 "full_name" :"Test User",
35 "company_name" :"Wind Power LLC",
36 "timezone" :"America/New_York",
37 "company_abbr" :"WP",
38 "industry" :"Manufacturing",
39 "country" :"United States",
40 "fy_start_date" :"2021-01-01",
41 "fy_end_date" :"2021-12-31",
42 "language" :"english",
43 "company_tagline" :"Testing",
44 "email" :"test@erpnext.com",
45 "password" :"test",
Nabin Hait1749b7c2017-11-16 17:48:59 +053046 "chart_of_accounts" : "Standard",
Ankush Menat22cb6422021-07-02 23:32:33 +053047 "domains" : ["Manufacturing"],
Nabin Hait87d70272016-12-08 14:43:11 +053048 })
Rushabh Mehtaa1516472014-04-30 19:38:28 +053049
Nabin Hait87d70272016-12-08 14:43:11 +053050 frappe.db.sql("delete from `tabLeave Allocation`")
51 frappe.db.sql("delete from `tabLeave Application`")
52 frappe.db.sql("delete from `tabSalary Slip`")
53 frappe.db.sql("delete from `tabItem Price`")
Rushabh Mehta5bd39422015-08-03 15:09:48 +053054
Nabin Hait87d70272016-12-08 14:43:11 +053055 frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0)
Makarand Bauskard0109a62017-07-26 16:29:22 +053056 enable_all_roles_and_domains()
Rushabh Mehta5bd39422015-08-03 15:09:48 +053057
Nabin Hait87d70272016-12-08 14:43:11 +053058 frappe.db.commit()
Anand Doshidffec8f2014-07-01 17:45:15 +053059
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053060@frappe.whitelist()
Shreya0db85062018-05-15 11:25:46 +053061def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=None):
Nabin Haitad4f1c72016-12-09 12:14:47 +053062 if not (from_currency and to_currency):
Nabin Hait87d70272016-12-08 14:43:11 +053063 # manqala 19/09/2016: Should this be an empty return or should it throw and exception?
64 return
Nabin Hait87d70272016-12-08 14:43:11 +053065 if from_currency == to_currency:
66 return 1
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053067
tundebabzyab5b0302017-09-21 10:20:39 +010068 if not transaction_date:
69 transaction_date = nowdate()
tundebabzyab5b0302017-09-21 10:20:39 +010070 currency_settings = frappe.get_doc("Accounts Settings").as_dict()
71 allow_stale_rates = currency_settings.get("allow_stale")
72
73 filters = [
74 ["date", "<=", get_datetime_str(transaction_date)],
75 ["from_currency", "=", from_currency],
76 ["to_currency", "=", to_currency]
77 ]
Shreyab547cdd2018-05-15 16:58:45 +053078
Shreya0db85062018-05-15 11:25:46 +053079 if args == "for_buying":
80 filters.append(["for_buying", "=", "1"])
81 elif args == "for_selling":
Shreyab547cdd2018-05-15 16:58:45 +053082 filters.append(["for_selling", "=", "1"])
83
tundebabzyab5b0302017-09-21 10:20:39 +010084 if not allow_stale_rates:
85 stale_days = currency_settings.get("stale_days")
86 checkpoint_date = add_days(transaction_date, -stale_days)
87 filters.append(["date", ">", get_datetime_str(checkpoint_date)])
88
Nabin Hait288a18e2016-12-08 15:36:23 +053089 # cksgb 19/09/2016: get last entry in Currency Exchange with from_currency and to_currency.
tundebabzyab5b0302017-09-21 10:20:39 +010090 entries = frappe.get_all(
91 "Currency Exchange", fields=["exchange_rate"], filters=filters, order_by="date desc",
92 limit=1)
Nabin Hait87d70272016-12-08 14:43:11 +053093 if entries:
94 return flt(entries[0].exchange_rate)
Rushabh Mehta746fd902015-10-15 11:50:38 +053095
Nabin Hait87d70272016-12-08 14:43:11 +053096 try:
97 cache = frappe.cache()
Dany Roberte9bc2f32021-06-28 19:01:52 +053098 key = "currency_exchange_rate_{0}:{1}:{2}".format(transaction_date, from_currency, to_currency)
Nabin Hait87d70272016-12-08 14:43:11 +053099 value = cache.get(key)
Rushabh Mehta9f436a72015-10-15 11:57:46 +0530100
Nabin Hait87d70272016-12-08 14:43:11 +0530101 if not value:
102 import requests
Dany Roberte9bc2f32021-06-28 19:01:52 +0530103 api_url = "https://api.exchangerate.host/convert"
tundebabzy7f8a2592017-08-31 10:00:15 +0100104 response = requests.get(api_url, params={
Dany Roberte9bc2f32021-06-28 19:01:52 +0530105 "date": transaction_date,
106 "from": from_currency,
107 "to": to_currency
Nabin Hait87d70272016-12-08 14:43:11 +0530108 })
109 # expire in 6 hours
110 response.raise_for_status()
Dany Roberte9bc2f32021-06-28 19:01:52 +0530111 value = response.json()["result"]
Dany Robert909995a2021-07-29 15:58:27 +0530112 cache.setex(name=key, time=21600, value=flt(value))
Nabin Hait87d70272016-12-08 14:43:11 +0530113 return flt(value)
Ankush Menat694ae812021-09-01 14:40:56 +0530114 except Exception:
Faris Ansaribadf1c42019-01-24 16:28:53 +0530115 frappe.log_error(title="Get Exchange Rate")
Nabin Hait7a9bd412017-05-24 16:18:27 +0530116 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 +0530117 return 0.0
Makarand Bauskard0109a62017-07-26 16:29:22 +0530118
119def enable_all_roles_and_domains():
120 """ enable all roles and domain for testing """
Makarand Bauskard0109a62017-07-26 16:29:22 +0530121 # add all roles to users
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530122 domains = frappe.get_all("Domain")
Makarand Bauskard0109a62017-07-26 16:29:22 +0530123 if not domains:
124 return
125
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530126 from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
127 frappe.get_single('Domain Settings').set_active_domains(\
128 [d.name for d in domains])
129 add_all_roles_to('Administrator')
Manas Solanki966f1412017-11-23 15:22:10 +0530130
131
132def insert_record(records):
133 for r in records:
134 doc = frappe.new_doc(r.get("doctype"))
135 doc.update(r)
136 try:
137 doc.insert(ignore_permissions=True)
Achilles Rasquinha2862e252018-01-30 13:47:18 +0530138 except frappe.DuplicateEntryError as e:
Manas Solanki966f1412017-11-23 15:22:10 +0530139 # pass DuplicateEntryError and continue
140 if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name:
141 # make sure DuplicateEntryError is for the exact same doc and not a related doc
142 pass
143 else:
144 raise
Neil Trini Lasrado771fbb82018-08-24 15:15:56 +0530145
146def welcome_email():
Chinmay D. Pai612e58d2020-03-05 10:08:43 +0530147 site_name = get_default_company() or "ERPNext"
Suraj Shetty48e9bc32020-01-29 15:06:18 +0530148 title = _("Welcome to {0}").format(site_name)
bcornwellmottb2638762019-03-05 16:44:02 -0800149 return title