m1m02530125 commited on
Commit
42a2b89
·
1 Parent(s): 49a044a

勤務統計に「シフトの公平性」「人員不足の発生頻度」セクションを追加

Browse files

- 公平性: 従業員ごとの希望シフト採用率・最大連続勤務日数(設定値超過は警告表示)・総休日数
- 人員不足の発生頻度: 朝食/ディナー × ホール/キッチンの不足日数を選択期間で集計しランキング表示

routes/schedule.py CHANGED
@@ -87,6 +87,128 @@ def _build_time_map(requests) -> dict:
87
  time_map[(r.employee_id, r.date, TimeSlot.DINNER.value)] = t
88
  return time_map
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  bp = Blueprint("schedule", __name__, url_prefix="/schedule")
91
 
92
 
@@ -409,6 +531,33 @@ def stats_multi():
409
  total_labor_cost / customer_counts["total"] if customer_counts["total"] > 0 else None
410
  )
411
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412
  return render_template(
413
  "schedule/stats_multi.html",
414
  all_periods=all_periods,
@@ -422,6 +571,9 @@ def stats_multi():
422
  customer_counts=customer_counts,
423
  total_labor_cost=total_labor_cost,
424
  cost_per_customer=cost_per_customer,
 
 
 
425
  )
426
 
427
 
 
87
  time_map[(r.employee_id, r.date, TimeSlot.DINNER.value)] = t
88
  return time_map
89
 
90
+
91
+ def _max_consecutive_days(date_strs) -> int:
92
+ """日付文字列(YYYY-MM-DD)の集合から最大連続勤務日数を求める"""
93
+ from datetime import date as _date_cls
94
+ if not date_strs:
95
+ return 0
96
+ sorted_dates = sorted(_date_cls.fromisoformat(ds) for ds in date_strs)
97
+ best = run = 1
98
+ for prev, cur in zip(sorted_dates, sorted_dates[1:]):
99
+ if (cur - prev).days == 1:
100
+ run += 1
101
+ best = max(best, run)
102
+ else:
103
+ run = 1
104
+ return best
105
+
106
+
107
+ def _compute_fairness_stats(sel_periods, employees) -> dict:
108
+ """
109
+ 各従業員の「希望シフト採用率」「最大連続勤務日数」「総休日数」を集計する。
110
+
111
+ 希望シフト採用率=(休み希望/有休が休みになった件数 + 出勤希望のスロットが
112
+ 実際に割り当てられた件数)÷ 希望件数の総数。
113
+
114
+ 返り値: {emp_id: {"req_total", "req_matched", "fulfillment_rate"(0〜1 or None),
115
+ "max_consecutive", "days_off", "total_days"}}
116
+ """
117
+ emp_ids = {e.id for e in employees}
118
+ work_dates: dict[int, set] = defaultdict(set)
119
+ req_total: dict[int, int] = defaultdict(int)
120
+ req_matched: dict[int, int] = defaultdict(int)
121
+ total_days = 0
122
+
123
+ for period in sel_periods:
124
+ dates = period.date_range()
125
+ total_days += len(dates)
126
+
127
+ assignments = repo.get_assignments(period.id)
128
+ asgn_set = {(a.employee_id, a.date, a.time_slot.value) for a in assignments}
129
+ for a in assignments:
130
+ work_dates[a.employee_id].add(a.date)
131
+
132
+ for r in repo.get_shift_requests(period.id):
133
+ if r.employee_id not in emp_ids:
134
+ continue
135
+ if r.pattern_id in ("off_request", "paid_leave"):
136
+ req_total[r.employee_id] += 1
137
+ worked = (r.employee_id, r.date, "breakfast") in asgn_set \
138
+ or (r.employee_id, r.date, "dinner") in asgn_set
139
+ if not worked:
140
+ req_matched[r.employee_id] += 1
141
+ else:
142
+ if r.breakfast:
143
+ req_total[r.employee_id] += 1
144
+ if (r.employee_id, r.date, "breakfast") in asgn_set:
145
+ req_matched[r.employee_id] += 1
146
+ if r.dinner:
147
+ req_total[r.employee_id] += 1
148
+ if (r.employee_id, r.date, "dinner") in asgn_set:
149
+ req_matched[r.employee_id] += 1
150
+
151
+ result = {}
152
+ for e in employees:
153
+ worked = work_dates.get(e.id, set())
154
+ rt = req_total.get(e.id, 0)
155
+ rm = req_matched.get(e.id, 0)
156
+ result[e.id] = {
157
+ "req_total": rt,
158
+ "req_matched": rm,
159
+ "fulfillment_rate": (rm / rt) if rt > 0 else None,
160
+ "max_consecutive": _max_consecutive_days(worked),
161
+ "days_off": total_days - len(worked),
162
+ "total_days": total_days,
163
+ }
164
+ return result
165
+
166
+
167
+ def _compute_shortage_summary(sel_periods, employees, constraints,
168
+ reserv_tiers_b, reserv_tiers_d) -> list[dict]:
169
+ """複数期間にわたる人員不足(朝食/ディナー × ホール/キッチン)の発生日数を集計する"""
170
+ _COMBOS = [
171
+ ('breakfast', '朝食', 'hall', 'ホール'),
172
+ ('breakfast', '朝食', 'kitchen', 'キッチン'),
173
+ ('dinner', 'ディナー', 'hall', 'ホール'),
174
+ ('dinner', 'ディナー', 'kitchen', 'キッチン'),
175
+ ]
176
+ totals = {(sv, pv): {"short_staff": 0, "short_leader": 0} for sv, _, pv, _ in _COMBOS}
177
+ total_days = 0
178
+
179
+ for period in sel_periods:
180
+ dates = period.date_range()
181
+ total_days += len(dates)
182
+ assignments = repo.get_assignments(period.id)
183
+ reservation_counts = repo.get_reservation_counts(period.id)
184
+ staffing = _compute_staffing(
185
+ assignments, employees, dates, constraints,
186
+ reservation_counts=reservation_counts,
187
+ reserv_tiers_b=reserv_tiers_b, reserv_tiers_d=reserv_tiers_d,
188
+ )
189
+ for d in dates:
190
+ ds = d.isoformat()
191
+ for sv, _, pv, _ in _COMBOS:
192
+ st = staffing.get((ds, sv, pv), {})
193
+ if st.get('short_staff'):
194
+ totals[(sv, pv)]["short_staff"] += 1
195
+ if st.get('short_leader'):
196
+ totals[(sv, pv)]["short_leader"] += 1
197
+
198
+ result = [
199
+ {
200
+ "slot": sl,
201
+ "pos": pl,
202
+ "short_staff_days": totals[(sv, pv)]["short_staff"],
203
+ "short_leader_days": totals[(sv, pv)]["short_leader"],
204
+ "total_days": total_days,
205
+ }
206
+ for sv, sl, pv, pl in _COMBOS
207
+ ]
208
+ result.sort(key=lambda x: -(x["short_staff_days"] + x["short_leader_days"]))
209
+ return result
210
+
211
+
212
  bp = Blueprint("schedule", __name__, url_prefix="/schedule")
