-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
499 lines (415 loc) · 16.9 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
from django.db.models import Q
from django.shortcuts import render
from django.views import View
from entities.project_models import OutletType, Outlet, Item
from .helpers import *
def unused():
pass
def get_outlet_types():
outlet_types = OutletType.objects.filter(
Q(name='Филиал') | Q(name='Киоск') | Q(name='Фотомагазин')
)
return {'outlet_types': outlet_types}
def get_outlets():
outlets = Outlet.objects.select_related('type').filter(
Q(type__name='Филиал') | Q(type__name='Киоск') | Q(type__name='Фотомагазин')
).order_by('id')
return {'outlets': outlets}
class Query1Handler(View):
@staticmethod
def get(request):
context = get_outlet_types()
return render(request, 'pages/queries/1.html', context=context)
@staticmethod
def post(request):
outlet_type_id = request.POST.get('outlet')
query = '''
SELECT o.address, o.num_workers
FROM outlets o
JOIN outlet_types t ON o.type_id = t.id
WHERE o.type_id = %s
'''
return build_success_response([execute_query(query, (outlet_type_id,), 'Адрес', 'Количество работников')])
class Query2Handler(View):
@staticmethod
def get(request):
context = get_outlet_types()
return render(request, 'pages/queries/2.html', context=context)
@staticmethod
def post(request):
outlet_type_id = request.POST.get('outlet_type')
start = request.POST.get('start_date')
end = request.POST.get('end_date')
try:
start, end = validate_date_range(start, end)
except ValueError as e:
return build_error_response(str(e))
query = '''
SELECT c.full_name,
ot.address,
TO_CHAR(o.accept_timestamp, 'DD-MM-YYYY HH24:MI:SS') AS accept_timestamp,
ot_types.name AS accept_type
FROM public.orders o
JOIN public.outlets ot ON o.accept_outlet_id = ot.id
JOIN public.outlet_types ot_types ON ot.type_id = ot_types.id
JOIN clients c ON o.client_id = c.id
WHERE ot.type_id = %s AND o.accept_timestamp BETWEEN %s AND %s
'''
return build_success_response(
[
execute_query(
query,
(outlet_type_id, start, end),
'ФИО', 'Адрес', 'Время приёма', 'Тип здания'
)
]
)
class Query3Handler(View):
@staticmethod
def get(request):
context = get_outlets()
return render(request, 'pages/queries/3.html', context=context)
@staticmethod
def post(request):
outlet_type_id = request.POST.get('outlet')
urgency = request.POST.get('urgency')
start = request.POST.get('start_date')
end = request.POST.get('end_date')
try:
start, end = validate_date_range(start, end)
except ValueError as e:
return build_error_response(str(e))
query = '''
SELECT
st.name,
CASE WHEN o.is_urgent THEN 'Срочный' ELSE 'Не срочный' END AS is_urgent,
TO_CHAR(o.accept_timestamp, 'DD-MM-YYYY HH24:MI:SS') AS accept_timestamp,
c.full_name
FROM
service_orders so
JOIN
orders o ON so.order_id = o.id
JOIN
service_types st ON so.service_type_id = st.id
JOIN
public.clients c ON o.client_id = c.id
JOIN
outlets ot ON o.accept_outlet_id = ot.id
WHERE
accept_outlet_id = %s
AND is_urgent = %s
AND accept_timestamp BETWEEN %s AND %s;
'''
resp = execute_query(query, (outlet_type_id, urgency, start, end), 'Название услуги', 'Срочность', 'Время приёма', 'Имя Клиента')
resp.add_value_to_column_by_field(field_name="Время приёма", key="bottomCalc", data="count")
return build_success_response([resp])
class Query4Handler(View):
@staticmethod
def get(request):
context = get_outlets()
return render(request, 'pages/queries/4.html', context=context)
@staticmethod
def post(request):
outlet_id = request.POST.get('outlet')
urgency = request.POST.get('urgency')
start = request.POST.get('start_date')
end = request.POST.get('end_date')
try:
start, end = validate_date_range(start, end)
except ValueError as e:
return build_error_response(str(e))
query = '''
SELECT ot.address, c.full_name,
TO_CHAR(o.accept_timestamp, 'DD-MM-YYYY HH24:MI:SS'), calculate_service_orders_price(o.id)
FROM orders o
JOIN public.clients c on c.id = o.client_id
JOIN outlets ot ON o.accept_outlet_id = ot.id
WHERE accept_outlet_id = %s
AND is_urgent = %s
AND accept_timestamp BETWEEN %s AND %s; ;
'''
resp = execute_query(query, (outlet_id, urgency, start, end), 'Адрес', 'Фио Клиента', 'Время',
'Стоимость услуг')
resp.add_value_to_column_by_field(field_name="Стоимость услуг", key="bottomCalc", data="sum")
return build_success_response([resp])
class Query5Handler(View):
@staticmethod
def get(request):
context = get_outlets()
context["all_outlets"] = True
return render(request, 'pages/queries/5.html', context=context)
@staticmethod
def post(request):
outlet_id = request.POST.get('outlet')
urgency = request.POST.get('urgency')
start = request.POST.get('start_date')
end = request.POST.get('end_date')
try:
start, end = validate_date_range(start, end)
except ValueError as e:
return build_error_response(str(e))
query = f'''
SELECT
ot.address,
c.full_name,
TO_CHAR(o.accept_timestamp, 'DD-MM-YYYY HH24:MI:SS'),
COALESCE(SUM(f.amount), 0)
FROM
print_orders po
JOIN
orders o ON po.order_id = o.id
JOIN
public.clients c ON c.id = o.client_id
JOIN
outlets ot ON o.accept_outlet_id = ot.id
LEFT JOIN
public.frames f ON po.id = f.print_order_id
WHERE
o.is_urgent = %s
AND o.accept_timestamp BETWEEN %s AND %s
{'AND o.accept_outlet_id = %s' if outlet_id else ''}
GROUP BY ot.address, c.full_name, o.accept_timestamp;
'''
args = [urgency, start, end]
if outlet_id:
args.append(outlet_id)
resp = execute_query(
query,
args,
'Адрес', 'ФИО', 'Дата', 'Количество'
)
resp.add_value_to_column_by_field(field_name="Количество", key="bottomCalc", data="sum")
return build_success_response([
resp
])
class Query6Handler(View):
@staticmethod
def get(request):
context = get_outlets()
context["all_outlets"] = True
return render(request, 'pages/queries/6.html', context=context)
@staticmethod
def post(request):
outlet_id = request.POST.get('outlet')
urgency = request.POST.get('urgency')
start = request.POST.get('start_date')
end = request.POST.get('end_date')
try:
start, end = validate_date_range(start, end)
except ValueError as e:
return build_error_response(str(e))
query = f'''
SELECT COUNT(*) FROM film_development_orders;
'''
args = [urgency, start, end]
if outlet_id:
args.append(outlet_id)
resp = execute_query(
query,
args,
'Количество'
)
return build_success_response([
resp
])
class Query7Handler(View):
@staticmethod
def get(request):
items = Item.objects.all()
context = {
'items': items
}
return render(request, 'pages/queries/7.html', context=context)
@staticmethod
def post(request):
item_id = request.POST.get("item")
start = request.POST.get('start_date')
end = request.POST.get('end_date')
volume = request.POST.get('volume')
if not volume:
volume = 0
try:
start, end = validate_date_range(start, end)
except ValueError as e:
return build_error_response(str(e))
args = [start, end, volume]
if item_id:
args.append(item_id)
query = f'''
SELECT
v.name,
SUM(di.amount) AS delivery_count
FROM
vendors v
JOIN vendor_items vi ON v.id = vi.vendor_id
JOIN delivery_items di ON vi.item_id = di.item_id
JOIN deliveries d ON di.delivery_id = d.id
WHERE
d.delivery_date BETWEEN %s AND %s
AND di.amount >= %s
{'AND vi.item_id = %s' if item_id else ''}
GROUP BY
v.name;
'''
resp = execute_query(query, args, 'Имя поставщика', 'Количество поставок')
resp.add_value_to_column_by_field(field_name="Количество поставок", key="bottomCalc", data="sum")
return build_success_response([resp])
class Query8Handler(View):
@staticmethod
def get(request):
context = get_outlets()
context["all_outlets"] = True
return render(request, 'pages/queries/8.html', context=context)
@staticmethod
def post(request):
outlet_id = request.POST.get("outlet")
buy_min = request.POST.get('volume')
discount_min = request.POST.get('discount')
args = [discount_min, buy_min]
if outlet_id:
args.append(outlet_id)
resp1 = execute_query(
f'''
SELECT DISTINCT c.full_name,
CASE
WHEN is_professional THEN 'Да'
WHEN NOT is_professional THEN 'Нет'
END AS is_professional,
discount,
SUM(COALESCE(o.total_amount_price, 0))
FROM clients c
LEFT JOIN orders o on c.id = o.client_id
WHERE c.discount >= %s
GROUP BY c.id, c.full_name, c.is_professional, c.discount, total_amount_price, accept_outlet_id
HAVING SUM(COALESCE(o.total_amount_price, 0)) >= %s
{'AND o.accept_outlet_id = %s' if outlet_id else ''}
''',
args, 'Имя', 'Профессионал', 'Скидка', 'Обьем покупок')
return build_success_response([resp1])
class Query9Handler(View):
@staticmethod
def get(request):
context = get_outlets()
context["all_outlets"] = True
return render(request, 'pages/queries/9.html', context=context)
@staticmethod
def post(request):
outlet_id = request.POST.get("outlet")
start_date = request.POST.get("start_date")
end_date = request.POST.get("end_date")
try:
start, end = validate_date_range(start_date, end_date)
except ValueError as e:
return build_error_response(str(e))
args = [start, end]
if outlet_id:
args.append(outlet_id)
query = f'''
SELECT ot.address, c.full_name,
TO_CHAR(o.accept_timestamp, 'DD-MM-YYYY HH24:MI:SS'), calculate_sale_orders_price(o.id)
FROM orders o
JOIN public.clients c on c.id = o.client_id
JOIN outlets ot ON o.accept_outlet_id = ot.id
WHERE accept_timestamp BETWEEN %s AND %s
{'AND o.accept_outlet_id = %s' if outlet_id else ''}
'''
resp = execute_query(query, args, 'Адрес', 'ФИО', 'Дата', 'Цена проданных фототоваров в заказе')
resp.add_value_to_column_by_field(field_name="Цена проданных фототоваров в заказе", key="bottomCalc",
data="sum")
return build_success_response([resp])
class Query10Handler(View):
@staticmethod
def get(request):
context = get_outlets()
context["all_outlets"] = True
return render(request, 'pages/queries/10.html', context=context)
@staticmethod
def post(request):
outlet_id = request.POST.get("outlet")
query = f'''
WITH SalesData AS (
SELECT
f.name AS firm_name,
i.product_name AS product_name,
SUM(so.amount) AS count_saled,
ROW_NUMBER() OVER (ORDER BY SUM(so.amount) DESC) AS sales_rank
FROM
firms f
JOIN
items i ON i.firm_id = f.id
JOIN
sale_orders so ON so.item_id = i.id
JOIN
orders o ON so.order_id = o.id
{'WHERE o.accept_outlet_id = %s' if outlet_id else ''}
GROUP BY
f.name, i.product_name
)
SELECT
firm_name,
product_name,
count_saled,
sales_rank
FROM
SalesData
ORDER BY
sales_rank;
'''
resp = execute_query(query, outlet_id, 'Фирма', 'Продукт', 'Сколько продано', 'Место')
return build_success_response([resp])
class Query11Handler(View):
@staticmethod
def get(request):
context = get_outlets()
context["all_outlets"] = True
return render(request, 'pages/queries/11.html', context=context)
@staticmethod
def post(request):
outlet_id = request.POST.get("outlet")
start_date = request.POST.get("start_date")
end_date = request.POST.get("end_date")
try:
start, end = validate_date_range(start_date, end_date)
except ValueError as e:
return build_error_response(str(e))
args = [start, end]
if outlet_id:
args.append(outlet_id)
query = f'''
SELECT ot.address, c.full_name,
TO_CHAR(o.accept_timestamp, 'DD-MM-YYYY HH24:MI:SS'), calculate_sale_orders_price(o.id)
FROM orders o
JOIN public.clients c on c.id = o.client_id
JOIN outlets ot ON o.accept_outlet_id = ot.id
WHERE accept_timestamp BETWEEN %s AND %s
{'AND o.accept_outlet_id = %s' if outlet_id else ''}
'''
resp = execute_query(query, args, 'Адрес', 'ФИО', 'Дата', 'Цена проданных фототоваров в заказе')
resp.add_value_to_column_by_field(field_name="Цена проданных фототоваров в заказе", key="bottomCalc",
data="sum")
return build_success_response([resp])
class Query12Handler(View):
@staticmethod
def get(request):
context = get_outlet_types()
context["all_types"] = True
return render(request, 'pages/queries/12.html', context=context)
@staticmethod
def post(request):
outlet_type_id = request.POST.get('outlet_type')
print(outlet_type_id)
if not outlet_type_id:
query = '''
SELECT
SUM(num_workers) AS total_workers
FROM
outlets;
'''
return build_success_response([execute_query(query, None, 'Количество')])
query = '''
SELECT o.address, o.num_workers
FROM outlets o
JOIN outlet_types t ON o.type_id = t.id
WHERE o.type_id = %s
'''
return build_success_response([execute_query(query, (outlet_type_id,), 'Адрес', 'Количество работников')])