gestioCOF/journaldecaisse/views.py

76 lines
2.3 KiB
Python
Raw Permalink Normal View History

2020-02-16 18:37:16 +01:00
from django.http import HttpResponse
2020-02-19 18:43:42 +01:00
from .models import JournalEntry, Produit, ProduitStock
2020-02-17 11:40:58 +01:00
from django.shortcuts import render
2020-02-18 18:25:54 +01:00
from django.http import JsonResponse
2020-02-19 18:43:42 +01:00
import datetime
global_filter = None
2020-02-16 18:37:16 +01:00
def index(request):
2020-02-17 11:40:58 +01:00
entry_list = JournalEntry.objects.order_by('entry_date')
context = {'entry_list': entry_list}
2020-02-18 18:25:54 +01:00
return render(request, 'journaldecaisse/index.html', context)
def vente(request):
2020-02-19 18:43:42 +01:00
print(request.user.first_name)
global global_filter
product_list = Produit.objects.all()
global_filter = product_list
product_list = product_list.values_list()
2020-02-18 18:25:54 +01:00
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):
2020-02-18 22:02:46 +01:00
2020-02-19 18:43:42 +01:00
global global_filter
elem_selected = None
elem_price = None
elem_stock = None
2020-02-18 18:25:54 +01:00
options_selected = request.GET.get('option', None)
options_selected = options_selected.split(":")
2020-02-18 22:02:46 +01:00
field = str(Produit._meta.get_fields()[int(options_selected[2])+3]).split(".")[-1]
2020-02-19 18:43:42 +01:00
elems = global_filter.filter(**{options_selected[0]: options_selected[1]})
global_filter = elems
2020-02-18 22:02:46 +01:00
elems = elems.values_list()
options = []
for elem in elems:
options.append(elem[int(options_selected[2])+2])
options = list(set(options))
2020-02-19 18:43:42 +01:00
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
2020-02-18 18:25:54 +01:00
data = {
2020-02-18 22:02:46 +01:00
'options': options,
'options_int': int(options_selected[2])+2,
2020-02-19 18:43:42 +01:00
'field': field,
'elem_selected': elem_selected,
'price': elem_price,
'stock': elem_stock,
'name': request.user.first_name
2020-02-18 18:25:54 +01:00
}
return JsonResponse(data)
2020-02-19 18:43:42 +01:00
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"})