m1m02530125 Claude Sonnet 4.6 commited on
Commit
bf2ba91
·
1 Parent(s): 1c5b273

カスタム手入力したシフトを過去パターンサジェストに反映する(Issue #13)

Browse files

get_employee_pattern_history() の集計対象から pattern_id='custom' の
除外条件を削除し、custom_start/custom_end の組み合わせ単位で頻度集計
するよう変更(CASE式でカスタム以外は時刻をNULL扱いにし、誤分割を防止)。

シフト入力画面の「全日」一括サジェストボタンに、カスタム時刻範囲
(例: 09:00〜15:00)をワンクリックで適用できるチップを追加。
bulkPart()/applyPat() を拡張し、custom_start/custom_end の値を
引き継いでカスタム時刻入力欄に自動補完するようにした。

デスクトップUI(ui/shift_input_view.py)は一括適用のカスタム時刻入力に
未対応のため、引き続き 'custom' 候補は履歴ボタンから除外する。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

db/repositories.py CHANGED
@@ -618,18 +618,23 @@ def save_breakfast_band_constraints(constraints: dict[tuple[str, str], dict]):
618
  conn.close()
619
 
620
 
621
- def get_employee_pattern_history(emp_id: int) -> list[tuple[str, int]]:
622
- """従業員の過去の希望パターン頻度を [(pattern_id, count), ...] で返す(降順)"""
 
 
623
  conn = get_connection()
624
  rows = conn.execute(
625
- """SELECT pattern_id, COUNT(*) as cnt FROM shift_requests
 
 
 
 
626
  WHERE employee_id=? AND pattern_id IS NOT NULL AND pattern_id != ''
627
- AND pattern_id != 'custom'
628
- GROUP BY pattern_id ORDER BY cnt DESC LIMIT 5""",
629
  (emp_id,)
630
  ).fetchall()
631
  conn.close()
632
- return [(r["pattern_id"], r["cnt"]) for r in rows]
633
 
634
 
635
  def get_employee_dow_patterns(emp_id: int) -> dict[int, str]:
 
618
  conn.close()
619
 
620
 
621
+ def get_employee_pattern_history(emp_id: int) -> list[tuple[str, int, Optional[str], Optional[str]]]:
622
+ """従業員の過去の希望パターン頻度を [(pattern_id, count, custom_start, custom_end), ...] で返す(降順)。
623
+ pattern_id='custom' のカスタム手入力は (custom_start, custom_end) の組み合わせ単位で集計する
624
+ (それ以外のパターンは custom_start/custom_end を NULL 扱いにして pattern_id だけで集計)"""
625
  conn = get_connection()
626
  rows = conn.execute(
627
+ """SELECT pattern_id,
628
+ CASE WHEN pattern_id='custom' THEN custom_start ELSE NULL END AS cs,
629
+ CASE WHEN pattern_id='custom' THEN custom_end ELSE NULL END AS ce,
630
+ COUNT(*) as cnt
631
+ FROM shift_requests
632
  WHERE employee_id=? AND pattern_id IS NOT NULL AND pattern_id != ''
633
+ GROUP BY pattern_id, cs, ce ORDER BY cnt DESC LIMIT 5""",
 
634
  (emp_id,)
635
  ).fetchall()
636
  conn.close()
637
+ return [(r["pattern_id"], r["cnt"], r["cs"], r["ce"]) for r in rows]
638
 
639
 
640
  def get_employee_dow_patterns(emp_id: int) -> dict[int, str]:
routes/shifts.py CHANGED
@@ -120,8 +120,9 @@ def input(period_id):
120
  if current_emp and current_emp.employment_type != EmploymentType.FULL_TIME:
121
  try:
122
  top_patterns = [
123
- (pid, cnt) for pid, cnt in repo.get_employee_pattern_history(current_emp.id)
124
- if pid in PATTERN_MAP
 
125
  ]
126
  dow_suggestions = repo.get_employee_dow_patterns(current_emp.id)
127
  except Exception:
 
120
  if current_emp and current_emp.employment_type != EmploymentType.FULL_TIME:
121
  try:
122
  top_patterns = [
123
+ (pid, cnt, cs, ce)
124
+ for pid, cnt, cs, ce in repo.get_employee_pattern_history(current_emp.id)
125
+ if pid in PATTERN_MAP and (pid != "custom" or (cs and ce))
126
  ]
127
  dow_suggestions = repo.get_employee_dow_patterns(current_emp.id)
128
  except Exception:
templates/shifts/input.html CHANGED
@@ -202,11 +202,13 @@
202
  <button type="button" class="btn btn-sm btn-outline-secondary" onclick="bulkPart('')">
203
  (休み)
204
  </button>
205
- {% for pid, cnt in top_patterns[:3] %}
206
  {% set p = PATTERN_MAP.get(pid) %}
207
  {% if p %}
208
- <button type="button" class="btn btn-sm btn-outline-primary" onclick="bulkPart('{{ pid }}')">
209
- {{ p.label }}
 
 
210
  </button>
211
  {% endif %}
212
  {% endfor %}
@@ -356,7 +358,7 @@ const PAT_CATS_JS = {
356
 
357
  // タップサイクル順:[休み, 過去履歴サジェスト(最大3), 有給, カスタム手動入力]
358
  const PAT_CYCLE = (() => {
359
- const tops = [{% for pid, cnt in top_patterns[:3] %}{{ pid|tojson }},{% endfor %}];
360
  const defs = ['d_std1', 'b_std'];
361
  const seen = new Set(['', 'paid_leave', 'custom']); // 末尾固定枠を予約
362
  const cycle = [''];
@@ -369,7 +371,8 @@ const PAT_CYCLE = (() => {
369
  })();
370
 
371
  // ── アルバイト:パターン適用(共通パス) ─────────────────────────
372
- function applyPat(empId, dateStr, patId) {
 
373
  const hid = document.getElementById(`hid_${empId}_${dateStr}`);
374
  if (!hid) return;
375
  hid.value = patId;
@@ -382,7 +385,12 @@ function applyPat(empId, dateStr, patId) {
382
  const cust = document.getElementById(`custom_${empId}_${dateStr}`);
383
  if (cust) {
384
  cust.style.display = patId === 'custom' ? 'flex' : 'none';
385
- if (patId === 'custom') cust.querySelector('input')?.focus();
 
 
 
 
 
386
  }
387
  window._formDirty = true;
388
  }
@@ -394,10 +402,10 @@ function cyclePart(empId, dateStr) {
394
  applyPat(empId, dateStr, PAT_CYCLE[(idx + 1) % PAT_CYCLE.length]);
395
  }
396
 
397
- // 全日一括
398
- function bulkPart(patId) {
399
  document.querySelectorAll('.pat-btn[data-mode="part"]').forEach(btn => {
400
- applyPat(btn.dataset.emp, btn.dataset.date, patId);
401
  });
402
  }
403
 
 
202
  <button type="button" class="btn btn-sm btn-outline-secondary" onclick="bulkPart('')">
203
  (休み)
204
  </button>
205
+ {% for pid, cnt, cs, ce in top_patterns[:3] %}
206
  {% set p = PATTERN_MAP.get(pid) %}
207
  {% if p %}
208
+ {% set label = (cs ~ '〜' ~ ce) if pid == 'custom' and cs and ce else p.label %}
209
+ <button type="button" class="btn btn-sm btn-outline-primary"
210
+ onclick="bulkPart('{{ pid }}', '{{ cs or '' }}', '{{ ce or '' }}')">
211
+ {{ label }}
212
  </button>
213
  {% endif %}
214
  {% endfor %}
 
358
 
359
  // タップサイクル順:[休み, 過去履歴サジェスト(最大3), 有給, カスタム手動入力]
360
  const PAT_CYCLE = (() => {
361
+ const tops = [{% for pid, cnt, cs, ce in top_patterns[:3] %}{{ pid|tojson }},{% endfor %}];
362
  const defs = ['d_std1', 'b_std'];
363
  const seen = new Set(['', 'paid_leave', 'custom']); // 末尾固定枠を予約
364
  const cycle = [''];
 
371
  })();
372
 
373
  // ── アルバイト:パターン適用(共通パス) ─────────────────────────
374
+ // customStart/customEnd は patId === 'custom' のサジェストから時刻を引き継ぐ場合に指定する
375
+ function applyPat(empId, dateStr, patId, customStart, customEnd) {
376
  const hid = document.getElementById(`hid_${empId}_${dateStr}`);
377
  if (!hid) return;
378
  hid.value = patId;
 
385
  const cust = document.getElementById(`custom_${empId}_${dateStr}`);
386
  if (cust) {
387
  cust.style.display = patId === 'custom' ? 'flex' : 'none';
388
+ if (patId === 'custom') {
389
+ const inputs = cust.querySelectorAll('input[type=time]');
390
+ if (customStart) inputs[0].value = customStart;
391
+ if (customEnd) inputs[1].value = customEnd;
392
+ if (!customStart && !customEnd) inputs[0]?.focus();
393
+ }
394
  }
395
  window._formDirty = true;
396
  }
 
402
  applyPat(empId, dateStr, PAT_CYCLE[(idx + 1) % PAT_CYCLE.length]);
403
  }
404
 
405
+ // 全日一括(customStart/customEnd は「カスタム」サジェストの時刻引き継ぎ用)
406
+ function bulkPart(patId, customStart, customEnd) {
407
  document.querySelectorAll('.pat-btn[data-mode="part"]').forEach(btn => {
408
+ applyPat(btn.dataset.emp, btn.dataset.date, patId, customStart, customEnd);
409
  });
410
  }
411
 
ui/shift_input_view.py CHANGED
@@ -422,7 +422,9 @@ class ShiftInputView(QWidget):
422
  return
423
 
424
  c = theme.c
425
- for pid, cnt in history:
 
 
426
  p = PATTERN_MAP.get(pid)
427
  if not p:
428
  continue
 
422
  return
423
 
424
  c = theme.c
425
+ for pid, cnt, cs, ce in history:
426
+ if pid == "custom":
427
+ continue # カスタム時刻入力はこの画面の一括適用UIでは未対応のため除外
428
  p = PATTERN_MAP.get(pid)
429
  if not p:
430
  continue