-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdf_creator.py
378 lines (341 loc) · 11.9 KB
/
pdf_creator.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
from io import BytesIO
from datetime import date
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import (
SimpleDocTemplate,
Table,
TableStyle,
Spacer,
Paragraph,
PageBreak,
)
from reportlab.lib.enums import TA_CENTER
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.styles import ParagraphStyle
from django.db.models import Q
from django.http import FileResponse
from django.contrib.staticfiles import finders
from applications.requests.models import Request
from applications.sickleaves.models import Sickleave
from applications.users.models import User
font_normal = finders.find('fonts/signika.ttf')
font_bold = finders.find('fonts/signika-bold.ttf')
pdfmetrics.registerFont(TTFont("signika", font_normal))
pdfmetrics.registerFont(TTFont("signika-bold", font_bold))
pdfmetrics.registerFontFamily(
"signika",
normal="signika",
bold="signika-bold",
)
def create_pdf_sheet(data, fileName, title, start_date, end_date):
if title == "Wykaz zwolnień lekarskich":
pagesize = A4
table_font_size = 10
else:
pagesize = landscape(A4)
table_font_size = 12
pdf = SimpleDocTemplate(
fileName,
pagesize=pagesize,
rightMargin=50,
leftMargin=50,
topMargin=30,
bottomMargin=30,
)
style = ParagraphStyle(
name="Title",
fontName="signika",
fontSize=12,
alignment=TA_CENTER,
)
style1 = ParagraphStyle(
name="footer",
fontName="signika",
fontSize=10,
alignment=TA_CENTER,
)
elems = []
for person in data:
if len(person["table_data"]) > 1:
table = Table(person["table_data"])
elems.append(
Paragraph(
(f"{title} w okresie od {start_date.strftime('%d-%m-%Y')} "
f"do {end_date.strftime('%d-%m-%Y')}"),
style=style,
)
)
elems.append(Spacer(1, 16))
if person["position"] != "":
elems.append(
Paragraph(
f"<b>{person['name']}</b> ({person['position']})",
style=style,
)
)
elems.append(Spacer(1, 20))
elems.append(table)
style_table = TableStyle(
[
("FONTNAME", (0, 0), (-1, -1), "signika"),
("FONTSIZE", (0, 0), (-1, -1), table_font_size),
("LEFTPADDING", (0, 0), (-1, -1), 4),
("RIGHTPADDING", (0, 0), (-1, -1), 4),
("GRID", (0, 0), (-1, -1), 1, colors.black),
("ALIGN", (4, 1), (5, -1), "CENTER"),
("ALIGN", (1, 0), (1, -1), "CENTER"),
("ALIGN", (0, 0), (0, -1), "RIGHT"),
]
)
table.setStyle(style_table)
elems.append(Spacer(1, 20))
elems.append(
Paragraph(
("Miejska Biblioteka Publiczna im. Jana Pawła II w Opolu. "
"Wygenerowano z Pracownik MBP"),
style=style1,
)
)
elems.append(PageBreak())
pdf.build(elems)
def create_pdf_report(person, start_date, end_date, leave_type, attachment):
pdf_buffer = BytesIO()
annualleave_header = [
[
"Lp.",
"Złożony",
"Nazwisko i imię",
"Od",
"Do",
"Dni/godz.",
"Status",
"Podpisany przez:",
]
]
othertypeleave_header = [
[
"Lp.",
"Złożony",
"Nazwisko i imię",
"Na dzień",
"Rodzaj",
"Za pracę dnia",
"Status",
"Podpisany przez:",
]
]
sickleaves_header = [
[
"Lp.",
"Wystawiony",
"Nr dokumentu",
"Nazwisko i imię",
"Rodzaj",
"Od",
"Do",
"Inne",
]
]
table_data_employee = []
def create_data_dict(name, position, employee_data):
"""helper function to create dictionary with requested data
about chosen employee"""
data_employee = {}
data_employee["name"] = name
data_employee["position"] = position
data_employee["table_data"] = employee_data
table_data_employee.append(data_employee)
return table_data_employee
if leave_type == "W":
"""report about annual leave"""
title = "Wykaz wniosków urlopowych"
for p in person:
x = 1
employee_data = []
employee_data += annualleave_header
if p == "all_employees":
name = ""
position = ""
employee = "- wszyscy pracownicy"
requests_data = Request.objects.filter(
Q(leave_type="W")
& (
Q(start_date__range=(start_date, end_date))
| Q(end_date__range=(start_date, end_date))
)
).order_by("author", "created")
else:
employee = User.objects.get(id=p)
name = f"{employee.last_name} {employee.first_name}"
position = employee.position
requests_data = Request.objects.filter(
Q(leave_type="W")
& Q(author__id=employee.id)
& (
Q(start_date__range=(start_date, end_date))
| Q(end_date__range=(start_date, end_date))
)
).order_by("created")
for item in requests_data:
created_newformat = item.created.strftime("%d-%m-%y")
employee_repr = (
f"{item.author.last_name} {item.author.first_name} "
f"{item.author.position_addinfo}"
)
data = [
x,
created_newformat,
employee_repr,
item.start_date.strftime("%d-%m-%y"),
item.end_date.strftime("%d-%m-%y"),
item.days,
item.status,
item.signed_by,
]
employee_data.append(data)
x += 1
create_data_dict(name, position, employee_data)
create_pdf_sheet(
table_data_employee, pdf_buffer, title, start_date, end_date
)
pdf_buffer.seek(0)
return FileResponse(
pdf_buffer,
as_attachment=attachment,
filename=f"wykaz urlopów {employee}.pdf",
)
elif leave_type == "WS":
# report about other type leaves
title = (
"Wykaz wniosków o dni wolne za pracujące soboty "
"(niedziele, święta)"
)
for p in person:
x = 1
employee_data = []
employee_data += othertypeleave_header
if p == "all_employees":
name = ""
position = ""
employee = "- wszyscy pracownicy"
requests_data = (
Request.objects.filter(
~Q(leave_type="W") & Q(
start_date__range=(start_date, end_date)
)
)
.order_by("author", "created")
.all()
)
else:
employee = User.objects.get(id=p)
name = f"{employee.last_name} {employee.first_name}"
position = employee.position
requests_data = (
Request.objects.filter(
~Q(leave_type="W")
& Q(author__id=employee.id)
& Q(start_date__range=(start_date, end_date))
)
.order_by("created")
.all()
)
for item in requests_data:
created_newformat = item.created.strftime("%d-%m-%y")
employee_repr = (
f"{item.author.last_name} {item.author.first_name} "
f"{item.author.position_addinfo}"
)
if not isinstance(item.work_date, date):
work_date = ""
else:
work_date = item.work_date.strftime("%d-%m-%y")
data = [
x,
created_newformat,
employee_repr,
item.start_date.strftime("%d-%m-%y"),
item.leave_type,
work_date,
item.status,
item.signed_by,
]
employee_data.append(data)
x += 1
create_data_dict(name, position, employee_data)
create_pdf_sheet(
table_data_employee, pdf_buffer, title, start_date, end_date
)
pdf_buffer.seek(0)
return FileResponse(
pdf_buffer,
as_attachment=attachment,
filename=f"wykaz dni wolne {employee}.pdf",
)
if leave_type == "C":
"""report about sick leaves"""
for p in person:
x = 1
employee_data = []
employee_data += sickleaves_header
if p == "all_employees":
name = ""
position = ""
employee = "- wszyscy pracownicy"
sickleaves_data = (
Sickleave.objects.filter(
Q(start_date__gte=start_date) & Q(
start_date__lte=end_date
)
)
.order_by("employee__last_name", "start_date")
.all()
)
else:
employee = User.objects.get(id=p)
name = employee.last_name + " " + employee.first_name
position = employee.position
sickleaves_data = (
Sickleave.objects.filter(
Q(employee__id=employee.id)
& Q(start_date__gte=start_date)
& Q(start_date__lte=end_date)
)
.order_by("start_date")
.all()
)
for item in sickleaves_data:
employee_repr = (
f"{item.employee.last_name} {item.employee.first_name} "
f"{item.employee.position_addinfo}"
)
additional_info = (
item.additional_info
if len(item.additional_info) < 25
else item.additional_info[:24] + "..."
)
data = [
x,
item.issue_date.strftime("%d-%m-%y"),
item.doc_number,
employee_repr,
item.leave_type,
item.start_date.strftime("%d-%m-%y"),
item.end_date.strftime("%d-%m-%y"),
additional_info,
]
employee_data.append(data)
x += 1
create_data_dict(name, position, employee_data)
title = "Wykaz zwolnień lekarskich"
create_pdf_sheet(
table_data_employee, pdf_buffer, title, start_date, end_date
)
pdf_buffer.seek(0)
return FileResponse(
pdf_buffer,
as_attachment=attachment,
filename=f"chorobowe {employee}.pdf"
)