purchase invoice with write off feature
diff --git a/erpnext/accounts/GL Mapper/Purchase Invoice with write off/Purchase Invoice with write off.txt b/erpnext/accounts/GL Mapper/Purchase Invoice with write off/Purchase Invoice with write off.txt
new file mode 100644
index 0000000..9cb6116
--- /dev/null
+++ b/erpnext/accounts/GL Mapper/Purchase Invoice with write off/Purchase Invoice with write off.txt
@@ -0,0 +1,131 @@
+# GL Mapper, Purchase Invoice with write off
+[
+
+	# These values are common in all dictionaries
+	{
+		'creation': '2012-04-23 11:43:56',
+		'docstatus': 0,
+		'modified': '2012-04-23 11:43:56',
+		'modified_by': u'Administrator',
+		'owner': u'Administrator'
+	},
+
+	# These values are common for all GL Mapper
+	{
+		'doc_type': u'Purchase Invoice with write off',
+		'doctype': 'GL Mapper',
+		'name': '__common__'
+	},
+
+	# These values are common for all GL Mapper Detail
+	{
+		'doctype': u'GL Mapper Detail',
+		'name': '__common__',
+		'parent': u'Purchase Invoice with write off',
+		'parentfield': u'fields',
+		'parenttype': u'GL Mapper'
+	},
+
+	# GL Mapper, Purchase Invoice with write off
+	{
+		'doctype': 'GL Mapper',
+		'name': u'Purchase Invoice with write off'
+	},
+
+	# GL Mapper Detail
+	{
+		'account': u'expense_head',
+		'against': u'parent:credit_to',
+		'aging_date': u'parent:aging_date',
+		'company': u'parent:company',
+		'cost_center': u'cost_center',
+		'credit': u'value:0',
+		'debit': u'amount',
+		'doctype': u'GL Mapper Detail',
+		'fiscal_year': u'parent:fiscal_year',
+		'is_opening': u'parent:is_opening',
+		'posting_date': u'parent:posting_date',
+		'remarks': u'parent:remarks',
+		'table_field': u'entries',
+		'transaction_date': u'parent:voucher_date',
+		'voucher_no': u'parent:name',
+		'voucher_type': u'parent:doctype'
+	},
+
+	# GL Mapper Detail
+	{
+		'account': u'account_head',
+		'against': u'parent:credit_to',
+		'aging_date': u'parent:aging_date',
+		'company': u'parent:company',
+		'cost_center': u'cost_center',
+		'credit': u"value:d.fields.get('category') != 'For Valuation' and d.fields.get('add_deduct_tax') == 'Deduct' and d.fields.get('tax_amount') or 0",
+		'debit': u"value:d.fields.get('category') != 'For Valuation' and d.fields.get('add_deduct_tax') == 'Add' and d.fields.get('tax_amount') or 0",
+		'doctype': u'GL Mapper Detail',
+		'fiscal_year': u'parent:fiscal_year',
+		'is_opening': u'parent:is_opening',
+		'posting_date': u'parent:posting_date',
+		'remarks': u'parent:remarks',
+		'table_field': u'purchase_tax_details',
+		'transaction_date': u'parent:voucher_date',
+		'voucher_no': u'parent:name',
+		'voucher_type': u'parent:doctype'
+	},
+
+	# GL Mapper Detail
+	{
+		'account': u'tax_code',
+		'against': u'credit_to',
+		'aging_date': u'aging_date',
+		'company': u'company',
+		'credit': u'ded_amount',
+		'debit': u'value:0',
+		'doctype': u'GL Mapper Detail',
+		'fiscal_year': u'fiscal_year',
+		'is_opening': u'is_opening',
+		'posting_date': u'posting_date',
+		'remarks': u'remarks',
+		'transaction_date': u'voucher_date',
+		'voucher_no': u'name',
+		'voucher_type': u'doctype'
+	},
+
+	# GL Mapper Detail
+	{
+		'account': u'credit_to',
+		'against': u'against_expense_account',
+		'against_voucher': u'name',
+		'against_voucher_type': u"value:'Purchase Invoice'",
+		'aging_date': u'aging_date',
+		'company': u'company',
+		'credit': u'total_amount_to_pay',
+		'debit': u'value:0',
+		'doctype': u'GL Mapper Detail',
+		'fiscal_year': u'fiscal_year',
+		'is_opening': u'is_opening',
+		'posting_date': u'posting_date',
+		'remarks': u'remarks',
+		'transaction_date': u'voucher_date',
+		'voucher_no': u'name',
+		'voucher_type': u'doctype'
+	},
+
+	# GL Mapper Detail
+	{
+		'account': u'write_off_account',
+		'against': u'credit_to',
+		'aging_date': u'aging_date',
+		'company': u'company',
+		'cost_center': u'write_off_cost_center',
+		'credit': u'write_off_amount',
+		'debit': u'value:0',
+		'doctype': u'GL Mapper Detail',
+		'fiscal_year': u'fiscal_year',
+		'is_opening': u'is_opening',
+		'posting_date': u'posting_date',
+		'remarks': u'remarks',
+		'transaction_date': u'voucher_date',
+		'voucher_no': u'name',
+		'voucher_type': u'doctype'
+	}
+]
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
index f880eb7..7a6ae1b 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -158,6 +158,14 @@
 	if (doc.is_opening == 'Yes') unhide_field('aging_date');
 }
 
