summaryrefslogtreecommitdiff
path: root/postgresqleu/invoices/backendforms.py
blob: ed3e923a9b071d95f11b61a80e357cc95137f3bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import django.forms

from postgresqleu.util.backendforms import BackendForm, BackendBeforeNewForm
from postgresqleu.util.widgets import TestButtonWidget
from postgresqleu.invoices.models import VatRate, VatValidationCache, InvoicePaymentMethod

from postgresqleu.util.payment import payment_implementation_choices


class BackendVatRateForm(BackendForm):
    helplink = 'payment'
    list_fields = ['name', 'shortname', 'vatpercent', ]

    class Meta:
        model = VatRate
        fields = ['name', 'shortname', 'vatpercent', 'vataccount', ]


class BackendVatValidationCacheForm(BackendForm):
    helplink = 'payment'
    list_fields = ['vatnumber', 'checkedat', ]

    class Meta:
        model = VatValidationCache
        fields = ['vatnumber', ]


class BackendInvoicePaymentMethodNewForm(BackendBeforeNewForm):
    helplink = 'payment'
    paymentclass = django.forms.ChoiceField(choices=payment_implementation_choices(), label="Payment implementation")

    def get_newform_data(self):
        return self.cleaned_data['paymentclass']


class BackendInvoicePaymentMethodForm(BackendForm):
    testsettings = django.forms.CharField(required=False, label='Test', widget=TestButtonWidget)

    helplink = 'payment'
    list_fields = ['name', 'internaldescription', 'classname_short', 'active', 'sortkey', ]
    form_before_new = BackendInvoicePaymentMethodNewForm
    verbose_field_names = {
        'classname_short': 'Implementation',
    }
    queryset_extra_fields = {
        'classname_short': r"substring(classname, '[^\.]+$')",
    }
    coltypes = {
        'Sort key': ['nosearch', ],
    }
    defaultsort = [['sortkey', 'asc'], ['name', 'asc']]

    config_fields = []
    config_fieldsets = []
    config_readonly = []

    class Meta:
        model = InvoicePaymentMethod
        fields = ['name', 'internaldescription', 'active', 'sortkey', 'classname']

    @property
    def fieldsets(self):
        fs = [
            {'id': 'common', 'legend': 'Common', 'fields': ['name', 'internaldescription', 'active', 'sortkey', 'classname'], }
        ] + self.config_fieldsets
        if hasattr(self, 'validate_data_for'):
            fs += [
                {'id': 'test', 'legend': 'Test', 'fields': ['testsettings', ]},
            ]

        return fs

    @property
    def readonly_fields(self):
        if hasattr(self, 'validate_data_for'):
            return ['classname', 'testsettings', ] + self.config_readonly
        else:
            return ['classname', ] + self.config_readonly

    @property
    def exclude_fields_from_validation(self):
        return self.config_readonly

    @property
    def json_form_fields(self):
        return {
            'config': self.config_fields,
        }

    def fix_fields(self):
        for k in self.config_readonly:
            self.fields[k].required = False

        if self.newformdata:
            self.instance.classname = self.newformdata
            self.initial['classname'] = self.newformdata

        if not hasattr(self, 'validate_data_for'):
            self.remove_field('testsettings')


class BankfilePaymentMethodChoiceForm(django.forms.Form):
    paymentmethod = django.forms.ModelChoiceField(queryset=None, required=True, label="Payment method")

    def __init__(self, *args, **kwargs):
        methods = kwargs.pop('methods')
        super().__init__(*args, **kwargs)
        self.fields['paymentmethod'].queryset = methods