Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/erpnext/accounts/doctype/purchase_tax_detail/purchase_tax_detail.txt b/erpnext/accounts/doctype/purchase_tax_detail/purchase_tax_detail.txt
index ed67ba2..58f66c7 100644
--- a/erpnext/accounts/doctype/purchase_tax_detail/purchase_tax_detail.txt
+++ b/erpnext/accounts/doctype/purchase_tax_detail/purchase_tax_detail.txt
@@ -5,8 +5,8 @@
 	{
 		'creation': '2010-08-08 17:09:16',
 		'docstatus': 0,
-		'modified': '2010-09-20 14:06:57',
-		'modified_by': 'umair@iwebnotes.com',
+		'modified': '2011-11-16 15:41:42',
+		'modified_by': 'Administrator',
 		'owner': 'wasim@webnotestech.com'
 	},
 
@@ -14,6 +14,7 @@
 	{
 		'autoname': 'PVTD.######',
 		'colour': 'White:FFF',
+		'default_print_format': 'Standard',
 		'doctype': 'DocType',
 		'hide_heading': 1,
 		'istable': 1,
@@ -22,7 +23,7 @@
 		'section_style': 'Tray',
 		'server_code_error': ' ',
 		'show_in_menu': 0,
-		'version': 10
+		'version': 12
 	},
 
 	# These values are common for all DocField
@@ -45,7 +46,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'category',
 		'fieldtype': 'Select',
-		'idx': 1,
 		'label': 'Category',
 		'oldfieldname': 'category',
 		'oldfieldtype': 'Select',
@@ -60,7 +60,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'add_deduct_tax',
 		'fieldtype': 'Select',
-		'idx': 2,
 		'label': 'Add or Deduct',
 		'oldfieldname': 'add_deduct_tax',
 		'oldfieldtype': 'Select',
@@ -75,7 +74,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'charge_type',
 		'fieldtype': 'Select',
-		'idx': 3,
 		'label': 'Type',
 		'oldfieldname': 'charge_type',
 		'oldfieldtype': 'Select',
@@ -91,7 +89,6 @@
 		'fieldname': 'row_id',
 		'fieldtype': 'Data',
 		'hidden': 0,
-		'idx': 4,
 		'label': 'Enter Row',
 		'oldfieldname': 'row_id',
 		'oldfieldtype': 'Data',
@@ -104,7 +101,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'item_wise_tax_detail',
 		'fieldtype': 'Small Text',
-		'idx': 5,
 		'label': 'Item Wise Tax Detail ',
 		'oldfieldname': 'item_wise_tax_detail',
 		'oldfieldtype': 'Small Text',
@@ -116,7 +112,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'description',
 		'fieldtype': 'Small Text',
-		'idx': 6,
 		'label': 'Description',
 		'oldfieldname': 'description',
 		'oldfieldtype': 'Small Text',
@@ -130,7 +125,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'account_head',
 		'fieldtype': 'Link',
-		'idx': 7,
 		'label': 'Account Head',
 		'oldfieldname': 'account_head',
 		'oldfieldtype': 'Link',
@@ -145,13 +139,11 @@
 		'doctype': 'DocField',
 		'fieldname': 'cost_center',
 		'fieldtype': 'Link',
-		'idx': 8,
 		'label': 'Cost Center',
 		'oldfieldname': 'cost_center',
 		'oldfieldtype': 'Link',
 		'options': 'Cost Center',
 		'permlevel': 0,
-		'search_index': 0,
 		'trigger': 'Client'
 	},
 
@@ -160,7 +152,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'rate',
 		'fieldtype': 'Currency',
-		'idx': 9,
 		'label': 'Rate',
 		'oldfieldname': 'rate',
 		'oldfieldtype': 'Currency',
@@ -174,7 +165,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'tax_amount',
 		'fieldtype': 'Currency',
-		'idx': 10,
 		'label': 'Amount',
 		'oldfieldname': 'tax_amount',
 		'oldfieldtype': 'Currency',
@@ -188,7 +178,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'total',
 		'fieldtype': 'Currency',
-		'idx': 11,
 		'label': 'Aggregate Total',
 		'oldfieldname': 'total',
 		'oldfieldtype': 'Currency',
@@ -201,7 +190,6 @@
 		'fieldname': 'parenttype',
 		'fieldtype': 'Data',
 		'hidden': 1,
-		'idx': 12,
 		'in_filter': 1,
 		'label': 'Parenttype',
 		'oldfieldname': 'parenttype',
@@ -218,7 +206,6 @@
 		'fieldname': 'total_tax_amount',
 		'fieldtype': 'Currency',
 		'hidden': 1,
-		'idx': 13,
 		'label': 'Total +Tax',
 		'no_copy': 1,
 		'oldfieldname': 'total_tax_amount',
