Fetch Customer's Item Code in Sales Transaction
diff --git a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.txt b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.txt
index 88af72b..0dddb8a 100644
--- a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.txt
+++ b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-04-13 11:56:18',
+		'creation': '2012-06-08 16:07:55',
 		'docstatus': 0,
-		'modified': '2012-06-07 19:05:06',
+		'modified': '2012-07-09 11:00:18',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -70,6 +70,17 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'customer_item_code',
+		'fieldtype': u'Data',
+		'hidden': 1,
+		'label': u"Customer's Item Code",
+		'permlevel': 1,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'item_name',
 		'fieldtype': u'Data',
 		'in_filter': 0,
diff --git a/erpnext/selling/doctype/opportunity/opportunity.js b/erpnext/selling/doctype/opportunity/opportunity.js
index c672aa1..4487c89 100644
--- a/erpnext/selling/doctype/opportunity/opportunity.js
+++ b/erpnext/selling/doctype/opportunity/opportunity.js
@@ -140,79 +140,8 @@
 	else 
 		return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem WHERE tabItem.is_sales_item="Yes" AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") AND tabItem.%(key)s LIKE "%s" LIMIT 50';
 }
-	
- //Fetch Item Details
-//====================================================================================================================
-cur_frm.cscript.item_code = function(doc, cdt, cdn) {
-	var d = locals[cdt][cdn];
-	if (d.item_code) {
-		get_server_fields('get_item_details',d.item_code,'enquiry_details',doc,cdt,cdn,1);
-	}
-}
 
