Merge branch 'master' into stable
diff --git a/accounts/search_criteria/budget_variance_report/budget_variance_report.js b/accounts/search_criteria/budget_variance_report/budget_variance_report.js
index ed5adb1..08c9693 100644
--- a/accounts/search_criteria/budget_variance_report/budget_variance_report.js
+++ b/accounts/search_criteria/budget_variance_report/budget_variance_report.js
@@ -1,17 +1,15 @@
 report.customize_filters = function() {
-
-  //this.hide_all_filters();
-  this.mytabs.items['Select Columns'].hide();
-  this.mytabs.items['More Filters'].hide();
-
- 
   this.add_filter({fieldname:'period', label:'Period', fieldtype:'Select', options:'Monthly'+NEWLINE+'Quarterly'+NEWLINE+'Half Yearly'+NEWLINE+'Annual',report_default:'Quarterly',ignore : 1, parent:'Budget Detail'});
+  this.add_filter({fieldname:'company', label:'Company', fieldtype:'Link',  options:'Company', report_default:sys_defaults.company, ignore : 1, parent:'Budget Detail', in_first_page:1});
   this.filter_fields_dict['Budget Detail'+FILTER_SEP +'Fiscal Year'].df.in_first_page = 1;
   this.filter_fields_dict['Budget Detail'+FILTER_SEP +'Period'].df.in_first_page = 1;
 
   this.filter_fields_dict['Budget Detail'+FILTER_SEP +'Fiscal Year'].df['report_default'] = sys_defaults.fiscal_year;
 }
+
 report.aftertableprint = function(t) {
    $yt(t,'*',1,{whiteSpace:'pre'});
 }
+
+this.mytabs.items['More Filters'].hide();
 this.mytabs.items['Select Columns'].hide();
diff --git a/accounts/search_criteria/budget_variance_report/budget_variance_report.py b/accounts/search_criteria/budget_variance_report/budget_variance_report.py
index 182f9e5..9f7c690 100644
--- a/accounts/search_criteria/budget_variance_report/budget_variance_report.py
+++ b/accounts/search_criteria/budget_variance_report/budget_variance_report.py
@@ -131,11 +131,6 @@
     col_idx[str(c) + n ] = len(colnames) - 1
 
 
-
-# make default columns
-#coltypes[col_idx[based_on]] = 'Link'
-#coloptions[col_idx[based_on]]= based_on
-
 # get start date
 start_date = get_value('Fiscal Year', fiscal_year, 'year_start_date')
 if not start_date:
@@ -162,7 +157,7 @@
 
     ch = make_child_lst(based_on,r[0].strip())
    
-    actual = sql("select sum(ifnull(t1.debit,0))-sum(ifnull(t1.credit,0)) from `tabGL Entry` t1, `tabAccount` t2 where t2.is_pl_account = 'Yes' and t1.is_cancelled = 'No' and t1.cost_center in %s and t2.debit_or_credit = 'Debit' and t1.posting_date between '%s' and '%s' and t1.account = t2.name"%(ch, mon_list[count][data['start_date']], mon_list[count][data['end_date']]))
+    actual = sql("select sum(ifnull(t1.debit,0))-sum(ifnull(t1.credit,0)) from `tabGL Entry` t1, `tabAccount` t2 where ifnull(t2.is_pl_account, 'No') = 'Yes' and ifnull(t1.is_cancelled, 'No') = 'No' and t1.cost_center in %s and t2.debit_or_credit = 'Debit' and t1.posting_date between '%s' and '%s' and t1.account = t2.name"%(ch, mon_list[count][data['start_date']], mon_list[count][data['end_date']]))
    
     #----------------------------------------------------------
     actual = flt(actual[0][0])
