Merge pull request #19490 from prssanna/dashboard-fix

fix: fix dashboard with date range
diff --git a/erpnext/accounts/deferred_revenue.py b/erpnext/accounts/deferred_revenue.py
index 32485a3..62a8f05 100644
--- a/erpnext/accounts/deferred_revenue.py
+++ b/erpnext/accounts/deferred_revenue.py
@@ -174,6 +174,8 @@
 	# GL Entry for crediting the amount in the deferred expense
 	from erpnext.accounts.general_ledger import make_gl_entries
 
+	if amount == 0: return
+
 	gl_entries = []
 	gl_entries.append(
 		doc.get_gl_dict({
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js
index 11d847d..221e3a7 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.js
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js
@@ -570,7 +570,7 @@
 				},
 				{fieldtype: "Date", fieldname: "posting_date", label: __("Date"), reqd: 1,
 					default: frm.doc.posting_date},
-				{fieldtype: "Small Text", fieldname: "user_remark", label: __("User Remark"), reqd: 1},
+				{fieldtype: "Small Text", fieldname: "user_remark", label: __("User Remark")},
 				{fieldtype: "Select", fieldname: "naming_series", label: __("Series"), reqd: 1,
 					options: naming_series_options, default: naming_series_default},
 			]
diff --git a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
index ce8aba7..54464e7 100644
--- a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
+++ b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
@@ -32,8 +32,10 @@
 				})
 				invoices_summary.update({company: _summary})
 
