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