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