From 99892c7ce746ea020af202a7fb4de2f5a20b765f Mon Sep 17 00:00:00 2001 From: Saurabh Date: Sun, 26 Jun 2016 22:32:05 +0530 Subject: [PATCH] [fix] provision to convert group to ledger and vice versa --- erpnext/accounts/doctype/account/account.js | 2 +- erpnext/patches.txt | 1 + .../v7_0/set_is_group_for_warehouse.py | 6 ++ erpnext/stock/doctype/warehouse/warehouse.js | 48 +++++++++---- .../stock/doctype/warehouse/warehouse.json | 57 +++++++-------- erpnext/stock/doctype/warehouse/warehouse.py | 71 ++++++++++++++++--- 6 files changed, 134 insertions(+), 51 deletions(-) create mode 100644 erpnext/patches/v7_0/set_is_group_for_warehouse.py diff --git a/erpnext/accounts/doctype/account/account.js b/erpnext/accounts/doctype/account/account.js index 9364b59f95..c3a01b941b 100644 --- a/erpnext/accounts/doctype/account/account.js +++ b/erpnext/accounts/doctype/account/account.js @@ -63,7 +63,7 @@ cur_frm.cscript.add_toolbar_buttons = function(doc) { frappe.set_route("query-report", "General Ledger"); }, __("View")); - cur_frm.add_custom_button(__('Group to Group'), + cur_frm.add_custom_button(__('Non-Group to Group'), function() { cur_frm.cscript.convert_to_group(); }, 'icon-retweet', 'btn-default') } } diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 0af9713b86..bcbe2dabf9 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -281,3 +281,4 @@ erpnext.patches.v7_0.convert_timelogbatch_to_timesheet erpnext.patches.v7_0.convert_timelog_to_timesheet erpnext.patches.v7_0.move_timelogbatch_from_salesinvoiceitem_to_salesinvoicetimesheet erpnext.patches.v7_0.remove_doctypes_and_reports +erpnext.patches.v7_0.set_is_group_for_warehouse diff --git a/erpnext/patches/v7_0/set_is_group_for_warehouse.py b/erpnext/patches/v7_0/set_is_group_for_warehouse.py new file mode 100644 index 0000000000..d74b97b4e1 --- /dev/null +++ b/erpnext/patches/v7_0/set_is_group_for_warehouse.py @@ -0,0 +1,6 @@ +import frappe + +def execute(): + frappe.reload_doc("stock", "doctype", "warehouse") + + frappe.db.sql("""update tabWarehouse set is_group = if (is_group="Yes", 1, 0)""") \ No newline at end of file diff --git a/erpnext/stock/doctype/warehouse/warehouse.js b/erpnext/stock/doctype/warehouse/warehouse.js index b7a3a34c6d..5983014855 100644 --- a/erpnext/stock/doctype/warehouse/warehouse.js +++ b/erpnext/stock/doctype/warehouse/warehouse.js @@ -9,29 +9,35 @@ frappe.ui.form.on("Warehouse", { frm.add_custom_button(__("Stock Balance"), function() { frappe.set_route("query-report", "Stock Balance", {"warehouse": frm.doc.name}); }); - if(frm.doc.__onload && frm.doc.__onload.account) { - frm.add_custom_button(__("General Ledger"), function() { - frappe.route_options = { - "account": frm.doc.__onload.account, - "company": frm.doc.company - } - frappe.set_route("query-report", "General Ledger"); - }); - } - + + if (cint(frm.doc.is_group) == 1) { + frm.add_custom_button(__('Group to Non-Group'), + function() { convert_to_group_or_ledger(frm); }, 'icon-retweet', 'btn-default') + } else if (cint(frm.doc.is_group) == 0) { + if(frm.doc.__onload && frm.doc.__onload.account) { + frm.add_custom_button(__("General Ledger"), function() { + frappe.route_options = { + "account": frm.doc.__onload.account, + "company": frm.doc.company + } + frappe.set_route("query-report", "General Ledger"); + }); + } + + frm.add_custom_button(__('Non-Group to Group'), + function() { convert_to_group_or_ledger(frm); }, 'icon-retweet', 'btn-default') + } + frm.fields_dict['parent_warehouse'].get_query = function(doc) { return { filters: { - "is_group": "Yes", + "is_group": 1, } } } } }); - - - cur_frm.set_query("create_account_under", function() { return { filters: { @@ -40,3 +46,17 @@ cur_frm.set_query("create_account_under", function() { } } }) + +function convert_to_group_or_ledger(frm){ + frappe.call({ + method:"erpnext.stock.doctype.warehouse.warehouse.convert_to_group_or_ledger", + args: { + docname: frm.doc.name, + is_group: frm.doc.is_group + }, + callback: function(){ + frm.refresh(); + } + + }) +} \ No newline at end of file diff --git a/erpnext/stock/doctype/warehouse/warehouse.json b/erpnext/stock/doctype/warehouse/warehouse.json index 1a3a97f1ff..a32ec1f93e 100644 --- a/erpnext/stock/doctype/warehouse/warehouse.json +++ b/erpnext/stock/doctype/warehouse/warehouse.json @@ -35,6 +35,33 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "default": "1", + "fieldname": "is_group", + "fieldtype": "Check", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_list_view": 1, + "label": "Is Group", + "length": 0, + "no_copy": 0, + "options": "", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_on_submit": 0, "bold": 0, @@ -473,32 +500,6 @@ "set_only_once": 0, "unique": 0 }, - { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "fieldname": "is_group", - "fieldtype": "Select", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 1, - "label": "Has Child Node", - "length": 0, - "no_copy": 0, - "options": "\nYes\nNo", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "report_hide": 0, - "reqd": 1, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, { "allow_on_submit": 0, "bold": 0, @@ -587,8 +588,8 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2016-06-25 18:21:05.175172", - "modified_by": "Administrator", + "modified": "2016-06-26 17:39:16.856800", + "modified_by": "s@s.com", "module": "Stock", "name": "Warehouse", "owner": "Administrator", diff --git a/erpnext/stock/doctype/warehouse/warehouse.py b/erpnext/stock/doctype/warehouse/warehouse.py index ed68065626..0ceaeda23c 100644 --- a/erpnext/stock/doctype/warehouse/warehouse.py +++ b/erpnext/stock/doctype/warehouse/warehouse.py @@ -60,7 +60,7 @@ class Warehouse(NestedSet): 'account_name': self.warehouse_name, 'parent_account': self.parent_warehouse if self.parent_warehouse \ else self.create_account_under, - 'is_group': 1 if self.is_group=="Yes" else 0 , + 'is_group': self.is_group, 'company':self.company, "account_type": "Stock", "warehouse": self.name, @@ -113,14 +113,21 @@ class Warehouse(NestedSet): if warehouse_account: frappe.delete_doc("Account", warehouse_account) - if frappe.db.sql("""select name from `tabStock Ledger Entry` - where warehouse = %s""", self.name): + if self.check_sle_exists(): throw(_("Warehouse can not be deleted as stock ledger entry exists for this warehouse.")) - if frappe.db.sql("""select name from `tabWarehouse` where parent_warehouse = %s""", self.name): + if self.check_if_child_exists(): throw(_("Child warehouse exists for this warehouse. You can not delete this warehouse.")) self.update_nsm_model() + + def check_if_sle_exists(self): + return frappe.db.sql("""select name from `tabStock Ledger Entry` + where warehouse = %s""", self.name) + + def check_if_child_exists(self): + return frappe.db.sql("""select name from `tabWarehouse` + where parent_warehouse = %s""", self.name) def before_rename(self, olddn, newdn, merge=False): # Add company abbr if not provided @@ -159,9 +166,19 @@ class Warehouse(NestedSet): from erpnext.setup.doctype.company.company import get_name_with_abbr return get_name_with_abbr(dn, self.company) - def get_account(self, warehouse): - return frappe.db.get_value("Account", {"account_type": "Stock", - "warehouse": warehouse, "company": self.company, "is_group": 0}) + def get_account(self, warehouse=None): + filters = { + "account_type": "Warehouse", + "company": self.company, + "is_group": self.is_group + } + + if warehouse: + filters.update({"warehouse": warehouse}) + else: + filters.update({"account_name": self.warehouse_name}) + + return frappe.db.get_value("Account", filters) def after_rename(self, olddn, newdn, merge=False): if merge: @@ -181,6 +198,38 @@ class Warehouse(NestedSet): frappe.db.set_value("Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock) frappe.db.auto_commit_on_many_writes = 0 + + def convert_to_group_or_ledger(self): + if self.is_group: + self.convert_to_ledger() + else: + self.convert_to_group() + + def convert_to_ledger(self): + if self.check_if_child_exists(): + frappe.throw(_("Warehouses with child nodes cannot be converted to ledger")) + elif self.check_if_sle_exists(): + throw(_("Warehouses with existing transaction can not be converted to ledger.")) + else: + account_name = self.get_account() + if account_name: + frappe.get_doc("Account", account_name).convert_group_to_ledger() + + self.is_group = 0 + self.save() + return 1 + + def convert_to_group(self): + if self.check_if_sle_exists(): + throw(_("Warehouses with existing transaction can not be converted to group.")) + else: + account_name = self.get_account(self.name) + if account_name: + frappe.get_doc("Account", account_name).convert_ledger_to_group() + + self.is_group = 1 + self.save() + return 1 @frappe.whitelist() def get_children(): @@ -195,7 +244,7 @@ def get_children(): parent = "" warehouses = frappe.db.sql("""select name as value, - if(is_group='Yes', 1, 0) as expandable + is_group as expandable from `tab{doctype}` where docstatus < 2 and ifnull(`{parent_field}`,'') = %s and `company` = %s @@ -229,3 +278,9 @@ def add_node(): }) doc.save() + +@frappe.whitelist() +def convert_to_group_or_ledger(): + args = frappe.form_dict + return frappe.get_doc("Warehouse", args.docname).convert_to_group_or_ledger() + \ No newline at end of file