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