[demo] [minor] Added accounts, moved JV creation to server-side
diff --git a/accounts/doctype/journal_voucher/journal_voucher.py b/accounts/doctype/journal_voucher/journal_voucher.py
index aca5aec..0fb9747 100644
--- a/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/accounts/doctype/journal_voucher/journal_voucher.py
@@ -339,11 +339,72 @@
 	account = webnotes.conn.get_value("Company", company,
 		voucher_type=="Bank Voucher" and "default_bank_account" or "default_cash_account")
 	if account:
-		return [{
+		return {
 			"account": account,
 			"balance": get_balance_on(account)
-		}]
+		}
+		
+@webnotes.whitelist()
+def get_payment_entry_from_sales_invoice(sales_invoice):
+	from accounts.utils import get_balance_on
+	si = webnotes.bean("Sales Invoice", sales_invoice)
+	jv = get_payment_entry(si.doc)
+	jv.doc.remark = 'Payment received against Sales Invoice %(name)s. %(remarks)s' % si.doc.fields
 
+	# credit customer
+	jv.doclist[1].account = si.doc.debit_to
+	jv.doclist[1].balance = get_balance_on(si.doc.debit_to)
+	jv.doclist[1].credit = si.doc.outstanding_amount
+	jv.doclist[1].against_invoice = si.doc.name
+
+	# debit bank
+	jv.doclist[2].debit = si.doc.outstanding_amount
+	
+	return [d.fields for d in jv.doclist]
+
+@webnotes.whitelist()
+def get_payment_entry_from_purchase_invoice(purchase_invoice):
+	from accounts.utils import get_balance_on
+	pi = webnotes.bean("Purchase Invoice", purchase_invoice)
+	jv = get_payment_entry(pi.doc)
+	jv.doc.remark = 'Payment against Purchase Invoice %(name)s. %(remarks)s' % pi.doc.fields
+	
+	# credit supplier
+	jv.doclist[1].account = pi.doc.credit_to
+	jv.doclist[1].balance = get_balance_on(pi.doc.credit_to)
+	jv.doclist[1].debit = pi.doc.outstanding_amount
+	jv.doclist[1].against_voucher = pi.doc.name
+
+	# credit bank
+	jv.doclist[2].credit = pi.doc.outstanding_amount
+	
+	return [d.fields for d in jv.doclist]
+
+def get_payment_entry(doc):
+	bank_account = get_default_bank_cash_account(doc.company, "Bank Voucher")
+	
+	jv = webnotes.new_bean('Journal Voucher')
+	jv.doc.voucher_type = 'Bank Voucher'
+
+	jv.doc.company = doc.company
+	jv.doc.fiscal_year = doc.fiscal_year
+
+	jv.doclist.append({
+		"doctype": "Journal Voucher Detail",
+		"parentfield": "entries"
+	})
+
+	jv.doclist.append({
+		"doctype": "Journal Voucher Detail",
+		"parentfield": "entries"
+	})
+	
+	if bank_account:
+		jv.doclist[2].account = bank_account["account"]
+		jv.doclist[2].balance = bank_account["balance"]
+	
+	return jv
+	
 @webnotes.whitelist()
 def get_opening_accounts(company):
 	"""get all balance sheet accounts for opening entry"""
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.js b/accounts/doctype/purchase_invoice/purchase_invoice.js
index b630499..2428a7d 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -106,13 +106,13 @@
 
 cur_frm.cscript.make_bank_voucher = function() {
 	return wn.call({
-		method: "accounts.doctype.journal_voucher.journal_voucher.get_default_bank_cash_account",
+		method: "accounts.doctype.journal_voucher.journal_voucher.get_payment_entry_from_purchase_invoice",
 		args: {
-			"company": cur_frm.doc.company,
-			"voucher_type": "Bank Voucher"
+			"purchase_invoice": cur_frm.doc.name,
 		},
 		callback: function(r) {
-			cur_frm.cscript.make_jv(cur_frm.doc, null, null, r.message);
+			var doclist = wn.model.sync(r.message);
+			wn.set_route("Form", doclist[0].doctype, doclist[0].name);
 		}
 	});
 }
@@ -198,31 +198,6 @@
 	refresh_field('entries');
 }
 
