[3d7fc00] | 1 | from django.shortcuts import render, redirect
|
---|
| 2 | from django.contrib.auth import get_user_model
|
---|
| 3 |
|
---|
| 4 | from app.models import Artikal, Klient, Lagerlist, Stavka
|
---|
| 5 |
|
---|
| 6 | # Create your views here.
|
---|
| 7 |
|
---|
| 8 | def home_v(request):
|
---|
| 9 | return render(request, 'home.html')
|
---|
| 10 |
|
---|
| 11 | def fakturi_v(request):
|
---|
| 12 | fakturi = Stavka.objects.using('db').all()
|
---|
| 13 | return render(request, 'fakturi.html', {'fakturi':fakturi})
|
---|
| 14 |
|
---|
| 15 | def fakturi_edit_v(request, id):
|
---|
| 16 | if (request.method != "POST"):
|
---|
| 17 | try:
|
---|
| 18 | stavka = Stavka.objects.using('db').get(stavkaid=id)
|
---|
| 19 | except Stavka.DoesNotExist:
|
---|
| 20 | stavka = None
|
---|
| 21 | artikli = Lagerlist.objects.using('db').filter(stavkaid=id)
|
---|
| 22 | items = Artikal.objects.using('db').all()
|
---|
| 23 | klienti = Klient.objects.using('db').all()
|
---|
| 24 | return render(request, 'fakturi_edit_form.html', {'artikli':artikli, 'stavka':stavka, 'items':items, 'klienti':klienti})
|
---|
| 25 | else:
|
---|
| 26 | lagerlist_dict = []
|
---|
| 27 | print(request.POST)
|
---|
| 28 | for i, element in enumerate(request.POST.getlist("id")):
|
---|
| 29 | lagerlist_dict.append([request.POST.getlist("lagerlist_id")[i],request.POST.getlist("id")[i],request.POST.getlist("kol")[i]])
|
---|
| 30 |
|
---|
| 31 | stavka_post = request.POST['stavka']
|
---|
| 32 | stavka_edit = Stavka.objects.using('db').get(stavkaid=stavka_post)
|
---|
| 33 |
|
---|
| 34 | stavka_edit.broj = request.POST['broj']
|
---|
| 35 | stavka_edit.klientid = Klient(request.POST['klient'])
|
---|
| 36 | stavka_edit.datum = request.POST['datum']
|
---|
| 37 | stavka_edit.save()
|
---|
| 38 |
|
---|
| 39 | data_in_lager = Lagerlist.objects.using('db').filter(stavkaid=id)
|
---|
| 40 |
|
---|
| 41 | for item in lagerlist_dict:
|
---|
| 42 | print(item)
|
---|
| 43 | try:
|
---|
| 44 | lagerlist_edit = Lagerlist.objects.using('db').get(lagerlist_id=item[0])
|
---|
| 45 | lagerlist_edit.artikal = Artikal(item[1])
|
---|
| 46 | lagerlist_edit.kolicina = item[2]
|
---|
| 47 | lagerlist_edit.save()
|
---|
| 48 | except ValueError:
|
---|
| 49 | avg_cena = Artikal.objects.using('db').get(artikal_id=item[1]).cena
|
---|
| 50 | lagerlist_edit = Lagerlist(stavkaid=Stavka(id), artikal=Artikal(item[1]), kolicina=item[2], avg_cena=avg_cena)
|
---|
| 51 | lagerlist_edit.save(using="db")
|
---|
| 52 |
|
---|
| 53 | return redirect('fakturi')
|
---|
| 54 |
|
---|
| 55 | def fakturi_new_v(request):
|
---|
| 56 | items = Artikal.objects.using('db').all()
|
---|
| 57 | klienti = Klient.objects.using('db').all()
|
---|
| 58 |
|
---|
| 59 | return render(request, 'fakturi_new_form.html', {'klienti':klienti, 'items':items}) |
---|