[3d7fc00] | 1 | import datetime
|
---|
| 2 | from django.shortcuts import render, redirect
|
---|
| 3 | from django.contrib.auth import get_user_model
|
---|
| 4 | from django.db.models import Sum, Avg
|
---|
| 5 |
|
---|
| 6 | from app.models import Artikal, Klient, Lagerlist, Stavka, Tipdok, Vraboteni
|
---|
| 7 |
|
---|
| 8 | # Create your views here.
|
---|
| 9 |
|
---|
| 10 | def home_v(request):
|
---|
| 11 | return render(request, 'home.html')
|
---|
| 12 |
|
---|
| 13 | def fakturi_v(request):
|
---|
| 14 | fakturi = Stavka.objects.using('db').all()
|
---|
| 15 | return render(request, 'fakturi.html', {'fakturi':fakturi})
|
---|
| 16 |
|
---|
| 17 | def fakturi_delete_v(request, id):
|
---|
| 18 | stavka = Stavka.objects.using('db').filter(stavkaid=id)
|
---|
| 19 | stavka.delete()
|
---|
| 20 | return redirect('fakturi')
|
---|
| 21 |
|
---|
| 22 | def fakturi_edit_v(request, id):
|
---|
| 23 | if (request.method != "POST"):
|
---|
| 24 | try:
|
---|
| 25 | stavka = Stavka.objects.using('db').get(stavkaid=id)
|
---|
| 26 | except Stavka.DoesNotExist:
|
---|
| 27 | stavka = None
|
---|
| 28 | artikli = Lagerlist.objects.using('db').filter(stavkaid=id)
|
---|
| 29 | items = Artikal.objects.using('db').all()
|
---|
| 30 | klienti = Klient.objects.using('db').all()
|
---|
| 31 | return render(request, 'fakturi_edit_form.html', {'artikli':artikli, 'stavka':stavka, 'items':items, 'klienti':klienti})
|
---|
| 32 | else:
|
---|
| 33 | lagerlist_dict = []
|
---|
| 34 | lagerlist_updated = []
|
---|
| 35 | totalbezddv = 0
|
---|
| 36 | totalsoddv = 0
|
---|
| 37 |
|
---|
| 38 | for i, element in enumerate(request.POST.getlist("id")):
|
---|
| 39 | lagerlist_dict.append([request.POST.getlist("lagerlist_id")[i],request.POST.getlist("id")[i],request.POST.getlist("kol")[i]])
|
---|
| 40 |
|
---|
| 41 | for cenibezddv in request.POST.getlist("cenabezddv"):
|
---|
| 42 | totalbezddv += float(cenibezddv)
|
---|
| 43 |
|
---|
| 44 | for cenisoddv in request.POST.getlist("cenasoddv"):
|
---|
| 45 | totalsoddv += float(cenisoddv)
|
---|
| 46 |
|
---|
| 47 | stavka_post = request.POST['stavka']
|
---|
| 48 | stavka_edit = Stavka.objects.using('db').get(stavkaid=stavka_post)
|
---|
| 49 |
|
---|
| 50 | stavka_edit.broj = request.POST['broj']
|
---|
| 51 | stavka_edit.klientid = Klient(request.POST['klient'])
|
---|
| 52 | stavka_edit.datum = request.POST['datum']
|
---|
| 53 | stavka_edit.cena_osnova = totalbezddv
|
---|
| 54 | stavka_edit.cena_vkupno = totalsoddv
|
---|
| 55 | stavka_edit.cena_ddv = totalsoddv - totalbezddv
|
---|
| 56 | stavka_edit.save()
|
---|
| 57 |
|
---|
| 58 | for item in lagerlist_dict:
|
---|
| 59 | try:
|
---|
| 60 | lagerlist_edit = Lagerlist.objects.using('db').get(lagerlist_id=item[0])
|
---|
| 61 | lagerlist_edit.artikal = Artikal(item[1])
|
---|
| 62 | lagerlist_edit.kolicina = item[2]
|
---|
| 63 | lagerlist_updated.append(lagerlist_edit)
|
---|
| 64 | lagerlist_edit.save()
|
---|
| 65 | except ValueError:
|
---|
| 66 | avg_cena = Artikal.objects.using('db').get(artikal_id=item[1]).cena
|
---|
| 67 | lagerlist_edit = Lagerlist(stavkaid=Stavka(id), artikal=Artikal(item[1]), kolicina=item[2], avg_cena=avg_cena)
|
---|
| 68 | lagerlist_updated.append(lagerlist_edit)
|
---|
| 69 | lagerlist_edit.save(using="db")
|
---|
| 70 |
|
---|
| 71 | data_in_lager = Lagerlist.objects.using('db').filter(stavkaid=id)
|
---|
| 72 | print(data_in_lager)
|
---|
| 73 |
|
---|
| 74 | for item_in_db in data_in_lager:
|
---|
| 75 | if item_in_db not in lagerlist_updated:
|
---|
| 76 | item_in_db.delete()
|
---|
| 77 |
|
---|
| 78 | return redirect('fakturi')
|
---|
| 79 |
|
---|
| 80 | def fakturi_new_v(request):
|
---|
| 81 | if (request.method != "POST"):
|
---|
| 82 | items = Artikal.objects.using('db').all()
|
---|
| 83 | klienti = Klient.objects.using('db').all()
|
---|
| 84 |
|
---|
| 85 | return render(request, 'fakturi_new_form.html', {'klienti':klienti, 'items':items})
|
---|
| 86 | else:
|
---|
| 87 | totalbezddv = 0
|
---|
| 88 | totalsoddv = 0
|
---|
| 89 |
|
---|
| 90 | for cenibezddv in request.POST.getlist("cenabezddv"):
|
---|
| 91 | totalbezddv += float(cenibezddv)
|
---|
| 92 |
|
---|
| 93 | for cenisoddv in request.POST.getlist("cenasoddv"):
|
---|
| 94 | totalsoddv += float(cenisoddv)
|
---|
| 95 |
|
---|
| 96 | stavka_new = Stavka(
|
---|
| 97 | tipid=Tipdok(10),
|
---|
| 98 | created_by=Vraboteni(1), datum=request.POST['datum'],
|
---|
| 99 | broj=request.POST['broj'], klientid=Klient(request.POST['klient']),
|
---|
| 100 | cena_osnova=totalbezddv,
|
---|
| 101 | cena_vkupno=totalsoddv,
|
---|
| 102 | cena_ddv=totalsoddv - totalbezddv)
|
---|
| 103 | stavka_new.save(using='db')
|
---|
| 104 |
|
---|
| 105 | lagerlist_dict = []
|
---|
| 106 |
|
---|
| 107 | print(request.POST)
|
---|
| 108 |
|
---|
| 109 | for i, element in enumerate(request.POST.getlist("id")):
|
---|
| 110 | lagerlist_dict.append([request.POST.getlist("id")[i],request.POST.getlist("kol")[i]])
|
---|
| 111 |
|
---|
| 112 | for item in lagerlist_dict:
|
---|
| 113 | lagerlist_new = Lagerlist()
|
---|
| 114 | lagerlist_new.stavkaid = stavka_new
|
---|
| 115 | lagerlist_new.artikal = Artikal(item[0])
|
---|
| 116 | lagerlist_new.kolicina = item[1]
|
---|
| 117 | lagerlist_new.avg_cena = 100
|
---|
| 118 | lagerlist_new.save(using='db')
|
---|
| 119 |
|
---|
| 120 | return redirect('fakturi')
|
---|
| 121 |
|
---|
| 122 | def lagerlist_v(request):
|
---|
| 123 | datum = request.GET.get('datum')
|
---|
| 124 |
|
---|
| 125 | if datum:
|
---|
| 126 | 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')
|
---|
| 127 | else:
|
---|
| 128 | lagerlist = Lagerlist.objects.values('artikal__artikal_ime').annotate(total_kol=Sum('kolicina'), avg_cena=Avg('avg_cena')).using('db').all()
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | return render(request, 'lagerlist.html', {'lagerlist':lagerlist}) |
---|