diff --git a/coopeV3/utils.py b/coopeV3/utils.py index d732603..d32e4b4 100644 --- a/coopeV3/utils.py +++ b/coopeV3/utils.py @@ -1,7 +1,7 @@ import math def compute_price(price, a, b, c, alpha): - if price < a: - return price * (a + b * math.exp(-c/(price-alpha)**2)) + if price < alpha: + return float(price) * (1 + float(a) + float(b) * math.exp(-c/(price-alpha)**2)) else: - return price * a + return price * (1 + a) diff --git a/gestion/forms.py b/gestion/forms.py index 31bfcf6..546b622 100644 --- a/gestion/forms.py +++ b/gestion/forms.py @@ -5,7 +5,7 @@ from django.contrib.auth.models import User from dal import autocomplete from .models import Reload, Refund, Product, Keg, Menu, Category -from preferences.models import PaymentMethod +from preferences.models import PaymentMethod, PriceProfile class ReloadForm(forms.ModelForm): """ @@ -136,4 +136,11 @@ class GenerateInvoiceForm(forms.Form): client_name = forms.CharField(label="Nom du client") client_address_fisrt_line = forms.CharField(label="Première ligne d'adresse") client_address_second_line = forms.CharField(label="Deuxième ligne d'adresse") - products = forms.CharField(widget=forms.Textarea, label="Produits", help_text="Au format nom;prix;quantité avec saut de ligne") \ No newline at end of file + products = forms.CharField(widget=forms.Textarea, label="Produits", help_text="Au format nom;prix;quantité avec saut de ligne") + +class ComputePriceForm(forms.Form): + """ + A form to compute price + """ + price_profile = forms.ModelChoiceField(queryset=PriceProfile.objects.all(), label="Profil de prix") + price = forms.DecimalField(max_digits=10, decimal_places=5, label="Prix") \ No newline at end of file diff --git a/gestion/urls.py b/gestion/urls.py index 1699087..01e846a 100644 --- a/gestion/urls.py +++ b/gestion/urls.py @@ -55,4 +55,5 @@ urlpatterns = [ path('stats', views.stats, name="stats"), path('divide', views.divide, name="divide"), path('gen_invoice', views.gen_invoice, name="gen_invoice"), + path('compute-price', views.compute_price_view, name="compute-price"), ] \ No newline at end of file diff --git a/gestion/views.py b/gestion/views.py index ffa8ef6..a079810 100644 --- a/gestion/views.py +++ b/gestion/views.py @@ -14,13 +14,15 @@ from datetime import datetime, timedelta from django_tex.views import render_to_pdf from coopeV3.acl import active_required, acl_or, admin_required +from coopeV3.utils import compute_price import simplejson as json from dal import autocomplete from decimal import * import os +from math import floor, ceil -from .forms import ReloadForm, RefundForm, ProductForm, KegForm, MenuForm, GestionForm, SearchMenuForm, SearchProductForm, SelectPositiveKegForm, SelectActiveKegForm, PinteForm, GenerateReleveForm, CategoryForm, SearchCategoryForm, GenerateInvoiceForm +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 @@ -1064,4 +1066,16 @@ def stats(request): "menus": menus, "payment_methods": payment_methods, "cotisations": cotisations, - }) \ No newline at end of file + }) + +########## Compute price ########## + +def compute_price_view(request): + form = ComputePriceForm(request.POST or None) + if form.is_valid(): + price_profile = form.cleaned_data["price_profile"] + price = compute_price(form.cleaned_data["price"], price_profile.a, price_profile.b, price_profile.c, price_profile.alpha) + form_p = "Le prix est " + str(ceil(100*price)/100) + " € (arrondi au centième) ou " + str(ceil(10*price)/10) + " € (arrondi au dixième)." + else: + form_p = "" + return render(request, "form.html", {"form": form, "form_title": "Calcul d'un prix", "form_button": "Calculer", "form_icon": "search_dollar", "form_p": form_p}) \ No newline at end of file diff --git a/templates/nav.html b/templates/nav.html index 0cd31f6..e31ee29 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -61,6 +61,9 @@ Profils de prix + + Calcul de prix + {% endif %} Deconnexion