File size: 3,901 Bytes
9ff08ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pandas as pd

# Read your cleaned CSV
df = pd.read_csv("clean_assets_final.csv")  # Change to your filename

# Department mapping
dept_map = {
    "MEP-ELECTRICAL": ("Engineering", "Electrical"),
    "MEP-HVAC": ("Engineering", "HVAC"),
    "MEP-PLUMBING": ("Engineering", "Plumbing"),
    "MEP-LIFT": ("Engineering", "Vertical Transportation (Lifts)"),
    "FIRE-FIGHTING": ("Engineering", "Fire Fighting"),
    "FM-OPERATIONS": ("Facility Management", "FM Operations"),
    "FM-HARD-SERVICES": ("Facility Management", "Hard Services"),
    "FM-SOFT-SERVICES": ("Facility Management", "Soft Services"),
    "FM-FITOUT": ("Facility Management", "Fitout Works"),
    "FM-FRONT-OF-HOUSE": ("Facility Management", "Front of House"),
    "SECURITY": ("Security", "Security"),
    "ELV-ACCESS-CONTROL": ("Technology Group", "Access Control"),
    "ELV-AUTOMATION": ("Technology Group", "Automation"),
    "ELV-BMS": ("Technology Group", "BMS"),
    "ELV-VIDEO-MANAGEMENT": ("Technology Group", "CCTV"),
    "ELV-FIRE-ALARM-EVAC": ("Technology Group", "Fire Alarm & Voice Evac"),
    "ELV-NETWORK-VOIP": ("Technology Group", "Networks & Connectivity"),
    "ELV-MDTH": ("Technology Group", "MDTH (DSTV)"),
    "GATES AND SHIELD (CONTRACTOR)": ("Contractor", "Gates and Shield"),
}

def map_dept(val):
    if pd.isna(val) or str(val).strip() == "" or str(val).strip() == "NA":
        return ("Uncategorized", "Uncategorized")
    val = str(val).strip().upper()
    return dept_map.get(val, (str(val), str(val)))

def safe_val(val, default="NULL"):
    if pd.isna(val) or str(val).strip() in ["", "NA", "na"]:
        return default
    cleaned = str(val).strip().replace("'", "''")
    return f"'{cleaned}'"

# Generate SQL
sql_lines = []
sql_lines.append("-- Auto-generated asset insert\n")
sql_lines.append("DELETE FROM assets WHERE facility_code = 'WTC';\n\n")

batch_size = 100
total = len(df)

for batch_start in range(0, total, batch_size):
    batch = df.iloc[batch_start:batch_start+batch_size]
    sql_lines.append("INSERT INTO assets (facility_code, name, asset_tag, department, sub_division, category_name, parent_asset, status, priority, manufacturer, model, model_no, serial_number, capacity, description, location_building, location_floor, barcode, checklist, ppm, verification_frequency, gross_weight, dimensions, health_condition, vendor, warranty_applicable, condition_rating, created_at) VALUES\n")
    
    values = []
    for _, row in batch.iterrows():
        dept, sub = map_dept(row.get("Department"))
        name = safe_val(row.get("Assetname"))
        serial = safe_val(row.get("Serial NO"))
        mfg = safe_val(row.get("Manufacturer"))
        model = safe_val(row.get("Model"))
        cap = safe_val(row.get("Capacity"))
        loc = safe_val(row.get("Location"))
        parent = safe_val(row.get("Parent Asset"))
        cat = safe_val(row.get("Category"))
        
        val = f"('WTC', {name}, {serial if serial != 'NULL' else safe_val(f'AUTO-{batch_start}')}, {safe_val(dept)}, {safe_val(sub)}, {cat}, {parent}, 'active', 'medium', {mfg}, {model}, {safe_val(row.get('Model NO'))}, {serial}, {cap}, {safe_val(row.get('Description'))}, {loc}, {safe_val(row.get('Sub Location'))}, {safe_val(row.get('Barcode'))}, {safe_val(row.get('Checklist'))}, {safe_val(row.get('PPM'))}, {safe_val(row.get('Verification Frequency'))}, {safe_val(row.get('Gross Weight'))}, {safe_val(row.get('Size and Dimensions'))}, {safe_val(row.get('Health Condition'))}, {safe_val(row.get('Vendor'))}, {safe_val(row.get('Warrenty Applicable'))}, 5, NOW())"
        values.append(val)
    
    sql_lines.append(",\n".join(values) + ";\n\n")

with open("assets_insert.sql", "w") as f:
    f.writelines(sql_lines)

print(f"Done! {total} rows written to assets_insert.sql")
print("Run this file in Supabase SQL Editor")