diff --git a/accounts/search_criteria/budget_variance_report/budget_variance_report.sql b/accounts/search_criteria/budget_variance_report/budget_variance_report.sql
index d53f7e1..575fbfb 100644
--- a/accounts/search_criteria/budget_variance_report/budget_variance_report.sql
+++ b/accounts/search_criteria/budget_variance_report/budget_variance_report.sql
@@ -1 +1,10 @@
-SELECT CONCAT(REPEAT('     ', COUNT(parent.name) - 1), node.name) AS name FROM `tabCost Center` AS node,`tabCost Center` AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.docstatus !=2 GROUP BY node.name ORDER BY node.lft
\ No newline at end of file
+SELECT 
+	CONCAT(REPEAT('     ', COUNT(parent.name) - 1), node.name) AS name 
+FROM 
+	`tabCost Center` AS node,`tabCost Center` AS parent 
+WHERE 
+	node.lft BETWEEN parent.lft AND parent.rgt 
+	AND node.docstatus !=2
+	AND node.company_name like '%(company)s%%'
+GROUP BY node.name 
+ORDER BY node.lft
diff --git a/hr/doctype/employee/employee.txt b/hr/doctype/employee/employee.txt
index 49a494b..98caa2c 100644
--- a/hr/doctype/employee/employee.txt
+++ b/hr/doctype/employee/employee.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2011-02-17 11:16:08',
+		'creation': '2010-08-08 17:08:59',
 		'docstatus': 0,
-		'modified': '2011-06-27 14:39:04',
+		'modified': '2011-08-11 17:27:12',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -26,7 +26,7 @@
 		'show_in_menu': 0,
 		'subject': '%(employee_name)s',
 		'tag_fields': 'status',
-		'version': 443
+		'version': 444
 	},
 
 	# These values are common for all DocField
@@ -40,12 +40,14 @@
 
 	# These values are common for all DocPerm
 	{
+		'amend': 0,
 		'doctype': 'DocPerm',
 		'name': '__common__',
 		'parent': 'Employee',
 		'parentfield': 'permissions',
 		'parenttype': 'DocType',
-		'read': 1
+		'read': 1,
+		'submit': 0
 	},
 
 	# DocType, Employee
@@ -56,21 +58,24 @@
 
 	# DocPerm
 	{
-		'cancel': 1,
-		'create': 1,
+		'cancel': 0,
+		'create': 0,
 		'doctype': 'DocPerm',
 		'idx': 1,
 		'permlevel': 0,
-		'role': 'System Manager',
-		'write': 1
+		'role': 'Employee',
+		'write': 0
 	},
 
 	# DocPerm
 	{
+		'cancel': 0,
+		'create': 0,
 		'doctype': 'DocPerm',
 		'idx': 2,
 		'permlevel': 1,
-		'role': 'All'
+		'role': 'All',
+		'write': 0
 	},
 
 	# DocPerm
@@ -95,22 +100,6 @@
 		'write': 1
 	},
 
-	# DocPerm
-	{
-		'doctype': 'DocPerm',
-		'idx': 5,
-		'permlevel': 1,
-		'role': 'HR User'
-	},
-
-	# DocPerm
-	{
-		'doctype': 'DocPerm',
-		'idx': 6,
-		'permlevel': 1,
-		'role': 'HR Manager'
-	},
-
 	# DocField
 	{
 		'doctype': 'DocField',
@@ -141,7 +130,7 @@
 		'no_copy': 1,
 		'oldfieldname': 'naming_series',
 		'oldfieldtype': 'Select',
-		'options': 'WN-EMP\nEMP/',
+		'options': 'EMP/',
 		'permlevel': 0,
 		'reqd': 0
 	},
@@ -173,8 +162,7 @@
 		'oldfieldname': 'employee_name',
 		'oldfieldtype': 'Data',
 		'permlevel': 0,
-		'reqd': 1,
-		'search_index': 1
+		'reqd': 1
 	},
 
 	# DocField
@@ -276,7 +264,7 @@
 		'options': '\nActive\nLeft',
 		'permlevel': 0,
 		'reqd': 1,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
@@ -409,8 +397,7 @@
 		'oldfieldtype': 'Link',
 		'options': 'Department',
 		'permlevel': 0,
-		'reqd': 1,
-		'search_index': 1
+		'reqd': 1
 	},
 
 	# DocField
@@ -477,18 +464,8 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'is_sales_person',
-		'fieldtype': 'Check',
-		'idx': 27,
-		'label': 'Is Sales Person',
-		'permlevel': 0
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 28,
+		'idx': 27,
 		'label': 'Salary Information',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0,
@@ -501,11 +478,11 @@
 		'doctype': 'DocField',
 		'fieldname': 'salary_mode',
 		'fieldtype': 'Select',
-		'idx': 29,
+		'idx': 28,
 		'label': 'Salary Mode',
 		'oldfieldname': 'salary_mode',
 		'oldfieldtype': 'Select',
