aii: default account from company
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index 7722c98..b56e2ac 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -444,6 +444,9 @@
 					
 		# item gl entries
 		stock_item_and_auto_inventory_accounting = False
+		if auto_inventory_accounting:
+			stock_acocunt = self.get_default_account("stock_received_but_not_billed")
+			
 		for item in self.doclist.get({"parentfield": "entries"}):
 			if auto_inventory_accounting and item.item_code in self.stock_items:
 				if flt(item.valuation_rate):
@@ -455,7 +458,7 @@
 					
 					gl_entries.append(
 						self.get_gl_dict({
-							"account": "Stock Received But Not Billed - %s" % (self.company_abbr,),
+							"account": stock_acocunt,
 							"against": self.doc.credit_to,
 							"debit": flt(item.valuation_rate) * flt(item.conversion_factor) \
 								*  flt(item.qty),
@@ -480,7 +483,7 @@
 			# this will balance out valuation amount included in cost of goods sold
 			gl_entries.append(
 				self.get_gl_dict({
-					"account": "Expenses Included In Valuation - %s" % (self.company_abbr,),
+					"account": self.get_default_account("expenses_included_in_valuation"),
 					"cost_center": "Auto Inventory Accounting - %s" % self.company_abbr,
 					"against": self.doc.credit_to,
 					"credit": valuation_tax,
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index b6b1f0b..f81a71b 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -704,9 +704,9 @@
 			
 		if auto_inventory_accounting:
 			if cint(self.doc.is_pos) and cint(self.doc.update_stock):
-				stock_account = self.get_stock_in_hand_account()
+				stock_account = self.get_default_account("stock_in_hand_account")
 			else:
-				stock_account = "Stock Delivered But Not Billed - %s" % (self.company_abbr,)
+				stock_account = self.get_default_account("stock_delivered_but_not_billed")
 		
 		for item in self.doclist.get({"parentfield": "entries"}):
 			# income account gl entries
@@ -794,7 +794,8 @@
 			stock_ledger_entries = item_sales_bom = None
 			
 		for item in self.doclist.get({"parentfield": "entries"}):
-			if item.item_code in self.stock_items:
+			if item.item_code in self.stock_items or \
+					(item_sales_bom and item_sales_bom.get(item.item_code)):
 				item.buying_amount = self.get_item_buying_amount(item, stock_ledger_entries, 
 					item_sales_bom)
 				webnotes.conn.set_value("Sales Invoice Item", item.name, 
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index 66a5d9e..df78212 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -71,15 +71,15 @@
 				"advance_amount": flt(d.amount),
 				"allocate_amount": 0
 			})
-			
-	def get_stock_in_hand_account(self):
-		stock_in_hand_account = webnotes.conn.get_value("Company", self.doc.company, "stock_in_hand_account")
-		if not stock_in_hand_account:
-			msgprint(_("Missing") + ": " 
-				+ _(webnotes.get_doctype("company").get_label("stock_in_hand_account")
-				+ " " + _("for Company") + " " + self.doc.company), raise_exception=True)
 		
-		return stock_in_hand_account
+	def get_default_account(self, account_for):
+		account = webnotes.conn.get_value("Company", self.doc.company, account_for)
+		if not account:
+			msgprint(_("Please mention default account for '") + 
+				_(webnotes.get_doctype("company").get_label(account_for) + 
+				_("' in Company: ") + self.doc.company), raise_exception=True)
+				
+		return account
 		
 	@property
 	def stock_items(self):
@@ -88,7 +88,7 @@
 			self._stock_items = [r[0] for r in webnotes.conn.sql("""select name
 				from `tabItem` where name in (%s) and is_stock_item='Yes'""" % \
 				(", ".join((["%s"]*len(item_codes))),), item_codes)]
-
+				
 		return self._stock_items
 		
 	@property
@@ -96,4 +96,9 @@
 		if not hasattr(self, "_abbr"):
 			self._abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr")
 			
-		return self._abbr
\ No newline at end of file
+		return self._abbr
+
+
+@webnotes.whitelist()
+def get_default_account(account_for, company):
+	return webnotes.conn.get_value("Company", company, account_for)
\ No newline at end of file
diff --git a/controllers/stock_controller.py b/controllers/stock_controller.py
index 3a900aa..eec7352 100644
--- a/controllers/stock_controller.py
+++ b/controllers/stock_controller.py
@@ -20,7 +20,7 @@
 
 class StockController(AccountsController):
 	def make_gl_entries(self, against_stock_account, amount, cost_center=None):
-		stock_in_hand_account = self.get_stock_in_hand_account()
+		stock_in_hand_account = self.get_default_account("stock_in_hand_account")
 		
 		if amount:
 			gl_entries = [
diff --git a/setup/doctype/company/company.py b/setup/doctype/company/company.py
index 6ba7985..15241a2 100644
--- a/setup/doctype/company/company.py
+++ b/setup/doctype/company/company.py
@@ -214,6 +214,11 @@
 			"Stock Adjustment - " + self.doc.abbr):
 				webnotes.conn.set(self.doc, "stock_adjustment_account", "Stock Adjustment - " + 
 					self.doc.abbr)
+					
+		if not self.doc.expenses_included_in_valuation and webnotes.conn.exists("Account", 
+			"Expenses Included In Valuation - " + self.doc.abbr):
+				webnotes.conn.set(self.doc, "expenses_included_in_valuation", 
+					"Expenses Included In Valuation - " + self.doc.abbr)
 			
 	# Create default cost center
 	# ---------------------------------------------------
diff --git a/setup/doctype/company/company.txt b/setup/doctype/company/company.txt
index d8c649f..4d2dcda 100644
--- a/setup/doctype/company/company.txt
+++ b/setup/doctype/company/company.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-02-27 09:38:05", 
   "docstatus": 0, 
-  "modified": "2013-03-18 16:34:04", 
+  "modified": "2013-03-19 12:52:00", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -196,6 +196,13 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "expenses_included_in_valuation", 
+  "fieldtype": "Link", 
+  "label": "Expenses Included In Valuation", 
+  "options": "Account"
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "col_break23", 
   "fieldtype": "Column Break", 
   "width": "50%"
diff --git a/setup/doctype/global_defaults/global_defaults.txt b/setup/doctype/global_defaults/global_defaults.txt
index 960da7e..d75f1ef 100644
--- a/setup/doctype/global_defaults/global_defaults.txt
+++ b/setup/doctype/global_defaults/global_defaults.txt
@@ -1,8 +1,8 @@
 [
  {
-  "creation": "2013-02-19 12:28:27", 
+  "creation": "2013-02-21 14:54:43", 
   "docstatus": 0, 
-  "modified": "2013-02-20 14:09:00", 
+  "modified": "2013-03-19 14:46:49", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -403,7 +403,7 @@
   "fieldname": "emp_created_by", 
   "fieldtype": "Select", 
   "label": "Employee Records to be created by ", 
-  "options": "\nNaming Series\nEmployee Number"
+  "options": "Naming Series\nEmployee Number"
  }, 
  {
   "doctype": "DocField", 
diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py
index aef7939..62d5c5f 100644
--- a/stock/doctype/delivery_note/delivery_note.py
+++ b/stock/doctype/delivery_note/delivery_note.py
@@ -400,12 +400,14 @@
 		
 		if stock_ledger_entries:
 			for item in self.doclist.get({"parentfield": "delivery_note_details"}):
-				buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, 
-					self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, 
-					item_sales_bom)
-				item.buying_amount = buying_amount > 0 and buying_amount or 0
-				webnotes.conn.set_value("Delivery Note Item", item.name, "buying_amount", 
-					item.buying_amount)
+				if item.item_code in self.stock_items or \
+						(item_sales_bom and item_sales_bom.get(item.item_code)):
+					buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, 
+						self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, 
+						item_sales_bom)
+					item.buying_amount = buying_amount > 0 and buying_amount or 0
+					webnotes.conn.set_value("Delivery Note Item", item.name, "buying_amount", 
+						item.buying_amount)
 		
 		self.validate_warehouse()
 		
@@ -420,7 +422,7 @@
 		if not cint(webnotes.defaults.get_global_default("auto_inventory_accounting")):
 			return
 		
-		against_stock_account = "Stock Delivered But Not Billed - %s" % (self.company_abbr,)
+		against_stock_account = self.get_default_account("stock_delivered_but_not_billed")
 		total_buying_amount = self.get_total_buying_amount()
 		
 		super(DocType, self).make_gl_entries(against_stock_account, -1*total_buying_amount)
diff --git a/stock/doctype/purchase_receipt/purchase_receipt.py b/stock/doctype/purchase_receipt/purchase_receipt.py
index e26c0a6..e7d030d 100644
--- a/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -318,7 +318,7 @@
 		if not cint(webnotes.defaults.get_global_default("auto_inventory_accounting")):
 			return
 		
-		against_stock_account = "Stock Received But Not Billed - %s" % (self.company_abbr,)
+		against_stock_account = self.get_default_account("stock_received_but_not_billed")
 		total_valuation_amount = self.get_total_valuation_amount()
 		
 		super(DocType, self).make_gl_entries(against_stock_account, total_valuation_amount)
diff --git a/stock/doctype/stock_entry/stock_entry.js b/stock/doctype/stock_entry/stock_entry.js
index 0a75914..2eb538c 100644
--- a/stock/doctype/stock_entry/stock_entry.js
+++ b/stock/doctype/stock_entry/stock_entry.js
@@ -18,6 +18,47 @@
 wn.provide("erpnext.stock");
 
 erpnext.stock.StockEntry = erpnext.stock.StockController.extend({
+	onload: function() {
+		this.set_default_account();
+	}, 
+	
+	set_default_account: function() {
+		var me = this;
+		
+		if (sys_defaults.auto_inventory_accounting && !this.frm.doc.expense_adjustment_account) {
+			if (this.frm.doc.purpose == "Sales Return") 
+				account_for = "stock_delivered_but_not_billed";
+			else if (this.frm.doc.purpose == "Purchase Return") 
+				account_for = "stock_received_but_not_billed";
+			else account_for = "stock_adjustment_account";
+			
+			this.frm.call({
+				method: "controllers.accounts_controller.get_default_account",
+				args: {
+					"account_for": account_for, 
+					"company": this.frm.doc.company
+				},
+				callback: function(r) {
+					if (!r.exc) me.frm.set_value("expense_adjustment_account", r.message);
+				}
+			});
+		}
+	},
+	
+	setup: function() {
+		var me = this;
+		if (sys_defaults.auto_inventory_accounting) {
+			this.frm.add_fetch("company", "expense_adjustment_account", "stock_adjustment_account");
+
+			this.frm.fields_dict["expense_adjustment_account"].get_query = function() {
+				return {
+					"query": "accounts.utils.get_account_list", 
+					"filters": { "company": me.frm.doc.company }
+				}
+			}
+		}
+	},
+	
 	onload_post_render: function() {
 		if(this.frm.doc.__islocal && (this.frm.doc.production_order || this.frm.doc.bom_no) 
 			&& !getchildren('Stock Entry Detail', this.frm.doc.name, 'mtn_details').length) {
@@ -234,11 +275,4 @@
 
 cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
 
-cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
-
-cur_frm.fields_dict["expense_adjustment_account"].get_query = function(doc) {
-	return {
-		"query": "accounts.utils.get_account_list", 
-		"filters": { "company": doc.company }
-	}
-}
\ No newline at end of file
+cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
\ No newline at end of file
diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.js b/stock/doctype/stock_reconciliation/stock_reconciliation.js
index fb4053c..372166e 100644
--- a/stock/doctype/stock_reconciliation/stock_reconciliation.js
+++ b/stock/doctype/stock_reconciliation/stock_reconciliation.js
@@ -18,10 +18,44 @@
 wn.provide("erpnext.stock");
 
 erpnext.stock.StockReconciliation = erpnext.stock.StockController.extend({
-	setup: function() {
-		this.frm.add_fetch("company", "stock_adjustment_account", "expense_account");
+	onload: function() {
+		this.set_default_expense_account();
 	}, 
 	
+	set_default_expense_account: function() {
+		var me = this;
+		
+		if (sys_defaults.auto_inventory_accounting && !this.frm.doc.expense_account) {
+			this.frm.call({
+				method: "controllers.accounts_controller.get_default_account",
+				args: {
+					"account_for": "stock_adjustment_account", 
+					"company": this.frm.doc.company
+				},
+				callback: function(r) {
+					if (!r.exc) me.frm.set_value("expense_account", r.message);
+				}
+			});
+		}
+	},
+	
+	setup: function() {
+		var me = this;
+		
+		this.frm.add_fetch("company", "expense_account", "stock_adjustment_account");
+		
+		this.frm.fields_dict["expense_account"].get_query = function() {
+			return {
+				"query": "accounts.utils.get_account_list", 
+				"filters": {
+					"is_pl_account": "Yes",
+					"debit_or_credit": "Debit",
+					"company": me.frm.doc.company
+				}
+			}
+		}
+	},
+	
 	refresh: function() {
 		if(this.frm.doc.docstatus===0) {
 			this.show_download_template();
@@ -126,15 +160,4 @@
 	},
 });
 
-cur_frm.cscript = new erpnext.stock.StockReconciliation({frm: cur_frm});
-
-cur_frm.fields_dict["expense_account"].get_query = function(doc) {
-	return {
-		"query": "accounts.utils.get_account_list", 
-		"filters": {
-			"is_pl_account": "Yes",
-			"debit_or_credit": "Debit",
-			"company": doc.company
-		}
-	}
-}
\ No newline at end of file
+cur_frm.cscript = new erpnext.stock.StockReconciliation({frm: cur_frm});
\ No newline at end of file