Skip to content

Commit

Permalink
Support fractional VAT rates
Browse files Browse the repository at this point in the history
Allow fractional tax rates, up to 6 decimal places.

Author: Steve Singer, with some fixes from me

Fixes #164
  • Loading branch information
mhagander committed Dec 4, 2024
1 parent dfff6e7 commit f1f9a37
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
20 changes: 20 additions & 0 deletions postgresqleu/invoices/migrations/0021_alter_vatrate_vatpercent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.22 on 2024-09-29 02:12

import django.core.validators
from django.db import migrations
import postgresqleu.invoices.models


class Migration(migrations.Migration):

dependencies = [
('invoices', '0020_regtransfer_processor'),
]

operations = [
migrations.AlterField(
model_name='vatrate',
name='vatpercent',
field=postgresqleu.util.fields.NormalizedDecimalField(decimal_places=6, default=0, max_digits=9, validators=[django.core.validators.MaxValueValidator(100), django.core.validators.MinValueValidator(0)], verbose_name='VAT percentage'),
),
]
6 changes: 2 additions & 4 deletions postgresqleu/invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from postgresqleu.util.validators import ListOfEmailAddressValidator
from postgresqleu.util.checksum import luhn
from postgresqleu.util.fields import LowercaseEmailField
from postgresqleu.util.fields import LowercaseEmailField, NormalizedDecimalField
from postgresqleu.accounting.models import Account, JournalEntry


Expand Down Expand Up @@ -238,10 +238,8 @@ class Meta:
class VatRate(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
shortname = models.CharField(max_length=16, blank=False, null=False, verbose_name="Short name")
vatpercent = models.IntegerField(null=False, default=0, verbose_name="VAT percentage",
validators=[MaxValueValidator(100), MinValueValidator(0)])
vatpercent = NormalizedDecimalField(null=False, default=0, verbose_name="VAT percentage", max_digits=9, decimal_places=6, validators=[MaxValueValidator(100), MinValueValidator(0)])
vataccount = models.ForeignKey(Account, null=False, blank=False, on_delete=models.CASCADE, verbose_name="VAT account")

_safe_attributes = ('vatpercent', 'shortstr', 'shortname', 'name')

def __str__(self):
Expand Down
9 changes: 9 additions & 0 deletions postgresqleu/util/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
from django.db import models
from django.core.exceptions import ValidationError
from .forms import ImageBinaryFormField, PdfBinaryFormField
Expand Down Expand Up @@ -117,3 +118,11 @@ def to_python(self, value):
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()
4 changes: 2 additions & 2 deletions template/confreg/admin_backend_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@

$('.backend-vat-field').each(function(i, e) {
$(e).change(function(e) {
if ($(this).hasClass('backend-vat-reg-field')) { vatrate = 1.{{conference.vat_registrations.vatpercent|default:0}};}
else if ($(this).hasClass('backend-vat-sponsor.field')) { vatrate = 1.{{conference.vat_sponsorship.vatpercent|default:0}};}
if ($(this).hasClass('backend-vat-reg-field')) { vatrate = 1 + {{conference.vat_registrations.vatpercent|default:0}}/100;}
else if ($(this).hasClass('backend-vat-sponsor-field')) { vatrate = 1 + {{conference.vat_sponsorship.vatpercent|default:0}}/100;}
else return;
if (vatrate == 1) return;

Expand Down

0 comments on commit f1f9a37

Please sign in to comment.