Merge branch 'develop' into staging
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index 375d85d..face5ed 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -12,8 +12,8 @@
 from erpnext.hr.doctype.employee_loan.employee_loan import update_disbursement_status
 
 class JournalEntry(AccountsController):
-	def __init__(self, arg1, arg2=None):
-		super(JournalEntry, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(JournalEntry, self).__init__(*args, **kwargs)
 
 	def get_feed(self):
 		return self.voucher_type
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index a46c4b9..78c5682 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -22,8 +22,8 @@
 }
 
 class PurchaseInvoice(BuyingController):
-	def __init__(self, arg1, arg2=None):
-		super(PurchaseInvoice, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(PurchaseInvoice, self).__init__(*args, **kwargs)
 		self.status_updater = [{
 			'source_dt': 'Purchase Invoice Item',
 			'target_dt': 'Purchase Order Item',
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 11d1825..be01184 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -520,6 +520,24 @@
 			};
 		});
 	},
+	//When multiple companies are set up. in case company name is changed set default company address
+	company:function(frm){
+		if (frm.doc.company)
+		{
+			frappe.call({
+				method:"frappe.contacts.doctype.address.address.get_default_address",
+				args:{ doctype:'Company',name:frm.doc.company},
+				callback: function(r){
+					if (r.message){
+						frm.set_value("company_address",r.message)
+					}
+					else {
+						frm.set_value("company_address","")
+					}
+				}
+			})
+		}
+	},
 
 	project: function(frm){
 		frm.call({
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 7a787c4..6ab6148 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -27,8 +27,8 @@
 }
 
 class SalesInvoice(SellingController):
-	def __init__(self, arg1, arg2=None):
-		super(SalesInvoice, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(SalesInvoice, self).__init__(*args, **kwargs)
 		self.status_updater = [{
 			'source_dt': 'Sales Invoice Item',
 			'target_field': 'billed_amt',
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 4dae78c..900a6e9 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -1084,7 +1084,7 @@
 		si.items[0].price_list_rate = price_list_rate
 		si.items[0].margin_type = 'Percentage'
 		si.items[0].margin_rate_or_amount = 25
-		si.insert()
+		si.save()
 		self.assertEqual(si.get("items")[0].rate, flt((price_list_rate*25)/100 + price_list_rate))
 
 	def test_outstanding_amount_after_advance_jv_cancelation(self):
diff --git a/erpnext/accounts/doctype/subscription/subscription.json b/erpnext/accounts/doctype/subscription/subscription.json
index fe41b42..dfdcbec 100644
--- a/erpnext/accounts/doctype/subscription/subscription.json
+++ b/erpnext/accounts/doctype/subscription/subscription.json
@@ -498,7 +498,7 @@
    "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
-   "collapsible": 0, 
+   "collapsible": 1, 
    "columns": 0, 
    "fieldname": "notification", 
    "fieldtype": "Section Break", 
@@ -560,6 +560,38 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "depends_on": "eval: doc.notify_by_email", 
+   "description": "To add dynamic subject, use jinja tags like\n\n<div><pre><code>New {{ doc.doctype }} #{{ doc.name }}</code></pre></div>", 
+   "fieldname": "subject", 
+   "fieldtype": "Data", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Subject", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_17", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -652,6 +684,69 @@
    "bold": 0, 
    "collapsible": 1, 
    "columns": 0, 
+   "depends_on": "eval:doc.notify_by_email", 
+   "fieldname": "section_break_20", 
+   "fieldtype": "Section Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Message", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "default": "Please find attached {{ doc.doctype }} #{{ doc.name }}", 
+   "fieldname": "message", 
+   "fieldtype": "Text", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Message", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 1, 
+   "columns": 0, 
+   "depends_on": "eval: !doc.__islocal", 
    "fieldname": "section_break_16", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
diff --git a/erpnext/accounts/doctype/subscription/subscription.py b/erpnext/accounts/doctype/subscription/subscription.py
index 0e14e84..b40169a 100644
--- a/erpnext/accounts/doctype/subscription/subscription.py
+++ b/erpnext/accounts/doctype/subscription/subscription.py
@@ -7,6 +7,7 @@
 import calendar
 from frappe import _
 from frappe.desk.form import assign_to
+from frappe.utils.jinja import validate_template
 from dateutil.relativedelta import relativedelta
 from frappe.utils.user import get_system_managers
 from frappe.utils import cstr, getdate, split_emails, add_days, today, get_last_day, get_first_day
@@ -20,6 +21,9 @@
 		self.validate_next_schedule_date()
 		self.validate_email_id()
 
+		validate_template(self.subject or "")
+		validate_template(self.message or "")
+
 	def before_submit(self):
 		self.set_next_schedule_date()
 
@@ -138,19 +142,19 @@
 def create_documents(data, schedule_date):
 	try:
 		doc = make_new_document(data, schedule_date)
-		if doc.from_date:
+		if getattr(doc, "from_date", None):
 			update_subscription_period(data, doc)
 
 		if data.notify_by_email and data.recipients:
 			print_format = data.print_format or "Standard"
-			send_notification(doc, print_format, data.recipients)
+			send_notification(doc, data, print_format=print_format)
 
 		frappe.db.commit()
 	except Exception:
 		frappe.db.rollback()
 		frappe.db.begin()
 		frappe.log_error(frappe.get_traceback())
-		disabled_subscription(data)
+		disable_subscription(data)
 		frappe.db.commit()
 		if data.reference_document and not frappe.flags.in_test:
 			notify_error_to_user(data)
@@ -162,7 +166,7 @@
 	frappe.db.set_value('Subscription', data.name, 'from_date', from_date)
 	frappe.db.set_value('Subscription', data.name, 'to_date', to_date)
 
-def disabled_subscription(data):
+def disable_subscription(data):
 	subscription = frappe.get_doc('Subscription', data.name)
 	subscription.db_set('disabled', 1)
 
@@ -225,14 +229,25 @@
 
 	return dt
 
-def send_notification(new_rv, print_format='Standard', recipients=None):
+def send_notification(new_rv, subscription_doc, print_format='Standard'):
 	"""Notify concerned persons about recurring document generation"""
 	print_format = print_format
 
-	frappe.sendmail(recipients,
-		subject=  _("New {0}: #{1}").format(new_rv.doctype, new_rv.name),
-		message = _("Please find attached {0} #{1}").format(new_rv.doctype, new_rv.name),
-		attachments = [frappe.attach_print(new_rv.doctype, new_rv.name, file_name=new_rv.name, print_format=print_format)])
+	if not subscription_doc.subject:
+		subject = _("New {0}: #{1}").format(new_rv.doctype, new_rv.name)
+	elif "{" in subscription_doc.subject:
+		subject = frappe.render_template(subscription_doc.subject, {'doc': new_rv})
+
+	if not subscription_doc.message:
+		message = _("Please find attached {0} #{1}").format(new_rv.doctype, new_rv.name)
+	elif "{" in subscription_doc.message:
+		message = frappe.render_template(subscription_doc.message, {'doc': new_rv})
+
+	attachments = [frappe.attach_print(new_rv.doctype, new_rv.name,
+		file_name=new_rv.name, print_format=print_format)]
+
+	frappe.sendmail(subscription_doc.recipients,
+		subject=subject, message=message, attachments=attachments)
 
 def notify_errors(doc, doctype, party, owner, name):
 	recipients = get_system_managers(only_name=True)
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index 69f40f8..bcec0a2 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -68,7 +68,8 @@
 	billing_address_field = "customer_address" if party_type == "Lead" \
 		else party_type.lower() + "_address"
 	out[billing_address_field] = get_default_address(party_type, party.name)
-	out.update(get_fetch_values(doctype, billing_address_field, out[billing_address_field]))
+	if doctype:
+		out.update(get_fetch_values(doctype, billing_address_field, out[billing_address_field]))
 
 	# address display
 	out.address_display = get_address_display(out[billing_address_field])
@@ -77,7 +78,8 @@
 	if party_type in ["Customer", "Lead"]:
 		out.shipping_address_name = get_default_address(party_type, party.name, 'is_shipping_address')
 		out.shipping_address = get_address_display(out["shipping_address_name"])
-		out.update(get_fetch_values(doctype, 'shipping_address_name', out.shipping_address_name))
+		if doctype:
+			out.update(get_fetch_values(doctype, 'shipping_address_name', out.shipping_address_name))
 
 	if doctype and doctype in ['Delivery Note', 'Sales Invoice']:
 		out.update(get_company_address(company))
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index a51246b..8134e7e 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -1,3 +1,4 @@
+
 // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 // License: GNU General Public License v3. See license.txt
 
@@ -24,6 +25,18 @@
 	},
 });
 
+frappe.ui.form.on("Purchase Order Item", {
+	item_code: function(frm) {
+		frappe.call({
+			method: "get_last_purchase_rate",
+			doc: frm.doc,
+			callback: function(r, rt) {
+				frm.trigger('calculate_taxes_and_totals');
+			}
+		})
+	}
+});
+
 erpnext.buying.PurchaseOrderController = erpnext.buying.BuyingController.extend({
 	refresh: function(doc, cdt, cdn) {
 		var me = this;
@@ -214,17 +227,6 @@
 
 	delivered_by_supplier: function(){
 		cur_frm.cscript.update_status('Deliver', 'Delivered')
-	},
-
-	get_last_purchase_rate: function() {
-		frappe.call({
-			"method": "get_last_purchase_rate",
-			"doc": cur_frm.doc,
-			callback: function(r, rt) {
-				cur_frm.dirty();
-				cur_frm.cscript.calculate_taxes_and_totals();
-			}
-		})
 	}
 
 });
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index 919707c..07a80b8 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -1213,37 +1213,6 @@
    "collapsible": 0, 
    "columns": 0, 
    "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", 
-   "fieldname": "get_last_purchase_rate", 
-   "fieldtype": "Button", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "ignore_xss_filter": 0, 
-   "in_filter": 0, 
-   "in_global_search": 0, 
-   "in_list_view": 0, 
-   "in_standard_filter": 0, 
-   "label": "Get last purchase rate", 
-   "length": 0, 
-   "no_copy": 0, 
-   "permlevel": 0, 
-   "precision": "", 
-   "print_hide": 0, 
-   "print_hide_if_no_value": 0, 
-   "read_only": 0, 
-   "remember_last_selected_value": 0, 
-   "report_hide": 0, 
-   "reqd": 0, 
-   "search_index": 0, 
-   "set_only_once": 0, 
-   "unique": 0
-  }, 
-  {
-   "allow_bulk_edit": 0, 
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "columns": 0, 
-   "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", 
    "fieldname": "link_to_mrs", 
    "fieldtype": "Button", 
    "hidden": 0, 
@@ -3458,7 +3427,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-09-19 11:22:30.190589", 
+ "modified": "2017-09-22 16:11:49.856808", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order", 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 56f3059..36cef43 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -20,8 +20,8 @@
 }
 
 class PurchaseOrder(BuyingController):
-	def __init__(self, arg1, arg2=None):
-		super(PurchaseOrder, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(PurchaseOrder, self).__init__(*args, **kwargs)
 		self.status_updater = [{
 			'source_dt': 'Purchase Order Item',
 			'target_dt': 'Material Request Item',
@@ -116,14 +116,13 @@
 					d.discount_percentage = last_purchase_details['discount_percentage']
 					d.base_rate = last_purchase_details['base_rate'] * (flt(d.conversion_factor) or 1.0)
 					d.price_list_rate = d.base_price_list_rate / conversion_rate
-					d.rate = d.base_rate / conversion_rate
+					d.last_purchase_rate = d.base_rate / conversion_rate
 				else:
-					msgprint(_("Last purchase rate not found"))
 
 					item_last_purchase_rate = frappe.db.get_value("Item", d.item_code, "last_purchase_rate")
 					if item_last_purchase_rate:
 						d.base_price_list_rate = d.base_rate = d.price_list_rate \
-							= d.rate = item_last_purchase_rate
+							= d.last_purchase_rate = item_last_purchase_rate
 
 	# Check for Closed status
 	def check_for_closed_status(self):
diff --git a/erpnext/buying/doctype/purchase_order/tests/test_purchase_order_with_last_purchase_rate.js b/erpnext/buying/doctype/purchase_order/tests/test_purchase_order_with_last_purchase_rate.js
new file mode 100644
index 0000000..d19f017
--- /dev/null
+++ b/erpnext/buying/doctype/purchase_order/tests/test_purchase_order_with_last_purchase_rate.js
@@ -0,0 +1,99 @@
+QUnit.module('Buying');
+
+QUnit.test("test: purchase order with last purchase rate", function(assert) {
+	assert.expect(5);
+	let done = assert.async();
+
+	frappe.run_serially([
+		() => {
+			return frappe.tests.make('Purchase Order', [
+				{supplier: 'Test Supplier'},
+				{is_subcontracted: 'No'},
+				{currency: 'INR'},
+				{items: [
+					[
+						{"item_code": 'Test Product 4'},
+						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
+						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
+						{"qty": 1},
+						{"rate": 800},
+						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
+					],
+					[
+						{"item_code": 'Test Product 1'},
+						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
+						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
+						{"qty": 1},
+						{"rate": 400},
+						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
+					]
+				]}
+			]);
+		},
+
+		() => {
+			// Get item details
+			assert.ok(cur_frm.doc.items[0].item_name == 'Test Product 4', "Item 1 name correct");
+			assert.ok(cur_frm.doc.items[1].item_name == 'Test Product 1', "Item 2 name correct");
+		},
+
+		() => frappe.timeout(1),
+
+		() => frappe.tests.click_button('Submit'),
+		() => frappe.tests.click_button('Yes'),
+		() => frappe.timeout(3),
+
+		() => frappe.tests.click_button('Close'),
+		() => frappe.timeout(1),
+
+		() => {
+			return frappe.tests.make('Purchase Order', [
+				{supplier: 'Test Supplier'},
+				{is_subcontracted: 'No'},
+				{currency: 'INR'},
+				{items: [
+					[
+						{"item_code": 'Test Product 4'},
+						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
+						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
+						{"qty": 1},
+						{"rate": 600},
+						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
+					],
+					[
+						{"item_code": 'Test Product 1'},
+						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
+						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
+						{"qty": 1},
+						{"rate": 200},
+						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
+					]
+				]}
+			]);
+		},
+
+		() => frappe.timeout(2),
+
+		// Get the last purchase rate of items
+		() => {
+			assert.ok(cur_frm.doc.items[0].last_purchase_rate == 800, "Last purchase rate of item 1 correct");
+		},
+		() => {
+			assert.ok(cur_frm.doc.items[1].last_purchase_rate == 400, "Last purchase rate of item 2 correct");
+		},
+
+		() => frappe.tests.click_button('Submit'),
+		() => frappe.tests.click_button('Yes'),
+		() => frappe.timeout(3),
+
+		() => frappe.tests.click_button('Close'),
+
+		() => frappe.timeout(1),
+
+		() => {
+			assert.ok(cur_frm.doc.status == 'To Receive and Bill', "Submitted successfully");
+		},
+
+		() => done()
+	]);
+});
\ No newline at end of file
diff --git a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
index 2dd7b6c..1ddce62 100755
--- a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
+++ b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
@@ -661,6 +661,37 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "last_purchase_rate", 
+   "fieldtype": "Currency", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Last Purchase Rate", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "currency", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "base_price_list_rate", 
    "fieldtype": "Currency", 
    "hidden": 0, 
@@ -1714,7 +1745,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-08-02 22:15:47.411235", 
+ "modified": "2017-09-22 16:47:08.783546", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order Item", 
diff --git a/erpnext/config/desktop.py b/erpnext/config/desktop.py
index ef1ff10..8fb66b1 100644
--- a/erpnext/config/desktop.py
+++ b/erpnext/config/desktop.py
@@ -268,5 +268,13 @@
 			"icon": "octicon octicon-plus",
 			"type": "module",
 			"label": _("Healthcare")
-		}
+		},
+		{
+			"module_name": "Data Import Tool",
+			"color": "#7f8c8d",
+			"icon": "octicon octicon-circuit-board",
+			"type": "page",
+			"link": "data-import-tool",
+			"label": _("Data Import Tool")
+		},
 	]
diff --git a/erpnext/config/manufacturing.py b/erpnext/config/manufacturing.py
index 11711ad..086d61b 100644
--- a/erpnext/config/manufacturing.py
+++ b/erpnext/config/manufacturing.py
@@ -123,6 +123,12 @@
 					"is_query_report": True,
 					"name": "BOM Search",
 					"doctype": "BOM"
+				},
+				{
+					"type": "report",
+					"is_query_report": True,
+					"name": "BOM Stock Report",
+					"doctype": "BOM"
 				}
 			]
 		},
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index d04143d..4f49c77 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -15,8 +15,8 @@
 force_item_fields = ("item_group", "barcode", "brand", "stock_uom")
 
 class AccountsController(TransactionBase):
