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