Anand Doshi | 227b238 | 2016-06-09 16:29:12 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | from __future__ import unicode_literals |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 3 | import inspect |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 4 | import frappe |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 5 | from erpnext.hooks import regional_overrides |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 6 | |
Saurabh | f9509a0 | 2017-11-30 17:11:46 +0600 | [diff] [blame^] | 7 | __version__ = '9.2.18' |
Anand Doshi | 227b238 | 2016-06-09 16:29:12 +0530 | [diff] [blame] | 8 | |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 9 | def get_default_company(user=None): |
| 10 | '''Get default company for user''' |
| 11 | from frappe.defaults import get_user_default_as_list |
| 12 | |
| 13 | if not user: |
| 14 | user = frappe.session.user |
| 15 | |
| 16 | companies = get_user_default_as_list(user, 'company') |
| 17 | if companies: |
| 18 | default_company = companies[0] |
| 19 | else: |
| 20 | default_company = frappe.db.get_single_value('Global Defaults', 'default_company') |
| 21 | |
| 22 | return default_company |
| 23 | |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 24 | |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 25 | def get_default_currency(): |
| 26 | '''Returns the currency of the default company''' |
| 27 | company = get_default_company() |
| 28 | if company: |
Anand Doshi | 227b238 | 2016-06-09 16:29:12 +0530 | [diff] [blame] | 29 | return frappe.db.get_value('Company', company, 'default_currency') |
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): |
| 32 | '''Returns the default cost center of the company''' |
| 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: |
| 39 | frappe.flags.company_cost_center[company] = frappe.db.get_value('Company', company, 'cost_center') |
| 40 | return frappe.flags.company_cost_center[company] |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 41 | |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 42 | def get_company_currency(company): |
| 43 | '''Returns the default company currency''' |
| 44 | if not frappe.flags.company_currency: |
| 45 | frappe.flags.company_currency = {} |
| 46 | if not company in frappe.flags.company_currency: |
| 47 | frappe.flags.company_currency[company] = frappe.db.get_value('Company', company, 'default_currency') |
| 48 | return frappe.flags.company_currency[company] |
| 49 | |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 50 | def set_perpetual_inventory(enable=1, company=None): |
| 51 | if not company: |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 52 | company = "_Test Company" if frappe.flags.in_test else get_default_company() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 53 | |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 54 | company = frappe.get_doc("Company", company) |
| 55 | company.enable_perpetual_inventory = enable |
| 56 | company.save() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 57 | |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 58 | def encode_company_abbr(name, company): |
| 59 | '''Returns name encoded with company abbreviation''' |
| 60 | company_abbr = frappe.db.get_value("Company", company, "abbr") |
| 61 | parts = name.rsplit(" - ", 1) |
| 62 | |
| 63 | if parts[-1].lower() != company_abbr.lower(): |
| 64 | parts.append(company_abbr) |
| 65 | |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 66 | return " - ".join(parts) |
| 67 | |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 68 | def is_perpetual_inventory_enabled(company): |
| 69 | if not company: |
| 70 | company = "_Test Company" if frappe.flags.in_test else get_default_company() |
tundebabzy | a109141 | 2017-05-19 07:12:45 +0100 | [diff] [blame] | 71 | |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 72 | if not hasattr(frappe.local, 'enable_perpetual_inventory'): |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 73 | frappe.local.enable_perpetual_inventory = {} |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 74 | |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 75 | if not company in frappe.local.enable_perpetual_inventory: |
Rohit Waghchaure | 2d9b3cd | 2017-06-21 09:40:06 +0530 | [diff] [blame] | 76 | frappe.local.enable_perpetual_inventory[company] = frappe.db.get_value("Company", |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 77 | company, "enable_perpetual_inventory") or 0 |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 78 | |
Rushabh Mehta | 777526e | 2017-06-20 12:14:32 +0530 | [diff] [blame] | 79 | return frappe.local.enable_perpetual_inventory[company] |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 80 | |
| 81 | def get_region(company=None): |
| 82 | '''Return the default country based on flag, company or global settings |
| 83 | |
| 84 | You can also set global company flag in `frappe.flags.company` |
| 85 | ''' |
| 86 | if company or frappe.flags.company: |
| 87 | return frappe.db.get_value('Company', |
| 88 | company or frappe.flags.company, 'country') |
| 89 | elif frappe.flags.country: |
| 90 | return frappe.flags.country |
| 91 | else: |
| 92 | return frappe.get_system_settings('country') |
| 93 | |
Rushabh Mehta | 393becc | 2017-07-13 15:49:37 +0530 | [diff] [blame] | 94 | def allow_regional(fn): |
| 95 | '''Decorator to make a function regionally overridable |
| 96 | |
| 97 | Example: |
| 98 | @erpnext.allow_regional |
| 99 | def myfunction(): |
| 100 | pass''' |
Rushabh Mehta | 7231f29 | 2017-07-13 15:00:56 +0530 | [diff] [blame] | 101 | def caller(*args, **kwargs): |
| 102 | region = get_region() |
| 103 | fn_name = inspect.getmodule(fn).__name__ + '.' + fn.__name__ |
| 104 | if region in regional_overrides and fn_name in regional_overrides[region]: |
| 105 | return frappe.get_attr(regional_overrides[region][fn_name])(*args, **kwargs) |
| 106 | else: |
| 107 | return fn(*args, **kwargs) |
| 108 | |
| 109 | return caller |
| 110 | |