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 | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 8 | __version__ = '11.1.39' |
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: |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 30 | return frappe.get_cached_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: |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 40 | frappe.flags.company_cost_center[company] = frappe.get_cached_value('Company', company, 'cost_center') |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 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: |
Nabin Hait | 2a774fa | 2018-08-09 11:50:34 +0530 | [diff] [blame] | 48 | frappe.flags.company_currency[company] = frappe.db.get_value('Company', company, 'default_currency', cache=True) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 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''' |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 61 | company_abbr = frappe.get_cached_value('Company', company, "abbr") |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 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: |
Himanshu Mishra | 35b2627 | 2018-11-13 11:13:04 +0530 | [diff] [blame] | 77 | frappe.local.enable_perpetual_inventory[company] = frappe.get_cached_value('Company', |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +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 | |
Rohit Waghchaure | f689d01 | 2018-05-17 23:29:37 +0530 | [diff] [blame] | 82 | def get_default_finance_book(company=None): |
| 83 | if not company: |
| 84 | company = get_default_company() |
| 85 | |
| 86 | if not hasattr(frappe.local, 'default_finance_book'): |
| 87 | frappe.local.default_finance_book = {} |
| 88 | |
| 89 | if not company in frappe.local.default_finance_book: |
Himanshu Mishra | 35b2627 | 2018-11-13 11:13:04 +0530 | [diff] [blame] | 90 | frappe.local.default_finance_book[company] = frappe.get_cached_value('Company', |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 91 | company, "default_finance_book") |
Rohit Waghchaure | f689d01 | 2018-05-17 23:29:37 +0530 | [diff] [blame] | 92 | |
| 93 | return frappe.local.default_finance_book[company] |
| 94 | |
rohitwaghchaure | e8358f3 | 2018-05-16 11:02:26 +0530 | [diff] [blame] | 95 | def get_party_account_type(party_type): |
| 96 | if not hasattr(frappe.local, 'party_account_types'): |
| 97 | frappe.local.party_account_types = {} |
| 98 | |
| 99 | if not party_type in frappe.local.party_account_types: |
| 100 | frappe.local.party_account_types[party_type] = frappe.db.get_value("Party Type", |
| 101 | party_type, "account_type") or '' |
| 102 | |
| 103 | return frappe.local.party_account_types[party_type] |
| 104 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 105 | def get_region(company=None): |
| 106 | '''Return the default country based on flag, company or global settings |
| 107 | |
| 108 | You can also set global company flag in `frappe.flags.company` |
| 109 | ''' |
| 110 | if company or frappe.flags.company: |
Himanshu Mishra | 35b2627 | 2018-11-13 11:13:04 +0530 | [diff] [blame] | 111 | return frappe.get_cached_value('Company', |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 112 | company or frappe.flags.company, 'country') |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 113 | elif frappe.flags.country: |
| 114 | return frappe.flags.country |
| 115 | else: |
| 116 | return frappe.get_system_settings('country') |
| 117 | |
Rushabh Mehta | 393becc | 2017-07-13 15:49:37 +0530 | [diff] [blame] | 118 | def allow_regional(fn): |
| 119 | '''Decorator to make a function regionally overridable |
| 120 | |
| 121 | Example: |
| 122 | @erpnext.allow_regional |
| 123 | def myfunction(): |
| 124 | pass''' |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 125 | def caller(*args, **kwargs): |
| 126 | region = get_region() |
| 127 | fn_name = inspect.getmodule(fn).__name__ + '.' + fn.__name__ |
| 128 | if region in regional_overrides and fn_name in regional_overrides[region]: |
| 129 | return frappe.get_attr(regional_overrides[region][fn_name])(*args, **kwargs) |
| 130 | else: |
| 131 | return fn(*args, **kwargs) |
| 132 | |
| 133 | return caller |
| 134 | |
Vishal | ca61453 | 2017-09-11 15:13:16 +0530 | [diff] [blame] | 135 | def get_last_membership(): |
| 136 | '''Returns last membership if exists''' |
| 137 | last_membership = frappe.get_all('Membership', 'name,to_date,membership_type', |
| 138 | dict(member=frappe.session.user, paid=1), order_by='to_date desc', limit=1) |
| 139 | |
| 140 | return last_membership and last_membership[0] |
| 141 | |
| 142 | def is_member(): |
| 143 | '''Returns true if the user is still a member''' |
| 144 | last_membership = get_last_membership() |
| 145 | if last_membership and getdate(last_membership.to_date) > getdate(): |
| 146 | return True |
Himanshu Mishra | 35b2627 | 2018-11-13 11:13:04 +0530 | [diff] [blame] | 147 | return False |