Rewritten update_against_document function
diff --git a/erpnext/accounts/doctype/cost_center/cost_center.txt b/erpnext/accounts/doctype/cost_center/cost_center.txt
index fab2dd5..7bc5810 100644
--- a/erpnext/accounts/doctype/cost_center/cost_center.txt
+++ b/erpnext/accounts/doctype/cost_center/cost_center.txt
@@ -5,18 +5,19 @@
 	{
 		'creation': '2010-08-08 17:08:56',
 		'docstatus': 0,
-		'modified': '2010-12-29 18:18:55',
-		'modified_by': 'umair@iwebnotes.com',
+		'modified': '2011-09-26 18:55:05',
+		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
 
 	# These values are common for all DocType
 	{
-		'_last_update': '1308741898',
+		'_last_update': '1316075905',
 		'allow_copy': 1,
 		'allow_trash': 1,
 		'autoname': 'field:cost_center_name',
 		'colour': 'White:FFF',
+		'default_print_format': 'Standard',
 		'doctype': 'DocType',
 		'document_type': 'Master',
 		'in_create': 1,
@@ -26,7 +27,7 @@
 		'section_style': 'Simple',
 		'server_code_error': ' ',
 		'show_in_menu': 0,
-		'version': 104
+		'version': 107
 	},
 
 	# These values are common for all DocField
@@ -60,7 +61,6 @@
 		'cancel': 0,
 		'create': 0,
 		'doctype': 'DocPerm',
-		'idx': 1,
 		'permlevel': 1,
 		'role': 'Accounts Manager',
 		'submit': 0,
@@ -73,7 +73,6 @@
 		'cancel': 1,
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 2,
 		'permlevel': 0,
 		'role': 'Accounts Manager',
 		'submit': 0,
@@ -86,7 +85,6 @@
 		'cancel': 0,
 		'create': 0,
 		'doctype': 'DocPerm',
-		'idx': 3,
 		'permlevel': 1,
 		'role': 'Accounts User',
 		'submit': 0,
@@ -99,7 +97,6 @@
 		'cancel': 0,
 		'create': 0,
 		'doctype': 'DocPerm',
-		'idx': 4,
 		'permlevel': 0,
 		'role': 'Accounts User',
 		'submit': 0,
@@ -111,7 +108,6 @@
 		'cancel': 1,
 		'create': 1,
 		'doctype': 'DocPerm',
-		'idx': 5,
 		'permlevel': 0,
 		'role': 'System Manager',
 		'write': 1
@@ -120,7 +116,6 @@
 	# DocPerm
 	{
 		'doctype': 'DocPerm',
-		'idx': 6,
 		'permlevel': 1,
 		'role': 'All'
 	},
@@ -130,7 +125,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'trash_reason',
 		'fieldtype': 'Small Text',
-		'idx': 1,
 		'label': 'Trash Reason',
 		'oldfieldname': 'trash_reason',
 		'oldfieldtype': 'Small Text',
@@ -142,7 +136,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'cost_center_name',
 		'fieldtype': 'Data',
-		'idx': 2,
 		'in_filter': 0,
 		'label': 'Cost Center Name',
 		'no_copy': 1,
@@ -159,7 +152,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'parent_cost_center',
 		'fieldtype': 'Link',
-		'idx': 3,
 		'label': 'Parent Cost Center',
 		'oldfieldname': 'parent_cost_center',
 		'oldfieldtype': 'Link',
@@ -175,14 +167,12 @@
 		'doctype': 'DocField',
 		'fieldname': 'company_name',
 		'fieldtype': 'Link',
-		'idx': 4,
 		'label': 'Company',
 		'oldfieldname': 'company_name',
 		'oldfieldtype': 'Link',
 		'options': 'Company',
 		'permlevel': 0,
 		'reqd': 1,
-		'search_index': 0,
 		'trigger': 'Client'
 	},
 
@@ -191,7 +181,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'company_abbr',
 		'fieldtype': 'Data',
-		'idx': 5,
 		'label': 'Company Abbr',
 		'oldfieldname': 'company_abbr',
 		'oldfieldtype': 'Data',
@@ -204,8 +193,7 @@
 		'doctype': 'DocField',
 		'fieldname': 'group_or_ledger',
 		'fieldtype': 'Select',
-		'hidden': 1,
-		'idx': 6,
+		'hidden': 0,
 		'label': 'Group or Ledger',
 		'no_copy': 1,
 		'oldfieldname': 'group_or_ledger',
@@ -223,13 +211,11 @@
 		'doctype': 'DocField',
 		'fieldname': 'distribution_id',
 		'fieldtype': 'Link',
-		'idx': 7,
 		'label': 'Distribution Id',
 		'oldfieldname': 'distribution_id',
 		'oldfieldtype': 'Link',
 		'options': 'Budget Distribution',
-		'permlevel': 0,
-		'search_index': 0
+		'permlevel': 0
 	},
 
 	# DocField
