From d6f6cccede7f78e4bee4563619e65f6cd6e5f938 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Thu, 28 Dec 2023 22:15:55 +0100 Subject: [PATCH 1/9] feat: new report "Summen- und Saldenliste" --- .../summen__und_saldenliste/__init__.py | 0 .../summen__und_saldenliste.js | 45 +++++++ .../summen__und_saldenliste.json | 33 +++++ .../summen__und_saldenliste.py | 125 ++++++++++++++++++ erpnext_germany/translations/de.csv | 4 + 5 files changed, 207 insertions(+) create mode 100644 erpnext_germany/erpnext_germany/report/summen__und_saldenliste/__init__.py create mode 100644 erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js create mode 100644 erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.json create mode 100644 erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/__init__.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js new file mode 100644 index 00000000..bcb0e489 --- /dev/null +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js @@ -0,0 +1,45 @@ +// Copyright (c) 2023, ALYF GmbH and contributors +// For license information, please see license.txt +/* eslint-disable */ + +frappe.query_reports["Summen- und Saldenliste"] = { + filters: [ + { + fieldname: "company", + label: __("Company"), + fieldtype: "Link", + options: "Company", + default: frappe.defaults.get_user_default("Company"), + reqd: 1, + }, + { + fieldname: "fiscal_year", + label: __("Fiscal Year"), + fieldtype: "Link", + options: "Fiscal Year", + default: erpnext.utils.get_fiscal_year(), + reqd: 1, + }, + { + fieldname: "month", + label: __("Month"), + fieldtype: "Select", + default: (new Date()).getMonth(), + options: [ + { value: 1, label: __("January") }, + { value: 2, label: __("February") }, + { value: 3, label: __("March") }, + { value: 4, label: __("April") }, + { value: 5, label: __("May") }, + { value: 6, label: __("June") }, + { value: 7, label: __("July") }, + { value: 8, label: __("August") }, + { value: 9, label: __("September") }, + { value: 10, label: __("October") }, + { value: 11, label: __("November") }, + { value: 12, label: __("December") }, + ], + reqd: 1, + } + ], +}; diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.json b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.json new file mode 100644 index 00000000..f65c825a --- /dev/null +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.json @@ -0,0 +1,33 @@ +{ + "add_total_row": 0, + "columns": [], + "creation": "2023-12-28 20:21:45.244186", + "disable_prepared_report": 1, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "filters": [], + "idx": 0, + "is_standard": "Yes", + "letter_head": "ALYF GmbH", + "modified": "2023-12-28 20:21:50.528134", + "modified_by": "Administrator", + "module": "ERPNext Germany", + "name": "Summen- und Saldenliste", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "GL Entry", + "report_name": "Summen- und Saldenliste", + "report_type": "Script Report", + "roles": [ + { + "role": "Accounts User" + }, + { + "role": "Accounts Manager" + }, + { + "role": "Auditor" + } + ] +} \ No newline at end of file diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py new file mode 100644 index 00000000..dd3d147c --- /dev/null +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py @@ -0,0 +1,125 @@ +# Copyright (c) 2023, ALYF GmbH and contributors +# For license information, please see license.txt + +from datetime import date +from calendar import monthrange +from babel.dates import format_date + +import frappe +from frappe import _ +from frappe.query_builder.functions import Sum + + +def execute(filters=None): + fy_start, month_start, month_end = get_dates(int(filters.month), filters.fiscal_year) + current_month_name = format_date(month_start, format="MMMM", locale=frappe.local.lang) + return get_columns(current_month_name), get_data(filters.company, fy_start, month_start, month_end) + + +def get_columns(current_month_name: str): + return [ + { + "fieldname": "account", + "label": _("Account"), + "fieldtype": "Link", + "options": "Account", + "width": 400, + }, + { + "fieldname": "account_currency", + "label": _("Currency"), + "fieldtype": "Link", + "options": "Currency", + "width": 100, + }, + { + "fieldname": "debit_until_evaluation_period", + "label": _("Debit until {0}").format(current_month_name), + "fieldtype": "Currency", + "width": 200, + "options": "account_currency", + }, + { + "fieldname": "credit_until_evaluation_period", + "label": _("Credit until {0}").format(current_month_name), + "fieldtype": "Currency", + "width": 200, + "options": "account_currency", + }, + { + "fieldname": "debit_in_evaluation_period", + "label": _("Debit in {0}").format(current_month_name), + "fieldtype": "Currency", + "width": 200, + "options": "account_currency", + }, + { + "fieldname": "credit_in_evaluation_period", + "label": _("Credit in {0}").format(current_month_name), + "fieldtype": "Currency", + "width": 200, + "options": "account_currency", + }, + ] + + +def get_data(company: str, fy_start, month_start, month_end): + gl_entry = frappe.qb.DocType("GL Entry") + + sum_until_month = frappe.qb.from_(gl_entry).select( + gl_entry.account, + gl_entry.account_currency, + Sum(gl_entry.debit).as_("debit_until_evaluation_period"), + Sum(gl_entry.credit).as_("credit_until_evaluation_period"), + ).where( + (gl_entry.company == company) + & (gl_entry.is_cancelled == 0) + & (gl_entry.posting_date >= fy_start) + & (gl_entry.posting_date < month_start) + & (gl_entry.voucher_type != "Period Closing Voucher") + ).orderby( + gl_entry.account + ).groupby( + gl_entry.account, gl_entry.account_currency + ) + + sum_in_month = frappe.qb.from_(gl_entry).select( + gl_entry.account, + gl_entry.account_currency, + Sum(gl_entry.debit).as_("debit_in_evaluation_period"), + Sum(gl_entry.credit).as_("credit_in_evaluation_period"), + ).where( + (gl_entry.company == company) + & (gl_entry.is_cancelled == 0) + & (gl_entry.posting_date >= month_start) + & (gl_entry.posting_date <= month_end) + & (gl_entry.voucher_type != "Period Closing Voucher") + ).orderby( + gl_entry.account + ).groupby( + gl_entry.account, gl_entry.account_currency + ) + + query = frappe.qb.from_(sum_in_month).left_join( + sum_until_month + ).on( + sum_until_month.account == sum_in_month.account + ).select( + sum_in_month.account, + sum_in_month.account_currency, + sum_until_month.debit_until_evaluation_period, + sum_until_month.credit_until_evaluation_period, + sum_in_month.debit_in_evaluation_period, + sum_in_month.credit_in_evaluation_period, + ) + + return query.run() + + +def get_dates(month: int, fiscal_year: str): + """Returns the start and end date for the given month.""" + fy_start: date = frappe.db.get_value("Fiscal Year", fiscal_year, "year_start_date") + month_start = fy_start.replace(month=month, day=1) + last_day_of_month = monthrange(month_start.year, month_start.month)[1] + month_end = month_start.replace(day=last_day_of_month) + return fy_start, month_start, month_end diff --git a/erpnext_germany/translations/de.csv b/erpnext_germany/translations/de.csv index 841f20b5..4063036c 100644 --- a/erpnext_germany/translations/de.csv +++ b/erpnext_germany/translations/de.csv @@ -43,3 +43,7 @@ Invalid Input,Ungültige Eingabe Error,Fehler Cannot delete this transaction,Diese Transaktion kann nicht gelöscht werden Only the most recent {0} can be deleted in order to avoid gaps in numbering.,"Nur die neueste Transaktion vom Typ {0} kann gelöscht werden, um Lücken in der Nummerierung zu vermeiden." +Debit until {0},Soll bis {0}, +Credit until {0},Haben bis {0}, +Debit in {0},Soll im {0}, +Credit in {0},Haben im {0}, From b7abe75d42d63812ad4e835ecbe4685cb8aae3c4 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Thu, 28 Dec 2023 22:19:39 +0100 Subject: [PATCH 2/9] docs: add "Summen- und Saldenliste" to list of features --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index cb888a97..2fa5cd9e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ App to hold regional code for Germany, built on top of ERPNext. ### Features +- German accounting reports + + - _Summen- und Saldenliste_ + - Section for Register Information (Registerart, -gericht und nummer) in **Company**, **Customer** and **Supplier** ![Section with Register Information](docs/register_information.png) From 30d674145fdcf1db69265499f1c0d4e22f4a1162 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Thu, 28 Dec 2023 22:35:19 +0100 Subject: [PATCH 3/9] fix: order by account number --- .../summen__und_saldenliste.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py index dd3d147c..f38cbc28 100644 --- a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py @@ -7,7 +7,7 @@ import frappe from frappe import _ -from frappe.query_builder.functions import Sum +from frappe.query_builder.functions import Sum, Cast def execute(filters=None): @@ -65,6 +65,7 @@ def get_columns(current_month_name: str): def get_data(company: str, fy_start, month_start, month_end): gl_entry = frappe.qb.DocType("GL Entry") + account = frappe.qb.DocType("Account") sum_until_month = frappe.qb.from_(gl_entry).select( gl_entry.account, @@ -77,13 +78,15 @@ def get_data(company: str, fy_start, month_start, month_end): & (gl_entry.posting_date >= fy_start) & (gl_entry.posting_date < month_start) & (gl_entry.voucher_type != "Period Closing Voucher") - ).orderby( - gl_entry.account ).groupby( gl_entry.account, gl_entry.account_currency ) - sum_in_month = frappe.qb.from_(gl_entry).select( + sum_in_month = frappe.qb.from_(gl_entry).left_join( + account + ).on( + gl_entry.account == account.name + ).select( gl_entry.account, gl_entry.account_currency, Sum(gl_entry.debit).as_("debit_in_evaluation_period"), @@ -95,7 +98,7 @@ def get_data(company: str, fy_start, month_start, month_end): & (gl_entry.posting_date <= month_end) & (gl_entry.voucher_type != "Period Closing Voucher") ).orderby( - gl_entry.account + Cast(account.account_number, "int") ).groupby( gl_entry.account, gl_entry.account_currency ) From 0dc3a23b86252388f5b77c805a9db5a69f533193 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Thu, 28 Dec 2023 22:46:34 +0100 Subject: [PATCH 4/9] fix: use debit/credit in account currency --- .../summen__und_saldenliste/summen__und_saldenliste.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py index f38cbc28..118e94db 100644 --- a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py @@ -70,8 +70,8 @@ def get_data(company: str, fy_start, month_start, month_end): sum_until_month = frappe.qb.from_(gl_entry).select( gl_entry.account, gl_entry.account_currency, - Sum(gl_entry.debit).as_("debit_until_evaluation_period"), - Sum(gl_entry.credit).as_("credit_until_evaluation_period"), + Sum(gl_entry.debit_in_account_currency).as_("debit_until_evaluation_period"), + Sum(gl_entry.credit_in_account_currency).as_("credit_until_evaluation_period"), ).where( (gl_entry.company == company) & (gl_entry.is_cancelled == 0) @@ -89,8 +89,8 @@ def get_data(company: str, fy_start, month_start, month_end): ).select( gl_entry.account, gl_entry.account_currency, - Sum(gl_entry.debit).as_("debit_in_evaluation_period"), - Sum(gl_entry.credit).as_("credit_in_evaluation_period"), + Sum(gl_entry.debit_in_account_currency).as_("debit_in_evaluation_period"), + Sum(gl_entry.credit_in_account_currency).as_("credit_in_evaluation_period"), ).where( (gl_entry.company == company) & (gl_entry.is_cancelled == 0) From 7179742f24354a8f2b0704387e29086b43cbc534 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Thu, 28 Dec 2023 22:59:49 +0100 Subject: [PATCH 5/9] style: format with black --- .../summen__und_saldenliste.py | 106 ++++++++++-------- 1 file changed, 60 insertions(+), 46 deletions(-) diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py index 118e94db..00742a27 100644 --- a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py @@ -11,9 +11,15 @@ def execute(filters=None): - fy_start, month_start, month_end = get_dates(int(filters.month), filters.fiscal_year) - current_month_name = format_date(month_start, format="MMMM", locale=frappe.local.lang) - return get_columns(current_month_name), get_data(filters.company, fy_start, month_start, month_end) + fy_start, month_start, month_end = get_dates( + int(filters.month), filters.fiscal_year + ) + current_month_name = format_date( + month_start, format="MMMM", locale=frappe.local.lang + ) + return get_columns(current_month_name), get_data( + filters.company, fy_start, month_start, month_end + ) def get_columns(current_month_name: str): @@ -67,53 +73,61 @@ def get_data(company: str, fy_start, month_start, month_end): gl_entry = frappe.qb.DocType("GL Entry") account = frappe.qb.DocType("Account") - sum_until_month = frappe.qb.from_(gl_entry).select( - gl_entry.account, - gl_entry.account_currency, - Sum(gl_entry.debit_in_account_currency).as_("debit_until_evaluation_period"), - Sum(gl_entry.credit_in_account_currency).as_("credit_until_evaluation_period"), - ).where( - (gl_entry.company == company) - & (gl_entry.is_cancelled == 0) - & (gl_entry.posting_date >= fy_start) - & (gl_entry.posting_date < month_start) - & (gl_entry.voucher_type != "Period Closing Voucher") - ).groupby( - gl_entry.account, gl_entry.account_currency + sum_until_month = ( + frappe.qb.from_(gl_entry) + .select( + gl_entry.account, + gl_entry.account_currency, + Sum(gl_entry.debit_in_account_currency).as_( + "debit_until_evaluation_period" + ), + Sum(gl_entry.credit_in_account_currency).as_( + "credit_until_evaluation_period" + ), + ) + .where( + (gl_entry.company == company) + & (gl_entry.is_cancelled == 0) + & (gl_entry.posting_date >= fy_start) + & (gl_entry.posting_date < month_start) + & (gl_entry.voucher_type != "Period Closing Voucher") + ) + .groupby(gl_entry.account, gl_entry.account_currency) ) - sum_in_month = frappe.qb.from_(gl_entry).left_join( - account - ).on( - gl_entry.account == account.name - ).select( - gl_entry.account, - gl_entry.account_currency, - Sum(gl_entry.debit_in_account_currency).as_("debit_in_evaluation_period"), - Sum(gl_entry.credit_in_account_currency).as_("credit_in_evaluation_period"), - ).where( - (gl_entry.company == company) - & (gl_entry.is_cancelled == 0) - & (gl_entry.posting_date >= month_start) - & (gl_entry.posting_date <= month_end) - & (gl_entry.voucher_type != "Period Closing Voucher") - ).orderby( - Cast(account.account_number, "int") - ).groupby( - gl_entry.account, gl_entry.account_currency + sum_in_month = ( + frappe.qb.from_(gl_entry) + .left_join(account) + .on(gl_entry.account == account.name) + .select( + gl_entry.account, + gl_entry.account_currency, + Sum(gl_entry.debit_in_account_currency).as_("debit_in_evaluation_period"), + Sum(gl_entry.credit_in_account_currency).as_("credit_in_evaluation_period"), + ) + .where( + (gl_entry.company == company) + & (gl_entry.is_cancelled == 0) + & (gl_entry.posting_date >= month_start) + & (gl_entry.posting_date <= month_end) + & (gl_entry.voucher_type != "Period Closing Voucher") + ) + .orderby(Cast(account.account_number, "int")) + .groupby(gl_entry.account, gl_entry.account_currency) ) - query = frappe.qb.from_(sum_in_month).left_join( - sum_until_month - ).on( - sum_until_month.account == sum_in_month.account - ).select( - sum_in_month.account, - sum_in_month.account_currency, - sum_until_month.debit_until_evaluation_period, - sum_until_month.credit_until_evaluation_period, - sum_in_month.debit_in_evaluation_period, - sum_in_month.credit_in_evaluation_period, + query = ( + frappe.qb.from_(sum_in_month) + .left_join(sum_until_month) + .on(sum_until_month.account == sum_in_month.account) + .select( + sum_in_month.account, + sum_in_month.account_currency, + sum_until_month.debit_until_evaluation_period, + sum_until_month.credit_until_evaluation_period, + sum_in_month.debit_in_evaluation_period, + sum_in_month.credit_in_evaluation_period, + ) ) return query.run() From 18e0ac74b739290b3edf0bed2f0eb98f17e9548f Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Thu, 28 Dec 2023 23:00:14 +0100 Subject: [PATCH 6/9] fix: better way to get the previous month --- .../summen__und_saldenliste.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js index bcb0e489..31fe8386 100644 --- a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.js @@ -24,7 +24,7 @@ frappe.query_reports["Summen- und Saldenliste"] = { fieldname: "month", label: __("Month"), fieldtype: "Select", - default: (new Date()).getMonth(), + default: get_previous_month(), options: [ { value: 1, label: __("January") }, { value: 2, label: __("February") }, @@ -40,6 +40,13 @@ frappe.query_reports["Summen- und Saldenliste"] = { { value: 12, label: __("December") }, ], reqd: 1, - } + }, ], }; + +function get_previous_month() { + /* Return the 1-indexed number of the previous month */ + const date = new Date(); + date.setDate(0); // set to last day of previous month + return date.getMonth() + 1; // getMonth() is 0-indexed +} From 482116e08edf992abd00ef36e90923630c08ff29 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:14:45 +0100 Subject: [PATCH 7/9] fix: increase column width --- .../summen__und_saldenliste/summen__und_saldenliste.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py index 00742a27..292e1e63 100644 --- a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py @@ -42,28 +42,28 @@ def get_columns(current_month_name: str): "fieldname": "debit_until_evaluation_period", "label": _("Debit until {0}").format(current_month_name), "fieldtype": "Currency", - "width": 200, + "width": 170, "options": "account_currency", }, { "fieldname": "credit_until_evaluation_period", "label": _("Credit until {0}").format(current_month_name), "fieldtype": "Currency", - "width": 200, + "width": 170, "options": "account_currency", }, { "fieldname": "debit_in_evaluation_period", "label": _("Debit in {0}").format(current_month_name), "fieldtype": "Currency", - "width": 200, + "width": 170, "options": "account_currency", }, { "fieldname": "credit_in_evaluation_period", "label": _("Credit in {0}").format(current_month_name), "fieldtype": "Currency", - "width": 200, + "width": 170, "options": "account_currency", }, ] From 2d01a6adcf9a755f3a4d26c2781e78709ded4328 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:17:01 +0100 Subject: [PATCH 8/9] refactor: simplify column names --- .../summen__und_saldenliste.py | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py index 292e1e63..c2804dab 100644 --- a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py @@ -77,13 +77,8 @@ def get_data(company: str, fy_start, month_start, month_end): frappe.qb.from_(gl_entry) .select( gl_entry.account, - gl_entry.account_currency, - Sum(gl_entry.debit_in_account_currency).as_( - "debit_until_evaluation_period" - ), - Sum(gl_entry.credit_in_account_currency).as_( - "credit_until_evaluation_period" - ), + Sum(gl_entry.debit_in_account_currency).as_("debit"), + Sum(gl_entry.credit_in_account_currency).as_("credit"), ) .where( (gl_entry.company == company) @@ -92,7 +87,7 @@ def get_data(company: str, fy_start, month_start, month_end): & (gl_entry.posting_date < month_start) & (gl_entry.voucher_type != "Period Closing Voucher") ) - .groupby(gl_entry.account, gl_entry.account_currency) + .groupby(gl_entry.account) ) sum_in_month = ( @@ -102,8 +97,8 @@ def get_data(company: str, fy_start, month_start, month_end): .select( gl_entry.account, gl_entry.account_currency, - Sum(gl_entry.debit_in_account_currency).as_("debit_in_evaluation_period"), - Sum(gl_entry.credit_in_account_currency).as_("credit_in_evaluation_period"), + Sum(gl_entry.debit_in_account_currency).as_("debit"), + Sum(gl_entry.credit_in_account_currency).as_("credit"), ) .where( (gl_entry.company == company) @@ -123,10 +118,10 @@ def get_data(company: str, fy_start, month_start, month_end): .select( sum_in_month.account, sum_in_month.account_currency, - sum_until_month.debit_until_evaluation_period, - sum_until_month.credit_until_evaluation_period, - sum_in_month.debit_in_evaluation_period, - sum_in_month.credit_in_evaluation_period, + sum_until_month.debit, + sum_until_month.credit, + sum_in_month.debit, + sum_in_month.credit, ) ) From 2d35592e55fe24e004e3f997343538421b836ccb Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:17:12 +0100 Subject: [PATCH 9/9] feat: add opening balance --- .../summen__und_saldenliste.py | 50 +++++++++++++++++++ erpnext_germany/translations/de.csv | 2 + 2 files changed, 52 insertions(+) diff --git a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py index c2804dab..937aac42 100644 --- a/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py +++ b/erpnext_germany/erpnext_germany/report/summen__und_saldenliste/summen__und_saldenliste.py @@ -8,6 +8,7 @@ import frappe from frappe import _ from frappe.query_builder.functions import Sum, Cast +from pypika.terms import Case def execute(filters=None): @@ -38,6 +39,20 @@ def get_columns(current_month_name: str): "options": "Currency", "width": 100, }, + { + "fieldname": "debit_opening_balance", + "label": _("Debit Opening Balance"), + "fieldtype": "Currency", + "width": 170, + "options": "account_currency", + }, + { + "fieldname": "credit_opening_balance", + "label": _("Credit Opening Balance"), + "fieldtype": "Currency", + "width": 170, + "options": "account_currency", + }, { "fieldname": "debit_until_evaluation_period", "label": _("Debit until {0}").format(current_month_name), @@ -73,6 +88,37 @@ def get_data(company: str, fy_start, month_start, month_end): gl_entry = frappe.qb.DocType("GL Entry") account = frappe.qb.DocType("Account") + opening_balance = ( + frappe.qb.from_(gl_entry) + .left_join(account) + .on(gl_entry.account == account.name) + .select( + gl_entry.account, + Case() + .when( + account.root_type == "Asset", + Sum(gl_entry.debit_in_account_currency) + - Sum(gl_entry.credit_in_account_currency), + ) + .else_(None) + .as_("debit"), + Case() + .when( + account.root_type.isin(("Liability", "Equity")), + Sum(gl_entry.credit_in_account_currency) + - Sum(gl_entry.debit_in_account_currency), + ) + .else_(None) + .as_("credit"), + ) + .where( + (gl_entry.company == company) + & (gl_entry.is_cancelled == 0) + & (gl_entry.posting_date < fy_start) + ) + .groupby(gl_entry.account) + ) + sum_until_month = ( frappe.qb.from_(gl_entry) .select( @@ -115,9 +161,13 @@ def get_data(company: str, fy_start, month_start, month_end): frappe.qb.from_(sum_in_month) .left_join(sum_until_month) .on(sum_until_month.account == sum_in_month.account) + .left_join(opening_balance) + .on(opening_balance.account == sum_in_month.account) .select( sum_in_month.account, sum_in_month.account_currency, + opening_balance.debit, + opening_balance.credit, sum_until_month.debit, sum_until_month.credit, sum_in_month.debit, diff --git a/erpnext_germany/translations/de.csv b/erpnext_germany/translations/de.csv index 4063036c..aabfa146 100644 --- a/erpnext_germany/translations/de.csv +++ b/erpnext_germany/translations/de.csv @@ -47,3 +47,5 @@ Debit until {0},Soll bis {0}, Credit until {0},Haben bis {0}, Debit in {0},Soll im {0}, Credit in {0},Haben im {0}, +Debit Opening Balance,Soll Anfangsbestand, +Credit Opening Balance,Haben Anfangsbestand,