import datetime from django.shortcuts import render, redirect from django.contrib.auth import get_user_model from django.db.models import Sum, Avg from app.models import Artikal, Klient, Lagerlist, Stavka, Tipdok, Vraboteni # Create your views here. def home_v(request): return render(request, 'home.html') def fakturi_v(request): fakturi = Stavka.objects.using('db').all() return render(request, 'fakturi.html', {'fakturi':fakturi}) def fakturi_delete_v(request, id): stavka = Stavka.objects.using('db').filter(stavkaid=id) stavka.delete() return redirect('fakturi') def fakturi_edit_v(request, id): if (request.method != "POST"): try: stavka = Stavka.objects.using('db').get(stavkaid=id) except Stavka.DoesNotExist: stavka = None artikli = Lagerlist.objects.using('db').filter(stavkaid=id) items = Artikal.objects.using('db').all() klienti = Klient.objects.using('db').all() return render(request, 'fakturi_edit_form.html', {'artikli':artikli, 'stavka':stavka, 'items':items, 'klienti':klienti}) else: lagerlist_dict = [] lagerlist_updated = [] totalbezddv = 0 totalsoddv = 0 for i, element in enumerate(request.POST.getlist("id")): lagerlist_dict.append([request.POST.getlist("lagerlist_id")[i],request.POST.getlist("id")[i],request.POST.getlist("kol")[i]]) for cenibezddv in request.POST.getlist("cenabezddv"): totalbezddv += float(cenibezddv) for cenisoddv in request.POST.getlist("cenasoddv"): totalsoddv += float(cenisoddv) stavka_post = request.POST['stavka'] stavka_edit = Stavka.objects.using('db').get(stavkaid=stavka_post) stavka_edit.broj = request.POST['broj'] stavka_edit.klientid = Klient(request.POST['klient']) stavka_edit.datum = request.POST['datum'] stavka_edit.cena_osnova = totalbezddv stavka_edit.cena_vkupno = totalsoddv stavka_edit.cena_ddv = totalsoddv - totalbezddv stavka_edit.save() for item in lagerlist_dict: try: lagerlist_edit = Lagerlist.objects.using('db').get(lagerlist_id=item[0]) lagerlist_edit.artikal = Artikal(item[1]) lagerlist_edit.kolicina = item[2] lagerlist_updated.append(lagerlist_edit) lagerlist_edit.save() except ValueError: avg_cena = Artikal.objects.using('db').get(artikal_id=item[1]).cena lagerlist_edit = Lagerlist(stavkaid=Stavka(id), artikal=Artikal(item[1]), kolicina=item[2], avg_cena=avg_cena) lagerlist_updated.append(lagerlist_edit) lagerlist_edit.save(using="db") data_in_lager = Lagerlist.objects.using('db').filter(stavkaid=id) print(data_in_lager) for item_in_db in data_in_lager: if item_in_db not in lagerlist_updated: item_in_db.delete() return redirect('fakturi') def fakturi_new_v(request): if (request.method != "POST"): items = Artikal.objects.using('db').all() klienti = Klient.objects.using('db').all() return render(request, 'fakturi_new_form.html', {'klienti':klienti, 'items':items}) else: totalbezddv = 0 totalsoddv = 0 for cenibezddv in request.POST.getlist("cenabezddv"): totalbezddv += float(cenibezddv) for cenisoddv in request.POST.getlist("cenasoddv"): totalsoddv += float(cenisoddv) stavka_new = Stavka( tipid=Tipdok(10), created_by=Vraboteni(1), datum=request.POST['datum'], broj=request.POST['broj'], klientid=Klient(request.POST['klient']), cena_osnova=totalbezddv, cena_vkupno=totalsoddv, cena_ddv=totalsoddv - totalbezddv) stavka_new.save(using='db') lagerlist_dict = [] print(request.POST) for i, element in enumerate(request.POST.getlist("id")): lagerlist_dict.append([request.POST.getlist("id")[i],request.POST.getlist("kol")[i]]) for item in lagerlist_dict: lagerlist_new = Lagerlist() lagerlist_new.stavkaid = stavka_new lagerlist_new.artikal = Artikal(item[0]) lagerlist_new.kolicina = item[1] lagerlist_new.avg_cena = 100 lagerlist_new.save(using='db') return redirect('fakturi') def lagerlist_v(request): datum = request.GET.get('datum') if datum: lagerlist = Lagerlist.objects.values('artikal__artikal_ime').filter(stavkaid__in=Stavka.objects.filter(datum__lte=datum).values('stavkaid').using('db')).annotate(total_kol=Sum('kolicina'), avg_cena=Avg('avg_cena')).using('db') else: lagerlist = Lagerlist.objects.values('artikal__artikal_ime').annotate(total_kol=Sum('kolicina'), avg_cena=Avg('avg_cena')).using('db').all() return render(request, 'lagerlist.html', {'lagerlist':lagerlist})