Merge pull request #159 from anandpdoshi/master

Commented print statement in email_digest.py
diff --git a/erpnext/accounts/doctype/c_form/c_form.js b/erpnext/accounts/doctype/c_form/c_form.js
index 4a90a9d..6629de6 100644
--- a/erpnext/accounts/doctype/c_form/c_form.js
+++ b/erpnext/accounts/doctype/c_form/c_form.js
@@ -1,7 +1,7 @@
 //c-form js file
 // -----------------------------
 cur_frm.fields_dict.invoice_details.grid.get_field("invoice_no").get_query = function(doc) {
-	return 'SELECT `tabReceivable Voucher`.`name` FROM `tabReceivable Voucher` WHERE `tabReceivable Voucher`.`company` = "' +doc.company+'" AND `tabReceivable Voucher`.%(key)s LIKE "%s" AND `tabReceivable Voucher`.`customer` = "' + doc.customer + '" AND `tabReceivable Voucher`.`docstatus` = 1 and `tabReceivable Voucher`.`c_form_applicable` = "Yes" ORDER BY `tabReceivable Voucher`.`name` ASC LIMIT 50';
+	return 'SELECT `tabReceivable Voucher`.`name` FROM `tabReceivable Voucher` WHERE `tabReceivable Voucher`.`company` = "' +doc.company+'" AND `tabReceivable Voucher`.%(key)s LIKE "%s" AND `tabReceivable Voucher`.`customer` = "' + doc.customer + '" AND `tabReceivable Voucher`.`docstatus` = 1 and `tabReceivable Voucher`.`c_form_applicable` = "Yes" and ifnull(`tabReceivable Voucher`.c_form_no, "") = "" ORDER BY `tabReceivable Voucher`.`name` ASC LIMIT 50';
 }
 
 cur_frm.cscript.invoice_no = function(doc, cdt, cdn) {
diff --git a/erpnext/accounts/doctype/c_form/c_form.py b/erpnext/accounts/doctype/c_form/c_form.py
index 00016cb..7228f11 100644
--- a/erpnext/accounts/doctype/c_form/c_form.py
+++ b/erpnext/accounts/doctype/c_form/c_form.py
@@ -17,12 +17,23 @@
 	def autoname(self):
 		self.doc.name = make_autoname(self.doc.naming_series + '.#####')
 
+
 	def on_update(self):
-		inv = "'" + "', '".join([d.invoice_no for d in getlist(self.doclist, 'invoice_details')]) + "'"
-		sql("""update `tabReceivable Voucher` set c_form_no = '%s', modified ='%s'
+		"""	Update C-Form No on invoices"""
+		
+		if len(getlist(self.doclist, 'invoice_details')):
+			inv = "'" + "', '".join([d.invoice_no for d in getlist(self.doclist, 'invoice_details')]) + "'"
+			sql("""update `tabReceivable Voucher` set c_form_no = '%s', modified ='%s'
 				where name in (%s)"""%(self.doc.name, self.doc.modified, inv))
+			sql("""update `tabReceivable Voucher` set c_form_no = '', modified = %s where name not
+				in (%s) and ifnull(c_form_no, '') = %s""", (self.doc.modified, self.doc.name, inv))
+		else:
+			msgprint("Please enter atleast 1 invoice in the table below", raise_exception=1)
+
 
 	def get_invoice_details(self, invoice_no):
+		"""	Pull details from invoices for referrence """
+
 		inv = sql("""select posting_date, territory, net_total, grand_total from
 			`tabReceivable Voucher` where name = %s""", invoice_no)	
 		ret = {
@@ -32,3 +43,19 @@
 			'grand_total'  : inv and flt(inv[0][3]) or ''
 		}
 		return ret
+
+
+	def validate_invoice(self):
+		"""Validate invoice that c-form is applicable and no other c-form is
+		received for that"""
+
+		for d in getlist(self.doclist, 'invoice_details'):
+			inv = sql("""select c_form_applicable, c_form_no from
+				`tabReceivable Voucher` where name = %s""", invoice_no)
+			if not inv:
+				msgprint("Invoice: %s is not exists in the system, please check." % d.invoice_no, raise_exception=1)
+			elif inv[0][0] != 'Yes':
+				msgprint("C-form is not applicable for Invoice: %s" % d.invoice_no, raise_exception=1)
+			elif inv[0][1] and inv[0][1] != self.doc.name:
+				msgprint("""Invoice %s is tagged in another C-form: %s. \nIf you want to change C-form no for this invoice,
+					please remove invoice no from the previous c-form and then try again""" % (d.invoice_no, inv[0][1]), raise_exception=1)
diff --git a/erpnext/accounts/doctype/gl_control/gl_control.py b/erpnext/accounts/doctype/gl_control/gl_control.py
index 01f31eb..bab8626 100644
--- a/erpnext/accounts/doctype/gl_control/gl_control.py
+++ b/erpnext/accounts/doctype/gl_control/gl_control.py
@@ -515,6 +515,7 @@
 
 			send_notification(new_rv)
 
+
 def create_new_invoice(prev_rv):
 	# clone rv
 	new_rv = clone(prev_rv)
@@ -533,6 +534,61 @@
 
 	return new_rv
 
+
 def send_notification(new_rv):
 	"""Notify concerned persons about recurring invoice generation"""
-	pass
+	subject = "Invoice : " + new_rv.doc.name
+
+	com = new_rv.doc.company   # get_value('Control Panel', '', 'letter_head')
+
+	hd = '''<div><h2>%s</h2></div>
+			<div><h3>Invoice: %s</h3></div>
+			<table cellspacing= "5" cellpadding="5"  width = "100%%">
+				<tr>
+					<td width = "50%%"><b>Customer</b><br>%s<br>%s</td>
+					<td width = "50%%">Invoice Date: %s<br>Due Date: %s</td>
+				</tr>
+			</table>
+		''' % (com, new_rv.doc.name, new_rv.doc.customer, new_rv.doc.address_display, new_rv.doc.posting_date, new_rv.doc.due_date)
+	
+	
+	tbl = '''<table border="1px solid #CCC" width="100%%" cellpadding="0px" cellspacing="0px">
+				<tr>
+					<td width = "15%%" bgcolor="#CCC" align="left"><b>Item</b></td>
+					<td width = "40%%" bgcolor="#CCC" align="left"><b>Description</b></td>
+					<td width = "15%%" bgcolor="#CCC" align="center"><b>Qty</b></td>
+					<td width = "15%%" bgcolor="#CCC" align="center"><b>Rate</b></td>
+					<td width = "15%%" bgcolor="#CCC" align="center"><b>Amount</b></td>
+				</tr>
+		'''
+	for d in getlist(new_rv.doclist, 'entries'):
+		tbl += '<tr><td>' + d.item_code +'</td><td>' + d.description+'</td><td>' + cstr(d.qty) +'</td><td>' + cstr(d.basic_rate) +'</td><td>' + cstr(d.amount) +'</td></tr>'
+	tbl += '</table>'
+
+	totals =''' <table cellspacing= "5" cellpadding="5"  width = "100%%">
+					<tr>
+						<td width = "50%%"></td>
+						<td width = "50%%">
+							<table width = "100%%">
+								<tr>
+									<td width = "50%%">Net Total: </td><td>%s </td>
+								</tr><tr>
+									<td width = "50%%">Total Tax: </td><td>%s </td>
+								</tr><tr>
+									<td width = "50%%">Grand Total: </td><td>%s</td>
+								</tr><tr>
+									<td width = "50%%">In Words: </td><td>%s</td>
+								</tr>
+							</table>
+						</td>
+					</tr>
+					<tr><td>Terms:</td></tr>
+					<tr><td>%s</td></tr>
+				</table>
+			''' % (new_rv.doc.net_total, new_rv.doc.total_tax,new_rv.doc.grand_total, new_rv.doc.in_words,new_rv.doc.terms)
+
+
+	msg = hd + tbl + totals
+	from webnotes.utils.email_lib import sendmail
+	sendmail(recipients = [new_rv.doc.email_notification_address], \
+		sender=new_rv.doc.owner, subject=subject, parts=[['text/plain', msg]])
diff --git a/erpnext/accounts/doctype/payable_voucher/payable_voucher.js b/erpnext/accounts/doctype/payable_voucher/payable_voucher.js
index 550c014..ca79387 100644
--- a/erpnext/accounts/doctype/payable_voucher/payable_voucher.js
+++ b/erpnext/accounts/doctype/payable_voucher/payable_voucher.js
@@ -77,13 +77,6 @@
   if(doc.supplier) get_server_fields('get_supplier_address', JSON.stringify({supplier: doc.supplier, address: doc.supplier_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
 }
 
-cur_frm.fields_dict['supplier_address'].get_query = function(doc, cdt, cdn) {
-  return 'SELECT name,address_line1,city FROM tabAddress WHERE supplier = "'+ doc.supplier +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
-cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-  return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE supplier = "'+ doc.supplier +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
 
 
 cur_frm.fields_dict.supplier_address.on_new = function(dn) {
@@ -111,12 +104,6 @@
 
 
 
-// Get Print Heading
-cur_frm.fields_dict['select_print_heading'].get_query = function(doc, cdt, cdn) {
-  return 'SELECT `tabPrint Heading`.name FROM `tabPrint Heading` WHERE `tabPrint Heading`.docstatus !=2 AND `tabPrint Heading`.name LIKE "%s" ORDER BY `tabPrint Heading`.name ASC LIMIT 50';
-}
-
-
 //Set expense_head and cost center on adding new row
 //----------------------------------------------
 cur_frm.fields_dict['entries'].grid.onrowadd = function(doc, cdt, cdn){
@@ -142,18 +129,10 @@
   if (doc.is_opening == 'Yes') unhide_field('aging_date');
 }
 
-/* ******************************** TRIGGERS **************************************** */
-
-// Conversion Rate
-// ----------------
-cur_frm.cscript.conversion_rate = function(doc,cdt,cdn) {
-  cur_frm.cscript.calc_total(doc,cdt,cdn);
-}
-
 // Recalculate Button
 // -------------------
 cur_frm.cscript['Recalculate'] = function(doc, dt, dn) {
-  cur_frm.cscript.calc_total(doc, cdt, cdn);
+  cur_frm.cscript['Calculate Tax'](doc,cdt,cdn);
   calc_total_advance(doc,cdt,cdn);
 }
 
@@ -179,31 +158,16 @@
   }
 }
 
-// Quantity
-// ---------
-cur_frm.cscript.qty  = function(doc,dt,dn) { cur_frm.cscript.calc_exp_row(doc,dt,dn); }
-
-// Import Rate
-// ------------
-cur_frm.cscript.import_rate = function(doc,dt,dn) {
-  var d = locals[dt][dn];
-  set_multiple('PV Detail', d.name, {'rate': flt(d.import_rate) * flt(doc.conversion_rate) }, 'entries');
-  cur_frm.cscript.calc_exp_row(doc,dt,dn)
-}
-
-
-// ============== TDS ===============
-
 // Rate in Deduct Taxes (TDS)
 // --------------------------
 cur_frm.cscript.rate = function(doc,dt,dn) {
   //This is done as Purchase tax detail and PV detail both contain the same fieldname 'rate'
-  if(dt != 'Purchase Tax Detail') cur_frm.cscript.calc_exp_row(doc,dt,dn); 
+  if(dt != 'Purchase Tax Detail')   cur_frm.cscript.calc_amount(doc, 2);
 }
 
 // Amount
 // -------
-cur_frm.cscript.ded_amount = function(doc,dt,dn) { cur_frm.cscript.calc_total(doc); }
+cur_frm.cscript.ded_amount = function(doc,dt,dn) {calculate_outstanding(doc);}
 
 // Get TDS Button
 // ---------------
@@ -211,7 +175,7 @@
   var callback = function(r,rt) {
     cur_frm.refresh();
     refresh_field('ded_amount');
-    cur_frm.cscript.calc_total(locals[dt][dn]);
+    //cur_frm.cscript.calc_total(locals[dt][dn]);
   }
   $c_obj(make_doclist(dt,dn), 'get_tds', '', callback);
 }
@@ -244,6 +208,15 @@
 
 /* ***************************** GET QUERY Functions *************************** */
 
+
+cur_frm.fields_dict['supplier_address'].get_query = function(doc, cdt, cdn) {
+  return 'SELECT name,address_line1,city FROM tabAddress WHERE supplier = "'+ doc.supplier +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
+}
+
+cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
+  return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE supplier = "'+ doc.supplier +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
+}
+
 // Item Code
 // ----------
 cur_frm.fields_dict['entries'].grid.get_field("item_code").get_query = function(doc, cdt, cdn) {
@@ -277,6 +250,12 @@
   }
 }
 
+// Get Print Heading
+cur_frm.fields_dict['select_print_heading'].get_query = function(doc, cdt, cdn) {
+  return 'SELECT `tabPrint Heading`.name FROM `tabPrint Heading` WHERE `tabPrint Heading`.docstatus !=2 AND `tabPrint Heading`.name LIKE "%s" ORDER BY `tabPrint Heading`.name ASC LIMIT 50';
+}
+
+
 // ================== PV Details Table ===================
 // Expense Head
 // -------------
@@ -324,27 +303,6 @@
 
 /* ***************************** UTILITY FUNCTIONS ************************ */
 
-// Calculate
-// ---------
-cur_frm.cscript.calc_total = function(doc) {
-  
-   // expense
-  var t_exp = 0.0;
-  var el = getchildren('PV Detail',doc.name,'entries');
-  for(var i in el) {
-   if (flt(el[i].import_rate) > 0){
-     set_multiple('PV Detail', el[i].name, {'rate': flt(doc.conversion_rate) * flt(el[i].import_rate) }, 'entries');
-     set_multiple('PV Detail', el[i].name, {'import_amount': flt(el[i].qty) * flt(el[i].import_rate) }, 'entries');
-   }
-   set_multiple('PV Detail', el[i].name, {'amount': flt(el[i].qty) * flt(el[i].rate) }, 'entries')
-   t_exp += flt(el[i].amount);
-  }
-  doc.net_total = flt(t_exp);
-  refresh_field('net_total');
-  cur_frm.cscript.val_cal_charges(doc, cdt, cdn, cur_frm.cscript.tname, cur_frm.cscript.fname, cur_frm.cscript.other_fname);
-}
-
-
 // Calculate Advance
 // ------------------
 var calc_total_advance = function(doc,cdt,cdn) {
@@ -365,30 +323,12 @@
   refresh_many(['total_advance','outstanding_amount','tds_amount_on_advance', 'total_amount_to_pay']);
 }
 
-
-cur_frm.cscript.calc_exp_row = function(doc,dt,dn) {
-  var d = locals[dt][dn];
-  d.amount = flt(d.qty * d.rate);
-  refresh_field('amount',dn,'entries');
-  
-  if (!doc.conversion_rate){ doc.conversion_rate = 1; refresh_field('conversion_rate'); }
-  
-  set_multiple('PV Detail', dn, {'import_rate': flt(d.rate) / flt(doc.conversion_rate) }, 'entries');
-  set_multiple('PV Detail', dn, {'import_amount': flt(d.qty) * flt(d.rate) / flt(doc.conversion_rate) }, 'entries');
-  
-  cur_frm.cscript.calc_total(doc)
-}
-
-
 // Make JV
 // --------
 cur_frm.cscript.make_jv = function(doc, dt, dn, det) {
   var jv = LocalDB.create('Journal Voucher');
   jv = locals['Journal Voucher'][jv];
   jv.voucher_type = 'Bank Voucher';
-  //jv.voucher_series = det.def_bv_series;
-  //jv.voucher_date = doc.voucher_date;
-  //jv.posting_date = doc.posting_date;
   jv.remark = repl('Payment against voucher %(vn)s for %(rem)s', {vn:doc.name, rem:doc.remarks});
   jv.total_debit = doc.outstanding_amount;
   jv.total_credit = doc.outstanding_amount;
@@ -399,20 +339,17 @@
   var d1 = LocalDB.add_child(jv, 'Journal Voucher Detail', 'entries');
   d1.account = doc.credit_to;
   d1.debit = doc.outstanding_amount;
-  //d1.balance = det.acc_balance;
   d1.against_voucher = doc.name;
   
   // credit to bank
   var d1 = LocalDB.add_child(jv, 'Journal Voucher Detail', 'entries');
-  //d1.account = det.def_bank_account;
-  //d1.balance = det.bank_balance;
   d1.credit = doc.outstanding_amount;
   
   loaddoc('Journal Voucher', jv.name);
 }
 
 // ***************** Get project name *****************
-cur_frm.fields_dict['pv_details'].grid.get_field('project_name').get_query = function(doc, cdt, cdn) {
+cur_frm.fields_dict['entries'].grid.get_field('project_name').get_query = function(doc, cdt, cdn) {
   return 'SELECT `tabProject`.name FROM `tabProject` WHERE `tabProject`.status = "Open" AND `tabProject`.name LIKE "%s" ORDER BY `tabProject`.name ASC LIMIT 50';
 }
 
@@ -426,15 +363,6 @@
     cur_frm.pformat.print_heading = "Purchase Invoice";
 }
 
-/* *********************** Client Side Validation **************************** */
-// Validate
-// ---------
-cur_frm.cscript.validate = function(doc, cdt, cdn) {
-  is_item_table(doc,cdt,cdn);
-  cur_frm.cscript.calc_total(doc, cdt, cdn);
-  calc_total_advance(doc, cdt, cdn);
-}
-
 /****************** Get Accounting Entry *****************/
 cur_frm.cscript['View Ledger Entry'] = function(){
   var callback = function(report){
diff --git a/erpnext/accounts/doctype/payable_voucher/payable_voucher.txt b/erpnext/accounts/doctype/payable_voucher/payable_voucher.txt
old mode 100644
new mode 100755
index c77db19..93014ec
--- a/erpnext/accounts/doctype/payable_voucher/payable_voucher.txt
+++ b/erpnext/accounts/doctype/payable_voucher/payable_voucher.txt
@@ -5,15 +5,16 @@
 	{
 		'creation': '2010-08-08 17:09:11',
 		'docstatus': 0,
-		'modified': '2011-06-21 17:01:01',
+		'modified': '2011-11-24 15:17:14',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
 
 	# These values are common for all DocType
 	{
-		'_last_update': '1309508838',
+		'_last_update': '1321601347',
 		'colour': 'White:FFF',
+		'default_print_format': 'Standard',
 		'doctype': 'DocType',
 		'module': 'Accounts',
 		'name': '__common__',
@@ -23,7 +24,7 @@
 		'server_code_error': ' ',
 		'show_in_menu': 0,
 		'subject': 'From %(supplier_name)s worth %(grand_total)s due on %(due_date)s | %(outstanding_amount)s outstanding',
-		'version': 518
+		'version': 522
 	},
 
 	# These values are common for all DocField
@@ -57,7 +58,6 @@
 		'cancel': 0,
 		'create': 0,
 		'doctype': 'DocPerm',
-		'idx': 1,
 		'permlevel': 1,
 		'role': 'Accounts Manager',
 		'submit': 0,
@@ -70,7 +70,6 @@
 		'cancel': 1,
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 2,
 		'permlevel': 0,
 		'role': 'Accounts Manager',
 		'submit': 1,
@@ -83,7 +82,6 @@
 		'cancel': 0,
 		'create': 0,
 		'doctype': 'DocPerm',
-		'idx': 3,
 		'permlevel': 1,
 		'role': 'Accounts User',
 		'submit': 0,
@@ -93,7 +91,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 4,
 		'permlevel': 1,
 		'role': 'Purchase User'
 	},
@@ -104,7 +101,6 @@
 		'cancel': 0,
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 5,
 		'permlevel': 0,
 		'role': 'Purchase User',
 		'submit': 0,
@@ -117,7 +113,6 @@
 		'cancel': 1,
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 6,
 		'permlevel': 0,
 		'role': 'Accounts User',
 		'submit': 1,
@@ -127,7 +122,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 7,
 		'match': 'supplier',
 		'permlevel': 0,
 		'role': 'Supplier'
@@ -138,7 +132,6 @@
 		'allow_on_submit': 0,
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 1,
 		'label': 'Basic Info',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0
@@ -148,7 +141,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 2,
 		'oldfieldtype': 'Column Break',
 		'permlevel': 0,
 		'width': '50%'
@@ -161,7 +153,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'naming_series',
 		'fieldtype': 'Select',
-		'idx': 3,
 		'label': 'Series',
 		'no_copy': 1,
 		'oldfieldname': 'naming_series',
@@ -179,7 +170,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'credit_to',
 		'fieldtype': 'Link',
-		'idx': 4,
 		'in_filter': 1,
 		'label': 'Credit To',
 		'oldfieldname': 'credit_to',
@@ -197,7 +187,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'amended_from',
 		'fieldtype': 'Link',
-		'idx': 5,
 		'label': 'Amended From',
 		'no_copy': 1,
 		'oldfieldname': 'amended_from',
@@ -214,14 +203,12 @@
 		'fieldname': 'supplier',
 		'fieldtype': 'Link',
 		'hidden': 0,
-		'idx': 6,
 		'label': 'Supplier',
 		'oldfieldname': 'supplier',
 		'oldfieldtype': 'Link',
 		'options': 'Supplier',
 		'permlevel': 0,
 		'print_hide': 1,
-		'search_index': 1,
 		'trigger': 'Client'
 	},
 
@@ -230,11 +217,8 @@
 		'doctype': 'DocField',
 		'fieldname': 'supplier_address',
 		'fieldtype': 'Link',
-		'idx': 7,
 		'label': 'Supplier Address',
-		'options': 'Address',
-		'permlevel': 0,
-		'print_hide': 1
+		'permlevel': 0
 	},
 
 	# DocField
@@ -242,7 +226,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'contact_person',
 		'fieldtype': 'Link',
-		'idx': 8,
 		'label': 'Contact Person',
 		'options': 'Contact',
 		'permlevel': 0,
@@ -254,7 +237,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'supplier_name',
 		'fieldtype': 'Text',
-		'idx': 9,
 		'label': 'Name',
 		'oldfieldname': 'supplier_name',
 		'oldfieldtype': 'Data',
@@ -266,7 +248,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'address_display',
 		'fieldtype': 'Small Text',
-		'idx': 10,
 		'label': 'Address',
 		'permlevel': 1
 	},
@@ -276,7 +257,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'contact_display',
 		'fieldtype': 'Small Text',
-		'idx': 11,
 		'label': 'Contact',
 		'permlevel': 1
 	},
@@ -286,7 +266,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'contact_mobile',
 		'fieldtype': 'Text',
-		'idx': 12,
 		'label': 'Mobile No',
 		'permlevel': 1
 	},
@@ -296,7 +275,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'contact_email',
 		'fieldtype': 'Text',
-		'idx': 13,
 		'label': 'Contact Email',
 		'permlevel': 1,
 		'print_hide': 1
@@ -306,7 +284,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 14,
 		'oldfieldtype': 'Column Break',
 		'permlevel': 0,
 		'reqd': 0,
@@ -319,7 +296,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'voucher_date',
 		'fieldtype': 'Date',
-		'idx': 15,
 		'in_filter': 1,
 		'label': 'Voucher Date',
 		'no_copy': 0,
@@ -327,7 +303,7 @@
 		'oldfieldtype': 'Date',
 		'permlevel': 0,
 		'reqd': 1,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
@@ -335,7 +311,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'due_date',
 		'fieldtype': 'Date',
-		'idx': 16,
 		'in_filter': 1,
 		'label': 'Due Date',
 		'no_copy': 0,
@@ -343,7 +318,7 @@
 		'oldfieldtype': 'Date',
 		'permlevel': 0,
 		'print_hide': 0,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
@@ -353,7 +328,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'posting_date',
 		'fieldtype': 'Date',
-		'idx': 17,
 		'in_filter': 1,
 		'label': 'Posting Date',
 		'no_copy': 0,
@@ -371,7 +345,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'bill_no',
 		'fieldtype': 'Data',
-		'idx': 18,
 		'in_filter': 1,
 		'label': 'Bill No',
 		'oldfieldname': 'bill_no',
@@ -379,7 +352,7 @@
 		'permlevel': 0,
 		'print_hide': 1,
 		'reqd': 1,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
@@ -387,7 +360,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'bill_date',
 		'fieldtype': 'Date',
-		'idx': 19,
 		'in_filter': 1,
 		'label': 'Bill Date',
 		'oldfieldname': 'bill_date',
@@ -395,7 +367,7 @@
 		'permlevel': 0,
 		'print_hide': 1,
 		'reqd': 0,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
@@ -404,7 +376,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'amendment_date',
 		'fieldtype': 'Date',
-		'idx': 20,
 		'label': 'Amendment Date',
 		'no_copy': 1,
 		'oldfieldname': 'amendment_date',
@@ -418,7 +389,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'mode_of_payment',
 		'fieldtype': 'Select',
-		'idx': 21,
 		'label': 'Mode of Payment',
 		'oldfieldname': 'mode_of_payment',
 		'oldfieldtype': 'Select',
@@ -430,7 +400,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 22,
 		'label': 'Items',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0
@@ -443,7 +412,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'purchase_order_main',
 		'fieldtype': 'Link',
-		'idx': 23,
 		'label': 'Purchase Order',
 		'oldfieldname': 'purchase_order_main',
 		'oldfieldtype': 'Link',
@@ -460,7 +428,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'purchase_receipt_main',
 		'fieldtype': 'Link',
-		'idx': 24,
 		'label': 'Purchase Receipt',
 		'oldfieldname': 'purchase_receipt_main',
 		'oldfieldtype': 'Link',
@@ -475,7 +442,6 @@
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 25,
 		'label': 'Get Items',
 		'oldfieldtype': 'Button',
 		'options': 'pull_details',
@@ -486,7 +452,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 26,
 		'permlevel': 0
 	},
 
@@ -495,14 +460,12 @@
 		'doctype': 'DocField',
 		'fieldname': 'currency',
 		'fieldtype': 'Select',
-		'idx': 27,
 		'label': 'Bill Currency',
 		'oldfieldname': 'currency',
 		'oldfieldtype': 'Select',
 		'options': 'link:Currency',
 		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 1
+		'print_hide': 1
 	},
 
 	# DocField
@@ -512,29 +475,26 @@
 		'doctype': 'DocField',
 		'fieldname': 'conversion_rate',
 		'fieldtype': 'Currency',
-		'idx': 28,
 		'label': 'Bill Conversion Rate',
 		'oldfieldname': 'conversion_rate',
 		'oldfieldtype': 'Currency',
 		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 1
+		'print_hide': 1
 	},
 
 	# DocField
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 29,
 		'permlevel': 0
 	},
 
 	# DocField
 	{
+		'allow_on_submit': 1,
 		'doctype': 'DocField',
 		'fieldname': 'entries',
 		'fieldtype': 'Table',
-		'idx': 30,
 		'label': 'Entries',
 		'oldfieldname': 'entries',
 		'oldfieldtype': 'Table',
@@ -546,7 +506,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 31,
 		'label': 'Recalculate',
 		'oldfieldtype': 'Button',
 		'permlevel': 0,
@@ -559,7 +518,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'net_total',
 		'fieldtype': 'Currency',
-		'idx': 32,
 		'label': 'Net Total',
 		'oldfieldname': 'net_total',
 		'oldfieldtype': 'Currency',
@@ -573,7 +531,6 @@
 		'description': 'Add / Edit taxes and other charges',
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 33,
 		'label': 'Taxes',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0
@@ -584,7 +541,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'purchase_other_charges',
 		'fieldtype': 'Link',
-		'idx': 34,
 		'label': 'Purchase Other Charges',
 		'oldfieldname': 'purchase_other_charges',
 		'oldfieldtype': 'Link',
@@ -597,7 +553,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 35,
 		'label': 'Get Tax Detail',
 		'oldfieldtype': 'Button',
 		'options': 'get_purchase_tax_details',
@@ -610,7 +565,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'purchase_tax_details',
 		'fieldtype': 'Table',
-		'idx': 36,
 		'label': 'Purchase Tax Details',
 		'oldfieldname': 'purchase_tax_details',
 		'oldfieldtype': 'Table',
@@ -622,7 +576,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 37,
 		'label': 'Calculate Tax',
 		'oldfieldtype': 'Button',
 		'permlevel': 0,
@@ -634,7 +587,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'HTML',
-		'idx': 38,
 		'label': 'Tax Calculation',
 		'oldfieldtype': 'HTML',
 		'permlevel': 0,
@@ -646,7 +598,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'total_tax',
 		'fieldtype': 'Currency',
-		'idx': 39,
 		'label': 'Total Tax',
 		'oldfieldname': 'total_tax',
 		'oldfieldtype': 'Currency',
@@ -658,7 +609,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 40,
 		'label': 'More Info',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0,
@@ -669,7 +619,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 41,
 		'oldfieldtype': 'Column Break',
 		'permlevel': 0,
 		'print_hide': 0,
@@ -683,7 +632,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'is_opening',
 		'fieldtype': 'Select',
-		'idx': 42,
 		'in_filter': 1,
 		'label': 'Is Opening',
 		'oldfieldname': 'is_opening',
@@ -691,33 +639,15 @@
 		'options': 'No\nYes',
 		'permlevel': 0,
 		'print_hide': 1,
-		'search_index': 0,
+		'search_index': 1,
 		'trigger': 'Client'
 	},
 
 	# DocField
 	{
-		'colour': 'White:FFF',
-		'description': 'Track this Purchase Invoice against any Project',
-		'doctype': 'DocField',
-		'fieldname': 'project_name',
-		'fieldtype': 'Link',
-		'idx': 43,
-		'in_filter': 1,
-		'label': 'Project Name',
-		'oldfieldname': 'project_name',
-		'oldfieldtype': 'Link',
-		'options': 'Project',
-		'permlevel': 0,
-		'search_index': 1
-	},
-
-	# DocField
-	{
 		'doctype': 'DocField',
 		'fieldname': 'aging_date',
 		'fieldtype': 'Date',
-		'idx': 44,
 		'label': 'Aging Date',
 		'oldfieldname': 'aging_date',
 		'oldfieldtype': 'Date',
@@ -733,7 +663,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'select_print_heading',
 		'fieldtype': 'Link',
-		'idx': 45,
 		'label': 'Select Print Heading',
 		'no_copy': 1,
 		'oldfieldname': 'select_print_heading',
@@ -749,7 +678,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 46,
 		'oldfieldtype': 'Column Break',
 		'permlevel': 0,
 		'width': '50%'
@@ -760,7 +688,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'company',
 		'fieldtype': 'Link',
-		'idx': 47,
 		'in_filter': 1,
 		'label': 'Company',
 		'oldfieldname': 'company',
@@ -768,8 +695,7 @@
 		'options': 'Company',
 		'permlevel': 0,
 		'print_hide': 1,
-		'reqd': 1,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
@@ -777,7 +703,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'fiscal_year',
 		'fieldtype': 'Select',
-		'idx': 48,
 		'in_filter': 1,
 		'label': 'Fiscal Year',
 		'oldfieldname': 'fiscal_year',
@@ -785,8 +710,7 @@
 		'options': 'link:Fiscal Year',
 		'permlevel': 0,
 		'print_hide': 1,
-		'reqd': 1,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
@@ -796,7 +720,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'cancel_reason',
 		'fieldtype': 'Data',
-		'idx': 49,
 		'label': 'Cancel Reason',
 		'no_copy': 1,
 		'oldfieldname': 'cancel_reason',
@@ -810,7 +733,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'remarks',
 		'fieldtype': 'Small Text',
-		'idx': 50,
 		'label': 'Remarks',
 		'no_copy': 1,
 		'oldfieldname': 'remarks',
@@ -824,7 +746,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 51,
 		'label': 'Advances',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0,
@@ -835,7 +756,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 52,
 		'label': 'Get Advances Paid',
 		'oldfieldtype': 'Button',
 		'options': 'get_advances',
@@ -849,7 +769,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'advance_allocation_details',
 		'fieldtype': 'Table',
-		'idx': 53,
 		'label': 'Advance Allocation Details',
 		'no_copy': 1,
 		'oldfieldname': 'advance_allocation_details',
@@ -863,7 +782,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 54,
 		'label': 'TDS',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0
@@ -873,7 +791,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 55,
 		'permlevel': 0,
 		'width': '50%'
 	},
@@ -883,7 +800,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'tds_applicable',
 		'fieldtype': 'Select',
-		'idx': 56,
 		'label': 'TDS Applicable',
 		'oldfieldname': 'tds_applicable',
 		'oldfieldtype': 'Select',
@@ -897,7 +813,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'tds_category',
 		'fieldtype': 'Select',
-		'idx': 57,
 		'label': 'TDS Category',
 		'oldfieldname': 'tds_category',
 		'oldfieldtype': 'Select',
@@ -911,7 +826,6 @@
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 58,
 		'label': 'Get TDS',
 		'oldfieldtype': 'Button',
 		'permlevel': 0,
@@ -923,7 +837,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 59,
 		'permlevel': 0,
 		'width': '50%'
 	},
@@ -933,7 +846,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'tax_code',
 		'fieldtype': 'Link',
-		'idx': 60,
 		'label': 'TDS Account Head',
 		'oldfieldname': 'tax_code',
 		'oldfieldtype': 'Link',
@@ -947,7 +859,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'rate',
 		'fieldtype': 'Currency',
-		'idx': 61,
 		'label': 'Rate',
 		'oldfieldname': 'rate',
 		'oldfieldtype': 'Currency',
@@ -960,7 +871,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'ded_amount',
 		'fieldtype': 'Currency',
-		'idx': 62,
 		'label': 'TDS Amount',
 		'oldfieldname': 'ded_amount',
 		'oldfieldtype': 'Currency',
@@ -972,7 +882,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 63,
 		'label': 'Totals',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0
@@ -982,7 +891,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 64,
 		'oldfieldtype': 'Column Break',
 		'permlevel': 0,
 		'width': '50%'
@@ -993,7 +901,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'other_charges_added',
 		'fieldtype': 'Currency',
-		'idx': 65,
 		'label': 'Other Charges Added',
 		'oldfieldname': 'other_charges_added',
 		'oldfieldtype': 'Currency',
@@ -1006,7 +913,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'other_charges_deducted',
 		'fieldtype': 'Currency',
-		'idx': 66,
 		'label': 'Other Charges Deducted',
 		'oldfieldname': 'other_charges_deducted',
 		'oldfieldtype': 'Currency',
@@ -1019,7 +925,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'total_tds_on_voucher',
 		'fieldtype': 'Currency',
-		'idx': 67,
 		'label': 'Total TDS On Voucher',
 		'no_copy': 1,
 		'oldfieldname': 'total_tds_on_voucher',
@@ -1034,7 +939,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'tds_amount_on_advance',
 		'fieldtype': 'Currency',
-		'idx': 68,
 		'label': 'TDS Amount On Advance',
 		'no_copy': 1,
 		'oldfieldname': 'tds_amount_on_advance',
@@ -1048,7 +952,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'total_advance',
 		'fieldtype': 'Currency',
-		'idx': 69,
 		'label': 'Total Advance (Incl. TDS)',
 		'no_copy': 1,
 		'oldfieldname': 'total_advance',
@@ -1063,7 +966,6 @@
 		'fieldname': 'total_amount_to_pay',
 		'fieldtype': 'Currency',
 		'hidden': 0,
-		'idx': 70,
 		'label': 'Total Amount To Pay',
 		'no_copy': 1,
 		'oldfieldname': 'total_amount_to_pay',
@@ -1077,7 +979,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'outstanding_amount',
 		'fieldtype': 'Currency',
-		'idx': 71,
 		'in_filter': 1,
 		'label': 'Outstanding Amount',
 		'no_copy': 1,
@@ -1085,14 +986,13 @@
 		'oldfieldtype': 'Currency',
 		'permlevel': 1,
 		'print_hide': 1,
-		'search_index': 0
+		'search_index': 1
 	},
 
 	# DocField
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
-		'idx': 72,
 		'oldfieldtype': 'Column Break',
 		'permlevel': 0,
 		'print_hide': 1,
@@ -1104,7 +1004,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'grand_total',
 		'fieldtype': 'Currency',
-		'idx': 73,
 		'label': 'Grand Total',
 		'oldfieldname': 'grand_total',
 		'oldfieldtype': 'Currency',
@@ -1119,7 +1018,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'in_words',
 		'fieldtype': 'Data',
-		'idx': 74,
 		'label': 'In Words',
 		'oldfieldname': 'in_words',
 		'oldfieldtype': 'Data',
@@ -1132,7 +1030,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'net_total_import',
 		'fieldtype': 'Currency',
-		'idx': 75,
 		'label': 'Net Total (Import)',
 		'oldfieldname': 'net_total_import',
 		'oldfieldtype': 'Currency',
@@ -1145,7 +1042,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'other_charges_added_import',
 		'fieldtype': 'Currency',
-		'idx': 76,
 		'label': 'Other Charges Added (Import)',
 		'oldfieldname': 'other_charges_added_import',
 		'oldfieldtype': 'Currency',
@@ -1158,7 +1054,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'other_charges_deducted_import',
 		'fieldtype': 'Currency',
-		'idx': 77,
 		'label': 'Other Charges Deducted (Import)',
 		'oldfieldname': 'other_charges_deducted_import',
 		'oldfieldtype': 'Currency',
@@ -1171,7 +1066,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'grand_total_import',
 		'fieldtype': 'Currency',
-		'idx': 78,
 		'label': 'Grand Total (Import)',
 		'oldfieldname': 'grand_total_import',
 		'oldfieldtype': 'Currency',
@@ -1186,7 +1080,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'in_words_import',
 		'fieldtype': 'Data',
-		'idx': 79,
 		'label': 'In Words (Import)',
 		'oldfieldname': 'in_words_import',
 		'oldfieldtype': 'Data',
@@ -1200,7 +1093,6 @@
 		'depends_on': 'eval:!doc.__islocal',
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 80,
 		'label': 'Actions',
 		'oldfieldtype': 'Section Break',
 		'permlevel': 0,
@@ -1212,7 +1104,6 @@
 		'allow_on_submit': 1,
 		'doctype': 'DocField',
 		'fieldtype': 'Button',
-		'idx': 81,
 		'label': 'Repair Outstanding Amt',
 		'oldfieldtype': 'Button',
 		'options': 'repair_pv_outstanding',
@@ -1226,7 +1117,6 @@
 		'fieldname': 'against_expense_account',
 		'fieldtype': 'Small Text',
 		'hidden': 1,
-		'idx': 82,
 		'label': 'Against Expense Account',
 		'no_copy': 1,
 		'oldfieldname': 'against_expense_account',
diff --git a/erpnext/accounts/doctype/pv_detail/pv_detail.txt b/erpnext/accounts/doctype/pv_detail/pv_detail.txt
old mode 100644
new mode 100755
index 1c1c0ae..e1181b0
--- a/erpnext/accounts/doctype/pv_detail/pv_detail.txt
+++ b/erpnext/accounts/doctype/pv_detail/pv_detail.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:17',
 		'docstatus': 0,
-		'modified': '2011-10-20 18:41:17',
+		'modified': '2011-12-14 10:44:21',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -22,7 +22,7 @@
 		'section_style': 'Tray',
 		'server_code_error': ' ',
 		'show_in_menu': 0,
-		'version': 27
+		'version': 42
 	},
 
 	# These values are common for all DocField
@@ -74,18 +74,6 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'brand',
-		'fieldtype': 'Data',
-		'label': 'Brand',
-		'oldfieldname': 'brand',
-		'oldfieldtype': 'Data',
-		'permlevel': 0,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
 		'fieldname': 'description',
 		'fieldtype': 'Text',
 		'label': 'Description',
@@ -98,49 +86,6 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'item_group',
-		'fieldtype': 'Link',
-		'in_filter': 1,
-		'label': 'Item Group',
-		'oldfieldname': 'item_group',
-		'oldfieldtype': 'Link',
-		'options': 'Item Group',
-		'permlevel': 1,
-		'print_hide': 1,
-		'search_index': 1
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
-		'fieldname': 'rate',
-		'fieldtype': 'Currency',
-		'label': 'Rate (Default Curr.)',
-		'oldfieldname': 'rate',
-		'oldfieldtype': 'Currency',
-		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 1,
-		'trigger': 'Client'
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
-		'fieldname': 'import_rate',
-		'fieldtype': 'Currency',
-		'label': 'Rate',
-		'oldfieldname': 'import_rate',
-		'oldfieldtype': 'Currency',
-		'permlevel': 0,
-		'print_hide': 0,
-		'reqd': 1,
-		'trigger': 'Client'
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
 		'fieldname': 'qty',
 		'fieldtype': 'Currency',
 		'label': 'Qty',
@@ -155,14 +100,35 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'amount',
+		'fieldname': 'import_ref_rate',
 		'fieldtype': 'Currency',
-		'label': 'Amount (Default Curr.)',
-		'oldfieldname': 'amount',
+		'label': 'Ref Rate ',
+		'permlevel': 0,
+		'trigger': 'Client'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'discount_rate',
+		'fieldtype': 'Currency',
+		'label': 'Discount %',
+		'permlevel': 0,
+		'trigger': 'Client'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'import_rate',
+		'fieldtype': 'Currency',
+		'label': 'Rate ',
+		'oldfieldname': 'import_rate',
 		'oldfieldtype': 'Currency',
-		'permlevel': 1,
-		'print_hide': 1,
-		'reqd': 1
+		'permlevel': 0,
+		'print_hide': 0,
+		'reqd': 1,
+		'trigger': 'Client'
 	},
 
 	# DocField
@@ -179,6 +145,43 @@
 
 	# DocField
 	{
+		'doctype': 'DocField',
+		'fieldname': 'purchase_ref_rate',
+		'fieldtype': 'Currency',
+		'label': 'Ref Rate *',
+		'permlevel': 0,
+		'trigger': 'Client'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'rate',
+		'fieldtype': 'Currency',
+		'label': 'Rate *(Default Curr.)',
+		'oldfieldname': 'rate',
+		'oldfieldtype': 'Currency',
+		'permlevel': 0,
+		'print_hide': 1,
+		'reqd': 1,
+		'trigger': 'Client'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'amount',
+		'fieldtype': 'Currency',
+		'label': 'Amount (Default Curr.)',
+		'oldfieldname': 'amount',
+		'oldfieldtype': 'Currency',
+		'permlevel': 1,
+		'print_hide': 1,
+		'reqd': 1
+	},
+
+	# DocField
+	{
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldname': 'expense_head',
@@ -226,6 +229,33 @@
 	# DocField
 	{
 		'doctype': 'DocField',
+		'fieldname': 'brand',
+		'fieldtype': 'Data',
+		'label': 'Brand',
+		'oldfieldname': 'brand',
+		'oldfieldtype': 'Data',
+		'permlevel': 0,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'item_group',
+		'fieldtype': 'Link',
+		'in_filter': 1,
+		'label': 'Item Group',
+		'oldfieldname': 'item_group',
+		'oldfieldtype': 'Link',
+		'options': 'Item Group',
+		'permlevel': 1,
+		'print_hide': 1,
+		'search_index': 1
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
 		'fieldname': 'purchase_order',
 		'fieldtype': 'Link',
 		'in_filter': 1,
diff --git a/erpnext/accounts/doctype/receivable_voucher/receivable_voucher.js b/erpnext/accounts/doctype/receivable_voucher/receivable_voucher.js
index 363da2e..12b57d9 100644
--- a/erpnext/accounts/doctype/receivable_voucher/receivable_voucher.js
+++ b/erpnext/accounts/doctype/receivable_voucher/receivable_voucher.js
@@ -67,6 +67,12 @@
 			}
 		}
 	}
+
+	// India related fields
+	var cp = locals['Control Panel']['Control Panel'];
+	if (cp.country == 'India') unhide_field(['c_form_applicable', 'c_form_no']);
+	else hide_field(['c_form_applicable', 'c_form_no']);
+
 }
 
 
diff --git a/erpnext/buying/doctype/po_detail/po_detail.txt b/erpnext/buying/doctype/po_detail/po_detail.txt
old mode 100644
new mode 100755
index bfd014e..1512666
--- a/erpnext/buying/doctype/po_detail/po_detail.txt
+++ b/erpnext/buying/doctype/po_detail/po_detail.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:12',
 		'docstatus': 0,
-		'modified': '2011-10-20 18:39:20',
+		'modified': '2011-12-14 10:49:46',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -22,7 +22,7 @@
 		'section_style': 'Tray',
 		'server_code_error': ' ',
 		'show_in_menu': 0,
-		'version': 54
+		'version': 60
 	},
 
 	# These values are common for all DocField
@@ -121,17 +121,54 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'uom',
-		'fieldtype': 'Link',
-		'label': 'UOM',
-		'oldfieldname': 'uom',
-		'oldfieldtype': 'Link',
-		'options': 'UOM',
+		'fieldname': 'import_ref_rate',
+		'fieldtype': 'Currency',
+		'label': 'Ref Rate ',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'discount_rate',
+		'fieldtype': 'Currency',
+		'label': 'Discount %',
+		'permlevel': 0,
+		'trigger': 'Client'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'import_rate',
+		'fieldtype': 'Currency',
+		'hidden': 0,
+		'label': 'Rate ',
+		'oldfieldname': 'import_rate',
+		'oldfieldtype': 'Currency',
 		'permlevel': 0,
 		'print_hide': 0,
-		'reqd': 1,
-		'trigger': 'Client',
-		'width': '100px'
+		'trigger': 'Client'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'import_amount',
+		'fieldtype': 'Currency',
+		'label': 'Amount',
+		'oldfieldname': 'import_amount',
+		'oldfieldtype': 'Currency',
+		'permlevel': 1
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'purchase_ref_rate',
+		'fieldtype': 'Currency',
+		'label': 'Ref Rate *',
+		'permlevel': 0
 	},
 
 	# DocField
@@ -140,7 +177,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'purchase_rate',
 		'fieldtype': 'Currency',
-		'label': 'Rate (Default Curr.)',
+		'label': 'Rate (Default Curr.) *',
 		'oldfieldname': 'purchase_rate',
 		'oldfieldtype': 'Currency',
 		'permlevel': 0,
@@ -167,31 +204,6 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'import_rate',
-		'fieldtype': 'Currency',
-		'hidden': 0,
-		'label': 'Rate',
-		'oldfieldname': 'import_rate',
-		'oldfieldtype': 'Currency',
-		'permlevel': 0,
-		'print_hide': 0,
-		'trigger': 'Client'
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
-		'fieldname': 'import_amount',
-		'fieldtype': 'Currency',
-		'label': 'Amount',
-		'oldfieldname': 'import_amount',
-		'oldfieldtype': 'Currency',
-		'permlevel': 1
-	},
-
-	# DocField
-	{
-		'doctype': 'DocField',
 		'fieldname': 'warehouse',
 		'fieldtype': 'Link',
 		'hidden': 0,
@@ -207,6 +219,35 @@
 
 	# DocField
 	{
+		'doctype': 'DocField',
+		'fieldname': 'project_name',
+		'fieldtype': 'Link',
+		'in_filter': 1,
+		'label': 'Project Name',
+		'options': 'Project',
+		'permlevel': 0,
+		'print_hide': 1,
+		'report_hide': 0
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'uom',
+		'fieldtype': 'Link',
+		'label': 'UOM',
+		'oldfieldname': 'uom',
+		'oldfieldtype': 'Link',
+		'options': 'UOM',
+		'permlevel': 0,
+		'print_hide': 0,
+		'reqd': 1,
+		'trigger': 'Client',
+		'width': '100px'
+	},
+
+	# DocField
+	{
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldname': 'conversion_factor',
@@ -224,23 +265,6 @@
 
 	# DocField
 	{
-		'colour': 'White:FFF',
-		'doctype': 'DocField',
-		'fieldname': 'stock_qty',
-		'fieldtype': 'Currency',
-		'hidden': 0,
-		'label': 'Stock Qty',
-		'no_copy': 1,
-		'oldfieldname': 'stock_qty',
-		'oldfieldtype': 'Currency',
-		'permlevel': 0,
-		'print_hide': 1,
-		'trigger': 'Client',
-		'width': '100px'
-	},
-
-	# DocField
-	{
 		'doctype': 'DocField',
 		'fieldname': 'stock_uom',
 		'fieldtype': 'Data',
@@ -256,19 +280,6 @@
 
 	# DocField
 	{
-		'doctype': 'DocField',
-		'fieldname': 'project_name',
-		'fieldtype': 'Link',
-		'in_filter': 1,
-		'label': 'Project Name',
-		'options': 'Project',
-		'permlevel': 0,
-		'print_hide': 1,
-		'report_hide': 0
-	},
-
-	# DocField
-	{
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
 		'fieldname': 'prevdoc_doctype',
@@ -365,6 +376,23 @@
 
 	# DocField
 	{
+		'colour': 'White:FFF',
+		'doctype': 'DocField',
+		'fieldname': 'stock_qty',
+		'fieldtype': 'Currency',
+		'hidden': 0,
+		'label': 'Stock Qty',
+		'no_copy': 1,
+		'oldfieldname': 'stock_qty',
+		'oldfieldtype': 'Currency',
+		'permlevel': 1,
+		'print_hide': 1,
+		'trigger': 'Client',
+		'width': '100px'
+	},
+
+	# DocField
+	{
 		'doctype': 'DocField',
 		'fieldname': 'received_qty',
 		'fieldtype': 'Currency',
@@ -410,7 +438,7 @@
 
 	# DocField
 	{
-		'allow_on_submit': 0,
+		'allow_on_submit': 1,
 		'doctype': 'DocField',
 		'fieldname': 'page_break',
 		'fieldtype': 'Check',
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js
index f91b1d8..a429b68 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.js
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.js
@@ -13,14 +13,6 @@
     }
 }
 
-/*
-// ======================== Supplier =================================================
-cur_frm.cscript.supplier = function(doc, cdt, cdn) {
-  if(doc.supplier) get_server_fields('get_supplier_details', doc.supplier,'', doc, cdt, cdn, 1);
-}
-
-*/
-
 
 // ======================== Conversion Rate ==========================================
 cur_frm.cscript.conversion_rate = function(doc,cdt,cdn) {
@@ -54,22 +46,11 @@
   }
 }
 
-//==================== Purchase UOM Get Query =======================================================
-//cur_frm.fields_dict[fname].grid.get_field("uom").get_query = function(doc, cdt, cdn) {
-//  var d = locals[this.doctype][this.docname];
-//  return 'SELECT `tabUOM Conversion Detail`.`uom` FROM `tabUOM Conversion Detail` WHERE `tabUOM Conversion Detail`.`parent` = "' + d.item_code + '" AND `tabUOM Conversion Detail`.uom LIKE "%s"'
-//}
-
-
 //==================== UOM ======================================================================
 cur_frm.cscript.uom = function(doc, cdt, cdn) {
   var d = locals[cdt][cdn];
   if (d.item_code && d.uom) {
     call_back = function(doc, cdt, cdn){
-      //refresh_field('purchase_rate', d.name, fname);
-      //refresh_field('qty' , d.name, fname);
-      //refresh_field('conversion_factor' , d.name, fname);
-      //var doc = locals[cdt][cdn];
       cur_frm.cscript.calc_amount(doc, 2);
     }
     str_arg = {'item_code':d.item_code, 'uom':d.uom, 'stock_qty':flt(d.stock_qty), 'qty': flt(d.qty)}
@@ -115,47 +96,26 @@
 
 //=================== Purchase Rate ==============================================================
 cur_frm.cscript.purchase_rate = function(doc, cdt, cdn) {
-  // Calculate Amount
   cur_frm.cscript.calc_amount(doc, 2);
 }
 
 //==================== Import Rate ================================================================
 cur_frm.cscript.import_rate = function(doc, cdt, cdn) {
-  // Calculate Amount
   cur_frm.cscript.calc_amount(doc, 1);
 }
 
-
-//====================== Calculate Amount  ============================================================
-/*cur_frm.cscript.calc_amount = function(doc, n) {
-  // Set defaults
-  doc = locals[doc.doctype][doc.name] 
-  if (! doc.conversion_rate) doc.conversion_rate = 1;
-  if(!n) n=0;
-  var net_total = 0;
-  var net_total_import = 0;
-  
-  var cl = getchildren(tname, doc.name, fname);
-  
-  for(var i=0;i<cl.length;i++) 
-  {
-    if(n == 1){ 
-      set_multiple(tname, cl[i].name, {'purchase_rate': flt(doc.conversion_rate) * flt(cl[i].import_rate) }, fname);
-      set_multiple(tname, cl[i].name, {'amount': flt(flt(cl[i].qty) * flt(doc.conversion_rate) * flt(cl[i].import_rate))}, fname);
-      set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) * flt(cl[i].import_rate))}, fname);
-    }
-    if(n == 2){
-      set_multiple(tname, cl[i].name, {'amount': flt(flt(cl[i].qty) * flt(cl[i].purchase_rate)), 'import_rate': flt(flt(cl[i].purchase_rate) / flt(doc.conversion_rate)) }, fname);
-      set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) *  flt(cl[i].purchase_rate) / flt(doc.conversion_rate))}, fname);
-    }
-    net_total += flt(flt(cl[i].qty) * flt(cl[i].purchase_rate));
-    net_total_import += flt(flt(cl[i].qty) * flt(cl[i].import_rate));
-  }
-  doc.net_total = flt(net_total) ;
-  doc.net_total_import = flt(net_total_import) ;
-  refresh_field('net_total');
-  refresh_field('net_total_import');
-}  */
+//==================== Discount Rate ================================================================
+cur_frm.cscript.discount_rate = function(doc, cdt, cdn) {
+  cur_frm.cscript.calc_amount(doc, 4);
+}
+//==================== Purchase Ref Rate ================================================================
+cur_frm.cscript.purchase_ref_rate = function(doc, cdt, cdn) {
+  cur_frm.cscript.calc_amount(doc, 4);
+}
+//==================== Import Ref Rate ================================================================
+cur_frm.cscript.import_ref_rate = function(doc, cdt, cdn) {
+  cur_frm.cscript.calc_amount(doc, 5);
+}
 
 //==================== check if item table is blank ==============================================
 var is_item_table = function(doc,cdt,cdn) {
@@ -173,18 +133,11 @@
   is_item_table(doc,cdt,cdn);
   // Step 2:=> Calculate Amount
   cur_frm.cscript.calc_amount(doc, 1);
+
+  // calculate advances if pv
+  if(doc.doctype == 'Payable Voucher') calc_total_advance(doc, cdt, cdn);
 }
 
-
-
-/*cur_frm.cscript.other_fname = "purchase_tax_details";
-other_charges ===> purchase_tax_details
-RV Tax Detail ===> Purchase Tax Detail
-cur_frm.cscript.recalc ===> cur_frm.cscript.calc_amount
-export ===> import
-other_charges_total ===> total_tax
-Other Charges Calculation  ===> Tax Calculation*/
-
 // **************** RE-CALCULATE VALUES ***************************
 
 cur_frm.cscript['Re-Calculate Values'] = function(doc, cdt, cdn) {
@@ -205,8 +158,7 @@
       validated = false;
     }
   }
-  if(doc.doctype != 'Payable Voucher') cur_frm.cscript.calc_amount(doc, 1);
-  else if(doc.doctype == 'Payable Voucher') cur_frm.cscript.calc_total(doc);
+  cur_frm.cscript.calc_amount(doc, 1);
 }
 
 
@@ -218,10 +170,6 @@
   return detail;
 }
 
-  //if(cur_frm.cscript.custom_recalc)cur_frm.cscript.custom_recalc(doc);
-
-
-
 cur_frm.cscript.amount = function(doc, cdt, cdn) {
   cur_frm.cscript.calc_amount(doc, 3);
 }
@@ -241,25 +189,56 @@
   
   for(var i=0;i<cl.length;i++) 
   {
+	var rate_fld = (doc.doctype != 'Payable Voucher') ? 'purchase_rate': 'rate';
+    var tmp = {};
+	if(!cl[i].discount_rate) cl[i].discount_rate = 0;
+
     if(n == 1){ 
-      set_multiple(tname, cl[i].name, {'purchase_rate': flt(doc.conversion_rate) * flt(cl[i].import_rate) }, fname);
+      set_multiple(tname, cl[i].name, {'purchase_ref_rate':flt(cl[i].import_ref_rate)*flt(doc.conversion_rate)}, fname);
+	  set_multiple(tname, cl[i].name, {'discount_rate': flt(flt( flt( flt(cl[i].import_ref_rate) - flt(cl[i].import_rate) ) * 100 )/flt(cl[i].import_ref_rate)) }, fname);
+	  tmp[rate_fld] = flt(doc.conversion_rate) * flt(cl[i].import_rate);
+      set_multiple(tname, cl[i].name, tmp, fname);
+
       set_multiple(tname, cl[i].name, {'amount': flt(flt(cl[i].qty) * flt(doc.conversion_rate) * flt(cl[i].import_rate))}, fname);
       set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) * flt(cl[i].import_rate))}, fname);
-    }
-    if(n == 2){
-      set_multiple(tname, cl[i].name, {'amount': flt(flt(cl[i].qty) * flt(cl[i].purchase_rate)), 'import_rate': flt(flt(cl[i].purchase_rate) / flt(doc.conversion_rate)) }, fname);
-      set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) *  flt(cl[i].purchase_rate) / flt(doc.conversion_rate))}, fname);
-    }
-    if(n == 3){
-      set_multiple(tname, cl[i].name, {'purchase_rate': flt(flt(cl[i].amount) / flt(cl[i].qty)) }, fname);
-      set_multiple(tname, cl[i].name, {'import_rate': flt(flt(cl[i].purchase_rate) / flt(doc.conversion_rate))}, fname); 
-      set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) *  flt(cl[i].purchase_rate) / flt(doc.conversion_rate))}, fname);
-    }
+
+    }else if(n == 2){
+	  set_multiple(tname, cl[i].name, {'purchase_ref_rate':flt(cl[i].import_ref_rate)*flt(doc.conversion_rate)}, fname);
+	  set_multiple(tname, cl[i].name, {'discount_rate': flt(flt( flt( flt(cl[i].purchase_ref_rate) - flt(cl[i][rate_fld]) ) * 100 )/flt(cl[i].purchase_ref_rate)) }, fname);
+      set_multiple(tname, cl[i].name, {'amount': flt(flt(cl[i].qty) * flt(cl[i][rate_fld])),}, fname);
+	  set_multiple(tname, cl[i].name, {'import_rate': flt(flt(cl[i][rate_fld]) / flt(doc.conversion_rate)) }, fname);
+      set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) *  flt(cl[i][rate_fld]) / flt(doc.conversion_rate))}, fname);
+
+	}else if(n == 3){
+	  tmp[rate_fld] = flt(flt(cl[i].amount) / flt(cl[i].qty));
+      set_multiple(tname, cl[i].name, tmp, fname);
+      set_multiple(tname, cl[i].name, {'import_rate': flt(flt(cl[i][rate_fld]) / flt(doc.conversion_rate))}, fname); 
+      set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) *  flt(cl[i][rate_fld]) / flt(doc.conversion_rate))}, fname);
+
+    }else if( n==4){	  
+
+	  set_multiple(tname, cl[i].name, {'import_ref_rate': flt(flt(cl[i].purchase_ref_rate) / flt(doc.conversion_rate))}, fname);
+
+      tmp[rate_fld] = flt( flt(cl[i].purchase_ref_rate) - flt(flt(cl[i].purchase_ref_rate)*flt(cl[i].discount_rate)/100) )
+	  set_multiple(tname, cl[i].name, tmp, fname);
+
+	  set_multiple(tname, cl[i].name, {'import_rate': flt(flt(cl[i][rate_fld]) / flt(doc.conversion_rate))}, fname); 
+	  set_multiple(tname, cl[i].name, {'amount':flt(flt(cl[i].qty) * flt(cl[i][rate_fld]))}, fname);
+	  set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) *  flt(cl[i][rate_fld]) / flt(doc.conversion_rate))}, fname); 
+
+	}else if( n==5){	
+	  tmp[rate_fld] = flt( flt(cl[i].import_ref_rate) - flt(flt(cl[i].import_ref_rate)*flt(cl[i].discount_rate)/100) ) * flt(doc.conversion_rate);
+	  set_multiple(tname, cl[i].name, {'purchase_ref_rate': flt(flt(cl[i].import_ref_rate) * flt(doc.conversion_rate))}, fname);
+	  set_multiple(tname, cl[i].name, tmp, fname);
+	  set_multiple(tname, cl[i].name, {'import_rate': flt(flt(cl[i][rate_fld]) / flt(doc.conversion_rate))}, fname); 
+	  set_multiple(tname, cl[i].name, {'amount':flt(flt(cl[i].qty) * flt(cl[i][rate_fld]))}, fname);
+	  set_multiple(tname, cl[i].name, {'import_amount': flt(flt(cl[i].qty) *  flt(cl[i][rate_fld]) / flt(doc.conversion_rate))}, fname); 
+	}
+	
     if (n != 3){
-      net_total += flt(flt(cl[i].qty) * flt(cl[i].purchase_rate));
+      net_total += flt(flt(cl[i].qty) * flt(cl[i][rate_fld]));
       net_total_import += flt(flt(cl[i].qty) * flt(cl[i].import_rate));
-    }
-    else if(n == 3){
+    } else if(n == 3){
       net_total += flt(cl[i].amount);
       net_total_import += flt(cl[i].amount) / flt(doc.conversion_rate);
     }
@@ -296,7 +275,7 @@
   }
   cur_frm.cscript.calc_doc_values(doc, cdt, cdn, tname, fname, other_fname); // calculates total amounts
 
-  refresh_many(['net_total','grand_total','rounded_total','grand_total_import','rounded_total_import','in_words','in_words_import','purchase_tax_details','total_tax','other_charges_added', 'other_charges_deducted','net_total_import','other_charges_added_import','other_charges_deducted_import']);
+  refresh_many(['net_total', 'grand_total', 'rounded_total', 'grand_total_import', 'rounded_total_import', 'in_words', 'in_words_import', 'purchase_tax_details', 'total_tax', 'other_charges_added', 'other_charges_deducted', 'net_total_import', 'other_charges_added_import', 'other_charges_deducted_import']);
 
 }
 
@@ -451,21 +430,27 @@
   doc.grand_total_import = flt(flt(doc.grand_total) / flt(doc.conversion_rate));
   doc.rounded_total_import = Math.round(doc.grand_total_import);
 
+  refresh_many(['net_total','total_taxes','grand_total']);
+
+
   if(doc.doctype == 'Payable Voucher'){
-    var t_tds_tax = 0.0;
-    
-    doc.total_tds_on_voucher = flt(doc.ded_amount)
-    // total amount to pay
-    
-    doc.total_amount_to_pay = flt(flt(net_total) + flt(other_charges_added) - flt(other_charges_deducted) - flt(doc.total_tds_on_voucher));
-    
-    // outstanding amount 
-    if(doc.docstatus==0) doc.outstanding_amount = flt(doc.net_total) + flt(other_charges_added) - flt(other_charges_deducted) - flt(doc.total_tds_on_voucher) - flt(doc.total_advance);
-    
-    refresh_many(['net_total','total_taxes','grand_total','total_tds_on_voucher','total_amount_to_pay', 'outstanding_amount']);
+    calculate_outstanding(doc);
   }
 }
 
+var calculate_outstanding = function(doc) {
+	var t_tds_tax = 0.0;	
+	doc.total_tds_on_voucher = flt(doc.ded_amount);
+
+	// total amount to pay	
+	doc.total_amount_to_pay = flt(flt(doc.net_total) + flt(doc.other_charges_added) - flt(doc.other_charges_deducted) - flt(doc.total_tds_on_voucher));
+	
+	// outstanding amount 
+	if(doc.docstatus==0) doc.outstanding_amount = flt(doc.net_total) + flt(doc.other_charges_added) - flt(doc.other_charges_deducted) - flt(doc.total_tds_on_voucher) - flt(doc.total_advance);
+	
+	refresh_many(['total_tds_on_voucher','total_amount_to_pay', 'outstanding_amount']);
+}
+
 
 cur_frm.cscript.check_charge_type_and_get_tax_amount = function(doc, tax, t, cl, rate, print_amt) {
   doc = locals[doc.doctype][doc.name];
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.py b/erpnext/buying/doctype/purchase_common/purchase_common.py
index 29658cb..7612c19 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.py
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.py
@@ -120,7 +120,8 @@
 			'conversion_factor'	: '1',
 			'warehouse'					: wh,
 			'item_tax_rate'			: str(t),
-			'batch_no'					 : ''		
+			'batch_no'					 : '',
+			'discount_rate'			: 0		
 		}
 		
 		# get min_order_qty from item
@@ -139,6 +140,8 @@
 		
 		#	get last purchase rate as per stock uom and default currency for following list of doctypes
 		if obj.doc.doctype in ['Supplier Quotation', 'Purchase Order', 'Purchase Receipt']:
+			ret['purchase_ref_rate'] = item and flt(item[0]['last_purchase_rate']) or 0
+			ret['import_ref_rate'] = flt(item and flt(item[0]['last_purchase_rate']) or 0) / flt(obj.doc.fields.has_key('conversion_rate') and flt(obj.doc.conversion_rate) or 1)			
 			ret['purchase_rate'] = item and flt(item[0]['last_purchase_rate']) or 0
 			ret['import_rate'] = flt(item and flt(item[0]['last_purchase_rate']) or 0) / flt(obj.doc.fields.has_key('conversion_rate') and flt(obj.doc.conversion_rate) or 1)
 		
@@ -159,8 +162,11 @@
 		if uom:
 			ret = {
 				'conversion_factor' : flt(uom[0]['conversion_factor']),
-				'qty'				 : flt(arg['stock_qty']) / flt(uom[0]['conversion_factor']),
-				'purchase_rate'		 : (lpr and flt(lpr[0]['last_purchase_rate']) * flt(uom[0]['conversion_factor'])) or 0
+				'qty'				: flt(arg['stock_qty']) / flt(uom[0]['conversion_factor']),
+				'purchase_ref_rate'	: (lpr and flt(lpr[0]['last_purchase_rate']) * flt(uom[0]['conversion_factor'])) or 0,
+				'purchase_rate'		: (lpr and flt(lpr[0]['last_purchase_rate']) * flt(uom[0]['conversion_factor'])) or 0,
+				'import_ref_rate'	: (lpr and flt(lpr[0]['last_purchase_rate']) * flt(uom[0]['conversion_factor'])/flt(self.doc.conversion_rate)) or 0,
+				'import_rate'		: (lpr and flt(lpr[0]['last_purchase_rate']) * flt(uom[0]['conversion_factor'])/flt(self.doc.conversion_rate)) or 0
 			}
 		
 		return ret
@@ -170,8 +176,7 @@
 		for d in getlist(obj.doclist, obj.fname):
 			if d.item_code:
 				rate = sql("select last_purchase_rate from `tabItem` where name = '%s' and (ifnull(end_of_life,'')='' or end_of_life = '0000-00-00' or end_of_life >	now())"% cstr(d.item_code), as_dict = 1 )
-				d.purchase_rate =	rate and flt(rate[0]['last_purchase_rate']) * flt(d.conversion_factor) or 0
-
+				d.purchase_rate = rate and flt(rate[0]['last_purchase_rate']) * flt(d.conversion_factor) or 0
 				if not rate[0]['last_purchase_rate']:
 					msgprint("%s has no Last Purchase Rate."% d.item_code)
 
diff --git a/erpnext/patches/Discount_purchase_cycle.py b/erpnext/patches/Discount_purchase_cycle.py
new file mode 100755
index 0000000..34fd498
--- /dev/null
+++ b/erpnext/patches/Discount_purchase_cycle.py
@@ -0,0 +1,14 @@
+def execute():
+	import webnotes
+	from webnotes.modules.module_manager import reload_doc
+
+	reload_doc('accounts', 'doctype', 'pv_detail')
+	reload_doc('buying', 'doctype', 'po_detail')
+	reload_doc('stock', 'doctype', 'purchase_receipt_detail')
+	if webnotes.conn.sql("select name from `tabDocField` where parent = 'PO Detail' and fieldname = 'discount'"):
+		webnotes.conn.sql("update `tabPO Detail` set discount_rate=discount")
+
+	# Features setup
+	reload_doc('setup', 'doctype', 'features_setup')
+	from webnotes.model.code import get_obj
+	get_obj('Features Setup').validate()
diff --git a/erpnext/patches/reload_rv.py b/erpnext/patches/reload_rv.py
index 5f3bc21..7b47b4c 100644
--- a/erpnext/patches/reload_rv.py
+++ b/erpnext/patches/reload_rv.py
@@ -5,4 +5,5 @@
 
 	reload_doc('accounts', 'doctype', 'receivable_voucher')
 
+	reload_doc('setup', 'doctype', 'features_setup')
 	get_obj('Features setup').validate()
diff --git a/erpnext/setup/doctype/features_setup/features_setup.py b/erpnext/setup/doctype/features_setup/features_setup.py
index f12d282..3a549d9 100644
--- a/erpnext/setup/doctype/features_setup/features_setup.py
+++ b/erpnext/setup/doctype/features_setup/features_setup.py
@@ -8,7 +8,6 @@
 		"""
 		from webnotes.model import default_fields 
 		from webnotes.utils import set_default
-		
 		for key in self.doc.fields:
 			if key not in default_fields:
 				set_default(key, self.doc.fields[key])
diff --git a/erpnext/setup/doctype/features_setup/features_setup.txt b/erpnext/setup/doctype/features_setup/features_setup.txt
index d0f02d5..a7c6fe8 100644
--- a/erpnext/setup/doctype/features_setup/features_setup.txt
+++ b/erpnext/setup/doctype/features_setup/features_setup.txt
@@ -5,14 +5,14 @@
 	{
 		'creation': '2011-09-07 11:59:05',
 		'docstatus': 0,
-		'modified': '2011-12-06 18:48:53',
+		'modified': '2011-12-14 11:05:55',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
 
 	# These values are common for all DocType
 	{
-		'_last_update': '1323176623',
+		'_last_update': '1323840127',
 		'colour': 'White:FFF',
 		'default_print_format': 'Standard',
 		'doctype': 'DocType',
@@ -22,7 +22,7 @@
 		'name_case': 'Title Case',
 		'section_style': 'Simple',
 		'show_in_menu': 1,
-		'version': 24
+		'version': 26
 	},
 
 	# These values are common for all DocField
@@ -179,7 +179,17 @@
 		'doctype': 'DocField',
 		'fieldname': 'fs_discounts',
 		'fieldtype': 'Check',
-		'label': 'Discounts'
+		'label': 'Sales Discounts'
+	},
+
+	# DocField
+	{
+		'colour': 'White:FFF',
+		'description': 'Discount Fields will be available in Purchase Order, Purchase Receipt, Purchase Invoice',
+		'doctype': 'DocField',
+		'fieldname': 'fs_purchase_discounts',
+		'fieldtype': 'Check',
+		'label': 'Purchase Discounts'
 	},
 
 	# DocField
diff --git a/erpnext/startup/startup.js b/erpnext/startup/startup.js
index 291265b..07c1629 100644
--- a/erpnext/startup/startup.js
+++ b/erpnext/startup/startup.js
@@ -572,6 +572,11 @@
 		'Receivable Voucher': {'entries':['adj_rate']},
 		'Sales Order': {'sales_order_details':['adj_rate','ref_rate']}
 	},
+	'fs_purchase_discounts': {
+		'Purchase Order': {'po_details':['purchase_ref_rate', 'discount_rate', 'import_ref_rate']},
+		'Purchase Receipt': {'purchase_receipt_details':['purchase_ref_rate', 'discount_rate', 'import_ref_rate']},
+		'Payable Voucher': {'entries':['purchase_ref_rate', 'discount_rate', 'import_ref_rate']}
+	},
 	'fs_brands': {
 		'Delivery Note': {'delivery_note_details':['brand']},
 		'Indent': {'indent_details':['brand']},
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.txt b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.txt
old mode 100644
new mode 100755
diff --git a/erpnext/stock/doctype/purchase_receipt_detail/purchase_receipt_detail.txt b/erpnext/stock/doctype/purchase_receipt_detail/purchase_receipt_detail.txt
old mode 100644
new mode 100755
index 2e3514e..9aacefd
--- 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-11-16 15:43:36',
+		'modified': '2011-12-14 10:50:00',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -22,7 +22,7 @@
 		'section_style': 'Tray',
 		'server_code_error': ' ',
 		'show_in_menu': 0,
-		'version': 63
+		'version': 73
 	},
 
 	# These values are common for all DocField
@@ -104,39 +104,6 @@
 	{
 		'default': '0.00',
 		'doctype': 'DocField',
-		'fieldname': 'rejected_qty',
-		'fieldtype': 'Currency',
-		'in_filter': 0,
-		'label': 'Rejected Quantity',
-		'oldfieldname': 'rejected_qty',
-		'oldfieldtype': 'Currency',
-		'permlevel': 0,
-		'search_index': 0,
-		'trigger': 'Client',
-		'width': '100px'
-	},
-
-	# DocField
-	{
-		'colour': 'White:FFF',
-		'default': '0.00',
-		'doctype': 'DocField',
-		'fieldname': 'purchase_rate',
-		'fieldtype': 'Currency',
-		'label': 'Rate (Default Curr.)',
-		'oldfieldname': 'purchase_rate',
-		'oldfieldtype': 'Currency',
-		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 1,
-		'trigger': 'Client',
-		'width': '100px'
-	},
-
-	# DocField
-	{
-		'default': '0.00',
-		'doctype': 'DocField',
 		'fieldname': 'qty',
 		'fieldtype': 'Currency',
 		'label': 'Accepted Quantity',
@@ -151,32 +118,34 @@
 	{
 		'default': '0.00',
 		'doctype': 'DocField',
-		'fieldname': 'amount',
+		'fieldname': 'rejected_qty',
 		'fieldtype': 'Currency',
-		'label': 'Amount (Default Curr.)',
-		'oldfieldname': 'amount',
+		'in_filter': 0,
+		'label': 'Rejected Quantity',
+		'oldfieldname': 'rejected_qty',
 		'oldfieldtype': 'Currency',
 		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 0,
+		'search_index': 0,
 		'trigger': 'Client',
 		'width': '100px'
 	},
 
 	# DocField
 	{
-		'colour': 'White:FFF',
 		'doctype': 'DocField',
-		'fieldname': 'uom',
-		'fieldtype': 'Link',
-		'label': 'UOM',
-		'oldfieldname': 'uom',
-		'oldfieldtype': 'Link',
-		'options': 'UOM',
-		'permlevel': 0,
-		'reqd': 1,
-		'trigger': 'Client',
-		'width': '100px'
+		'fieldname': 'import_ref_rate',
+		'fieldtype': 'Currency',
+		'label': 'Ref Rate ',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'discount_rate',
+		'fieldtype': 'Currency',
+		'label': 'Discount  %',
+		'permlevel': 0
 	},
 
 	# DocField
@@ -209,10 +178,21 @@
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldname': 'conversion_factor',
+		'fieldname': 'purchase_ref_rate',
 		'fieldtype': 'Currency',
-		'label': 'Conversion Factor',
-		'oldfieldname': 'conversion_factor',
+		'label': 'Ref Rate *',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'colour': 'White:FFF',
+		'default': '0.00',
+		'doctype': 'DocField',
+		'fieldname': 'purchase_rate',
+		'fieldtype': 'Currency',
+		'label': 'Rate *(Default Curr.)',
+		'oldfieldname': 'purchase_rate',
 		'oldfieldtype': 'Currency',
 		'permlevel': 0,
 		'print_hide': 1,
@@ -223,6 +203,22 @@
 
 	# DocField
 	{
+		'default': '0.00',
+		'doctype': 'DocField',
+		'fieldname': 'amount',
+		'fieldtype': 'Currency',
+		'label': 'Amount (Default Curr.)',
+		'oldfieldname': 'amount',
+		'oldfieldtype': 'Currency',
+		'permlevel': 0,
+		'print_hide': 1,
+		'reqd': 0,
+		'trigger': 'Client',
+		'width': '100px'
+	},
+
+	# DocField
+	{
 		'doctype': 'DocField',
 		'fieldname': 'warehouse',
 		'fieldtype': 'Link',
@@ -238,6 +234,37 @@
 
 	# DocField
 	{
+		'colour': 'White:FFF',
+		'doctype': 'DocField',
+		'fieldname': 'uom',
+		'fieldtype': 'Link',
+		'label': 'UOM',
+		'oldfieldname': 'uom',
+		'oldfieldtype': 'Link',
+		'options': 'UOM',
+		'permlevel': 0,
+		'reqd': 1,
+		'trigger': 'Client',
+		'width': '100px'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'conversion_factor',
+		'fieldtype': 'Currency',
+		'label': 'Conversion Factor',
+		'oldfieldname': 'conversion_factor',
+		'oldfieldtype': 'Currency',
+		'permlevel': 0,
+		'print_hide': 1,
+		'reqd': 1,
+		'trigger': 'Client',
+		'width': '100px'
+	},
+
+	# DocField
+	{
 		'doctype': 'DocField',
 		'fieldname': 'stock_uom',
 		'fieldtype': 'Data',
@@ -269,6 +296,50 @@
 
 	# DocField
 	{
+		'colour': 'White:FFF',
+		'doctype': 'DocField',
+		'fieldname': 'schedule_date',
+		'fieldtype': 'Date',
+		'label': 'Schedule date',
+		'no_copy': 1,
+		'oldfieldname': 'schedule_date',
+		'oldfieldtype': 'Date',
+		'permlevel': 0,
+		'print_hide': 1,
+		'report_hide': 0,
+		'reqd': 1
+	},
+
+	# DocField
+	{
+		'colour': 'White:FFF',
+		'doctype': 'DocField',
+		'fieldname': 'project_name',
+		'fieldtype': 'Link',
+		'in_filter': 1,
+		'label': 'Project Name',
+		'options': 'Project',
+		'permlevel': 0,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'colour': 'White:FFF',
+		'doctype': 'DocField',
+		'fieldname': 'qa_no',
+		'fieldtype': 'Link',
+		'label': 'QA No',
+		'no_copy': 1,
+		'oldfieldname': 'qa_no',
+		'oldfieldtype': 'Link',
+		'options': 'QA Inspection Report',
+		'permlevel': 0,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
 		'doctype': 'DocField',
 		'fieldname': 'brand',
 		'fieldtype': 'Link',
@@ -301,22 +372,6 @@
 	{
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
-		'fieldname': 'schedule_date',
-		'fieldtype': 'Date',
-		'label': 'Schedule date',
-		'no_copy': 1,
-		'oldfieldname': 'schedule_date',
-		'oldfieldtype': 'Date',
-		'permlevel': 0,
-		'print_hide': 1,
-		'report_hide': 0,
-		'reqd': 1
-	},
-
-	# DocField
-	{
-		'colour': 'White:FFF',
-		'doctype': 'DocField',
 		'fieldname': 'serial_no',
 		'fieldtype': 'Text',
 		'in_filter': 1,
@@ -346,21 +401,6 @@
 
 	# DocField
 	{
-		'colour': 'White:FFF',
-		'doctype': 'DocField',
-		'fieldname': 'qa_no',
-		'fieldtype': 'Link',
-		'label': 'QA No',
-		'no_copy': 1,
-		'oldfieldname': 'qa_no',
-		'oldfieldtype': 'Link',
-		'options': 'QA Inspection Report',
-		'permlevel': 0,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
 		'doctype': 'DocField',
 		'fieldname': 'stock_qty',
 		'fieldtype': 'Currency',
@@ -377,19 +417,6 @@
 	{
 		'colour': 'White:FFF',
 		'doctype': 'DocField',
-		'fieldname': 'project_name',
-		'fieldtype': 'Link',
-		'in_filter': 1,
-		'label': 'Project Name',
-		'options': 'Project',
-		'permlevel': 0,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
-		'colour': 'White:FFF',
-		'doctype': 'DocField',
 		'fieldname': 'prevdoc_doctype',
 		'fieldtype': 'Data',
 		'hidden': 1,
@@ -512,7 +539,7 @@
 		'no_copy': 1,
 		'oldfieldname': 'valuation_rate',
 		'oldfieldtype': 'Currency',
-		'permlevel': 0,
+		'permlevel': 1,
 		'print_hide': 1,
 		'width': '80px'
 	},