-		'options': '\nBank\nCash \nCheque',
+		'options': '\nBank\nCash\nCheque',
 		'permlevel': 0,
 		'trigger': 'Client'
 	},
@@ -518,7 +495,7 @@
 		'fieldname': 'bank_name',
 		'fieldtype': 'Data',
 		'hidden': 0,
-		'idx': 30,
+		'idx': 29,
 		'in_filter': 1,
 		'label': 'Bank Name',
 		'oldfieldname': 'bank_name',
@@ -535,7 +512,7 @@
 		'fieldname': 'bank_ac_no',
 		'fieldtype': 'Data',
 		'hidden': 0,
-		'idx': 31,
+		'idx': 30,
 		'label': 'Bank A/C No.',
 		'oldfieldname': 'bank_ac_no',
 		'oldfieldtype': 'Data',
@@ -547,7 +524,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'esic_card_no',
 		'fieldtype': 'Data',
-		'idx': 32,
+		'idx': 31,
 		'label': 'ESIC CARD No',
 		'oldfieldname': 'esic_card_no',
 		'oldfieldtype': 'Data',
@@ -559,7 +536,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'pf_number',
 		'fieldtype': 'Data',
-		'idx': 33,
+		'idx': 32,
 		'label': 'PF Number',
 		'oldfieldname': 'pf_number',
 		'oldfieldtype': 'Data',
@@ -571,7 +548,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'gratuity_lic_id',
 		'fieldtype': 'Data',
-		'idx': 34,
+		'idx': 33,
 		'label': 'Gratuity LIC ID',
 		'oldfieldname': 'gratuity_lic_id',
 		'oldfieldtype': 'Data',
@@ -582,7 +559,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 35,
+		'idx': 34,
 		'label': 'Contact Details',
 		'permlevel': 0
 	},
@@ -591,7 +568,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 36,
+		'idx': 35,
 		'permlevel': 0,
 		'width': '50%'
 	},
@@ -601,7 +578,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'cell_number',
 		'fieldtype': 'Data',
-		'idx': 37,
+		'idx': 36,
 		'label': 'Cell Number',
 		'permlevel': 0
 	},
@@ -611,7 +588,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'personal_email',
 		'fieldtype': 'Data',
-		'idx': 38,
+		'idx': 37,
 		'label': 'Personal Email',
 		'permlevel': 0
 	},
@@ -621,7 +598,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'notice_number_of_days',
 		'fieldtype': 'Int',
-		'idx': 39,
+		'idx': 38,
 		'label': 'Notice - Number of Days',
 		'oldfieldname': 'notice_number_of_days',
 		'oldfieldtype': 'Int',
@@ -632,7 +609,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'HTML',
-		'idx': 40,
+		'idx': 39,
 		'label': 'Emergency Contact Details',
 		'options': '<b>Emergency Contact Details</b>',
 		'permlevel': 0
@@ -643,7 +620,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'person_to_be_contacted',
 		'fieldtype': 'Data',
-		'idx': 41,
+		'idx': 40,
 		'label': 'Person To Be Contacted',
 		'permlevel': 0
 	},
@@ -653,7 +630,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'relation',
 		'fieldtype': 'Data',
-		'idx': 42,
+		'idx': 41,
 		'label': 'Relation',
 		'permlevel': 0
 	},
@@ -663,7 +640,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'emergency_phone_number',
 		'fieldtype': 'Data',
-		'idx': 43,
+		'idx': 42,
 		'label': 'Emergency Phone Number',
 		'permlevel': 0
 	},
@@ -672,7 +649,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 44,
+		'idx': 43,
 		'permlevel': 0,
 		'width': '50%'
 	},
@@ -682,7 +659,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'permanent_accommodation_type',
 		'fieldtype': 'Select',
-		'idx': 45,
+		'idx': 44,
 		'label': 'Permanent Accommodation Type',
 		'options': '\nRented\nOwned',
 		'permlevel': 0
@@ -693,7 +670,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'permanent_address',
 		'fieldtype': 'Small Text',
-		'idx': 46,
+		'idx': 45,
 		'label': 'Permanent Address',
 		'permlevel': 0
 	},
@@ -703,7 +680,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'current_accommodation_type',
 		'fieldtype': 'Select',
-		'idx': 47,
+		'idx': 46,
 		'label': 'Current Accommodation Type',
 		'options': '\nRented\nOwned',
 		'permlevel': 0
@@ -714,7 +691,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'current_address',
 		'fieldtype': 'Small Text',
