[fix] default account for manufacturing stock entry should be cogs
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index e4e35ad..30ffd1a 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -127,3 +127,4 @@
 erpnext.patches.v5_0.taxes_and_totals_in_party_currency
 erpnext.patches.v5_0.replace_renamed_fields_in_custom_scripts_and_print_formats
 erpnext.patches.v5_0.update_from_bom
+erpnext.patches.v5_0.update_account_types
diff --git a/erpnext/patches/v5_0/update_account_types.py b/erpnext/patches/v5_0/update_account_types.py
new file mode 100644
index 0000000..4686a5f
--- /dev/null
+++ b/erpnext/patches/v5_0/update_account_types.py
@@ -0,0 +1,20 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	for company in frappe.db.get_all("Company"):
+		company = frappe.get_doc("Company", company.name)
+
+		match_types = ("Stock Received But Not Billed", "Stock Adjustment", "Expenses Included In Valuation",
+			"Cost of Goods Sold")
+
+		for account_type in match_types:
+			account_name = "{0} - {1}".format(account_type, company.abbr)
+			current_account_type = frappe.db.get_value("Account", account_name, "account_type")
+			if current_account_type != account_type:
+				frappe.db.set_value("Account", account_name, "account_type", account_type)
+
+		company.set_default_accounts()
diff --git a/erpnext/setup/doctype/company/company.json b/erpnext/setup/doctype/company/company.json
index 67e1ba8..cede702 100644
--- a/erpnext/setup/doctype/company/company.json
+++ b/erpnext/setup/doctype/company/company.json
@@ -188,7 +188,7 @@
    "fieldname": "default_expense_account", 
    "fieldtype": "Link", 
    "ignore_user_permissions": 1, 
-   "label": "Default Expense Account", 
+   "label": "Default Cost of Goods Sold Account", 
    "no_copy": 1, 
    "options": "Account", 
    "permlevel": 0
@@ -399,7 +399,7 @@
  ], 
  "icon": "icon-building", 
  "idx": 1, 
- "modified": "2015-02-21 10:32:38.523900", 
+ "modified": "2015-02-25 06:28:13.565128", 
  "modified_by": "Administrator", 
  "module": "Setup", 
  "name": "Company", 
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index 6247afd..56e0243 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -106,28 +106,30 @@
 		account.insert()
 
 	def set_default_accounts(self):
-		def _set_default_account(fieldname, account_type):
-			if self.get(fieldname):
-				return
+		self._set_default_account("default_cash_account", "Cash")
+		self._set_default_account("default_bank_account", "Bank")
 
-			account = frappe.db.get_value("Account", {"account_type": account_type,
-				"group_or_ledger": "Ledger", "company": self.name})
-
-			if account:
-				self.db_set(fieldname, account)
-
-		_set_default_account("default_cash_account", "Cash")
-		_set_default_account("default_bank_account", "Bank")
-
-		if cint(frappe.db.get_value("Accounts Settings", None, "auto_accounting_for_stock")):
-			_set_default_account("stock_received_but_not_billed", "Stock Received But Not Billed")
-			_set_default_account("stock_adjustment_account", "Stock Adjustment")
-			_set_default_account("expenses_included_in_valuation", "Expenses Included In Valuation")
+		if cint(frappe.db.get_single_value("Accounts Settings", "auto_accounting_for_stock")):
+			self._set_default_account("stock_received_but_not_billed", "Stock Received But Not Billed")
+			self._set_default_account("stock_adjustment_account", "Stock Adjustment")
+			self._set_default_account("expenses_included_in_valuation", "Expenses Included In Valuation")
+			self._set_default_account("default_expense_account", "Cost of Goods Sold")
 
 		if not self.default_income_account:
 			self.db_set("default_income_account", frappe.db.get_value("Account",
 				{"account_name": _("Sales"), "company": self.name}))
 
+
+	def _set_default_account(self, fieldname, account_type):
+		if self.get(fieldname):
+			return
+
+		account = frappe.db.get_value("Account", {"account_type": account_type,
+			"group_or_ledger": "Ledger", "company": self.name})
+
+		if account:
+			self.db_set(fieldname, account)
+
 	def create_default_cost_center(self):
 		cc_list = [
 			{
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.json b/erpnext/stock/doctype/stock_entry/stock_entry.json
index 5b1cce0..16e7a89 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.json
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.json
@@ -386,6 +386,15 @@
    "read_only": 1
   }, 
   {
+   "description": "This will override Difference Account in Item", 
+   "fieldname": "difference_account", 
+   "fieldtype": "Link", 
+   "label": "Difference Account", 
+   "options": "Account", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
    "fieldname": "fold", 
    "fieldtype": "Fold", 
    "permlevel": 0
@@ -645,7 +654,7 @@
  "is_submittable": 1, 
  "issingle": 0, 
  "max_attachments": 0, 
- "modified": "2015-02-25 01:59:14.371042", 
+ "modified": "2015-02-25 06:13:11.899840", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Stock Entry", 
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 2d7fc2d..f4f1eec 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -86,6 +86,12 @@
 		if self.purpose not in valid_purposes:
 			frappe.throw(_("Purpose must be one of {0}").format(comma_or(valid_purposes)))
 
+		if self.purpose in ("Manufacture", "Repack", "Sales Return") and not self.difference_account:
+			self.difference_account = frappe.db.get_value("Company", self.company, "default_expense_account")
+
+		if self.purpose in ("Purchase Return") and not self.difference_account:
+			frappe.throw(_("Difference Account mandatory for purpose '{0}'").format(self.purpose))
+
 	def set_transfer_qty(self):
 		for item in self.get("items"):
 			if not flt(item.qty):
@@ -108,6 +114,9 @@
 					if f not in ["expense_account", "cost_center"] or not item.get(f):
 						item.set(f, item_details.get(f))
 
+			if self.difference_account:
+				item.expense_account = self.difference_account
+
 			if not item.transfer_qty:
 				item.transfer_qty = item.qty * item.conversion_factor