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