@@ -235,7 +222,6 @@
 		'fieldname': 'total_amount',
 		'fieldtype': 'Currency',
 		'hidden': 1,
-		'idx': 14,
 		'label': 'Tax Amount',
 		'no_copy': 1,
 		'oldfieldname': 'total_amount',
diff --git a/erpnext/patches/reload_lc_wizard.py b/erpnext/patches/reload_lc_wizard.py
new file mode 100644
index 0000000..6c884c9
--- /dev/null
+++ b/erpnext/patches/reload_lc_wizard.py
@@ -0,0 +1,8 @@
+def execute():
+	import webnotes
+	from webnotes.modules.module_manager import reload_doc
+	from webnotes.model import delete_doc
+
+	delete_doc('DocType', 'Landed Cost Wizard')
+	reload_doc('stock', 'doctype', 'landed_cost_wizard')
+	reload_doc('stock', 'doctype', 'lc_pr_detail')
diff --git a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.js b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.js
index 5209d1a..7e68f9b 100644
--- a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.js
+++ b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.js
@@ -2,10 +2,7 @@
 if(!doc.currency){doc.currency = sys_defaults.currency;}
 }
 
-cur_frm.fields_dict['landed_cost_details'].grid.get_field("account_head").get_query = function(doc,cdt,cdn) {
-  return 'SELECT tabAccount.name FROM tabAccount WHERE tabAccount.group_or_ledger="Ledger" AND tabAccount.docstatus != 2 AND tabAccount.account_type = "Chargeable" AND  tabAccount.name LIKE "%s"'
-}
 
 cur_frm.fields_dict['landed_cost_details'].grid.get_field("account_head").get_query = function(doc,cdt,cdn) {
   return 'SELECT tabAccount.name FROM tabAccount WHERE tabAccount.group_or_ledger="Ledger" AND tabAccount.docstatus != 2 AND (tabAccount.account_type = "Tax" OR tabAccount.account_type = "Chargeable") AND  tabAccount.name LIKE "%s"'
-}
\ No newline at end of file
+}
diff --git a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py
index e1d5774..2b828f9 100644
--- a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py
+++ b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py
@@ -1,236 +1,47 @@
-# Please edit this list and import only required elements
 import webnotes
+from webnotes.utils import cint, cstr, flt
+from webnotes.model.doc import addchild, getchildren
+from webnotes.model.doclist import getlist
+from webnotes.model.code import get_obj
+from webnotes import msgprint
 
-from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
-from webnotes.model import db_exists
-from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
-from webnotes.model.doclist import getlist, copy_doclist
-from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
-from webnotes import session, form, is_testing, msgprint, errprint
-
-set = webnotes.conn.set
 sql = webnotes.conn.sql
-get_value = webnotes.conn.get_value
-in_transaction = webnotes.conn.in_transaction
-convert_to_lists = webnotes.conn.convert_to_lists
 
 # -----------------------------------------------------------------------------------------
 
-
 class DocType:
 	def __init__(self, doc, doclist=[]):
 		self.doc = doc
 		self.doclist = doclist
+		self.prwise_cost = {}
+		
+	def check_mandatory(self):
+		""" Check mandatory fields """		
+		if not self.doc.from_pr_date or not self.doc.to_pr_date:
+			msgprint("Please enter From and To PR Date", raise_exception=1)
+
+		if not self.doc.currency:
+			msgprint("Please enter Currency.", raise_exception=1)
+
 
 	def get_purchase_receipts(self):
+		"""	Get purchase receipts for given period """
+				
 		self.doc.clear_table(self.doclist,'lc_pr_details',1)
-		if not self.doc.from_pr_date or not self.doc.to_pr_date:
-			msgprint("Please enter From and To PR Date")
-			raise Exception
-		if not self.doc.currency:
-			msgprint("Please enter Currency.")
-			raise Exception
-		pr = sql("select name from `tabPurchase Receipt` where docstatus = 1 and posting_date >= '%s' and posting_date <= '%s' and currency = '%s' order by name "%(self.doc.from_pr_date,self.doc.to_pr_date,self.doc.currency), as_dict = 1)
+		self.check_mandatory()
+		
+		pr = sql("select name from `tabPurchase Receipt` where docstatus = 1 and posting_date >= '%s' and posting_date <= '%s' and currency = '%s' order by name " % (self.doc.from_pr_date, self.doc.to_pr_date, self.doc.currency), as_dict = 1)
 		if len(pr)>200:
-			msgprint("Please enter date of shorter duration as there are too many purchase receipt, hence it cannot be loaded.")
-			raise Exception
+			msgprint("Please enter date of shorter duration as there are too many purchase receipt, hence it cannot be loaded.", raise_exception=1)
+			
 		for i in pr:
