merge
diff --git a/accounts/doctype/journal_voucher/journal_voucher.txt b/accounts/doctype/journal_voucher/journal_voucher.txt
index b30c4db..09c6148 100644
--- a/accounts/doctype/journal_voucher/journal_voucher.txt
+++ b/accounts/doctype/journal_voucher/journal_voucher.txt
@@ -2,9 +2,9 @@
  {
   "owner": "Administrator", 
   "docstatus": 0, 
-  "creation": "2013-01-17 12:30:11", 
+  "creation": "2013-01-18 10:46:42", 
   "modified_by": "Administrator", 
-  "modified": "2013-01-17 14:27:43"
+  "modified": "2013-01-18 17:44:58"
  }, 
  {
   "autoname": "naming_series:", 
@@ -28,6 +28,7 @@
   "read": 1, 
   "doctype": "DocPerm", 
   "parenttype": "DocType", 
+  "report": 1, 
   "parentfield": "permissions"
  }, 
  {
@@ -102,8 +103,8 @@
  }, 
  {
   "print_hide": 0, 
-  "oldfieldtype": "Table", 
   "allow_on_submit": 1, 
+  "oldfieldtype": "Table", 
   "doctype": "DocField", 
   "label": "Entries", 
   "oldfieldname": "entries", 
@@ -461,26 +462,13 @@
   "permlevel": 1
  }, 
  {
-  "print_hide": 1, 
-  "no_copy": 1, 
-  "oldfieldtype": "Link", 
-  "doctype": "DocField", 
-  "label": "Supplier Account", 
-  "oldfieldname": "supplier_account", 
-  "options": "Account", 
-  "fieldname": "supplier_account", 
-  "fieldtype": "Link", 
-  "hidden": 0, 
-  "permlevel": 0
- }, 
- {
   "amend": 1, 
   "create": 1, 
   "doctype": "DocPerm", 
   "submit": 1, 
   "write": 1, 
-  "role": "Accounts User", 
   "cancel": 1, 
+  "role": "Accounts User", 
   "permlevel": 0
  }, 
  {
@@ -489,8 +477,8 @@
   "doctype": "DocPerm", 
   "submit": 0, 
   "write": 0, 
-  "role": "Accounts User", 
   "cancel": 0, 
+  "role": "Accounts User", 
   "permlevel": 1
  }, 
  {
@@ -499,8 +487,8 @@
   "doctype": "DocPerm", 
   "submit": 0, 
   "write": 0, 
-  "role": "Accounts Manager", 
   "cancel": 0, 
+  "role": "Accounts Manager", 
   "permlevel": 1
  }, 
  {
@@ -509,8 +497,8 @@
   "doctype": "DocPerm", 
   "submit": 1, 
   "write": 1, 
-  "role": "Accounts Manager", 
   "cancel": 1, 
+  "role": "Accounts Manager", 
   "permlevel": 0
  }, 
  {
@@ -519,8 +507,8 @@
   "doctype": "DocPerm", 
   "submit": 0, 
   "write": 0, 
-  "role": "Auditor", 
   "cancel": 0, 
+  "role": "Auditor", 
   "permlevel": 0
  }, 
  {
@@ -529,8 +517,8 @@
   "doctype": "DocPerm", 
   "submit": 0, 
   "write": 0, 
-  "role": "Auditor", 
   "cancel": 0, 
+  "role": "Auditor", 
   "permlevel": 1
  }
 ]
\ No newline at end of file
diff --git a/hr/doctype/appraisal/appraisal.py b/hr/doctype/appraisal/appraisal.py
index 82fb77b..849e48a 100644
--- a/hr/doctype/appraisal/appraisal.py
+++ b/hr/doctype/appraisal/appraisal.py
@@ -29,7 +29,15 @@
 	def __init__(self, doc, doclist=[]):
 		self.doc = doc
 		self.doclist = doclist