-		'idx': 48,
+		'idx': 47,
 		'label': 'Current Address',
 		'permlevel': 0
 	},
@@ -723,7 +700,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 49,
+		'idx': 48,
 		'label': 'Personal Details',
 		'permlevel': 0
 	},
@@ -732,7 +709,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 50,
+		'idx': 49,
 		'permlevel': 0,
 		'width': '50%'
 	},
@@ -742,7 +719,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'pan_number',
 		'fieldtype': 'Data',
-		'idx': 51,
+		'idx': 50,
 		'label': 'PAN Number',
 		'permlevel': 0
 	},
@@ -752,7 +729,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'passport_number',
 		'fieldtype': 'Data',
-		'idx': 52,
+		'idx': 51,
 		'label': 'Passport Number',
 		'permlevel': 0
 	},
@@ -763,7 +740,7 @@
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
 		'hidden': 1,
-		'idx': 53,
+		'idx': 52,
 		'label': 'Employee Profile',
 		'oldfieldtype': 'Button',
 		'permlevel': 0,
@@ -775,7 +752,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'date_of_issue',
 		'fieldtype': 'Date',
-		'idx': 54,
+		'idx': 53,
 		'label': 'Date of Issue',
 		'permlevel': 0
 	},
@@ -785,7 +762,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'valid_upto',
 		'fieldtype': 'Date',
-		'idx': 55,
+		'idx': 54,
 		'label': 'Valid Upto',
 		'permlevel': 0
 	},
@@ -796,7 +773,7 @@
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
 		'hidden': 1,
-		'idx': 56,
+		'idx': 55,
 		'label': 'Salary Structure',
 		'oldfieldtype': 'Button',
 		'permlevel': 0,
@@ -808,7 +785,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'place_of_issue',
 		'fieldtype': 'Data',
-		'idx': 57,
+		'idx': 56,
 		'label': 'Place of Issue',
 		'permlevel': 0
 	},
@@ -818,7 +795,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'marital_status',
 		'fieldtype': 'Select',
-		'idx': 58,
+		'idx': 57,
 		'label': 'Marital Status',
 		'options': '\nSingle\nMarried\nDivorced\nWidowed',
 		'permlevel': 0
@@ -829,7 +806,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'blood_group',
 		'fieldtype': 'Select',
-		'idx': 59,
+		'idx': 58,
 		'label': 'Blood Group',
 		'options': '\nA+\nA-\nB+\nB-\nAB+\nAB-\nO+\nO-',
 		'permlevel': 0
@@ -839,7 +816,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 60,
+		'idx': 59,
 		'permlevel': 0,
 		'width': '50%'
 	},
@@ -851,7 +828,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'family_background',
 		'fieldtype': 'Small Text',
-		'idx': 61,
+		'idx': 60,
 		'label': 'Family Background',
 		'permlevel': 0
 	},
@@ -863,7 +840,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'health_details',
 		'fieldtype': 'Small Text',
-		'idx': 62,
+		'idx': 61,
 		'label': 'Health Details',
 		'permlevel': 0
 	},
@@ -872,7 +849,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 63,
+		'idx': 62,
 		'label': 'Educational Qualification',
 		'permlevel': 0
 	},
@@ -882,7 +859,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'educational_qualification_details',
 		'fieldtype': 'Table',
-		'idx': 64,
+		'idx': 63,
 		'label': 'Educational Qualification Details',
 		'options': 'Educational Qualifications Detail',
 		'permlevel': 0
@@ -892,7 +869,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 65,
+		'idx': 64,
 		'label': 'Career History',
 		'permlevel': 0
 	},
@@ -901,7 +878,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 66,
+		'idx': 65,
 		'label': 'Previous Work Experience',
 		'options': 'Simple',
 		'permlevel': 0
@@ -912,7 +889,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'previous_experience_details',
 		'fieldtype': 'Table',
-		'idx': 67,
+		'idx': 66,
 		'label': 'Previous Experience Details',
 		'options': 'Previous Experience Detail',
 		'permlevel': 0
@@ -922,7 +899,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 68,
+		'idx': 67,
 		'label': 'History In Company',
 		'options': 'Simple',
 		'permlevel': 0
@@ -933,7 +910,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'experience_in_company_details',
 		'fieldtype': 'Table',
