customer.py 20.1 KB
Newer Older
Patrick's avatar
Patrick committed
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
# -*- coding: utf-8
from __future__ import unicode_literals

import uuid
from collections import OrderedDict
from os import sep as os_sep

from decimal import Decimal
from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.db.models import Q
from django.forms import Textarea
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from import_export import resources, fields
from import_export.admin import ImportExportMixin
from import_export.formats.base_formats import XLS
from import_export.widgets import CharWidget

from repanier.const import EMPTY_STRING, ORDER_GROUP, INVOICE_GROUP, \
    COORDINATION_GROUP, DECIMAL_ONE, TWO_DECIMALS
from repanier.models import Customer, LUT_DeliveryPoint
from repanier.xlsx import xlsx_purchase
from repanier.xlsx.widget import IdWidget, OneToOneWidget, \
    DecimalBooleanWidget, ZeroDecimalsWidget, TwoMoneysWidget, TranslatedForeignKeyWidget, DateWidgetExcel
from repanier.xlsx.extended_formats import XLSX_OPENPYXL_1_8_6


class UserDataForm(forms.ModelForm):
    email = forms.EmailField(label=_('Email'))
    user = None

    def __init__(self, *args, **kwargs):
        super(UserDataForm, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(UserDataForm, self).clean()
        initial_username = None
        try:
            initial_username = self.instance.user.username
        except:
            pass
        username_field_name = 'short_basket_name'
        username = self.cleaned_data.get(username_field_name)
        user_error1 = _('The given short_basket_name must be set')
        user_error2 = _('The given short_basket_name is used by another user')
        if not username:
            self.add_error(username_field_name, user_error1)
        # Check that the email is set
        if not "email" in self.cleaned_data:
            self.add_error('email', _('The given email must be set'))
        else:
            email = self.cleaned_data["email"]
            user_model = get_user_model()
            user = user_model.objects.filter(email=email).order_by("?").first()
            # Check that the username is not already used
            if user is not None:
                if initial_username != user.username:
                    self.add_error('email', _('The given email is used by another user'))
            user = user_model.objects.filter(username=username).order_by("?").first()
            if user is not None:
                if initial_username != user.username:
                    self.add_error(username_field_name, user_error2)
68
        if self.instance.id is not None and "price_list_multiplier" in self.cleaned_data:
Patrick's avatar
Patrick committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            if self.instance.delivery_point is not None \
                    and self.instance.delivery_point.customer_responsible is not None \
                    and self.cleaned_data["price_list_multiplier"] != DECIMAL_ONE:
                self.add_error('price_list_multiplier', _('If the customer is member of a closed group with a customer_responsible, the customer.price_list_multiplier must be set to ONE'))
            may_order = self.cleaned_data["may_order"]
            if may_order:
                delivery_point = LUT_DeliveryPoint.objects.filter(
                        customer_responsible=self.instance.id
                ).order_by('?').first()
                if delivery_point is not None:
                    self.add_error(
                        "may_order",
                        _(
                            'This customer is responsible of the delivery point (%(delivery_point)s). A customer responsible of a delivery point may not pass order.') % {
                            'delivery_point': delivery_point,})
84
85
            is_active = self.cleaned_data.get("is_active")
            if is_active is not None and not is_active:
Patrick's avatar
Patrick committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                delivery_point = LUT_DeliveryPoint.objects.filter(
                    customer_responsible=self.instance.id
                ).order_by('?').first()
                if delivery_point is not None:
                    self.add_error(
                        "is_active",
                        _(
                            'This customer is responsible of the delivery point (%(delivery_point)s). A customer responsible of a delivery point must be active.') % {
                            'delivery_point': delivery_point,})
        bank_account1 = self.cleaned_data["bank_account1"]
        if bank_account1:
            other_bank_account1 = Customer.objects.filter(
                Q(bank_account1=bank_account1) | Q(bank_account2=bank_account1)
            ).order_by("?")
            if self.instance.id is not None:
                other_bank_account1 = other_bank_account1.exclude(id=self.instance.id)
            if other_bank_account1.exists():
                self.add_error('bank_account1', _('This bank account already belongs to another customer.'))
        bank_account2 = self.cleaned_data["bank_account2"]
        if bank_account2:
            other_bank_account2 = Customer.objects.filter(
                Q(bank_account1=bank_account2) | Q(bank_account2=bank_account2)
            ).order_by("?")
            if self.instance.id is not None:
                other_bank_account2 = other_bank_account2.exclude(id=self.instance.id)
            if other_bank_account2.exists():
                self.add_error('bank_account2', _('This bank account already belongs to another customer.'))
        return cleaned_data

    def save(self, *args, **kwargs):
        super(UserDataForm, self).save(*args, **kwargs)
        change = (self.instance.id is not None)
        username = self.data['short_basket_name']
        email = self.data['email'].lower()
        user_model = get_user_model()
        if change:
            user = user_model.objects.get(id=self.instance.user_id)
            user.username = username
            user.first_name = EMPTY_STRING
            user.last_name = username
            user.email = email
            user.save()
        else:
            user = user_model.objects.create_user(
                username=username, email=email, password=uuid.uuid1().hex,
                first_name=EMPTY_STRING, last_name=username)
        self.user = user
        return self.instance


# Customer
class CustomerResource(resources.ModelResource):
    id = fields.Field(attribute='id', widget=IdWidget(), readonly=True)
    email = fields.Field(attribute='user', default="ask.it@to.me", widget=OneToOneWidget(User, 'email'), readonly=False)
    phone1 = fields.Field(attribute='phone1', default="1234", widget=CharWidget(), readonly=False)
    phone2 = fields.Field(attribute='phone2', widget=CharWidget(), readonly=False)
    date_balance = fields.Field(attribute='get_admin_date_balance', widget=DateWidgetExcel(), readonly=True)
    balance = fields.Field(attribute='get_admin_balance', widget=TwoMoneysWidget(), readonly=True)
    may_order = fields.Field(attribute='may_order', default=False, widget=DecimalBooleanWidget(), readonly=False)
145
    is_group = fields.Field(attribute='is_group', default=False, widget=DecimalBooleanWidget(), readonly=False)
Patrick's avatar
Patrick committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    represent_this_buyinggroup = fields.Field(attribute='represent_this_buyinggroup', widget=DecimalBooleanWidget(),
                                              readonly=True)
    is_active = fields.Field(attribute='is_active', widget=DecimalBooleanWidget(), readonly=True)
    membership_fee_valid_until = fields.Field(
        attribute='membership_fee_valid_until', default=timezone.now().date(),
        widget=DateWidgetExcel(),
        readonly=False)
    last_membership_fee = fields.Field(attribute='get_last_membership_fee', widget=TwoMoneysWidget(), readonly=True)
    last_membership_fee_date = fields.Field(
        attribute='last_membership_fee_date',
        widget=DateWidgetExcel(),
        readonly=True)
    purchase = fields.Field(attribute='get_purchase', widget=ZeroDecimalsWidget(), readonly=True)
    participation = fields.Field(attribute='get_participation', widget=ZeroDecimalsWidget(), readonly=True)
    delivery_point = fields.Field(attribute='delivery_point',
                                  widget=TranslatedForeignKeyWidget(LUT_DeliveryPoint, field='short_name'))
    valid_email = fields.Field(attribute='valid_email', widget=DecimalBooleanWidget(), readonly=True)

    def before_save_instance(self, instance, using_transactions, dry_run):
        """
        Override to add additional logic.
        """
        user_model = get_user_model()
        user_email_qs = user_model.objects.filter(email=instance.user.email).order_by('?')
        user_username_qs = user_model.objects.filter(username=instance.short_basket_name).order_by('?')
        if instance.id is not None:
            customer = Customer.objects.filter(id=instance.id).order_by('?').only('id', 'user_id').first()
            user_email_qs = user_email_qs.exclude(id=customer.user_id)
            user_username_qs = user_username_qs.exclude(id=customer.user_id)
        else:
            customer = None
        if user_email_qs.exists():
            raise ValueError(_("The email %s is already used by another user.") % instance.user.email)
        if user_username_qs.exists():
            raise ValueError(
                _("The short_basket_name %s is already used by another user.") % instance.short_basket_name)
        if using_transactions or not dry_run:
            if instance.id is not None:
                email = instance.user.email
                instance.user = user_model.objects.get(id=customer.user_id)
                instance.user.username = instance.short_basket_name
                instance.user.first_name = EMPTY_STRING
                instance.user.last_name = instance.short_basket_name
                instance.user.email = email
                instance.user.save()
            else:
                instance.user = user_model.objects.create_user(
                    username=instance.short_basket_name, email=instance.user.email, password=uuid.uuid1().hex,
                    first_name=EMPTY_STRING, last_name=instance.short_basket_name)
                instance.user_id = instance.user.id

    class Meta:
        model = Customer
        fields = (
            'id', 'may_order', 'short_basket_name', 'long_basket_name', 'email',
            'email2', 'language',
            'phone1', 'phone2', 'address', 'city',
            'bank_account1', 'bank_account2',
            'date_balance', 'balance', 'price_list_multiplier',
            'membership_fee_valid_until', 'last_membership_fee', 'last_membership_fee_date',
206
207
            'participation', 'purchase', 'represent_this_buyinggroup', 'is_group', 'is_active', 'delivery_point',
            'valid_email'
Patrick's avatar
Patrick committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
        )
        export_order = fields
        import_id_fields = ('id',)
        skip_unchanged = True
        report_skipped = False
        use_transactions = False


def create__customer_action(year):
    def action(modeladmin, request, queryset):
        queryset = queryset.order_by('?')
        return xlsx_purchase.admin_export_year_by_customer(year, queryset)

    name = "export_producer_%d" % (year,)
    return (name, (action, name, _("Export purchases of %s") % (year,)))


class CustomerWithUserDataForm(UserDataForm):
    class Meta:
        widgets = {
            'address': Textarea(attrs={'rows': 4, 'cols': 80, 'style': 'height: 5em; width: 30em;'}),
            'memo'   : Textarea(attrs={'rows': 4, 'cols': 160, 'style': 'height: 5em; width: 60em;'}),
        }
        model = Customer
        fields = "__all__"


class CustomerWithUserDataAdmin(ImportExportMixin, admin.ModelAdmin):
    form = CustomerWithUserDataForm
    resource_class = CustomerResource
    list_display = (
        'short_basket_name', 'get_balance', 'may_order', 'long_basket_name', 'phone1', 'get_email',
        'get_last_login', 'valid_email')
    search_fields = ('short_basket_name', 'long_basket_name', 'user__email', 'email2')
    list_per_page = 16
    list_max_show_all = 16
244
    list_filter = ('is_active', 'may_order', 'is_group', 'valid_email')
Patrick's avatar
Patrick committed
245
246
247
248
249
250
251
252
253
254
255

    def has_delete_permission(self, request, customer=None):
        if request.user.groups.filter(
                name__in=[ORDER_GROUP, INVOICE_GROUP, COORDINATION_GROUP]).exists() or request.user.is_superuser:
            if customer is not None:
                if customer.represent_this_buyinggroup:
                    # I can't delete the customer representing the buying group
                    return False
            return True
        return False

256
    def has_add_permission(self, request):
Patrick's avatar
Patrick committed
257
258
259
260
        if request.user.groups.filter(
                name__in=[ORDER_GROUP, INVOICE_GROUP, COORDINATION_GROUP]).exists() or request.user.is_superuser:
            return True
        return False
261
262

    def has_change_permission(self, request, obj=None):
Patrick's avatar
Patrick committed
263
        return self.has_add_permission(request)
264

Patrick's avatar
Patrick committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    def get_email(self, customer):
        if customer.user is not None:
            return customer.user.email
        else:
            return EMPTY_STRING

    get_email.short_description = _("email")
    get_email.admin_order_field = 'user__email'

    def get_date_joined(self, customer):
        if customer.user is not None:
            return customer.user.date_joined.strftime(settings.DJANGO_SETTINGS_DATE)
        else:
            return EMPTY_STRING

    get_date_joined.short_description = _("date joined")

    def get_last_login(self, customer):
        if customer.user is not None and customer.user.last_login is not None:
            return customer.user.last_login.strftime(settings.DJANGO_SETTINGS_DATE)
        else:
            return EMPTY_STRING

    get_last_login.short_description = _("last login")
    get_last_login.admin_order_field = 'user__last_login'

    def get_actions(self, request):
        actions = super(CustomerWithUserDataAdmin, self).get_actions(request)
        this_year = timezone.now().year
        actions.update(OrderedDict(create__customer_action(y) for y in [this_year, this_year - 1, this_year - 2]))
        return actions

    def get_fieldsets(self, request, customer=None):
        fields_basic = [
            ('short_basket_name', 'long_basket_name', 'language'),
            ('email', 'email2', 'accept_mails_from_members'),
            ('phone1', 'phone2', 'accept_phone_call_from_members'),
            ('membership_fee_valid_until', 'vat_id',),
        ]
        if customer is not None:
            fields_basic += [
                ('address', 'city', 'picture'),
                'memo',
            ]
        else:
            # Do not accept the picture because there is no customer.id for the "upload_to"
            fields_basic += [
                ('address', 'city'),
                'memo',
            ]

        if customer is not None:
            if customer.represent_this_buyinggroup:
                fields_basic += [
                    ('get_admin_balance', 'get_admin_date_balance'),
                    ('may_order', 'represent_this_buyinggroup')
                ]
            else:
                fields_basic += [
                    ('get_admin_balance', 'price_list_multiplier', 'get_admin_date_balance'),
325
                    ('may_order', 'is_group', 'is_active'),
Patrick's avatar
Patrick committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
                ]
        else:
            fields_basic += [
                'price_list_multiplier',
                ('may_order', 'is_active'),
            ]
        fields_advanced = [
            'bank_account1',
            'bank_account2',
            'get_last_login',
            'get_date_joined',
            'get_last_membership_fee',
            'get_last_membership_fee_date',
            'get_participation',
            'get_purchase'
        ]
        fieldsets = (
            (None, {'fields': fields_basic}),
            (_('Advanced options'), {'classes': ('collapse',), 'fields': fields_advanced})
        )
        return fieldsets

    def get_readonly_fields(self, request, customer=None):
        if customer is not None:
350
351
352
353
354
355
356
357
358
359
360
361
362
363
            if customer.represent_this_buyinggroup:
                return [
                    'get_admin_date_balance', 'get_admin_balance',
                    'represent_this_buyinggroup', 'get_last_login',
                    'get_date_joined', 'get_participation', 'get_purchase',
                    'get_last_membership_fee', 'get_last_membership_fee_date'
                ]
            else:
                return [
                    'get_admin_date_balance', 'get_admin_balance',
                    'get_last_login',
                    'get_date_joined', 'get_participation', 'get_purchase',
                    'get_last_membership_fee', 'get_last_membership_fee_date'
                ]
Patrick's avatar
Patrick committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
        else:
            return [
                'represent_this_buyinggroup', 'get_last_login',
                'get_date_joined', 'get_participation', 'get_purchase',
                'get_last_membership_fee', 'get_last_membership_fee_date'
            ]

    def get_form(self, request, customer=None, **kwargs):
        form = super(CustomerWithUserDataAdmin, self).get_form(request, customer, **kwargs)
        username_field = form.base_fields['short_basket_name']
        email_field = form.base_fields['email']

        if customer is not None:
            user_model = get_user_model()
            user = user_model.objects.get(id=customer.user_id)
            username_field.initial = getattr(user, user_model.USERNAME_FIELD)
            email_field.initial = user.email
            # One folder by customer to avoid picture names conflicts
            picture_field = form.base_fields["picture"]
            if hasattr(picture_field.widget, 'upload_to'):
                picture_field.widget.upload_to = "%s%s%d" % ("customer", os_sep, customer.id)
        else:
            # Clean data displayed
            username_field.initial = EMPTY_STRING
            email_field.initial = EMPTY_STRING
        return form

    def save_model(self, request, customer, form, change):
        customer.user = form.user
        form.user.is_staff = False
        form.user.is_active = customer.is_active
        form.user.save()
        super(CustomerWithUserDataAdmin, self).save_model(
            request, customer, form, change)
        if customer.delivery_point is not None:
            customer_price = EMPTY_STRING
            if customer.price_list_multiplier < DECIMAL_ONE:
                customer_price = \
                    _(' in addition to the %(discount)s%% personal discount rate on to the pricelist') % {
                        'discount': Decimal((DECIMAL_ONE - customer.price_list_multiplier) * 100).quantize(TWO_DECIMALS)
                    }
            elif customer.price_list_multiplier > DECIMAL_ONE:
                customer_price = \
                    _(' in addition to the %(surcharge)s%% personal surcharge on to the pricelist') % {
                        'surcharge': Decimal((customer.price_list_multiplier - DECIMAL_ONE) * 100).quantize(TWO_DECIMALS)
                    }
            if customer.delivery_point.price_list_multiplier < DECIMAL_ONE:
                messages.add_message(request, messages.WARNING,
                        _('%(discount)s%% discount is granted to consumer invoices when delivered to %(delivery_point)s%(customer_price)s.') % {
                            'discount': Decimal((DECIMAL_ONE - customer.delivery_point.price_list_multiplier) * 100).quantize(TWO_DECIMALS),
                            'delivery_point': customer.delivery_point,
                            'customer_price': customer_price
                        }
                )
            elif customer.delivery_point.price_list_multiplier > DECIMAL_ONE:
                messages.add_message(request, messages.WARNING,
                        _('%(surcharge)s%% surcharge is applied to consumer invoices when delivered to %(delivery_point)s%(customer_price)s.') % {
                            'surcharge': Decimal((customer.delivery_point.price_list_multiplier - DECIMAL_ONE) * 100).quantize(TWO_DECIMALS),
                            'delivery_point': customer.delivery_point,
                            'customer_price': customer_price
                        }
                )

    def get_import_formats(self):
        """
        Returns available import formats.
        """
        return [f for f in (XLS, XLSX_OPENPYXL_1_8_6) if f().can_import()]