# Vendor Type & Infrastructure Support - Complete Code Audit

**Date:** 2025-10-23
**Status:** Audit Complete
**Purpose:** Identify all code changes required for vendor type refactoring and infrastructure vendor/model support

---

## Executive Summary

This audit identifies **all files requiring changes** for two related database migrations:
1. **Infrastructure Support**: Add vendor/model tracking for servers, switches, cameras
2. **Vendor Type Refactoring**: Normalize 6 boolean flags into proper one-to-many relationship

### Files Requiring Changes

| Category | File Count | Priority |
|----------|------------|----------|
| **Core Data Cache** | 1 file | 🔴 CRITICAL (affects all dropdowns) |
| **Vendor Queries** | 30 files | 🟡 HIGH |
| **Infrastructure Pages** | 0 files | 🟢 NEW DEVELOPMENT REQUIRED |
| **Network/Map Pages** | 3 files | 🟡 MEDIUM (may need infrastructure support) |

**Total Files to Modify:** 31 existing files
**New Files to Create:** ~9-12 files (infrastructure CRUD pages)

---

## Part 1: Vendor Type Boolean Flag Usage (30 Files)

### Critical Priority: Data Cache (Affects All Dropdowns)

#### includes/data_cache.asp
**Impact:** This file provides cached vendor dropdowns used throughout the application.

**Current Implementation:**
- **Line 30:** `sql = "SELECT vendorid, vendor FROM vendors WHERE isprinter=1 AND isactive=1 ORDER BY vendor ASC"`
- **Line 91:** `sql = "... WHERE models.vendorid = vendors.vendorid AND vendors.isprinter=1 AND models.isactive=1 ..."`

**Functions to Update:**
1. `GetPrinterVendors()` - Line 30
2. `GetPrinterModels()` - Line 91
3. **TODO:** Add new functions for infrastructure devices:
   - `GetServerVendors()`
   - `GetSwitchVendors()`
   - `GetCameraVendors()`
   - `GetServerModels()`
   - `GetSwitchModels()`
   - `GetCameraModels()`

**Change Strategy:**
```vbscript
' OLD:
sql = "SELECT vendorid, vendor FROM vendors WHERE isprinter=1 AND isactive=1 ORDER BY vendor ASC"

' NEW (Option 1 - Using vendortypeid directly):
sql = "SELECT vendorid, vendor FROM vendors WHERE vendortypeid=2 AND isactive=1 ORDER BY vendor ASC"

' NEW (Option 2 - Using view for backward compatibility):
sql = "SELECT vendorid, vendor FROM vw_vendors_with_types WHERE isprinter=1 AND isactive=1 ORDER BY vendor ASC"

' NEW (Option 3 - Using JOIN with vendortypes):
sql = "SELECT v.vendorid, v.vendor FROM vendors v " & _
      "INNER JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid " & _
      "WHERE vt.vendortype='Printer' AND v.isactive=1 ORDER BY v.vendor ASC"
```

---

### High Priority: Direct Vendor Queries

#### Printer Management (7 files)

**1. addprinter.asp**
- **Line 90:** Vendor dropdown query - `WHERE isprinter = 1`
- **Change:** Use vendortypeid=2 or vw_vendors_with_types
- **Impact:** Add printer form vendor selection

**2. displayprinter.asp**
- **Line 291:** Edit form vendor dropdown - `WHERE isprinter = 1`
- **Uses:** RenderVendorOptions (from data_cache.asp)
- **Change:** Update query + ensure RenderVendorOptions updated first
- **Impact:** Edit printer inline form

**3. editprinter.asp**
- **Contains:** vendor flag usage (grep found it)
- **Action Required:** Full file review needed
- **Impact:** Standalone printer edit page

**4. saveprinter_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review for vendor validation/creation logic
- **Impact:** Printer save endpoint

**5-7. Additional Printer Files**
- Review required for complete audit

#### Machine Management (4 files)

**1. addmachine.asp**
- **Line 98:** `strSQL = "SELECT * FROM vendors WHERE ismachine = 1 AND isactive = 1 ORDER BY vendor ASC"`
- **Change:** Use vendortypeid=4 (Machine)
- **Impact:** Add machine form vendor dropdown

**2. displaymachine.asp**
- **Line 236:** `strSQL2 = "SELECT vendorid, vendor FROM vendors WHERE ismachine = 1 AND isactive = 1 ORDER BY vendor ASC"`
- **Change:** Use vendortypeid=4
- **Impact:** Edit machine inline form vendor dropdown