-			if i and i['name']:
-				pr_no = addchild(self.doc, 'lc_pr_details', 'LC PR Detail', 1, self.doclist)
-				pr_no.purchase_receipt_no = i and i['name'] or ''
-				pr_no.save()
+			ch = addchild(self.doc, 'lc_pr_details', 'LC PR Detail', 1, self.doclist)
+			ch.purchase_receipt = i and i['name'] or ''
+			ch.save()
 
 
-	def update_pr_lc_se(self):
-		lst = []
-		condition = ' name in('
-		
-		amt = 0
-		for d in getlist(self.doclist, 'lc_pr_details'):
-			
-			if cint(d.include_in_landed_cost) == 1:
-				condition += '"'+d.purchase_receipt_no+'",'
-				lst.append(d.purchase_receipt_no) 
-		condition += '"")'
-		
-		amount = sql("SELECT SUM(net_total) FROM `tabPurchase Receipt` WHERE docstatus = 1 AND %s"%condition)
-		amt = amount and flt(amount[0][0]) or 0
-		for lc in getlist(self.doclist, 'landed_cost_details'):
-			for name in lst:
-				pr_oc_det = sql("select name from `tabPurchase Tax Detail` where parent = %s and category = 'For Valuation' and add_deduct_tax = 'Add' and charge_type = 'Actual' and account_head = %s ",(name, lc.account_head))
-				#obj = get_obj('Purchase Receipt', name, with_children = 1)
-				if not pr_oc_det:
-					obj = get_obj('Purchase Receipt', name, with_children = 1)
-					lgth = cint(sql("select count(name) from `tabPurchase Tax Detail` where parent = '%s' "%(name))[0][0])
-					pr_oc = addchild(obj.doc, 'purchase_tax_details', 'Purchase Tax Detail', 1)
-					pr_oc.category = 'For Valuation'
-					pr_oc.add_deduct_tax = 'Add'
-					pr_oc.charge_type = 'Actual'
-					pr_oc.description = lc.description
-					pr_oc.account_head = lc.account_head
-					pr_oc.rate = flt(flt(lc.amount) * flt(obj.doc.net_total/ amt))
-					pr_oc.tax_amount = flt(flt(lc.amount) * flt(obj.doc.net_total/ amt))
-					pr_oc.total = obj.doc.grand_total
-					pr_oc.docstatus = 1
-					pr_oc.idx = cint(lgth)
-					pr_oc.save()
-				else:
-					obj = get_obj('Purchase Receipt', name)
-					sql("update `tabPurchase Tax Detail` set rate = %s, tax_amount = %s where name = %s and parent = %s",(flt(flt(lc.amount) * flt(obj.doc.net_total/ amt)),flt(flt(lc.amount) * flt(obj.doc.net_total/ amt)),pr_oc_det[0][0],name))
-					
-				self.calc_pr_other_charges(name)
-				obj = get_obj('Purchase Receipt', name, with_children = 1)
-				for d in getlist(obj.doclist, 'purchase_receipt_details'):
-					if flt(d.qty):
-						d.valuation_rate = (flt(d.purchase_rate) + (flt(d.rm_supp_cost) / flt(d.qty)) + (flt(d.item_tax_amount)/flt(d.qty))) / flt(d.conversion_factor)
-						d.save()
-					sql("update `tabStock Ledger Entry` set incoming_rate = '%s' where voucher_detail_no = '%s'"%(flt(d.valuation_rate), d.name))
-
-					bin_name = sql("select t1.name, t2.posting_date, t2.posting_time from `tabBin` t1, `tabStock Ledger Entry` t2 where t2.voucher_detail_no = '%s' and t2.item_code = t1.item_code and t2.warehouse = t1.warehouse LIMIT 1"%(d.name))
-					if bin_name and bin_name[0][0]:
-						obj = get_obj('Bin', bin_name[0][0]).update_entries_after(bin_name[0][1], bin_name[0][2])
-
-		# now distribute the taxes among the PRs
-		for lc in getlist(self.doclist, 'landed_cost_details'):
-			for d in lst:
-				pr_id = d.purchase_receipt_no
-				
-				# load the purchase receipt object
-				pr = get_obj('Purchase Receipt', pr_id, with_children = 1)
-				
-				# get the tax detail (for valuation) in the PR if it exists 
-				pr_oc_det = sql("select name from `tabPurchase Tax Detail` where parent = %s and category = 'For Valuation' and add_deduct_tax = 'Add' and charge_type = 'Actual' and account_head = %s ",(pr_id, lc.account_head))
-				
-				# update tax rate and tax amount
-				tax_amount = flt(flt(lc.amount) * flt(pr.doc.net_total/ amt))
-				
-				# for display
-				d.net_total = pr.doc.net_total
-				d.added_cost = tax_amount
-				d.save()
-				
-				# if not exist, create the tax detail for valuation
-				if not pr_oc_det:
-					lgth = cint(sql("select count(name) from `tabPurchase Tax Detail` where parent = '%s' "%(pr_id))[0][0])
-					pr_oc = addchild(pr.doc, 'purchase_tax_details', 'Purchase Tax Detail', 1)
-					pr_oc.category = 'For Valuation'
-					pr_oc.add_deduct_tax = 'Add'
-					pr_oc.charge_type = 'Actual'
-					pr_oc.description = lc.description
-					pr_oc.account_head = lc.account_head
-					pr_oc.rate = tax_amount
-					pr_oc.tax_amount = tax_amount
-					pr_oc.total = obj.doc.grand_total
-					pr_oc.docstatus = 1
-					pr_oc.idx = cint(lgth)
-					pr_oc.save()
-				else:
-					obj = get_obj('Purchase Receipt', name)
-					sql("update `tabPurchase Tax Detail` set rate = %s, tax_amount = %s where name = %s and parent = %s", \
-						(tax_amount, tax_amount, pr_oc_det[0][0], pr_id))
-					
-				self.calc_pr_other_charges(name)
-				
-				# reload - and update the stock entries with the
-				# additional valuations
-				
-				obj = get_obj('Purchase Receipt', name, with_children = 1)
-				for d in getlist(obj.doclist, 'purchase_receipt_details'):
-					if flt(d.qty):
-						d.valuation_rate = (flt(d.purchase_rate) + (flt(d.rm_supp_cost) / flt(d.qty)) + (flt(d.item_tax_amount)/flt(d.qty))) / flt(d.conversion_factor)
-						d.save()
-					sql("update `tabStock Ledger Entry` set incoming_rate = '%s' where voucher_detail_no = '%s'"%(flt(d.valuation_rate), d.name))
-					bin_name = sql("select t1.name, t2.posting_date, t2.posting_time from `tabBin` t1, `tabStock Ledger Entry` t2 where t2.voucher_detail_no = '%s' and t2.item_code = t1.item_code and t2.warehouse = t1.warehouse LIMIT 1"%(d.name))
-
-					# update valuation of the item
-					if bin_name and bin_name[0][0]:
-						obj = get_obj('Bin', bin_name[0][0]).update_entries_after(bin_name[0][1], bin_name[0][2])
-
-	def add_deduct_taxes(self, ocd, oc, tax_amount, total_amount, total, prev_total, f=1):
-		ocd[oc].total_amount = flt(tax_amount.toFixed(2))		 
-		ocd[oc].total_tax_amount = flt(prev_total.toFixed(2))
-		ocd[oc].tax_amount += flt(tax_amount.toFixed(2))
-			 
-		total_amount = flt(ocd[oc].tax_amount)
-		total_tax_amount = flt(ocd[oc].total_tax_amount) + (f * flt(total_amount))
-		
-		if ocd[oc].category != "For Valuation":	
-			prev_total += f * flt(ocd[oc].total_amount)
-			total += f * flt(ocd[oc].tax_amount)
-			ocd[oc].total = flt(total) + (f * flt(tax[t].tax_amount))
-		else:
-			prev_total = prev_total				
-			ocd[oc].total = flt(total)
-		if ocd[oc].category != "For Total":
-			item_tax += f * ocd[oc].total_amount
-		ocd[oc].save()
-		
-		return total, prev_total
-		
-	# calculate the taxes for these PRs		
-	def calc_pr_other_charges(self, name):
-		
-		# re-calculate other charges
-		obj = get_obj('Purchase Receipt', name, with_children = 1)
-		total = 0
-		net_total = obj.doc.net_total
-
-
-		for prd in getlist(obj.doclist, 'purchase_receipt_details'):
-			prev_total, item_tax = flt(prd.amount), 0
-			total += flt(flt(prd.qty) * flt(prd.purchase_rate))
-			
-			check_tax = prd.item_tax_rate and eval(prd.item_tax_rate) or {}
-			ocd = getlist(obj.doclist, 'purchase_tax_details')
-			
-			# calculate tax for other charges
-			for oc in range(len(ocd)):
-				if check_tax.get(ocd[oc].account_head) and ocd[oc].charge_type != 'Actual':
-					rate = check_tax[ocd[oc].account_head]
-				else:
-					rate = flt(ocd[oc].rate)
-				
-				tax_amount = self.cal_tax(ocd, prd, rate, net_total, oc)
-				if ocd[oc].add_deduct_tax == 'Add':
-					add_deduct_taxes(self, ocd, oc, tax_amount, total_amount, total, prev_total, f=1)
-											
-				elif ocd[oc].add_deduct_tax == 'Deduct':
-					add_deduct_taxes(self, ocd, oc, tax_amount, total_amount, total, prev_total, f=-1)
-
-			prd.item_tax_amount = flt(item_tax)
-			prd.save()
-		obj.doc.save()
-					
-	def cal_tax(self, ocd, prd, rate, net_total, oc):
-		tax_amount = 0
-		if ocd[oc].charge_type == 'Actual':
-			value = flt(flt(rate) / flt(net_total))
-			return flt(flt(value) * flt(prd.amount))
-			
-		elif ocd[oc].charge_type == 'On Net Total':
-			return flt(flt(rate) * flt(prd.amount) / 100)
-			
-		elif ocd[oc].charge_type == 'On Previous Row Amount':
-			
-			row_no = cstr(ocd[oc].row_id)
-			row = (row_no).split("+")
-			for r in range(0, len(row.length)):
-				id = cint(row[r])
-				tax_amount += flt((flt(rate) * flt(ocd[id-1].total_amount) / 100))
-			row_id = row_no.find("/")
-			if row_id != -1:
-				rate = ''
-				row = (row_no).split("/")
-				
-				id1 = cint(row[0])
-				id2 = cint(row[1])
-				tax_amount = flt(flt(ocd[id1-1].total_amount) / flt(ocd[id2-1].total_amount))
-				
-			return tax_amount
-
-	# get details for landed cost table from master
-	# ---------------------------------------------
 	def get_landed_cost_master_details(self):
