Sagar Vora | 776b56c | 2023-04-04 19:24:07 +0530 | [diff] [blame] | 1 | import functools |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 2 | import inspect |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 3 | |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 4 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | |
Ankush Menat | 54f7cf5 | 2023-12-01 10:37:35 +0530 | [diff] [blame] | 6 | __version__ = "16.0.0-dev" |
Ankush Menat | fcbd25f | 2022-04-08 17:24:10 +0530 | [diff] [blame] | 7 | |
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): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 10 | """Get default company for user""" |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 11 | from frappe.defaults import get_user_default_as_list |
| 12 | |
| 13 | if not user: |
| 14 | user = frappe.session.user |
| 15 | |
Devin Slauenwhite | ac1961b | 2024-03-08 18:09:11 -0500 | [diff] [blame] | 16 | companies = get_user_default_as_list("company", user) |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 17 | if companies: |
| 18 | default_company = companies[0] |
| 19 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 20 | default_company = frappe.db.get_single_value("Global Defaults", "default_company") |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 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(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | """Returns the currency of the default company""" |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 27 | company = get_default_company() |
| 28 | if company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 29 | return frappe.get_cached_value("Company", company, "default_currency") |
| 30 | |
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): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 33 | """Returns the default cost center of the company""" |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 34 | if not company: |
| 35 | return None |
| 36 | |
| 37 | if not frappe.flags.company_cost_center: |
| 38 | frappe.flags.company_cost_center = {} |
barredterra | eb9ee3f | 2023-12-05 11:22:55 +0100 | [diff] [blame] | 39 | if company not in frappe.flags.company_cost_center: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +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 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 43 | |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 44 | def get_company_currency(company): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 45 | """Returns the default company currency""" |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 46 | if not frappe.flags.company_currency: |
| 47 | frappe.flags.company_currency = {} |
barredterra | eb9ee3f | 2023-12-05 11:22:55 +0100 | [diff] [blame] | 48 | if company not in frappe.flags.company_currency: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | frappe.flags.company_currency[company] = frappe.db.get_value( |
| 50 | "Company", company, "default_currency", cache=True |
| 51 | ) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 52 | return frappe.flags.company_currency[company] |
| 53 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 54 | |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 55 | def set_perpetual_inventory(enable=1, company=None): |
| 56 | if not company: |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 57 | company = "_Test Company" if frappe.flags.in_test else get_default_company() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 58 | |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 59 | company = frappe.get_doc("Company", company) |
| 60 | company.enable_perpetual_inventory = enable |
| 61 | company.save() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 62 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 63 | |
Ankush Menat | d7148ad | 2021-12-20 22:00:31 +0530 | [diff] [blame] | 64 | def encode_company_abbr(name, company=None, abbr=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 65 | """Returns name encoded with company abbreviation""" |
| 66 | company_abbr = abbr or frappe.get_cached_value("Company", company, "abbr") |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 67 | parts = name.rsplit(" - ", 1) |
| 68 | |
| 69 | if parts[-1].lower() != company_abbr.lower(): |
| 70 | parts.append(company_abbr) |
| 71 | |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 72 | return " - ".join(parts) |
| 73 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 74 | |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 75 | def is_perpetual_inventory_enabled(company): |
| 76 | if not company: |
| 77 | company = "_Test Company" if frappe.flags.in_test else get_default_company() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 78 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 79 | if not hasattr(frappe.local, "enable_perpetual_inventory"): |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 80 | frappe.local.enable_perpetual_inventory = {} |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 81 | |
barredterra | eb9ee3f | 2023-12-05 11:22:55 +0100 | [diff] [blame] | 82 | if company not in frappe.local.enable_perpetual_inventory: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 83 | frappe.local.enable_perpetual_inventory[company] = ( |
| 84 | frappe.get_cached_value("Company", company, "enable_perpetual_inventory") or 0 |
| 85 | ) |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 86 | |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 87 | return frappe.local.enable_perpetual_inventory[company] |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 88 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 89 | |
Rohit Waghchaure | f689d01 | 2018-05-17 23:29:37 +0530 | [diff] [blame] | 90 | def get_default_finance_book(company=None): |
| 91 | if not company: |
| 92 | company = get_default_company() |
| 93 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 94 | if not hasattr(frappe.local, "default_finance_book"): |
Rohit Waghchaure | f689d01 | 2018-05-17 23:29:37 +0530 | [diff] [blame] | 95 | frappe.local.default_finance_book = {} |
| 96 | |
barredterra | eb9ee3f | 2023-12-05 11:22:55 +0100 | [diff] [blame] | 97 | if company not in frappe.local.default_finance_book: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 98 | frappe.local.default_finance_book[company] = frappe.get_cached_value( |
| 99 | "Company", company, "default_finance_book" |
| 100 | ) |
Rohit Waghchaure | f689d01 | 2018-05-17 23:29:37 +0530 | [diff] [blame] | 101 | |
| 102 | return frappe.local.default_finance_book[company] |
| 103 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 104 | |
rohitwaghchaure | e8358f3 | 2018-05-16 11:02:26 +0530 | [diff] [blame] | 105 | def get_party_account_type(party_type): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 106 | if not hasattr(frappe.local, "party_account_types"): |
rohitwaghchaure | e8358f3 | 2018-05-16 11:02:26 +0530 | [diff] [blame] | 107 | frappe.local.party_account_types = {} |
| 108 | |
barredterra | eb9ee3f | 2023-12-05 11:22:55 +0100 | [diff] [blame] | 109 | if party_type not in frappe.local.party_account_types: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 110 | frappe.local.party_account_types[party_type] = ( |
| 111 | frappe.db.get_value("Party Type", party_type, "account_type") or "" |
| 112 | ) |
rohitwaghchaure | e8358f3 | 2018-05-16 11:02:26 +0530 | [diff] [blame] | 113 | |
| 114 | return frappe.local.party_account_types[party_type] |
| 115 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 116 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 117 | def get_region(company=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 118 | """Return the default country based on flag, company or global settings |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 119 | |
| 120 | You can also set global company flag in `frappe.flags.company` |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 121 | """ |
Sagar Vora | 2fa641f | 2023-04-04 17:50:31 +0530 | [diff] [blame] | 122 | |
| 123 | if not company: |
| 124 | company = frappe.local.flags.company |
| 125 | |
| 126 | if company: |
| 127 | return frappe.get_cached_value("Company", company, "country") |
| 128 | |
| 129 | return frappe.flags.country or frappe.get_system_settings("country") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 130 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 131 | |
Rushabh Mehta | 393becc | 2017-07-13 15:49:37 +0530 | [diff] [blame] | 132 | def allow_regional(fn): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 133 | """Decorator to make a function regionally overridable |
Rushabh Mehta | 393becc | 2017-07-13 15:49:37 +0530 | [diff] [blame] | 134 | |
| 135 | Example: |
| 136 | @erpnext.allow_regional |
| 137 | def myfunction(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 138 | pass""" |
Sagar Vora | b68a996 | 2022-02-09 16:56:24 +0530 | [diff] [blame] | 139 | |
Sagar Vora | 776b56c | 2023-04-04 19:24:07 +0530 | [diff] [blame] | 140 | @functools.wraps(fn) |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 141 | def caller(*args, **kwargs): |
Sagar Vora | b68a996 | 2022-02-09 16:56:24 +0530 | [diff] [blame] | 142 | overrides = frappe.get_hooks("regional_overrides", {}).get(get_region()) |
| 143 | function_path = f"{inspect.getmodule(fn).__name__}.{fn.__name__}" |
| 144 | |
| 145 | if not overrides or function_path not in overrides: |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 146 | return fn(*args, **kwargs) |
| 147 | |
Sagar Vora | b68a996 | 2022-02-09 16:56:24 +0530 | [diff] [blame] | 148 | # Priority given to last installed app |
| 149 | return frappe.get_attr(overrides[function_path][-1])(*args, **kwargs) |
| 150 | |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 151 | return caller |