213
 
214
 
 
531
  total_labor_cost / customer_counts["total"] if customer_counts["total"] > 0 else None
532
  )
533
 
534
+ # 希望シフトの公平性(採用率・最大連続勤務日数・総休日数)
535
+ fairness_stats = _compute_fairness_stats(sel_periods, employees)
536
+ try:
537
+ max_consecutive_setting = int(repo.get_app_setting("max_consecutive_days", "6"))
538
+ except Exception:
539
+ max_consecutive_setting = 6
540
+
541
+ # 人員不足の発生頻度(朝食/ディナー × ホール/キッチン)
542
+ constraints = repo.get_shift_constraints()
543
+ try:
544
+ reserv_thresh_b = int(repo.get_app_setting("reserv_threshold_breakfast", "100"))
545
+ reserv_extra_b = int(repo.get_app_setting("reserv_extra_breakfast", "1"))
546
+ reserv_thresh_b2 = int(repo.get_app_setting("reserv_threshold_breakfast2", "0"))
547
+ reserv_extra_b2 = int(repo.get_app_setting("reserv_extra_breakfast2", "0"))
548
+ reserv_thresh_d = int(repo.get_app_setting("reserv_threshold_dinner", "25"))
549
+ reserv_extra_d = int(repo.get_app_setting("reserv_extra_dinner", "1"))
550
+ reserv_thresh_d2 = int(repo.get_app_setting("reserv_threshold_dinner2", "0"))
551
+ reserv_extra_d2 = int(repo.get_app_setting("reserv_extra_dinner2", "0"))
552
+ except Exception:
553
+ reserv_thresh_b = 100; reserv_extra_b = 1; reserv_thresh_b2 = 0; reserv_extra_b2 = 0
554
+ reserv_thresh_d = 25; reserv_extra_d = 1; reserv_thresh_d2 = 0; reserv_extra_d2 = 0
555
+ reserv_tiers_b = [(reserv_thresh_b, reserv_extra_b), (reserv_thresh_b2, reserv_extra_b2)]
556
+ reserv_tiers_d = [(reserv_thresh_d, reserv_extra_d), (reserv_thresh_d2, reserv_extra_d2)]
557
+ shortage_summary = _compute_shortage_summary(
558
+ sel_periods, employees, constraints, reserv_tiers_b, reserv_tiers_d
559
+ )
560
+
561
  return render_template(
562
  "schedule/stats_multi.html",
563
  all_periods=all_periods,
 
571
  customer_counts=customer_counts,
572
  total_labor_cost=total_labor_cost,
573
  cost_per_customer=cost_per_customer,
574
+ fairness_stats=fairness_stats,
575
+ max_consecutive_setting=max_consecutive_setting,
576
+ shortage_summary=shortage_summary,
577
  )
