Anand Doshi | 227b238 | 2016-06-09 16:29:12 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | from __future__ import unicode_literals |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 3 | import inspect |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 4 | import frappe |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 5 | from erpnext.hooks import regional_overrides |
Vishal | b4d73cb | 2017-09-19 15:18:33 +0530 | [diff] [blame] | 6 | from frappe.utils import getdate |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 7 | |
Nabin Hait | 1ff9e04 | 2018-04-06 15:36:09 +0600 | [diff] [blame] | 8 | __version__ = '10.1.20' |
Anand Doshi | 227b238 | 2016-06-09 16:29:12 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 10 | def get_default_company(user=None): |
| 11 | '''Get default company for user''' |
| 12 | from frappe.defaults import get_user_default_as_list |
| 13 | |
| 14 | if not user: |
| 15 | user = frappe.session.user |
| 16 | |
| 17 | companies = get_user_default_as_list(user, 'company') |
| 18 | if companies: |
| 19 | default_company = companies[0] |
| 20 | else: |
| 21 | default_company = frappe.db.get_single_value('Global Defaults', 'default_company') |
| 22 | |
| 23 | return default_company |
| 24 | |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 25 | |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 26 | def get_default_currency(): |
| 27 | '''Returns the currency of the default company''' |
| 28 | company = get_default_company() |
| 29 | if company: |
Anand Doshi | 227b238 | 2016-06-09 16:29:12 +0530 | [diff] [blame] | 30 | return frappe.db.get_value('Company', company, 'default_currency') |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 31 | |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 32 | def get_default_cost_center(company): |
| 33 | '''Returns the default cost center of the company''' |
| 34 | if not company: |
| 35 | return None |
| 36 | |
| 37 | if not frappe.flags.company_cost_center: |
| 38 | frappe.flags.company_cost_center = {} |
| 39 | if not company in frappe.flags.company_cost_center: |
| 40 | frappe.flags.company_cost_center[company] = frappe.db.get_value('Company', company, 'cost_center') |
| 41 | return frappe.flags.company_cost_center[company] |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 42 | |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 43 | def get_company_currency(company): |
| 44 | '''Returns the default company currency''' |
| 45 | if not frappe.flags.company_currency: |
| 46 | frappe.flags.company_currency = {} |
| 47 | if not company in frappe.flags.company_currency: |
| 48 | frappe.flags.company_currency[company] = frappe.db.get_value('Company', company, 'default_currency') |
| 49 | return frappe.flags.company_currency[company] |
| 50 | |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 51 | def set_perpetual_inventory(enable=1, company=None): |
| 52 | if not company: |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 53 | company = "_Test Company" if frappe.flags.in_test else get_default_company() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 54 | |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 55 | company = frappe.get_doc("Company", company) |
| 56 | company.enable_perpetual_inventory = enable |
| 57 | company.save() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 58 | |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 59 | def encode_company_abbr(name, company): |
| 60 | '''Returns name encoded with company abbreviation''' |
| 61 | company_abbr = frappe.db.get_value("Company", company, "abbr") |
| 62 | parts = name.rsplit(" - ", 1) |
| 63 | |
| 64 | if parts[-1].lower() != company_abbr.lower(): |
| 65 | parts.append(company_abbr) |
| 66 | |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 67 | return " - ".join(parts) |
| 68 | |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 69 | def is_perpetual_inventory_enabled(company): |
| 70 | if not company: |
| 71 | company = "_Test Company" if frappe.flags.in_test else get_default_company() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 72 | |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 73 | if not hasattr(frappe.local, 'enable_perpetual_inventory'): |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 74 | frappe.local.enable_perpetual_inventory = {} |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 75 | |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 76 | if not company in frappe.local.enable_perpetual_inventory: |
Rohit Waghchaure | 2d9b3cd | 2017-06-21 09:40:06 +0530 | [diff] [blame] | 77 | frappe.local.enable_perpetual_inventory[company] = frappe.db.get_value("Company", |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 78 | company, "enable_perpetual_inventory") or 0 |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 79 | |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 80 | return frappe.local.enable_perpetual_inventory[company] |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 81 | |
| 82 | def get_region(company=None): |
| 83 | '''Return the default country based on flag, company or global settings |
| 84 | |
| 85 | You can also set global company flag in `frappe.flags.company` |
| 86 | ''' |
| 87 | if company or frappe.flags.company: |
| 88 | return frappe.db.get_value('Company', |
| 89 | company or frappe.flags.company, 'country') |
| 90 | elif frappe.flags.country: |
| 91 | return frappe.flags.country |
| 92 | else: |
| 93 | return frappe.get_system_settings('country') |
| 94 | |
Rushabh Mehta | 393becc | 2017-07-13 15:49:37 +0530 | [diff] [blame] | 95 | def allow_regional(fn): |
| 96 | '''Decorator to make a function regionally overridable |
| 97 | |
| 98 | Example: |
| 99 | @erpnext.allow_regional |
| 100 | def myfunction(): |
| 101 | pass''' |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 102 | def caller(*args, **kwargs): |
| 103 | region = get_region() |
| 104 | fn_name = inspect.getmodule(fn).__name__ + '.' + fn.__name__ |
| 105 | if region in regional_overrides and fn_name in regional_overrides[region]: |
| 106 | return frappe.get_attr(regional_overrides[region][fn_name])(*args, **kwargs) |
| 107 | else: |
| 108 | return fn(*args, **kwargs) |
| 109 | |
| 110 | return caller |
| 111 | |
Vishal | ca61453 | 2017-09-11 15:13:16 +0530 | [diff] [blame] | 112 | def get_last_membership(): |
| 113 | '''Returns last membership if exists''' |
| 114 | last_membership = frappe.get_all('Membership', 'name,to_date,membership_type', |
| 115 | dict(member=frappe.session.user, paid=1), order_by='to_date desc', limit=1) |
| 116 | |
| 117 | return last_membership and last_membership[0] |
| 118 | |
| 119 | def is_member(): |
| 120 | '''Returns true if the user is still a member''' |
| 121 | last_membership = get_last_membership() |
| 122 | if last_membership and getdate(last_membership.to_date) > getdate(): |
| 123 | return True |
| 124 | return False |