+		""" pull details from landed cost master"""
 		self.doc.clear_table(self.doclist, 'landed_cost_details')
 		idx = 0
 		landed_cost = sql("select account_head, description from `tabLanded Cost Master Detail` where parent=%s", (self.doc.landed_cost), as_dict = 1)
@@ -238,3 +49,177 @@
 			lct = addchild(self.doc, 'landed_cost_details', 'Landed Cost Detail', 1, self.doclist)
 			lct.account_head = cost['account_head']
 			lct.description = cost['description']
+
+
+	def get_selected_pr(self):
+		""" Get selected purchase receipt no """
+		self.selected_pr = [d.purchase_receipt for d in getlist(self.doclist, 'lc_pr_details') if d.select_pr]
+		if not self.selected_pr:
+			msgprint("Please select atleast one PR to proceed.", raise_exception=1)
+		
+	def validate_selected_pr(self):
+		"""Validate selected PR as submitted"""
+		invalid_pr =  sql("SELECT name FROM `tabPurchase Receipt` WHERE docstatus != 1 and name in (%s)" % ("'" + "', '".join(self.selected_pr) + "'"))
+		if invalid_pr:
+			msgprint("Selected purchase receipts must be submitted. Following PR are not submitted: %s" % invalid_pr, raise_exception=1)
+			
+
+	def get_total_amt(self):
+		""" Get sum of net total of all selected PR"""		
+		return sql("SELECT SUM(net_total) FROM `tabPurchase Receipt` WHERE name in (%s)" % ("'" + "', '".join(self.selected_pr) + "'"))[0][0]
+		
+
+	def add_charges_in_pr(self):
+		""" Add additional charges in selected pr proportionately"""
+		total_amt = self.get_total_amt()
+		
+		for pr in self.selected_pr:
+			pr_obj = get_obj('Purchase Receipt', pr, with_children = 1)
+			cumulative_grand_total = flt(pr_obj.doc.grand_total)
+			
+			for lc in getlist(self.doclist, 'landed_cost_details'):
+				amt = flt(lc.amount) * flt(pr_obj.doc.net_total)/ flt(total_amt)
+				self.prwise_cost[pr] = self.prwise_cost.get(pr, 0) + amt
+				cumulative_grand_total += amt
+				
+				pr_oc_row = sql("select name from `tabPurchase Tax Detail` where parent = %s and category = 'For Valuation' and add_deduct_tax = 'Add' and charge_type = 'Actual' and account_head = %s",(pr, lc.account_head))
+				if not pr_oc_row:	# add if not exists
+					ch = addchild(pr_obj.doc, 'purchase_tax_details', 'Purchase Tax Detail', 1)
+					ch.category = 'For Valuation'
+					ch.add_deduct_tax = 'Add'
+					ch.charge_type = 'Actual'
+					ch.description = lc.description
+					ch.account_head = lc.account_head
+					ch.rate = amt
+					ch.tax_amount = amt
+					ch.total = cumulative_grand_total
+					ch.docstatus = 1
+					ch.idx = 500 # add at the end
+					ch.save(1)
+				else:	# overwrite if exists
+					sql("update `tabPurchase Tax Detail` set rate = %s, tax_amount = %s where name = %s and parent = %s ", (amt, amt, pr_oc_row[0][0], pr))
+		
+		
+	def reset_other_charges(self, pr_obj):
+		""" Reset all calculated values to zero"""
+		for t in getlist(pr_obj.doclist, 'purchase_tax_details'):
+			t.total_tax_amount = 0;
+			t.total_amount = 0;
+			t.tax_amount = 0;
+			t.total = 0;
+			t.save()
+			
+		
+	def cal_charges_and_item_tax_amt(self):
+		""" Re-calculates other charges values and itemwise tax amount for getting valuation rate"""
+		for pr in self.selected_pr:
+			obj = get_obj('Purchase Receipt', pr, with_children = 1)
+			total = 0
+			self.reset_other_charges(obj)
+
+			for prd in getlist(obj.doclist, 'purchase_receipt_details'):
+				prev_total, item_tax = flt(prd.amount), 0
+				total += flt(prd.qty) * flt(prd.purchase_rate)
+			
+				item_tax_rate = prd.item_tax_rate and eval(prd.item_tax_rate) or {}
+				
+				ocd = getlist(obj.doclist, 'purchase_tax_details')
+				# calculate tax for other charges
+				for oc in range(len(ocd)):
+					# Get rate : consider if diff for this item
+					if item_tax_rate.get(ocd[oc].account_head) and ocd[oc].charge_type != 'Actual':
+						rate = item_tax_rate[ocd[oc].account_head]
+					else:
+						rate = flt(ocd[oc].rate)
+				
+					tax_amount = self.cal_tax(ocd, prd, rate, obj.doc.net_total, oc)					
+					total, prev_total, item_tax = self.add_deduct_taxes(ocd, oc, tax_amount, total, prev_total, item_tax)
+
+				prd.item_tax_amount = flt(item_tax)
+				prd.save()
+			obj.doc.save()
+	
+	
+	def cal_tax(self, ocd, prd, rate, net_total, oc):
+		""" Calculates tax amount for one item"""
+		tax_amount = 0
+		if ocd[oc].charge_type == 'Actual':
+			tax_amount = flt(rate) * flt(prd.amount) / flt(net_total)
+		elif ocd[oc].charge_type == 'On Net Total':
+			tax_amount = flt(rate) * flt(prd.amount) / 100		
+		elif ocd[oc].charge_type == 'On Previous Row Amount':			
+			row_no = cstr(ocd[oc].row_id)
+			row = row_no.split("+")
+			for r in range(0, len(row)):
+				id = cint(row[r])
+				tax_amount += flt((flt(rate) * flt(ocd[id-1].total_amount) / 100))
+			row_id = row_no.find("/")
+			if row_id != -1:
+				rate = ''
+				row = (row_no).split("/")				
+				id1 = cint(row[0])
+				id2 = cint(row[1])
+				tax_amount = flt(flt(ocd[id1-1].total_amount) / flt(ocd[id2-1].total_amount))
+		elif ocd[oc].charge_type == 'On Previous Row Total':
+			row = cint(ocd[oc].row_id)
+			if ocd[row-1].add_deduct_tax == 'Add':
+			  tax_amount = flt(rate) * (flt(ocd[row-1].total_tax_amount)+flt(ocd[row-1].total_amount)) / 100
+			elif ocd[row-1].add_deduct_tax == 'Deduct':
+			  tax_amount = flt(rate) * (flt(ocd[row-1].total_tax_amount)-flt(ocd[row-1].total_amount)) / 100
+			  
+		return tax_amount  
+
+	def add_deduct_taxes(self, ocd, oc, tax_amount, total, prev_total, item_tax):
+		"""Calculates other charges values"""
+		add_ded = ocd[oc].add_deduct_tax == 'Add' and 1 or ocd[oc].add_or_deduct == 'Deduct' and -1
+		ocd[oc].total_amount = flt(tax_amount)
+		ocd[oc].total_tax_amount = flt(prev_total)
+		ocd[oc].tax_amount += flt(tax_amount)
+		
+		total_amount = flt(ocd[oc].tax_amount)
+		total_tax_amount = flt(ocd[oc].total_tax_amount) + (add_ded * flt(total_amount))
+		
+		if ocd[oc].category != "For Valuation":	
+			prev_total += add_ded * flt(ocd[oc].total_amount)
+			total += add_ded * flt(ocd[oc].tax_amount)
+			ocd[oc].total = total
+		else:
+			prev_total = prev_total
+			ocd[oc].total = flt(total)
+		ocd[oc].save()
+				
+		if ocd[oc].category != "For Total":
+			item_tax += add_ded * ocd[oc].total_amount
+		
+		return total, prev_total, item_tax
+
+
+	def update_sle(self):
+		""" Recalculate valuation rate in all sle after pr posting date"""
+		for pr in self.selected_pr:
+			pr_obj = get_obj('Purchase Receipt', pr, with_children = 1)
+			
+			for d in getlist(pr_obj.doclist, 'purchase_receipt_details'):
+				if flt(d.qty):
+					d.valuation_rate = (flt(d.purchase_rate) + (flt(d.rm_supp_cost)/flt(d.qty)) + (flt(d.item_tax_amount)/flt(d.qty))) / flt(d.conversion_factor)
+					d.save()	
+				sql("update `tabStock Ledger Entry` set incoming_rate = '%s' where voucher_detail_no = '%s'"%(flt(d.valuation_rate), d.name))
+				
+				bin = sql("select t1.name, t2.posting_date, t2.posting_time from `tabBin` t1, `tabStock Ledger Entry` t2 where t2.voucher_detail_no = '%s' and t2.item_code = t1.item_code and t2.warehouse = t1.warehouse LIMIT 1" % d.name)
+
+				# update valuation rate after pr posting date
+				if bin and bin[0][0]:
+					obj = get_obj('Bin', bin[0][0]).update_entries_after(bin[0][1], bin[0][2])
+
+				
+	def update_landed_cost(self):
+		""" 
+			Add extra cost and recalculate all values in pr, 
+			Recalculate valuation rate in all sle after pr posting date
+		"""	
+		self.get_selected_pr()
+		self.validate_selected_pr()			
+		self.add_charges_in_pr()		
+		self.cal_charges_and_item_tax_amt()
+		self.update_sle()
+		msgprint("Landed Cost updated successfully")
diff --git a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.txt b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.txt
index 90c570a..1e12afa 100644
--- a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.txt
+++ b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.txt
@@ -3,16 +3,18 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2010-08-08 17:09:07',
+		'creation': '2011-11-16 16:17:22',
 		'docstatus': 0,
