Files
HRM-System/scratch/sql_to_json_roster.py

77 lines
2.2 KiB
Python

import re
import json
sql_file = '../ERP-NEW/rame_seb.sql'
def extract_table_data(table_name):
try:
with open(sql_file, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
pattern = rf"INSERT INTO `{table_name}`.*?VALUES\s*(.*?);"
matches = re.findall(pattern, content, re.DOTALL)
data = []
for match in matches:
rows = re.findall(r"\((.*?)\)", match)
for row in rows:
parts = []
current = []
in_quote = False
for char in row:
if char == "'" and not in_quote: in_quote = True
elif char == "'" and in_quote: in_quote = False
elif char == "," and not in_quote:
parts.append("".join(current).strip().strip("'"))
current = []
else:
current.append(char)
parts.append("".join(current).strip().strip("'"))
data.append(parts)
return data
except Exception as e:
print(f"Error reading {table_name}: {e}")
return []
# 1. Map Legacy User ID -> Employee Code
users_raw = extract_table_data('users')
user_code_map = {}
for u in users_raw:
if len(u) > 12:
uid = u[0]
code = u[11] # code column
user_code_map[uid] = code
# 2. Map Legacy Shift ID -> Shift Name
shifts_raw = extract_table_data('shifts')
shift_map = {}
for s in shifts_raw:
if len(s) > 1:
sid = s[0]
name = s[1]
shift_map[sid] = name
# 3. Get Daily Shifts
daily_shifts_raw = extract_table_data('employee_daily_shifts')
final_roster = []
for ds in daily_shifts_raw:
if len(ds) < 6: continue
uid = ds[1]
sid = ds[2]
date = ds[5]
code = user_code_map.get(uid)
shift_name = shift_map.get(sid)
if code:
final_roster.append({
'code': code,
'date': date,
'shift_name': shift_name,
'is_rest_day': sid == 'NULL'
})
with open('/Users/dvapp/Documents/HRM/scratch/legacy_roster.json', 'w') as f:
json.dump(final_roster, f, indent=2)
print(f"Extracted {len(final_roster)} roster entries to legacy_roster.json")