diff options
| author | Magnus Hagander | 2024-12-04 19:05:06 +0000 |
|---|---|---|
| committer | Magnus Hagander | 2024-12-04 19:17:01 +0000 |
| commit | f1f9a37a48606e5ff710b09add9048d8104d0c77 (patch) | |
| tree | 3136200169a3ae0f64d8da7fa8e43ebb99db6b33 /postgresqleu/util | |
| parent | dfff6e725710f307dce3f3ad445b038e82ac1ad4 (diff) | |
Support fractional VAT rates
Allow fractional tax rates, up to 6 decimal places.
Author: Steve Singer, with some fixes from me
Fixes #164
Diffstat (limited to 'postgresqleu/util')
| -rw-r--r-- | postgresqleu/util/fields.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/postgresqleu/util/fields.py b/postgresqleu/util/fields.py index 061c5494..8565548a 100644 --- a/postgresqleu/util/fields.py +++ b/postgresqleu/util/fields.py @@ -1,3 +1,4 @@ +from decimal import Decimal from django.db import models from django.core.exceptions import ValidationError from .forms import ImageBinaryFormField, PdfBinaryFormField @@ -117,3 +118,11 @@ class PdfBinaryField(ImageBinaryField): class UserModelChoiceField(ModelChoiceField): def label_from_instance(self, obj): return "{0} - {1} {2} <{3}>".format(obj.username, obj.first_name, obj.last_name, obj.email) + + +class NormalizedDecimalField(models.DecimalField): + def from_db_value(self, value, expression, connection): + return value.quantize(Decimal(1)) if value == value.to_integral() else value.normalize() + + def to_python(self, value): + return super().to_python(value).quantize(Decimal(1)) if value == value.to_integral() else value.normalize() |
