2023-04-04 13:54:07 +00:00
|
|
|
import functools
|
2017-07-13 09:30:56 +00:00
|
|
|
import inspect
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-04-15 11:14:44 +00:00
|
|
|
import frappe
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2022-04-08 11:54:10 +00:00
|
|
|
__version__ = "14.0.0-dev"
|
|
|
|
|
2016-06-09 10:59:12 +00:00
|
|
|
|
2016-04-15 11:14:44 +00:00
|
|
|
def get_default_company(user=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Get default company for user"""
|
2016-04-15 11:14:44 +00:00
|
|
|
from frappe.defaults import get_user_default_as_list
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
user = frappe.session.user
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
companies = get_user_default_as_list(user, "company")
|
2016-04-15 11:14:44 +00:00
|
|
|
if companies:
|
|
|
|
default_company = companies[0]
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
default_company = frappe.db.get_single_value("Global Defaults", "default_company")
|
2016-04-15 11:14:44 +00:00
|
|
|
|
|
|
|
return default_company
|
|
|
|
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2016-04-15 11:14:44 +00:00
|
|
|
def get_default_currency():
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Returns the currency of the default company"""
|
2016-04-15 11:14:44 +00:00
|
|
|
company = get_default_company()
|
|
|
|
if company:
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.get_cached_value("Company", company, "default_currency")
|
|
|
|
|
2017-01-16 11:27:53 +00:00
|
|
|
|
2017-08-10 06:02:59 +00:00
|
|
|
def get_default_cost_center(company):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Returns the default cost center of the company"""
|
2017-08-10 06:02:59 +00:00
|
|
|
if not company:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not frappe.flags.company_cost_center:
|
|
|
|
frappe.flags.company_cost_center = {}
|
|
|
|
if not company in frappe.flags.company_cost_center:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.flags.company_cost_center[company] = frappe.get_cached_value(
|
|
|
|
"Company", company, "cost_center"
|
|
|
|
)
|
2017-08-10 06:02:59 +00:00
|
|
|
return frappe.flags.company_cost_center[company]
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-03-31 07:14:29 +00:00
|
|
|
def get_company_currency(company):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Returns the default company currency"""
|
2017-03-31 07:14:29 +00:00
|
|
|
if not frappe.flags.company_currency:
|
|
|
|
frappe.flags.company_currency = {}
|
|
|
|
if not company in frappe.flags.company_currency:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.flags.company_currency[company] = frappe.db.get_value(
|
|
|
|
"Company", company, "default_currency", cache=True
|
|
|
|
)
|
2017-03-31 07:14:29 +00:00
|
|
|
return frappe.flags.company_currency[company]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-06-16 09:51:36 +00:00
|
|
|
def set_perpetual_inventory(enable=1, company=None):
|
|
|
|
if not company:
|
2017-06-19 07:24:59 +00:00
|
|
|
company = "_Test Company" if frappe.flags.in_test else get_default_company()
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2017-06-16 09:51:36 +00:00
|
|
|
company = frappe.get_doc("Company", company)
|
|
|
|
company.enable_perpetual_inventory = enable
|
|
|
|
company.save()
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-12-20 16:30:31 +00:00
|
|
|
def encode_company_abbr(name, company=None, abbr=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Returns name encoded with company abbreviation"""
|
|
|
|
company_abbr = abbr or frappe.get_cached_value("Company", company, "abbr")
|
2017-01-16 11:27:53 +00:00
|
|
|
parts = name.rsplit(" - ", 1)
|
|
|
|
|
|
|
|
if parts[-1].lower() != company_abbr.lower():
|
|
|
|
parts.append(company_abbr)
|
|
|
|
|
2017-05-19 06:12:45 +00:00
|
|
|
return " - ".join(parts)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-06-19 07:24:59 +00:00
|
|
|
def is_perpetual_inventory_enabled(company):
|
|
|
|
if not company:
|
|
|
|
company = "_Test Company" if frappe.flags.in_test else get_default_company()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if not hasattr(frappe.local, "enable_perpetual_inventory"):
|
2017-06-20 06:44:32 +00:00
|
|
|
frappe.local.enable_perpetual_inventory = {}
|
2017-06-19 07:24:59 +00:00
|
|
|
|
2017-06-20 06:44:32 +00:00
|
|
|
if not company in frappe.local.enable_perpetual_inventory:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.local.enable_perpetual_inventory[company] = (
|
|
|
|
frappe.get_cached_value("Company", company, "enable_perpetual_inventory") or 0
|
|
|
|
)
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2017-06-20 06:44:32 +00:00
|
|
|
return frappe.local.enable_perpetual_inventory[company]
|
2017-07-13 09:30:56 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-05-17 17:59:37 +00:00
|
|
|
def get_default_finance_book(company=None):
|
|
|
|
if not company:
|
|
|
|
company = get_default_company()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if not hasattr(frappe.local, "default_finance_book"):
|
2018-05-17 17:59:37 +00:00
|
|
|
frappe.local.default_finance_book = {}
|
|
|
|
|
|
|
|
if not company in frappe.local.default_finance_book:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.local.default_finance_book[company] = frappe.get_cached_value(
|
|
|
|
"Company", company, "default_finance_book"
|
|
|
|
)
|
2018-05-17 17:59:37 +00:00
|
|
|
|
|
|
|
return frappe.local.default_finance_book[company]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-05-16 05:32:26 +00:00
|
|
|
def get_party_account_type(party_type):
|
2022-03-28 13:22:46 +00:00
|
|
|
if not hasattr(frappe.local, "party_account_types"):
|
2018-05-16 05:32:26 +00:00
|
|
|
frappe.local.party_account_types = {}
|
|
|
|
|
|
|
|
if not party_type in frappe.local.party_account_types:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.local.party_account_types[party_type] = (
|
|
|
|
frappe.db.get_value("Party Type", party_type, "account_type") or ""
|
|
|
|
)
|
2018-05-16 05:32:26 +00:00
|
|
|
|
|
|
|
return frappe.local.party_account_types[party_type]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-07-13 09:30:56 +00:00
|
|
|
def get_region(company=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Return the default country based on flag, company or global settings
|
2017-07-13 09:30:56 +00:00
|
|
|
|
|
|
|
You can also set global company flag in `frappe.flags.company`
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
2023-04-04 12:20:31 +00:00
|
|
|
|
|
|
|
if not company:
|
|
|
|
company = frappe.local.flags.company
|
|
|
|
|
|
|
|
if company:
|
|
|
|
return frappe.get_cached_value("Company", company, "country")
|
|
|
|
|
|
|
|
return frappe.flags.country or frappe.get_system_settings("country")
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-07-13 09:30:56 +00:00
|
|
|
|
2017-07-13 10:19:37 +00:00
|
|
|
def allow_regional(fn):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Decorator to make a function regionally overridable
|
2017-07-13 10:19:37 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
@erpnext.allow_regional
|
|
|
|
def myfunction():
|
2022-03-28 13:22:46 +00:00
|
|
|
pass"""
|
2022-02-09 11:26:24 +00:00
|
|
|
|
2023-04-04 13:54:07 +00:00
|
|
|
@functools.wraps(fn)
|
2017-07-13 09:30:56 +00:00
|
|
|
def caller(*args, **kwargs):
|
2022-02-09 11:26:24 +00:00
|
|
|
overrides = frappe.get_hooks("regional_overrides", {}).get(get_region())
|
|
|
|
function_path = f"{inspect.getmodule(fn).__name__}.{fn.__name__}"
|
|
|
|
|
|
|
|
if not overrides or function_path not in overrides:
|
2017-07-13 09:30:56 +00:00
|
|
|
return fn(*args, **kwargs)
|
|
|
|
|
2022-02-09 11:26:24 +00:00
|
|
|
# Priority given to last installed app
|
|
|
|
return frappe.get_attr(overrides[function_path][-1])(*args, **kwargs)
|
|
|
|
|
2017-07-13 09:30:56 +00:00
|
|
|
return caller
|