0013_customer_recorded_products.py 1.7 KB
Newer Older
Sli's avatar
Sli committed
1
2
3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

Sli's avatar
Sli committed
4
from django.utils.translation import ugettext_lazy as _
Sli's avatar
Sli committed
5
from django.db import migrations, models
Sli's avatar
Sli committed
6
7
8
9
10
11
12
13
from django.conf import settings

from core.models import User
from counter.models import Customer, Product, Selling, Counter


def balance_ecocups(apps, schema_editor):
    for customer in Customer.objects.all():
Sli's avatar
Sli committed
14
        customer.recorded_products = 0
Sli's avatar
Sli committed
15
16
        for selling in customer.buyings.filter(product__id__in=[settings.SITH_ECOCUP_CONS, settings.SITH_ECOCUP_DECO]).all():
            if selling.product.is_record_product:
Sli's avatar
Sli committed
17
                customer.recorded_products += selling.quantity
Sli's avatar
Sli committed
18
19
20
21
22
23
            elif selling.product.is_unrecord_product:
                customer.recorded_products -= selling.quantity
        if customer.recorded_products < -settings.SITH_ECOCUP_LIMIT:
            qt = -(customer.recorded_products + settings.SITH_ECOCUP_LIMIT)
            cons = Product.objects.get(id=settings.SITH_ECOCUP_CONS)
            Selling(label=_("Ecocup regularization"), product=cons, unit_price=cons.selling_price,
Sli's avatar
Sli committed
24
                    club=cons.club, counter=Counter.objects.filter(name='Foyer').first(),
Sli's avatar
Sli committed
25
                    quantity=qt, seller=User.objects.get(id=0), customer=customer).save(allow_negative=True)
Sli's avatar
Sli committed
26
            customer.recorded_products += qt
Sli's avatar
Sli committed
27
            customer.save()
Sli's avatar
Sli committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41


class Migration(migrations.Migration):

    dependencies = [
        ('counter', '0012_auto_20170515_2202'),
    ]

    operations = [
        migrations.AddField(
            model_name='customer',
            name='recorded_products',
            field=models.IntegerField(verbose_name='recorded items', default=0),
        ),
Sli's avatar
Sli committed
42
        migrations.RunPython(balance_ecocups),
Sli's avatar
Sli committed
43
    ]