-cur_frm.cscript.make_jv = function(doc, dt, dn, bank_account) {
-	var jv = wn.model.make_new_doc_and_get_name('Journal Voucher');
-	jv = locals['Journal Voucher'][jv];
-	jv.voucher_type = 'Bank Voucher';
-	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;
-	jv.fiscal_year = doc.fiscal_year;
-	jv.company = doc.company;
-	
-	// debit to creditor
-	var d1 = wn.model.add_child(jv, 'Journal Voucher Detail', 'entries');
-	d1.account = doc.credit_to;
-	d1.debit = doc.outstanding_amount;
-	d1.against_voucher = doc.name;
-	
-	// credit to bank
-	var d1 = wn.model.add_child(jv, 'Journal Voucher Detail', 'entries');
-	d1.account = bank_account.account;
-	d1.credit = doc.outstanding_amount;
-	d1.balance = bank_account.balance;
-	
-	loaddoc('Journal Voucher', jv.name);
-}
-
 cur_frm.fields_dict['entries'].grid.get_field('project_name').get_query = function(doc, cdt, cdn) {
 	return{
 		filters:[
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index 516c014..bfea0bb 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -127,9 +127,9 @@
 			if not self.doc.remarks and self.doc.bill_date:
 				self.doc.remarks = (self.doc.remarks or '') + "\n" + ("Against Bill %s dated %s" 
 					% (self.doc.bill_no, formatdate(self.doc.bill_date)))
-		else:
-			if not self.doc.remarks:
-				self.doc.remarks = "No Remarks"
+
+		if not self.doc.remarks:
+			self.doc.remarks = "No Remarks"
 
 	def validate_credit_acc(self):
 		acc = sql("select debit_or_credit, is_pl_account from tabAccount where name = %s", 
diff --git a/accounts/doctype/sales_invoice/sales_invoice.js b/accounts/doctype/sales_invoice/sales_invoice.js
index b571813..0b25a68 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/accounts/doctype/sales_invoice/sales_invoice.js
@@ -255,13 +255,13 @@
 
 cur_frm.cscript.make_bank_voucher = function() {
 	return wn.call({
-		method: "accounts.doctype.journal_voucher.journal_voucher.get_default_bank_cash_account",
+		method: "accounts.doctype.journal_voucher.journal_voucher.get_payment_entry_from_sales_invoice",
 		args: {
-			"company": cur_frm.doc.company,
-			"voucher_type": "Bank Voucher"
+			"sales_invoice": cur_frm.doc.name
 		},
 		callback: function(r) {
-			cur_frm.cscript.make_jv(cur_frm.doc, null, null, r.message);
+			var doclist = wn.model.sync(r.message);
+			wn.set_route("Form", doclist[0].doctype, doclist[0].name);
 		}
 	});
 }
@@ -395,34 +395,6 @@
 	refresh_field(cur_frm.cscript.fname);
 }
 
-// Make Journal Voucher
-// --------------------
-cur_frm.cscript.make_jv = function(doc, dt, dn, bank_account) {
-	var jv = wn.model.make_new_doc_and_get_name('Journal Voucher');
-	jv = locals['Journal Voucher'][jv];
-	jv.voucher_type = 'Bank Voucher';
-
-	jv.company = doc.company;
-	jv.remark = repl('Payment received against invoice %(vn)s for %(rem)s', {vn:doc.name, rem:doc.remarks});
-	jv.fiscal_year = doc.fiscal_year;
-
-	// debit to creditor
-	var d1 = wn.model.add_child(jv, 'Journal Voucher Detail', 'entries');
-	d1.account = doc.debit_to;
-	d1.credit = doc.outstanding_amount;
-	d1.against_invoice = doc.name;
-
-
-	// credit to bank
-	var d1 = wn.model.add_child(jv, 'Journal Voucher Detail', 'entries');
-	d1.account = bank_account.account;
-	d1.debit = doc.outstanding_amount;
-	d1.balance = bank_account.balance;
-
-	loaddoc('Journal Voucher', jv.name);
-}
-
-
 cur_frm.cscript.on_submit = function(doc, cdt, cdn) {
 	if(cint(wn.boot.notification_settings.sales_invoice)) {
 		cur_frm.email_doc(wn.boot.notification_settings.sales_invoice_message);
diff --git a/utilities/make_demo.py b/utilities/make_demo.py
index d1758a0..809a12b 100644
--- a/utilities/make_demo.py
+++ b/utilities/make_demo.py
@@ -3,6 +3,7 @@
 
 import webnotes, os, datetime
 import webnotes.utils
+from webnotes.utils import random_string
 from webnotes.widgets import query_report
 import random
 import json
@@ -14,10 +15,12 @@
 # fix fiscal year
 
 company = "Wind Power LLC"
+company_abbr = "WP"
 country = "United States"
 currency = "USD"
 time_zone = "America/New York"
 start_date = '2010-01-01'
+bank_name = "Citibank"
 runs_for = 20
 prob = {
 	"default": { "make": 0.6, "qty": (1,5) },
@@ -41,6 +44,7 @@
 	make_customers_suppliers_contacts()
 	make_items()
 	make_users_and_employees()
+	make_bank_account()
 	# make_opening_stock()
 	# make_opening_accounts()
 
@@ -66,6 +70,7 @@
 		run_purchase(current_date)
 		run_manufacturing(current_date)
 		run_stock(current_date)
+		run_accounts(current_date)
 		
 def run_sales(current_date):
 	if can_make("Quotation"):
@@ -76,6 +81,52 @@
 		for i in xrange(how_many("Sales Order")):
 			make_sales_order(current_date)
 
+def run_accounts(current_date):
+	if can_make("Sales Invoice"):
+		from selling.doctype.sales_order.sales_order import make_sales_invoice
+		report = "Ordered Items to be Billed"
+		for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Sales Invoice")]:
+			si = webnotes.bean(make_sales_invoice(so))
+			si.doc.posting_date = current_date
+			si.insert()
+			si.submit()
+			webnotes.conn.commit()
+
+	if can_make("Purchase Invoice"):
+		from stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
+		report = "Received Items to be Billed"
+		for pr in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Invoice")]:
+			pi = webnotes.bean(make_purchase_invoice(pr))
+			pi.doc.posting_date = current_date
+			pi.doc.bill_no = random_string(6)
+			pi.insert()
+			pi.submit()
+			webnotes.conn.commit()
+			
+	if can_make("Payment Received"):
+		from accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_sales_invoice
+		report = "Accounts Receivable"
+		for si in list(set([r[4] for r in query_report.run(report, {"report_date": current_date })["result"] if r[3]=="Sales Invoice"]))[:how_many("Payment Received")]:
+			jv = webnotes.bean(get_payment_entry_from_sales_invoice(si))
+			jv.doc.posting_date = current_date
+			jv.doc.cheque_no = random_string(6)
+			jv.doc.cheque_date = current_date
+			jv.insert()
+			jv.submit()
+			webnotes.conn.commit()
+			
+	if can_make("Payment Made"):
+		from accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_purchase_invoice
+		report = "Accounts Payable"
+		for pi in list(set([r[4] for r in query_report.run(report, {"report_date": current_date })["result"] if r[3]=="Purchase Invoice"]))[:how_many("Payment Made")]:
+			jv = webnotes.bean(get_payment_entry_from_purchase_invoice(pi))
+			jv.doc.posting_date = current_date
+			jv.doc.cheque_no = random_string(6)
+			jv.doc.cheque_date = current_date
+			jv.insert()
+			jv.submit()
+			webnotes.conn.commit()
+
 def run_stock(current_date):
 	# make purchase requests
 	if can_make("Purchase Receipt"):
@@ -113,8 +164,6 @@
 		b.submit()
 		webnotes.conn.commit()
 	
-	
-	
 def run_purchase(current_date):
 	# make material requests for purchase items that have negative projected qtys
 	if can_make("Material Request"):
@@ -310,7 +359,7 @@
 		"fy_start": "1st Jan",
 		"industry": "Manufacturing",
 		"company_name": company,
-		"company_abbr": "WP",
+		"company_abbr": company_abbr,
 		"currency": currency,
 		"timezone": time_zone,
 		"country": country
@@ -330,7 +379,19 @@
 	webnotes.conn.commit()
 	
 	import_data(["Profile", "Employee", "Salary_Structure"])
+
+def make_bank_account():
+	ba = webnotes.bean({
+		"doctype": "Account",
+		"account_name": bank_name,
+		"account_type": "Bank or Cash",
+		"group_or_ledger": "Ledger",
+		"parent_account": "Bank Accounts - " + company_abbr,
+		"company": company
+	}).insert()
 	
+	webnotes.set_value("Company", company, "default_bank_account", ba.doc.name)
+
 def import_data(dt, submit=False):
 	if not isinstance(dt, (tuple, list)):
 		dt = [dt]