diff --git a/src/documents/migrations/1054_customfieldinstance_value_monetary_amount_and_more.py b/src/documents/migrations/1054_customfieldinstance_value_monetary_amount_and_more.py index 210df44fe..37d1aaeab 100644 --- a/src/documents/migrations/1054_customfieldinstance_value_monetary_amount_and_more.py +++ b/src/documents/migrations/1054_customfieldinstance_value_monetary_amount_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 5.1.1 on 2024-09-29 00:39 +# Generated by Django 5.1.1 on 2024-09-29 05:16 import django.db.models.functions.text from django.db import migrations @@ -16,7 +16,17 @@ class Migration(migrations.Migration): name="value_monetary_amount", field=models.GeneratedField( db_persist=True, - expression=django.db.models.functions.text.Substr("value_monetary", 4), + expression=models.Case( + models.When( + then=django.db.models.functions.text.Substr( + "value_monetary", + 1, + ), + value_monetary__regex="^\\d+", + ), + default=django.db.models.functions.text.Substr("value_monetary", 4), + output_field=models.DecimalField(decimal_places=2, max_digits=125), + ), output_field=models.DecimalField(decimal_places=2, max_digits=125), ), ), diff --git a/src/documents/models.py b/src/documents/models.py index 71622b31b..51c19c83e 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -22,6 +22,7 @@ from multiselectfield import MultiSelectField if settings.AUDIT_LOG_ENABLED: from auditlog.registry import auditlog +from django.db.models import Case from django.db.models.functions import Substr from django_softdelete.models import SoftDeleteModel @@ -924,7 +925,16 @@ class CustomFieldInstance(models.Model): value_monetary = models.CharField(null=True, max_length=128) value_monetary_amount = models.GeneratedField( - expression=Substr("value_monetary", 4), + expression=Case( + # If the value starts with a number and no currency symbol, use the whole string + models.When( + value_monetary__regex=r"^\d+", + then=Substr("value_monetary", 1), + ), + # If the value starts with a 3-char currency symbol, use the rest of the string + default=Substr("value_monetary", 4), + output_field=models.DecimalField(decimal_places=2, max_digits=125), + ), output_field=models.DecimalField(decimal_places=2, max_digits=125), db_persist=True, ) diff --git a/src/documents/tests/test_api_filter_by_custom_fields.py b/src/documents/tests/test_api_filter_by_custom_fields.py index 3dabf896b..c9a0cdcfc 100644 --- a/src/documents/tests/test_api_filter_by_custom_fields.py +++ b/src/documents/tests/test_api_filter_by_custom_fields.py @@ -104,6 +104,7 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase): self._create_document(monetary_field="USD100.00") self._create_document(monetary_field="USD1.00") self._create_document(monetary_field="EUR50.00") + self._create_document(monetary_field="101.00") # CustomField.FieldDataType.DOCUMENTLINK self._create_document(documentlink_field=None) @@ -398,7 +399,10 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase): ["monetary_field", "gt", "99"], lambda document: "monetary_field" in document and document["monetary_field"] is not None - and document["monetary_field"] == "USD100.00", + and ( + document["monetary_field"] == "USD100.00" # With currency symbol + or document["monetary_field"] == "101.00" # No currency symbol + ), ) # ==========================================================#