Sith/counter/migrations/0013_customer_recorded_prod...

51 lines
1.8 KiB
Python
Raw Normal View History

2017-07-21 19:39:49 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.utils.translation import gettext_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
2018-10-04 19:29:19 +00:00
for selling in customer.buyings.filter(
product__id__in=[settings.SITH_ECOCUP_CONS, settings.SITH_ECOCUP_DECO]
).all():
2017-08-15 00:09:44 +00:00
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)
2018-10-04 19:29:19 +00:00
Selling(
label=_("Ecocup regularization"),
product=cons,
unit_price=cons.selling_price,
club=cons.club,
counter=Counter.objects.filter(name="Foyer").first(),
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):
2018-10-04 19:29:19 +00:00
dependencies = [("counter", "0012_auto_20170515_2202")]
2017-07-21 19:39:49 +00:00
operations = [
migrations.AddField(
2018-10-04 19:29:19 +00:00
model_name="customer",
name="recorded_products",
field=models.IntegerField(verbose_name="recorded items", default=0),
2017-07-21 19:39:49 +00:00
),
2017-08-14 01:13:06 +00:00
migrations.RunPython(balance_ecocups),
2017-07-21 19:39:49 +00:00
]