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