From 6c48b7c718493cb5b55883b47e17f3eb29c3437e Mon Sep 17 00:00:00 2001 From: Skia Date: Tue, 31 May 2016 19:32:15 +0200 Subject: [PATCH] Add Selling and Refilling classes --- core/management/commands/populate.py | 23 +++++--- counter/admin.py | 3 ++ counter/migrations/0002_refilling_selling.py | 42 +++++++++++++++ counter/migrations/0003_customer_amount.py | 21 ++++++++ counter/models.py | 56 ++++++++++++++++++++ counter/views.py | 8 ++- sith/settings.py | 5 ++ 7 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 counter/migrations/0002_refilling_selling.py create mode 100644 counter/migrations/0003_customer_amount.py diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 9d1cf35b..dfc9cfc1 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -8,7 +8,7 @@ from core.models import Group, User, Page, PageRev from accounting.models import GeneralJournal, BankAccount, ClubAccount, Operation, AccountingType from club.models import Club, Membership from subscription.models import Subscription, Subscriber -from counter.models import Customer, ProductType, Product +from counter.models import Customer, ProductType, Product, Counter class Command(BaseCommand): help = "Populate a new instance of the Sith AE" @@ -138,18 +138,27 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site. address="Terre Du Milieu", parent=ae) troll.save() - # Accounting test values: - Customer(user=skia, account_id="6568j").save() + # Counters + Customer(user=skia, account_id="6568j", amount=0).save() p = ProductType(name="Bières bouteilles") p.save() - Product(name="Barbar", code="BARB", product_type=p, purchase_price="1.50", selling_price="1.7", - special_selling_price="1.6", club=ae).save() - Product(name="Chimay", code="CBLE", product_type=p, purchase_price="1.50", selling_price="1.7", - special_selling_price="1.6", club=ae).save() + barb = Product(name="Barbar", code="BARB", product_type=p, purchase_price="1.50", selling_price="1.7", + special_selling_price="1.6", club=ae) + barb.save() + cble = Product(name="Chimay Bleue", code="CBLE", product_type=p, purchase_price="1.50", selling_price="1.7", + special_selling_price="1.6", club=ae) + cble.save() Product(name="Corsendonk", code="CORS", product_type=p, purchase_price="1.50", selling_price="1.7", special_selling_price="1.6", club=ae).save() Product(name="Carolus", code="CARO", product_type=p, purchase_price="1.50", selling_price="1.7", special_selling_price="1.6", club=ae).save() + mde = Counter(name="MDE", club=ae, type="BAR") + mde.save() + mde.products.add(barb) + mde.products.add(cble) + mde.save() + + # Accounting test values: BankAccount(name="AE TG", club=ae).save() BankAccount(name="Carte AE", club=ae).save() ba = BankAccount(name="AE TI", club=ae) diff --git a/counter/admin.py b/counter/admin.py index c0dd7c95..bb9735d0 100644 --- a/counter/admin.py +++ b/counter/admin.py @@ -7,3 +7,6 @@ admin.site.register(Customer) admin.site.register(ProductType) admin.site.register(Product) admin.site.register(Counter) +admin.site.register(Refilling) +admin.site.register(Selling) + diff --git a/counter/migrations/0002_refilling_selling.py b/counter/migrations/0002_refilling_selling.py new file mode 100644 index 00000000..31f8ce22 --- /dev/null +++ b/counter/migrations/0002_refilling_selling.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import accounting.models +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('counter', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Refilling', + fields=[ + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), + ('amount', accounting.models.CurrencyField(verbose_name='amount', decimal_places=2, max_digits=12)), + ('date', models.DateTimeField(verbose_name='date', auto_now=True)), + ('payment_method', models.CharField(verbose_name='payment method', max_length=255, choices=[('cheque', 'Chèque'), ('cash', 'Espèce')])), + ('counter', models.ForeignKey(to='counter.Counter', related_name='refillings')), + ('customer', models.ForeignKey(to='counter.Customer', related_name='refill_customers')), + ('operator', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='refill_operators')), + ], + ), + migrations.CreateModel( + name='Selling', + fields=[ + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), + ('unit_price', accounting.models.CurrencyField(verbose_name='unit price', decimal_places=2, max_digits=12)), + ('quantity', models.IntegerField(verbose_name='quantity')), + ('date', models.DateTimeField(verbose_name='date', auto_now=True)), + ('counter', models.ForeignKey(to='counter.Counter', related_name='sellings')), + ('customer', models.ForeignKey(to='counter.Customer', related_name='customers')), + ('product', models.ForeignKey(to='counter.Product', related_name='sellings')), + ('seller', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='sellers')), + ], + ), + ] diff --git a/counter/migrations/0003_customer_amount.py b/counter/migrations/0003_customer_amount.py new file mode 100644 index 00000000..d111ebb0 --- /dev/null +++ b/counter/migrations/0003_customer_amount.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import accounting.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('counter', '0002_refilling_selling'), + ] + + operations = [ + migrations.AddField( + model_name='customer', + name='amount', + field=accounting.models.CurrencyField(verbose_name='amount', default=0, decimal_places=2, max_digits=12), + preserve_default=False, + ), + ] diff --git a/counter/models.py b/counter/models.py index 4563c6b5..2417db5f 100644 --- a/counter/models.py +++ b/counter/models.py @@ -18,6 +18,7 @@ class Customer(models.Model): """ user = models.OneToOneField(User, primary_key=True) account_id = models.CharField(_('account id'), max_length=10, unique=True) + amount = CurrencyField(_('amount')) class Meta: verbose_name = _('customer') @@ -108,9 +109,64 @@ class Counter(models.Model): Counter.barmen_session[counter_id]['users'] = set() return bl + def get_random_barman(counter_id): # TODO: improve this function + bl = Counter.get_barmen_list(counter_id) + return bl[0] + +class Refilling(models.Model): + """ + Handle the refilling + """ + counter = models.ForeignKey(Counter, related_name="refillings", blank=False) + amount = CurrencyField(_('amount')) + operator = models.ForeignKey(User, related_name="refill_operators", blank=False) + customer = models.ForeignKey(Customer, related_name="refill_customers", blank=False) + date = models.DateTimeField(_('date'), auto_now=True) + payment_method = models.CharField(_('payment method'), max_length=255, + choices=settings.SITH_COUNTER_PAYMENT_METHOD) + # TODO: add the bank if the payment is made by cheque + + def __str__(self): + return "Refilling: %f for %s" % (self.amount, self.customer.user.get_display_name()) + + # def get_absolute_url(self): + # return reverse('counter:details', kwargs={'counter_id': self.id}) + + def save(self, *args, **kwargs): + self.full_clean() + self.customer.amount += self.quantity * self.unit_price + self.customer.save() + super(Selling, self).save(*args, **kwargs) + +class Selling(models.Model): + """ + Handle the sellings + """ + product = models.ForeignKey(Product, related_name="sellings", blank=False) + counter = models.ForeignKey(Counter, related_name="sellings", blank=False) + unit_price = CurrencyField(_('unit price')) + quantity = models.IntegerField(_('quantity')) + seller = models.ForeignKey(User, related_name="sellers", blank=False) + customer = models.ForeignKey(Customer, related_name="customers", blank=False) + date = models.DateTimeField(_('date'), auto_now=True) + + def __str__(self): + return "Selling: %d x %s (%f) for %s" % (self.quantity, self.product.name, + self.quantity*self.unit_price, self.customer.user.get_display_name()) + + def save(self, *args, **kwargs): + self.full_clean() + self.customer.amount -= self.quantity * self.unit_price + self.customer.save() + super(Selling, self).save(*args, **kwargs) + + # def get_absolute_url(self): + # return reverse('counter:details', kwargs={'counter_id': self.id}) + # TODO: # une classe Vente # foreign key vers comptoir, vendeur, client, produit, mais stocker le prix du produit, pour gerer les maj de prix # une classe Rechargement # foreign key vers comptoir, vendeur, client, plus montant + diff --git a/counter/views.py b/counter/views.py index 8f2527ef..4f41a778 100644 --- a/counter/views.py +++ b/counter/views.py @@ -12,7 +12,7 @@ from django import forms from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin from subscription.models import Subscriber -from counter.models import Counter, Customer, Product +from counter.models import Counter, Customer, Product, Selling, Refilling class GetUserForm(forms.Form): """ @@ -151,7 +151,11 @@ class CounterClick(DetailView): def finish(self, request): """ Finish the click session, and validate the basket """ - # TODO: handle the basket + for pid,qty in request.session['basket'].items(): + p = Product.objects.filter(pk=pid).first() + s = Selling(product=p, counter=self.object, unit_price=p.selling_price, + quantity=qty, seller=Counter.get_random_barman(self.object.id), customer=self.customer) + s.save() kwargs = {'counter_id': self.object.id} del request.session['basket'] return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs)) diff --git a/sith/settings.py b/sith/settings.py index 2d3a65db..05a678cf 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -218,6 +218,11 @@ SITH_SUBSCRIPTION_PAYMENT_METHOD = [ ('other', 'Autre'), ] +SITH_COUNTER_PAYMENT_METHOD = [ + ('cheque', 'Chèque'), + ('cash', 'Espèce'), + ] + # Subscription durations are in semestres (should be settingized) SITH_SUBSCRIPTIONS = { 'un-semestre': {