**3. editmacine.asp** (note: typo in filename)
- **Contains:** vendor flag usage
- **Action Required:** Full file review
- **Impact:** Standalone machine edit page

**4. savemachine_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review for vendor validation logic
- **Impact:** Machine save endpoint

#### PC/Device Management (4 files)

**1. displaypc.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review - may display vendor info
- **Impact:** PC detail page

**2. editdevice.asp**
- **Line 199:** `sqlVendor = "SELECT vendorid, vendor FROM vendors WHERE ispc = 1 ORDER BY vendor"`
- **Change:** Use vendortypeid=3 (PC)
- **Impact:** Device edit form vendor dropdown

**3. updatedevice_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review for vendor update logic
- **Impact:** Device update endpoint

**4. updatepc_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review for vendor update logic
- **Impact:** PC update endpoint

#### Model/Vendor Management (6 files)

**1. addmodel.asp**
- **Line 57:** `strSQL = "SELECT * FROM vendors WHERE isactive = 1 ORDER BY vendor ASC"`
- **Note:** No type filter! Shows ALL vendors
- **Change:** May need type filter dropdown or keep as-is
- **Impact:** Add model form - vendor selection

**2. savemodel.asp**
- **Line 71:** Vendor duplicate check query
- **Action Required:** Review vendor creation logic
- **Impact:** Model save with inline vendor creation

**3. savemodel_direct.asp**
- **Line 85:** Vendor duplicate check
- **Action Required:** Review vendor creation logic
- **Impact:** Direct model save endpoint

**4. addvendor.asp**
- **Contains:** vendor flag usage
- **Action Required:** CRITICAL - Form likely has checkboxes for all 6 types
- **Change:** Replace checkboxes with single dropdown (vendortypeid)
- **Impact:** Add vendor form UI changes required

**5. savevendor.asp**
- **Line 44:** Vendor duplicate check
- **Action Required:** Review - likely saves vendor type flags
- **Change:** Update to save vendortypeid instead
- **Impact:** Vendor save logic changes

**6. savevendor_direct.asp**
- **Line 40:** Vendor duplicate check
- **Action Required:** Review vendor save logic with type flags
- **Change:** Update to save vendortypeid
- **Impact:** Direct vendor save endpoint

#### Application Management (9 files)

**1. addapplication.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review - may be for related vendors
- **Impact:** TBD

**2. displayapplication.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

**3. editapplication.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

**4. editapplication_v2.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

**5. editapplication_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

**6. editapp_standalone.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

**7. saveapplication.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

**8. saveapplication_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

**9. quickadd_application.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review
- **Impact:** TBD

#### Knowledge Base (2 files)

**1. addlink_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review - likely minimal
- **Impact:** TBD

**2. updatelink_direct.asp**
- **Contains:** vendor flag usage
- **Action Required:** Review - likely minimal
- **Impact:** TBD

---

## Part 2: Infrastructure Device Management (NEW DEVELOPMENT REQUIRED)

### Current State: NO DEDICATED PAGES EXIST

The database has tables for:
- `servers` (with serverid, serialnumber, ipaddress, description, maptop, mapleft, isactive)
- `switches` (with switchid, serialnumber, ipaddress, description, maptop, mapleft, isactive)
- `cameras` (with cameraid, serialnumber, ipaddress, description, maptop, mapleft, isactive)

**But there are NO ASP pages to manage them!**

### Required New Pages

#### Server Management (4 files needed)
1. **displayservers.asp** - List all servers
2. **displayserver.asp** - Server detail page with inline edit
3. **addserver.asp** - Add new server form (with model/vendor support)
4. **saveserver_direct.asp** - Server save endpoint

#### Switch Management (4 files needed)
1. **displayswitches.asp** - List all switches
2. **displayswitch.asp** - Switch detail page with inline edit
3. **addswitch.asp** - Add new switch form (with model/vendor support)
4. **saveswitch_direct.asp** - Switch save endpoint

#### Camera Management (4 files needed)
1. **displaycameras.asp** - List all cameras
2. **displaycamera.asp** - Camera detail page with inline edit
3. **addcamera.asp** - Add new camera form (with model/vendor support)
4. **savecamera_direct.asp** - Camera save endpoint

### Existing Pages That May Display Infrastructure Data

**network_map.asp** - Network topology map
- **Action Required:** Review to see if servers/switches/cameras are displayed
- **Change:** May need to add vendor/model info if displayed

**printer_installer_map.asp** - Printer map
- **Action Required:** Review
- **Change:** Unlikely to need changes

