Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 6 | from frappe import _ |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 7 | from frappe.utils import flt, add_days |
Nabin Hait | ad4f1c7 | 2016-12-09 12:14:47 +0530 | [diff] [blame] | 8 | from frappe.utils import get_datetime_str, nowdate |
Neil Trini Lasrado | 771fbb8 | 2018-08-24 15:15:56 +0530 | [diff] [blame] | 9 | from erpnext import get_default_company |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 10 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 11 | def get_root_of(doctype): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 12 | """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 Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 17 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 18 | def get_ancestors_of(doctype, name): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 19 | """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 Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 24 | |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 25 | def before_tests(): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 26 | 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({ |
Ankush Menat | 22cb642 | 2021-07-02 23:32:33 +0530 | [diff] [blame] | 31 | "currency" :"USD", |
| 32 | "full_name" :"Test User", |
| 33 | "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" :"2021-01-01", |
| 39 | "fy_end_date" :"2021-12-31", |
| 40 | "language" :"english", |
| 41 | "company_tagline" :"Testing", |
| 42 | "email" :"test@erpnext.com", |
| 43 | "password" :"test", |
Nabin Hait | 1749b7c | 2017-11-16 17:48:59 +0530 | [diff] [blame] | 44 | "chart_of_accounts" : "Standard", |
Ankush Menat | 22cb642 | 2021-07-02 23:32:33 +0530 | [diff] [blame] | 45 | "domains" : ["Manufacturing"], |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 46 | }) |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 47 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 48 | 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 Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 52 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 53 | frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0) |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 54 | enable_all_roles_and_domains() |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 55 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 56 | frappe.db.commit() |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 57 | |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 58 | @frappe.whitelist() |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 59 | def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=None): |
Nabin Hait | ad4f1c7 | 2016-12-09 12:14:47 +0530 | [diff] [blame] | 60 | if not (from_currency and to_currency): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 61 | # manqala 19/09/2016: Should this be an empty return or should it throw and exception? |
| 62 | return |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 63 | if from_currency == to_currency: |
| 64 | return 1 |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 65 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 66 | if not transaction_date: |
| 67 | transaction_date = nowdate() |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 68 | currency_settings = frappe.get_doc("Accounts Settings").as_dict() |
| 69 | allow_stale_rates = currency_settings.get("allow_stale") |
| 70 | |
| 71 | filters = [ |
| 72 | ["date", "<=", get_datetime_str(transaction_date)], |
| 73 | ["from_currency", "=", from_currency], |
| 74 | ["to_currency", "=", to_currency] |
| 75 | ] |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 76 | |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 77 | if args == "for_buying": |
| 78 | filters.append(["for_buying", "=", "1"]) |
| 79 | elif args == "for_selling": |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 80 | filters.append(["for_selling", "=", "1"]) |
| 81 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 82 | if not allow_stale_rates: |
| 83 | stale_days = currency_settings.get("stale_days") |
| 84 | checkpoint_date = add_days(transaction_date, -stale_days) |
| 85 | filters.append(["date", ">", get_datetime_str(checkpoint_date)]) |
| 86 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 87 | # cksgb 19/09/2016: get last entry in Currency Exchange with from_currency and to_currency. |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 88 | entries = frappe.get_all( |
| 89 | "Currency Exchange", fields=["exchange_rate"], filters=filters, order_by="date desc", |
| 90 | limit=1) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 91 | if entries: |
| 92 | return flt(entries[0].exchange_rate) |
Rushabh Mehta | 746fd90 | 2015-10-15 11:50:38 +0530 | [diff] [blame] | 93 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 94 | try: |
| 95 | cache = frappe.cache() |
Dany Robert | e9bc2f3 | 2021-06-28 19:01:52 +0530 | [diff] [blame] | 96 | key = "currency_exchange_rate_{0}:{1}:{2}".format(transaction_date, from_currency, to_currency) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 97 | value = cache.get(key) |
Rushabh Mehta | 9f436a7 | 2015-10-15 11:57:46 +0530 | [diff] [blame] | 98 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 99 | if not value: |
| 100 | import requests |
Dany Robert | e9bc2f3 | 2021-06-28 19:01:52 +0530 | [diff] [blame] | 101 | api_url = "https://api.exchangerate.host/convert" |
tundebabzy | 7f8a259 | 2017-08-31 10:00:15 +0100 | [diff] [blame] | 102 | response = requests.get(api_url, params={ |
Dany Robert | e9bc2f3 | 2021-06-28 19:01:52 +0530 | [diff] [blame] | 103 | "date": transaction_date, |
| 104 | "from": from_currency, |
| 105 | "to": to_currency |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 106 | }) |
| 107 | # expire in 6 hours |
| 108 | response.raise_for_status() |
Dany Robert | e9bc2f3 | 2021-06-28 19:01:52 +0530 | [diff] [blame] | 109 | value = response.json()["result"] |
Dany Robert | 909995a | 2021-07-29 15:58:27 +0530 | [diff] [blame] | 110 | cache.setex(name=key, time=21600, value=flt(value)) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 111 | return flt(value) |
| 112 | except: |
Faris Ansari | badf1c4 | 2019-01-24 16:28:53 +0530 | [diff] [blame] | 113 | frappe.log_error(title="Get Exchange Rate") |
Nabin Hait | 7a9bd41 | 2017-05-24 16:18:27 +0530 | [diff] [blame] | 114 | 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 Bauskar | ddd4845 | 2017-07-06 11:09:34 +0530 | [diff] [blame] | 115 | return 0.0 |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 116 | |
| 117 | def enable_all_roles_and_domains(): |
| 118 | """ enable all roles and domain for testing """ |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 119 | # add all roles to users |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 120 | domains = frappe.get_all("Domain") |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 121 | if not domains: |
| 122 | return |
| 123 | |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 124 | from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to |
| 125 | frappe.get_single('Domain Settings').set_active_domains(\ |
| 126 | [d.name for d in domains]) |
| 127 | add_all_roles_to('Administrator') |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def insert_record(records): |
| 131 | for r in records: |
| 132 | doc = frappe.new_doc(r.get("doctype")) |
| 133 | doc.update(r) |
| 134 | try: |
| 135 | doc.insert(ignore_permissions=True) |
Achilles Rasquinha | 2862e25 | 2018-01-30 13:47:18 +0530 | [diff] [blame] | 136 | except frappe.DuplicateEntryError as e: |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 137 | # pass DuplicateEntryError and continue |
| 138 | if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name: |
| 139 | # make sure DuplicateEntryError is for the exact same doc and not a related doc |
| 140 | pass |
| 141 | else: |
| 142 | raise |
Neil Trini Lasrado | 771fbb8 | 2018-08-24 15:15:56 +0530 | [diff] [blame] | 143 | |
| 144 | def welcome_email(): |
Chinmay D. Pai | 612e58d | 2020-03-05 10:08:43 +0530 | [diff] [blame] | 145 | site_name = get_default_company() or "ERPNext" |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 146 | title = _("Welcome to {0}").format(site_name) |
bcornwellmott | b263876 | 2019-03-05 16:44:02 -0800 | [diff] [blame] | 147 | return title |