2016-06-09 10:59:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
2016-04-15 11:14:44 +00:00
|
|
|
import frappe
|
|
|
|
|
2017-06-15 10:35:49 +00:00
|
|
|
__version__ = '8.0.50'
|
2016-06-09 10:59:12 +00:00
|
|
|
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2016-04-15 11:14:44 +00:00
|
|
|
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 06:12:45 +00:00
|
|
|
|
2016-04-15 11:14:44 +00:00
|
|
|
def get_default_currency():
|
|
|
|
'''Returns the currency of the default company'''
|
|
|
|
company = get_default_company()
|
|
|
|
if company:
|
2016-06-09 10:59:12 +00:00
|
|
|
return frappe.db.get_value('Company', company, 'default_currency')
|
2017-01-16 11:27:53 +00:00
|
|
|
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2017-03-31 07:14:29 +00:00
|
|
|
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:
|
|
|
|
frappe.flags.company_currency[company] = frappe.db.get_value('Company', company, 'default_currency')
|
|
|
|
return frappe.flags.company_currency[company]
|
|
|
|
|
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
|
|
|
|
2017-01-16 11:27:53 +00:00
|
|
|
def encode_company_abbr(name, company):
|
|
|
|
'''Returns name encoded with company abbreviation'''
|
|
|
|
company_abbr = frappe.db.get_value("Company", company, "abbr")
|
|
|
|
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)
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
if not hasattr(frappe.local, 'enable_perpetual_inventory'):
|
|
|
|
perpetual_inventory = get_company_wise_perptual_inventory()
|
|
|
|
frappe.local.enable_perpetual_inventory = perpetual_inventory
|
|
|
|
|
|
|
|
if not frappe.local.enable_perpetual_inventory.get(company):
|
|
|
|
is_enabled = frappe.db.get_value("Company", company, "enable_perpetual_inventory") or 0
|
|
|
|
frappe.local.enable_perpetual_inventory.setdefault(company, is_enabled)
|
|
|
|
|
|
|
|
return frappe.local.enable_perpetual_inventory.get(company)
|
|
|
|
|
|
|
|
def get_company_wise_perptual_inventory():
|
|
|
|
company_dict = {}
|
|
|
|
for data in frappe.get_all('Company', fields = ["name", "enable_perpetual_inventory"]):
|
|
|
|
company_dict[data.name] = data.enable_perpetual_inventory
|
2017-05-19 06:12:45 +00:00
|
|
|
|
2017-06-19 07:24:59 +00:00
|
|
|
return company_dict
|