**printermap.asp** - Another printer map
- **Action Required:** Review
- **Change:** Unlikely to need changes

---

## Part 3: Vendor Type Reference IDs

After migration, use these IDs:

| vendortypeid | vendortype | Description |
|--------------|------------|-------------|
| 1 | TBD | Default/unassigned |
| 2 | Printer | Printer manufacturers |
| 3 | PC | Computer manufacturers |
| 4 | Machine | CNC machine manufacturers |
| 5 | Server | Server manufacturers |
| 6 | Switch | Network switch manufacturers |
| 7 | Camera | Security camera manufacturers |

---

## Part 4: Implementation Strategy

### Phase 1: Database Migration
1. ✅ Migration scripts already created
2. Run `add_infrastructure_vendor_model_support.sql`
3. Run `refactor_vendor_types.sql`
4. Verify both migrations successful

### Phase 2: Core Infrastructure (Most Critical)
1. **Update includes/data_cache.asp first** (affects everything)
   - Update existing vendor query functions
   - Add new infrastructure vendor/model functions
2. Test that dropdowns still work

### Phase 3: Vendor Management Pages (Critical)
1. Update **addvendor.asp** - Change UI from checkboxes to dropdown
2. Update **savevendor.asp** and **savevendor_direct.asp** - Save vendortypeid instead of flags
3. Test vendor creation/editing

### Phase 4: Update Existing Device Pages (High Priority)
1. Printer pages (7 files) - Use vendortypeid=2
2. Machine pages (4 files) - Use vendortypeid=4
3. PC pages (4 files) - Use vendortypeid=3
4. Model management (3 files)
5. Test all existing functionality

### Phase 5: Create Infrastructure Pages (New Development)
1. Create server management pages (4 files)
2. Create switch management pages (4 files)
3. Create camera management pages (4 files)
4. Add navigation links
5. Test infrastructure CRUD operations

### Phase 6: Application/KB Pages (Lower Priority)
1. Review and update application pages (9 files)
2. Review and update KB pages (2 files)
3. These likely have minimal vendor flag usage

### Phase 7: Testing & Documentation
1. Full regression testing
2. Update user documentation
3. Update technical documentation

---

## Part 5: Code Pattern Templates

### Template 1: Simple Vendor Dropdown (Direct ID)
```vbscript
' Get printer vendors (vendortypeid = 2)
strSQL = "SELECT vendorid, vendor FROM vendors WHERE vendortypeid = 2 AND isactive = 1 ORDER BY vendor ASC"
Set rsVendors = objConn.Execute(strSQL)
```

### Template 2: Vendor Dropdown (With JOIN)
```vbscript
' Get machine vendors with type name
strSQL = "SELECT v.vendorid, v.vendor, vt.vendortype " & _
         "FROM vendors v " & _
         "INNER JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid " & _
         "WHERE vt.vendortype = 'Machine' AND v.isactive = 1 " & _
         "ORDER BY v.vendor ASC"
Set rsVendors = objConn.Execute(strSQL)
```

### Template 3: Using Compatibility View (Migration Phase)
```vbscript
' Temporary: Use view during migration
strSQL = "SELECT vendorid, vendor FROM vw_vendors_with_types WHERE isprinter = 1 AND isactive = 1 ORDER BY vendor ASC"
Set rsVendors = objConn.Execute(strSQL)
```

### Template 4: Model Dropdown with Vendor (Infrastructure)
```vbscript
' Get server models with vendor info
strSQL = "SELECT m.modelnumberid, m.modelnumber, v.vendor " & _
         "FROM models m " & _
         "INNER JOIN vendors v ON m.vendorid = v.vendorid " & _
         "WHERE v.vendortypeid = 5 AND m.isactive = 1 " & _
         "ORDER BY m.modelnumber ASC"
Set rsModels = objConn.Execute(strSQL)
```

### Template 5: Infrastructure Device with Model/Vendor Display
```vbscript
' Display server with model and vendor
strSQL = "SELECT s.*, m.modelnumber, v.vendor " & _
         "FROM servers s " & _
         "LEFT JOIN models m ON s.modelid = m.modelnumberid " & _
         "LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
         "WHERE s.serverid = ? AND s.isactive = 1"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(serverid))
```