-	def __init__(self, arg1, arg2=None):
-		super(AccountsController, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(AccountsController, self).__init__(*args, **kwargs)
 
 	@property
 	def company_currency(self):
@@ -187,9 +187,6 @@
 								if stock_qty != len(get_serial_nos(item.get('serial_no'))):
 									item.set(fieldname, value)
 
-							elif fieldname == "conversion_factor" and not item.get("conversion_factor"):
-								item.set(fieldname, value)
-
 					if ret.get("pricing_rule"):
 						# if user changed the discount percentage then set user's discount percentage ?
 						item.set("discount_percentage", ret.get("discount_percentage"))
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 9cc0616..1f9051d 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -61,7 +61,7 @@
 
 		# set contact and address details for supplier, if they are not mentioned
 		if getattr(self, "supplier", None):
-			self.update_if_missing(get_party_details(self.supplier, party_type="Supplier", ignore_permissions=self.flags.ignore_permissions))
+			self.update_if_missing(get_party_details(self.supplier, party_type="Supplier", ignore_permissions=self.flags.ignore_permissions, doctype=self.doctype, company=self.company))
 
 		self.set_missing_item_details(for_validate)
 
diff --git a/erpnext/controllers/item_variant.py b/erpnext/controllers/item_variant.py
index cf2ae5f..513b97f 100644
--- a/erpnext/controllers/item_variant.py
+++ b/erpnext/controllers/item_variant.py
@@ -5,7 +5,7 @@
 import frappe
 from frappe import _
 from frappe.utils import cstr, flt
-import json
+import json, copy
 
 class ItemVariantExistsError(frappe.ValidationError): pass
 class InvalidItemAttributeValueError(frappe.ValidationError): pass
@@ -174,18 +174,30 @@
 
 	# copy non no-copy fields
 
-	exclude_fields = ["item_code", "item_name", "show_in_website"]
+	exclude_fields = ["naming_series", "item_code", "item_name", "show_in_website",
+		"show_variant_in_website", "opening_stock", "variant_of", "valuation_rate"]
 
 	if item.variant_based_on=='Manufacturer':
 		# don't copy manufacturer values if based on part no
 		exclude_fields += ['manufacturer', 'manufacturer_part_no']
 
 	allow_fields = [d.field_name for d in frappe.get_all("Variant Field", fields = ['field_name'])]
+	if "variant_based_on" not in allow_fields:
+		allow_fields.append("variant_based_on")
 	for field in item.meta.fields:
 		# "Table" is part of `no_value_field` but we shouldn't ignore tables
 		if (field.reqd or field.fieldname in allow_fields) and field.fieldname not in exclude_fields:
 			if variant.get(field.fieldname) != item.get(field.fieldname):
-				variant.set(field.fieldname, item.get(field.fieldname))
+				if field.fieldtype == "Table":
+					variant.set(field.fieldname, [])
+					for d in item.get(field.fieldname):
+						row = copy.deepcopy(d)
+						if row.get("name"):
+							row.name = None
+						variant.append(field.fieldname, row)
+				else:
+					variant.set(field.fieldname, item.get(field.fieldname))
+
 	variant.variant_of = item.name
 	variant.has_variants = 0
 	if not variant.description:
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index d881f18..c1028a5 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -49,7 +49,8 @@
 		if getattr(self, "customer", None):
 			from erpnext.accounts.party import _get_party_details
 			party_details = _get_party_details(self.customer,
-				ignore_permissions=self.flags.ignore_permissions)
+				ignore_permissions=self.flags.ignore_permissions,
+				doctype=self.doctype, company=self.company)
 			if not self.meta.get_field("sales_team"):
 				party_details.pop("sales_team")
 
diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py
index 4251cae..970fd57 100644
--- a/erpnext/crm/doctype/opportunity/opportunity.py
+++ b/erpnext/crm/doctype/opportunity/opportunity.py
@@ -42,10 +42,28 @@
 		if not self.with_items:
 			self.items = []
 
-
 	def make_new_lead_if_required(self):
 		"""Set lead against new opportunity"""
 		if not (self.lead or self.customer) and self.contact_email:
+			# check if customer is already created agains the self.contact_email
+			customer = frappe.db.sql("""select
+				distinct `tabDynamic Link`.link_name as customer
+				from 
+					`tabContact`,
+					`tabDynamic Link`
+				where `tabContact`.email_id='{0}'
+				and 
+					`tabContact`.name=`tabDynamic Link`.parent
+				and
+					ifnull(`tabDynamic Link`.link_name, '')<>'' 
+				and 
+					`tabDynamic Link`.link_doctype='Customer' 
+			""".format(self.contact_email), as_dict=True)
+			if customer and customer[0].customer:
+				self.customer = customer[0].customer
+				self.enquiry_from = "Customer"
+				return
+
 			lead_name = frappe.db.get_value("Lead", {"email_id": self.contact_email})
 			if not lead_name:
 				sender_name = get_fullname(self.contact_email)