-		'idx': 69,
+		'idx': 68,
 		'label': 'Experience In Company Details',
 		'options': 'Experience In Company Detail',
 		'permlevel': 0
@@ -943,7 +920,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 70,
+		'idx': 69,
 		'label': 'Exit',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0
@@ -953,7 +930,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 71,
+		'idx': 70,
 		'permlevel': 0,
 		'width': '50%'
 	},
@@ -963,7 +940,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'resignation_letter_date',
 		'fieldtype': 'Date',
-		'idx': 72,
+		'idx': 71,
 		'label': 'Resignation Letter Date',
 		'oldfieldname': 'resignation_letter_date',
 		'oldfieldtype': 'Date',
@@ -975,7 +952,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'relieving_date',
 		'fieldtype': 'Date',
-		'idx': 73,
+		'idx': 72,
 		'in_filter': 1,
 		'label': 'Relieving Date',
 		'oldfieldname': 'relieving_date',
@@ -988,7 +965,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'reason_for_leaving',
 		'fieldtype': 'Data',
-		'idx': 74,
+		'idx': 73,
 		'label': 'Reason for Leaving',
 		'oldfieldname': 'reason_for_leaving',
 		'oldfieldtype': 'Data',
@@ -1000,7 +977,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'leave_encashed',
 		'fieldtype': 'Select',
-		'idx': 75,
+		'idx': 74,
 		'label': 'Leave Encashed?',
 		'oldfieldname': 'leave_encashed',
 		'oldfieldtype': 'Select',
@@ -1013,7 +990,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'encashment_date',
 		'fieldtype': 'Date',
-		'idx': 76,
+		'idx': 75,
 		'label': 'Encashment Date',
 		'oldfieldname': 'encashment_date',
 		'oldfieldtype': 'Date',
@@ -1024,7 +1001,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 77,
+		'idx': 76,
 		'label': 'Exit Interview Details',
 		'oldfieldname': 'col_brk6',
 		'oldfieldtype': 'Column Break',
@@ -1037,7 +1014,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'held_on',
 		'fieldtype': 'Date',
-		'idx': 78,
+		'idx': 77,
 		'label': 'Held On',
 		'oldfieldname': 'held_on',
 		'oldfieldtype': 'Date',
@@ -1049,7 +1026,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'reason_for_resignation',
 		'fieldtype': 'Select',
-		'idx': 79,
+		'idx': 78,
 		'label': 'Reason for Resignation',
 		'oldfieldname': 'reason_for_resignation',
 		'oldfieldtype': 'Select',
@@ -1062,7 +1039,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'new_workplace',
 		'fieldtype': 'Data',
-		'idx': 80,
+		'idx': 79,
 		'label': 'New Workplace',
 		'oldfieldname': 'new_workplace',
 		'oldfieldtype': 'Data',
@@ -1074,7 +1051,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'feedback',
 		'fieldtype': 'Small Text',
-		'idx': 81,
+		'idx': 80,
 		'label': 'Feedback',
 		'oldfieldname': 'feedback',
 		'oldfieldtype': 'Text',
@@ -1087,7 +1064,7 @@
 		'fieldname': 'file_list',
 		'fieldtype': 'Text',
 		'hidden': 1,
-		'idx': 82,
+		'idx': 81,
 		'label': 'File List',
 		'permlevel': 0,
 		'print_hide': 1
@@ -1098,7 +1075,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'trash_reason',
 		'fieldtype': 'Small Text',
-		'idx': 83,
+		'idx': 82,
 		'label': 'Trash Reason',
 		'oldfieldname': 'trash_reason',
 		'oldfieldtype': 'Small Text',
diff --git a/stock/doctype/bin/bin.py b/stock/doctype/bin/bin.py
index 1a1eb70..798b442 100644
--- a/stock/doctype/bin/bin.py
+++ b/stock/doctype/bin/bin.py
@@ -37,7 +37,10 @@
 		self.doc.projected_qty = flt(self.doc.actual_qty) + flt(self.doc.ordered_qty) + flt(self.doc.indented_qty) + flt(self.doc.planned_qty) - flt(self.doc.reserved_qty)
 
 		self.doc.save()
-
+			
+		# check actual qty with total number of serial no
+		self.check_qty_with_serial_no()
+		
 		# update valuation for post dated entry
 		if actual_qty:
 			prev_sle = self.get_prev_sle(dt, posting_time, sle_id)
