Merge branch 'responsive' of git://github.com/webnotes/erpnext into responsive
diff --git a/accounts/DocType Mapper/Purchase Order-Purchase Invoice/Purchase Order-Purchase Invoice.txt b/accounts/DocType Mapper/Purchase Order-Purchase Invoice/Purchase Order-Purchase Invoice.txt
deleted file mode 100644
index 8c11d50..0000000
--- a/accounts/DocType Mapper/Purchase Order-Purchase Invoice/Purchase Order-Purchase Invoice.txt
+++ /dev/null
@@ -1,123 +0,0 @@
-[
- {
-  "creation": "2010-08-08 17:09:35", 
-  "docstatus": 0, 
-  "modified": "2013-07-03 18:00:40", 
-  "modified_by": "Administrator", 
-  "owner": "Administrator"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "name": "__common__", 
-  "parent": "Purchase Order-Purchase Invoice", 
-  "parentfield": "table_mapper_details", 
-  "parenttype": "DocType Mapper"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "name": "__common__", 
-  "parent": "Purchase Order-Purchase Invoice", 
-  "parentfield": "field_mapper_details", 
-  "parenttype": "DocType Mapper"
- }, 
- {
-  "doctype": "DocType Mapper", 
-  "from_doctype": "Purchase Order", 
-  "module": "Accounts", 
-  "name": "__common__", 
-  "ref_doc_submitted": 1, 
-  "to_doctype": "Purchase Invoice"
- }, 
- {
-  "doctype": "DocType Mapper", 
-  "name": "Purchase Order-Purchase Invoice"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "eval: (flt(obj.amount) - flt(obj.billed_amt)) / obj.purchase_rate", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "qty"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "purchase_rate", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "rate"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "eval: flt(obj.amount) - flt(obj.billed_amt)", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "amount"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "eval: (flt(obj.amount) - flt(obj.billed_amt)) / flt(obj.conversion_rate)", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "import_amount"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "parent", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "purchase_order"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "name", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "po_detail"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "naming_series", 
-  "map": "No", 
-  "match_id": 0, 
-  "to_field": "naming_series"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "total_tax", 
-  "map": "Yes", 
-  "match_id": 0, 
-  "to_field": "total_tax"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "conversion_rate", 
-  "map": "Yes", 
-  "match_id": 0, 
-  "to_field": "conversion_rate"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "from_table": "Purchase Order", 
-  "match_id": 0, 
-  "to_table": "Purchase Invoice", 
-  "validation_logic": "docstatus =1"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "from_field": "po_details", 
-  "from_table": "Purchase Order Item", 
-  "match_id": 1, 
-  "to_field": "entries", 
-  "to_table": "Purchase Invoice Item", 
-  "validation_logic": "ifnull(billed_amt,0)  < amount and docstatus = 1"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "from_field": "purchase_tax_details", 
-  "from_table": "Purchase Taxes and Charges", 
-  "match_id": 2, 
-  "to_field": "purchase_tax_details", 
-  "to_table": "Purchase Taxes and Charges", 
-  "validation_logic": "docstatus =1"
- }
-]
\ No newline at end of file
diff --git a/accounts/DocType Mapper/Purchase Receipt-Purchase Invoice/Purchase Receipt-Purchase Invoice.txt b/accounts/DocType Mapper/Purchase Receipt-Purchase Invoice/Purchase Receipt-Purchase Invoice.txt
deleted file mode 100644
index 99c74ac..0000000
--- a/accounts/DocType Mapper/Purchase Receipt-Purchase Invoice/Purchase Receipt-Purchase Invoice.txt
+++ /dev/null
@@ -1,144 +0,0 @@
-[
- {
-  "creation": "2010-08-08 17:09:35", 
-  "docstatus": 0, 
-  "modified": "2013-07-03 17:56:30", 
-  "modified_by": "Administrator", 
-  "owner": "Administrator"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "name": "__common__", 
-  "parent": "Purchase Receipt-Purchase Invoice", 
-  "parentfield": "table_mapper_details", 
-  "parenttype": "DocType Mapper"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "name": "__common__", 
-  "parent": "Purchase Receipt-Purchase Invoice", 
-  "parentfield": "field_mapper_details", 
-  "parenttype": "DocType Mapper"
- }, 
- {
-  "doctype": "DocType Mapper", 
-  "from_doctype": "Purchase Receipt", 
-  "module": "Accounts", 
-  "name": "__common__", 
-  "ref_doc_submitted": 1, 
-  "to_doctype": "Purchase Invoice"
- }, 
- {
-  "doctype": "DocType Mapper", 
-  "name": "Purchase Receipt-Purchase Invoice"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "eval: (flt(obj.amount) - flt(obj.billed_amt)) / obj.purchase_rate", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "qty"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "purchase_rate", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "rate"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "eval: flt(obj.amount) - flt(obj.billed_amt)", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "amount"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "eval: (flt(obj.amount) - flt(obj.billed_amt)) / flt(obj.conversion_rate)", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "import_amount"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "parent", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "purchase_receipt"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "prevdoc_docname", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "purchase_order"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "name", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "pr_detail"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "prevdoc_detail_docname", 
-  "map": "Yes", 
-  "match_id": 1, 
-  "to_field": "po_detail"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "naming_series", 
-  "map": "No", 
-  "match_id": 0, 
-  "to_field": "naming_series"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "net_total", 
-  "map": "Yes", 
-  "match_id": 0, 
-  "to_field": "net_total"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "grand_total", 
-  "map": "Yes", 
-  "match_id": 0, 
-  "to_field": "grand_total"
- }, 
- {
-  "doctype": "Field Mapper Detail", 
-  "from_field": "total_tax", 
-  "map": "Yes", 
-  "match_id": 0, 
-  "to_field": "total_tax"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "from_field": "purchase_receipt_details", 
-  "from_table": "Purchase Receipt Item", 
-  "match_id": 1, 
-  "to_field": "entries", 
-  "to_table": "Purchase Invoice Item", 
-  "validation_logic": "ifnull(billed_amt,0)  < amount"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "from_table": "Purchase Receipt", 
-  "match_id": 0, 
-  "to_table": "Purchase Invoice", 
-  "validation_logic": "docstatus=1"
- }, 
- {
-  "doctype": "Table Mapper Detail", 
-  "from_field": "purchase_tax_details", 
-  "from_table": "Purchase Taxes and Charges", 
-  "match_id": 2, 
-  "to_field": "purchase_tax_details", 
-  "to_table": "Purchase Taxes and Charges", 
-  "validation_logic": "docstatus=1"
- }
-]
\ No newline at end of file
diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py
index 2d49f0d..98808f4 100644
--- a/accounts/doctype/account/account.py
+++ b/accounts/doctype/account/account.py
@@ -18,7 +18,7 @@
 import webnotes
 
 from webnotes.utils import flt, fmt_money
