주소 그룹 내 배치 인원 순환 재분배 (export 시 적용)

This commit is contained in:
user01
2026-05-25 01:18:52 +09:00
parent 310885ed8a
commit f609fc87f8

54
app.py
View File

@@ -810,6 +810,20 @@ def export():
wp_lookup = {wp['id']: wp for wp in t['workplaces']} wp_lookup = {wp['id']: wp for wp in t['workplaces']}
tab_name = t['name'] tab_name = t['name']
# 주소가 같은 근무처 그룹 내에서 배치 인원을 순환 재분배 (export 전용)
addr_groups = {}
for wp in t['workplaces']:
addr_groups.setdefault(wp['address'], []).append(wp['id'])
export_assignments = dict(t['assignments'])
for addr, wp_ids in addr_groups.items():
if len(wp_ids) <= 1:
continue
group_wids = [wid for wid, wpid in export_assignments.items() if wpid in wp_ids]
if not group_wids:
continue
for i, wid in enumerate(group_wids):
export_assignments[wid] = wp_ids[i % len(wp_ids)]
suffix_map = {'unassigned': '미배치', 'assigned': '배치결과', 'all': '배치결과'} suffix_map = {'unassigned': '미배치', 'assigned': '배치결과', 'all': '배치결과'}
suffix = suffix_map.get(export_type, '배치결과') suffix = suffix_map.get(export_type, '배치결과')
@@ -826,7 +840,7 @@ def export():
ws.title = '미배치 근로자' ws.title = '미배치 근로자'
ws.append(['이름', '희망사항', '주소', '생년월일', '연락처', '성별']) ws.append(['이름', '희망사항', '주소', '생년월일', '연락처', '성별'])
for w in t['workers']: for w in t['workers']:
if w['id'] not in t['assignments']: if w['id'] not in export_assignments:
ws.append([w['name'], w['hope'], w['address'], w['dob'], w['phone'], w['gender']]) ws.append([w['name'], w['hope'], w['address'], w['dob'], w['phone'], w['gender']])
elif export_type == 'assigned': elif export_type == 'assigned':
@@ -837,7 +851,7 @@ def export():
wb = openpyxl.load_workbook(template_path) wb = openpyxl.load_workbook(template_path)
wp_workers = {} wp_workers = {}
for w in t['workers']: for w in t['workers']:
a_id = t['assignments'].get(w['id']) a_id = export_assignments.get(w['id'])
if a_id and a_id in wp_lookup: if a_id and a_id in wp_lookup:
wp_workers.setdefault(wp_lookup[a_id]['name'], []).append(w) wp_workers.setdefault(wp_lookup[a_id]['name'], []).append(w)
used_names = set() used_names = set()
@@ -875,14 +889,14 @@ def export():
cells[addr_col].value = w['address'] cells[addr_col].value = w['address']
used_names.add(w['id']) used_names.add(w['id'])
# 템플릿에 없는 나머지 배치된 근로자는 추가 시트로 내보내기 # 템플릿에 없는 나머지 배치된 근로자는 추가 시트로 내보내기
all_assigned = {w['id'] for w in t['workers'] if w['id'] in t['assignments']} all_assigned = {w['id'] for w in t['workers'] if w['id'] in export_assignments}
remaining = all_assigned - used_names remaining = all_assigned - used_names
if remaining: if remaining:
ws_extra = wb.create_sheet('추가 배치') ws_extra = wb.create_sheet('추가 배치')
ws_extra.append(['이름', '연락처', '생년월일', '희망사항', '주소', '성별', '배치근무지', '근무지주소']) ws_extra.append(['이름', '연락처', '생년월일', '희망사항', '주소', '성별', '배치근무지', '근무지주소'])
for w in t['workers']: for w in t['workers']:
if w['id'] in remaining: if w['id'] in remaining:
a_id = t['assignments'].get(w['id']) a_id = export_assignments.get(w['id'])
wn = wp_lookup[a_id]['name'] if a_id in wp_lookup else '' wn = wp_lookup[a_id]['name'] if a_id in wp_lookup else ''
wa = wp_lookup[a_id]['address'] if a_id in wp_lookup else '' wa = wp_lookup[a_id]['address'] if a_id in wp_lookup else ''
ws_extra.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], w['gender'], wn, wa]) ws_extra.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], w['gender'], wn, wa])
@@ -892,7 +906,7 @@ def export():
ws1.title = '배치 현황' ws1.title = '배치 현황'
ws1.append(['이름', '연락처', '생년월일', '희망사항', '주소', '성별', '배치근무지', '근무지주소']) ws1.append(['이름', '연락처', '생년월일', '희망사항', '주소', '성별', '배치근무지', '근무지주소'])
for w in t['workers']: for w in t['workers']:
a_id = t['assignments'].get(w['id']) a_id = export_assignments.get(w['id'])
if not a_id: if not a_id:
continue continue
wn = wp_lookup[a_id]['name'] if a_id in wp_lookup else '' wn = wp_lookup[a_id]['name'] if a_id in wp_lookup else ''
@@ -900,16 +914,16 @@ def export():
ws1.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], w['gender'], wn, wa]) ws1.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], w['gender'], wn, wa])
ws2 = wb.create_sheet('근무지별 배치') ws2 = wb.create_sheet('근무지별 배치')
ws2.append(['근무지명', '주소', '배치인원', '배치된근무자']) ws2.append(['근무지명', '주소', '배치인원', '배치된근무자'])
addr_groups = {} addr_groups2 = {}
for wp in t['workplaces']: for wp in t['workplaces']:
key = wp['address'] key = wp['address']
if key not in addr_groups: if key not in addr_groups2:
addr_groups[key] = {'names': [], 'workplace_ids': []} addr_groups2[key] = {'names': [], 'workplace_ids': []}
addr_groups[key]['names'].append(wp['name']) addr_groups2[key]['names'].append(wp['name'])
addr_groups[key]['workplace_ids'].append(wp['id']) addr_groups2[key]['workplace_ids'].append(wp['id'])
for addr, g in addr_groups.items(): for addr, g in addr_groups2.items():
combined_name = ', '.join(g['names']) combined_name = ', '.join(g['names'])
aw = [w['name'] for w in t['workers'] if t['assignments'].get(w['id']) in g['workplace_ids']] aw = [w['name'] for w in t['workers'] if export_assignments.get(w['id']) in g['workplace_ids']]
ws2.append([combined_name, addr, len(aw), ', '.join(aw)]) ws2.append([combined_name, addr, len(aw), ', '.join(aw)])
else: # all (전체) else: # all (전체)
@@ -917,22 +931,22 @@ def export():
ws1.title = '배치 현황' ws1.title = '배치 현황'
ws1.append(['이름', '연락처', '생년월일', '희망사항', '주소', '성별', '배치근무지', '근무지주소']) ws1.append(['이름', '연락처', '생년월일', '희망사항', '주소', '성별', '배치근무지', '근무지주소'])
for w in t['workers']: for w in t['workers']:
a_id = t['assignments'].get(w['id']) a_id = export_assignments.get(w['id'])
wn = wp_lookup[a_id]['name'] if a_id and a_id in wp_lookup else '' wn = wp_lookup[a_id]['name'] if a_id and a_id in wp_lookup else ''
wa = wp_lookup[a_id]['address'] if a_id and a_id in wp_lookup else '' wa = wp_lookup[a_id]['address'] if a_id and a_id in wp_lookup else ''
ws1.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], w['gender'], wn, wa]) ws1.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], w['gender'], wn, wa])
ws2 = wb.create_sheet('근무지별 배치') ws2 = wb.create_sheet('근무지별 배치')
ws2.append(['근무지명', '주소', '배치인원', '배치된근무자']) ws2.append(['근무지명', '주소', '배치인원', '배치된근무자'])
addr_groups = {} addr_groups2 = {}
for wp in t['workplaces']: for wp in t['workplaces']:
key = wp['address'] key = wp['address']
if key not in addr_groups: if key not in addr_groups2:
addr_groups[key] = {'names': [], 'workplace_ids': []} addr_groups2[key] = {'names': [], 'workplace_ids': []}
addr_groups[key]['names'].append(wp['name']) addr_groups2[key]['names'].append(wp['name'])
addr_groups[key]['workplace_ids'].append(wp['id']) addr_groups2[key]['workplace_ids'].append(wp['id'])
for addr, g in addr_groups.items(): for addr, g in addr_groups2.items():
combined_name = ', '.join(g['names']) combined_name = ', '.join(g['names'])
aw = [w['name'] for w in t['workers'] if t['assignments'].get(w['id']) in g['workplace_ids']] aw = [w['name'] for w in t['workers'] if export_assignments.get(w['id']) in g['workplace_ids']]
ws2.append([combined_name, addr, len(aw), ', '.join(aw)]) ws2.append([combined_name, addr, len(aw), ', '.join(aw)])
buf = io.BytesIO() buf = io.BytesIO()