-/*
- //Fetch Customer Details
-//======================================================================================================================
-cur_frm.cscript.customer = function(doc, cdt, cdn){
-	if (doc.customer) {
-		get_server_fields('get_cust_address',doc.customer,'',doc,cdt,cdn,1);
-	}
-}
-*/
-
-/*
-//=======================================================================================================================
-cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT `tabContact`.contact_name FROM `tabContact` WHERE `tabContact`.is_customer = 1 AND `tabContact`.customer = "'+ doc.customer+'" AND `tabContact`.docstatus != 2 AND `tabContact`.contact_name LIKE "%s" ORDER BY `tabContact`.contact_name ASC LIMIT 50';
-}
-*/
-
-/*
-//=======================================================================================================================
-cur_frm.cscript.contact_person = function(doc, cdt, cdn){
-	if (doc.contact_person) {
-		arg = {};
-		arg.contact_person = doc.contact_person;
-		arg.customer = doc.customer;
-		get_server_fields('get_contact_details',docstring(arg),'',doc,cdt,cdn,1);
-	}
-}
-*/
-
-/*
-// hide - unhide fields based on lead or customer..
-//=======================================================================================================================
-cur_frm.cscript.clear_values = function(doc,cdt,cdn) {
-	if(doc.enquiry_from == 'Lead') {
-		doc.customer = doc.customer_name = doc.contact_person = doc.customer_group = "";
-	}
-	else if(doc.enquiry_from == 'Customer') {
-		doc.lead =	doc.lead_name = "";
-	}
-	refresh_many(['lead','lead_name','customer','customer_name','contact_person','customer_group']);
-}
-*/
-
-/*
-//================ hide - unhide fields on basis of enquiry from either lead or customer =============================== 
-cur_frm.cscript.enquiry_from = function(doc,cdt,cdn){
-	cur_frm.cscript.clear_values(doc,cdt,cdn);
-	 doc.address = doc.territory = doc.contact_no = doc.email_id = "";
-	 refresh_many(['territory','address','contact_no','email_id']);
-}
-*/
-
-/*
-//================ create new contact ============================================================================
-cur_frm.cscript.new_contact = function(){
-	tn = createLocal('Contact');
-	locals['Contact'][tn].is_customer = 1;
-	if(doc.customer) locals['Contact'][tn].customer = doc.customer;
-	loaddoc('Contact', tn);
-}
-*/
- // Create New Quotation
-// =======================================================================================================================
+// Create New Quotation
 cur_frm.cscript['Create Quotation'] = function(){
 	n = createLocal("Quotation");
 	$c('dt_map', args={
@@ -284,39 +213,5 @@
 }
 
 //get query select Territory
-//=======================================================================================================================
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
 	return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.%(key)s LIKE "%s"	ORDER BY	`tabTerritory`.`name` ASC LIMIT 50';}
-
-/*
-//===================== Opportunity From validation - either customer or lead is mandatory =====================================
-cur_frm.cscript.enq_frm_validate = function(doc,cdt,cdn){
-	
-	if(doc.enquiry_from == 'Lead'){
-		if(!doc.lead){
-			alert("Lead is mandatory.");	
-			validated = false; 
-		}
-	}
-	else if(doc.enquiry_from == 'Customer'){
-			if(!doc.customer){
-			alert("Customer is mandatory.");
-			validated = false;
-		}
-		else if(!doc.contact_person){
-			alert("Contact Person is mandatory.");
-			validated = false;
-		}
-		else if(!doc.customer_group){
-			alert("Customer Group is mandatory.");
-			validated = false;
-		}
-	} 
-}
-*/
-
-//===================validation function ==============================================================================
-
-cur_frm.cscript.validate = function(doc,cdt,cdn){
-	//cur_frm.cscript.enq_frm_validate(doc,cdt,cdn);
-}
diff --git a/erpnext/selling/doctype/quotation_item/quotation_item.txt b/erpnext/selling/doctype/quotation_item/quotation_item.txt
index d5ece1f..6ae657c 100644
--- a/erpnext/selling/doctype/quotation_item/quotation_item.txt
+++ b/erpnext/selling/doctype/quotation_item/quotation_item.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-06-04 15:40:56',
+		'creation': '2012-06-08 16:07:57',
 		'docstatus': 0,
-		'modified': '2012-06-07 17:58:39',
+		'modified': '2012-07-09 11:04:47',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -63,6 +63,17 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'customer_item_code',
+		'fieldtype': u'Data',
+		'hidden': 1,
+		'label': u"Customer's Item Code",
+		'permlevel': 1,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'item_name',
 		'fieldtype': u'Data',
 		'in_filter': 1,
diff --git a/erpnext/selling/doctype/sales_common/sales_common.py b/erpnext/selling/doctype/sales_common/sales_common.py
index f57e719..0a776a8 100644
--- a/erpnext/selling/doctype/sales_common/sales_common.py
+++ b/erpnext/selling/doctype/sales_common/sales_common.py
@@ -163,6 +163,15 @@
 		if ret['warehouse'] or ret['reserved_warehouse']:
 			av_qty = self.get_available_qty({'item_code': args['item_code'], 'warehouse': ret['warehouse'] or ret['reserved_warehouse']})
 			ret.update(av_qty)
+			
+		# get customer code for given item from Item Customer Detail
+		customer_item_code_row = webnotes.conn.sql("""\
+			select ref_code from `tabItem Customer Detail`
+			where parent = %s and customer_name = %s""",
+			(args['item_code'], obj.doc.customer))
+		if customer_item_code_row and customer_item_code_row[0][0]:
+			ret['customer_item_code'] = customer_item_code_row[0][0]
+		
 		return ret
 
 
diff --git a/erpnext/selling/doctype/sales_order_item/sales_order_item.txt b/erpnext/selling/doctype/sales_order_item/sales_order_item.txt
index 1d2f358..1c80698 100644
--- a/erpnext/selling/doctype/sales_order_item/sales_order_item.txt
+++ b/erpnext/selling/doctype/sales_order_item/sales_order_item.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-04-13 11:56:28',
+		'creation': '2012-06-08 16:07:58',
 		'docstatus': 0,
-		'modified': '2012-06-07 18:04:52',
+		'modified': '2012-07-09 11:05:16',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -60,6 +60,17 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'customer_item_code',
+		'fieldtype': u'Data',
+		'hidden': 1,
+		'label': u"Customer's Item Code",
+		'permlevel': 1,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'item_name',
 		'fieldtype': u'Data',
 		'label': u'Item Name',
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.txt b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.txt
index 5abb70d..8d0d387 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.txt
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-04-13 11:56:35',
+		'creation': '2012-06-08 16:08:01',
 		'docstatus': 0,
-		'modified': '2012-06-07 17:57:14',
+		'modified': '2012-07-09 11:06:26',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -70,6 +70,17 @@
 
 	# DocField
 	{
+		'doctype': u'DocField',
+		'fieldname': u'customer_item_code',
+		'fieldtype': u'Data',
+		'hidden': 1,
+		'label': u"Customer's Item Code",
+		'permlevel': 1,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
 		'colour': u'White:FFF',
 		'doctype': u'DocField',
 		'fieldname': u'item_name',