From 0989f071218c98800bad13a2438bc4723dca681a Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:36:00 +0200 Subject: [PATCH] feat: delete only most recent sales transactions --- README.md | 6 ++++-- erpnext_germany/custom/__init__.py | 0 erpnext_germany/custom/sales.py | 26 ++++++++++++++++++++++++++ erpnext_germany/hooks.py | 18 +++++++++++------- erpnext_germany/translations/de.csv | 2 ++ 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 erpnext_germany/custom/__init__.py create mode 100644 erpnext_germany/custom/sales.py diff --git a/README.md b/README.md index 52ad0bd1..cb888a97 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,11 @@ App to hold regional code for Germany, built on top of ERPNext. Automatically checks the validity of EU VAT IDs of all your customers every three months, or manually whenever you want. Check out the [intro on Youtube](https://youtu.be/hsFMn2Y85zA) (german). - ![Validate EU VAT ID](docs/vat_check.png) + ![Validate EU VAT ID](docs/vat_check.png) -### HR +- Allow deletion of the most recent sales transaction only + + This ensures consecutive numbering of transactions. Applies to **Quotation**, **Sales Order**, **Sales Invoice**. - Custom fields in **Employee** (tax information, etc.) - List of religios denominations ("Konfessionen") diff --git a/erpnext_germany/custom/__init__.py b/erpnext_germany/custom/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/erpnext_germany/custom/sales.py b/erpnext_germany/custom/sales.py new file mode 100644 index 00000000..e0864c8e --- /dev/null +++ b/erpnext_germany/custom/sales.py @@ -0,0 +1,26 @@ +import frappe +from frappe import _ +from erpnext.controllers.selling_controller import SellingController + + +def on_trash(doc: SellingController, event: str = None) -> None: + if doc.flags.ignore_validate: + return + + if is_not_latest(doc.doctype, doc.name, doc.creation): + frappe.throw( + msg=_( + "Only the most recent {0} can be deleted in order to avoid gaps in numbering." + ).format(_(doc.doctype)), + title=_("Cannot delete this transaction"), + ) + + +def is_not_latest(doctype, name, creation): + return frappe.db.exists( + doctype, + { + "creation": (">", creation), + "name": ("!=", name), + }, + ) diff --git a/erpnext_germany/hooks.py b/erpnext_germany/hooks.py index abb596e4..531e4135 100644 --- a/erpnext_germany/hooks.py +++ b/erpnext_germany/hooks.py @@ -106,13 +106,17 @@ # --------------- # Hook on document methods and events -# doc_events = { -# "*": { -# "on_update": "method", -# "on_cancel": "method", -# "on_trash": "method" -# } -# } +doc_events = { + "Quotation": { + "on_trash": "erpnext_germany.custom.sales.on_trash", + }, + "Sales Order": { + "on_trash": "erpnext_germany.custom.sales.on_trash", + }, + "Sales Invoice": { + "on_trash": "erpnext_germany.custom.sales.on_trash", + }, +} # doc_events = {} diff --git a/erpnext_germany/translations/de.csv b/erpnext_germany/translations/de.csv index 0958d6a8..841f20b5 100644 --- a/erpnext_germany/translations/de.csv +++ b/erpnext_germany/translations/de.csv @@ -41,3 +41,5 @@ Completed,Abgeschlossen Service Unavailable,Dienst nicht verfügbar 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."