+cur_frm.cscript.write_off_amount = function(doc) {
+	doc.total_amount_to_pay = flt(doc.grand_total) - flt(doc.ded_amount) - flt(doc.write_off_amount);
+	doc.outstanding_amount = flt(doc.total_amount_to_pay) - flt(doc.total_advance);
+	refresh_many(['outstanding_amount', 'total_amount_to_pay']);
+}
+
+
+
 // Recalculate Button
 // -------------------
 cur_frm.cscript.recalculate = function(doc, cdt, cdn) {
@@ -347,7 +355,7 @@
 			tot_tds += flt(el[i].tds_allocated)
 		}
 	}
-	doc.total_amount_to_pay = flt(doc.grand_total) - flt(doc.ded_amount);
+	doc.total_amount_to_pay = flt(doc.grand_total) - flt(doc.ded_amount) - flt(doc.write_off_amount);
 	doc.tds_amount_on_advance = flt(tot_tds);
 	doc.total_advance = flt(total_advance);
 	doc.outstanding_amount = flt(doc.total_amount_to_pay) - flt(total_advance);
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index be62c93..66df40f 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -52,11 +52,7 @@
 	# ----------
 	def get_credit_to(self):
 		acc_head = sql("select name, credit_days from `tabAccount` where (name = %s or (master_name = %s and master_type = 'supplier')) and docstatus != 2", (cstr(self.doc.supplier) + " - " + self.get_company_abbr(),self.doc.supplier))		
-		#supp_detail = sql("select supplier_name,address from `tabSupplier` where name = %s", self.doc.supplier, as_dict =1)
-		#ret = {
-		#	'supplier_name' : supp_detail and supp_detail[0]['supplier_name'] or '',
-		#	'supplier_address': supp_detail and supp_detail[0]['address'] or ''
-		#}
+
 		ret = {}
 		if acc_head and acc_head[0][0]:
 			ret['credit_to'] = acc_head[0][0]
@@ -68,7 +64,7 @@
 		
 	def get_cust(self):
 		ret = {}
-		if self.doc.credit_to:			
+		if self.doc.credit_to:
 			ret['supplier'] = get_value('Account',self.doc.credit_to,'master_name')
 			
 		return ret
@@ -364,7 +360,8 @@
 						else:
 							get_obj('TDS Control').get_tds_amount(self)
 							self.doc.total_tds_on_voucher = self.doc.ded_amount
-							self.doc.total_amount_to_pay=flt(self.doc.grand_total)-flt(self.doc.ded_amount)-flt(self.doc.other_tax_deducted)
+							self.doc.total_amount_to_pay=flt(self.doc.grand_total) - flt(self.doc.ded_amount) - self.doc.write_off_amount
+							self.doc.outstanding_amount = self.doc.total_amount_to_pay - flt(self.doc.total_advance)
 					elif self.doc.tds_applicable == 'No':
 						self.doc.tds_category = ''
 						self.doc.tax_code = ''
@@ -416,6 +413,10 @@
 					 msgprint("Purchase Receipt No. required against item %s"%d.item_code)
 					 raise Exception
 
+	def validate_write_off_account(self):
+		if self.doc.write_off_amount and not self.doc.write_off_account:
+			msgprint("Please enter Write Off Account", raise_exception=1)
+
 	# VALIDATE
 	# ====================================================================================
 	def validate(self):
