-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
605 lines (497 loc) · 19.6 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
from django.shortcuts import render, get_object_or_404, redirect
from .models import Formulari, Anagrafica
from .models import Ripartizioni, Prezzi, Materiali, Riepiloghi
import datetime
from django.http import HttpResponse
from django.db.models import Sum, Q, Count, OuterRef, Subquery
from django.db import models
from django.contrib.auth.decorators import login_required
from .forms import FormulariForm, AnagraficaForm, PrezziForm
from .forms import RipartizioniForm, MaterialiForm, RiepiloghiForm
# from random import randint
# from django.views.generic import TemplateView
# from django.utils import timezone
# from django.template import Context
# from itertools import chain
# from django.views.generic.edit import CreateView
# from django.db.models import Avg, Max, Min, F
# from django.views.generic import View
# prove grafici
from django.http import JsonResponse
from formulari.utils import render_to_pdf
# Prove email
from django.core.mail import send_mail, EmailMessage
# Prove
def search_formulari(request):
string = request.GET.get('searchbar')
form = Formulari.objects.get(cod=string)
return render(request, 'formulari/formulari_details.html', {'form': form})
def search_cod(request):
res = []
string = request.GET['string']
result = Formulari.objects.filter(cod__icontains=string)
vals = result.values_list('cod')
for val in vals:
for i in val:
res.append(i)
return JsonResponse(res, safe=False)
def duplicati(request):
res = []
result = Formulari.objects.values_list('cod').annotate(
Count('id')).order_by().filter(id__count__gt=1)
for val in result:
for i in val:
res.append(i)
return JsonResponse(res, safe=False)
def sendsome(request):
send_mail(
'Subject here',
'Here is the message.',
['[email protected]'],
fail_silently=False,
)
return redirect('riepiloghi_list')
def send_riepilogo(request, pk):
dest = Anagrafica.objects.get(pk=pk)
attachment = Riepiloghi.objects.get(pk=pk)
eml = EmailMessage(
'Oggetto',
'In allegato il riepilogo',
[dest.mail],
)
attachment = './' + str(attachment.doc)
eml.attach_file(attachment)
eml.send(fail_silently=False)
return redirect('formulari_list')
# Create your views here.
@login_required(login_url='/login/')
def formulari_list_all(request):
formulari = Formulari.objects.order_by('-data')
return render(request, 'formulari/formulari_list.html', {'formularis': formulari})
@login_required(login_url='/login/')
def formulari_list(request):
formulari = Formulari.objects.filter(
~Q(stato='CH'), ~Q(stato='IA')).order_by('-data')
return render(request, 'formulari/formulari_list.html', {'formularis': formulari})
@login_required(login_url='/login/')
def formulari_new(request):
if request.method == "POST":
form = FormulariForm(request.POST)
if form.is_valid():
formulario = form.save(commit=False)
formulario.autore = request.user
formulario.imp = formulario.prez * formulario.qu
formulario.save()
return redirect('formulari_detail', pk=formulario.pk)
else:
form = FormulariForm()
return render(request, 'formulari/formulari_edit.html', {'form': form})
# Inseriemnto di Formulari Simili o consecutivi
@login_required(login_url='/login/')
def formulari_like(request, rif):
data_obj = Formulari.objects.filter(pk=rif)
data = data_obj.values('comm', 'cod', 'mat', 'data', 'prez', 'ripa')
if request.method == "POST":
form = FormulariForm(request.POST)
if form.is_valid():
formulario = form.save(commit=False)
formulario.autore = request.user
formulario.imp = formulario.prez * formulario.qu
formulario.save()
return redirect('formulari_detail', pk=formulario.pk)
else:
form = FormulariForm(initial=data[0])
return render(request, 'formulari/formulari_edit.html', {'form': form})
@login_required(login_url='/login/')
def formulari_detail(request, pk):
form = get_object_or_404(Formulari, pk=pk)
return render(request, 'formulari/formulari_details.html', {'form': form})
@login_required(login_url='/login/')
def formulari_edit(request, pk):
formulario = get_object_or_404(Formulari, pk=pk)
if request.method == "POST":
form = FormulariForm(request.POST, instance=formulario)
if form.is_valid():
formulario = form.save(commit=False)
formulario.autore = request.user
formulario.imp = formulario.prez * formulario.qu
formulario.save()
return redirect('formulari_detail', pk=formulario.pk)
else:
form = FormulariForm(instance=formulario)
return render(request, 'formulari/formulari_edit.html', {'form': form})
@login_required(login_url='/login/')
def formulari_delete(request, pk):
form = get_object_or_404(Formulari, pk=pk)
Formulari.objects.filter(pk=form.pk).delete()
formulari = Formulari.objects.order_by('data')
return render(request, 'formulari/formulari_list.html', {'formularis': formulari})
@login_required(login_url='/login/')
def formulari_delete_all(request):
Formulari.objects.all().delete()
return redirect('formulari_list')
@login_required(login_url='/login/')
def formulari_change(request, pk, cod):
fo = get_object_or_404(Formulari, pk=pk)
fo.stato = cod
fo.save()
return redirect('formulari_detail', pk=fo.pk)
@login_required(login_url='/login/')
def formulari_orfani(request):
formulari = Formulari.objects.filter(riepi=None).order_by('-data')
return render(request, 'formulari/formulari_list.html', {'formularis': formulari})
# Anagrafica
@login_required(login_url='/login/')
def anagrafica_list(request):
array = Anagrafica.objects.annotate(doc_ap=Subquery(Anagrafica.objects.filter(pk=OuterRef('pk')).filter(
formulari__stato="AP").annotate(doc_ap=Count('formulari')).values('doc_ap'), output_field=models.IntegerField()))
context = {'arrays': array}
return render(request, 'formulari/anagrafica_list.html', context)
@login_required(login_url='/login/')
def anagrafica_new(request):
if request.method == "POST":
form = AnagraficaForm(request.POST)
if form.is_valid():
anagrafica = form.save(commit=False)
anagrafica.save()
return redirect('anagrafica_detail', pk=anagrafica.pk)
else:
form = AnagraficaForm()
return render(request, 'formulari/anagrafica_edit.html', {'form': form})
@login_required(login_url='/login/')
def anagrafica_edit(request, pk):
record = get_object_or_404(Anagrafica, pk=pk)
if request.method == "POST":
form = AnagraficaForm(request.POST, instance=record)
if form.is_valid():
record = form.save(commit=False)
record.save()
return redirect('anagrafica_detail', pk=record.pk)
else:
form = AnagraficaForm(instance=record)
return render(request, 'formulari/anagrafica_edit.html', {'form': form})
@login_required(login_url='/login/')
def anagrafica_detail(request, pk):
record = get_object_or_404(Anagrafica, pk=pk)
return render(request, 'formulari/anagrafica_details.html', {'record': record})
@login_required(login_url='/login/')
def anagrafica_delete(request, pk):
form = get_object_or_404(Anagrafica, pk=pk)
Anagrafica.objects.filter(pk=form.pk).delete()
return redirect('anagrafica_list')
@login_required(login_url='/login/')
def anagrafica_riepilogo(request, pk):
fqs = Formulari.objects.filter(comm=pk).filter(
stato='AP').order_by('-data')
gks = []
fps = []
gsa = []
for f1 in fqs:
fps.append(f1.pk)
if f1 and f1.ripa not in gks:
gks.append(f1.ripa)
for g1 in gks:
gs = Ripartizioni.objects.filter(formulari__pk__in=fps).filter(
pk=g1.pk).annotate(totale=Sum('formulari__imp')).values('nome', 'totale', 'pk')
appo = dict(gs[0])
gsa.append(appo)
context = {'fqss': fqs}
context['gsas'] = gsa
context['comm'] = pk
return render(request, 'formulari/formulari_ripa.html', context)
@login_required(login_url='/login/')
def anagrafica_rpdf(request, pk):
fqs = Formulari.objects.filter(comm=pk).filter(
stato='AP').order_by('-data')
gks = []
fps = []
gsa = []
for f1 in fqs:
fps.append(f1.pk)
if f1 and f1.ripa not in gks:
gks.append(f1.ripa)
for g1 in gks:
gs = Ripartizioni.objects.filter(pk=g1.pk).filter(formulari__pk__in=fps).annotate(
totale=Sum('formulari__imp')).values('nome', 'totale', 'pk')
appo = dict(gs[0])
gsa.append(appo)
context = {'fqss': fqs}
context['gsas'] = gsa
context['comm'] = Anagrafica.objects.get(id=pk)
context['data'] = datetime.date.today()
pdf = render_to_pdf('pdf/formulari_rpdf.html', context)
return HttpResponse(pdf, content_type='application/pdf')
@login_required(login_url='/login/')
def anagrafica_preview(request, pk):
fqs = Formulari.objects.filter(comm=pk).filter(
stato='AP').order_by('-data')
gks = []
fps = []
gsa = []
for f1 in fqs:
fps.append(f1.pk)
if f1 and f1.ripa not in gks:
gks.append(f1.ripa)
for g1 in gks:
gs = Ripartizioni.objects.filter(pk=g1.pk).filter(formulari__pk__in=fps).annotate(
totale=Sum('formulari__imp')).values('nome', 'totale', 'pk')
appo = dict(gs[0])
gsa.append(appo)
context = {'fqss': fqs}
context['gsas'] = gsa
context['comm'] = Anagrafica.objects.get(id=pk)
context['data'] = datetime.date.today()
pdf = render_to_pdf('pdf/preview_rpdf.html', context)
return HttpResponse(pdf, content_type='application/pdf')
@login_required(login_url='/login/')
def anagrafica_docs(request, pk):
formulari = Formulari.objects.filter(comm=pk).order_by('-data')
return render(request, 'formulari/formulari_list.html', {'formularis': formulari})
# Ripartizioni
@login_required(login_url='/login/')
def ripartizioni_list(request):
array = Ripartizioni.objects.order_by('nome')
return render(request, 'formulari/ripartizioni_list.html', {'arrays': array})
@login_required(login_url='/login/')
def ripartizioni_detail(request, pk):
record = get_object_or_404(Ripartizioni, pk=pk)
# if record:
# num_doc = Formulari.objects.filter(ripa=pk)
return render(request, 'formulari/ripartizioni_details.html', {'record': record})
@login_required(login_url='/login/')
def ripartizioni_new(request):
if request.method == "POST":
form = RipartizioniForm(request.POST)
if form.is_valid():
anagrafica = form.save(commit=False)
anagrafica.save()
return redirect('ripartizioni_detail', pk=anagrafica.pk)
else:
form = RipartizioniForm()
return render(request, 'formulari/ripartizioni_edit.html', {'form': form})
@login_required(login_url='/login/')
def ripartizioni_edit(request, pk):
record = get_object_or_404(Ripartizioni, pk=pk)
if request.method == "POST":
form = RipartizioniForm(request.POST, instance=record)
if form.is_valid():
record = form.save(commit=False)
record.save()
return redirect('ripartizioni_detail', pk=record.pk)
else:
form = RipartizioniForm(instance=record)
return render(request, 'formulari/ripartizioni_edit.html', {'form': form})
@login_required(login_url='/login/')
def ripartizioni_delete(request, pk):
form = get_object_or_404(Ripartizioni, pk=pk)
Ripartizioni.objects.filter(pk=form.pk).delete()
return redirect('ripartizioni_list')
@login_required(login_url='/login/')
def ripartizioni_riepilogo(request, pk):
formulari = Formulari.objects.filter(ripa=pk)
return render(request, 'formulari/formulari_list.html', {'formularis': formulari})
def ripartizioni_bilancio(request, pk):
data = dict()
for i in Materiali.objects.all():
incassi = Formulari.objects.filter(ripa__pk=pk).filter(
mat__pk=i.pk).aggregate(Sum('imp'))
if incassi['imp__sum'] is not None:
data[str(i)] = incassi['imp__sum']
result = {
"labels": data.keys(),
"default": data.values(),
}
return JsonResponse(result)
# Prezzi
@login_required(login_url='/login/')
def prezzi_list(request):
array = Prezzi.objects.order_by('-data')
return render(request, 'formulari/prezzi_list.html', {'arrays': array})
# @login_required(login_url='/login/')
# def prezzi_chart(request):
# prezzi_data = []
# prezzi_chart = []
# prezzi_data = Prezzi.objects.order_by('-data').values_list('data', flat=True)
# prezzi_chart = Prezzi.objects.order_by('-data').values_list('valore', flat=True)
# context = {'prezzi_data': list(prezzi_data)}
# context['prezzi_chart'] = list(prezzi_chart)
# return render(request, 'formulari/prezzi_chart.html', context)
@login_required(login_url='/login/')
def prezzi_new(request):
if request.method == "POST":
form = PrezziForm(request.POST)
if form.is_valid():
record = form.save(commit=False)
record.autore = request.user
record.save()
return redirect('prezzi_list')
else:
form = PrezziForm()
return render(request, 'formulari/prezzi_edit.html', {'form': form})
@login_required(login_url='/login/')
def prezzi_ultimi(request):
values = []
qs = Anagrafica.objects.all()
ms = Materiali.objects.all()
for q1 in qs:
for m1 in ms:
valk = Prezzi.objects.filter(comm__nome=q1).filter(
mat__mat=m1.mat).order_by('-data').values('id')[:1]
if len(valk):
appo = dict(valk[0])
values.append(appo['id'])
array = Prezzi.objects.filter(pk__in=values)
return render(request, 'formulari/prezzi_list.html', {'arrays': array})
# Materiali
@login_required(login_url='/login/')
def materiali_list(request):
array = Materiali.objects.order_by('cod')
return render(request, 'formulari/materiali_list.html', {'arrays': array})
@login_required(login_url='/login/')
def materiali_new(request):
title = "Nuovo Prezzo"
if request.method == "POST":
form = MaterialiForm(request.POST)
context = {'form': form}
context['title'] = title
if form.is_valid():
record = form.save(commit=False)
record.autore = request.user
record.save()
return redirect('materiali_list')
else:
form = MaterialiForm()
context = {'form': form}
context['title'] = title
return render(request, 'formulari/materiali_edit.html', context)
def mat_mod(request):
if request.method == "POST":
form = MaterialiForm(request.POST)
if form.is_valid():
record = form.save(commit=False)
record.autore = request.user
record.save()
return redirect('materiali_list')
else:
form = MaterialiForm()
return render(request, 'formulari/materiali_create_form.html', {'form': form})
def materiali_group_by(request, pk):
array = Formulari.objects.filter(mat=pk).order_by('-data')
return render(request, 'formulari/formulari_list.html', {'formularis': array})
# Riepiloghi
@login_required(login_url='/login/')
def riepiloghi_list(request):
array = Riepiloghi.objects.annotate(
totale=Sum('formulari__imp')).order_by('-data')
return render(request, 'formulari/riepiloghi_list.html', {'arrays': array})
@login_required(login_url='/login/')
def riepiloghi_f_anagrafica(request, pk):
array = Riepiloghi.objects.filter(comm__pk=pk).annotate(
totale=Sum('formulari__imp')).order_by('-data')
return render(request, 'formulari/riepiloghi_list.html', {'arrays': array})
@login_required(login_url='/login/')
def riepiloghi_details(request, pk):
formulari = Formulari.objects.filter(riepi=pk).order_by('-data')
context = {'formularis': formulari}
context['riepilogo'] = pk
return render(request, 'formulari/riepiloghi_detail_list.html', context)
@login_required(login_url='/login/')
def riepiloghi_delete(request, pk):
form = get_object_or_404(Riepiloghi, pk=pk)
Riepiloghi.objects.filter(pk=form.pk).delete()
return redirect('riepiloghi_list')
@login_required(login_url='/login/')
def riepiloghi_generate(request, pk):
nr = Riepiloghi(comm=Anagrafica.objects.get(
pk=pk), data=datetime.date.today(), autore=request.user, stato='PR')
nr.save()
fqs = Formulari.objects.filter(comm=pk).filter(stato='AP')
fqs.update(riepi=nr.pk, stato='RP')
for step in fqs:
step.save()
return redirect('riepiloghi_details', pk=nr.pk)
@login_required(login_url='/login/')
def riepiloghi_change(request, pk, cod):
riep = get_object_or_404(Riepiloghi, pk=pk)
riep.stato = cod
riep.save()
if cod == 'PA':
fqs = Formulari.objects.filter(riepi=riep.pk)
fqs.update(stato='CH')
for step in fqs:
step.save()
return redirect('riepiloghi_list')
@login_required(login_url='/login/')
def riepiloghi_upld(request, pk):
riepilogo = get_object_or_404(Riepiloghi, pk=pk)
title = "Modifica Riepilogo"
if request.method == "POST":
form = RiepiloghiForm(request.POST, request.FILES, instance=riepilogo)
context = {'form': form}
context['title'] = title
if form.is_valid():
record = form.save(commit=False)
record.autore = request.user
record.save()
return redirect('riepiloghi_list')
else:
form = RiepiloghiForm(instance=riepilogo)
context = {'form': form}
context['title'] = title
return render(request, 'formulari/materiali_edit.html', context)
@login_required(login_url='/login/')
def riepiloghi_preview(request, pk):
fqs = Formulari.objects.filter(riepi=pk).order_by('-data')
gks = []
fps = []
gsa = []
for f1 in fqs:
fps.append(f1.pk)
if f1 and f1.ripa not in gks:
gks.append(f1.ripa)
for g1 in gks:
gs = Ripartizioni.objects.filter(pk=g1.pk).filter(formulari__pk__in=fps).annotate(
totale=Sum('formulari__imp')).values('nome', 'totale', 'pk')
appo = dict(gs[0])
gsa.append(appo)
fa = Riepiloghi.objects.filter(id=pk).values('comm')
context = {'fqss': fqs}
context['gsas'] = gsa
context['comm'] = Anagrafica.objects.get(id=fa)
context['data'] = datetime.date.today()
pdf = render_to_pdf('pdf/preview_rpdf.html', context)
return HttpResponse(pdf, content_type='application/pdf')
@login_required(login_url='/login/')
def riepiloghi_rpdf(request, pk):
fqs = Formulari.objects.filter(riepi=pk).order_by('-data')
gks = []
fps = []
gsa = []
for f1 in fqs:
fps.append(f1.pk)
if f1 and f1.ripa not in gks:
gks.append(f1.ripa)
for g1 in gks:
gs = Ripartizioni.objects.filter(pk=g1.pk).filter(formulari__pk__in=fps).annotate(
totale=Sum('formulari__imp')).values('nome', 'totale', 'pk')
appo = dict(gs[0])
gsa.append(appo)
fa = Riepiloghi.objects.filter(id=pk).values('comm')
context = {'fqss': fqs}
context['gsas'] = gsa
context['comm'] = Anagrafica.objects.get(id=fa)
context['data'] = datetime.date.today()
pdf = render_to_pdf('pdf/formulari_rpdf.html', context)
return HttpResponse(pdf, content_type='application/pdf')
def comp_r_to_f(cod):
if (cod == 'RP')or (cod == 'ER'):
res = cod
elif cod == 'IN':
res = 'RP'
elif cod == 'PA':
res = 'CH'
else:
res = 'ER'
return res