blob: 7a8ff199c8f0303673c64e943f69df09264c7711 [file] [log] [blame]
Sagar Vora776b56c2023-04-04 19:24:07 +05301import functools
Rushabh Mehta7231f292017-07-13 15:00:56 +05302import inspect
Chillar Anand915b3432021-09-02 16:44:59 +05303
Rushabh Mehta86a824c2016-04-15 16:44:44 +05304import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05305
Ankush Menat54f7cf52023-12-01 10:37:35 +05306__version__ = "16.0.0-dev"
Ankush Menatfcbd25f2022-04-08 17:24:10 +05307
Anand Doshi227b2382016-06-09 16:29:12 +05308
Rushabh Mehta86a824c2016-04-15 16:44:44 +05309def get_default_company(user=None):
Ankush Menat494bd9e2022-03-28 18:52:46 +053010 """Get default company for user"""
Rushabh Mehta86a824c2016-04-15 16:44:44 +053011 from frappe.defaults import get_user_default_as_list
12
13 if not user:
14 user = frappe.session.user
15
Devin Slauenwhiteac1961b2024-03-08 18:09:11 -050016 companies = get_user_default_as_list("company", user)
Rushabh Mehta86a824c2016-04-15 16:44:44 +053017 if companies:
18 default_company = companies[0]
19 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +053020 default_company = frappe.db.get_single_value("Global Defaults", "default_company")
Rushabh Mehta86a824c2016-04-15 16:44:44 +053021
22 return default_company
23
tundebabzya1091412017-05-19 07:12:45 +010024
Rushabh Mehta86a824c2016-04-15 16:44:44 +053025def get_default_currency():
Ankush Menat494bd9e2022-03-28 18:52:46 +053026 """Returns the currency of the default company"""
Rushabh Mehta86a824c2016-04-15 16:44:44 +053027 company = get_default_company()
28 if company:
Ankush Menat494bd9e2022-03-28 18:52:46 +053029 return frappe.get_cached_value("Company", company, "default_currency")
30
Rushabh Mehta66958302017-01-16 16:57:53 +053031
rohitwaghchaure2e8232e2017-08-10 11:32:59 +053032def get_default_cost_center(company):
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 """Returns the default cost center of the company"""
rohitwaghchaure2e8232e2017-08-10 11:32:59 +053034 if not company:
35 return None
36
37 if not frappe.flags.company_cost_center:
38 frappe.flags.company_cost_center = {}
barredterraeb9ee3f2023-12-05 11:22:55 +010039 if company not in frappe.flags.company_cost_center:
Akhil Narang3effaf22024-03-27 11:37:26 +053040 frappe.flags.company_cost_center[company] = frappe.get_cached_value("Company", company, "cost_center")
rohitwaghchaure2e8232e2017-08-10 11:32:59 +053041 return frappe.flags.company_cost_center[company]
tundebabzya1091412017-05-19 07:12:45 +010042
Ankush Menat494bd9e2022-03-28 18:52:46 +053043
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053044def get_company_currency(company):
Ankush Menat494bd9e2022-03-28 18:52:46 +053045 """Returns the default company currency"""
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053046 if not frappe.flags.company_currency:
47 frappe.flags.company_currency = {}
barredterraeb9ee3f2023-12-05 11:22:55 +010048 if company not in frappe.flags.company_currency:
Ankush Menat494bd9e2022-03-28 18:52:46 +053049 frappe.flags.company_currency[company] = frappe.db.get_value(
50 "Company", company, "default_currency", cache=True
51 )
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053052 return frappe.flags.company_currency[company]
53
Ankush Menat494bd9e2022-03-28 18:52:46 +053054
Rohit Waghchaurea5f40942017-06-16 15:21:36 +053055def set_perpetual_inventory(enable=1, company=None):
56 if not company:
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053057 company = "_Test Company" if frappe.flags.in_test else get_default_company()
tundebabzya1091412017-05-19 07:12:45 +010058
Rohit Waghchaurea5f40942017-06-16 15:21:36 +053059 company = frappe.get_doc("Company", company)
60 company.enable_perpetual_inventory = enable
61 company.save()
tundebabzya1091412017-05-19 07:12:45 +010062
Ankush Menat494bd9e2022-03-28 18:52:46 +053063
Ankush Menatd7148ad2021-12-20 22:00:31 +053064def encode_company_abbr(name, company=None, abbr=None):
Ankush Menat494bd9e2022-03-28 18:52:46 +053065 """Returns name encoded with company abbreviation"""
66 company_abbr = abbr or frappe.get_cached_value("Company", company, "abbr")
Rushabh Mehta66958302017-01-16 16:57:53 +053067 parts = name.rsplit(" - ", 1)
68
69 if parts[-1].lower() != company_abbr.lower():
70 parts.append(company_abbr)
71
tundebabzya1091412017-05-19 07:12:45 +010072 return " - ".join(parts)
73
Ankush Menat494bd9e2022-03-28 18:52:46 +053074
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053075def is_perpetual_inventory_enabled(company):
76 if not company:
77 company = "_Test Company" if frappe.flags.in_test else get_default_company()
tundebabzya1091412017-05-19 07:12:45 +010078
Ankush Menat494bd9e2022-03-28 18:52:46 +053079 if not hasattr(frappe.local, "enable_perpetual_inventory"):
Rushabh Mehta777526e2017-06-20 12:14:32 +053080 frappe.local.enable_perpetual_inventory = {}
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053081
barredterraeb9ee3f2023-12-05 11:22:55 +010082 if company not in frappe.local.enable_perpetual_inventory:
Ankush Menat494bd9e2022-03-28 18:52:46 +053083 frappe.local.enable_perpetual_inventory[company] = (
84 frappe.get_cached_value("Company", company, "enable_perpetual_inventory") or 0
85 )
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053086
Rushabh Mehta777526e2017-06-20 12:14:32 +053087 return frappe.local.enable_perpetual_inventory[company]
Rushabh Mehta7231f292017-07-13 15:00:56 +053088
Ankush Menat494bd9e2022-03-28 18:52:46 +053089
Rohit Waghchauref689d012018-05-17 23:29:37 +053090def get_default_finance_book(company=None):
91 if not company:
92 company = get_default_company()
93
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 if not hasattr(frappe.local, "default_finance_book"):
Rohit Waghchauref689d012018-05-17 23:29:37 +053095 frappe.local.default_finance_book = {}
96
barredterraeb9ee3f2023-12-05 11:22:55 +010097 if company not in frappe.local.default_finance_book:
Ankush Menat494bd9e2022-03-28 18:52:46 +053098 frappe.local.default_finance_book[company] = frappe.get_cached_value(
99 "Company", company, "default_finance_book"
100 )
Rohit Waghchauref689d012018-05-17 23:29:37 +0530101
102 return frappe.local.default_finance_book[company]
103
Ankush Menat494bd9e2022-03-28 18:52:46 +0530104
rohitwaghchauree8358f32018-05-16 11:02:26 +0530105def get_party_account_type(party_type):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530106 if not hasattr(frappe.local, "party_account_types"):
rohitwaghchauree8358f32018-05-16 11:02:26 +0530107 frappe.local.party_account_types = {}
108
barredterraeb9ee3f2023-12-05 11:22:55 +0100109 if party_type not in frappe.local.party_account_types:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530110 frappe.local.party_account_types[party_type] = (
111 frappe.db.get_value("Party Type", party_type, "account_type") or ""
112 )
rohitwaghchauree8358f32018-05-16 11:02:26 +0530113
114 return frappe.local.party_account_types[party_type]
115
Ankush Menat494bd9e2022-03-28 18:52:46 +0530116
Rushabh Mehta7231f292017-07-13 15:00:56 +0530117def get_region(company=None):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530118 """Return the default country based on flag, company or global settings
Rushabh Mehta7231f292017-07-13 15:00:56 +0530119
120 You can also set global company flag in `frappe.flags.company`
Ankush Menat494bd9e2022-03-28 18:52:46 +0530121 """
Sagar Vora2fa641f2023-04-04 17:50:31 +0530122
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 Menat494bd9e2022-03-28 18:52:46 +0530130
Rushabh Mehta7231f292017-07-13 15:00:56 +0530131
Rushabh Mehta393becc2017-07-13 15:49:37 +0530132def allow_regional(fn):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530133 """Decorator to make a function regionally overridable
Rushabh Mehta393becc2017-07-13 15:49:37 +0530134
135 Example:
136 @erpnext.allow_regional
137 def myfunction():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530138 pass"""
Sagar Vorab68a9962022-02-09 16:56:24 +0530139
Sagar Vora776b56c2023-04-04 19:24:07 +0530140 @functools.wraps(fn)
Rushabh Mehta7231f292017-07-13 15:00:56 +0530141 def caller(*args, **kwargs):
Sagar Vorab68a9962022-02-09 16:56:24 +0530142 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 Mehta7231f292017-07-13 15:00:56 +0530146 return fn(*args, **kwargs)
147
Sagar Vorab68a9962022-02-09 16:56:24 +0530148 # Priority given to last installed app
149 return frappe.get_attr(overrides[function_path][-1])(*args, **kwargs)
150
Rushabh Mehta7231f292017-07-13 15:00:56 +0530151 return caller