@@ -456,6 +457,8 @@
 		#FY validation
 		get_obj('Sales Common').validate_fiscal_year(self.doc.fiscal_year,self.doc.posting_date,'Posting Date')
 		
+		self.validate_write_off_account()
+		
 		#get Purchase Common Obj
 		pc_obj = get_obj(dt='Purchase Common')
 		
@@ -507,6 +510,8 @@
 		if lst:
 			get_obj('GL Control').reconcile_against_document(lst)
 
+
+
 	# On Submit
 	#--------------------------------------------------------------------
 	def on_submit(self):
@@ -517,13 +522,19 @@
 		
 		
 		# this sequence because outstanding may get -negative
-		get_obj(dt='GL Control').make_gl_entries(self.doc, self.doclist)
-		
+		self.make_gl_entries()
+				
 		self.update_against_document_in_jv()
 		
 		get_obj(dt = 'Purchase Common').update_prevdoc_detail(self, is_submit = 1)
 
 
+	def make_gl_entries(self, is_cancel = 0):
+		get_obj(dt='GL Control').make_gl_entries(self.doc, self.doclist, cancel = is_cancel, \
+			use_mapper = (self.doc.write_off_account and self.doc.write_off_amount and 'Purchase Invoice with write off' or ''))
+
+
+
 
 # ********************************* CANCEL *********************************
 	# Check Next Document's docstatus
@@ -542,7 +553,7 @@
 		# Check whether tds payment voucher has been created against this voucher
 		self.check_tds_payment_voucher()
 		
-		get_obj(dt='GL Control').make_gl_entries(self.doc, self.doclist, cancel=1)
+		self.make_gl_entries(is_cancel=1)
 		get_obj(dt = 'Purchase Common').update_prevdoc_detail(self, is_submit = 0)
 
 
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.txt b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.txt
index 524fa07..666cd1c 100755
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.txt
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-04-11 13:17:19',
+		'creation': '2012-04-13 11:56:17',
 		'docstatus': 0,
-		'modified': '2012-04-12 18:09:05',
+		'modified': '2012-04-23 12:08:52',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -26,7 +26,7 @@
 		'server_code_error': u' ',
 		'show_in_menu': 0,
 		'subject': u'From %(supplier_name)s worth %(grand_total)s due on %(due_date)s | %(outstanding_amount)s outstanding',
-		'version': 522
+		'version': 1
 	},
 
 	# These values are common for all DocField
