2016-06-09 16:29:12 +05:30
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
2017-07-13 15:00:56 +05:30
|
|
|
import inspect
|
2016-04-15 16:44:44 +05:30
|
|
|
import frappe
|
2017-07-13 15:00:56 +05:30
|
|
|
from erpnext.hooks import regional_overrides
|
2017-09-19 15:18:33 +05:30
|
|
|
from frappe.utils import getdate
|
2016-04-15 16:44:44 +05:30
|
|
|
|
2021-08-01 14:55:09 +05:50
|
|
|
__version__ = '13.8.0'
|
2016-06-09 16:29:12 +05:30
|
|
|
|
2016-04-15 16:44:44 +05:30
|
|
|
def get_default_company(user=None):
|
|
|
|
'''Get default company for user'''
|
|
|
|
from frappe.defaults import get_user_default_as_list
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
user = frappe.session.user
|
|
|
|
|
|
|
|
companies = get_user_default_as_list(user, 'company')
|
|
|
|
if companies:
|
|
|
|
default_company = companies[0]
|
|
|
|
else:
|
|
|
|
default_company = frappe.db.get_single_value('Global Defaults', 'default_company')
|
|
|
|
|
|
|
|
return default_company
|
|
|
|
|
2017-05-19 07:12:45 +01:00
|
|
|
|
2016-04-15 16:44:44 +05:30
|
|
|
def get_default_currency():
|
|
|
|
'''Returns the currency of the default company'''
|
|
|
|
company = get_default_company()
|
|
|
|
if company:
|
2018-08-08 16:37:31 +05:30
|
|
|
return frappe.get_cached_value('Company', company, 'default_currency')
|
2017-01-16 16:57:53 +05:30
|
|
|
|
2017-08-10 11:32:59 +05:30
|
|
|
def get_default_cost_center(company):
|
|
|
|
'''Returns the default cost center of the company'''
|
|
|
|
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:
|
2018-08-08 16:37:31 +05:30
|
|
|
frappe.flags.company_cost_center[company] = frappe.get_cached_value('Company', company, 'cost_center')
|
2017-08-10 11:32:59 +05:30
|
|
|
return frappe.flags.company_cost_center[company]
|
2017-05-19 07:12:45 +01:00
|
|
|
|
2017-03-31 12:44:29 +05:30
|
|
|
def get_company_currency(company):
|
|
|
|
'''Returns the default company currency'''
|
|
|
|
if not frappe.flags.company_currency:
|
|
|
|
frappe.flags.company_currency = {}
|
|
|
|
if not company in frappe.flags.company_currency:
|
2018-08-09 11:50:34 +05:30
|
|
|
frappe.flags.company_currency[company] = frappe.db.get_value('Company', company, 'default_currency', cache=True)
|
2017-03-31 12:44:29 +05:30
|
|
|
return frappe.flags.company_currency[company]
|
|
|
|
|
2017-06-16 15:21:36 +05:30
|
|
|
def set_perpetual_inventory(enable=1, company=None):
|
|
|
|
if not company:
|
2017-06-19 12:54:59 +05:30
|
|
|
company = "_Test Company" if frappe.flags.in_test else get_default_company()
|
2017-05-19 07:12:45 +01:00
|
|
|
|
2017-06-16 15:21:36 +05:30
|
|
|
company = frappe.get_doc("Company", company)
|
|
|
|
company.enable_perpetual_inventory = enable
|
|
|
|
company.save()
|
2017-05-19 07:12:45 +01:00
|
|
|
|
2017-01-16 16:57:53 +05:30
|
|
|
def encode_company_abbr(name, company):
|
|
|
|
'''Returns name encoded with company abbreviation'''
|
2018-08-08 16:37:31 +05:30
|
|
|
company_abbr = frappe.get_cached_value('Company', company, "abbr")
|
2017-01-16 16:57:53 +05:30
|
|
|
parts = name.rsplit(" - ", 1)
|
|
|
|
|
|
|
|
if parts[-1].lower() != company_abbr.lower():
|
|
|
|
parts.append(company_abbr)
|
|
|
|
|
2017-05-19 07:12:45 +01:00
|
|
|
return " - ".join(parts)
|
|
|
|
|
2017-06-19 12:54:59 +05:30
|
|
|
def is_perpetual_inventory_enabled(company):
|
|
|
|
if not company:
|
|
|
|
company = "_Test Company" if frappe.flags.in_test else get_default_company()
|
|
|
|
|
|
|
|
if not hasattr(frappe.local, 'enable_perpetual_inventory'):
|
2017-06-20 12:14:32 +05:30
|
|
|
frappe.local.enable_perpetual_inventory = {}
|
2017-06-19 12:54:59 +05:30
|
|
|
|
2017-06-20 12:14:32 +05:30
|
|
|
if not company in frappe.local.enable_perpetual_inventory:
|
2018-11-13 11:13:04 +05:30
|
|
|
frappe.local.enable_perpetual_inventory[company] = frappe.get_cached_value('Company',
|
2018-08-08 16:37:31 +05:30
|
|
|
company, "enable_perpetual_inventory") or 0
|
2017-05-19 07:12:45 +01:00
|
|
|
|
2017-06-20 12:14:32 +05:30
|
|
|
return frappe.local.enable_perpetual_inventory[company]
|
2017-07-13 15:00:56 +05:30
|
|
|
|
2018-05-17 23:29:37 +05:30
|
|
|
def get_default_finance_book(company=None):
|
|
|
|
if not company:
|
|
|
|
company = get_default_company()
|
|
|
|
|
|
|
|
if not hasattr(frappe.local, 'default_finance_book'):
|
|
|
|
frappe.local.default_finance_book = {}
|
|
|
|
|
|
|
|
if not company in frappe.local.default_finance_book:
|
2018-11-13 11:13:04 +05:30
|
|
|
frappe.local.default_finance_book[company] = frappe.get_cached_value('Company',
|
2018-08-08 16:37:31 +05:30
|
|
|
company, "default_finance_book")
|
2018-05-17 23:29:37 +05:30
|
|
|
|
|
|
|
return frappe.local.default_finance_book[company]
|
|
|
|
|
2018-05-16 11:02:26 +05:30
|
|
|
def get_party_account_type(party_type):
|
|
|
|
if not hasattr(frappe.local, 'party_account_types'):
|
|
|
|
frappe.local.party_account_types = {}
|
|
|
|
|
|
|
|
if not party_type in frappe.local.party_account_types:
|
|
|
|
frappe.local.party_account_types[party_type] = frappe.db.get_value("Party Type",
|
|
|
|
party_type, "account_type") or ''
|
|
|
|
|
|
|
|
return frappe.local.party_account_types[party_type]
|
|
|
|
|
2017-07-13 15:00:56 +05:30
|
|
|
def get_region(company=None):
|
|
|
|
'''Return the default country based on flag, company or global settings
|
|
|
|
|
|
|
|
You can also set global company flag in `frappe.flags.company`
|
|
|
|
'''
|
|
|
|
if company or frappe.flags.company:
|
2018-11-13 11:13:04 +05:30
|
|
|
return frappe.get_cached_value('Company',
|
2021-02-19 14:30:23 +05:30
|
|
|
company or frappe.flags.company, 'country')
|
2017-07-13 15:00:56 +05:30
|
|
|
elif frappe.flags.country:
|
|
|
|
return frappe.flags.country
|
|
|
|
else:
|
|
|
|
return frappe.get_system_settings('country')
|
|
|
|
|
2017-07-13 15:49:37 +05:30
|
|
|
def allow_regional(fn):
|
|
|
|
'''Decorator to make a function regionally overridable
|
|
|
|
|
|
|
|
Example:
|
|
|
|
@erpnext.allow_regional
|
|
|
|
def myfunction():
|
|
|
|
pass'''
|
2017-07-13 15:00:56 +05:30
|
|
|
def caller(*args, **kwargs):
|
|
|
|
region = get_region()
|
|
|
|
fn_name = inspect.getmodule(fn).__name__ + '.' + fn.__name__
|
|
|
|
if region in regional_overrides and fn_name in regional_overrides[region]:
|
|
|
|
return frappe.get_attr(regional_overrides[region][fn_name])(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
return fn(*args, **kwargs)
|
|
|
|
|
|
|
|
return caller
|
|
|
|
|
2020-11-09 14:01:11 +05:30
|
|
|
def get_last_membership(member):
|
2017-09-11 15:13:16 +05:30
|
|
|
'''Returns last membership if exists'''
|
|
|
|
last_membership = frappe.get_all('Membership', 'name,to_date,membership_type',
|
2020-11-09 14:01:11 +05:30
|
|
|
dict(member=member, paid=1), order_by='to_date desc', limit=1)
|
2017-09-11 15:13:16 +05:30
|
|
|
|
2020-11-09 14:01:11 +05:30
|
|
|
if last_membership:
|
|
|
|
return last_membership[0]
|