-		'modified': '2011-06-30 18:47:46',
+		'modified': '2011-11-16 16:32:18',
 		'modified_by': 'Administrator',
 		'owner': 'wasim@webnotestech.com'
 	},
 
 	# These values are common for all DocType
 	{
+		'_last_update': '1321441191',
 		'colour': 'White:FFF',
+		'default_print_format': 'Standard',
 		'doctype': 'DocType',
 		'issingle': 1,
 		'module': 'Stock',
@@ -20,7 +22,7 @@
 		'section_style': 'Simple',
 		'server_code_error': ' ',
 		'show_in_menu': 1,
-		'version': 31
+		'version': 35
 	},
 
 	# These values are common for all DocField
@@ -52,7 +54,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 1,
 		'permlevel': 0,
 		'role': 'All',
 		'write': 0
@@ -64,7 +65,6 @@
 		'cancel': 0,
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 2,
 		'permlevel': 0,
 		'role': 'Purchase Manager',
 		'submit': 0,
@@ -75,7 +75,6 @@
 	{
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 3,
 		'permlevel': 0,
 		'role': 'System Manager',
 		'write': 1
@@ -85,7 +84,6 @@
 	{
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 4,
 		'permlevel': 0,
 		'role': 'Purchase User',
 		'write': 1
@@ -94,7 +92,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 5,
 		'permlevel': 1,
 		'role': 'All'
 	},
@@ -102,7 +99,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 6,
 		'permlevel': 1,
 		'role': 'System Manager'
 	},
