Files
HRM-System/scratch/legacy_analysis.py

85 lines
2.9 KiB
Python

import re
import collections
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:
# Naive csv parser that handles basic quotes
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 []
# Load Users
users_raw = extract_table_data('users')
users = {u[0]: u[5] for u in users_raw if len(u) > 5}
# Load Shifts
shifts_raw = extract_table_data('shifts')
shifts = {s[0]: s[1] for s in shifts_raw if len(s) > 1}
shifts['NULL'] = 'Rest Day'
# Load Daily Shifts
daily_shifts_raw = extract_table_data('employee_daily_shifts')
# Week Range: 2026-01-12 (Mon) to 2026-01-18 (Sun)
week_dates = ['2026-01-12', '2026-01-13', '2026-01-14', '2026-01-15', '2026-01-16', '2026-01-17', '2026-01-18']
summary = collections.defaultdict(dict)
for ds in daily_shifts_raw:
if len(ds) < 6: continue
user_id = ds[1]
shift_id = ds[2]
date = ds[5]
if date in week_dates:
summary[user_id][date] = shifts.get(shift_id, 'RD' if shift_id == 'NULL' else 'Unknown')
# Print Summary
print(f"{'Employee':<25} | {'Mon':<5} | {'Tue':<5} | {'Wed':<5} | {'Thu':<5} | {'Fri':<5} | {'Sat':<5} | {'Sun':<5}")
print("-" * 80)
# Get sorted user IDs numerically
sorted_uids = sorted(summary.keys(), key=lambda x: int(x) if x.isdigit() else 999)
for uid in sorted_uids:
name = users.get(uid, f"User {uid}")
row = [name[:25]]
for d in week_dates:
s_name = summary[uid].get(d, '--')
if s_name == 'Rest Day' or s_name == 'RD':
short_s = 'RD'
else:
# Extract time e.g. "08:00AM" from "OPS 08:00AM-05:00PM"
time_match = re.search(r"(\d+:\d+[AP]M)", s_name)
short_s = time_match.group(1) if time_match else s_name[:5]
row.append(short_s)
print(f"{row[0]:<25} | {row[1]:<5} | {row[2]:<5} | {row[3]:<5} | {row[4]:<5} | {row[5]:<5} | {row[6]:<5} | {row[7]:<5}")