Merge pull request #7020 from nabinhait/balance_sheet_fix

Provisional Loss related fix in Balance Sheet. Fixed #6918
diff --git a/erpnext/accounts/doctype/monthly_distribution/monthly_distribution.json b/erpnext/accounts/doctype/monthly_distribution/monthly_distribution.json
index 377a95c..7af7f48 100644
--- a/erpnext/accounts/doctype/monthly_distribution/monthly_distribution.json
+++ b/erpnext/accounts/doctype/monthly_distribution/monthly_distribution.json
@@ -9,11 +9,14 @@
  "description": "**Monthly Distribution** helps you distribute the Budget/Target across months if you have seasonality in your business.", 
  "docstatus": 0, 
  "doctype": "DocType", 
+ "editable_grid": 0, 
+ "engine": "InnoDB", 
  "fields": [
   {
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "Name of the Monthly Distribution", 
    "fieldname": "distribution_id", 
    "fieldtype": "Data", 
@@ -31,6 +34,7 @@
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 1, 
    "search_index": 0, 
@@ -41,6 +45,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "fiscal_year", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -58,6 +63,7 @@
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 1, 
@@ -68,6 +74,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "percentages", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -85,6 +92,7 @@
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
    "read_only": 0, 
+   "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -96,13 +104,14 @@
  "hide_toolbar": 0, 
  "icon": "icon-bar-chart", 
  "idx": 1, 
+ "image_view": 0, 
  "in_create": 0, 
  "in_dialog": 0, 
  "is_submittable": 0, 
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-05-16 16:35:20.349194", 
+ "modified": "2016-11-21 14:54:35.998761", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Monthly Distribution", 
@@ -119,6 +128,7 @@
    "export": 0, 
    "if_owner": 0, 
    "import": 0, 
+   "is_custom": 0, 
    "permlevel": 0, 
    "print": 1, 
    "read": 1, 
@@ -139,6 +149,7 @@
    "export": 0, 
    "if_owner": 0, 
    "import": 0, 
+   "is_custom": 0, 
    "permlevel": 2, 
    "print": 0, 
    "read": 1, 
@@ -150,7 +161,7 @@
    "write": 0
   }
  ], 
- "quick_entry": 1, 
+ "quick_entry": 0, 
  "read_only": 0, 
  "read_only_onload": 0, 
  "sort_field": "modified", 
diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py
index e5c86dc..e0a8121 100644
--- a/erpnext/accounts/doctype/sales_invoice/pos.py
+++ b/erpnext/accounts/doctype/sales_invoice/pos.py
@@ -122,7 +122,8 @@
 	return frappe.db.sql(""" 
 		select
 			name, item_code, item_name, description, item_group, expense_account, has_batch_no,
-			has_serial_no, expense_account, selling_cost_center, stock_uom, image, default_warehouse
+			has_serial_no, expense_account, selling_cost_center, stock_uom, image, 
+			default_warehouse, is_stock_item
 		from
 			tabItem
 		where
diff --git a/erpnext/accounts/page/pos/pos.js b/erpnext/accounts/page/pos/pos.js
index 26bf3ea..57ffcb1 100644
--- a/erpnext/accounts/page/pos/pos.js
+++ b/erpnext/accounts/page/pos/pos.js
@@ -1076,7 +1076,7 @@
 	},
 
 	validate_warehouse: function(){
-		if(!this.items[0].default_warehouse && !this.pos_profile_data['warehouse']){
+		if(this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']){
 			frappe.throw(__("Default warehouse is required for selected item"))
 		}
 	},
diff --git a/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.js b/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.js
index 4d2efad..7ccec30 100644
--- a/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.js
+++ b/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.js
@@ -51,6 +51,19 @@
 			"default": "Customer"
 		},
 		{
+			"fieldname":"party",
+			"label": __("Party"),
+			"fieldtype": "Dynamic Link",
+			"get_options": function() {
+				var party_type = frappe.query_report_filters_by_name.party_type.get_value();
+				var party = frappe.query_report_filters_by_name.party.get_value();
+				if(party && !party_type) {
+					frappe.throw(__("Please select Party Type first"));
+				}
+				return party_type;
+			}
+		},
+		{
 			"fieldname": "show_zero_values",
 			"label": __("Show zero values"),
 			"fieldtype": "Check"
diff --git a/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py b/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py
index 58222ac..6480623 100644
--- a/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py
+++ b/erpnext/accounts/report/trial_balance_for_party/trial_balance_for_party.py
@@ -20,13 +20,23 @@
 	
 def get_data(filters, show_party_name):
 	party_name_field = "customer_name" if filters.get("party_type")=="Customer" else "supplier_name"
-	parties = frappe.get_all(filters.get("party_type"), fields = ["name", party_name_field], order_by="name")
+	party_filters = {"name": filters.get("party")} if filters.get("party") else {}
+	parties = frappe.get_all(filters.get("party_type"), fields = ["name", party_name_field], 
+		filters = party_filters, order_by="name")
 	company_currency = frappe.db.get_value("Company", filters.company, "default_currency")
 	opening_balances = get_opening_balances(filters)
 	balances_within_period = get_balances_within_period(filters)
 	
 	data = []
-	total_debit, total_credit = 0, 0
+	# total_debit, total_credit = 0, 0
+	total_row = frappe._dict({
+		"opening_debit": 0,
+		"opening_credit": 0,
+		"debit": 0,
+		"credit": 0,
+		"closing_debit": 0,
+		"closing_credit": 0
+	})
 	for party in parties:
 		row = { "party": party.name }
 		if show_party_name:
@@ -45,11 +55,7 @@
 			"debit": debit,
 			"credit": credit
 		})
-		
-		# totals
-		total_debit += debit
-		total_credit += credit
-		
+				
 		# closing
 		closing_debit, closing_credit = toggle_debit_credit(opening_debit + debit, opening_credit + credit)
 		row.update({
@@ -57,6 +63,10 @@
 			"closing_credit": closing_credit
 		})
 		
+		# totals
+		for col in total_row:
+			total_row[col] += row.get(col)
+		
 		row.update({
 			"currency": company_currency
 		})
@@ -69,13 +79,12 @@
 			data.append(row)
 		
 	# Add total row
-	if total_debit or total_credit:
-		data.append({
-			"party": "'" + _("Totals") + "'",
-			"debit": total_debit,
-			"credit": total_credit,
-			"currency": company_currency
-		})
+	
+	total_row.update({
+		"party": "'" + _("Totals") + "'",
+		"currency": company_currency
+	})
+	data.append(total_row)
 	
 	return data
 	
diff --git a/erpnext/config/accounts.py b/erpnext/config/accounts.py
index 419d83f..b9c503a 100644
--- a/erpnext/config/accounts.py
+++ b/erpnext/config/accounts.py
@@ -373,6 +373,12 @@
 				},
 				{
 					"type": "report",
+					"name": "Profitability Analysis",
+					"doctype": "GL Entry",
+					"is_query_report": True,
+				},
+				{
+					"type": "report",
 					"name": "Payment Period Based On Invoice Date",
 					"is_query_report": True,
 					"doctype": "Journal Entry"
diff --git a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
index aa9e78c..a3d0f1b 100644
--- a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
+++ b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
@@ -486,7 +486,9 @@
 						"qty": requested_qty,
 						"schedule_date": add_days(nowdate(), cint(item_wrapper.lead_time_days)),
 						"warehouse": self.purchase_request_for_warehouse,
-						"sales_order": sales_order if sales_order!="No Sales Order" else None
+						"sales_order": sales_order if sales_order!="No Sales Order" else None,
+						"project": frappe.db.get_value("Sales Order", sales_order, "project") \
+							if sales_order!="No Sales Order" else None
 					})
 
 				material_request.flags.ignore_permissions = 1
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 5f8531d..30e8e03 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -349,3 +349,4 @@
 erpnext.patches.v7_1.update_invoice_status
 erpnext.patches.v7_0.po_status_issue_for_pr_return
 erpnext.patches.v7_1.update_missing_salary_component_type
+erpnext.patches.v7_0.update_autoname_field
\ No newline at end of file
diff --git a/erpnext/patches/v7_0/update_autoname_field.py b/erpnext/patches/v7_0/update_autoname_field.py
new file mode 100644
index 0000000..bfa9b28
--- /dev/null
+++ b/erpnext/patches/v7_0/update_autoname_field.py
@@ -0,0 +1,14 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	doctypes = frappe.db.sql(""" select name, autoname from `tabDocType`
+		where autoname like 'field:%' and allow_rename = 1""", as_dict=1)
+
+	for doctype in doctypes:
+		fieldname = doctype.autoname.split(":")[1]
+		if fieldname:
+			frappe.db.sql(""" update `tab%s` set %s = name """%(doctype.name, fieldname))
\ No newline at end of file
diff --git a/erpnext/stock/doctype/material_request/material_request.py b/erpnext/stock/doctype/material_request/material_request.py
index 06df636..4fb5f5d 100644
--- a/erpnext/stock/doctype/material_request/material_request.py
+++ b/erpnext/stock/doctype/material_request/material_request.py
@@ -372,6 +372,7 @@
 	mr= frappe.get_doc("Material Request", material_request)
 	errors =[]
 	production_orders = []
+	default_wip_warehouse = frappe.db.get_single_value("Manufacturing Settings", "default_wip_warehouse")
 	for d in mr.items:
 		if (d.qty - d.ordered_qty) >0:
 			if frappe.db.get_value("BOM", {"item": d.item_code, "is_default": 1}):
@@ -379,6 +380,7 @@
 				prod_order.production_item = d.item_code
 				prod_order.qty = d.qty - d.ordered_qty
 				prod_order.fg_warehouse = d.warehouse
+				prod_order.wip_warehouse = default_wip_warehouse
 				prod_order.description = d.description
 				prod_order.stock_uom = d.uom
 				prod_order.expected_delivery_date = d.schedule_date