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