@@ -48,6 +51,18 @@
 
 			self.update_item_valuation(sle_id, dt, posting_time, serial_no, prev_sle)
 
+	def check_qty_with_serial_no(self):
+		"""
+			check actual qty with total number of serial no in store
+			Temporary validation added on: 18-07-2011
+		"""
+		if sql("select name from `tabItem` where ifnull(has_serial_no, 'No') = 'Yes' and name = '%s'" % self.doc.item_code):
+			sr_count = sql("select count(name) from `tabSerial No` where item_code = '%s' and warehouse = '%s' and status  ='In Store' and docstatus != 2" % (self.doc.item_code, self.doc.warehouse))[0][0]
+			if sr_count != self.doc.actual_qty:
+				msg = "Actual Qty in Bin is mismatched with total number of serial no in store for item: '%s' and warehouse: '%s'" % (self.doc.item_code, self.doc.warehouse)
+				msgprint(msg, raise_exception=1)
+				sendmail(['developer@iwebnotes.com'], sender='automail@webnotestech.com', subject='Serial No Count vs Bin Actual Qty', parts=[['text/plain', msg]])			
+
 	# --------------------------------
 	# get first stock ledger entry
 	# --------------------------------
diff --git a/stock/doctype/serial_no/serial_no.js b/stock/doctype/serial_no/serial_no.js
index 32620dd..a41113c 100644
--- a/stock/doctype/serial_no/serial_no.js
+++ b/stock/doctype/serial_no/serial_no.js
@@ -7,15 +7,16 @@
 
 // ************************************** refresh ***************************************************
 cur_frm.cscript.refresh = function(doc, cdt, cdn) {
-  if(!doc.__islocal && doc.warehouse) set_field_permlevel('warehouse', 1);
-  if(!doc.__islocal && doc.item_code) set_field_permlevel('item_code', 1);
-  if(doc.__islocal) set_field_permlevel('status', 0);
+  if(!doc.__islocal) {
+    flds = ['item_code', 'warehouse', 'purchase_document_type', 'purchase_document_no', 'purchase_date', 'purchase_time', 'purchase_rate', 'supplier']
+    for(i=0;i<flds.length;i++) 
+      set_field_permlevel(flds[i], 1);
+  }
 }
 
 
 // ************************************** triggers **************************************************
 
-// -------------
 // item details
 // -------------
 cur_frm.add_fetch('item_code', 'item_name', 'item_name')
@@ -24,23 +25,12 @@
 cur_frm.add_fetch('item_code', 'description', 'description')
 cur_frm.add_fetch('item_code', 'warranty_period', 'warranty_period')
 
-
-// ---------
 // customer
 // ---------
 cur_frm.add_fetch('customer', 'customer_name', 'customer_name')
 cur_frm.add_fetch('customer', 'address', 'delivery_address')
 cur_frm.add_fetch('customer', 'territory', 'territory')
 
-
-// ---------
-// supplier
-// ---------
-//cur_frm.add_fetch('supplier', 'supplier_name', 'supplier_name')
-//cur_frm.add_fetch('customer', 'address', 'supplier_address')
-
-
-// ----------
 // territory
 // ----------
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
@@ -48,10 +38,8 @@
 }
 
 // Supplier
+//-------------
 cur_frm.cscript.supplier = function(doc,dt,dn) {
   if(doc.supplier) get_server_fields('get_default_supplier_address', JSON.stringify({supplier: doc.supplier}),'', doc, dt, dn, 1);
   if(doc.supplier) unhide_field(['supplier_name','address_display']);
 }
-
-
-
diff --git a/stock/doctype/serial_no/serial_no.py b/stock/doctype/serial_no/serial_no.py
index 9c99357..cf7fd50 100644
--- a/stock/doctype/serial_no/serial_no.py
+++ b/stock/doctype/serial_no/serial_no.py
@@ -16,39 +16,19 @@
 		self.doc = doc
 		self.doclist = doclist
 
-
-# ********************************* validate warranty / amc status ***************************************
-
-	# --------------------
-	# validate amc status
-	# --------------------
 	def validate_amc_status(self):