-	
+
+	def validate(self):
+		if not self.doc.status:
+			self.doc.status = "Draft"
+
+		self.validate_dates()
+		self.validate_existing_appraisal()
+		self.calculate_total()
+
 	def get_employee_name(self):
 		emp_nm = sql("select employee_name from `tabEmployee` where name=%s", self.doc.employee)
 		emp_nm= emp_nm and emp_nm[0][0] or ''
@@ -47,25 +55,19 @@
 			raise Exception
 	
 	def validate_existing_appraisal(self):
-		chk = sql("select name from `tabAppraisal` where employee=%s and (status='Submitted' or status='Completed') and ((start_date>=%s and start_date<=%s) or (end_date>=%s and end_date<=%s))",(self.doc.employee,self.doc.start_date,self.doc.end_date,self.doc.start_date,self.doc.end_date))
+		chk = sql("""select name from `tabAppraisal` where employee=%s 
+			and (status='Submitted' or status='Completed') 
+			and ((start_date>=%s and start_date<=%s) 
+			or (end_date>=%s and end_date<=%s))""",(self.doc.employee,self.doc.start_date,self.doc.end_date,self.doc.start_date,self.doc.end_date))
 		if chk:
 			msgprint("You have already created Appraisal "\
 				+cstr(chk[0][0])+" in the current date range for employee "\
 				+cstr(self.doc.employee_name))
 			raise Exception
-		
-	def validate(self):
-		if not self.doc.status:
-			self.doc.status = "Draft"
-
-		self.validate_dates()
-		self.validate_existing_appraisal()
-		self.calculate_total()
 	
 	def calculate_total(self):
 		total, total_w  = 0, 0
 		for d in getlist(self.doclist, 'appraisal_details'):
-			
 			if d.score:
 				d.score_earned = flt(d.score) * flt(d.per_weightage) / 100
 				total = total + d.score_earned
@@ -74,9 +76,12 @@
 		if int(total_w) != 100:
 			msgprint("Total weightage assigned should be 100%. It is :" + str(total_w) + "%", 
 				raise_exception=1)
+
+		if webnotes.conn.get_default("employee", webnotes.session.user) != self.doc.employee:
 		
-		if total==0:
-			msgprint("Total can't be zero. You must atleast give some points!", raise_exception=1)
+			if total==0:
+				msgprint("Total can't be zero. You must atleast give some points!", raise_exception=1)
+
 		self.doc.total_score = total
 			
 	def on_submit(self):
diff --git a/manufacturing/doctype/production_planning_tool/production_planning_tool.txt b/manufacturing/doctype/production_planning_tool/production_planning_tool.txt
index d4a0d94..dc1eb51 100644
--- a/manufacturing/doctype/production_planning_tool/production_planning_tool.txt
+++ b/manufacturing/doctype/production_planning_tool/production_planning_tool.txt
@@ -2,9 +2,9 @@
  {
   "owner": "jai@webnotestech.com", 
   "docstatus": 0, 
-  "creation": "2013-01-16 14:48:56", 
+  "creation": "2013-01-19 10:23:35", 
   "modified_by": "Administrator", 
-  "modified": "2013-01-17 11:39:55"
+  "modified": "2013-01-19 10:55:55"
  }, 
  {
   "read_only": 1, 
@@ -31,8 +31,8 @@
   "submit": 0, 
   "doctype": "DocPerm", 
   "write": 1, 
-  "report": 0, 
   "parenttype": "DocType", 
+  "report": 0, 
   "permlevel": 0, 
   "parentfield": "permissions"
  }, 