578
 
579
 
templates/schedule/stats_multi.html CHANGED
@@ -206,6 +206,89 @@
206
  </div>
207
  {% endif %}
208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  {% if any_wage %}
210
  <!-- 人件費グラフ -->
211
  <div class="card mb-3">
 
206
  </div>
207
  {% endif %}
208
 
209
+ <!-- シフトの公平性 -->
210
+ <div class="card mb-3">
211
+ <div class="card-body p-0">
212
+ <div class="small fw-semibold text-muted px-3 pt-3 pb-2">
213
+ <i class="bi bi-people-fill me-1"></i>シフトの公平性
214
+ </div>
215
+ <div class="table-responsive">
216
+ <table class="table table-sm stat-table mb-0">
217
+ <thead class="table-dark">
218
+ <tr>
219
+ <th>名前</th>
220
+ <th class="text-center">希望採用率</th>
221
+ <th class="text-center">最大連続勤務</th>
222
+ <th class="text-center">総休日数</th>
223
+ </tr>
224
+ </thead>
225
+ <tbody>
226
+ {% for emp in employees %}
227
+ {% set f = fairness_stats.get(emp.id, {}) %}
228
+ <tr>
229
+ <td class="fw-semibold">{{ emp.name }}</td>
230
+ <td class="num text-center">
231
+ {% if f.fulfillment_rate is not none %}
232
+ {{ "%.0f"|format(f.fulfillment_rate * 100) }}%
233
+ <span class="text-muted small">({{ f.req_matched }}/{{ f.req_total }})</span>
234
+ {% else %}
235
+ <span class="text-muted">—</span>
236
+ {% endif %}
237
+ </td>
238
+ <td class="num text-center {{ 'text-danger fw-bold' if f.max_consecutive > max_consecutive_setting else '' }}">
239
+ {{ f.max_consecutive }}日
240
+ {% if f.max_consecutive > max_consecutive_setting %}
241
+ <i class="bi bi-exclamation-triangle-fill ms-1" title="設定値({{ max_consecutive_setting }}日)を超過"></i>
242
+ {% endif %}
243
+ </td>
244
+ <td class="num text-center">
245
+ {{ f.days_off }}<span class="text-muted small"> / {{ f.total_days }}日</span>
246
+ </td>
247
+ </tr>
248
+ {% endfor %}
249
+ </tbody>
250
+ </table>
251
+ </div>
252
+ <p class="text-muted small mb-0 px-3 pb-3 pt-2">
253
+ ※ 希望採用率=休み希望・出勤希望(朝食/ディナー)が実際の割当と一致した割合。希望未提出の方は「—」。<br>
254
+ ※ 最大連続勤務日数が設定値({{ max_consecutive_setting }}日)を超える場合は警告表示しています。
255
+ </p>
256
+ </div>
257
+ </div>
258
+
259
+ <!-- 人員不足の発生頻度 -->
260
+ <div class="card mb-3">
261
+ <div class="card-body">
262
+ <div class="small fw-semibold text-muted mb-2">
263
+ <i class="bi bi-exclamation-diamond-fill me-1"></i>人員不足の発生頻度
264
+ </div>
265
+ {% if shortage_summary and shortage_summary[0].total_days > 0 %}
266
+ {% for s in shortage_summary %}
267
+ <div class="mb-3">
268
+ <div class="d-flex justify-content-between small mb-1">
269
+ <span class="fw-semibold">{{ s.slot }}・{{ s.pos }}</span>
270
+ <span class="text-muted">
271
+ 人員不足 {{ s.short_staff_days }}/{{ s.total_days }}日
272
+ {% if s.short_leader_days > 0 %}
273
+ / うちリーダー不足 {{ s.short_leader_days }}日
274
+ {% endif %}
275
+ </span>
276
+ </div>
277
+ <div class="progress" style="height:6px">
278
+ <div class="progress-bar {{ 'bg-danger' if s.short_staff_days > 0 else 'bg-success' }}"
279
+ style="width: {{ (s.short_staff_days / s.total_days * 100) }}%"></div>
280
+ </div>
281
+ </div>
282
+ {% endfor %}
283
+ <p class="text-muted small mb-0 mt-1">
284
+ ※ 選択期間(合計{{ shortage_summary[0].total_days }}日)のうち、各区分で必要人数を満たせなかった日数の割合です。多い区分から順に表示しています。
285
+ </p>
286
+ {% else %}
287
+ <p class="text-muted small mb-0">対象期間がありません。</p>
288
+ {% endif %}
289
+ </div>
290
+ </div>
291
+
292
  {% if any_wage %}
293
  <!-- 人件費グラフ -->
294
  <div class="card mb-3">