diff --git a/erpnext/crm/doctype/opportunity/test_opportunity.py b/erpnext/crm/doctype/opportunity/test_opportunity.py
index 4cd20ea..61b583c 100644
--- a/erpnext/crm/doctype/opportunity/test_opportunity.py
+++ b/erpnext/crm/doctype/opportunity/test_opportunity.py
@@ -4,6 +4,7 @@
 
 import frappe
 from frappe.utils import today
+from erpnext.crm.doctype.lead.lead import make_customer
 from erpnext.crm.doctype.opportunity.opportunity import make_quotation
 import unittest
 
@@ -25,12 +26,45 @@
 		doc = frappe.get_doc('Opportunity', doc.name)
 		self.assertEquals(doc.status, "Quotation")
 
+	def test_make_new_lead_if_required(self):
+		args = {
+			"doctype": "Opportunity",
+			"contact_email":"new.opportunity@example.com",
+			"enquiry_type": "Sales",
+			"with_items": 0,
+			"transaction_date": today()
+		}
+		# new lead should be created against the new.opportunity@example.com
+		opp_doc = frappe.get_doc(args).insert(ignore_permissions=True)
+
+		self.assertTrue(opp_doc.lead)
+		self.assertEquals(opp_doc.enquiry_from, "Lead")
+		self.assertEquals(frappe.db.get_value("Lead", opp_doc.lead, "email_id"),
+			'new.opportunity@example.com')
+
+		# create new customer and create new contact against 'new.opportunity@example.com'
+		customer = make_customer(opp_doc.lead).insert(ignore_permissions=True)
+		frappe.get_doc({
+			"doctype": "Contact",
+			"email_id": "new.opportunity@example.com",
+			"first_name": "_Test Opportunity Customer",
+			"links": [{
+				"link_doctype": "Customer",
+				"link_name": customer.name
+			}]
+		}).insert(ignore_permissions=True)
+
+		opp_doc = frappe.get_doc(args).insert(ignore_permissions=True)
+		self.assertTrue(opp_doc.customer)
+		self.assertEquals(opp_doc.enquiry_from, "Customer")
+		self.assertEquals(opp_doc.customer, customer.name)
+
 def make_opportunity(**args):
 	args = frappe._dict(args)
 
 	opp_doc = frappe.get_doc({
 		"doctype": "Opportunity",
-		"enquiry_from": "Customer" or args.enquiry_from,
+		"enquiry_from": args.enquiry_from or "Customer",
 		"enquiry_type": "Sales",
 		"with_items": args.with_items or 0,
 		"transaction_date": today()
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 48ed741..a6dbd20 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -11,7 +11,7 @@
 app_license = "GNU General Public License (v3)"
 source_link = "https://github.com/frappe/erpnext"
 
-develop_version = '8.x.x-beta'
+develop_version = '9.x.x-develop'
 
 error_report_email = "support@erpnext.com"
 
@@ -47,7 +47,7 @@
 update_website_context = "erpnext.shopping_cart.utils.update_website_context"
 my_account_context = "erpnext.shopping_cart.utils.update_my_account_context"
 
-email_append_to = ["Job Applicant", "Opportunity", "Issue"]
+email_append_to = ["Job Applicant", "Lead", "Opportunity", "Issue"]
 
 calendars = ["Task", "Production Order", "Leave Application", "Sales Order", "Holiday List"]
 
diff --git a/erpnext/hr/doctype/employee/employee.json b/erpnext/hr/doctype/employee/employee.json
index 87f0537..227302c 100644
--- a/erpnext/hr/doctype/employee/employee.json
+++ b/erpnext/hr/doctype/employee/employee.json
@@ -149,7 +149,7 @@
    "in_filter": 0, 
    "in_global_search": 1, 
    "in_list_view": 1, 
-   "in_standard_filter": 0, 
+   "in_standard_filter": 1, 
    "label": "Full Name", 
    "length": 0, 
    "no_copy": 0, 
@@ -431,7 +431,7 @@
    "no_copy": 0, 
    "oldfieldname": "gender", 
    "oldfieldtype": "Select", 
-   "options": "Gender",
+   "options": "Gender", 
    "permlevel": 0, 
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
@@ -2432,7 +2432,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-06-13 14:29:13.694009", 
+ "modified": "2017-10-04 11:42:02.495731", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Employee", 
diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
index 91b5070..e3c61ed 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
+++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
@@ -30,12 +30,13 @@
 				frappe.throw(_("The selected BOMs are not for the same item"))
 
 	def update_new_bom(self):
-		current_bom_unitcost = frappe.db.sql("""select total_cost/quantity
-			from `tabBOM` where name = %s""", self.current_bom)
-		current_bom_unitcost = current_bom_unitcost and flt(current_bom_unitcost[0][0]) or 0
+		new_bom_unitcost = frappe.db.sql("""select total_cost/quantity
+			from `tabBOM` where name = %s""", self.new_bom)
+		new_bom_unitcost = flt(new_bom_unitcost[0][0]) if new_bom_unitcost else 0
+
 		frappe.db.sql("""update `tabBOM Item` set bom_no=%s,
 			rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2""",
-			(self.new_bom, current_bom_unitcost, current_bom_unitcost, self.current_bom))
+			(self.new_bom, new_bom_unitcost, new_bom_unitcost, self.current_bom))
 
 	def get_parent_boms(self):
 		return [d[0] for d in frappe.db.sql("""select distinct parent
diff --git a/erpnext/manufacturing/doctype/production_order/test_production_order.js b/erpnext/manufacturing/doctype/production_order/test_production_order.js
index 7ce67ba..32cc3ef 100644
--- a/erpnext/manufacturing/doctype/production_order/test_production_order.js
+++ b/erpnext/manufacturing/doctype/production_order/test_production_order.js
@@ -59,8 +59,6 @@
 		// Confirm the production order timesheet, save and submit it
 		() => frappe.click_link("TS-00"),
 		() => frappe.timeout(1),
-		() => frappe.click_button("Save"),
-		() => frappe.timeout(1),
 		() => frappe.click_button("Submit"),
 		() => frappe.timeout(1),
 		() => frappe.click_button("Yes"),
diff --git a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
index 815e504..1d57a2f 100644
--- a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
+++ b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
@@ -12,10 +12,6 @@
 from erpnext.manufacturing.doctype.production_order.production_order import get_item_details
 
 class ProductionPlanningTool(Document):
-	def __init__(self, arg1, arg2=None):
-		super(ProductionPlanningTool, self).__init__(arg1, arg2)
-		self.item_dict = {}
-
 	def clear_table(self, table_name):
 		self.set(table_name, [])
 
@@ -398,6 +394,9 @@
 		return bom_wise_item_details
 
 	def make_items_dict(self, item_list):
+		if not getattr(self, "item_dict", None):
+			self.item_dict = {}
+
 		for i in item_list:
 			self.item_dict.setdefault(i[0], []).append([flt(i[1]), i[2], i[3], i[4], i[5]])
 
diff --git a/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.html b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.html
new file mode 100644
index 0000000..119a4fc
--- /dev/null
+++ b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.html
@@ -0,0 +1,27 @@
+<h1 class="text-left"><b>{%= __("BOM Stock Report") %}</b></h1>
+<h5 class="text-left">{%= filters.bom %}</h5>
+<h5 class="text-left">{%= filters.warehouse %}</h5>
+<hr>
+
+<table class="table table-bordered">
+	<thead>
+		<tr>
+			<th style="width: 15%">{%= __("Item") %}</th>
+			<th style="width: 35%">{%= __("Description") %}</th>
+			<th style="width: 14%">{%= __("Required Qty") %}</th>
+			<th style="width: 13%">{%= __("In Stock Qty") %}</th>
+			<th style="width: 23%">{%= __("Enough Parts to Build") %}</th>
+		</tr>
+	</thead>
+	<tbody>
+        {% for(var i=0, l=data.length; i<l; i++) { %}
+        <tr>
+            <td>{%= data[i][ __("Item")] %}</td>
+            <td>{%= data[i][ __("Description")] %} </td>
+            <td align="right">{%= data[i][ __("Required Qty")] %} </td>
+            <td align="right">{%= data[i][ __("In Stock Qty")] %} </td>
+            <td align="right">{%= data[i][ __("Enough Parts to Build")] %} </td>
+        </tr>
+        {% } %}
+	</tbody>
+</table>
\ No newline at end of file
diff --git a/erpnext/projects/doctype/project/project.py b/erpnext/projects/doctype/project/project.py
index 2cb93f0..460ddc6 100644
--- a/erpnext/projects/doctype/project/project.py
+++ b/erpnext/projects/doctype/project/project.py
@@ -53,12 +53,17 @@
 			return frappe.get_all("Task", "*", {"project": self.name}, order_by="exp_start_date asc")
 
 	def validate(self):
+		self.validate_project_name()
 		self.validate_dates()
 		self.validate_weights()
 		self.sync_tasks()
 		self.tasks = []
 		self.send_welcome_email()
 
+	def validate_project_name(self):
+		if self.get("__islocal") and frappe.db.exists("Project", self.project_name):
+			frappe.throw(_("Project {0} already exists").format(self.project_name))
+
 	def validate_dates(self):
 		if self.expected_start_date and self.expected_end_date:
 			if getdate(self.expected_end_date) < getdate(self.expected_start_date):
diff --git a/erpnext/projects/doctype/task/task.py b/erpnext/projects/doctype/task/task.py
index 43240b2..52ae132 100644
--- a/erpnext/projects/doctype/task/task.py
+++ b/erpnext/projects/doctype/task/task.py
@@ -47,7 +47,7 @@
 
 			from frappe.desk.form.assign_to import clear
 			clear(self.doctype, self.name)
-			
+
 	def validate_progress(self):
 		if self.progress > 100:
 			frappe.throw(_("Progress % for a task cannot be more than 100."))
@@ -63,6 +63,12 @@
 		self.check_recursion()
 		self.reschedule_dependent_tasks()
 		self.update_project()
+		self.unassign_todo()
+
+	def unassign_todo(self):
+		if self.status == "Closed" or self.status == "Cancelled":
+			from frappe.desk.form.assign_to import clear
+			clear(self.doctype, self.name)
 
 	def update_total_expense_claim(self):
 		self.total_expense_claim = frappe.db.sql("""select sum(total_sanctioned_amount) from `tabExpense Claim`
@@ -120,7 +126,7 @@
 	def has_webform_permission(doc):
 		project_user = frappe.db.get_value("Project User", {"parent": doc.project, "user":frappe.session.user} , "user")
 		if project_user:
-			return True				
+			return True
 
 @frappe.whitelist()
 def get_events(start, end, filters=None):
@@ -154,7 +160,7 @@
 			order by name
 			limit %(start)s, %(page_len)s """ % {'key': searchfield,
 			'txt': "%%%s%%" % frappe.db.escape(txt), 'mcond':get_match_cond(doctype),
-			'start': start, 'page_len': page_len})			
+			'start': start, 'page_len': page_len})
 
 
 @frappe.whitelist()
@@ -170,4 +176,5 @@
 		where exp_end_date is not null
 		and exp_end_date < CURDATE()
 		and `status` not in ('Closed', 'Cancelled')""")
-		
+
+
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 5f35ea4..5b647f8 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -101,27 +101,6 @@
 				return me.set_query_for_batch(doc, cdt, cdn)
 			});
 		}
