Merge pull request #1746 from nabinhait/v4-hotfix

Multiple issues
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index c551a62..b1edaf4 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -30,6 +30,7 @@
 		self.validate_mandatory()
 		self.validate_warehouse_account()
 		self.validate_frozen_accounts_modifier()
+		self.validate_balance_must_be_debit_or_credit()
 
 	def validate_master_name(self):
 		if self.master_type in ('Customer', 'Supplier') or self.account_type == "Warehouse":
@@ -69,6 +70,16 @@
 				frozen_accounts_modifier not in frappe.user.get_roles():
 					throw(_("You are not authorized to set Frozen value"))
 
+	def validate_balance_must_be_debit_or_credit(self):
+		from erpnext.accounts.utils import get_balance_on
+		if not self.get("__islocal") and self.balance_must_be:
+			account_balance = get_balance_on(self.name)
+
+			if account_balance > 0 and self.balance_must_be == "Credit":
+				frappe.throw(_("Account balance already in Debit, you are not allowed to set 'Balance Must Be' as 'Credit'"))
+			elif account_balance < 0 and self.balance_must_be == "Debit":
+				frappe.throw(_("Account balance already in Credit, you are not allowed to set 'Balance Must Be' as 'Debit'"))
+
 	def convert_group_to_ledger(self):
 		if self.check_if_child_exists():
 			throw(_("Account with child nodes cannot be converted to ledger"))
