Browse Source

Implement price computing

pull/1/head
Yoann Piétri 7 years ago
parent
commit
55e5efa38a
  1. 6
      coopeV3/utils.py
  2. 9
      gestion/forms.py
  3. 1
      gestion/urls.py
  4. 16
      gestion/views.py
  5. 3
      templates/nav.html

6
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)

9
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):
"""
@ -137,3 +137,10 @@ class GenerateInvoiceForm(forms.Form):
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")
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")

1
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"),
]

16
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
@ -1065,3 +1067,15 @@ def stats(request):
"payment_methods": payment_methods,
"cotisations": cotisations,
})
########## 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})

3
templates/nav.html

@ -61,6 +61,9 @@
<span class="tabulation2">
<i class="fa fa-search-dollar"></i> <a href="{% url 'preferences:priceProfilesIndex' %}">Profils de prix</a>
</span>
<span class="tabulation2">
<i class="fa fa-search-dollar"></i> <a href="{% url 'gestion:compute-price' %}">Calcul de prix</a>
</span>
{% endif %}
<span class="tabulation2">
<i class="fa fa-bed"></i> <a href="{% url 'users:logout' %}">Deconnexion</a>

Loading…
Cancel
Save