-from webnotes import msgprint
+from webnotes import msgprint, _
 
 sql = webnotes.conn.sql
 get_value = webnotes.conn.get_value
@@ -196,10 +196,23 @@
 
 		if parts[-1].lower() != company_abbr.lower():
 			parts.append(company_abbr)
-
+		
 		# rename account name
-		account_name = " - ".join(parts[:-1])
-		sql("update `tabAccount` set account_name = %s where name = %s", (account_name, old))
+		new_account_name = " - ".join(parts[:-1])
+		sql("update `tabAccount` set account_name = %s where name = %s", (new_account_name, old))
+		
+		if merge:
+			new_name = " - ".join(parts)
+			val = list(webnotes.conn.get_value("Account", new_name, 
+				["group_or_ledger", "debit_or_credit", "is_pl_account"]))
+			
+			if val != [self.doc.group_or_ledger, self.doc.debit_or_credit, self.doc.is_pl_account]:
+				msgprint(_("""Merging is only possible if following \
+					properties are same in both records.
+					Group or Ledger, Debit or Credit, Is PL Account"""), raise_exception=1)
+
+			from webnotes.utils.nestedset import rebuild_tree
+			rebuild_tree("Account", "parent_account")
 
 		return " - ".join(parts)
 
diff --git a/accounts/doctype/accounts_settings/accounts_settings.py b/accounts/doctype/accounts_settings/accounts_settings.py
index 4b58048..b548936 100644
--- a/accounts/doctype/accounts_settings/accounts_settings.py
+++ b/accounts/doctype/accounts_settings/accounts_settings.py
@@ -2,6 +2,7 @@
 
 from __future__ import unicode_literals
 import webnotes
+from webnotes.utils import cint
 
 class DocType:
 	def __init__(self, d, dl):
diff --git a/accounts/doctype/cost_center/cost_center.py b/accounts/doctype/cost_center/cost_center.py
index e405b4d..8be14c7 100644
--- a/accounts/doctype/cost_center/cost_center.py
+++ b/accounts/doctype/cost_center/cost_center.py
@@ -98,5 +98,7 @@
 		cost_center_name = " - ".join(parts[:-1])
 		webnotes.conn.sql("update `tabCost Center` set cost_center_name = %s where name = %s", \
 			(cost_center_name, old))