diff --git a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
index 70bee90..d75101d 100644
--- a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
@@ -410,7 +410,7 @@
 	"""get all balance sheet accounts for opening entry"""
 	from erpnext.accounts.utils import get_balance_on
 	accounts = frappe.db.sql_list("""select name from tabAccount
-		where group_or_ledger='Ledger' and report_type='Profit and Loss' and company=%s""", company)
+		where group_or_ledger='Ledger' and report_type='Balance Sheet' and company=%s""", company)
 
 	return [{"account": a, "balance": get_balance_on(a)} for a in accounts]
 
diff --git a/erpnext/accounts/page/financial_analytics/financial_analytics.js b/erpnext/accounts/page/financial_analytics/financial_analytics.js
index df30d83..52298bb 100644
--- a/erpnext/accounts/page/financial_analytics/financial_analytics.js
+++ b/erpnext/accounts/page/financial_analytics/financial_analytics.js
@@ -256,9 +256,6 @@
 				}
 			});
 			this.data.push(net_profit);
-			// $.each(me.data, function(i, v) {
-			// 	if(v.report_type=="Profit and Loss") console.log(v)
-			// })
 		}
 	},
 	add_balance: function(field, account, gl) {
diff --git a/erpnext/accounts/page/trial_balance/trial_balance.js b/erpnext/accounts/page/trial_balance/trial_balance.js
index e73e1d4..2edf822 100644
--- a/erpnext/accounts/page/trial_balance/trial_balance.js
+++ b/erpnext/accounts/page/trial_balance/trial_balance.js
@@ -29,6 +29,7 @@
 			this.with_period_closing_entry = this.wrapper
 				.find(".with_period_closing_entry input:checked").length;
 			this._super();
+			this.add_total_debit_credit();
 		},
 
 		update_balances: function(account, posting_date, v) {
@@ -42,6 +43,32 @@
 				this._super(account, posting_date, v);
 			}
 		},
+
+		add_total_debit_credit: function() {
+			var me = this;
+
+			var total_row = {
+				company: me.company,
+				id: "Total Debit / Credit",
+				name: "Total Debit / Credit",
+				indent: 0,
+				opening_dr: "NA",
+				opening_cr: "NA",
+				debit: 0,
+				credit: 0,
+				checked: false,
+			};
+			me.item_by_name[total_row.name] = total_row;
+
+			$.each(this.data, function(i, account) {
+				if((account.group_or_ledger == "Ledger") || (account.rgt - account.lft == 1)) {
+					total_row["debit"] += account.debit;
+					total_row["credit"] += account.credit;
+				}
+			});
+
+			this.data.push(total_row);
+		}
 	})
 	erpnext.trial_balance = new TrialBalance(wrapper, 'Trial Balance');
 
diff --git a/erpnext/config/buying.py b/erpnext/config/buying.py
index c8a74be..bc62519 100644
--- a/erpnext/config/buying.py
+++ b/erpnext/config/buying.py
@@ -132,12 +132,6 @@
 				{
 					"type": "report",
 					"is_query_report": True,
-					"name": "Purchase In Transit",
-					"doctype": "Purchase Order"
-				},
-				{
-					"type": "report",
-					"is_query_report": True,
 					"name": "Item-wise Purchase History",
 					"doctype": "Item"
 				},
diff --git a/erpnext/config/stock.py b/erpnext/config/stock.py
index 0f53288..ace83fc 100644
--- a/erpnext/config/stock.py
+++ b/erpnext/config/stock.py
@@ -214,12 +214,6 @@
 				{
 					"type": "report",
 					"is_query_report": True,
-					"name": "Purchase In Transit",
-					"doctype": "Purchase Order"
-				},
-				{
-					"type": "report",
-					"is_query_report": True,
 					"name": "Requested Items To Be Transferred",
 					"doctype": "Material Request"
 				},
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index ad56d8f..accaeb4 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -267,7 +267,7 @@
 		for d in self.get(raw_material_table):
 			if [d.main_item_code, d.reference_name] not in parent_items:
 				# mark for deletion from doclist
-				delete_list.append([d.main_item_code, d.reference_name])
+				delete_list.append(d)
 
 		# delete from doclist
 		if delete_list:
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index e5e9e0a..b5e66d4 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -44,3 +44,4 @@
 execute:frappe.delete_doc("Print Format", "SalesInvoice")
 execute:import frappe.defaults;frappe.defaults.clear_default("price_list_currency")
 erpnext.patches.v4_0.update_account_root_type
+execute:frappe.delete_doc("Report", "Purchase In Transit")
diff --git a/erpnext/stock/report/purchase_in_transit/__init__.py b/erpnext/stock/report/purchase_in_transit/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/erpnext/stock/report/purchase_in_transit/__init__.py
+++ /dev/null
diff --git a/erpnext/stock/report/purchase_in_transit/purchase_in_transit.json b/erpnext/stock/report/purchase_in_transit/purchase_in_transit.json
deleted file mode 100644
index 7ef63d4..0000000
--- a/erpnext/stock/report/purchase_in_transit/purchase_in_transit.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "apply_user_permissions": 1, 
- "creation": "2013-05-06 12:09:05", 
- "docstatus": 0, 
- "doctype": "Report", 
- "idx": 1, 
- "is_standard": "Yes", 
- "modified": "2014-06-03 07:18:17.234173", 
- "modified_by": "Administrator", 
- "module": "Stock", 
- "name": "Purchase In Transit", 
- "owner": "Administrator", 
- "query": "SELECT\n    pi.name as \"Purchase Invoice:Link/Purchase Invoice:120\",\n\tpi.posting_date as \"Posting Date:Date:100\",\n\tpi.credit_to as \"Supplier Account:Link/Account:120\",\n\tpi_item.item_code as \"Item Code:Link/Item:120\",\n\tpi_item.description as \"Description:Data:140\",\n\tpi_item.qty as \"Qty:Float:120\",\n\tpi_item.base_amount as \"Amount:Currency:120\",\n\tpi_item.purchase_order as \"Purchase Order:Link/Purchase Order:120\",\n\tpi_item.purchase_receipt as \"Purchase Receipt:Link/Purchase Receipt:120\",\n\tpr.posting_date as \"PR Posting Date:Date:130\",\n\tpi.company as \"Company:Link/Company:120\"\nFROM\n\t`tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item, `tabPurchase Receipt` pr\nWHERE\n\tpi.name = pi_item.parent and pi_item.purchase_receipt = pr.name\n\tand pi.docstatus = 1 and pr.posting_date > pi.posting_date\nORDER BY\n\tpi.name desc", 
- "ref_doctype": "Purchase Receipt", 
- "report_name": "Purchase In Transit", 
- "report_type": "Query Report"
-}
\ No newline at end of file