diff --git a/india_compliance/gst_india/overrides/test_purchase_invoice.py b/india_compliance/gst_india/overrides/test_purchase_invoice.py index 7ba8bf5af..cb66f33ff 100644 --- a/india_compliance/gst_india/overrides/test_purchase_invoice.py +++ b/india_compliance/gst_india/overrides/test_purchase_invoice.py @@ -25,8 +25,9 @@ def test_validate_invoice_length(self): setattr(pinv, "__newname", "INV/2022/00001/asdfsadg") # NOQA pinv.meta.autoname = "prompt" - self.assertRaisesRegex( - frappe.exceptions.ValidationError, + pinv.save() + + self.assertEqual( + frappe.parse_json(frappe.message_log[-1]).get("message"), "Transaction Name must be 16 characters or fewer to meet GST requirements", - pinv.save, ) diff --git a/india_compliance/gst_india/overrides/test_sales_invoice.py b/india_compliance/gst_india/overrides/test_sales_invoice.py index 010f6df56..3301c9332 100644 --- a/india_compliance/gst_india/overrides/test_sales_invoice.py +++ b/india_compliance/gst_india/overrides/test_sales_invoice.py @@ -17,7 +17,9 @@ def test_validate_invoice_number(self): "PI2021 - 001", ] for name in invalid_names: - doc = frappe._dict(name=name, posting_date=posting_date) + doc = frappe._dict( + name=name, posting_date=posting_date, doctype="Sales Invoice" + ) self.assertRaises(frappe.ValidationError, validate_invoice_number, doc) valid_names = [ diff --git a/india_compliance/gst_india/utils/__init__.py b/india_compliance/gst_india/utils/__init__.py index f7e2f366e..bd067ea23 100644 --- a/india_compliance/gst_india/utils/__init__.py +++ b/india_compliance/gst_india/utils/__init__.py @@ -919,22 +919,28 @@ def disable_item_tax_template_notification(): def validate_invoice_number(doc): """Validate GST invoice number requirements.""" - if len(doc.name) > 16: - frappe.throw( - _( - "Transaction Name must be 16 characters or fewer to meet GST requirements" - ), - title=_("Invalid GST Transaction Name"), - ) + is_valid_length = len(doc.name) <= 16 + is_valid_format = GST_INVOICE_NUMBER_FORMAT.match(doc.name) - if not GST_INVOICE_NUMBER_FORMAT.match(doc.name): - frappe.throw( - _( - "Transaction Name should start with an alphanumeric character and can" - " only contain alphanumeric characters, dash (-) and slash (/) to meet GST requirements" - ), - title=_("Invalid GST Transaction Name"), + if is_valid_length and is_valid_format: + return + + title = _("Invalid GST Transaction Name") + + if not is_valid_length: + message = _( + "Transaction Name must be 16 characters or fewer to meet GST requirements" ) + else: + message = _( + "Transaction Name should start with an alphanumeric character and can" + " only contain alphanumeric characters, dash (-) and slash (/) to meet GST requirements" + ) + + if doc.doctype == "Sales Invoice": + frappe.throw(message, title=title) + + frappe.msgprint(message, title=title) def handle_server_errors(settings, doc, document_type, error):