Sith/counter/migrations/0013_customer_recorded_products.py

44 lines
1.7 KiB
Python
Raw Normal View History

2017-07-21 19:39:49 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
2017-08-14 01:13:06 +00:00
from django.utils.translation import ugettext_lazy as _
2017-07-21 19:39:49 +00:00
from django.db import migrations, models
2017-08-14 01:13:06 +00:00
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():
2017-08-14 11:52:58 +00:00
customer.recorded_products = 0
2017-08-15 00:09:44 +00:00
for selling in customer.buyings.filter(product__id__in=[settings.SITH_ECOCUP_CONS, settings.SITH_ECOCUP_DECO]).all():
if selling.product.is_record_product:
2017-08-14 01:13:06 +00:00
customer.recorded_products += selling.quantity
2017-08-15 00:09:44 +00:00
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,
2017-08-14 01:13:06 +00:00
club=cons.club, counter=Counter.objects.filter(name='Foyer').first(),
2017-08-14 11:52:58 +00:00
quantity=qt, seller=User.objects.get(id=0), customer=customer).save(allow_negative=True)
2017-08-15 00:09:44 +00:00
customer.recorded_products += qt
2017-08-15 15:37:25 +00:00
customer.save()
2017-07-21 19:39:49 +00:00
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),
),
2017-08-14 01:13:06 +00:00
migrations.RunPython(balance_ecocups),
2017-07-21 19:39:49 +00:00
]