-	},
-	onload: function() {
-		var me = this;
-		if(this.frm.doc.__islocal) {
-			var today = frappe.datetime.get_today(),
-				currency = frappe.defaults.get_user_default("currency");
-
-			$.each({
-				currency: currency,
-				price_list_currency: currency,
-				status: "Draft",
-				is_subcontracted: "No",
-			}, function(fieldname, value) {
-				if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
-					me.frm.set_value(fieldname, value);
-			});
-
-			if(this.frm.doc.company && !this.frm.doc.amended_from) {
-				this.frm.trigger("company");
-			}
-		}
 
 		if(this.frm.fields_dict["taxes"]) {
 			this["taxes_remove"] = this.calculate_taxes_and_totals;
@@ -153,11 +132,36 @@
 
 				return {
 					filters: filters
-				}
+				};
 			});
 		}
+	},
+	onload: function() {
+		var me = this;
 
 		this.setup_quality_inspection();
+
+		if(this.frm.doc.__islocal) {
+			var currency = frappe.defaults.get_user_default("currency");
+
+			let set_value = (fieldname, value) => {
+				if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) {
+					return me.frm.set_value(fieldname, value);
+				}
+			};
+
+			return frappe.run_serially([
+				() => set_value('currency', currency),
+				() => set_value('price_list_currency', currency),
+				() => set_value('status', 'Draft'),
+				() => set_value('is_subcontracted', 'No'),
+				() => {
+					if(this.frm.doc.company && !this.frm.doc.amended_from) {
+						this.frm.trigger("company");
+					}
+				}
+			]);
+		}
 	},
 
 	setup_quality_inspection: function() {
@@ -195,13 +199,12 @@
 	},
 
 	onload_post_render: function() {
-		var me = this;
 		if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
 			&& !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
-			this.apply_default_taxes();
+			frappe.after_ajax(() => this.apply_default_taxes());
 		} else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
 			&& !this.frm.doc.is_pos) {
-			me.calculate_taxes_and_totals();
+			frappe.after_ajax(() => this.calculate_taxes_and_totals());
 		}
 		if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
 			this.setup_item_selector();