@@ -522,6 +522,18 @@
 
 	# DocField
 	{
+		'doctype': u'DocField',
+		'fieldname': u'net_total_import',
+		'fieldtype': u'Currency',
+		'label': u'Net Total (Import)',
+		'oldfieldname': u'net_total_import',
+		'oldfieldtype': u'Currency',
+		'permlevel': 1,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
 		'colour': u'White:FFF',
 		'description': u'Add / Edit Taxes and Charges',
 		'doctype': u'DocField',
@@ -938,6 +950,32 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'grand_total',
+		'fieldtype': u'Currency',
+		'label': u'Grand Total',
+		'oldfieldname': u'grand_total',
+		'oldfieldtype': u'Currency',
+		'permlevel': 1,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'colour': u'White:FFF',
+		'description': u'In Words will be visible once you save the Purchase Invoice.',
+		'doctype': u'DocField',
+		'fieldname': u'in_words',
+		'fieldtype': u'Data',
+		'label': u'In Words',
+		'oldfieldname': u'in_words',
+		'oldfieldtype': u'Data',
+		'permlevel': 1,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'total_tds_on_voucher',
 		'fieldtype': u'Currency',
 		'label': u'Total TDS On Voucher',
@@ -965,11 +1003,12 @@
 	# DocField
 	{
 		'doctype': u'DocField',
-		'fieldname': u'total_advance',
+		'fieldname': u'total_amount_to_pay',
 		'fieldtype': u'Currency',
-		'label': u'Total Advance (Incl. TDS)',
+		'hidden': 0,
+		'label': u'Total Amount To Pay',
 		'no_copy': 1,
-		'oldfieldname': u'total_advance',
+		'oldfieldname': u'total_amount_to_pay',
 		'oldfieldtype': u'Currency',
 		'permlevel': 1,
 		'print_hide': 1
@@ -978,12 +1017,11 @@
 	# DocField
 	{
 		'doctype': u'DocField',
-		'fieldname': u'total_amount_to_pay',
+		'fieldname': u'total_advance',
 		'fieldtype': u'Currency',
-		'hidden': 0,
-		'label': u'Total Amount To Pay',
+		'label': u'Total Advance (Incl. TDS)',
 		'no_copy': 1,
-		'oldfieldname': u'total_amount_to_pay',
+		'oldfieldname': u'total_advance',
 		'oldfieldtype': u'Currency',
 		'permlevel': 1,
 		'print_hide': 1
@@ -1018,38 +1056,39 @@
 	# DocField
 	{
 		'doctype': u'DocField',
-		'fieldname': u'grand_total',
+		'fieldname': u'write_off_amount',
 		'fieldtype': u'Currency',
-		'label': u'Grand Total',
-		'oldfieldname': u'grand_total',
-		'oldfieldtype': u'Currency',
-		'permlevel': 1,
+		'label': u'Write Off Amount',
+		'no_copy': 1,
+		'permlevel': 0,
 		'print_hide': 1
 	},
 
 	# DocField
 	{
 		'colour': u'White:FFF',
-		'description': u'In Words will be visible once you save the Purchase Invoice.',
+		'depends_on': u'eval:flt(doc.write_off_amount)!=0',
 		'doctype': u'DocField',
-		'fieldname': u'in_words',
-		'fieldtype': u'Data',
-		'label': u'In Words',
-		'oldfieldname': u'in_words',
-		'oldfieldtype': u'Data',
-		'permlevel': 1,
+		'fieldname': u'write_off_account',
+		'fieldtype': u'Link',
+		'label': u'Write Off Account',
+		'no_copy': 1,
+		'options': u'Account',
+		'permlevel': 0,
 		'print_hide': 1
 	},
 
 	# DocField
 	{
+		'colour': u'White:FFF',
+		'depends_on': u'eval:flt(doc.write_off_amount)!=0',
 		'doctype': u'DocField',
-		'fieldname': u'net_total_import',
-		'fieldtype': u'Currency',
-		'label': u'Net Total (Import)',
-		'oldfieldname': u'net_total_import',
-		'oldfieldtype': u'Currency',
-		'permlevel': 1,
+		'fieldname': u'write_off_cost_center',
+		'fieldtype': u'Link',
+		'label': u'Write Off Cost Center',
+		'no_copy': 1,
+		'options': u'Account',
+		'permlevel': 0,
 		'print_hide': 1
 	},
 
diff --git a/erpnext/patches/jan_mar_2012/rename_dt.py b/erpnext/patches/jan_mar_2012/rename_dt.py
index 7847ee1..3e56fc1 100644
--- a/erpnext/patches/jan_mar_2012/rename_dt.py
+++ b/erpnext/patches/jan_mar_2012/rename_dt.py
@@ -82,6 +82,9 @@
 	#  Reload GL Mapper
 	for d in webnotes.conn.sql("select name from `tabGL Mapper`"):
 		reload_doc('accounts', 'GL Mapper', d[0])
+	reload_doc('accounts', 'GL Mapper', 'Purchase Invoice with write off')
+
+
 		
 	#gl entry, stock ledger entry, 
 
diff --git a/erpnext/utilities/doctype/gl_mapper/gl_mapper.txt b/erpnext/utilities/doctype/gl_mapper/gl_mapper.txt
index ef45427..aef8bd2 100644
--- a/erpnext/utilities/doctype/gl_mapper/gl_mapper.txt
+++ b/erpnext/utilities/doctype/gl_mapper/gl_mapper.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-03-27 14:36:46',
+		'creation': '2012-04-13 11:56:40',
 		'docstatus': 0,
-		'modified': '2012-03-27 14:36:46',
+		'modified': '2012-04-23 12:27:38',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -21,7 +21,7 @@
 		'section_style': u'Simple',
 		'server_code_error': u' ',
 		'show_in_menu': 0,
-		'version': 3
+		'version': 1
 	},
 
 	# These values are common for all DocField
@@ -36,6 +36,7 @@
 
 	# These values are common for all DocPerm
 	{
+		'create': 0,
 		'doctype': u'DocPerm',
 		'name': '__common__',
 		'parent': u'GL Mapper',
@@ -55,7 +56,6 @@
 	{
 		'amend': 0,
 		'cancel': 0,
-		'create': 0,
 		'doctype': u'DocPerm',
 		'role': u'Accounts User',
 		'submit': 0,