blob: bbf122e8cfff69637cf1a990d830a9be864ff96b [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 _
tundebabzyab5b0302017-09-21 10:20:39 +01007from frappe.utils import flt, add_days
Nabin Haitad4f1c72016-12-09 12:14:47 +05308from frappe.utils import get_datetime_str, nowdate
Neil Trini Lasrado771fbb82018-08-24 15:15:56 +05309from erpnext import get_default_company
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053010
Anand Doshi61a2f682013-06-21 17:55:31 +053011def get_root_of(doctype):
Nabin Hait87d70272016-12-08 14:43:11 +053012 """Get root element of a DocType with a tree structure"""
13 result = frappe.db.sql_list("""select name from `tab%s`
14 where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" %
15 (doctype, doctype))
16 return result[0] if result else None
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053017
Anand Doshi61a2f682013-06-21 17:55:31 +053018def get_ancestors_of(doctype, name):
Nabin Hait87d70272016-12-08 14:43:11 +053019 """Get ancestor elements of a DocType with a tree structure"""
20 lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"])
21 result = frappe.db.sql_list("""select name from `tab%s`
22 where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt))
23 return result or []
Anand Doshi61a2f682013-06-21 17:55:31 +053024
Rushabh Mehtaa1516472014-04-30 19:38:28 +053025def before_tests():
Nabin Hait87d70272016-12-08 14:43:11 +053026 frappe.clear_cache()
27 # complete setup if missing
28 from frappe.desk.page.setup_wizard.setup_wizard import setup_complete
29 if not frappe.get_list("Company"):
30 setup_complete({
31 "currency" :"USD",
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053032 "full_name" :"Test User",
Nabin Hait87d70272016-12-08 14:43:11 +053033 "company_name" :"Wind Power LLC",
34 "timezone" :"America/New_York",
35 "company_abbr" :"WP",
36 "industry" :"Manufacturing",
37 "country" :"United States",
38 "fy_start_date" :"2011-01-01",
39 "fy_end_date" :"2011-12-31",
40 "language" :"english",
41 "company_tagline" :"Testing",
42 "email" :"test@erpnext.com",
43 "password" :"test",
Nabin Hait1749b7c2017-11-16 17:48:59 +053044 "chart_of_accounts" : "Standard",
Zlash655fc89232017-12-06 17:04:03 +053045 "domains" : ["Manufacturing"],
Nabin Hait87d70272016-12-08 14:43:11 +053046 })
Rushabh Mehtaa1516472014-04-30 19:38:28 +053047
Nabin Hait87d70272016-12-08 14:43:11 +053048 frappe.db.sql("delete from `tabLeave Allocation`")
49 frappe.db.sql("delete from `tabLeave Application`")
50 frappe.db.sql("delete from `tabSalary Slip`")
51 frappe.db.sql("delete from `tabItem Price`")
Rushabh Mehta5bd39422015-08-03 15:09:48 +053052
Nabin Hait87d70272016-12-08 14:43:11 +053053 frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0)
Makarand Bauskard0109a62017-07-26 16:29:22 +053054 enable_all_roles_and_domains()
Rushabh Mehta5bd39422015-08-03 15:09:48 +053055
rohitwaghchaurefb997d62018-06-14 09:13:39 +053056 if not frappe.db.exists('Company', 'Woocommerce'):
57 company = frappe.new_doc("Company")
58 company.company_name = "Woocommerce"
59 company.abbr = "W"
60 company.default_currency = "INR"
61 company.save()
62
63 woo_settings = frappe.get_doc("Woocommerce Settings")
64 if not woo_settings.secret:
65 woo_settings.secret = "ec434676aa1de0e502389f515c38f89f653119ab35e9117c7a79e576"
66 woo_settings.woocommerce_server_url = "https://woocommerce.mntechnique.com/"
67 woo_settings.api_consumer_key = "ck_fd43ff5756a6abafd95fadb6677100ce95a758a1"
68 woo_settings.api_consumer_secret = "cs_94360a1ad7bef7fa420a40cf284f7b3e0788454e"
69 woo_settings.enable_sync = 1
70 woo_settings.tax_account = "Sales Expenses - W"
71 woo_settings.f_n_f_account = "Expenses - W"
72 woo_settings.save(ignore_permissions=True)
73
Nabin Hait87d70272016-12-08 14:43:11 +053074 frappe.db.commit()
Anand Doshidffec8f2014-07-01 17:45:15 +053075
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +053076@frappe.whitelist()
Shreya0db85062018-05-15 11:25:46 +053077def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=None):
Nabin Haitad4f1c72016-12-09 12:14:47 +053078 if not (from_currency and to_currency):
Nabin Hait87d70272016-12-08 14:43:11 +053079 # manqala 19/09/2016: Should this be an empty return or should it throw and exception?
80 return
Nabin Hait87d70272016-12-08 14:43:11 +053081 if from_currency == to_currency:
82 return 1
Rushabh Mehtacf99dc02017-02-09 18:06:11 +053083
tundebabzyab5b0302017-09-21 10:20:39 +010084 if not transaction_date:
85 transaction_date = nowdate()
tundebabzyab5b0302017-09-21 10:20:39 +010086 currency_settings = frappe.get_doc("Accounts Settings").as_dict()
87 allow_stale_rates = currency_settings.get("allow_stale")
88
89 filters = [
90 ["date", "<=", get_datetime_str(transaction_date)],
91 ["from_currency", "=", from_currency],
92 ["to_currency", "=", to_currency]
93 ]
Shreyab547cdd2018-05-15 16:58:45 +053094
Shreya0db85062018-05-15 11:25:46 +053095 if args == "for_buying":
96 filters.append(["for_buying", "=", "1"])
97 elif args == "for_selling":
Shreyab547cdd2018-05-15 16:58:45 +053098 filters.append(["for_selling", "=", "1"])
99
tundebabzyab5b0302017-09-21 10:20:39 +0100100 if not allow_stale_rates:
101 stale_days = currency_settings.get("stale_days")
102 checkpoint_date = add_days(transaction_date, -stale_days)
103 filters.append(["date", ">", get_datetime_str(checkpoint_date)])
104
Nabin Hait288a18e2016-12-08 15:36:23 +0530105 # cksgb 19/09/2016: get last entry in Currency Exchange with from_currency and to_currency.
tundebabzyab5b0302017-09-21 10:20:39 +0100106 entries = frappe.get_all(
107 "Currency Exchange", fields=["exchange_rate"], filters=filters, order_by="date desc",
108 limit=1)
Nabin Hait87d70272016-12-08 14:43:11 +0530109 if entries:
110 return flt(entries[0].exchange_rate)
Rushabh Mehta746fd902015-10-15 11:50:38 +0530111
Nabin Hait87d70272016-12-08 14:43:11 +0530112 try:
113 cache = frappe.cache()
114 key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
115 value = cache.get(key)
Rushabh Mehta9f436a72015-10-15 11:57:46 +0530116
Nabin Hait87d70272016-12-08 14:43:11 +0530117 if not value:
118 import requests
Ameya Shenoy5c3b6942018-07-13 12:46:07 +0530119 api_url = "https://frankfurter.erpnext.org/{0}".format(transaction_date)
tundebabzy7f8a2592017-08-31 10:00:15 +0100120 response = requests.get(api_url, params={
Nabin Hait87d70272016-12-08 14:43:11 +0530121 "base": from_currency,
122 "symbols": to_currency
123 })
124 # expire in 6 hours
125 response.raise_for_status()
126 value = response.json()["rates"][to_currency]
127 cache.setex(key, value, 6 * 60 * 60)
Nabin Hait87d70272016-12-08 14:43:11 +0530128 return flt(value)
129 except:
Nabin Hait7a9bd412017-05-24 16:18:27 +0530130 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 +0530131 return 0.0
Makarand Bauskard0109a62017-07-26 16:29:22 +0530132
133def enable_all_roles_and_domains():
134 """ enable all roles and domain for testing """
Makarand Bauskard0109a62017-07-26 16:29:22 +0530135 # add all roles to users
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530136 domains = frappe.get_all("Domain")
Makarand Bauskard0109a62017-07-26 16:29:22 +0530137 if not domains:
138 return
139
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530140 from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
141 frappe.get_single('Domain Settings').set_active_domains(\
142 [d.name for d in domains])
143 add_all_roles_to('Administrator')
Manas Solanki966f1412017-11-23 15:22:10 +0530144
145
146def insert_record(records):
147 for r in records:
148 doc = frappe.new_doc(r.get("doctype"))
149 doc.update(r)
150 try:
151 doc.insert(ignore_permissions=True)
Achilles Rasquinha2862e252018-01-30 13:47:18 +0530152 except frappe.DuplicateEntryError as e:
Manas Solanki966f1412017-11-23 15:22:10 +0530153 # pass DuplicateEntryError and continue
154 if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name:
155 # make sure DuplicateEntryError is for the exact same doc and not a related doc
156 pass
157 else:
158 raise
Neil Trini Lasrado771fbb82018-08-24 15:15:56 +0530159
160def welcome_email():
161 site_name = get_default_company()
162 title = _("Welcome to {0}".format(site_name))
163 return title