Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 3 | import frappe |
Nabin Hait | d2c5b50 | 2018-10-08 18:19:53 +0530 | [diff] [blame] | 4 | from frappe import _ |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 5 | |
Anand Doshi | 7e0479b | 2012-04-05 18:27:56 +0530 | [diff] [blame] | 6 | install_docs = [ |
Rushabh Mehta | 9415733 | 2015-07-13 15:06:12 +0530 | [diff] [blame] | 7 | {"doctype":"Role", "role_name":"Stock Manager", "name":"Stock Manager"}, |
| 8 | {"doctype":"Role", "role_name":"Item Manager", "name":"Item Manager"}, |
| 9 | {"doctype":"Role", "role_name":"Stock User", "name":"Stock User"}, |
Anand Doshi | 24e1856 | 2012-04-17 11:45:35 +0530 | [diff] [blame] | 10 | {"doctype":"Role", "role_name":"Quality Manager", "name":"Quality Manager"}, |
Saurabh | 6bc7b89 | 2016-07-14 14:03:19 +0530 | [diff] [blame] | 11 | {"doctype":"Item Group", "item_group_name":"All Item Groups", "is_group": 1}, |
Rohit Waghchaure | 6b33c9b | 2019-03-08 11:13:35 +0530 | [diff] [blame] | 12 | {"doctype":"Item Group", "item_group_name":"Default", |
Saurabh | 6bc7b89 | 2016-07-14 14:03:19 +0530 | [diff] [blame] | 13 | "parent_item_group":"All Item Groups", "is_group": 0}, |
Anand Doshi | 7e0479b | 2012-04-05 18:27:56 +0530 | [diff] [blame] | 14 | ] |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 15 | |
Rohit Waghchaure | 6b33c9b | 2019-03-08 11:13:35 +0530 | [diff] [blame] | 16 | def get_warehouse_account_map(company=None): |
Saqib | 25846cc | 2020-03-31 15:31:15 +0530 | [diff] [blame] | 17 | company_warehouse_account_map = company and frappe.flags.setdefault('warehouse_account_map', {}).get(company) |
| 18 | warehouse_account_map = frappe.flags.warehouse_account_map |
| 19 | |
| 20 | if not warehouse_account_map or not company_warehouse_account_map or frappe.flags.in_test: |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 21 | warehouse_account = frappe._dict() |
| 22 | |
Rohit Waghchaure | 6b33c9b | 2019-03-08 11:13:35 +0530 | [diff] [blame] | 23 | filters = {} |
| 24 | if company: |
| 25 | filters['company'] = company |
Saqib | 25846cc | 2020-03-31 15:31:15 +0530 | [diff] [blame] | 26 | frappe.flags.setdefault('warehouse_account_map', {}).setdefault(company, {}) |
Rohit Waghchaure | 6b33c9b | 2019-03-08 11:13:35 +0530 | [diff] [blame] | 27 | |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 28 | for d in frappe.get_all('Warehouse', |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 29 | fields = ["name", "account", "parent_warehouse", "company", "is_group"], |
Rohit Waghchaure | 6b33c9b | 2019-03-08 11:13:35 +0530 | [diff] [blame] | 30 | filters = filters, |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 31 | order_by="lft, rgt"): |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 32 | if not d.account: |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 33 | d.account = get_warehouse_account(d, warehouse_account) |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 34 | |
| 35 | if d.account: |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 36 | d.account_currency = frappe.db.get_value('Account', d.account, 'account_currency', cache=True) |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 37 | warehouse_account.setdefault(d.name, d) |
Saqib | 25846cc | 2020-03-31 15:31:15 +0530 | [diff] [blame] | 38 | if company: |
| 39 | frappe.flags.warehouse_account_map[company] = warehouse_account |
| 40 | else: |
| 41 | frappe.flags.warehouse_account_map = warehouse_account |
Rohit Waghchaure | c603fd6 | 2021-02-26 17:34:53 +0530 | [diff] [blame] | 42 | |
Saqib | 25846cc | 2020-03-31 15:31:15 +0530 | [diff] [blame] | 43 | return frappe.flags.warehouse_account_map.get(company) or frappe.flags.warehouse_account_map |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 44 | |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 45 | def get_warehouse_account(warehouse, warehouse_account=None): |
| 46 | account = warehouse.account |
| 47 | if not account and warehouse.parent_warehouse: |
| 48 | if warehouse_account: |
Nabin Hait | d2c5b50 | 2018-10-08 18:19:53 +0530 | [diff] [blame] | 49 | if warehouse_account.get(warehouse.parent_warehouse): |
| 50 | account = warehouse_account.get(warehouse.parent_warehouse).account |
| 51 | else: |
| 52 | from frappe.utils.nestedset import rebuild_tree |
| 53 | rebuild_tree("Warehouse", "parent_warehouse") |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 54 | else: |
| 55 | account = frappe.db.sql(""" |
| 56 | select |
| 57 | account from `tabWarehouse` |
| 58 | where |
| 59 | lft <= %s and rgt >= %s and company = %s |
| 60 | and account is not null and ifnull(account, '') !='' |
| 61 | order by lft desc limit 1""", (warehouse.lft, warehouse.rgt, warehouse.company), as_list=1) |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 62 | |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 63 | account = account[0][0] if account else None |
| 64 | |
Nabin Hait | 9409edd | 2018-08-16 13:58:44 +0530 | [diff] [blame] | 65 | if not account and warehouse.company: |
Nabin Hait | edd320e | 2018-04-02 10:30:17 +0530 | [diff] [blame] | 66 | account = get_company_default_inventory_account(warehouse.company) |
| 67 | |
Rohit Waghchaure | c603fd6 | 2021-02-26 17:34:53 +0530 | [diff] [blame] | 68 | if not account and warehouse.company: |
| 69 | account = frappe.db.get_value('Account', |
| 70 | {'account_type': 'Stock', 'is_group': 0, 'company': warehouse.company}, 'name') |
| 71 | |
Nabin Hait | 9b178bc | 2021-02-16 14:57:00 +0530 | [diff] [blame] | 72 | if not account and warehouse.company and not warehouse.is_group: |
Nabin Hait | d2c5b50 | 2018-10-08 18:19:53 +0530 | [diff] [blame] | 73 | frappe.throw(_("Please set Account in Warehouse {0} or Default Inventory Account in Company {1}") |
Nabin Hait | 5d11716 | 2018-10-19 18:03:13 +0530 | [diff] [blame] | 74 | .format(warehouse.name, warehouse.company)) |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 75 | return account |
Rohit Waghchaure | 6b33c9b | 2019-03-08 11:13:35 +0530 | [diff] [blame] | 76 | |
Nabin Hait | 6d7b0ce | 2017-06-15 11:09:27 +0530 | [diff] [blame] | 77 | def get_company_default_inventory_account(company): |
Nabin Hait | d826bee | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 78 | return frappe.get_cached_value('Company', company, 'default_inventory_account') |