@@ -378,6 +381,7 @@
 			if(me.frm.doc.company && me.frm.fields_dict.currency) {
 				var company_currency = me.get_company_currency();
 				var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
+
 				if (!me.frm.doc.currency) {
 					me.frm.set_value("currency", company_currency);
 				}
diff --git a/erpnext/public/js/setup_wizard.js b/erpnext/public/js/setup_wizard.js
index 88178f4..7c274f1 100644
--- a/erpnext/public/js/setup_wizard.js
+++ b/erpnext/public/js/setup_wizard.js
@@ -86,6 +86,10 @@
 			});
 		},
 		validate: function() {
+			if ((this.values.company_name || "").toLowerCase() == "company") {
+				frappe.msgprint(__("Company Name cannot be Company"));
+				return false;
+			}
 			if (!this.values.company_abbr) {
 				return false;
 			}
@@ -135,10 +139,6 @@
 				frappe.msgprint(__("Please enter valid Financial Year Start and End Dates"));
 				return false;
 			}
-			if ((this.values.company_name || "").toLowerCase() == "company") {
-				frappe.msgprint(__("Company Name cannot be Company"));
-				return false;
-			}
 			return true;
 		},
 
diff --git a/erpnext/regional/india/setup.py b/erpnext/regional/india/setup.py
index 2165023..68a52cc 100644
--- a/erpnext/regional/india/setup.py
+++ b/erpnext/regional/india/setup.py
@@ -12,7 +12,7 @@
 	make_custom_fields()
 	add_permissions()
 	add_custom_roles_for_reports()
-	frappe.enqueue('erpnext.regional.india.setup.add_hsn_sac_codes')
+	frappe.enqueue('erpnext.regional.india.setup.add_hsn_sac_codes', now=frappe.flags.in_test)
 	add_print_formats()
 	if not patch:
 		update_address_template()
diff --git a/erpnext/schools/doctype/student_applicant/student_applicant.py b/erpnext/schools/doctype/student_applicant/student_applicant.py
index 081fa06..aeeffce 100644
--- a/erpnext/schools/doctype/student_applicant/student_applicant.py
+++ b/erpnext/schools/doctype/student_applicant/student_applicant.py
@@ -13,7 +13,6 @@
 		if self.student_admission:
 			naming_series = frappe.db.get_value('Student Admission', self.student_admission,
 				'naming_series_for_student_applicant')
-			print(naming_series)
 
 			if naming_series:
 				self.naming_series = naming_series
diff --git a/erpnext/selling/doctype/installation_note/installation_note.py b/erpnext/selling/doctype/installation_note/installation_note.py
index 720247d..9f730f4 100644
--- a/erpnext/selling/doctype/installation_note/installation_note.py
+++ b/erpnext/selling/doctype/installation_note/installation_note.py
@@ -12,8 +12,8 @@
 from erpnext.utilities.transaction_base import TransactionBase
 
 class InstallationNote(TransactionBase):