@@ -70,9 +70,9 @@
  {
   "doctype": "DocField", 
   "label": "Company", 
-  "reqd": 1, 
   "fieldname": "company", 
   "fieldtype": "Link", 
+  "reqd": 1, 
   "options": "Company"
  }, 
  {
@@ -161,9 +161,9 @@
   "default": "1", 
   "doctype": "DocField", 
   "label": "Use Multi-Level BOM", 
-  "reqd": 0, 
   "fieldname": "use_multi_level_bom", 
-  "fieldtype": "Check"
+  "fieldtype": "Check", 
+  "reqd": 0
  }, 
  {
   "doctype": "DocField", 
@@ -181,9 +181,9 @@
  }, 
  {
   "doctype": "DocField", 
+  "label": "Materials Requirement Planning (MRP)", 
   "fieldname": "sb5", 
-  "fieldtype": "Section Break", 
-  "options": "Simple"
+  "fieldtype": "Section Break"
  }, 
  {
   "description": "Download a report containing all raw materials with their latest inventory status", 
@@ -214,6 +214,10 @@
   "options": "raise_purchase_request"
  }, 
  {
+  "role": "System Manager", 
+  "doctype": "DocPerm"
+ }, 
+ {
   "role": "Manufacturing User", 
   "doctype": "DocPerm"
  }, 
diff --git a/patches/january_2013/change_patch_structure.py b/patches/january_2013/change_patch_structure.py
index 37c4f08..1370cdd 100644
--- a/patches/january_2013/change_patch_structure.py
+++ b/patches/january_2013/change_patch_structure.py
@@ -2,11 +2,12 @@
 
 def execute():
 	webnotes.reload_doc("core", "doctype", "patch_log")
-	for d in webnotes.conn.sql("""select patch from __PatchLog"""):
-		webnotes.doc({
-			"doctype": "Patch Log",
-			"patch": d[0]
-		}).insert()
+	if webnotes.conn.table_exists("__PatchLog"):
+		for d in webnotes.conn.sql("""select patch from __PatchLog"""):
+			webnotes.doc({
+				"doctype": "Patch Log",
+				"patch": d[0]
+			}).insert()
 	
-	webnotes.conn.commit()
-	webnotes.conn.sql("drop table __PatchLog")
\ No newline at end of file
+		webnotes.conn.commit()
+		webnotes.conn.sql("drop table __PatchLog")
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 16310ea..2788093 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -151,5 +151,6 @@
 	"patches.january_2013.change_patch_structure",
 	"patches.january_2013.update_country_info",
 	"patches.january_2013.remove_tds_entry_from_gl_mapper",
+	"patches.january_2013.update_number_format",
 	"patches.january_2013.purchase_price_list",
 ]
\ No newline at end of file
diff --git a/setup/doctype/currency/currency.js b/setup/doctype/currency/currency.js
index cd42c18..9a18372 100644
--- a/setup/doctype/currency/currency.js
+++ b/setup/doctype/currency/currency.js
@@ -1,3 +1,2 @@
 cur_frm.cscript.refresh = function(doc) {
-	cur_frm.set_intro(doc.__islocal ? "" : "There is nothing to edit.")
 }
\ No newline at end of file
diff --git a/setup/doctype/currency/currency.txt b/setup/doctype/currency/currency.txt
index fd75525..c8dbb62 100644
--- a/setup/doctype/currency/currency.txt
+++ b/setup/doctype/currency/currency.txt
@@ -2,9 +2,9 @@
  {
   "owner": "Administrator", 
   "docstatus": 0, 
-  "creation": "2013-01-17 12:30:28", 
+  "creation": "2013-01-19 10:23:31", 
   "modified_by": "Administrator", 
-  "modified": "2013-01-17 16:24:59"
+  "modified": "2013-01-19 11:10:45"
  }, 
  {
   "read_only": 0, 
@@ -27,8 +27,12 @@
   "name": "__common__", 
   "parent": "Currency", 
   "read": 1, 
+  "create": 1, 
+  "submit": 0, 
   "doctype": "DocPerm", 
+  "write": 1, 
   "parenttype": "DocType", 
+  "report": 1, 
   "permlevel": 0, 
   "parentfield": "permissions"
  }, 
