-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing.py
207 lines (182 loc) · 7.95 KB
/
printing.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
from openpyxl import Workbook
from openpyxl.styles import PatternFill, colors
import openpyxl
import csv
import json
from pulp import LpStatus
MIP_TOLERANCE = 1e-6
FORMAT_DATE = "%d/%m/%Y"
def save_csv(solver, name_csv):
from_date, to_date = solver.from_date, solver.to_date
days, shift_types = solver.days, solver.shift_types
employees, n_shifts = solver.employees, solver.n_shifts
shifts, leave = solver.shifts, solver.leave
map_slot_hours_t_i = solver.map_slot_hours_t_i
#split_shift_emp = solver.split_shift_emp
leave_gap_2_days = solver.leave_gap_2_days
#print(solver.min_max_hours_emp)
map_e_h = {}
map_e_leave = {}
map_e_leave_sat_sun = {}
map_e_spezzati = {}
map_e_split = {}
data = []
# init struct
for i in range(90):
data.append({})
for e in employees:
map_e_h[e] = {}
map_e_leave[e] = {}
map_e_leave_sat_sun[e] = 0
map_e_split[e] = 0
map_e_spezzati[e] = {}
map_e_h[e]['total'] = 0
map_e_leave[e]['total'] = 0
for w in range(int(from_date.strftime('%V')), int(to_date.strftime('%V'))+1):
map_e_h[e][w] = 0
map_e_leave[e][w] = 0
for d in days:
map_e_spezzati[e][d] = 0
data[0]['Summary'] = ''
# loop
for d in days:
j = 0
# add slots
for i in solver.get_indexes_shift(d):
for e in employees:
if abs(1 - shifts[d][i][e].varValue) <= MIP_TOLERANCE:
data[j][f'{d}:Giorno'] = d
data[j][f'{d}:Da'] = f'{map_slot_hours_t_i[d][i].t_from.strftime("%H:%M")} - {map_slot_hours_t_i[d][i].t_to.strftime("%H:%M")}'
data[j][f'{d}:Durata H'] = map_slot_hours_t_i[d][i].duration
data[j][f'{d}:Tipo'] = map_slot_hours_t_i[d][i].type
data[j][f'{d}:Nome'] = e
week = map_slot_hours_t_i[d][i].week
map_e_h[e]['total'] = map_slot_hours_t_i[d][i].duration + map_e_h[e]['total']
map_e_h[e][week] = map_slot_hours_t_i[d][i].duration + map_e_h[e][week]
j+=1
# add leave days
for e in employees:
if abs(1 - leave[d][e].varValue) <= MIP_TOLERANCE:
data[j][f'{d}:Giorno'] = d
data[j][f'{d}:Da'] = 'Riposo'
data[j][f'{d}:Nome'] = e
week = map_slot_hours_t_i[d][i].week
map_e_leave[e]['total'] = map_e_leave[e]['total'] + 1
map_e_leave[e][week] = map_e_leave[e][week] + 1
if map_slot_hours_t_i[d][0].day_of_week in [6,7]:
map_e_leave_sat_sun[e] = map_e_leave_sat_sun[e] + 1
j+=1
for e in employees:
lastday = -1
for d in days:
for i in solver.get_indexes_shift(d):
if abs(1 - shifts[d][i][e].varValue) <= MIP_TOLERANCE:
if lastday == d:
map_e_split[e] = map_e_split[e] + 1
lastday = d
# Add total, week hours for employee
j+=3
data[j]['Summary'] = 'Ore totali'
j+=1
for k,v in map_e_h.items():
data[j]['Summary'] = k + ' ' + str(v)
j+=1
# Add total leave days for employee
j+=2
data[j]['Summary'] = 'Ferie totali'
j+=1
for k,v in map_e_leave.items():
#gap_2_days = sum(leave_gap_2_days[d][k].varValue if leave_gap_2_days[d][k].varValue is not None else 0 for d in days[:-1] )
data[j]['Summary'] = '{}: {} ({}) '.format(k, v, map_e_leave_sat_sun[k])
j+=1
j+=2
data[j]['Summary'] = 'Spezzati totali'
j+=1
for k,v in map_e_split.items():
data[j]['Summary'] = k + ' ' + str(v)
j+=1
fieldnames = data[0].keys()
with open(name_csv, 'w') as myfile:
wr = csv.DictWriter(myfile, fieldnames=fieldnames)
wr.writeheader()
wr.writerows(data)
def response_build(solver):
from_date, to_date = solver.from_date, solver.to_date
days, shift_types = solver.days, solver.shift_types
employees, n_shifts = solver.employees, solver.n_shifts
shifts, leave = solver.shifts, solver.leave
map_slot_hours_t_i = solver.map_slot_hours_t_i
#split_shift_emp = solver.split_shift_emp
leave_gap_2_days = solver.leave_gap_2_days
response = {}
scheduling = {}
response['status'] = LpStatus[solver.status] # Not Solved, Optimal, Infeasible, Unbounded, Undefined
response['from_date'] = solver.from_date.strftime(FORMAT_DATE)
response['to_date'] = solver.to_date.strftime(FORMAT_DATE)
response['num_days'] = solver.num_days
response['employees'] = solver.employees
response['employees_far'] = solver.employees_far
response['scheduling'] = scheduling
# loop
for d in days:
j = 0
# add slots
for i in solver.get_indexes_shift(d):
for e in employees:
if abs(1 - shifts[d][i][e].varValue) <= MIP_TOLERANCE:
date_str = map_slot_hours_t_i[d][i].date.strftime(FORMAT_DATE)
shift = {}
shift['day'] = map_slot_hours_t_i[d][i].date.strftime(FORMAT_DATE)
shift['from'] = f'{map_slot_hours_t_i[d][i].t_from.strftime("%H:%M")}'
shift['to'] = f'{map_slot_hours_t_i[d][i].t_to.strftime("%H:%M")}'
shift['duratoin'] = map_slot_hours_t_i[d][i].duration
shift['type'] = map_slot_hours_t_i[d][i].type
shift['employee'] = e
if date_str not in scheduling:
scheduling[date_str] = []
scheduling[date_str].append(shift)
# add leave days
for e in employees:
if abs(1 - leave[d][e].varValue) <= MIP_TOLERANCE:
date_str = map_slot_hours_t_i[d][i].date.strftime(FORMAT_DATE)
shift = {}
shift['giorno'] = map_slot_hours_t_i[d][0].date.strftime(FORMAT_DATE)
shift['da'] = ''
shift['durata'] = ''
shift['tipo'] = 'Riposo'
shift['nome'] = e
if date_str not in scheduling:
scheduling[date_str] = []
scheduling[date_str].append(shift)
return response
def save_excel(solver, name_csv, name_excel):
COLOR_INDEX = (
'00FFCC00', '003366FF', '00FF0000', '0000FF00', '0099CC00', #0-4
'00FFFF00', '00FF00FF', '0000FFFF', '00FFCC99', '00CC99FF', #5-9
'00FF0000', '0000FF00', '000000FF', '00FFFF00', '00FF00FF', #10-14
'0000FFFF', '00800000', '00008000', '00000080', '00808000', #15-19
'00800080', '00008080', '00C0C0C0', '00808080', '009999FF', #20-24
'00993366', '00FFFFCC', '00CCFFFF', '00660066', '00FF8080', #25-29
'000066CC', '00CCCCFF', '00000080', '00FF00FF', '00FFFF00', #30-34
'0000FFFF', '00800080', '00800000', '00008080', '000000FF', #35-39
'0000CCFF', '00CCFFFF', '00CCFFCC', '00FFFF99', '0099CCFF', #40-44
'00FF99CC', '00CC99FF', '00FFCC99', '003366FF', '0033CCCC', #45-49
'0099CC00', '00FFCC00', '00FF9900', '00FF6600', '00666699', #50-54
'00969696', '00003366', '00339966', '00003300', '00333300', #55-59
'00993300', '00993366', '00333399', '00333333', #60-63
)
wb = openpyxl.Workbook()
worksheet = wb.active
with open(name_csv) as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
worksheet.append(row)
for x in range(1,500):
for y in range(1,500):
cell = worksheet.cell(row=x, column=y)
if cell.value is None:
continue
for idx, e in enumerate(solver.employees):
if e in str(cell.value):
cell.fill = PatternFill(start_color=COLOR_INDEX[idx], end_color=COLOR_INDEX[idx], fill_type='solid')
wb.save(name_excel)