-		if self.doc.amc_expiry_date and getdate(self.doc.amc_expiry_date) >= datetime.date.today() and self.doc.maintenance_status == 'Out of AMC':
-			msgprint("AMC expiry date and maintenance status mismatch. Please verify")
-			raise Exception
-		elif (not self.doc.amc_expiry_date or getdate(self.doc.amc_expiry_date) < datetime.date.today()) and self.doc.maintenance_status == 'Under AMC':
-			msgprint("AMC expiry date and maintenance status mismatch. Please verify")
-			raise Exception
+		"""
+			validate amc status
+		"""
+		if (self.doc.maintenance_status == 'Out of AMC' and self.doc.amc_expiry_date and getdate(self.doc.amc_expiry_date) >= datetime.date.today()) or (self.doc.maintenance_status == 'Under AMC' and (not self.doc.amc_expiry_date or getdate(self.doc.amc_expiry_date) < datetime.date.today())):
+			msgprint("AMC expiry date and maintenance status mismatch. Please verify", raise_exception=1)
 
-
-	# -------------------------
-	# validate warranty status
-	# -------------------------
 	def validate_warranty_status(self):
-		if self.doc.warranty_expiry_date and getdate(self.doc.warranty_expiry_date) >= datetime.date.today() and self.doc.maintenance_status == 'Out of Warranty':
-			msgprint("Warranty expiry date and maintenance status mismatch. Please verify")
-			raise Exception
-		elif (not self.doc.warranty_expiry_date or getdate(self.doc.warranty_expiry_date) < datetime.date.today()) and self.doc.maintenance_status == 'Under Warranty':
-			msgprint("Warranty expiry date and maintenance status mismatch. Please verify")
-			raise Exception
-
-
-	# -------------------------------
-	# validate warranty / amc status
-	# -------------------------------
-	def validate_warranty_amc_status(self):
-		self.validate_warranty_status()
-		self.validate_amc_status()
+		"""
+			validate warranty status	
+		"""
+		if (self.doc.maintenance_status == 'Out of Warranty' and self.doc.warranty_expiry_date and getdate(self.doc.warranty_expiry_date) >= datetime.date.today()) or (self.doc.maintenance_status == 'Under Warranty' and (not self.doc.warranty_expiry_date or getdate(self.doc.warranty_expiry_date) < datetime.date.today())):
+			msgprint("Warranty expiry date and maintenance status mismatch. Please verify", raise_exception=1)
 
 
 	def validate_warehouse(self):
@@ -56,6 +36,9 @@
 			msgprint("Warehouse is mandatory if this Serial No is <b>In Store</b>", raise_exception=1)
 
 	def validate_item(self):
+		"""
+			Validate whether serial no is required for this item
+		"""
 		item = sql("select name, has_serial_no from tabItem where name = '%s'" % self.doc.item_code)
 		if not item:
 			msgprint("Item is not exists in the system", raise_exception=1)
@@ -67,7 +50,8 @@
 	# validate
 	# ---------
 	def validate(self):
-		self.validate_warranty_amc_status()
+		self.validate_warranty_status()
+		self.validate_amc_status()
 		self.validate_warehouse()
 		self.validate_item()
 
@@ -102,7 +86,7 @@
 	# on update
 	# ----------
 	def on_update(self):
-		if self.doc.warehouse and not sql("select name from `tabStock Ledger Entry` where serial_no = '%s'" % (self.doc.name)) and self.doc.status == 'In Store':
+		if self.doc.localname and self.doc.warehouse and self.doc.status == 'In Store' and not sql("select name from `tabStock Ledger Entry` where serial_no = '%s' and ifnull(is_cancelled, 'No') = 'No'" % (self.doc.name)):
 			self.make_stock_ledger_entry(update_stock = 1)
 
 
diff --git a/stock/doctype/stock_entry/stock_entry.py b/stock/doctype/stock_entry/stock_entry.py
index 47f0394..024bd1f 100644
--- a/stock/doctype/stock_entry/stock_entry.py
+++ b/stock/doctype/stock_entry/stock_entry.py
@@ -355,6 +355,9 @@
 	# ----------------------------------
 	def update_serial_no(self, is_submit):
 		sl_obj = get_obj('Stock Ledger')
+		if is_submit:
+			sl_obj.validate_serial_no_warehouse(self, 'mtn_details')
+		
 		for d in getlist(self.doclist, 'mtn_details'):
 			if d.serial_no:
 				serial_nos = sl_obj.get_sr_no_list(d.serial_no)
@@ -363,7 +366,7 @@
 					if d.s_warehouse:
 						sl_obj.update_serial_delivery_details(self, d, serial_no, is_submit)
 					if d.t_warehouse:
-						sl_obj.update_serial_purchase_details(self, d, serial_no, is_submit, (self.doc.purpose in ['Material Transfer', 'Sales Return']) and 1 or 0)
+						sl_obj.update_serial_purchase_details(self, d, serial_no, is_submit, self.doc.purpose)
 					
 					if self.doc.purpose == 'Purchase Return':
 						delete_doc("Serial No", serial_no)
diff --git a/stock/doctype/stock_ledger/stock_ledger.py b/stock/doctype/stock_ledger/stock_ledger.py
index 5188e08..dca9754 100644
--- a/stock/doctype/stock_ledger/stock_ledger.py
+++ b/stock/doctype/stock_ledger/stock_ledger.py
@@ -57,11 +57,11 @@
 					s = s.strip()
 					sr_war = sql("select warehouse,name from `tabSerial No` where name = '%s'" % (s))
 					if not sr_war:
-						msgprint("Serial No %s does not exists",s, raise_exception = 1)
+						msgprint("Serial No %s does not exists"%s, raise_exception = 1)
 					elif not sr_war[0][0]:
-						msgprint("Please set a warehouse in the Serial No <b>%s</b>" % s, raise_exception = 1)
-					elif sr_war[0][0] != d.warehouse:
-						msgprint("Serial No : %s for Item : %s doesn't exists in Warehouse : %s" % (s, d.item_code, d.warehouse), raise_exception = 1)
+						msgprint("Warehouse not mentioned in the Serial No <b>%s</b>" % s, raise_exception = 1)
+					elif (d.warehouse and sr_war[0][0] != d.warehouse) or (d.s_warehouse and sr_war[0][0] != d.s_warehouse):
+						msgprint("Serial No : %s for Item : %s doesn't exists in Warehouse : %s" % (s, d.item_code, d.warehouse or d.s_warehouse), raise_exception = 1)
 
 
 	# ------------------------------------
@@ -119,10 +119,10 @@
 	# ----------------------------------
 	# update serial no purchase details
 	# ----------------------------------
-	def update_serial_purchase_details(self, obj, d, serial_no, is_submit, is_transfer = 0):
+	def update_serial_purchase_details(self, obj, d, serial_no, is_submit, purpose = ''):
 		exists = sql("select name, status, docstatus from `tabSerial No` where name = '%s'" % (serial_no))
 		if is_submit:
-			if exists and exists[0][2] != 2 and not is_transfer:
+			if exists and exists[0][2] != 2 and purpose not in ['Material Transfer', 'Sales Return']:
 				msgprint("Serial No: %s already %s" % (serial_no, exists and exists[0][1]), raise_exception = 1)
 			elif exists:
 				s = Document('Serial No', exists and exists[0][0])
@@ -133,8 +133,10 @@
 		else:
 			if exists and exists[0][1] == 'Delivered' and exists[0][2] != 2:
 				msgprint("Serial No: %s is already delivered, you can not cancel the document." % serial_no, raise_exception=1)
+			elif purpose in ['Material Transfer', 'Sales Return']:
+				sql("update `tabSerial No` set status = '%s', purchase_document_type = '', purchase_document_no = '', warehouse = '%s' where name = '%s'" % (purpose == 'Material Transfer' and 'In Store' or 'Delivered', d.s_warehouse, serial_no))				
 			else:
-				sql("update `tabSerial No` set docstatus = '%s', status = '%s', purchase_document_type = '', purchase_document_no = '', purchase_date = '', purchase_rate = '', supplier = null, supplier_name = '', supplier_address = '', warehouse = null where name = '%s'" % (not is_transfer and 2 or 0, is_transfer and 'In Store' or 'Not in Use', serial_no))
+				sql("update `tabSerial No` set docstatus = 2, status = 'Not in Use', purchase_document_type = '', purchase_document_no = '', purchase_date = '', purchase_rate = '', supplier = null, supplier_name = '', supplier_address = '', warehouse = '' where name = '%s'" % serial_no)
 
 
 	# -------------------------------
@@ -233,6 +235,7 @@
 				sle.fields['warehouse_type'] = get_value('Warehouse' , args[k], 'warehouse_type')
 			sle.fields[k] = args[k]
 		sle_obj = get_obj(doc=sle)
+		
 		# validate
 		sle_obj.validate()
 		sle.save(new = 1)