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 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
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 _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | from frappe.utils import add_days, flt, get_datetime_str, nowdate |
| 8 | |
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 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 12 | def get_root_of(doctype): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 13 | """Get root element of a DocType with a tree structure""" |
| 14 | result = frappe.db.sql_list("""select name from `tab%s` |
| 15 | where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" % |
| 16 | (doctype, doctype)) |
| 17 | return result[0] if result else None |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 18 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 19 | def get_ancestors_of(doctype, name): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 20 | """Get ancestor elements of a DocType with a tree structure""" |
| 21 | lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"]) |
| 22 | result = frappe.db.sql_list("""select name from `tab%s` |
| 23 | where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt)) |
| 24 | return result or [] |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 25 | |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 26 | def before_tests(): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 27 | frappe.clear_cache() |
| 28 | # complete setup if missing |
| 29 | from frappe.desk.page.setup_wizard.setup_wizard import setup_complete |
| 30 | if not frappe.get_list("Company"): |
| 31 | setup_complete({ |
Ankush Menat | 22cb642 | 2021-07-02 23:32:33 +0530 | [diff] [blame] | 32 | "currency" :"USD", |
| 33 | "full_name" :"Test User", |
| 34 | "company_name" :"Wind Power LLC", |
| 35 | "timezone" :"America/New_York", |
| 36 | "company_abbr" :"WP", |
| 37 | "industry" :"Manufacturing", |
| 38 | "country" :"United States", |
| 39 | "fy_start_date" :"2021-01-01", |
| 40 | "fy_end_date" :"2021-12-31", |
| 41 | "language" :"english", |
| 42 | "company_tagline" :"Testing", |
| 43 | "email" :"test@erpnext.com", |
| 44 | "password" :"test", |
Nabin Hait | 1749b7c | 2017-11-16 17:48:59 +0530 | [diff] [blame] | 45 | "chart_of_accounts" : "Standard", |
Ankush Menat | 22cb642 | 2021-07-02 23:32:33 +0530 | [diff] [blame] | 46 | "domains" : ["Manufacturing"], |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 47 | }) |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 49 | frappe.db.sql("delete from `tabLeave Allocation`") |
| 50 | frappe.db.sql("delete from `tabLeave Application`") |
| 51 | frappe.db.sql("delete from `tabSalary Slip`") |
| 52 | frappe.db.sql("delete from `tabItem Price`") |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 53 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 54 | 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] | 55 | enable_all_roles_and_domains() |
Ankush Menat | a837523 | 2021-12-05 22:07:14 +0530 | [diff] [blame] | 56 | set_defaults_for_tests() |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 57 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 58 | frappe.db.commit() |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 59 | |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 60 | @frappe.whitelist() |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 61 | 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] | 62 | if not (from_currency and to_currency): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 63 | # manqala 19/09/2016: Should this be an empty return or should it throw and exception? |
| 64 | return |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 65 | if from_currency == to_currency: |
| 66 | return 1 |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 67 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 68 | if not transaction_date: |
| 69 | transaction_date = nowdate() |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 70 | 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 | ] |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 78 | |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 79 | if args == "for_buying": |
| 80 | filters.append(["for_buying", "=", "1"]) |
| 81 | elif args == "for_selling": |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 82 | filters.append(["for_selling", "=", "1"]) |
| 83 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 84 | 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 Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 89 | # 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] | 90 | entries = frappe.get_all( |
| 91 | "Currency Exchange", fields=["exchange_rate"], filters=filters, order_by="date desc", |
| 92 | limit=1) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 93 | if entries: |
| 94 | return flt(entries[0].exchange_rate) |
Rushabh Mehta | 746fd90 | 2015-10-15 11:50:38 +0530 | [diff] [blame] | 95 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 96 | try: |
| 97 | cache = frappe.cache() |
Dany Robert | e9bc2f3 | 2021-06-28 19:01:52 +0530 | [diff] [blame] | 98 | 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] | 99 | value = cache.get(key) |
Rushabh Mehta | 9f436a7 | 2015-10-15 11:57:46 +0530 | [diff] [blame] | 100 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 101 | if not value: |
| 102 | import requests |
Deepesh Garg | e2dab6f | 2022-01-10 17:31:38 +0530 | [diff] [blame] | 103 | settings = frappe.get_cached_doc('Currency Exchange Settings') |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 104 | req_params = { |
rtdany10 | e093863 | 2021-09-03 15:03:47 +0530 | [diff] [blame] | 105 | "transaction_date": transaction_date, |
| 106 | "from_currency": from_currency, |
| 107 | "to_currency": to_currency |
| 108 | } |
| 109 | params = {} |
| 110 | for row in settings.req_params: |
rtdany10 | 227466c | 2021-09-04 14:04:56 +0530 | [diff] [blame] | 111 | params[row.key] = format_ces_api(row.value, req_params) |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 112 | response = requests.get(format_ces_api(settings.api_endpoint, req_params), params=params) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 113 | # expire in 6 hours |
| 114 | response.raise_for_status() |
rtdany10 | e093863 | 2021-09-03 15:03:47 +0530 | [diff] [blame] | 115 | value = response.json() |
| 116 | for res_key in settings.result_key: |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 117 | value = value[format_ces_api(str(res_key.key), req_params)] |
Dany Robert | 909995a | 2021-07-29 15:58:27 +0530 | [diff] [blame] | 118 | cache.setex(name=key, time=21600, value=flt(value)) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 119 | return flt(value) |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 120 | except Exception: |
Faris Ansari | badf1c4 | 2019-01-24 16:28:53 +0530 | [diff] [blame] | 121 | frappe.log_error(title="Get Exchange Rate") |
Nabin Hait | 7a9bd41 | 2017-05-24 16:18:27 +0530 | [diff] [blame] | 122 | 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] | 123 | return 0.0 |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 124 | |
Dany Robert | 1f8be84 | 2021-11-04 05:12:09 +0000 | [diff] [blame] | 125 | def format_ces_api(data, param): |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 126 | return data.format( |
Dany Robert | 1f8be84 | 2021-11-04 05:12:09 +0000 | [diff] [blame] | 127 | transaction_date=param.get("transaction_date"), |
| 128 | to_currency=param.get("to_currency"), |
| 129 | from_currency=param.get("from_currency") |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 130 | ) |
| 131 | |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 132 | def enable_all_roles_and_domains(): |
| 133 | """ enable all roles and domain for testing """ |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 134 | # add all roles to users |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 135 | domains = frappe.get_all("Domain") |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 136 | if not domains: |
| 137 | return |
| 138 | |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 139 | from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to |
| 140 | frappe.get_single('Domain Settings').set_active_domains(\ |
| 141 | [d.name for d in domains]) |
| 142 | add_all_roles_to('Administrator') |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 143 | |
Ankush Menat | a837523 | 2021-12-05 22:07:14 +0530 | [diff] [blame] | 144 | def set_defaults_for_tests(): |
| 145 | from frappe.utils.nestedset import get_root_of |
| 146 | |
| 147 | selling_settings = frappe.get_single("Selling Settings") |
| 148 | selling_settings.customer_group = get_root_of("Customer Group") |
| 149 | selling_settings.territory = get_root_of("Territory") |
| 150 | selling_settings.save() |
| 151 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 152 | |
| 153 | def insert_record(records): |
| 154 | for r in records: |
| 155 | doc = frappe.new_doc(r.get("doctype")) |
| 156 | doc.update(r) |
| 157 | try: |
| 158 | doc.insert(ignore_permissions=True) |
Achilles Rasquinha | 2862e25 | 2018-01-30 13:47:18 +0530 | [diff] [blame] | 159 | except frappe.DuplicateEntryError as e: |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 160 | # pass DuplicateEntryError and continue |
| 161 | if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name: |
| 162 | # make sure DuplicateEntryError is for the exact same doc and not a related doc |
| 163 | pass |
| 164 | else: |
| 165 | raise |
Neil Trini Lasrado | 771fbb8 | 2018-08-24 15:15:56 +0530 | [diff] [blame] | 166 | |
| 167 | def welcome_email(): |
Chinmay D. Pai | 612e58d | 2020-03-05 10:08:43 +0530 | [diff] [blame] | 168 | site_name = get_default_company() or "ERPNext" |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 169 | title = _("Welcome to {0}").format(site_name) |
bcornwellmott | b263876 | 2019-03-05 16:44:02 -0800 | [diff] [blame] | 170 | return title |