gestioCOF/journaldecaisse/views.py
2020-02-19 18:43:42 +01:00

75 lines
2.3 KiB
Python

from django.http import HttpResponse
from .models import JournalEntry, Produit, ProduitStock
from django.shortcuts import render
from django.http import JsonResponse
import datetime
global_filter = None
def index(request):
entry_list = JournalEntry.objects.order_by('entry_date')
context = {'entry_list': entry_list}
return render(request, 'journaldecaisse/index.html', context)
def vente(request):
print(request.user.first_name)
global global_filter
product_list = Produit.objects.all()
global_filter = product_list
product_list = product_list.values_list()
products = []
for elem in product_list:
products.append(elem[1])
products = list(set(products))
context = {"product_list" : products}
return render(request, 'journaldecaisse/vente.html', context)
def iterate_options(request):
global global_filter
elem_selected = None
elem_price = None
elem_stock = None
options_selected = request.GET.get('option', None)
options_selected = options_selected.split(":")
field = str(Produit._meta.get_fields()[int(options_selected[2])+3]).split(".")[-1]
elems = global_filter.filter(**{options_selected[0]: options_selected[1]})
global_filter = elems
elems = elems.values_list()
options = []
for elem in elems:
options.append(elem[int(options_selected[2])+2])
options = list(set(options))
if global_filter.count() == 1:
elem_selected = str(global_filter.first())
elem_price = global_filter.first().price
elem_stock = ProduitStock.objects.get(ProduitTaille=global_filter.first()).Amount
data = {
'options': options,
'options_int': int(options_selected[2])+2,
'field': field,
'elem_selected': elem_selected,
'price': elem_price,
'stock': elem_stock,
'name': request.user.first_name
}
return JsonResponse(data)
def submit_entry(request):
product = global_filter.first()
buyer = request.GET.get('buyer', None)
payment = request.GET.get('payment', None)
author = request.user.first_name
JournalEntry.objects.create(entry_date=datetime.datetime.now(), entry_text=str(product) + " " + buyer, cofeux_id=author, entry_amount=product.price, payment_type=payment)
return JsonResponse({"success":"True"})