@@ -110,7 +106,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 7,
 		'permlevel': 1,
 		'role': 'Purchase User'
 	},
@@ -118,7 +113,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 8,
 		'permlevel': 1,
 		'role': 'Purchase Manager'
 	},
@@ -126,9 +120,23 @@
 	# DocField
 	{
 		'doctype': 'DocField',
+		'fieldtype': 'HTML',
+		'label': 'Process',
+		'options': '<div class="field_description"><b>Process:</b><br>1. Fetch and select Purchase Receipt<br>2. Enter extra costs<br>3. Click on Update Landed Cost button<br> 4. Cost will be added into other charges table of selected PR proportionately based on net total<br>5. Item Valuation Rate will be recalculated</div>'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldtype': 'Section Break',
+		'options': 'Simple'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
 		'fieldname': 'from_pr_date',
 		'fieldtype': 'Date',
-		'idx': 1,
 		'label': 'From PR Date',
 		'reqd': 1
 	},
@@ -138,7 +146,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'to_pr_date',
 		'fieldtype': 'Date',
-		'idx': 2,
 		'label': 'To PR Date',
 		'reqd': 1
 	},
@@ -148,7 +155,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'currency',
 		'fieldtype': 'Select',
-		'idx': 3,
 		'label': 'Currency',
 		'options': 'link:Currency',
 		'reqd': 1