-				paid_amount.append(invoice.paid_amount)
-				outstanding_amount.append(invoice.outstanding_amount)
+				if invoice.paid_amount:
+					paid_amount.append(invoice.paid_amount)
+				if invoice.outstanding_amount:
+					outstanding_amount.append(invoice.outstanding_amount)
 
 			if paid_amount or outstanding_amount:
 				max_count.update({
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.json b/erpnext/accounts/doctype/payment_entry/payment_entry.json
index a85eccd..acfc660 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.json
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.json
@@ -62,6 +62,7 @@
   "dimension_col_break",
   "cost_center",
   "section_break_12",
+  "status",
   "remarks",
   "column_break_16",
   "letter_head",
@@ -563,10 +564,18 @@
   {
    "fieldname": "dimension_col_break",
    "fieldtype": "Column Break"
+  },
+  {
+   "default": "Draft",
+   "fieldname": "status",
+   "fieldtype": "Select",
+   "label": "Status",
+   "options": "\nDraft\nSubmitted\nCancelled",
+   "read_only": 1
   }
  ],
  "is_submittable": 1,
- "modified": "2019-05-27 15:53:21.108857",
+ "modified": "2019-11-06 12:59:43.151721",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Payment Entry",
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 89aaffb..bf7e833 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -61,6 +61,7 @@
 		self.validate_duplicate_entry()
 		self.validate_allocated_amount()
 		self.ensure_supplier_is_not_blocked()
+		self.set_status()
 
 	def on_submit(self):
 		self.setup_party_account_field()
@@ -70,6 +71,7 @@
 		self.update_outstanding_amounts()
 		self.update_advance_paid()
 		self.update_expense_claim()
+		self.set_status()
 
 
 	def on_cancel(self):
@@ -79,6 +81,7 @@
 		self.update_advance_paid()
 		self.update_expense_claim()
 		self.delink_advance_entry_references()
+		self.set_status()
 
 	def update_outstanding_amounts(self):
 		self.set_missing_ref_details(force=True)
@@ -275,6 +278,14 @@
 						frappe.throw(_("Against Journal Entry {0} does not have any unmatched {1} entry")
 							.format(d.reference_name, dr_or_cr))
 
+	def set_status(self):
+		if self.docstatus == 2:
+			self.status = 'Cancelled'
+		elif self.docstatus == 1:
+			self.status = 'Submitted'
+		else:
+			self.status = 'Draft'
+
 	def set_amounts(self):
 		self.set_amounts_in_company_currency()
 		self.set_total_allocated_amount()
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.js b/erpnext/accounts/report/accounts_receivable/accounts_receivable.js
index 228be18..9b4dda2 100644
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.js
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.js
@@ -79,13 +79,20 @@
 			"options": "Customer",
 			on_change: () => {
 				var customer = frappe.query_report.get_filter_value('customer');
+				var company = frappe.query_report.get_filter_value('company');
 				if (customer) {
-					frappe.db.get_value('Customer', customer, ["tax_id", "customer_name", "credit_limit", "payment_terms"], function(value) {
+					frappe.db.get_value('Customer', customer, ["tax_id", "customer_name", "payment_terms"], function(value) {
 						frappe.query_report.set_filter_value('tax_id', value["tax_id"]);
 						frappe.query_report.set_filter_value('customer_name', value["customer_name"]);
-						frappe.query_report.set_filter_value('credit_limit', value["credit_limit"]);
 						frappe.query_report.set_filter_value('payment_terms', value["payment_terms"]);
 					});
+
+					frappe.db.get_value('Customer Credit Limit', {'parent': customer, 'company': company}, 
+						["credit_limit"], function(value) {
+						if (value) {
+							frappe.query_report.set_filter_value('credit_limit', value["credit_limit"]);
+						}
+					}, "Customer");
 				} else {
 					frappe.query_report.set_filter_value('tax_id', "");
 					frappe.query_report.set_filter_value('customer_name', "");
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.js b/erpnext/hr/doctype/salary_structure/salary_structure.js
index d56320a..dd34ef2 100755
--- a/erpnext/hr/doctype/salary_structure/salary_structure.js
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.js
@@ -46,10 +46,12 @@
 		frm.trigger("toggle_fields");
 		frm.fields_dict['earnings'].grid.set_column_disp("default_amount", false);
 		frm.fields_dict['deductions'].grid.set_column_disp("default_amount", false);
-
-		frm.add_custom_button(__("Preview Salary Slip"), function() {
-			frm.trigger('preview_salary_slip');
-		});
+		
+		if(frm.doc.docstatus === 1) {
+			frm.add_custom_button(__("Preview Salary Slip"), function() {
+				frm.trigger('preview_salary_slip');
+			});
+		}
 
 		if(frm.doc.docstatus==1) {
 			frm.add_custom_button(__("Assign Salary Structure"), function() {
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.py b/erpnext/hr/doctype/salary_structure/salary_structure.py
index f7d712d..0e1a74f 100644
--- a/erpnext/hr/doctype/salary_structure/salary_structure.py
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.py
@@ -169,5 +169,10 @@
 @frappe.whitelist()
 def get_employees(salary_structure):
 	employees = frappe.get_list('Salary Structure Assignment',
-		filters={'salary_structure': salary_structure}, fields=['employee'])
+		filters={'salary_structure': salary_structure, 'docstatus': 1}, fields=['employee'])
+	
+	if not employees:
+		frappe.throw(_("There's no Employee with Salary Structure: {0}. \
+			Assign {1} to an Employee to preview Salary Slip").format(salary_structure, salary_structure))
+
 	return list(set([d.employee for d in employees]))
diff --git a/erpnext/hr/report/department_analytics/department_analytics.js b/erpnext/hr/report/department_analytics/department_analytics.js
index a0b6fc7..29fedcd 100644
--- a/erpnext/hr/report/department_analytics/department_analytics.js
+++ b/erpnext/hr/report/department_analytics/department_analytics.js
@@ -2,4 +2,14 @@
 // For license information, please see license.txt
 
 frappe.query_reports["Department Analytics"] = {
+	"filters": [
+		{
+			"fieldname":"company",
+			"label": __("Company"),
+			"fieldtype": "Link",
+			"options": "Company",
+			"default": frappe.defaults.get_user_default("Company"),
+			"reqd": 1
+		},
+	]
 };
\ No newline at end of file
diff --git a/erpnext/hr/report/department_analytics/department_analytics.py b/erpnext/hr/report/department_analytics/department_analytics.py
index c4a9030..b28eac4 100644
--- a/erpnext/hr/report/department_analytics/department_analytics.py
+++ b/erpnext/hr/report/department_analytics/department_analytics.py
@@ -7,6 +7,10 @@
 
 def execute(filters=None):
 	if not filters: filters = {}
+
+	if not filters["company"]:
+		frappe.throw(_('{0} is mandatory').format(_('Company')))
+
 	columns = get_columns()
 	employees = get_employees(filters)
 	departments_result = get_department(filters)
@@ -28,6 +32,9 @@
 	conditions = ""
 	if filters.get("department"): conditions += " and department = '%s'" % \
 		filters["department"].replace("'", "\\'")
+	
+	if filters.get("company"): conditions += " and company = '%s'" % \
+		filters["company"].replace("'", "\\'")
 	return conditions
 
 def get_employees(filters):
@@ -37,7 +44,7 @@
 	gender, company from `tabEmployee` where status = 'Active' %s""" % conditions, as_list=1)
 
 def get_department(filters):
-	return frappe.db.sql("""select name from `tabDepartment`""" , as_list=1)
+	return frappe.db.sql("""select name from `tabDepartment` where company = %s""", (filters["company"]), as_list=1)
 	
 def get_chart_data(departments,employees):
 	if not departments:
diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.js b/erpnext/manufacturing/doctype/production_plan/production_plan.js
index 4b654b4..96bb0ae 100644
--- a/erpnext/manufacturing/doctype/production_plan/production_plan.js
+++ b/erpnext/manufacturing/doctype/production_plan/production_plan.js
@@ -233,7 +233,7 @@
 
 		if (item_wise_qty) {
 			for (var key in item_wise_qty) {
-				title += __('Item {0}: {1} qty produced, ', [key, item_wise_qty[key]]);
+				title += __('Item {0}: {1} qty produced. ', [key, item_wise_qty[key]]);
 			}
 		}
 
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index ee6bdff..cc00b7e 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -641,3 +641,4 @@
 erpnext.patches.v12_0.generate_leave_ledger_entries
 erpnext.patches.v12_0.set_default_shopify_app_type
 erpnext.patches.v12_0.replace_accounting_with_accounts_in_home_settings
+erpnext.patches.v12_0.set_payment_entry_status
diff --git a/erpnext/patches/v12_0/set_payment_entry_status.py b/erpnext/patches/v12_0/set_payment_entry_status.py
new file mode 100644
index 0000000..af95ed7
--- /dev/null
+++ b/erpnext/patches/v12_0/set_payment_entry_status.py
@@ -0,0 +1,8 @@
+import frappe
+
+def execute():
+	frappe.db.sql("""update `tabPayment Entry` set status = CASE
+		WHEN docstatus = 1 THEN 'Submitted'
+		WHEN docstatus = 2 THEN 'Cancelled'
+		ELSE 'Draft'
+		END;""")
\ No newline at end of file
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index 8b42b19..04e8a83 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -207,7 +207,7 @@
 				"default_expense_account": "Cost of Goods Sold"
 			})
 
-		if self.update_default_account or frappe.flags.in_test:
+		if self.update_default_account:
 			for default_account in default_accounts:
 				self._set_default_account(default_account, default_accounts.get(default_account))
 
diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py
index a4c10cf..a0ac791 100644
--- a/erpnext/shopping_cart/cart.py
+++ b/erpnext/shopping_cart/cart.py
@@ -11,6 +11,7 @@
 from frappe.utils.nestedset import get_root_of
 from erpnext.accounts.utils import get_account_name
 from erpnext.utilities.product import get_qty_in_stock
+from frappe.contacts.doctype.contact.contact import get_contact_name
 
 
 class WebsitePriceListMissingError(frappe.ValidationError):
@@ -371,7 +372,7 @@
 	if not user:
 		user = frappe.session.user
 
-	contact_name = frappe.db.get_value("Contact", {"email_id": user})
+	contact_name = get_contact_name(user)
 	party = None
 
 	if contact_name:
@@ -417,7 +418,7 @@
 		contact = frappe.new_doc("Contact")
 		contact.update({
 			"first_name": fullname,
-			"email_id": user
+			"email_ids": [{"email_id": user, "is_primary": 1}]
 		})
 		contact.append('links', dict(link_doctype='Customer', link_name=customer.name))
 		contact.flags.ignore_mandatory = True