### Template 6: Save Infrastructure Device
```vbscript
' Insert server with model
Dim modelid, serialnumber, ipaddress, description
modelid = GetSafeInteger("FORM", "modelid", 0, 0, 999999)
serialnumber = GetSafeString("FORM", "serialnumber", "", 0, 100, "^[A-Za-z0-9\-]+$")
ipaddress = GetSafeString("FORM", "ipaddress", "", 0, 15, "^[0-9\.]+$")
description = GetSafeString("FORM", "description", "", 0, 255, "")

strSQL = "INSERT INTO servers (modelid, serialnumber, ipaddress, description, isactive) " & _
         "VALUES (?, ?, ?, ?, 1)"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(modelid, serialnumber, ipaddress, description))
```

---

## Part 6: Testing Checklist

### Vendor Type Refactoring Tests
- [ ] All vendor dropdowns display correct vendors (printer, PC, machine)
- [ ] Vendor add/edit form changed from checkboxes to dropdown
- [ ] Vendor save correctly sets vendortypeid
- [ ] Existing printers/machines/PCs display correct vendor info
- [ ] Model add/edit shows correct vendors based on type
- [ ] Search functionality still works with vendor queries

### Infrastructure Support Tests
- [ ] Can add server with model/vendor selection
- [ ] Can edit server model/vendor
- [ ] Can add switch with model/vendor selection
- [ ] Can edit switch model/vendor
- [ ] Can add camera with model/vendor selection
- [ ] Can edit camera model/vendor
- [ ] Server/switch/camera lists display vendor/model info
- [ ] vw_network_devices view returns correct data
- [ ] Infrastructure devices show on network map (if implemented)

### Data Integrity Tests
- [ ] No SQL errors on any page
- [ ] All foreign keys working correctly
- [ ] Compatibility view returns correct data during migration
- [ ] Old boolean flags match new vendortypeid values
- [ ] No orphaned records after migration

---

## Part 7: Risk Assessment

### High Risk Areas
1. **includes/data_cache.asp** - Used by many pages, breaking this breaks everything
2. **addvendor.asp / savevendor.asp** - UI changes required, not just query updates
3. **Application pages** - Unknown vendor usage, need detailed review

### Medium Risk Areas
1. Printer/Machine/PC pages - Well-documented, straightforward updates
2. Model management - Some inline vendor creation logic

### Low Risk Areas
1. KB pages - Likely minimal vendor interaction
2. Display-only pages - Read queries only, easy to update

### Mitigation Strategies
1. **Use compatibility view initially** - Minimal code changes, easy rollback
2. **Test data_cache.asp first** - If this works, 80% of dropdowns work
3. **Keep old boolean columns** - Don't drop until fully validated
4. **Create infrastructure pages incrementally** - Server first, then switch, then camera

---

## Part 8: File Change Priority Matrix

| Priority | Files | Reason | Est. Hours |
|----------|-------|--------|------------|
| 🔴 P0 | includes/data_cache.asp | Affects all dropdowns | 2-3h |
| 🔴 P1 | addvendor.asp, savevendor*.asp | UI changes required | 3-4h |
| 🟡 P2 | Printer pages (7 files) | High usage feature | 4-5h |
| 🟡 P2 | Machine pages (4 files) | High usage feature | 3-4h |
| 🟡 P2 | PC pages (4 files) | High usage feature | 3-4h |
| 🟢 P3 | Model management (3 files) | Backend only | 2-3h |
| 🟢 P3 | Create server pages (4 files) | New development | 6-8h |
| 🟢 P3 | Create switch pages (4 files) | New development | 4-6h |
| 🟢 P3 | Create camera pages (4 files) | New development | 4-6h |
| 🟢 P4 | Application pages (9 files) | Low vendor interaction | 4-6h |
| 🟢 P4 | KB pages (2 files) | Minimal changes | 1-2h |

**Total Estimated Time:** 36-54 hours

---

## Part 9: Files Not Requiring Changes

The following files were checked and **do NOT** reference vendors or infrastructure tables:
- default.asp (dashboard)
- calendar.asp
- search.asp (searches content, not vendors directly)
- displaynotifications.asp
- displaysubnets.asp
- All other display*.asp not listed in audit

---

## Part 10: Next Steps

1. **Review and approve this audit**
2. **Run database migrations** (add_infrastructure_vendor_model_support.sql + refactor_vendor_types.sql)
3. **Create vendor_helpers.asp** include file
4. **Update includes/data_cache.asp** (P0 - most critical)
5. **Test vendor dropdowns** across application
6. **Begin P1-P4 file updates** in priority order
7. **Create infrastructure CRUD pages**
8. **Full regression testing**
9. **Document and deploy**

---

**Audit Completed By:** Claude Code
**Audit Date:** 2025-10-23
**Status:** Ready for Implementation
**Next Action:** Review audit and approve implementation plan