@@ -237,7 +223,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'budget_details',
 		'fieldtype': 'Table',
-		'idx': 8,
 		'label': 'Budget Details',
 		'oldfieldname': 'budget_details',
 		'oldfieldtype': 'Table',
@@ -251,7 +236,6 @@
 		'fieldname': 'lft',
 		'fieldtype': 'Int',
 		'hidden': 1,
-		'idx': 9,
 		'in_filter': 1,
 		'label': 'lft',
 		'no_copy': 1,
@@ -269,7 +253,6 @@
 		'fieldname': 'rgt',
 		'fieldtype': 'Int',
 		'hidden': 1,
-		'idx': 10,
 		'in_filter': 1,
 		'label': 'rgt',
 		'no_copy': 1,
@@ -286,13 +269,13 @@
 	{
 		'doctype': 'DocField',
 		'fieldname': 'old_parent',
-		'fieldtype': 'Data',
+		'fieldtype': 'Link',
 		'hidden': 1,
-		'idx': 11,
 		'label': 'old_parent',
 		'no_copy': 1,
 		'oldfieldname': 'old_parent',
 		'oldfieldtype': 'Data',
+		'options': 'Cost Center',
 		'permlevel': 0,
 		'print_hide': 1,
 		'report_hide': 1
diff --git a/erpnext/accounts/doctype/gl_control/gl_control.py b/erpnext/accounts/doctype/gl_control/gl_control.py
index a346b65..44a9e8d 100644
--- a/erpnext/accounts/doctype/gl_control/gl_control.py
+++ b/erpnext/accounts/doctype/gl_control/gl_control.py
@@ -191,6 +191,7 @@
 			else:
 				self.entries.append(le)
 
+
 	# Save GL Entries
 	# ----------------
 	def save_entries(self, cancel, adv_adj, update_outstanding):
@@ -200,7 +201,6 @@
 				tmp=le.debit
 				le.debit, le.credit = abs(flt(le.credit)), abs(flt(tmp))
 
-
 			le_obj = get_obj(doc=le)
 			# validate except on_cancel
 			if not cancel:
@@ -213,10 +213,12 @@
 			# update total debit / credit
 			self.td += flt(le.debit)
 			self.tc += flt(le.credit)
-
+			
+			
 	# Make Multiple Entries
 	# ---------------------
 	def make_gl_entries(self, doc, doclist, cancel=0, adv_adj = 0, use_mapper='', merge_entries = 1, update_outstanding='Yes'):
+		self.entries = []
 		# get entries
 		le_map_list = sql("select * from `tabGL Mapper Detail` where parent = %s", use_mapper or doc.doctype, as_dict=1)
 		self.td, self.tc = 0.0, 0.0
@@ -329,6 +331,7 @@
 			else:
 				msgprint("Allocation amount cannot be greater than advance amount")
 				raise Exception
+				
 
 	# Add extra row in jv detail for unadjusted amount
 	#--------------------------------------------------
@@ -351,7 +354,7 @@
 		add.against_account = cstr(jvd[0][3])
 		add.is_advance = 'Yes'
 		add.save(1)
-
+		
 	# check if advance entries are still valid
 	# ----------------------------------------
 	def validate_jv_entry(self, d, account_head, dr_or_cr):
@@ -359,15 +362,92 @@
 		# 2. check if amount is same
 		# 3. check if is_advance is 'Yes'
 		# 4. check if jv is submitted
-		ret = sql("select t2.%s from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 where t1.name = t2.parent and (t2.against_voucher = '' or t2.against_voucher is null) and (t2.against_invoice = '' or t2.against_invoice is null) and t2.account = '%s' and t1.name = '%s' and t2.name = '%s' and t2.is_advance = 'Yes' and t1.docstatus=1 and t2.%s = %s" % ( dr_or_cr, account_head, d.journal_voucher, d.jv_detail_no, dr_or_cr, d.advance_amount))
+		ret = sql("select t2.%s from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 where t1.name = t2.parent and ifnull(t2.against_voucher, '') = '' and ifnull(t2.against_invoice, '') = '' and t2.account = '%s' and t1.name = '%s' and t2.name = '%s' and t2.is_advance = 'Yes' and t1.docstatus=1 and t2.%s = %s" % (dr_or_cr, account_head, d.journal_voucher, d.jv_detail_no, dr_or_cr, d.advance_amount))
 		if (not ret):
 			msgprint("Please click on 'Get Advances Paid' button as the advance entries have been changed.")
 			raise Exception
 		return
 
-##############################################################################
-# Repair Outstanding Amount
-##############################################################################
+
+######################################################################################################################
+
+	#------------------------------------------
+	def reconcile_against_document(self, args):
+		"""
+			Cancel JV, Update aginst document, split if required and resubmit jv
+		"""
+		
+		for d in args:
+			self.check_if_jv_modified(d)
+
+			against_fld = {
+				'Journal Voucher' : 'against_jv',
+				'Receivable Voucher' : 'against_invoice',
+				'Payable Voucher' : 'against_voucher'
+			}
+			
+			d['against_fld'] = against_fld[d['against_voucher_type']]
+
+			# cancel JV
+			jv_obj = get_obj('Journal Voucher', d['voucher_no'], with_children=1)
+			self.make_gl_entries(jv_obj.doc, jv_obj.doclist, cancel =1, adv_adj =1)
+
+			# update ref in JV Detail
+			self.update_against_doc(d, jv_obj)
+
+			# re-submit JV
+			jv_obj = get_obj('Journal Voucher', d['voucher_no'], with_children =1)
+			self.make_gl_entries(jv_obj.doc, jv_obj.doclist, cancel = 0, adv_adj =1)
+
+	#------------------------------------------
+	def update_against_doc(self, d, jv_obj):
+		"""
+			Updates against document, if partial amount splits into rows
+		"""
+
+		sql("""
+			update `tabJournal Voucher Detail` t1, `tabJournal Voucher` t2	
+			set t1.%(dr_or_cr)s = '%(allocated_amt)s', t1.%(against_fld)s = '%(against_voucher)s', t2.modified = now() 
+			where t1.name = '%(voucher_detail_no)s' and t1.parent = t2.name""" % d)
+
+		if d['allocated_amt'] < d['unadjusted_amt']:
+			jvd = sql("select cost_center, balance, against_account, is_advance from `tabJournal Voucher Detail` where name = '%s'" % d['voucher_detail_no'])
+			# new entry with balance amount
+			ch = addchild(jv_obj.doc, 'entries', 'Journal Voucher Detail', 1)
+			ch.account = d['account']
+			ch.cost_center = cstr(jvd[0][0])
+			ch.balance = cstr(jvd[0][1])
+			ch.fields[d['dr_or_cr']] = flt(d['unadjusted_amt']) - flt(d['allocated_amt'])
+			ch.fields[d['dr_or_cr']== 'debit' and 'credit' or 'debit'] = 0
+			ch.against_account = cstr(jvd[0][2])
+			ch.is_advance = cstr(jvd[0][3])
+			ch.docstatus = 1
+			ch.save(1)
+
+	#------------------------------------------
+	def check_if_jv_modified(self, args):
+		"""
+			check if there is already a voucher reference
+			check if amount is same
+			check if jv is submitted
+		"""
+		ret = sql("""
+			select t2.%(dr_or_cr)s from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 
+			where t1.name = t2.parent and t2.account = '%(account)s' 
+			and ifnull(t2.against_voucher, '')='' and ifnull(t2.against_invoice, '')='' and ifnull(t2.against_jv, '')=''
+			and t1.name = '%(voucher_no)s' and t2.name = '%(voucher_detail_no)s'
+			and t1.docstatus=1 and t2.%(dr_or_cr)s = %(unadjusted_amt)s
+		""" % (args))
+		
+		if not ret:
+			msgprint("Payment Entry has been modified after you pulled it. Please pull it again.", raise_exception=1)
+		
+######################################################################################################################
+		
+
+
+	# Repair Outstanding Amount
+	#---------------------------------
 	def repair_voucher_outstanding(self, voucher_obj):
 		msg = []
 
diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.py b/erpnext/accounts/doctype/gl_entry/gl_entry.py
index 5d39760..cbb5b7f 100644
--- a/erpnext/accounts/doctype/gl_entry/gl_entry.py
+++ b/erpnext/accounts/doctype/gl_entry/gl_entry.py
@@ -35,7 +35,7 @@
 		if not (flt(self.doc.debit) or flt(self.doc.credit)):
 			msgprint("GL Entry: Debit or Credit amount is mandatory for %s" % self.doc.account)
 			raise Exception
-			
+
 		# Debit and credit can not done at the same time
 		if flt(self.doc.credit) != 0 and flt(self.doc.debit) != 0:
 			msgprint("Sorry you cannot credit and debit under same account head.")
@@ -185,7 +185,6 @@
 
 		bal = flt(sql("select sum(debit)-sum(credit) from `tabGL Entry` where against_voucher=%s and against_voucher_type=%s and ifnull(is_cancelled,'No') = 'No'", (self.doc.against_voucher, self.doc.against_voucher_type))[0][0] or 0.0)
 		tds = 0
-		
 		if self.doc.against_voucher_type=='Payable Voucher':
 			# amount to debit
 			bal = -bal
@@ -200,7 +199,7 @@
 			raise Exception
 			
 		# Update outstanding amt on against voucher
-		sql("update `tab%s` set outstanding_amount=%s where name='%s'"% (self.doc.against_voucher_type,bal,self.doc.against_voucher))
+		sql("update `tab%s` set outstanding_amount=%s where name='%s'" % (self.doc.against_voucher_type, bal, self.doc.against_voucher))
 		
 					
 	# Total outstanding can not be greater than credit limit for any time for any customer