-	def __init__(self, arg1, arg2=None):
-		super(InstallationNote, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(InstallationNote, self).__init__(*args, **kwargs)
 		self.status_updater = [{
 			'source_dt': 'Installation Note Item',
 			'target_dt': 'Delivery Note Item',
diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py
index 1cdd840..f0cce5f 100644
--- a/erpnext/selling/doctype/quotation/quotation.py
+++ b/erpnext/selling/doctype/quotation/quotation.py
@@ -32,7 +32,7 @@
 		self.validate_valid_till()
 		if self.items:
 			self.with_items = 1
-
+			
 	def validate_valid_till(self):
 		if self.valid_till and self.valid_till < self.transaction_date:
 			frappe.throw(_("Valid till date cannot be before transaction date"))
@@ -79,15 +79,10 @@
 		else:
 			frappe.throw(_("Cannot set as Lost as Sales Order is made."))
 
-	def check_item_table(self):
-		if not self.get('items'):
-			frappe.throw(_("Please enter item details"))
-
 	def on_submit(self):
-		self.check_item_table()
-
 		# Check for Approving Authority
-		frappe.get_doc('Authorization Control').validate_approving_authority(self.doctype, self.company, self.base_grand_total, self)
+		frappe.get_doc('Authorization Control').validate_approving_authority(self.doctype,
+			self.company, self.base_grand_total, self)
 
 		#update enquiry status
 		self.update_opportunity()
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 8720482..98333c4 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -22,8 +22,8 @@
 class WarehouseRequired(frappe.ValidationError): pass
 
 class SalesOrder(SellingController):
-	def __init__(self, arg1, arg2=None):
-		super(SalesOrder, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(SalesOrder, self).__init__(*args, **kwargs)
 
 	def validate(self):
 		super(SalesOrder, self).validate()
@@ -696,7 +696,8 @@
 				"contact_display",
 				"contact_mobile",
 				"contact_email",
-				"contact_person"
+				"contact_person",
+				"taxes_and_charges"
 			],
 			"validation": {
 				"docstatus": ["=", 1]
diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py
index 7c0d7f9..9c5c82e 100644
--- a/erpnext/selling/doctype/sales_order/test_sales_order.py
+++ b/erpnext/selling/doctype/sales_order/test_sales_order.py
@@ -494,7 +494,7 @@
 		so.items[0].price_list_rate = price_list_rate = 100
 		so.items[0].margin_type = 'Percentage'
 		so.items[0].margin_rate_or_amount = 25
-		so.insert()
+		so.save()
 
 		new_so = frappe.copy_doc(so)
 		new_so.save(ignore_permissions=True)
diff --git a/erpnext/setup/doctype/email_digest/email_digest.py b/erpnext/setup/doctype/email_digest/email_digest.py
index c85a541..8d1fb3d 100644
--- a/erpnext/setup/doctype/email_digest/email_digest.py
+++ b/erpnext/setup/doctype/email_digest/email_digest.py
@@ -16,14 +16,13 @@
 
 from frappe.model.document import Document
 class EmailDigest(Document):
-	def __init__(self, arg1, arg2=None):
-		super(EmailDigest, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(EmailDigest, self).__init__(*args, **kwargs)
 
 		self.from_date, self.to_date = self.get_from_to_date()
 		self.set_dates()
 		self._accounts = {}
-		self.currency = frappe.db.get_value("Company", self.company,
-			"default_currency")
+		self.currency = frappe.db.get_value("Company", self.company, "default_currency")
 
 	def get_users(self):
 		"""get list of users"""
diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py
index f003ce4..49c439b 100644
--- a/erpnext/setup/utils.py
+++ b/erpnext/setup/utils.py
@@ -120,8 +120,9 @@
 		_role.save()
 
 	# add all roles to users
-	user = frappe.get_doc("User", "Administrator")
-	user.add_roles(*[role.get("name") for role in roles])
+	if roles:
+		user = frappe.get_doc("User", "Administrator")
+		user.add_roles(*[role.get("name") for role in roles])
 
 	domains = frappe.get_list("Domain")
 	if not domains:
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index f5a99af..dd00398 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -21,8 +21,8 @@
 }
 
 class DeliveryNote(SellingController):
-	def __init__(self, arg1, arg2=None):
-		super(DeliveryNote, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(DeliveryNote, self).__init__(*args, **kwargs)
 		self.status_updater = [{
 			'source_dt': 'Delivery Note Item',
 			'target_dt': 'Sales Order Item',
diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json
index b168631..525321c 100644
--- a/erpnext/stock/doctype/item/item.json
+++ b/erpnext/stock/doctype/item/item.json
@@ -1479,6 +1479,37 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "purchase_uom", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Default Purchase Unit of Measure", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "UOM", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "default": "0.00", 
    "depends_on": "is_stock_item", 
    "description": "", 
@@ -2075,6 +2106,37 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "sales_uom", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Default Sales Unit of Measure", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "UOM", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "default": "0", 
    "description": "Publish Item to hub.erpnext.com", 
    "fieldname": "publish_in_hub", 
@@ -3143,7 +3205,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 1, 
- "modified": "2017-07-06 18:28:36.645217", 
+ "modified": "2017-09-27 14:08:02.948326", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Item", 
diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py
index 34e3af6..c3f399a 100644
--- a/erpnext/stock/doctype/item/test_item.py
+++ b/erpnext/stock/doctype/item/test_item.py
@@ -120,6 +120,8 @@
 		self.assertRaises(ItemVariantExistsError, variant.save)
 
 	def test_copy_fields_from_template_to_variants(self):
+		frappe.delete_doc_if_exists("Item", "_Test Variant Item-XL", force=1)
+		
 		fields = [{'field_name': 'item_group'}, {'field_name': 'is_stock_item'}]
 		allow_fields = [d.get('field_name') for d in fields]
 		set_item_variant_settings(fields)
diff --git a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.js b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.js
index f3404cc..24f7e31 100644
--- a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.js
+++ b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.js
@@ -4,8 +4,8 @@
 frappe.ui.form.on('Item Variant Settings', {
 	setup: function(frm) {
 		const allow_fields = [];
-		const exclude_fields = ["item_code", "item_name", "show_in_website", "show_variant_in_website", 
-			"opening_stock", "variant_of", "valuation_rate", "variant_based_on"];
+		const exclude_fields = ["naming_series", "item_code", "item_name", "show_in_website",
+		"show_variant_in_website", "opening_stock", "variant_of", "valuation_rate"];
 
 		frappe.model.with_doctype('Item', () => {
 			frappe.get_meta('Item').fields.forEach(d => {
diff --git a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py
index 80462d1..678de1a 100644
--- a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py
+++ b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py
@@ -10,9 +10,9 @@
 	def set_default_fields(self):
 		self.fields = []
 		fields = frappe.get_meta('Item').fields
-		exclude_fields = ["item_code", "item_name", "show_in_website", "show_variant_in_website",
-			"standard_rate", "opening_stock", "image", "description",
-			"variant_of", "valuation_rate", "description", "variant_based_on",
+		exclude_fields = ["naming_series", "item_code", "item_name", "show_in_website",
+			"show_variant_in_website", "standard_rate", "opening_stock", "image", "description",
+			"variant_of", "valuation_rate", "description",
 			"website_image", "thumbnail", "website_specifiations", "web_long_description"]
 
 		for d in fields:
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index 2d089c4..e49f993 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -19,8 +19,8 @@
 }
 
 class PurchaseReceipt(BuyingController):
-	def __init__(self, arg1, arg2=None):
-		super(PurchaseReceipt, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(PurchaseReceipt, self).__init__(*args, **kwargs)
 		self.status_updater = [{
 			'source_dt': 'Purchase Receipt Item',
 			'target_dt': 'Purchase Order Item',
diff --git a/erpnext/stock/doctype/serial_no/serial_no.py b/erpnext/stock/doctype/serial_no/serial_no.py
index c39efa0..80c93ef 100644
--- a/erpnext/stock/doctype/serial_no/serial_no.py
+++ b/erpnext/stock/doctype/serial_no/serial_no.py
@@ -20,8 +20,8 @@
 class SerialNoDuplicateError(ValidationError): pass
 
 class SerialNo(StockController):
-	def __init__(self, arg1, arg2=None):
-		super(SerialNo, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(SerialNo, self).__init__(*args, **kwargs)
 		self.via_stock_ledger = False
 
 	def validate(self):
diff --git a/erpnext/stock/doctype/stock_entry/test_stock_entry.py b/erpnext/stock/doctype/stock_entry/test_stock_entry.py
index 4bcbcc4..0aecb78 100644
--- a/erpnext/stock/doctype/stock_entry/test_stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/test_stock_entry.py
@@ -11,7 +11,7 @@
 from erpnext.stock.doctype.stock_ledger_entry.stock_ledger_entry import StockFreezeError
 from erpnext.stock.stock_ledger import get_previous_sle
 from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import create_stock_reconciliation
-from erpnext.stock.doctype.item.test_item import set_item_variant_settings
+from erpnext.stock.doctype.item.test_item import set_item_variant_settings, make_item_variant
 from frappe.tests.test_permissions import set_user_permission_doctypes
 from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
 from erpnext.accounts.doctype.account.test_account import get_inventory_account
@@ -46,6 +46,7 @@
 
 		make_stock_entry(item_code=item_code, target=warehouse, qty=1, basic_rate=10)
 		sle = get_sle(item_code = item_code, warehouse = warehouse)[0]
+
 		self.assertEqual([[1, 10]], frappe.safe_eval(sle.stock_queue))
 
 		# negative qty
@@ -74,7 +75,6 @@
 		frappe.db.set_default("allow_negative_stock", 0)
 
 	def test_auto_material_request(self):
-		from erpnext.stock.doctype.item.test_item import make_item_variant
 		make_item_variant()
 		self._test_auto_material_request("_Test Item")
 		self._test_auto_material_request("_Test Item", material_request_type="Transfer")
@@ -82,6 +82,7 @@
 	def test_auto_material_request_for_variant(self):
 		fields = [{'field_name': 'reorder_levels'}]
 		set_item_variant_settings(fields)
+		make_item_variant()
 		template = frappe.get_doc("Item", "_Test Variant Item")
 
 		if not template.reorder_levels:
diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
index 360ebca..0f91e43 100644
--- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
+++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
@@ -14,8 +14,8 @@
 class EmptyStockReconciliationItemsError(frappe.ValidationError): pass
 
 class StockReconciliation(StockController):
-	def __init__(self, arg1, arg2=None):
-		super(StockReconciliation, self).__init__(arg1, arg2)
+	def __init__(self, *args, **kwargs):
+		super(StockReconciliation, self).__init__(*args, **kwargs)
 		self.head_row = ["Item Code", "Warehouse", "Quantity", "Valuation Rate"]
 
 	def validate(self):
diff --git a/erpnext/stock/doctype/warehouse/test_records.json b/erpnext/stock/doctype/warehouse/test_records.json
index af3bd23..014cf3e 100644
--- a/erpnext/stock/doctype/warehouse/test_records.json
+++ b/erpnext/stock/doctype/warehouse/test_records.json
@@ -15,13 +15,6 @@
  },
  {
   "company": "_Test Company",
-  "create_account_under": "Stock Assets - _TC",
-  "doctype": "Warehouse",
-  "warehouse_name": "_Test Warehouse",
-  "is_group": 0
- },
- {
-  "company": "_Test Company",
   "create_account_under": "Fixed Assets - _TC",
   "doctype": "Warehouse",
   "warehouse_name": "_Test Warehouse 1",
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index 72a83c6..539e8a5 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -164,6 +164,15 @@
 
 	warehouse = user_default_warehouse or item.default_warehouse or args.warehouse
 
+	#Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
+	if not args.uom:
+		if args.get('doctype') in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']:
+			args.uom = item.sales_uom if item.sales_uom else item.stock_uom
+		elif args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']:
+			args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom
+		else:
+			args.uom = item.stock_uom
+
 	out = frappe._dict({
 		"item_code": item.name,
 		"item_name": item.item_name,
@@ -178,7 +187,7 @@
 		"batch_no": None,
 		"item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
 			item.get("taxes")))),
-		"uom": item.stock_uom,
+		"uom": args.uom,
 		"min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
 		"qty": args.qty or 1.0,
 		"stock_qty": args.qty or 1.0,
diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py
index 7c6b34b..180ccbb 100644
--- a/erpnext/stock/stock_ledger.py
+++ b/erpnext/stock/stock_ledger.py
@@ -461,6 +461,6 @@
 	if not allow_zero_rate and not valuation_rate \
 			and cint(erpnext.is_perpetual_inventory_enabled(company)):
 		frappe.local.message_log = []
-		frappe.throw(_("Valuation rate not found for the Item {0}, which is required to do accounting entries for {1} {2}. If the item is transacting as a sample item in the {1}, please mention that in the {1} Item table. Otherwise, please create an incoming stock transaction for the item or mention valuation rate in the Item record, and then try submiting/cancelling this entry").format(item_code, voucher_type, voucher_no))
+		frappe.throw(_("Valuation rate not found for the Item {0}, which is required to do accounting entries for {1} {2}. If the item is transacting as a zero valuation rate item in the {1}, please mention that in the {1} Item table. Otherwise, please create an incoming stock transaction for the item or mention valuation rate in the Item record, and then try submiting/cancelling this entry").format(item_code, voucher_type, voucher_no))
 
 	return valuation_rate
diff --git a/erpnext/templates/emails/recurring_document_failed.html b/erpnext/templates/emails/recurring_document_failed.html
index ea48034..27c43bc 100644
--- a/erpnext/templates/emails/recurring_document_failed.html
+++ b/erpnext/templates/emails/recurring_document_failed.html
@@ -1,11 +1,11 @@
 <h2>{{_("Recurring")}} {{ type }} {{ _("Failed")}}</h2>
 
-<p>An error occured while creating recurring {{ type }} <b>{{ name }}</b> for <b>{{ party }}</b>.</p>
-<p>This could be because of some invalid Email Addresses in the {{ type }}.</p>
-<p>To stop sending repetitive error notifications from the system, we have checked "Disabled" field in the subscription {{ subscription}} for the {{ type }} {{ name }}.</p>
-<p><b>Please correct the {{ type }} and unchcked "Disabled" in the {{ subscription }} for making recurring again.</b></p>
+<p>{{_("An error occured while creating recurring")}} {{ type }} <b>{{ name }}</b> {{_("for")}} <b>{{ party }}</b>.</p>
+<p>{{_("This could be because of some invalid Email Addresses in the")}} {{ type }}.</p>
+<p>{{_("To stop sending repetitive error notifications from the system, we have checked "Disabled" field in the subscription")}} {{ subscription}} {{_("for the")}} {{ type }} {{ name }}.</p>
+<p><b>{{_("Please correct the")}} {{ type }} {{_('and unchcked "Disabled" in the')}} {{ subscription }} {{_("for making recurring again.")}}</b></p>
 <hr>
-<p><b>It is necessary to take this action today itself for the above mentioned recurring {{ type }}
-to be generated. If delayed, you will have to manually change the "Repeat on Day of Month" field
-of this {{ type }} for generating the recurring {{ type }} in the subscription {{ subscription }}.</b></p>
-<p>[This email is autogenerated]</p>
+<p><b>{{_("It is necessary to take this action today itself for the above mentioned recurring")}} {{ type }}
+{{_('to be generated. If delayed, you will have to manually change the "Repeat on Day of Month" field
+of this')}} {{ type }} {{_("for generating the recurring")}} {{ type }} {{_("in the subscription")}} {{ subscription }}.</b></p>
+<p>[{{_("This email is autogenerated")}}]</p>
diff --git a/erpnext/tests/ui/tests.txt b/erpnext/tests/ui/tests.txt
index 909216b..199d886 100644
--- a/erpnext/tests/ui/tests.txt
+++ b/erpnext/tests/ui/tests.txt
@@ -128,3 +128,4 @@
 erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_repack.js
 erpnext/accounts/doctype/sales_invoice/tests/test_sales_invoice_with_serialize_item.js
 erpnext/accounts/doctype/payment_entry/tests/test_payment_against_invoice.js
+erpnext/buying/doctype/purchase_order/tests/test_purchase_order_with_last_purchase_rate.js
\ No newline at end of file
diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py
index 0e3a4f9..65310aa 100644
--- a/erpnext/utilities/transaction_base.py
+++ b/erpnext/utilities/transaction_base.py
@@ -25,7 +25,7 @@
 		if not getattr(self, 'set_posting_time', None):
 			now = now_datetime()
 			self.posting_date = now.strftime('%Y-%m-%d')
-			self.posting_time = now.strftime('%H:%M:%S')
+			self.posting_time = now.strftime('%H:%M:%S.%f')
 
 	def add_calendar_event(self, opts, force=False):
 		if cstr(self.contact_by) != cstr(self._prev.contact_by) or \