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 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 4 | import frappe |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 5 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | from frappe.utils import add_days, flt, get_datetime_str, nowdate |
Ankush Menat | 51873da | 2022-03-22 11:13:15 +0530 | [diff] [blame] | 7 | from frappe.utils.data import now_datetime |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 8 | from frappe.utils.nestedset import get_root_of |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Neil Trini Lasrado | 771fbb8 | 2018-08-24 15:15:56 +0530 | [diff] [blame] | 10 | from erpnext import get_default_company |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 11 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 12 | |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 13 | def before_tests(): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 14 | frappe.clear_cache() |
| 15 | # complete setup if missing |
| 16 | from frappe.desk.page.setup_wizard.setup_wizard import setup_complete |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 17 | |
| 18 | if not frappe.db.a_row_exists("Company"): |
| 19 | current_year = now_datetime().year |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 20 | setup_complete( |
| 21 | { |
| 22 | "currency": "USD", |
| 23 | "full_name": "Test User", |
| 24 | "company_name": "Wind Power LLC", |
| 25 | "timezone": "America/New_York", |
| 26 | "company_abbr": "WP", |
| 27 | "industry": "Manufacturing", |
| 28 | "country": "United States", |
| 29 | "fy_start_date": f"{current_year}-01-01", |
| 30 | "fy_end_date": f"{current_year}-12-31", |
| 31 | "language": "english", |
| 32 | "company_tagline": "Testing", |
| 33 | "email": "test@erpnext.com", |
| 34 | "password": "test", |
| 35 | "chart_of_accounts": "Standard", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 36 | } |
| 37 | ) |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 38 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 39 | frappe.db.sql("delete from `tabItem Price`") |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 40 | |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 41 | _enable_all_roles_for_admin() |
| 42 | |
Ankush Menat | a837523 | 2021-12-05 22:07:14 +0530 | [diff] [blame] | 43 | set_defaults_for_tests() |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 44 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 45 | frappe.db.commit() |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 46 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 47 | |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 48 | @frappe.whitelist() |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 49 | 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] | 50 | if not (from_currency and to_currency): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 51 | # manqala 19/09/2016: Should this be an empty return or should it throw and exception? |
| 52 | return |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 53 | if from_currency == to_currency: |
| 54 | return 1 |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 55 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 56 | if not transaction_date: |
| 57 | transaction_date = nowdate() |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 58 | currency_settings = frappe.get_doc("Accounts Settings").as_dict() |
| 59 | allow_stale_rates = currency_settings.get("allow_stale") |
| 60 | |
| 61 | filters = [ |
| 62 | ["date", "<=", get_datetime_str(transaction_date)], |
| 63 | ["from_currency", "=", from_currency], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 64 | ["to_currency", "=", to_currency], |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 65 | ] |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 66 | |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 67 | if args == "for_buying": |
| 68 | filters.append(["for_buying", "=", "1"]) |
| 69 | elif args == "for_selling": |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 70 | filters.append(["for_selling", "=", "1"]) |
| 71 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 72 | if not allow_stale_rates: |
| 73 | stale_days = currency_settings.get("stale_days") |
| 74 | checkpoint_date = add_days(transaction_date, -stale_days) |
| 75 | filters.append(["date", ">", get_datetime_str(checkpoint_date)]) |
| 76 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 77 | # 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] | 78 | entries = frappe.get_all( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 79 | "Currency Exchange", fields=["exchange_rate"], filters=filters, order_by="date desc", limit=1 |
| 80 | ) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 81 | if entries: |
| 82 | return flt(entries[0].exchange_rate) |
Rushabh Mehta | 746fd90 | 2015-10-15 11:50:38 +0530 | [diff] [blame] | 83 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 84 | if frappe.get_cached_value("Currency Exchange Settings", "Currency Exchange Settings", "disabled"): |
Devin Slauenwhite | 179a31e | 2023-01-16 10:30:10 -0500 | [diff] [blame] | 85 | return 0.00 |
| 86 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 87 | try: |
| 88 | cache = frappe.cache() |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 89 | key = f"currency_exchange_rate_{transaction_date}:{from_currency}:{to_currency}" |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 90 | value = cache.get(key) |
Rushabh Mehta | 9f436a7 | 2015-10-15 11:57:46 +0530 | [diff] [blame] | 91 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 92 | if not value: |
| 93 | import requests |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 94 | |
| 95 | settings = frappe.get_cached_doc("Currency Exchange Settings") |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 96 | req_params = { |
rtdany10 | e093863 | 2021-09-03 15:03:47 +0530 | [diff] [blame] | 97 | "transaction_date": transaction_date, |
| 98 | "from_currency": from_currency, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 99 | "to_currency": to_currency, |
rtdany10 | e093863 | 2021-09-03 15:03:47 +0530 | [diff] [blame] | 100 | } |
| 101 | params = {} |
| 102 | for row in settings.req_params: |
rtdany10 | 227466c | 2021-09-04 14:04:56 +0530 | [diff] [blame] | 103 | params[row.key] = format_ces_api(row.value, req_params) |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 104 | 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] | 105 | # expire in 6 hours |
| 106 | response.raise_for_status() |
rtdany10 | e093863 | 2021-09-03 15:03:47 +0530 | [diff] [blame] | 107 | value = response.json() |
| 108 | for res_key in settings.result_key: |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 109 | value = value[format_ces_api(str(res_key.key), req_params)] |
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) |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 112 | except Exception: |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 113 | frappe.log_error("Unable to fetch exchange rate") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | frappe.msgprint( |
| 115 | _( |
| 116 | "Unable to find exchange rate for {0} to {1} for key date {2}. Please create a Currency Exchange record manually" |
| 117 | ).format(from_currency, to_currency, transaction_date) |
| 118 | ) |
Makarand Bauskar | ddd4845 | 2017-07-06 11:09:34 +0530 | [diff] [blame] | 119 | return 0.0 |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 120 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 121 | |
Dany Robert | 1f8be84 | 2021-11-04 05:12:09 +0000 | [diff] [blame] | 122 | def format_ces_api(data, param): |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 123 | return data.format( |
Dany Robert | 1f8be84 | 2021-11-04 05:12:09 +0000 | [diff] [blame] | 124 | transaction_date=param.get("transaction_date"), |
| 125 | to_currency=param.get("to_currency"), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 126 | from_currency=param.get("from_currency"), |
rtdany10 | a86d9c9 | 2021-09-03 19:21:06 +0530 | [diff] [blame] | 127 | ) |
| 128 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 129 | |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 130 | def enable_all_roles_and_domains(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 131 | """enable all roles and domain for testing""" |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 132 | _enable_all_roles_for_admin() |
| 133 | |
| 134 | |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 135 | def _enable_all_roles_for_admin(): |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 136 | from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 137 | |
| 138 | all_roles = set(frappe.db.get_values("Role", pluck="name")) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 139 | admin_roles = set( |
| 140 | frappe.db.get_values("Has Role", {"parent": "Administrator"}, fieldname="role", pluck="role") |
| 141 | ) |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 142 | |
| 143 | if all_roles.difference(admin_roles): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 144 | add_all_roles_to("Administrator") |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 145 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 146 | |
Ankush Menat | a837523 | 2021-12-05 22:07:14 +0530 | [diff] [blame] | 147 | def set_defaults_for_tests(): |
Ankush Menat | 9daea6f | 2022-03-25 22:59:20 +0530 | [diff] [blame] | 148 | defaults = { |
| 149 | "customer_group": get_root_of("Customer Group"), |
| 150 | "territory": get_root_of("Territory"), |
| 151 | } |
| 152 | frappe.db.set_single_value("Selling Settings", defaults) |
| 153 | for key, value in defaults.items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 154 | frappe.db.set_default(key, value) |
Ankush Menat | 51873da | 2022-03-22 11:13:15 +0530 | [diff] [blame] | 155 | frappe.db.set_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing", 0) |
| 156 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 157 | |
| 158 | def insert_record(records): |
Ankush Menat | e401ad3 | 2022-03-26 12:53:39 +0530 | [diff] [blame] | 159 | from frappe.desk.page.setup_wizard.setup_wizard import make_records |
| 160 | |
| 161 | make_records(records) |
Neil Trini Lasrado | 771fbb8 | 2018-08-24 15:15:56 +0530 | [diff] [blame] | 162 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 163 | |
Neil Trini Lasrado | 771fbb8 | 2018-08-24 15:15:56 +0530 | [diff] [blame] | 164 | def welcome_email(): |
Chinmay D. Pai | 612e58d | 2020-03-05 10:08:43 +0530 | [diff] [blame] | 165 | site_name = get_default_company() or "ERPNext" |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 166 | title = _("Welcome to {0}").format(site_name) |
bcornwellmott | b263876 | 2019-03-05 16:44:02 -0800 | [diff] [blame] | 167 | return title |