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 |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 9 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 10 | def get_root_of(doctype): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 11 | """Get root element of a DocType with a tree structure""" |
| 12 | result = frappe.db.sql_list("""select name from `tab%s` |
| 13 | where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" % |
| 14 | (doctype, doctype)) |
| 15 | return result[0] if result else None |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 16 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 17 | def get_ancestors_of(doctype, name): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 18 | """Get ancestor elements of a DocType with a tree structure""" |
| 19 | lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"]) |
| 20 | result = frappe.db.sql_list("""select name from `tab%s` |
| 21 | where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt)) |
| 22 | return result or [] |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 23 | |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 24 | def before_tests(): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 25 | frappe.clear_cache() |
| 26 | # complete setup if missing |
| 27 | from frappe.desk.page.setup_wizard.setup_wizard import setup_complete |
| 28 | if not frappe.get_list("Company"): |
| 29 | setup_complete({ |
| 30 | "currency" :"USD", |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 31 | "full_name" :"Test User", |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 32 | "company_name" :"Wind Power LLC", |
| 33 | "timezone" :"America/New_York", |
| 34 | "company_abbr" :"WP", |
| 35 | "industry" :"Manufacturing", |
| 36 | "country" :"United States", |
| 37 | "fy_start_date" :"2011-01-01", |
| 38 | "fy_end_date" :"2011-12-31", |
| 39 | "language" :"english", |
| 40 | "company_tagline" :"Testing", |
| 41 | "email" :"test@erpnext.com", |
| 42 | "password" :"test", |
Nabin Hait | 1749b7c | 2017-11-16 17:48:59 +0530 | [diff] [blame] | 43 | "chart_of_accounts" : "Standard", |
Zlash65 | 5fc8923 | 2017-12-06 17:04:03 +0530 | [diff] [blame] | 44 | "domains" : ["Manufacturing"], |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 45 | }) |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 46 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 47 | frappe.db.sql("delete from `tabLeave Allocation`") |
| 48 | frappe.db.sql("delete from `tabLeave Application`") |
| 49 | frappe.db.sql("delete from `tabSalary Slip`") |
| 50 | frappe.db.sql("delete from `tabItem Price`") |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 51 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 52 | 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] | 53 | enable_all_roles_and_domains() |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 54 | |
rohitwaghchaure | fb997d6 | 2018-06-14 09:13:39 +0530 | [diff] [blame^] | 55 | if not frappe.db.exists('Company', 'Woocommerce'): |
| 56 | company = frappe.new_doc("Company") |
| 57 | company.company_name = "Woocommerce" |
| 58 | company.abbr = "W" |
| 59 | company.default_currency = "INR" |
| 60 | company.save() |
| 61 | |
| 62 | woo_settings = frappe.get_doc("Woocommerce Settings") |
| 63 | if not woo_settings.secret: |
| 64 | woo_settings.secret = "ec434676aa1de0e502389f515c38f89f653119ab35e9117c7a79e576" |
| 65 | woo_settings.woocommerce_server_url = "https://woocommerce.mntechnique.com/" |
| 66 | woo_settings.api_consumer_key = "ck_fd43ff5756a6abafd95fadb6677100ce95a758a1" |
| 67 | woo_settings.api_consumer_secret = "cs_94360a1ad7bef7fa420a40cf284f7b3e0788454e" |
| 68 | woo_settings.enable_sync = 1 |
| 69 | woo_settings.tax_account = "Sales Expenses - W" |
| 70 | woo_settings.f_n_f_account = "Expenses - W" |
| 71 | woo_settings.save(ignore_permissions=True) |
| 72 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 73 | frappe.db.commit() |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 74 | |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 75 | @frappe.whitelist() |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 76 | 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] | 77 | if not (from_currency and to_currency): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 78 | # manqala 19/09/2016: Should this be an empty return or should it throw and exception? |
| 79 | return |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 80 | if from_currency == to_currency: |
| 81 | return 1 |
Rushabh Mehta | cf99dc0 | 2017-02-09 18:06:11 +0530 | [diff] [blame] | 82 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 83 | if not transaction_date: |
| 84 | transaction_date = nowdate() |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 85 | currency_settings = frappe.get_doc("Accounts Settings").as_dict() |
| 86 | allow_stale_rates = currency_settings.get("allow_stale") |
| 87 | |
| 88 | filters = [ |
| 89 | ["date", "<=", get_datetime_str(transaction_date)], |
| 90 | ["from_currency", "=", from_currency], |
| 91 | ["to_currency", "=", to_currency] |
| 92 | ] |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 93 | |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 94 | if args == "for_buying": |
| 95 | filters.append(["for_buying", "=", "1"]) |
| 96 | elif args == "for_selling": |
Shreya | b547cdd | 2018-05-15 16:58:45 +0530 | [diff] [blame] | 97 | filters.append(["for_selling", "=", "1"]) |
| 98 | |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 99 | if not allow_stale_rates: |
| 100 | stale_days = currency_settings.get("stale_days") |
| 101 | checkpoint_date = add_days(transaction_date, -stale_days) |
| 102 | filters.append(["date", ">", get_datetime_str(checkpoint_date)]) |
| 103 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 104 | # 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] | 105 | entries = frappe.get_all( |
| 106 | "Currency Exchange", fields=["exchange_rate"], filters=filters, order_by="date desc", |
| 107 | limit=1) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 108 | if entries: |
| 109 | return flt(entries[0].exchange_rate) |
Rushabh Mehta | 746fd90 | 2015-10-15 11:50:38 +0530 | [diff] [blame] | 110 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 111 | try: |
| 112 | cache = frappe.cache() |
| 113 | key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency) |
| 114 | value = cache.get(key) |
Rushabh Mehta | 9f436a7 | 2015-10-15 11:57:46 +0530 | [diff] [blame] | 115 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 116 | if not value: |
| 117 | import requests |
Shreya Shah | 630721a | 2018-06-05 12:56:10 +0530 | [diff] [blame] | 118 | api_url = "https://exchangeratesapi.io/api/{0}".format(transaction_date) |
tundebabzy | 7f8a259 | 2017-08-31 10:00:15 +0100 | [diff] [blame] | 119 | response = requests.get(api_url, params={ |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 120 | "base": from_currency, |
| 121 | "symbols": to_currency |
| 122 | }) |
| 123 | # expire in 6 hours |
| 124 | response.raise_for_status() |
| 125 | value = response.json()["rates"][to_currency] |
| 126 | cache.setex(key, value, 6 * 60 * 60) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 127 | return flt(value) |
| 128 | except: |
Nabin Hait | 7a9bd41 | 2017-05-24 16:18:27 +0530 | [diff] [blame] | 129 | 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] | 130 | return 0.0 |
Makarand Bauskar | d0109a6 | 2017-07-26 16:29:22 +0530 | [diff] [blame] | 131 | |
| 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 | |
| 144 | |
| 145 | def insert_record(records): |
| 146 | for r in records: |
| 147 | doc = frappe.new_doc(r.get("doctype")) |
| 148 | doc.update(r) |
| 149 | try: |
| 150 | doc.insert(ignore_permissions=True) |
Achilles Rasquinha | 2862e25 | 2018-01-30 13:47:18 +0530 | [diff] [blame] | 151 | except frappe.DuplicateEntryError as e: |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 152 | # pass DuplicateEntryError and continue |
| 153 | if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name: |
| 154 | # make sure DuplicateEntryError is for the exact same doc and not a related doc |
| 155 | pass |
| 156 | else: |
| 157 | raise |