@@ -158,7 +164,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 4,
 		'label': 'Get Purchase Receipt',
 		'options': 'get_purchase_receipts'
 	},
@@ -166,18 +171,8 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldtype': 'Section Break',
-		'idx': 5,
-		'label': 'LC PR Detail',
-		'options': 'Simple'
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
 		'fieldname': 'lc_pr_details',
 		'fieldtype': 'Table',
-		'idx': 6,
 		'label': 'LC PR Details',
 		'options': 'LC PR Detail'
 	},
@@ -186,8 +181,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 7,
-		'label': 'Landed Cost Detail',
 		'options': 'Simple'
 	},
 
@@ -196,7 +189,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'landed_cost',
 		'fieldtype': 'Link',
-		'idx': 8,
 		'label': 'Select Landed Cost Details Master',
 		'options': 'Landed Cost Master'
 	},
@@ -206,7 +198,6 @@
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 9,
 		'label': 'Get Details',
 		'options': 'get_landed_cost_master_details'
 	},
@@ -216,7 +207,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'landed_cost_details',
 		'fieldtype': 'Table',
-		'idx': 10,
 		'label': 'Landed Cost Details',
 		'options': 'Landed Cost Detail'
 	},
@@ -225,8 +215,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 11,
 		'label': 'Update PR',