+			
+		super(DocType, self).on_rename(new, old, merge, "group_or_ledger")
 
 		return " - ".join(parts)	
diff --git a/accounts/report/purchase_register/purchase_register.py b/accounts/report/purchase_register/purchase_register.py
index 655cf8c..0fbe451 100644
--- a/accounts/report/purchase_register/purchase_register.py
+++ b/accounts/report/purchase_register/purchase_register.py
@@ -58,7 +58,7 @@
 			row.append(invoice_tax_map.get(inv.name, {}).get(tax_acc))
 
 		# total tax, grand total, outstanding amount & rounded total
-		row += [inv.other_charges_total, inv.grand_total, flt(inv.grand_total, 2), \
+		row += [inv.total_tax, inv.grand_total, flt(inv.grand_total, 2), \
 			inv.outstanding_amount]
 		data.append(row)
 	
@@ -164,4 +164,4 @@
 		where name in (%s)""" % ", ".join(["%s"]*len(accounts)), tuple(accounts), as_dict=1):
 			account_map[acc.name] = acc.parent_account
 						
-	return account_map	
\ No newline at end of file
+	return account_map	
diff --git a/selling/doctype/quotation/quotation.py b/selling/doctype/quotation/quotation.py
index 1adce20..7e4ebe0 100644
--- a/selling/doctype/quotation/quotation.py
+++ b/selling/doctype/quotation/quotation.py
@@ -254,9 +254,6 @@
 @webnotes.whitelist()
 def make_sales_order(source_name, target_doclist=None):
 	from webnotes.model.mapper import get_mapped_doclist
-	
-	if target_doclist:
-		target_doclist = json.loads(target_doclist)
 
 	doclist = get_mapped_doclist("Quotation", source_name, {
 			"Quotation": {
diff --git a/setup/doctype/setup_control/setup_control.py b/setup/doctype/setup_control/setup_control.py
index c481edf..a4af7f8 100644
--- a/setup/doctype/setup_control/setup_control.py
+++ b/setup/doctype/setup_control/setup_control.py
@@ -251,7 +251,7 @@
 	country = webnotes.conn.get_value("Control Panel", None, "country")
 	root_territory = get_root_of("Territory")
 	for name in (country, "Rest Of The World"):
-		if not webnotes.conn.exists("Territory", name):
+		if name and not webnotes.conn.exists("Territory", name):
 			webnotes.bean({
 				"doctype": "Territory",
 				"territory_name": name,
diff --git a/setup/utils.py b/setup/utils.py
index 04c4527..c343ed1 100644
--- a/setup/utils.py
+++ b/setup/utils.py
@@ -32,14 +32,15 @@
 def get_root_of(doctype):
 	"""Get root element of a DocType with a tree structure"""
 	result = webnotes.conn.sql_list("""select name from `tab%s` 
-		where lft=1 and rgt=(select max(rgt) from `tab%s`)""" % (doctype, doctype))
+		where lft=1 and rgt=(select max(rgt) from `tab%s` where docstatus < 2)""" % 
+		(doctype, doctype))
 	return result[0] if result else None
 	
 def get_ancestors_of(doctype, name):
 	"""Get ancestor elements of a DocType with a tree structure"""
 	lft, rgt = webnotes.conn.get_value(doctype, name, ["lft", "rgt"])
 	result = webnotes.conn.sql_list("""select name from `tab%s` 
-		where lft<%s and rgt>%s""" % (doctype, "%s", "%s"), (lft, rgt))
+		where lft<%s and rgt>%s and docstatus < 2""" % (doctype, "%s", "%s"), (lft, rgt))
 	return result or None
 
 @webnotes.whitelist()
diff --git a/stock/doctype/item/item.py b/stock/doctype/item/item.py
index dc4fde8..85bb3d1 100644
--- a/stock/doctype/item/item.py
+++ b/stock/doctype/item/item.py
@@ -287,8 +287,14 @@
 			from webnotes.webutils import clear_cache
 			clear_cache(self.doc.page_name)
 
-	def on_rename(self,newdn,olddn, merge=False):
+	def on_rename(self, newdn, olddn, merge=False):
 		webnotes.conn.sql("update tabItem set item_code = %s where name = %s", (newdn, olddn))
 		if self.doc.page_name:
 			from webnotes.webutils import clear_cache
-			clear_cache(self.doc.page_name)
\ No newline at end of file
+			clear_cache(self.doc.page_name)
+			
+		if merge:
+			from stock.stock_ledger import update_entries_after
+			for wh in webnotes.conn.sql("""select warehouse from `tabBin` 
+				where item_code=%s""", newdn):
+					update_entries_after({"item_code": newdn, "warehouse": wh})
diff --git a/stock/doctype/material_request/material_request.js b/stock/doctype/material_request/material_request.js
index 025f7b5..3446b02 100644
--- a/stock/doctype/material_request/material_request.js
+++ b/stock/doctype/material_request/material_request.js
@@ -102,10 +102,11 @@
 };
 
 cur_frm.cscript['Make Purchase Order'] = function() {
-	cur_frm.map([
-		["Material Request", "Purchase Order"], 
-		["Material Request Item", "Purchase Order Item"]]);
-};
+	wn.model.open_mapped_doc({
+		method: "stock.doctype.material_request.material_request.make_purchase_order",
+		source_name: cur_frm.doc.name
+	})
+}
 
 cur_frm.cscript.make_supplier_quotation = function() {
 	cur_frm.map([
diff --git a/stock/doctype/material_request/material_request.py b/stock/doctype/material_request/material_request.py
index dfed6c4..269c4c5 100644
--- a/stock/doctype/material_request/material_request.py
+++ b/stock/doctype/material_request/material_request.py
@@ -240,3 +240,35 @@
 			"indented_qty": (se_detail.docstatus==2 and 1 or -1) * add_indented_qty,
 			"posting_date": controller.doc.posting_date,
 		})
+
+
+
+@webnotes.whitelist()
+def make_purchase_order(source_name, target_doclist=None):
+	from webnotes.model.mapper import get_mapped_doclist
+
+	def update_item(obj, target):
+		target.conversion_factor = 1
+		target.qty = flt(obj.qty) - flt(obj.ordered_qty)
+
+	doclist = get_mapped_doclist("Material Request", source_name, 	{
+		"Material Request": {
+			"doctype": "Purchase Order", 
+			"validation": {
+				"docstatus": ["=", 1],
+				"material_request_type": ["=", "Purchase"]
+			}
+		}, 
+		"Material Request Item": {
+			"doctype": "Purchase Order Item", 
+			"field_map": {
+				"name": "prevdoc_detail_docname", 
+				"parent": "prevdoc_docname", 
+				"parenttype": "prevdoc_doctype", 
+				"uom": "stock_uom"
+			},
+			"postprocess": update_item
+		}
+	}, target_doclist)
+
+	return [d.fields for d in doclist]
\ No newline at end of file
diff --git a/stock/doctype/material_request/test_material_request.py b/stock/doctype/material_request/test_material_request.py
index f5dbb52..f26773b 100644
--- a/stock/doctype/material_request/test_material_request.py
+++ b/stock/doctype/material_request/test_material_request.py
@@ -6,6 +6,21 @@
 from webnotes.utils import flt
 
 class TestMaterialRequest(unittest.TestCase):
+	def test_make_purchase_order(self):
+		from stock.doctype.material_request.material_request import make_purchase_order
+
+		mr = webnotes.bean(copy=test_records[0]).insert()
+
+		self.assertRaises(webnotes.ValidationError, make_purchase_order, 
+			mr.doc.name)
+
+		mr = webnotes.bean("Material Request", mr.doc.name)
+		mr.submit()
+		po = make_purchase_order(mr.doc.name)
+		
+		self.assertEquals(po[0]["doctype"], "Purchase Order")
+		self.assertEquals(len(po), len(mr.doclist))
+	
 	def _test_expected(self, doclist, expected_values):
 		for i, expected in enumerate(expected_values):
 			for fieldname, val in expected.items():
diff --git a/stock/doctype/warehouse/warehouse.py b/stock/doctype/warehouse/warehouse.py
index 758bd31..7eb407c 100644
--- a/stock/doctype/warehouse/warehouse.py
+++ b/stock/doctype/warehouse/warehouse.py
@@ -204,3 +204,9 @@
 		else:
 			sql("delete from `tabStock Ledger Entry` where warehouse = %s", self.doc.name)
 
+	def on_rename(self, newdn, olddn, merge=False):
+		if merge:
+			from stock.stock_ledger import update_entries_after
+			for item_code in webnotes.conn.sql("""select item_code from `tabBin` 
+				where warehouse=%s""", newdn):
+					update_entries_after({"item_code": item_code, "warehouse": newdn})