BOM replace tool
diff --git a/production/doctype/bom_replace_tool/__init__.py b/production/doctype/bom_replace_tool/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/production/doctype/bom_replace_tool/__init__.py
diff --git a/production/doctype/bom_replace_tool/bom_replace_tool.js b/production/doctype/bom_replace_tool/bom_replace_tool.js
new file mode 100644
index 0000000..60e8884
--- /dev/null
+++ b/production/doctype/bom_replace_tool/bom_replace_tool.js
@@ -0,0 +1,25 @@
+// ERPNext - web based ERP (http://erpnext.com)
+// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
+// 
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// 
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+cur_frm.set_query("current_bom", function(doc) {
+	return erpnext.queries.bom({name: "!" + doc.new_bom});
+});
+
+
+cur_frm.set_query("new_bom", function(doc) {
+	return erpnext.queries.bom({name: "!" + doc.current_bom});
+});
\ No newline at end of file
diff --git a/production/doctype/bom_replace_tool/bom_replace_tool.py b/production/doctype/bom_replace_tool/bom_replace_tool.py
new file mode 100644
index 0000000..d9d9598
--- /dev/null
+++ b/production/doctype/bom_replace_tool/bom_replace_tool.py
@@ -0,0 +1,56 @@
+# ERPNext - web based ERP (http://erpnext.com)
+# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
+# 
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import unicode_literals
+import webnotes
+from webnotes.utils import cstr, flt
+from webnotes.model.code import get_obj
+from webnotes import msgprint
+	
+class DocType:
+	def __init__( self, doc, doclist=[]):
+		self.doc = doc
+		self.doclist = doclist
+		
+	def replace_bom(self):
+		self.validate_bom()
+		self.update_new_bom()
+		bom_list = self.get_parent_boms()
+		for bom in bom_list:
+			bom_obj = get_obj("BOM", bom, with_children=1)
+			bom_obj.update_cost_by_traversing()
+			bom_obj.update_flat_bom_by_traversing()
+		
+	def validate_bom(self):
+		if cstr(self.doc.current_bom) == cstr(self.doc.new_bom):
+			msgprint("Current BOM and New BOM can not be same", raise_exception=1)
+	
+	def update_new_bom(self):
+		current_bom_unitcost = webnotes.conn.sql("""select total_cost/quantity 
+			from `tabBOM` where name = %s""", self.doc.current_bom)
+		current_bom_unitcost = current_bom_unitcost and flt(current_bom_unitcost[0][0]) or 0
+		webnotes.conn.sql("""update `tabBOM Item` set bom_no=%s, 
+			rate=%s, amount=qty*%s where bom_no = %s and docstatus < 2""", 
+			(self.doc.new_bom, current_bom_unitcost, current_bom_unitcost, self.doc.current_bom))
+			
+	def get_parent_boms(bom_no):
+		return [d[0] for d in webnotes.conn.sql("""select distinct parent from
+			`tabBOM Item` where ifnull(bom_no, '')=%s and docstatus < 2""", bom_no)]
+	
+	def get_parent_boms(self):
+		return [d[0] for d in webnotes.conn.sql("""select distinct parent 
+			from `tabBOM Item` where ifnull(bom_no, '') = %s and docstatus < 2""",
+			self.doc.new_bom)]
\ No newline at end of file
diff --git a/production/doctype/bom_replace_tool/bom_replace_tool.txt b/production/doctype/bom_replace_tool/bom_replace_tool.txt
new file mode 100644
index 0000000..bb2c56b
--- /dev/null
+++ b/production/doctype/bom_replace_tool/bom_replace_tool.txt
@@ -0,0 +1,78 @@
+[
+ {
+  "owner": "Administrator", 
+  "docstatus": 0, 
+  "creation": "2012-12-06 12:10:10", 
+  "modified_by": "Administrator", 
+  "modified": "2012-12-06 12:32:22"
+ }, 
+ {
+  "in_create": 1, 
+  "allow_print": 1, 
+  "module": "Production", 
+  "document_type": "Other", 
+  "description": "Replace a particular BOM in all other BOMs where it is used. It will replace the old BOM link, update cost and regenerate \"BOM Explosion Item\" table as per new BOM", 
+  "read_only": 1, 
+  "allow_email": 1, 
+  "hide_heading": 1, 
+  "issingle": 1, 
+  "name": "__common__", 
+  "doctype": "DocType", 
+  "hide_toolbar": 1, 
+  "allow_copy": 1
+ }, 
+ {
+  "name": "__common__", 
+  "parent": "BOM Replace Tool", 
+  "doctype": "DocField", 
+  "parenttype": "DocType", 
+  "permlevel": 0, 
+  "parentfield": "fields"
+ }, 
+ {
+  "parent": "BOM Replace Tool", 
+  "read": 1, 
+  "name": "__common__", 
+  "create": 1, 
+  "doctype": "DocPerm", 
+  "write": 1, 
+  "parenttype": "DocType", 
+  "role": "Administrator", 
+  "permlevel": 0, 
+  "parentfield": "permissions"
+ }, 
+ {
+  "name": "BOM Replace Tool", 
+  "doctype": "DocType"
+ }, 
+ {
+  "description": "The BOM which will be replaced", 
+  "colour": "White:FFF", 
+  "doctype": "DocField", 
+  "label": "Current BOM", 
+  "fieldname": "current_bom", 
+  "fieldtype": "Link", 
+  "reqd": 1, 
+  "options": "BOM"
+ }, 
+ {
+  "description": "The new BOM after replacement", 
+  "colour": "White:FFF", 
+  "doctype": "DocField", 
+  "label": "New BOM", 
+  "fieldname": "new_bom", 
+  "fieldtype": "Link", 
+  "reqd": 1, 
+  "options": "BOM"
+ }, 
+ {
+  "doctype": "DocField", 
+  "label": "Replace", 
+  "fieldname": "replace", 
+  "fieldtype": "Button", 
+  "options": "replace_bom"
+ }, 
+ {
+  "doctype": "DocPerm"
+ }
+]
\ No newline at end of file