coa: payment to invoice matching tool fixes
diff --git a/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.js b/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.js
index 48cb789..f902745 100644
--- a/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.js
+++ b/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.js
@@ -1,9 +1,6 @@
 // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 // License: GNU General Public License v3. See license.txt
 
-// Booking Entry Id
-// --------------------
-
 cur_frm.add_fetch("account", "company", "company")
 
 cur_frm.cscript.onload_post_render = function(doc) {
@@ -25,26 +22,15 @@
 	else {
 		return {
 			doctype: doc.voucher_type,
-			query: "accounts.doctype.payment_to_invoice_matching_tool.payment_to_invoice_matching_tool.gl_entry_details",
+			query: "erpnext.accounts.doctype.payment_to_invoice_matching_tool.payment_to_invoice_matching_tool.gl_entry_details",
 			filters: {
 				"dt": doc.voucher_type,
-				"acc": doc.account,
-				"account_type": doc.account_type 
+				"acc": doc.account
 			}
 		}		
 	}
 }
 
-cur_frm.cscript.voucher_no  =function(doc, cdt, cdn) {
+cur_frm.cscript.voucher_no  = function(doc, cdt, cdn) {
 	return get_server_fields('get_voucher_details', '', '', doc, cdt, cdn, 1)
-}
-
-cur_frm.cscript.account = function(doc, cdt, cdn) {
-	return frappe.call({
-		doc: this.frm.doc,
-		method: "set_account_type",
-		callback: function(r) {
-			if(!r.exc) refresh_field("account_type");
-		}
-	});
-}
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.py b/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.py
index e0bd61d..5b23608 100644
--- a/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.py
+++ b/erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.py
@@ -7,33 +7,24 @@
 from frappe.utils import flt
 from frappe.model.doc import addchild
 from frappe.model.bean import getlist
-from frappe import msgprint
+from frappe import msgprint, _
 
 class DocType:
 	def __init__(self, doc, doclist):
 		self.doc = doc
 		self.doclist = doclist
-	
-	def set_account_type(self):
-		self.doc.account_type = ""
-		if self.doc.account:
-			root_type = frappe.db.get_value("Account", self.doc.account, "root_type")
-			self.doc.account_type = "debit" if root_type in ["Asset", "Income"] else "credit"
 		
 	def get_voucher_details(self):
-		total_amount = frappe.db.sql("""select sum(%s) from `tabGL Entry` 
+		total_amount = frappe.db.sql("""select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) 
+			from `tabGL Entry` 
 			where voucher_type = %s and voucher_no = %s 
-			and account = %s""" % 
-			(self.doc.account_type, '%s', '%s', '%s'), 
-			(self.doc.voucher_type, self.doc.voucher_no, self.doc.account))
+			and account = %s""", (self.doc.voucher_type, self.doc.voucher_no, self.doc.account))
 			
 		total_amount = total_amount and flt(total_amount[0][0]) or 0
 		reconciled_payment = frappe.db.sql("""
-			select sum(ifnull(%s, 0)) - sum(ifnull(%s, 0)) from `tabGL Entry` where 
+			select abs(sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))) from `tabGL Entry` where 
 			against_voucher = %s and voucher_no != %s
-			and account = %s""" % 
-			((self.doc.account_type == 'debit' and 'credit' or 'debit'), self.doc.account_type, 
-			 	'%s', '%s', '%s'), (self.doc.voucher_no, self.doc.voucher_no, self.doc.account))
+			and account = %s""", (self.doc.voucher_no, self.doc.voucher_no, self.doc.account))
 			
 		reconciled_payment = reconciled_payment and flt(reconciled_payment[0][0]) or 0
 		ret = {
@@ -55,25 +46,26 @@
 
 	def get_gl_entries(self):
 		self.validate_mandatory()
-		dc = self.doc.account_type == 'debit' and 'credit' or 'debit'
 		
 		cond = self.doc.from_date and " and t1.posting_date >= '" + self.doc.from_date + "'" or ""
 		cond += self.doc.to_date and " and t1.posting_date <= '" + self.doc.to_date + "'"or ""
 		
-		cond += self.doc.amt_greater_than and \
-			' and t2.' + dc+' >= ' + self.doc.amt_greater_than or ''
-		cond += self.doc.amt_less_than and \
-			' and t2.' + dc+' <= ' + self.doc.amt_less_than or ''
+		if self.doc.amt_greater_than:
+			cond += ' and abs(ifnull(t2.debit, 0) - ifnull(t2.credit, 0)) >= ' + \
+				self.doc.amt_greater_than
+		if self.doc.amt_less_than:
+			cond += ' and abs(ifnull(t2.debit, 0) - ifnull(t2.credit, 0)) >= ' + \
+				self.doc.amt_less_than
 
 		gle = frappe.db.sql("""
 			select t1.name as voucher_no, t1.posting_date, t1.total_debit as total_amt, 