@@ -64,17 +68,26 @@
   "fieldtype": "Data"
  }, 
  {
+  "doctype": "DocField", 
+  "label": "Number Format", 
+  "fieldname": "number_format", 
+  "fieldtype": "Data"
+ }, 
+ {
+  "cancel": 1, 
+  "role": "Accounts Manager", 
+  "doctype": "DocPerm"
+ }, 
+ {
   "amend": 0, 
-  "create": 1, 
   "doctype": "DocPerm", 
-  "submit": 0, 
-  "write": 1, 
-  "report": 1, 
-  "role": "System Manager", 
+  "role": "Sales Master Manager", 
   "cancel": 1
  }, 
  {
-  "role": "All", 
-  "doctype": "DocPerm"
+  "amend": 0, 
+  "doctype": "DocPerm", 
+  "role": "Purchase Master Manager", 
+  "cancel": 0
  }
 ]
\ No newline at end of file
diff --git a/startup/install.py b/startup/install.py
index e24f815..00b7b84 100644
--- a/startup/install.py
+++ b/startup/install.py
@@ -94,7 +94,8 @@
 				"currency_name": country.currency,
 				"fraction": country.currency_fraction,
 				"symbol": country.currency_symbol,
-				"fraction_units": country.currency_fraction_units
+				"fraction_units": country.currency_fraction_units,
+				"number_format": country.number_format
 			}).insert()
 
 def import_defaults():
diff --git a/stock/doctype/item/item.js b/stock/doctype/item/item.js
index ab8fb43..6c2c882 100644
--- a/stock/doctype/item/item.js
+++ b/stock/doctype/item/item.js
@@ -36,20 +36,6 @@
 	if(!doc.description) cur_frm.set_value("description", doc.item_code);
 }
 
