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 | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 6 | from frappe import _, throw |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 7 | from frappe.utils import flt |
Nabin Hait | ad4f1c7 | 2016-12-09 12:14:47 +0530 | [diff] [blame] | 8 | from frappe.utils import get_datetime_str, nowdate |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 9 | |
Anand Doshi | 9d9aec1 | 2013-01-17 15:44:57 +0530 | [diff] [blame] | 10 | def get_company_currency(company): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 11 | currency = frappe.db.get_value("Company", company, "default_currency", cache=True) |
| 12 | if not currency: |
| 13 | currency = frappe.db.get_default("currency") |
| 14 | if not currency: |
| 15 | throw(_('Please specify Default Currency in Company Master and Global Defaults')) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 16 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 17 | return currency |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 18 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 19 | def get_root_of(doctype): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 20 | """Get root element of a DocType with a tree structure""" |
| 21 | result = frappe.db.sql_list("""select name from `tab%s` |
| 22 | where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" % |
| 23 | (doctype, doctype)) |
| 24 | return result[0] if result else None |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 25 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 26 | def get_ancestors_of(doctype, name): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 27 | """Get ancestor elements of a DocType with a tree structure""" |
| 28 | lft, rgt = frappe.db.get_value(doctype, name, ["lft", "rgt"]) |
| 29 | result = frappe.db.sql_list("""select name from `tab%s` |
| 30 | where lft<%s and rgt>%s order by lft desc""" % (doctype, "%s", "%s"), (lft, rgt)) |
| 31 | return result or [] |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 32 | |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 33 | def before_tests(): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 34 | frappe.clear_cache() |
| 35 | # complete setup if missing |
| 36 | from frappe.desk.page.setup_wizard.setup_wizard import setup_complete |
| 37 | if not frappe.get_list("Company"): |
| 38 | setup_complete({ |
| 39 | "currency" :"USD", |
| 40 | "first_name" :"Test", |
| 41 | "last_name" :"User", |
| 42 | "company_name" :"Wind Power LLC", |
| 43 | "timezone" :"America/New_York", |
| 44 | "company_abbr" :"WP", |
| 45 | "industry" :"Manufacturing", |
| 46 | "country" :"United States", |
| 47 | "fy_start_date" :"2011-01-01", |
| 48 | "fy_end_date" :"2011-12-31", |
| 49 | "language" :"english", |
| 50 | "company_tagline" :"Testing", |
| 51 | "email" :"test@erpnext.com", |
| 52 | "password" :"test", |
| 53 | "chart_of_accounts" : "Standard", |
| 54 | "domain" : "Manufacturing", |
| 55 | |
| 56 | }) |
Rushabh Mehta | a151647 | 2014-04-30 19:38:28 +0530 | [diff] [blame] | 57 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 58 | frappe.db.sql("delete from `tabLeave Allocation`") |
| 59 | frappe.db.sql("delete from `tabLeave Application`") |
| 60 | frappe.db.sql("delete from `tabSalary Slip`") |
| 61 | frappe.db.sql("delete from `tabItem Price`") |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 62 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 63 | frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0) |
Rushabh Mehta | 5bd3942 | 2015-08-03 15:09:48 +0530 | [diff] [blame] | 64 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 65 | frappe.db.commit() |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 66 | |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 67 | @frappe.whitelist() |
Nabin Hait | ad4f1c7 | 2016-12-09 12:14:47 +0530 | [diff] [blame] | 68 | def get_exchange_rate(from_currency, to_currency, transaction_date=None): |
| 69 | if not transaction_date: |
| 70 | transaction_date = nowdate() |
| 71 | if not (from_currency and to_currency): |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 72 | # manqala 19/09/2016: Should this be an empty return or should it throw and exception? |
| 73 | return |
| 74 | |
| 75 | if from_currency == to_currency: |
| 76 | return 1 |
| 77 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 78 | # cksgb 19/09/2016: get last entry in Currency Exchange with from_currency and to_currency. |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 79 | entries = frappe.get_all("Currency Exchange", fields = ["exchange_rate"], |
| 80 | filters=[ |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 81 | ["date", "<=", get_datetime_str(transaction_date)], |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 82 | ["from_currency", "=", from_currency], |
| 83 | ["to_currency", "=", to_currency] |
| 84 | ], order_by="date desc", limit=1) |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 85 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 86 | if entries: |
| 87 | return flt(entries[0].exchange_rate) |
Rushabh Mehta | 746fd90 | 2015-10-15 11:50:38 +0530 | [diff] [blame] | 88 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 89 | try: |
| 90 | cache = frappe.cache() |
| 91 | key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency) |
| 92 | value = cache.get(key) |
Rushabh Mehta | 9f436a7 | 2015-10-15 11:57:46 +0530 | [diff] [blame] | 93 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 94 | if not value: |
| 95 | import requests |
| 96 | response = requests.get("http://api.fixer.io/latest", params={ |
| 97 | "base": from_currency, |
| 98 | "symbols": to_currency |
| 99 | }) |
| 100 | # expire in 6 hours |
| 101 | response.raise_for_status() |
| 102 | value = response.json()["rates"][to_currency] |
| 103 | cache.setex(key, value, 6 * 60 * 60) |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 104 | |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 105 | return flt(value) |
| 106 | except: |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 107 | frappe.msgprint(_("Unable to find exchange rate for {0} to {1} for key date {2}").format(from_currency, to_currency, transaction_date)) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 108 | return 0.0 |