-			 	sum(ifnull(t2.credit, 0)) - sum(ifnull(t2.debit, 0)) as amt_due, t1.remark,
+			 	abs(sum(ifnull(t2.credit, 0)) - sum(ifnull(t2.debit, 0))) as amt_due, t1.remark,
 			 	t2.against_account, t2.name as voucher_detail_no
 			from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2
 			where t1.name = t2.parent and t1.docstatus = 1 and t2.account = %s
 			and ifnull(t2.against_voucher, '')='' and ifnull(t2.against_invoice, '')='' 
-			and ifnull(t2.against_jv, '')='' and t2.%s > 0 %s group by t1.name, t2.name """ % 
-			('%s', dc, cond), self.doc.account, as_dict=1)
+			and ifnull(t2.against_jv, '')='' and t1.name != %s %s group by t1.name, t2.name """ % 
+			('%s', '%s', cond), (self.doc.account, self.doc.voucher_no), as_dict=1)
 
 		return gle
 
@@ -83,8 +75,7 @@
 				'Payment to Invoice Matching Tool Detail', self.doclist)
 			ch.voucher_no = d.get('voucher_no')
 			ch.posting_date = d.get('posting_date')
-			ch.amt_due =  self.doc.account_type == 'debit' and flt(d.get('amt_due')) \
-				or -1*flt(d.get('amt_due'))
+			ch.amt_due =  flt(d.get('amt_due'))
 			ch.total_amt = flt(d.get('total_amt'))
 			ch.against_account = d.get('against_account')
 			ch.remarks = d.get('remark')
@@ -103,7 +94,7 @@
 		"""
 		if not self.doc.voucher_no or not frappe.db.sql("""select name from `tab%s` 
 				where name = %s""" % (self.doc.voucher_type, '%s'), self.doc.voucher_no):
-			msgprint("Please select valid Voucher No to proceed", raise_exception=1)
+			frappe.throw(_("Please select valid Voucher No to proceed"))
 		
 		lst = []
 		for d in getlist(self.doclist, 'ir_payment_details'):
@@ -115,7 +106,7 @@
 					'against_voucher'  : self.doc.voucher_no,
 					'account' : self.doc.account, 
 					'is_advance' : 'No', 
-					'dr_or_cr' :  self.doc.account_type=='debit' and 'credit' or 'debit', 
+					# 'dr_or_cr' :  self.doc.account_type=='debit' and 'credit' or 'debit', 
 					'unadjusted_amt' : flt(d.amt_due),
 					'allocated_amt' : flt(d.amt_to_be_reconciled)
 				}
@@ -131,15 +122,13 @@
 
 def gl_entry_details(doctype, txt, searchfield, start, page_len, filters):
 	from erpnext.controllers.queries import get_match_cond
-	
 	return frappe.db.sql("""select gle.voucher_no, gle.posting_date, 
-		gle.%(account_type)s from `tabGL Entry` gle
+		gle.debit, gle.credit from `tabGL Entry` gle
 	    where gle.account = '%(acc)s' 
 	    	and gle.voucher_type = '%(dt)s'
 			and gle.voucher_no like '%(txt)s'  
 	    	and (ifnull(gle.against_voucher, '') = '' 
 	    		or ifnull(gle.against_voucher, '') = gle.voucher_no ) 
-			and ifnull(gle.%(account_type)s, 0) > 0 
 	   		and (select ifnull(abs(sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))), 0) 
 				from `tabGL Entry` 
 	        	where account = '%(acc)s'
@@ -154,7 +143,6 @@
 	    limit %(start)s, %(page_len)s""" % {
 			"dt":filters["dt"], 
 			"acc":filters["acc"], 
-			"account_type": filters['account_type'], 
 			'mcond':get_match_cond(doctype), 
 			'txt': "%%%s%%" % txt, 
 			"start": start, 
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index 0e6851a..97c22fd 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -162,7 +162,9 @@
 	if not frappe.db.exists("Account", (party + " - " + company_details.abbr)):
 		parent_account = company_details.receivables_group \
 			if party_type=="Customer" else company_details.payables_group
-
+		if not parent_account:
+			frappe.throw(_("Please enter Account Receivable/Payable group in company master"))
+		
 		# create
 		account = frappe.bean({
 			"doctype": "Account",
@@ -172,7 +174,8 @@
 			'company': company, 
 			'master_type': party_type, 
 			'master_name': party,
-			"freeze_account": "No"
+			"freeze_account": "No",
+			"report_type": "Balance Sheet"
 		}).insert(ignore_permissions=True)
 		
 		frappe.msgprint(_("Account Created") + ": " + account.doc.name)