-cur_frm.cscript.hide_website_fields = function(doc) {
-	var website_fields_list = ['page_name', 'website_image', 'web_short_description',
-								'web_long_description'];
-	if (doc && cint(doc.show_in_website)) {
-		unhide_field(website_fields_list);
-	} else {
-		hide_field(website_fields_list);
-	}
-}
-
-cur_frm.cscript.show_in_website = function(doc, dt, dn) {
-	cur_frm.cscript.hide_website_fields(doc);
-}
-
 cur_frm.fields_dict['default_bom'].get_query = function(doc) {
    //var d = locals[this.doctype][this.docname];
    return 'SELECT DISTINCT `tabBOM`.`name` FROM `tabBOM` WHERE `tabBOM`.`item` = "' + doc.item_code + '"  AND ifnull(`tabBOM`.`is_active`, 0) = 0 and `tabBOM`.docstatus != 2 AND `tabBOM`.%(key)s LIKE "%s" ORDER BY `tabBOM`.`name` LIMIT 50'
diff --git a/stock/doctype/item/item.py b/stock/doctype/item/item.py
index cca814f..748f01a 100644
--- a/stock/doctype/item/item.py
+++ b/stock/doctype/item/item.py
@@ -77,7 +77,12 @@
 
 	def update_website(self):
 		from website.utils import update_page_name
-		update_page_name(self.doc, self.doc.name + " " + self.doc.item_name)
+		if self.doc.name==self.doc.item_name:
+			page_name_from = self.doc.name
+		else:
+			page_name_from = self.doc.name + " " + self.doc.item_name
+
+		update_page_name(self.doc, page_name_from)
 		
 		from website.helpers.product import invalidate_cache_for
 		invalidate_cache_for(self.doc.item_group)
diff --git a/stock/doctype/item/item.txt b/stock/doctype/item/item.txt
index 9608367..c7e88cc 100644
--- a/stock/doctype/item/item.txt
+++ b/stock/doctype/item/item.txt
@@ -32,9 +32,10 @@
   "read": 1, 
   "submit": 0, 
   "doctype": "DocPerm", 
-  "amend": 0, 
   "parenttype": "DocType", 
   "report": 1, 
+  "permlevel": 0, 
+  "amend": 0, 
   "parentfield": "permissions"
  }, 
  {
@@ -423,6 +424,7 @@
   "permlevel": 1
  }, 
  {
+  "read_only": 1, 
   "no_copy": 1, 
   "oldfieldtype": "Currency", 
   "doctype": "DocField", 
@@ -431,7 +433,7 @@
   "fieldname": "last_purchase_rate", 
   "fieldtype": "Currency", 
   "depends_on": "eval:doc.is_purchase_item==\"Yes\"", 
-  "permlevel": 1
+  "permlevel": 0
  }, 
  {
   "oldfieldtype": "Currency", 
@@ -776,11 +778,12 @@
  }, 
  {
   "description": "Products will be sorted by weight-age in default searches. More the weight-age, higher the product will appear in the list.", 
-  "search_index": 1, 
+  "depends_on": "show_in_website", 
   "doctype": "DocField", 
   "label": "Weightage", 
   "fieldname": "weightage", 
   "fieldtype": "Int", 
+  "search_index": 1, 
   "permlevel": 0
  }, 
  {
@@ -864,35 +867,26 @@
   "permlevel": 0
  }, 
  {
+  "amend": 0, 
+  "create": 0, 
+  "doctype": "DocPerm", 
+  "write": 0, 
+  "role": "Material Manager", 
+  "cancel": 0
+ }, 
+ {
+  "amend": 0, 
+  "create": 0, 
+  "doctype": "DocPerm", 
+  "write": 0, 
+  "role": "Material User", 
+  "cancel": 0
+ }, 
+ {
   "create": 1, 
   "doctype": "DocPerm", 
   "write": 1, 
-  "cancel": 1, 
   "role": "Material Master Manager", 
-  "permlevel": 0
- }, 
- {
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "write": 0, 
-  "cancel": 0, 
-  "role": "Material User", 
-  "permlevel": 0
- }, 
- {
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "write": 0, 
-  "cancel": 0, 
-  "role": "Material Master Manager", 
-  "permlevel": 1
- }, 
- {
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "write": 0, 
-  "cancel": 0, 
-  "role": "Material User", 
-  "permlevel": 1
+  "cancel": 1
  }
 ]
\ No newline at end of file
diff --git a/stock/doctype/stock_entry/stock_entry.py b/stock/doctype/stock_entry/stock_entry.py
index 00ae0e0..6d09045 100644
--- a/stock/doctype/stock_entry/stock_entry.py
+++ b/stock/doctype/stock_entry/stock_entry.py
@@ -17,10 +17,9 @@
 from __future__ import unicode_literals
 import webnotes
 
-from webnotes.utils import cstr, cint, flt, getdate, now, comma_or
-from webnotes.model import db_exists, delete_doc
+from webnotes.utils import cstr, cint, flt, comma_or
 from webnotes.model.doc import Document, addchild
-from webnotes.model.wrapper import getlist, copy_doclist
+from webnotes.model.wrapper import getlist
 from webnotes.model.code import get_obj
 from webnotes import msgprint, _
 from stock.utils import get_incoming_rate
@@ -51,6 +50,7 @@
 		self.validate_incoming_rate()
 		self.validate_bom()
 		self.validate_finished_goods()
+		self.validate_return_reference_doc()
 		
 	def on_submit(self):
 		self.update_serial_no(1)
@@ -85,6 +85,15 @@
 		validate_for_manufacture_repack = any([d.bom_no for d in self.doclist.get(
 			{"parentfield": "mtn_details"})])
 
