Merge pull request #25238 from alyf-de/datev_fixes

feat: Improve DATEV export
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 444b40e..db605f7 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -364,7 +364,7 @@
 		payment_terms_details = frappe.db.sql("""
 			select
 				si.name, si.party_account_currency, si.currency, si.conversion_rate,
-				ps.due_date, ps.payment_amount, ps.description, ps.paid_amount, ps.discounted_amount
+				ps.due_date, ps.payment_term, ps.payment_amount, ps.description, ps.paid_amount, ps.discounted_amount
 			from `tab{0}` si, `tabPayment Schedule` ps
 			where
 				si.name = ps.parent and
@@ -394,7 +394,7 @@
 			"due_date": d.due_date,
 			"invoiced": invoiced,
 			"invoice_grand_total": row.invoiced,
-			"payment_term": d.description,
+			"payment_term": d.description or d.payment_term,
 			"paid": d.paid_amount + d.discounted_amount,
 			"credit_note": 0.0,
 			"outstanding": invoiced - d.paid_amount - d.discounted_amount
diff --git a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js
index 0947922..1363b53 100644
--- a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js
+++ b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js
@@ -2,118 +2,128 @@
 // For license information, please see license.txt
 /* eslint-disable */
 
-frappe.query_reports["Consolidated Financial Statement"] = {
-	"filters": [
-		{
-			"fieldname":"company",
-			"label": __("Company"),
-			"fieldtype": "Link",
-			"options": "Company",
-			"default": frappe.defaults.get_user_default("Company"),
-			"reqd": 1
-		},
-		{
-			"fieldname":"filter_based_on",
-			"label": __("Filter Based On"),
-			"fieldtype": "Select",
-			"options": ["Fiscal Year", "Date Range"],
-			"default": ["Fiscal Year"],
-			"reqd": 1,
-			on_change: function() {
-				let filter_based_on = frappe.query_report.get_filter_value('filter_based_on');
-				frappe.query_report.toggle_filter_display('from_fiscal_year', filter_based_on === 'Date Range');
-				frappe.query_report.toggle_filter_display('to_fiscal_year', filter_based_on === 'Date Range');
-				frappe.query_report.toggle_filter_display('period_start_date', filter_based_on === 'Fiscal Year');
-				frappe.query_report.toggle_filter_display('period_end_date', filter_based_on === 'Fiscal Year');
+frappe.require("assets/erpnext/js/financial_statements.js", function() {
+	frappe.query_reports["Consolidated Financial Statement"] = {
+		"filters": [
+			{
+				"fieldname":"company",
+				"label": __("Company"),
+				"fieldtype": "Link",
+				"options": "Company",
+				"default": frappe.defaults.get_user_default("Company"),
+				"reqd": 1
+			},
+			{
+				"fieldname":"filter_based_on",
+				"label": __("Filter Based On"),
+				"fieldtype": "Select",
+				"options": ["Fiscal Year", "Date Range"],
+				"default": ["Fiscal Year"],
+				"reqd": 1,
+				on_change: function() {
+					let filter_based_on = frappe.query_report.get_filter_value('filter_based_on');
+					frappe.query_report.toggle_filter_display('from_fiscal_year', filter_based_on === 'Date Range');
+					frappe.query_report.toggle_filter_display('to_fiscal_year', filter_based_on === 'Date Range');
+					frappe.query_report.toggle_filter_display('period_start_date', filter_based_on === 'Fiscal Year');
+					frappe.query_report.toggle_filter_display('period_end_date', filter_based_on === 'Fiscal Year');
 
-				frappe.query_report.refresh();
+					frappe.query_report.refresh();
+				}
+			},
+			{
+				"fieldname":"period_start_date",
+				"label": __("Start Date"),
+				"fieldtype": "Date",
+				"hidden": 1,
+				"reqd": 1
+			},
+			{
+				"fieldname":"period_end_date",
+				"label": __("End Date"),
+				"fieldtype": "Date",
+				"hidden": 1,
+				"reqd": 1
+			},
+			{
+				"fieldname":"from_fiscal_year",
+				"label": __("Start Year"),
+				"fieldtype": "Link",
+				"options": "Fiscal Year",
+				"default": frappe.defaults.get_user_default("fiscal_year"),
+				"reqd": 1
+			},
+			{
+				"fieldname":"to_fiscal_year",
+				"label": __("End Year"),
+				"fieldtype": "Link",
+				"options": "Fiscal Year",
+				"default": frappe.defaults.get_user_default("fiscal_year"),
+				"reqd": 1
+			},
+			{
+				"fieldname":"finance_book",
+				"label": __("Finance Book"),
+				"fieldtype": "Link",
+				"options": "Finance Book"
+			},
+			{
+				"fieldname":"report",
+				"label": __("Report"),
+				"fieldtype": "Select",
+				"options": ["Profit and Loss Statement", "Balance Sheet", "Cash Flow"],
+				"default": "Balance Sheet",
+				"reqd": 1
+			},
+			{
+				"fieldname": "presentation_currency",
+				"label": __("Currency"),
+				"fieldtype": "Select",
+				"options": erpnext.get_presentation_currency_list(),
+				"default": frappe.defaults.get_user_default("Currency")
+			},
+			{
+				"fieldname":"accumulated_in_group_company",
+				"label": __("Accumulated Values in Group Company"),
+				"fieldtype": "Check",
+				"default": 0
+			},
+			{
+				"fieldname": "include_default_book_entries",
+				"label": __("Include Default Book Entries"),
+				"fieldtype": "Check",
+				"default": 1
 			}
-		},
-		{
-			"fieldname":"period_start_date",
-			"label": __("Start Date"),
-			"fieldtype": "Date",
-			"hidden": 1,
-			"reqd": 1
-		},
-		{
-			"fieldname":"period_end_date",
-			"label": __("End Date"),
-			"fieldtype": "Date",
-			"hidden": 1,
-			"reqd": 1
-		},
-		{
-			"fieldname":"from_fiscal_year",
-			"label": __("Start Year"),
-			"fieldtype": "Link",
-			"options": "Fiscal Year",
-			"default": frappe.defaults.get_user_default("fiscal_year"),
-			"reqd": 1
-		},
-		{
-			"fieldname":"to_fiscal_year",
-			"label": __("End Year"),
-			"fieldtype": "Link",
-			"options": "Fiscal Year",
-			"default": frappe.defaults.get_user_default("fiscal_year"),
-			"reqd": 1
-		},
-		{
-			"fieldname":"finance_book",
-			"label": __("Finance Book"),
-			"fieldtype": "Link",
-			"options": "Finance Book"
-		},
-		{
-			"fieldname":"report",
-			"label": __("Report"),
-			"fieldtype": "Select",
-			"options": ["Profit and Loss Statement", "Balance Sheet", "Cash Flow"],
-			"default": "Balance Sheet",
-			"reqd": 1
-		},
-		{
-			"fieldname": "presentation_currency",
-			"label": __("Currency"),
-			"fieldtype": "Select",
-			"options": erpnext.get_presentation_currency_list(),
-			"default": frappe.defaults.get_user_default("Currency")
-		},
-		{
-			"fieldname":"accumulated_in_group_company",
-			"label": __("Accumulated Values in Group Company"),
-			"fieldtype": "Check",
-			"default": 0
-		},
-		{
-			"fieldname": "include_default_book_entries",
-			"label": __("Include Default Book Entries"),
-			"fieldtype": "Check",
-			"default": 1
-		}
-	],
-	"formatter": function(value, row, column, data, default_formatter) {
-		value = default_formatter(value, row, column, data);
+		],
+		"formatter": function(value, row, column, data, default_formatter) {			
+			if (data && column.fieldname=="account") {
+				value = data.account_name || value;
+				
+				column.link_onclick =
+				"erpnext.financial_statements.open_general_ledger(" + JSON.stringify(data) + ")";
+				column.is_tree = true;
+			}
 
-		if (!data.parent_account) {
-			value = $(`<span>${value}</span>`);
+			value = default_formatter(value, row, column, data);
 
-			var $value = $(value).css("font-weight", "bold");
+			if (!data.parent_account) {
+				value = $(`<span>${value}</span>`);
 
-			value = $value.wrap("<p></p>").parent().html();
-		}
-		return value;
-	},
-	onload: function() {
-		let fiscal_year = frappe.defaults.get_user_default("fiscal_year")
+				var $value = $(value).css("font-weight", "bold");
 
-		frappe.model.with_doc("Fiscal Year", fiscal_year, function(r) {
-			var fy = frappe.model.get_doc("Fiscal Year", fiscal_year);
-			frappe.query_report.set_filter_value({
-				period_start_date: fy.year_start_date,
-				period_end_date: fy.year_end_date
+				value = $value.wrap("<p></p>").parent().html();
+			}
+			return value;
+		},
+		onload: function() {
+			let fiscal_year = frappe.defaults.get_user_default("fiscal_year")
+
+			frappe.model.with_doc("Fiscal Year", fiscal_year, function(r) {
+				var fy = frappe.model.get_doc("Fiscal Year", fiscal_year);
+				frappe.query_report.set_filter_value({
+					period_start_date: fy.year_start_date,
+					period_end_date: fy.year_end_date
+				});
 			});
-		});
+		}
 	}
-}
+});
\ No newline at end of file
diff --git a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py
index 0c4a422..094f5db 100644
--- a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py
+++ b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py
@@ -329,8 +329,9 @@
 		has_value = False
 		total = 0
 		row = frappe._dict({
-			"account_name": _(d.account_name),
-			"account": _(d.account_name),
+			"account_name": ('%s - %s' %(_(d.account_number), _(d.account_name))
+				if d.account_number else _(d.account_name)),
+			"account": _(d.name),
 			"parent_account": _(d.parent_account),
 			"indent": flt(d.indent),
 			"year_start_date": start_date,
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index 3fc1df7..3832415 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -470,7 +470,9 @@
 			item_tax_template = _get_item_tax_template(args, item_group_doc.taxes, out)
 			item_group = item_group_doc.parent_item_group
 
-def _get_item_tax_template(args, taxes, out={}, for_validate=False):
+def _get_item_tax_template(args, taxes, out=None, for_validate=False):
+	if out is None:
+		out = {}
 	taxes_with_validity = []
 	taxes_with_no_validity = []
 
@@ -935,8 +937,8 @@
 def get_company_total_stock(item_code, company):
 	return frappe.db.sql("""SELECT sum(actual_qty) from 
 		(`tabBin` INNER JOIN `tabWarehouse` ON `tabBin`.warehouse = `tabWarehouse`.name) 
-		WHERE `tabWarehouse`.company = '{0}' and `tabBin`.item_code = '{1}'"""
-		.format(company, item_code))[0][0]
+		WHERE `tabWarehouse`.company = %s and `tabBin`.item_code = %s""",
+		(company, item_code))[0][0]
 
 @frappe.whitelist()
 def get_serial_no_details(item_code, warehouse, stock_qty, serial_no):