-		'options': 'update_pr_lc_se'
+		'options': 'update_landed_cost'
 	}
 ]
\ No newline at end of file
diff --git a/erpnext/stock/doctype/lc_pr_detail/lc_pr_detail.txt b/erpnext/stock/doctype/lc_pr_detail/lc_pr_detail.txt
index d09285d..b6bcb42 100644
--- a/erpnext/stock/doctype/lc_pr_detail/lc_pr_detail.txt
+++ b/erpnext/stock/doctype/lc_pr_detail/lc_pr_detail.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:07',
 		'docstatus': 0,
-		'modified': '2011-06-30 18:47:22',
+		'modified': '2011-11-16 16:10:00',
 		'modified_by': 'Administrator',
 		'owner': 'wasim@webnotestech.com'
 	},
@@ -13,6 +13,7 @@
 	# These values are common for all DocType
 	{
 		'colour': 'White:FFF',
+		'default_print_format': 'Standard',
 		'doctype': 'DocType',
 		'istable': 1,
 		'module': 'Stock',
@@ -20,7 +21,7 @@
 		'section_style': 'Simple',
 		'server_code_error': ' ',
 		'show_in_menu': 0,
-		'version': 2
+		'version': 5
 	},
 
 	# These values are common for all DocField
@@ -42,25 +43,23 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'purchase_receipt_no',
+		'fieldname': 'purchase_receipt',
 		'fieldtype': 'Link',
-		'idx': 1,
-		'label': 'Purchase Receipt No',
+		'label': 'Purchase Receipt',
 		'oldfieldname': 'purchase_receipt_no',
 		'oldfieldtype': 'Link',
 		'options': 'Purchase Receipt',
-		'search_index': 1
+		'width': '220px'
 	},
 
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'include_in_landed_cost',
+		'fieldname': 'select_pr',
 		'fieldtype': 'Check',
-		'idx': 2,
-		'label': 'Include In Landed Cost',
+		'label': 'Select PR',
 		'oldfieldname': 'include_in_landed_cost',
 		'oldfieldtype': 'Check',
-		'width': '150px'
+		'width': '120px'
 	}
 ]
\ No newline at end of file
diff --git a/erpnext/stock/doctype/purchase_receipt_detail/purchase_receipt_detail.txt b/erpnext/stock/doctype/purchase_receipt_detail/purchase_receipt_detail.txt
index 2348e8e..2e3514e 100644
--- a/erpnext/stock/doctype/purchase_receipt_detail/purchase_receipt_detail.txt
+++ b/erpnext/stock/doctype/purchase_receipt_detail/purchase_receipt_detail.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:16',
 		'docstatus': 0,
-		'modified': '2011-10-20 18:40:15',
+		'modified': '2011-11-16 15:43:36',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -22,7 +22,7 @@
 		'section_style': 'Tray',
 		'server_code_error': ' ',
 		'show_in_menu': 0,
-		'version': 60
+		'version': 63
 	},
 
 	# These values are common for all DocField
@@ -451,6 +451,7 @@
 
 	# DocField
 	{
+		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldname': 'item_tax_amount',
 		'fieldtype': 'Currency',
@@ -501,6 +502,7 @@
 
 	# DocField
 	{
+		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldname': 'valuation_rate',
 		'fieldtype': 'Currency',
@@ -517,6 +519,7 @@
 
 	# DocField
 	{
+		'colour': 'White:FFF',
 		'description': 'Tax detail table fetched from item master as a string and stored in this field.\nUsed for Purchase Other Charges',
 		'doctype': 'DocField',
 		'fieldname': 'item_tax_rate',