+		if self.doc.purpose in source_mandatory and self.doc.purpose not in target_mandatory:
+			self.doc.to_warehouse = None
+			for d in getlist(self.doclist, 'mtn_details'):
+				d.t_warehouse = None
+		elif self.doc.purpose in target_mandatory and self.doc.purpose not in source_mandatory:
+			self.doc.from_warehouse = None
+			for d in getlist(self.doclist, 'mtn_details'):
+				d.s_warehouse = None
+
 		for d in getlist(self.doclist, 'mtn_details'):
 			if not d.s_warehouse and not d.t_warehouse:
 				d.s_warehouse = self.doc.from_warehouse
@@ -93,21 +102,13 @@
 			if not (d.s_warehouse or d.t_warehouse):
 				msgprint(_("Atleast one warehouse is mandatory"), raise_exception=1)
 			
-			if self.doc.purpose in source_mandatory:
-				if not d.s_warehouse:
-					msgprint(_("Row # ") + "%s: " % cint(d.idx)
-						+ _("Source Warehouse") + _(" is mandatory"), raise_exception=1)
-
-				if self.doc.purpose not in target_mandatory:
-					d.t_warehouse = None
+			if self.doc.purpose in source_mandatory and not d.s_warehouse:
+				msgprint(_("Row # ") + "%s: " % cint(d.idx)
+					+ _("Source Warehouse") + _(" is mandatory"), raise_exception=1)
 				
-			if self.doc.purpose in target_mandatory:
-				if not d.t_warehouse:
-					msgprint(_("Row # ") + "%s: " % cint(d.idx)
-						+ _("Target Warehouse") + _(" is mandatory"), raise_exception=1)
-						
-				if self.doc.purpose not in source_mandatory:
-					d.s_warehouse = None
+			if self.doc.purpose in target_mandatory and not d.t_warehouse:
+				msgprint(_("Row # ") + "%s: " % cint(d.idx)
+					+ _("Target Warehouse") + _(" is mandatory"), raise_exception=1)
 
 			if self.doc.purpose == "Manufacture/Repack":
 				if validate_for_manufacture_repack:
@@ -225,7 +226,28 @@
 					+ _("Quantity should be equal to Manufacturing Quantity. ")
 					+ _("To fetch items again, click on 'Get Items' button \
 						or update the Quantity manually."), raise_exception=1)
+						
+	def validate_return_reference_doc(self):
+		""" validate item with reference doc"""
+		ref_doctype = ref_docname = ""
+		if self.doc.purpose == "Sales Return" and \
+				(self.doc.delivery_note_no or self.doc.sales_invoice_no):
+			ref_doctype = self.doc.delivery_note_no and "Delivery Note" or "Sales Invoice"
+			ref_docname = self.doc.delivery_note_no or self.doc.sales_invoice_no
+		elif self.doc.purpose == "Purchase Return" and self.doc.purchase_receipt_no:
+			ref_doctype = "Purchase Receipt"
+			ref_docname = self.doc.purchase_receipt_no
+			
+		if ref_doctype and ref_docname:
+			for item in self.doclist.get({"parentfield": "mtn_details"}):
+				ref_exists = webnotes.conn.sql("""select name from `tab%s` 
+					where parent = %s and item_code = %s and docstatus=1""" % 
+					(ref_doctype + " Item", '%s', '%s'), (ref_docname, item.item_code))
 					
+				if not ref_exists:
+					msgprint(_("Item: '") + item.item_code + _("' does not exists in ") +
+						ref_doctype + ": " + ref_docname, raise_exception=1)
+			
 	def update_serial_no(self, is_submit):
 		"""Create / Update Serial No"""
 		from stock.utils import get_valid_serial_nos
diff --git a/website/helpers/contact.py b/website/helpers/contact.py
index df4510f..a9499ab 100644
--- a/website/helpers/contact.py
+++ b/website/helpers/contact.py
@@ -50,7 +50,7 @@
 		lead.ignore_permissions = True
 		lead.insert()
 	
-	make(content=message, sender=sender, 
+	make(content=message, sender=sender, subject=subject,
 		doctype="Lead", name=lead.doc.name, lead=lead.doc.name)