3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-10-05 19:12:09 +00:00

Automatic creation of products when creating a keg

This commit is contained in:
Yoann Pétri 2019-06-23 16:32:38 +02:00
parent 55e5efa38a
commit 19c56ca267
4 changed files with 118 additions and 11 deletions

View file

@ -1,6 +1,8 @@
from django import forms
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator
from dal import autocomplete
@ -44,17 +46,21 @@ class KegForm(forms.ModelForm):
"""
A form to create and edit a :class:`~gestion.models.Keg`.
"""
def __init__(self, *args, **kwargs):
super(KegForm, self).__init__(*args, **kwargs)
self.fields['pinte'].queryset = Product.objects.filter(draft_category=Product.DRAFT_PINTE)
self.fields['demi'].queryset = Product.objects.filter(draft_category=Product.DRAFT_DEMI)
self.fields['galopin'].queryset = Product.objects.filter(draft_category=Product.DRAFT_GALOPIN)
class Meta:
model = Keg
fields = "__all__"
fields = ["name", "stockHold", "barcode", "amount", "capacity"]
widgets = {'amount': forms.TextInput}
category = forms.ModelChoiceField(queryset=Category.objects.all(), label="Catégorie")
deg = forms.DecimalField(max_digits=5, decimal_places=2, label="Degré", validators=[MinValueValidator(0)])
create_galopin = forms.BooleanField(label="Créer le produit galopin ?")
def clean(self):
cleaned_data = super().clean()
if cleaned_data.get("name")[0:4] != "Fût ":
raise ValidationError("Le nom du fût doit être sous la forme 'Fût nom de la bière'")
class MenuForm(forms.ModelForm):
"""
A form to create and edit a :class:`~gestion.models.Menu`.

View file

@ -0,0 +1,43 @@
# Generated by Django 2.1 on 2019-06-23 14:23
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('gestion', '0009_auto_20190506_0939'),
]
operations = [
migrations.AlterField(
model_name='historicalkeg',
name='name',
field=models.CharField(db_index=True, max_length=255, verbose_name='Nom'),
),
migrations.AlterField(
model_name='historicalproduct',
name='barcode',
field=models.CharField(db_index=True, max_length=255, verbose_name='Code barre'),
),
migrations.AlterField(
model_name='historicalproduct',
name='name',
field=models.CharField(db_index=True, max_length=255, verbose_name='Nom'),
),
migrations.AlterField(
model_name='keg',
name='name',
field=models.CharField(max_length=255, unique=True, verbose_name='Nom'),
),
migrations.AlterField(
model_name='product',
name='barcode',
field=models.CharField(max_length=255, unique=True, verbose_name='Code barre'),
),
migrations.AlterField(
model_name='product',
name='name',
field=models.CharField(max_length=255, unique=True, verbose_name='Nom'),
),
]

View file

@ -46,7 +46,7 @@ class Product(models.Model):
class Meta:
verbose_name = "Produit"
name = models.CharField(max_length=40, verbose_name="Nom", unique=True)
name = models.CharField(max_length=255, verbose_name="Nom", unique=True)
"""
The name of the product.
"""
@ -62,7 +62,7 @@ class Product(models.Model):
"""
Number of product at the bar.
"""
barcode = models.CharField(max_length=20, unique=True, verbose_name="Code barre")
barcode = models.CharField(max_length=255, unique=True, verbose_name="Code barre")
"""
The barcode of the product.
"""
@ -162,7 +162,7 @@ class Keg(models.Model):
("close_keg", "Peut fermer les fûts")
)
name = models.CharField(max_length=20, unique=True, verbose_name="Nom")
name = models.CharField(max_length=255, unique=True, verbose_name="Nom")
"""
The name of the keg.
"""

View file

@ -25,7 +25,7 @@ from math import floor, ceil
from .forms import ReloadForm, RefundForm, ProductForm, KegForm, MenuForm, GestionForm, SearchMenuForm, SearchProductForm, SelectPositiveKegForm, SelectActiveKegForm, PinteForm, GenerateReleveForm, CategoryForm, SearchCategoryForm, GenerateInvoiceForm, ComputePriceForm
from .models import Product, Menu, Keg, ConsumptionHistory, KegHistory, Consumption, MenuHistory, Pinte, Reload, Refund, Category
from users.models import School
from preferences.models import PaymentMethod, GeneralPreferences, Cotisation, DivideHistory
from preferences.models import PaymentMethod, GeneralPreferences, Cotisation, DivideHistory, PriceProfile
from users.models import CotisationHistory
@active_required
@ -452,7 +452,65 @@ def addKeg(request):
Displays a :class:`gestion.forms.KegForm` to add a :class:`gestion.models.Keg`.
"""
form = KegForm(request.POST or None)
if(form.is_valid()):
if form.is_valid():
keg = form.save(commit=False)
price_profile = get_object_or_404(PriceProfile, use_for_draft=True)
pinte_price = compute_price(form.cleaned_data["amount"]/(2*form.cleaned_data["capacity"]), price_profile.a, price_profile.b, price_profile.c, price_profile.alpha)
pinte_price = ceil(10*pinte_price)/10
name = form.cleaned_data["name"][4:]
create_galopin = form.cleaned_data["create_galopin"]
pinte = Product(
name = "Pinte " + name,
amount = pinte_price,
stockHold = 0,
stockBar = 0,
barcode = "pinte_" + form.cleaned_data["barcode"],
category = form.cleaned_data["category"],
needQuantityButton = False,
is_active = True,
volume = 50,
deg = form.cleaned_data["deg"],
adherentRequired = True,
showingMultiplier = 1,
draft_category = Product.DRAFT_PINTE
)
pinte.save()
keg.pinte = pinte
demi = Product(
name = "Demi " + name,
amount = ceil(5*pinte_price)/10,
stockHold = 0,
stockBar = 0,
barcode = "demi_" + form.cleaned_data["barcode"],
category = form.cleaned_data["category"],
needQuantityButton = False,
is_active = True,
volume = 25,
deg = form.cleaned_data["deg"],
adherentRequired = True,
showingMultiplier = 1,
draft_category = Product.DRAFT_DEMI
)
demi.save()
keg.demi = demi
if create_galopin:
galopin = Product(
name = "Galopin " + name,
amount = ceil(2.5 * pinte_price)/10,
stockHold = 0,
stockBar = 0,
barcode = "galopin_" + form.cleaned_data["barcode"],
category = form.cleaned_data["category"],
needQuantityButton = False,
is_active = True,
volume = 13,
deg = form.cleaned_data["deg"],
adherentRequired = True,
showingMultiplier = 1,
draft_category = Product.DRAFT_DEMI
)
galopin.save()
keg.galopin = galopin
keg = form.save()
messages.success(request, "Le fût " + keg.name + " a bien été ajouté")
return redirect(reverse('gestion:kegsList'))