Complete Phase 2 PC migration and network device infrastructure updates

This commit captures 20 days of development work (Oct 28 - Nov 17, 2025)
including Phase 2 PC migration, network device unification, and numerous
bug fixes and enhancements.

## Major Changes

### Phase 2: PC Migration to Unified Machines Table
- Migrated all PCs from separate `pc` table to unified `machines` table
- PCs identified by `pctypeid IS NOT NULL` in machines table
- Updated all display, add, edit, and update pages for PC functionality
- Comprehensive testing: 15 critical pages verified working

### Network Device Infrastructure Unification
- Unified network devices (Switches, Servers, Cameras, IDFs, Access Points)
  into machines table using machinetypeid 16-20
- Updated vw_network_devices view to query both legacy tables and machines table
- Enhanced network_map.asp to display all device types from machines table
- Fixed location display for all network device types

### Machine Management System
- Complete machine CRUD operations (Create, Read, Update, Delete)
- 5-tab interface: Basic Info, Network, Relationships, Compliance, Location
- Support for multiple network interfaces (up to 3 per machine)
- Machine relationships: Controls (PC→Equipment) and Dualpath (redundancy)
- Compliance tracking with third-party vendor management

### Bug Fixes (Nov 7-14, 2025)
- Fixed editdevice.asp undefined variable (pcid → machineid)
- Migrated updatedevice.asp and updatedevice_direct.asp to Phase 2 schema
- Fixed network_map.asp to show all network device types
- Fixed displaylocation.asp to query machines table for network devices
- Fixed IP columns migration and compliance column handling
- Fixed dateadded column errors in network device pages
- Fixed PowerShell API integration issues
- Simplified displaypcs.asp (removed IP and Machine columns)

### Documentation
- Created comprehensive session summaries (Nov 10, 13, 14)
- Added Machine Quick Reference Guide
- Documented all bug fixes and migrations
- API documentation for ASP endpoints

### Database Schema Updates
- Phase 2 migration scripts for PC consolidation
- Phase 3 migration scripts for network devices
- Updated views to support hybrid table approach
- Sample data creation/removal scripts for testing

## Files Modified (Key Changes)
- editdevice.asp, updatedevice.asp, updatedevice_direct.asp
- network_map.asp, network_devices.asp, displaylocation.asp
- displaypcs.asp, displaypc.asp, displaymachine.asp
- All machine management pages (add/edit/save/update)
- save_network_device.asp (fixed machine type IDs)

## Testing Status
- 15 critical pages tested and verified
- Phase 2 PC functionality: 100% working
- Network device display: 100% working
- Security: All queries use parameterized commands

## Production Readiness
- Core functionality complete and tested
- 85% production ready
- Remaining: Full test coverage of all 123 ASP pages

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
cproudlock
2025-11-17 20:04:06 -05:00
commit 4bcaf0913f
1954 changed files with 434785 additions and 0 deletions

View File

@@ -0,0 +1,586 @@
# Classic ASP/VBScript Development Guide
## Overview
**shopdb** is a Classic ASP application using VBScript running on IIS Express in Windows 11 VM.
- **Language:** VBScript (Classic ASP)
- **Server:** IIS Express (Windows 11 VM)
- **Database:** MySQL 5.6 (Docker container on Linux host)
- **Development:** Edit files on Linux with Claude Code, test on Windows/IIS
## Project Setup
### Location
- **Linux:** `~/projects/windows/shopdb/`
- **Windows:** `Z:\shopdb\`
- **IIS Config:** Points to `Z:\shopdb\`
### Database Connection
```vbscript
<%
' Connection string for shopdb
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "Driver={MySQL ODBC 8.0 Driver};" & _
"Server=192.168.122.1;" & _
"Port=3306;" & _
"Database=shopdb;" & _
"User=570005354;" & _
"Password=570005354;" & _
"Option=3;"
conn.Open
' Use the connection
' ... your code here ...
conn.Close
Set conn = Nothing
%>
```
## Database Credentials
**Production Database: shopdb**
- **Host (from Windows):** 192.168.122.1
- **Port:** 3306
- **Database:** shopdb
- **User:** 570005354
- **Password:** 570005354
## Prerequisites in Windows VM
### Required Software
1. **MySQL ODBC 8.0 Driver**
- Download: https://dev.mysql.com/downloads/connector/odbc/
- Install 64-bit version
- Used by Classic ASP to connect to MySQL
2. **IIS Express**
- Already installed
- Location: `C:\Program Files\IIS Express\`
### Windows Configuration
- **Z: Drive** mapped to `\\192.168.122.1\windows-projects`
- **Firewall** allows port 8080 inbound
- **URL ACL** configured: `netsh http add urlacl url=http://*:8080/ user="Everyone"`
### Auto-Start IIS Express on Windows Boot
To automatically start IIS Express when Windows boots:
1. **In Windows, open Task Scheduler** (search for "Task Scheduler")
2. **Create a new task:**
- Click "Create Task..." (not "Create Basic Task")
- **General tab:**
- Name: `Start IIS Express - shopdb`
- Description: `Auto-start IIS Express for shopdb site`
- Check "Run with highest privileges"
- Check "Run whether user is logged on or not"
- Configure for: Windows 10/11
- **Triggers tab:**
- Click "New..."
- Begin the task: "At startup"
- Delay task for: 30 seconds (gives network time to connect)
- Click OK
- **Actions tab:**
- Click "New..."
- Action: "Start a program"
- Program/script: `wscript.exe`
- Add arguments: `Z:\start-iis-shopdb.vbs`
- Click OK
- **Conditions tab:**
- Uncheck "Start the task only if the computer is on AC power"
- Check "Wake the computer to run this task" (optional)
- **Settings tab:**
- Check "Allow task to be run on demand"
- Check "Run task as soon as possible after a scheduled start is missed"
- If the task is already running: "Do not start a new instance"
- Click OK to save
3. **Test the task:**
- Right-click the task in Task Scheduler
- Click "Run"
- Check http://localhost:8080 in browser
- Should see shopdb running
4. **Verify on next boot:**
- Restart Windows VM
- Wait 30 seconds after login
- Check http://192.168.122.151:8080 from Linux
- IIS Express should be running automatically
**Files Created:**
- `Z:\start-iis-shopdb.bat` - Batch file to start IIS Express
- `Z:\start-iis-shopdb.vbs` - VBScript wrapper (runs silently, no console window)
**Manual Start (if needed):**
```powershell
# In Windows, double-click:
Z:\start-iis-shopdb.vbs
# Or run from PowerShell:
wscript.exe Z:\start-iis-shopdb.vbs
```
## Development Workflow
### 1. Edit Code on Linux
```bash
# Navigate to project
cd ~/projects/windows/shopdb
# Start Claude Code
claude
# Ask Claude to help with your ASP/VBScript code
# Example: "Create a VBScript function to query the database and display results"
```
### 2. Files Auto-Sync to Windows
- Any changes saved on Linux automatically appear in Windows at `Z:\shopdb\`
- No manual copying needed thanks to Samba share
### 3. Test on IIS Express
**In Windows PowerShell (as Administrator or with URL ACL):**
```powershell
cd "C:\Program Files\IIS Express"
.\iisexpress.exe /site:shopdb
```
**Access from Linux:**
- Browser: http://192.168.122.151:8080
**Access from Windows:**
- Browser: http://localhost:8080
### 4. Iterate
- Edit on Linux with Claude
- Refresh browser to see changes
- Debug and repeat
## Common VBScript/ASP Patterns
### Database Query (SELECT)
```vbscript
<%
Dim conn, rs, sql
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
conn.ConnectionString = "Driver={MySQL ODBC 8.0 Driver};Server=192.168.122.1;Port=3306;Database=shopdb;User=570005354;Password=570005354;"
conn.Open
sql = "SELECT * FROM products WHERE category = ?"
rs.Open sql, conn
Do While Not rs.EOF
Response.Write rs("product_name") & "<br>"
rs.MoveNext
Loop
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>
```
### Database Insert
```vbscript
<%
Dim conn, sql
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "Driver={MySQL ODBC 8.0 Driver};Server=192.168.122.1;Port=3306;Database=shopdb;User=570005354;Password=570005354;"
conn.Open
sql = "INSERT INTO orders (customer_id, order_date, total) VALUES (1, NOW(), 99.99)"
conn.Execute sql
conn.Close
Set conn = Nothing
Response.Write "Order inserted successfully"
%>
```
### Database Update
```vbscript
<%
Dim conn, sql, orderId
orderId = Request.Form("order_id")
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "Driver={MySQL ODBC 8.0 Driver};Server=192.168.122.1;Port=3306;Database=shopdb;User=570005354;Password=570005354;"
conn.Open
sql = "UPDATE orders SET status = 'completed' WHERE order_id = " & orderId
conn.Execute sql
conn.Close
Set conn = Nothing
Response.Redirect "orders.asp"
%>
```
### Form Handling
```vbscript
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
' Handle form submission
Dim name, email
name = Request.Form("name")
email = Request.Form("email")
' Validate and process
' ...
Response.Write "Form submitted successfully"
Else
' Display form
%>
<form method="post" action="submit.asp">
<input type="text" name="name" required>
<input type="email" name="email" required>
<button type="submit">Submit</button>
</form>
<%
End If
%>
```
### Include Files
```vbscript
<!-- #include file="config.asp" -->
<!-- #include file="header.asp" -->
<% ' Your page content here %>
<!-- #include file="footer.asp" -->
```
### Session Management
```vbscript
<%
' Set session variable
Session("user_id") = 123
Session("username") = "admin"
' Get session variable
If Session("user_id") <> "" Then
Response.Write "Welcome, " & Session("username")
Else
Response.Redirect "login.asp"
End If
' Clear session
Session.Abandon
%>
```
## Connection File Template
**Create: `~/projects/windows/shopdb/includes/db_connection.asp`**
```vbscript
<%
' Database connection configuration
Function GetConnection()
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "Driver={MySQL ODBC 8.0 Driver};" & _
"Server=192.168.122.1;" & _
"Port=3306;" & _
"Database=shopdb;" & _
"User=570005354;" & _
"Password=570005354;" & _
"Option=3;"
On Error Resume Next
conn.Open
If Err.Number <> 0 Then
Response.Write "Database connection failed: " & Err.Description
Response.End
End If
Set GetConnection = conn
End Function
%>
```
**Usage in other files:**
```vbscript
<!-- #include file="includes/db_connection.asp" -->
<%
Dim conn
Set conn = GetConnection()
' Use the connection
' ...
conn.Close
Set conn = Nothing
%>
```
## Testing Database Connection
**Create: `~/projects/windows/shopdb/test_connection.asp`**
```vbscript
<%@ Language=VBScript %>
<html>
<head>
<title>Database Connection Test</title>
</head>
<body>
<h1>MySQL Connection Test</h1>
<%
On Error Resume Next
Dim conn, rs
Set conn = Server.CreateObject("ADODB.Connection")
Response.Write "<p>Attempting to connect to shopdb...</p>"
conn.ConnectionString = "Driver={MySQL ODBC 8.0 Driver};" & _
"Server=192.168.122.1;" & _
"Port=3306;" & _
"Database=shopdb;" & _
"User=570005354;" & _
"Password=570005354;"
conn.Open
If Err.Number <> 0 Then
Response.Write "<p style='color:red;'><strong>Connection Failed!</strong></p>"
Response.Write "<p>Error: " & Err.Description & "</p>"
Else
Response.Write "<p style='color:green;'><strong>Connection Successful!</strong></p>"
' Test query
Set rs = conn.Execute("SELECT VERSION() as version, DATABASE() as db")
Response.Write "<p>MySQL Version: " & rs("version") & "</p>"
Response.Write "<p>Current Database: " & rs("db") & "</p>"
rs.Close
Set rs = Nothing
conn.Close
End If
Set conn = Nothing
%>
</body>
</html>
```
## Troubleshooting
### Can't Connect to MySQL
**Check from Windows PowerShell:**
```powershell
# Test network connectivity
Test-NetConnection -ComputerName 192.168.122.1 -Port 3306
# Should show: TcpTestSucceeded : True
```
**Check MySQL is running on Linux:**
```bash
docker ps | grep mysql
docker compose logs mysql
```
### ODBC Driver Not Found
**Error:** `[Microsoft][ODBC Driver Manager] Data source name not found`
**Solution:**
1. Install MySQL ODBC 8.0 Driver in Windows
2. Verify in Control Panel → Administrative Tools → ODBC Data Sources
3. Check driver name matches in connection string
### Permission Denied
**Error:** Access denied for user '570005354'
**Solution on Linux:**
```bash
# Re-grant permissions
docker exec -it dev-mysql mysql -u root -prootpassword -e "
GRANT ALL PRIVILEGES ON shopdb.* TO '570005354'@'%' IDENTIFIED BY '570005354';
FLUSH PRIVILEGES;
"
```
### IIS Express Won't Start
**Check:**
1. Another process using port 8080? Check Task Manager
2. URL ACL configured? Run as Admin or check: `netsh http show urlacl`
3. applicationhost.config correct? Check binding: `*:8080:*`
### Changes Not Appearing
**Solutions:**
1. Hard refresh browser: `Ctrl + F5`
2. Clear browser cache
3. Check file actually saved on Linux: `ls -la ~/projects/windows/shopdb/`
4. Check Samba: `sudo systemctl status smbd`
## MySQL 5.6 Limitations
Our MySQL version (5.6) doesn't support:
- JSON data type (use TEXT and parse)
- `CREATE USER IF NOT EXISTS` syntax
- Some newer functions
**User management in MySQL 5.6:**
```sql
-- Create/update user
GRANT ALL PRIVILEGES ON shopdb.* TO 'username'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
```
## Security Notes
⚠️ **Development Environment Only**
These credentials are for DEVELOPMENT:
- User: 570005354
- Password: 570005354
**For Production:**
- Use strong, unique passwords
- Implement proper authentication
- Use SSL/TLS connections
- Restrict database access by IP
- Never commit credentials to Git
## Quick Commands Reference
### Start Development
```bash
# On Linux
~/start-dev-env.sh
# In Windows
cd "C:\Program Files\IIS Express"
.\iisexpress.exe /site:shopdb
# Open browser to: http://192.168.122.151:8080
```
### Edit Code
```bash
# On Linux
cd ~/projects/windows/shopdb
claude
```
### Check Database
```bash
# On Linux
docker exec -it dev-mysql mysql -u 570005354 -p570005354 shopdb
```
### Backup Database
```bash
# On Linux
docker exec dev-mysql mysqldump -u 570005354 -p570005354 shopdb > ~/backups/shopdb-$(date +%Y%m%d).sql
```
### Restore Database
```bash
# On Linux
docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < backup.sql
```
## Using Claude Code for ASP/VBScript
**Good Prompts:**
```
"Create a VBScript function to display all products from the database in an HTML table"
"Add error handling to this database query in Classic ASP"
"Create a login form in Classic ASP that checks credentials against the users table"
"Write VBScript code to handle a POST form submission and insert into database"
"Create a pagination system for displaying database results in Classic ASP"
```
**Be Specific:**
```
"I'm using Classic ASP with VBScript and MySQL 5.6. Create a page that..."
```
### Starting a Claude Code Session
When beginning work, tell Claude to "start up" or "let's start the dev environment". Claude will automatically:
1. Review all .md documentation files
2. Run `~/start-dev-env.sh` to start Docker containers and Windows VM
3. Check service status to ensure everything is running
4. Load the todo list to continue from where you left off
### Closing Out a Claude Code Session
When you're done working, tell Claude to "close out" or "we're closing out for now". Claude will automatically:
1. Update and consolidate the todo list with completed work
2. Mark completed phases/tasks
3. Run `~/stop-dev-env.sh` to properly shutdown the environment
4. Update relevant documentation
This ensures your development environment is properly shut down and all progress is tracked.
## Project Structure Example
```
shopdb/
├── index.asp # Homepage
├── test_connection.asp # Database test page
├── includes/
│ ├── db_connection.asp # Database connection function
│ ├── header.asp # Common header
│ └── footer.asp # Common footer
├── admin/
│ ├── login.asp # Admin login
│ └── dashboard.asp # Admin dashboard
├── css/
│ └── styles.css # Stylesheets
├── js/
│ └── scripts.js # JavaScript files
└── images/
└── logo.png # Images
```
## Additional Notes
- **No PHP on Windows** - PHP development is done via Docker/Nginx on Linux (port 8080)
- **ASP on Windows only** - Classic ASP runs on IIS Express in Windows VM
- **Database shared** - Both PHP (Docker) and ASP (Windows) can access the same MySQL
- **File editing** - Always edit on Linux with Claude Code, files sync automatically to Windows
---
**Technology Stack Summary:**
- Classic ASP with VBScript
- IIS Express on Windows 11
- MySQL 5.6 (Docker/Linux)
- Samba for file sharing
- Claude Code for development assistance

View File

@@ -0,0 +1,357 @@
# Complete Database Refactoring - Executive Summary
**Date**: 2025-11-06
**Status**: DESIGN PHASE - Ready for Review
**Scope**: MASSIVE - Complete database restructuring
---
## Overview
This document provides a high-level overview of the complete database refactoring project. This is a comprehensive modernization effort that will:
1. **Consolidate PCs into Machines** - Eliminate duplicate entity tracking
2. **Create Generic Communications Infrastructure** - Flexible, extensible communication tracking
3. **Implement Warranty Management System** - Professional warranty tracking for all assets
---
## Three Major Components
### Component 1: PC-to-Machines Consolidation
**Document**: `PC_MACHINES_CONSOLIDATION_PLAN.md`
**What**: Merge the `pc` table (277 records) into `machines` table (266 records)
**Why**:
- Eliminate duplicate entity tracking
- Simplify data model
- Unified asset management
**Impact**:
- 543 total machines after consolidation
- 10 ASP files need updates (direct queries)
- 30 ASP files affected (use pc fields/views)
- 19 database views need updates
- 6 related tables need updates
### Component 2: Communications Infrastructure
**Document**: `PC_MACHINES_CONSOLIDATION_PLAN.md` (Part 2)
**What**: Create generic communications system supporting multiple types
**New Tables**:
- `comstypes` - Communication types (IP, Serial, Network, USB, etc.)
- `communications` - Universal communication tracking
**Features**:
- Supports IP addresses, serial ports, network interfaces
- Extensible for future communication types
- Links to machines via machineid
- Replaces pc_comm_config and pc_network_interfaces
### Component 3: Warranty Management System
**Document**: `WARRANTY_MANAGEMENT_DESIGN.md`
**What**: Professional warranty tracking system for all machines
**New Tables**:
- `warrantytypes` - Types of coverage
- `warranties` - Individual warranty records (links to existing vendors table)
- `warrantyhistory` - Audit trail for changes
**Features**:
- Multiple warranties per machine
- Warranty renewal tracking
- Automated expiration notifications
- Cost tracking
- Historical audit trail
---
## Database Changes Summary
### New Tables (5 total)
| Table | Records | Purpose |
|-------|---------|---------|
| comstypes | ~7 | Communication type definitions |
| communications | ~500+ | Machine communication records |
| warrantytypes | ~8 | Warranty coverage types |
| warranties | ~277+ | Individual warranty records (uses existing vendors table) |
| warrantyhistory | ~0 | Warranty change audit trail |
### Modified Tables (1)
| Table | Changes | New Columns |
|-------|---------|-------------|
| machines | Extended for PC data | +9 columns (hostname, serialnumber, osid, pctypeid, etc.) |
### Deprecated Tables (To be removed after migration)
| Table | Replacement | When to Remove |
|-------|-------------|----------------|
| pc | machines | After 30-day testing period |
| pc_comm_config | communications | After data migration verified |
| pc_network_interfaces | communications | After data migration verified |
### Views to Update (19)
All views currently using the `pc` table will be updated to use `machines` with compatibility maintained.
---
## Migration Execution Order
### Phase 1: Infrastructure Setup (Scripts 01-04)
**Reversible**: Yes
1. ✅ Script 01: Create communications infrastructure
2. ✅ Script 02: Extend machines table
3. ✅ Script 03: Create PC machine types
4. ✅ Script 04: Create warranty infrastructure
**Production Deployment**: Run during maintenance window
**Estimated Time**: 15 minutes
**Rollback**: Rollback scripts 01-04 available
### Phase 2: Data Migration (Scripts 05-08)
**Reversible**: Yes (with backups)
5. ⏳ Script 05: Migrate PC data to machines
6. ⏳ Script 06: Migrate communication data
7. ⏳ Script 07: Migrate warranty data
8. ⏳ Script 08: Update relationship tables
**Production Deployment**: Run during maintenance window
**Estimated Time**: 30-45 minutes
**Rollback**: Restore from backup
### Phase 3: Application Updates (Manual)
**Reversible**: Yes (via version control)
9. ⏳ Update 19 database views
10. ⏳ Create compatibility views
11. ⏳ Update 10 ASP files (direct queries)
12. ⏳ Update 30 ASP files (pc fields/views)
13. ⏳ Test all functionality
**Production Deployment**: Deploy with application
**Estimated Time**: 2-3 weeks development + testing
**Rollback**: Revert code deployment
### Phase 4: Cleanup (After 30 days)
**Reversible**: No - PERMANENT
14. ⏳ Drop pc table
15. ⏳ Drop pc_comm_config table
16. ⏳ Drop pc_network_interfaces table
17. ⏳ Drop compatibility views
18. ⏳ Remove deprecated columns
**Production Deployment**: Final cleanup
**Estimated Time**: 15 minutes
**Rollback**: None (irreversible)
---
## Complete SQL Script List
### Creation Scripts
1. `sql/01_create_communications_infrastructure.sql`
2. `sql/02_extend_machines_table.sql`
3. `sql/03_create_pc_machine_types.sql`
4. `sql/04_create_warranty_infrastructure.sql`
### Migration Scripts (TBD)
5. `sql/05_migrate_pc_to_machines.sql`
6. `sql/06_migrate_communications.sql`
7. `sql/07_migrate_warranties.sql`
8. `sql/08_update_relationships.sql`
### View Scripts (TBD)
9. `sql/09_update_views.sql`
10. `sql/10_create_compatibility_views.sql`
### Rollback Scripts
- `sql/ROLLBACK_01_communications_infrastructure.sql`
- `sql/ROLLBACK_02_machines_table_extensions.sql`
- `sql/ROLLBACK_03_pc_machine_types.sql` (delete from machinetypes)
- `sql/ROLLBACK_04_warranty_infrastructure.sql`
---
## Benefits of This Refactoring
### Before Refactoring
**Data Model**:
- ❌ Machines and PCs tracked separately
- ❌ Warranty data scattered across PC records
- ❌ Communication data in PC-specific tables
- ❌ Multiple machines types systems (machines.machinetypeid, pc.pctypeid)
- ❌ Rigid communication tracking
- ❌ No warranty history
- ❌ Limited to one warranty per PC
**Code**:
- ❌ Duplicate logic for machines vs PCs
- ❌ Complex joins across pc/machines
- ❌ PC-specific ASP files
- ❌ Warranty tracking only for PCs
### After Refactoring
**Data Model**:
- ✅ Unified machine tracking (all assets)
- ✅ Professional warranty management
- ✅ Generic communication infrastructure
- ✅ Single machine type system
- ✅ Extensible for new communication types
- ✅ Complete warranty audit trail
- ✅ Multiple warranties per machine
**Code**:
- ✅ Single codebase for all machines
- ✅ Simpler queries
- ✅ Unified ASP files
- ✅ Warranty tracking for ALL machines
- ✅ Better reporting capabilities
---
## Risk Assessment
| Risk | Severity | Likelihood | Mitigation |
|------|----------|------------|------------|
| Data loss during migration | CRITICAL | Low | Full backups, dev testing, rollback scripts |
| Extended downtime | HIGH | Medium | Parallel testing, staged deployment |
| View compatibility issues | HIGH | Medium | Compatibility views, thorough testing |
| Performance degradation | MEDIUM | Low | Indexes optimized, query testing |
| Application bugs | MEDIUM | Medium | Comprehensive testing, UAT |
| User confusion | LOW | Medium | Documentation, training |
---
## Timeline & Effort Estimate
| Phase | Duration | Resources |
|-------|----------|-----------|
| Design & Review | 1-2 days | ✅ COMPLETE |
| SQL Script Development | 3-5 days | ⏳ In Progress |
| Dev Environment Testing | 3-5 days | ⏳ Pending |
| ASP File Updates | 5-7 days | ⏳ Pending |
| Integration Testing | 3-5 days | ⏳ Pending |
| User Acceptance Testing | 2-3 days | ⏳ Pending |
| Production Deployment | 1 day | ⏳ Pending |
| Monitoring Period | 30 days | ⏳ Pending |
| Final Cleanup | 1 day | ⏳ Pending |
| **TOTAL** | **20-35 days** | |
---
## Key Success Metrics
### Data Integrity
- [ ] All 277 PCs migrated to machines
- [ ] All communication records migrated
- [ ] All warranty records migrated
- [ ] All FK relationships intact
- [ ] Zero data loss
### Functionality
- [ ] All 19 views return correct data
- [ ] All ASP files working correctly
- [ ] Warranty tracking functional
- [ ] Communication tracking functional
- [ ] Reports generating correctly
### Performance
- [ ] Query performance same or better
- [ ] Page load times acceptable
- [ ] Database size reduced (from consolidation)
---
## Production Deployment Checklist
### Pre-Deployment
- [ ] All SQL scripts tested on dev
- [ ] All ASP files tested
- [ ] All views validated
- [ ] Performance testing complete
- [ ] UAT sign-off received
- [ ] Rollback plan documented
- [ ] Full database backup created
- [ ] Maintenance window scheduled
- [ ] Users notified
### Deployment Day
- [ ] Verify backup completed
- [ ] Run scripts 01-04 (infrastructure)
- [ ] Verify tables created
- [ ] Run scripts 05-08 (data migration)
- [ ] Verify data migrated
- [ ] Update views
- [ ] Deploy ASP files
- [ ] Smoke test functionality
- [ ] Monitor for errors
### Post-Deployment
- [ ] Verify all functionality
- [ ] Monitor performance
- [ ] Check error logs
- [ ] User feedback collection
- [ ] Document issues
- [ ] Plan fixes if needed
### 30-Day Cleanup
- [ ] Verify stability
- [ ] Run cleanup scripts
- [ ] Remove old tables
- [ ] Update documentation
- [ ] Close project
---
## Documentation Index
1. **PC_MACHINES_CONSOLIDATION_PLAN.md** - Complete PC consolidation design
2. **WARRANTY_MANAGEMENT_DESIGN.md** - Warranty system design
3. **COMPLETE_REFACTORING_SUMMARY.md** - This document (overview)
---
## Questions & Approvals
### Design Decisions Needed
- [ ] Approve warranty system design
- [ ] Approve communications infrastructure
- [ ] Approve PC machine type mapping
- [ ] Set maintenance window date
### Approvals Required
- [ ] Database Administrator
- [ ] Lead Developer
- [ ] System Owner
- [ ] Business Stakeholder
---
## Support & Contact
**For questions about this refactoring:**
- Review design documents first
- Check rollback procedures
- Test on dev environment
- Contact system administrator
---
**Status**: DESIGN PHASE COMPLETE ✅
**Next Step**: Create migration scripts (scripts 05-08)
**Target Deployment**: TBD
---
*This is a living document and will be updated as the project progresses.*

View File

@@ -0,0 +1,696 @@
# Database Migration - Final Design Specification
**Date Created**: 2025-11-06
**Status**: DESIGN FINALIZED - Ready for SQL Script Creation
**Version**: 2.0 - Simplified Design
---
## Executive Summary
This document outlines the finalized database migration plan that:
1. Consolidates PCs into machines table (543 total machines)
2. Creates generic communications infrastructure
3. Adds compliance tracking from inventory.xlsx
4. Implements simplified warranty management
5. Adds liaison tracking in businessunits
6. Implements machine relationships tracking (dualpath, controller associations)
**Key Design Principles:**
- ✅ Simplicity over complexity
- ✅ Generic/flexible structures (address field in communications)
- ✅ Leverage existing tables (vendors for third-party management)
- ✅ Minimal columns (remove unnecessary audit fields)
---
## NEW TABLES (7 Tables)
### 1. `comstypes` - Communication Types Lookup
Defines types of communication methods available.
```sql
CREATE TABLE comstypes (
comstypeid INT(11) PRIMARY KEY AUTO_INCREMENT,
typename VARCHAR(50) NOT NULL UNIQUE,
description VARCHAR(255),
addresslabel VARCHAR(50), -- 'IP Address', 'COM Port', 'Interface Name'
requiresport TINYINT(1) DEFAULT 0, -- Does this type use port field?
settingsschema TEXT, -- JSON schema for validation (optional)
displayorder INT(11) DEFAULT 0,
isactive TINYINT(1) DEFAULT 1,
KEY idx_isactive (isactive),
KEY idx_displayorder (displayorder)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Initial Data:**
```sql
INSERT INTO comstypes (typename, description, addresslabel, requiresport, displayorder) VALUES
('IP', 'IP-based communication', 'IP Address', 1, 1),
('Serial', 'Serial port communication', 'COM Port', 0, 2),
('Network Interface', 'Network adapter/interface', 'Interface Name', 0, 3),
('USB', 'USB connection', 'USB Device ID', 0, 4),
('Parallel', 'Parallel port', 'LPT Port', 0, 5),
('VNC', 'VNC remote access', 'IP Address', 1, 6),
('FTP', 'FTP connection', 'IP Address', 1, 7);
```
**Columns: 8**
---
### 2. `communications` - Universal Communication Tracking
**KEY INNOVATION**: Generic `address` field replaces specific fields (ipaddress, portname, etc.)
```sql
CREATE TABLE communications (
comid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL,
comstypeid INT(11) NOT NULL,
-- GENERIC FIELDS
address VARCHAR(255), -- Universal: '192.168.1.1', 'COM1', 'eth0', 'USB-001'
port INT(11), -- Port/socket number (for IP types)
macaddress VARCHAR(17), -- MAC address (network types)
description VARCHAR(255), -- Human-readable description
-- TYPE-SPECIFIC SETTINGS
settings JSON, -- Flexible config (baud rate, subnet, dhcp, etc.)
-- STATUS
isactive TINYINT(1) DEFAULT 1,
KEY idx_machineid (machineid),
KEY idx_comstypeid (comstypeid),
KEY idx_address (address),
KEY idx_isactive (isactive),
CONSTRAINT fk_communications_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid),
CONSTRAINT fk_communications_comstypeid FOREIGN KEY (comstypeid) REFERENCES comstypes(comstypeid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Examples:**
| comstypeid | Type | address | port | settings |
|------------|------|---------|------|----------|
| 1 | IP | `192.168.1.100` | `8080` | `{"protocol":"tcp"}` |
| 2 | Serial | `COM1` | NULL | `{"baud":9600,"databits":8,"parity":"None"}` |
| 3 | Network Interface | `eth0` | NULL | `{"subnet":"255.255.255.0","gateway":"192.168.1.1","dhcp":true}` |
**Columns: 9**
**Replaces:**
- `pc_comm_config`
- `pc_network_interfaces`
- `machines.ipaddress1`
- `machines.ipaddress2`
---
### 3. `compliance` - Compliance & Security Tracking
Data sourced from inventory.xlsx spreadsheet.
```sql
CREATE TABLE compliance (
complianceid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL,
-- Network & Software
ongenetwork TINYINT(1), -- On GE Network?
gecoreload TINYINT(1), -- Has GE Coreload software?
-- Security Classification
assetcriticality VARCHAR(10), -- 'Low', 'Medium', 'High'
cuidataclassification VARCHAR(20), -- 'CUI', 'NON-CUI'
dodassettype VARCHAR(100), -- 'Specialized Asset', etc.
dodassetsubtype VARCHAR(50), -- 'IT', 'OT'
otenvironment VARCHAR(100), -- 'Manufacturing/Production', 'Shipping/Receiving'
-- Management & Access
managedbyvendorid INT(11), -- FK to vendors (third-party management)
changerestricted TINYINT(1), -- Change restricted?
jumpbox TINYINT(1), -- Is a jump box?
mft VARCHAR(100), -- Managed File Transfer designation
-- Notes
notes TEXT,
isactive TINYINT(1) DEFAULT 1,
KEY idx_machineid (machineid),
KEY idx_managedbyvendorid (managedbyvendorid),
KEY idx_assetcriticality (assetcriticality),
KEY idx_cuidataclassification (cuidataclassification),
CONSTRAINT fk_compliance_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid),
CONSTRAINT fk_compliance_managedbyvendorid FOREIGN KEY (managedbyvendorid) REFERENCES vendors(vendorid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Design Decision:** Use existing `vendors` table for third-party management instead of storing vendor names as strings.
**Columns: 15**
---
### 4. `compliancescans` - Scan History Log
Tracks antivirus/malware scan results over time.
```sql
CREATE TABLE compliancescans (
scanid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL,
scandate DATETIME,
scanstatus VARCHAR(100), -- 'No Threats Found', 'Threats Found', 'CORELOADED'
scandetails TEXT, -- Full scan results
KEY idx_machineid (machineid),
KEY idx_scandate (scandate),
CONSTRAINT fk_compliancescans_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Columns: 5**
---
### 5. `warranties` - Warranty Management
**SIMPLIFIED DESIGN**: Just name and expiration date. No complex vendor/type tables.
```sql
CREATE TABLE warranties (
warrantyid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL,
warrantyname VARCHAR(100) NOT NULL, -- 'Dell ProSupport Plus', 'Standard 3-Year'
enddate DATE NOT NULL, -- When warranty expires
isactive TINYINT(1) DEFAULT 1,
KEY idx_machineid (machineid),
KEY idx_enddate (enddate),
KEY idx_isactive (isactive),
CONSTRAINT fk_warranties_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Design Decision:** Keep it minimal. Multiple warranties per machine supported.
**Columns: 5**
---
### 6. `relationshiptypes` - Machine Relationship Types Lookup
Defines types of relationships between machines.
```sql
CREATE TABLE relationshiptypes (
relationshiptypeid INT(11) PRIMARY KEY AUTO_INCREMENT,
relationshiptype VARCHAR(50) NOT NULL UNIQUE,
description VARCHAR(255),
isbidirectional TINYINT(1) DEFAULT 0, -- Is relationship symmetric?
isactive TINYINT(1) DEFAULT 1,
KEY idx_isactive (isactive)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Initial Data:**
```sql
INSERT INTO relationshiptypes (relationshiptype, description, isbidirectional) VALUES
('Dualpath', 'Machines share same controller (bidirectional)', 1),
('Controlled By', 'Machine is controlled by PC/controller (directional)', 0),
('Controls', 'PC/controller controls machine (directional)', 0),
('Master/Slave', 'Master-slave relationship', 0),
('Linked', 'Generic link between machines', 1);
```
**Columns: 5**
**Use Cases:**
- Dualpath lathes (2001 & 2002 share same controller)
- PC controlling multiple CNC machines
- Future: Machine clusters, backup systems, etc.
---
### 7. `machinerelationships` - Machine-to-Machine Relationships
Tracks relationships between machines (PC-to-CNC, dualpath, etc.)
```sql
CREATE TABLE machinerelationships (
relationshipid INT(11) PRIMARY KEY AUTO_INCREMENT,
relationshiptypeid INT(11) NOT NULL, -- FK to relationshiptypes
machineid1 INT(11) NOT NULL, -- FK to machines (primary machine)
machineid2 INT(11) NOT NULL, -- FK to machines (related machine)
notes TEXT,
isactive TINYINT(1) DEFAULT 1,
KEY idx_relationshiptypeid (relationshiptypeid),
KEY idx_machineid1 (machineid1),
KEY idx_machineid2 (machineid2),
CONSTRAINT fk_machinerel_machineid1 FOREIGN KEY (machineid1) REFERENCES machines(machineid),
CONSTRAINT fk_machinerel_machineid2 FOREIGN KEY (machineid2) REFERENCES machines(machineid),
CONSTRAINT fk_machinerel_typeid FOREIGN KEY (relationshiptypeid) REFERENCES relationshiptypes(relationshiptypeid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Columns: 6**
**Example Data:**
```sql
-- Machines 2001 and 2002 are dualpath (linked together)
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2) VALUES
(1, 2001, 2002); -- Dualpath relationship
-- PC (machineid 500) controls machine 2001
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2) VALUES
(2, 2001, 500); -- 2001 controlled by PC 500
-- PC (machineid 500) controls machine 2002
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2) VALUES
(2, 2002, 500); -- 2002 controlled by PC 500
```
**Key Feature:** Both machines and PCs are in the machines table, so relationships can link any machine to any other machine.
**Query Examples:**
```sql
-- Find dualpath partner of machine 2001
SELECT m2.machinenumber
FROM machinerelationships mr
JOIN machines m2 ON (
CASE WHEN mr.machineid1 = 2001 THEN mr.machineid2 ELSE mr.machineid1 END = m2.machineid
)
WHERE (mr.machineid1 = 2001 OR mr.machineid2 = 2001)
AND mr.relationshiptypeid = 1; -- Dualpath
-- Find which PC controls machine 2001
SELECT pc.hostname, pc.machinenumber
FROM machinerelationships mr
JOIN machines pc ON mr.machineid2 = pc.machineid
WHERE mr.machineid1 = 2001
AND mr.relationshiptypeid = 2; -- Controlled By
-- Find all machines controlled by PC 500
SELECT m.machinenumber
FROM machinerelationships mr
JOIN machines m ON mr.machineid1 = m.machineid
WHERE mr.machineid2 = 500
AND mr.relationshiptypeid = 2; -- Controlled By
```
---
## MODIFIED TABLES (2 Tables)
### 8. `machines` - Extended for PC Data
**NEW COLUMNS ADDED (11):**
```sql
ALTER TABLE machines
ADD COLUMN hostname VARCHAR(100) AFTER machinenumber,
ADD COLUMN serialnumber VARCHAR(100) AFTER modelnumberid,
ADD COLUMN loggedinuser VARCHAR(100) AFTER hostname,
ADD COLUMN controllertypeid INT(11) AFTER modelnumberid,
ADD COLUMN controllerosid INT(11) AFTER controllertypeid,
ADD COLUMN osid INT(11) AFTER controllerosid,
ADD COLUMN machinestatusid INT(11) AFTER osid,
ADD COLUMN lastupdated DATETIME,
ADD COLUMN dateadded DATETIME,
ADD KEY idx_hostname (hostname),
ADD KEY idx_serialnumber (serialnumber),
ADD KEY idx_controllertypeid (controllertypeid),
ADD KEY idx_controllerosid (controllerosid),
ADD KEY idx_osid (osid),
ADD KEY idx_machinestatusid (machinestatusid),
ADD KEY idx_lastupdated (lastupdated),
ADD CONSTRAINT fk_machines_controllertypeid FOREIGN KEY (controllertypeid) REFERENCES controllertypes(controllertypeid),
ADD CONSTRAINT fk_machines_controllerosid FOREIGN KEY (controllerosid) REFERENCES operatingsystems(osid),
ADD CONSTRAINT fk_machines_osid FOREIGN KEY (osid) REFERENCES operatingsystems(osid),
ADD CONSTRAINT fk_machines_machinestatusid FOREIGN KEY (machinestatusid) REFERENCES machinestatus(machinestatusid);
```
**FINAL MACHINES TABLE (21 columns):**
| # | Column | Type | Purpose |
|---|--------|------|---------|
| 1 | machineid | INT(11) PK | Primary key |
| 2 | machinetypeid | INT(11) FK | Machine type (now includes PC types!) |
| 3 | machinenumber | VARCHAR(50) | Machine number |
| 4 | alias | VARCHAR(255) | Friendly name |
| 5 | hostname | VARCHAR(100) | **NEW** - PC hostname |
| 6 | serialnumber | VARCHAR(100) | **NEW** - Serial number |
| 7 | loggedinuser | VARCHAR(100) | **NEW** - Logged in user |
| 8 | modelnumberid | INT(11) FK | Model |
| 9 | controllertypeid | INT(11) FK | **NEW** - Controller type (for CNC machines) |
| 10 | controllerosid | INT(11) FK | **NEW** - Controller OS version (uses operatingsystems table) |
| 11 | osid | INT(11) FK | **NEW** - Operating system (for PCs) |
| 12 | machinestatusid | INT(11) FK | **NEW** - Machine status |
| 13 | businessunitid | INT(11) FK | Business unit |
| 14 | printerid | INT(11) FK | Associated printer |
| 15 | mapleft | SMALLINT(6) | Map X position |
| 16 | maptop | SMALLINT(6) | Map Y position |
| 17 | isactive | INT(11) | Is active? |
| 18 | islocationonly | BIT(1) | Location marker only? |
| 19 | machinenotes | TEXT | Notes |
| 20 | lastupdated | DATETIME | **NEW** - Last updated |
| 21 | dateadded | DATETIME | **NEW** - Date added |
**REMOVED FIELDS (after migration):**
-`pctypeid` - No longer needed (use machinetypeid)
-`isvnc` - Tracked in communications table
-`requires_manual_machine_config` - Not needed
-`ipaddress1` - Migrated to communications
-`ipaddress2` - Migrated to communications
---
### 9. `businessunits` - Add Liaison Fields
**NEW COLUMNS ADDED (2):**
```sql
ALTER TABLE businessunits
ADD COLUMN liaisonname VARCHAR(100) AFTER distributiongroupid,
ADD COLUMN liaisonsso VARCHAR(50) AFTER liaisonname,
ADD KEY idx_liaisonsso (liaisonsso);
```
**FINAL BUSINESSUNITS TABLE:**
| # | Column | Purpose |
|---|--------|---------|
| 1 | businessunitid | Primary key |
| 2 | businessunit | Business unit name |
| 3 | distributiongroupid | Distribution group |
| 4 | liaisonname | **NEW** - Liaison name (e.g., "Patrick Lipinski") |
| 5 | liaisonsso | **NEW** - Liaison SSO/Employee ID |
| 6 | isactive | Is active? |
**Design Decision:** Added directly to businessunits instead of creating separate liaisons table.
---
## RENAMED TABLES (1 Table)
### 10. `pcstatus` → `machinestatus`
Make naming consistent (applies to all machines, not just PCs).
```sql
RENAME TABLE pcstatus TO machinestatus;
ALTER TABLE machinestatus
CHANGE pcstatusid machinestatusid INT(11) AUTO_INCREMENT,
CHANGE pcstatus machinestatus CHAR(50);
```
**Final columns:**
- machinestatusid (PK) - renamed from pcstatusid
- machinestatus - renamed from pcstatus
- isactive
---
## UPDATED LOOKUP TABLES
### 11. `machinetypes` - Add PC Types
Consolidate PC types into machine types (eliminates need for separate pctype system).
```sql
INSERT INTO machinetypes (machinetype, machinedescription, isactive) VALUES
('PC - Standard', 'Standard office/engineering workstation', 1),
('PC - Shopfloor', 'Shopfloor machine control PC', 1),
('PC - Engineer', 'Engineering workstation', 1),
('PC - Server', 'Server', 1),
('PC - Laptop', 'Laptop computer', 1);
```
---
## DEPRECATED TABLES (Drop after 30-day testing)
These tables will be migrated to new structure, then dropped:
1. **`pc`** (277 records) → Data migrated to `machines`
2. **`pc_comm_config`** → Data migrated to `communications`
3. **`pc_network_interfaces`** → Data migrated to `communications`
4. **`pctype`** → Data migrated to `machinetypes` as entries
---
## MIGRATION DATA FLOW
### From `pc` table (277 records):
```
pc.hostname → machines.hostname
pc.serialnumber → machines.serialnumber
pc.loggedinuser → machines.loggedinuser
pc.machinenumber → machines.machinenumber
pc.osid → machines.osid
pc.pcstatusid → machines.machinestatusid
pc.pctypeid → machines.machinetypeid (mapped to new PC types)
pc.modelnumberid → machines.modelnumberid
pc.isactive → machines.isactive
pc.lastupdated → machines.lastupdated
pc.dateadded → machines.dateadded
Warranty fields → warranties table:
pc.warrantyenddate → warranties.enddate
pc.warrantyservicelevel → warranties.warrantyname (or derived)
```
### From `machines.ipaddress1` and `machines.ipaddress2`:
```sql
-- For each machine with ipaddress1
INSERT INTO communications (machineid, comstypeid, address, description, isactive)
VALUES (machineid, 1, ipaddress1, 'Migrated from machines.ipaddress1', 1);
-- For each machine with ipaddress2
INSERT INTO communications (machineid, comstypeid, address, description, isactive)
VALUES (machineid, 1, ipaddress2, 'Migrated from machines.ipaddress2', 1);
-- Then drop the columns after 30-day verification
ALTER TABLE machines DROP COLUMN ipaddress1, DROP COLUMN ipaddress2;
```
### From `pc_network_interfaces`:
```sql
INSERT INTO communications (machineid, comstypeid, address, macaddress, description, settings, isactive)
SELECT
pcid AS machineid,
3 AS comstypeid, -- Network Interface type
interfacename AS address,
macaddress,
'Migrated from pc_network_interfaces' AS description,
JSON_OBJECT(
'subnet', subnetmask,
'gateway', defaultgateway,
'dhcp', isdhcp,
'machineNetwork', ismachinenetwork
) AS settings,
isactive
FROM pc_network_interfaces;
```
### From `pc_comm_config`:
```sql
-- Serial communications
INSERT INTO communications (machineid, comstypeid, address, settings, isactive)
SELECT
pcid AS machineid,
2 AS comstypeid, -- Serial type
portid AS address,
JSON_OBJECT(
'baud', baud,
'databits', databits,
'stopbits', stopbits,
'parity', parity
) AS settings,
1 AS isactive
FROM pc_comm_config
WHERE configtype = 'Serial';
-- IP communications
INSERT INTO communications (machineid, comstypeid, address, port, settings, isactive)
SELECT
pcid AS machineid,
1 AS comstypeid, -- IP type
ipaddress AS address,
socketnumber AS port,
additionalsettings AS settings,
1 AS isactive
FROM pc_comm_config
WHERE configtype = 'IP';
```
---
## SUMMARY STATISTICS
**New Tables:** 7
- comstypes
- communications
- compliance
- compliancescans
- warranties
- relationshiptypes
- machinerelationships
**Modified Tables:** 2
- machines (+11 columns)
- businessunits (+2 columns)
**Renamed Tables:** 1
- pcstatus → machinestatus
**Deprecated Tables:** 4
- pc
- pc_comm_config
- pc_network_interfaces
- pctype
**Total New Columns:** 61 columns across all tables
**Estimated Records After Migration:**
- machines: 543 (266 existing + 277 from PCs)
- communications: ~650+ (266 from machines.ipaddress, 277+ from pc tables)
- warranties: ~277+ (from PC warranty data)
- compliance: TBD (from inventory.xlsx)
- compliancescans: TBD (ongoing logging)
- machinerelationships: ~50+ (dualpath pairs, PC-to-machine controllers)
---
## KEY DESIGN DECISIONS
### ✅ Generic `address` Field
**Decision:** Use single `address` field in communications instead of separate ipaddress/portname/etc fields.
**Benefit:** Flexible, extensible, no unused columns per row.
### ✅ Simplified Warranties
**Decision:** Just warrantyname and enddate. No complex vendor/type/cost tracking.
**Benefit:** Easy to use, easy to maintain, covers 90% of use cases.
### ✅ Liaison in BusinessUnits
**Decision:** Add liaison fields directly to businessunits instead of separate table.
**Benefit:** Simpler structure, one less JOIN, appropriate granularity.
### ✅ Unified Machine Types
**Decision:** Add PC types as entries in machinetypes instead of separate pctype system.
**Benefit:** Single source of truth, eliminates pctypeid column.
### ✅ Vendor FK for Third-Party Management
**Decision:** Use vendorid FK in compliance.managedbyvendorid instead of text field.
**Benefit:** Normalized, prevents typos, enables vendor-based queries.
### ✅ JSON Settings Field
**Decision:** Use JSON for type-specific config in communications.
**Benefit:** Flexible, no table bloat, easy to extend.
### ✅ Minimal Audit Fields
**Decision:** Removed dateadded/lastupdated from warranties and communications.
**Benefit:** Simpler, only track audit fields where truly needed.
### ✅ Machine Relationships Table
**Decision:** Generic relationship table instead of specific PC-to-machine or dualpath-only tables.
**Benefit:** Flexible, extensible, supports bidirectional and directional relationships, future-proof for clusters/backup systems.
---
## NEXT STEPS
### Phase 1: Infrastructure Setup (Scripts 01-08)
**Status:** Ready to create SQL scripts
1. ✅ Script 01: Create communications infrastructure (comstypes, communications)
2. ✅ Script 02: Extend machines table (+9 columns)
3. ✅ Script 03: Create PC machine types in machinetypes
4. ✅ Script 04: Create warranty infrastructure (warranties)
5. ✅ Script 05: Create compliance infrastructure (compliance, compliancescans)
6. ✅ Script 06: Extend businessunits table (+2 liaison fields)
7. ✅ Script 07: Rename pcstatus to machinestatus
8. ✅ Script 08: Create machine relationships infrastructure (relationshiptypes, machinerelationships)
**Estimated Time:** 25 minutes
**Reversible:** Yes (rollback scripts provided)
### Phase 2: Data Migration (Scripts 09-12)
**Status:** Design complete, scripts pending
1. ⏳ Script 09: Migrate PC data to machines
2. ⏳ Script 10: Migrate communication data (ipaddress1/2, pc_network_interfaces, pc_comm_config)
3. ⏳ Script 11: Migrate warranty data
4. ⏳ Script 12: Populate machine relationships (dualpath, controller associations)
**Estimated Time:** 30-45 minutes
**Reversible:** Yes (with backups)
### Phase 3: Application Updates
**Status:** Pending
1. ⏳ Update 19 database views
2. ⏳ Create compatibility views
3. ⏳ Update ASP files
4. ⏳ Test all functionality
**Estimated Time:** 2-3 weeks
### Phase 4: Cleanup (After 30 days)
**Status:** Pending
1. ⏳ Drop pc table
2. ⏳ Drop pc_comm_config, pc_network_interfaces
3. ⏳ Drop pctype table
4. ⏳ Drop compatibility views
5. ⏳ Drop machines.ipaddress1, machines.ipaddress2
---
## RISK MITIGATION
**Critical Success Factors:**
- ✅ Full database backup before any changes
- ✅ Test all scripts on dev environment first
- ✅ Rollback scripts for Phase 1
- ✅ 30-day verification period before dropping old tables
- ✅ Compatibility views to support gradual application migration
**Rollback Strategy:**
- Phase 1: Run rollback scripts (drop new tables, revert ALTER TABLEs)
- Phase 2: Restore from backup
- Phase 3: Revert code deployment
- Phase 4: IRREVERSIBLE - ensure 30-day testing complete
---
**Document Status:** FINALIZED
**Date Finalized:** 2025-11-06
**Ready for:** Phase 1 SQL Script Creation
**Dependencies:** None - self-contained design
---
*This document represents the final approved design. Any changes after this point should be documented as amendments with version numbers.*

1153
docs/DEEP_DIVE_REPORT.md Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,398 @@
# Infrastructure Architecture - Final Design
**Date:** 2025-10-23
**Decision:** Use dedicated infrastructure tables with hierarchical relationships
---
## Existing Schema (Already in Database!)
### IDFs (Intermediate Distribution Frames)
```sql
idfs:
- idfid INT(11) PK
- idfname VARCHAR(100)
- description VARCHAR(255)
- maptop, mapleft INT(11) -- map coordinates
- isactive BIT(1)
```
**No parent** - IDFs are top-level containers
### Cameras
```sql
cameras:
- cameraid INT(11) PK
- modelid INT(11) models.modelnumberid vendors
- idfid INT(11) idfs.idfid Already has parent!
- serialnumber VARCHAR(100)
- macaddress VARCHAR(17) Camera-specific
- ipaddress VARCHAR(45)
- description VARCHAR(255)
- maptop, mapleft INT(11)
- isactive BIT(1)
```
### Switches
```sql
switches:
- switchid INT(11) PK
- modelid INT(11) models.modelnumberid vendors
- serialnumber VARCHAR(100)
- ipaddress VARCHAR(45)
- description VARCHAR(255)
- maptop, mapleft INT(11)
- isactive BIT(1)
```
**Missing:** `idfid` (switches should belong to IDFs)
### Servers
```sql
servers:
- serverid INT(11) PK
- modelid INT(11) models.modelnumberid vendors
- serialnumber VARCHAR(100)
- ipaddress VARCHAR(45)
- description VARCHAR(255)
- maptop, mapleft INT(11)
- isactive BIT(1)
```
**Optional:** `idfid` (servers might be in IDFs)
---
## Hierarchical Relationships
```
IDFs (top level)
├─ Switches (belong to IDF)
│ └─ Cameras (might connect to switch)
└─ Cameras (belong to IDF)
└─ Servers (might be in IDF)
```
---
## Migration Needed
### Step 1: Add idfid to switches (Required)
```sql
ALTER TABLE switches
ADD COLUMN idfid INT(11) AFTER modelid,
ADD INDEX idx_switches_idfid (idfid),
ADD CONSTRAINT fk_switches_idf
FOREIGN KEY (idfid) REFERENCES idfs(idfid) ON DELETE SET NULL;
```
### Step 2: Add idfid to servers (Optional)
```sql
ALTER TABLE servers
ADD COLUMN idfid INT(11) AFTER modelid,
ADD INDEX idx_servers_idfid (idfid),
ADD CONSTRAINT fk_servers_idf
FOREIGN KEY (idfid) REFERENCES idfs(idfid) ON DELETE SET NULL;
```
### Step 3: Ensure modelid exists (migration script handles this)
Run `add_infrastructure_vendor_model_support.sql`
---
## Page Architecture
### Unified List Page + Type-Specific Detail Pages
**Why:** Different device types have different fields, so unified edit forms would be messy.
### Files (7 total):
```
network_devices.asp → Unified list with tabs/filter
network_device_detail_idf.asp?id=5 → IDF detail/edit
network_device_detail_server.asp?id=3 → Server detail/edit
network_device_detail_switch.asp?id=2 → Switch detail/edit
network_device_detail_camera.asp?id=1 → Camera detail/edit
add_network_device.asp?type=idf → Add form (type selector)
save_network_device.asp → Universal save (routes by type)
```
---
## Page 1: network_devices.asp (Unified List)
### Features
- **Tabs:** All | IDFs | Servers | Switches | Cameras
- **Single table** showing all infrastructure
- **Click device** → routes to appropriate detail page based on type
### Routing Logic
```vbscript
Select Case rs("device_type")
Case "IDF"
detailUrl = "network_device_detail_idf.asp?id=" & rs("device_id")
Case "Server"
detailUrl = "network_device_detail_server.asp?id=" & rs("device_id")
Case "Switch"
detailUrl = "network_device_detail_switch.asp?id=" & rs("device_id")
Case "Camera"
detailUrl = "network_device_detail_camera.asp?id=" & rs("device_id")
End Select
```
---
## Page 2: network_device_detail_idf.asp
### Unique Fields
- **idfname** (no model/vendor - IDFs are just locations)
- **description**
- **Map coordinates**
### Form Fields
```html
<input type="text" name="idfname" required>
<textarea name="description"></textarea>
<input type="number" name="maptop" placeholder="Y coordinate">
<input type="number" name="mapleft" placeholder="X coordinate">
```
### No Parent Selection
IDFs are top-level, no parent dropdown needed.
---
## Page 3: network_device_detail_server.asp
### Fields
- **Model/Vendor dropdown** (modelid)
- **Serial number**
- **IP address**
- **Description**
- **IDF dropdown** (optional - which IDF is this server in?)
- **Map coordinates**
### IDF Dropdown
```vbscript
<div class="form-group">
<label>IDF Location (Optional)</label>
<select name="idfid" class="form-control">
<option value="">-- Not in an IDF --</option>
<%
strSQL = "SELECT idfid, idfname FROM idfs WHERE isactive = 1 ORDER BY idfname"
Set rsIDFs = objConn.Execute(strSQL)
Do While Not rsIDFs.EOF
%>
<option value="<%=rsIDFs("idfid")%>"
<%If Not IsNull(rs("idfid")) And rs("idfid") = rsIDFs("idfid") Then Response.Write("selected")%>>
<%=Server.HTMLEncode(rsIDFs("idfname"))%>
</option>
<%
rsIDFs.MoveNext
Loop
%>
</select>
</div>
```
---
## Page 4: network_device_detail_switch.asp
### Fields
- **Model/Vendor dropdown** (modelid)
- **Serial number**
- **IP address**
- **Description**
- **IDF dropdown** (required - which IDF is this switch in?)
- **Port count** (optional - could add this field)
- **Map coordinates**
### IDF Dropdown (Required for switches)
```vbscript
<div class="form-group">
<label>IDF Location <span class="text-danger">*</span></label>
<select name="idfid" required class="form-control">
<option value="">-- Select IDF --</option>
<!-- IDF options... -->
</select>
<small class="form-text text-muted">
Switches must be assigned to an IDF
</small>
</div>
```
---
## Page 5: network_device_detail_camera.asp
### Fields
- **Model/Vendor dropdown** (modelid)
- **Serial number**
- **MAC address** (cameras have this!)
- **IP address**
- **Description**
- **IDF dropdown** (required - which IDF does this camera connect to?)
- **Switch dropdown** (optional - which switch port?)
- **Map coordinates**
### IDF Dropdown (Required)
```vbscript
<div class="form-group">
<label>IDF Location <span class="text-danger">*</span></label>
<select name="idfid" required class="form-control">
<option value="">-- Select IDF --</option>
<!-- IDF options... -->
</select>
</div>
```
### MAC Address Field (Unique to cameras)
```vbscript
<div class="form-group">
<label>MAC Address</label>
<input type="text" name="macaddress" class="form-control"
pattern="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
placeholder="00:11:22:33:44:55">
</div>
```
---
## Page 6: add_network_device.asp
### Step 1: Device Type Selector
Show cards for IDF, Server, Switch, Camera
### Step 2: Type-Specific Form
Route to appropriate form based on selected type:
- `add_network_device.asp?type=idf` → IDF form (no model)
- `add_network_device.asp?type=server` → Server form (model + optional IDF)
- `add_network_device.asp?type=switch` → Switch form (model + required IDF)
- `add_network_device.asp?type=camera` → Camera form (model + required IDF + MAC)
---
## Page 7: save_network_device.asp
### Universal Save Endpoint
```vbscript
<%
Dim deviceType
deviceType = Request.Form("type")
' Route to appropriate table
Select Case deviceType
Case "idf"
tableName = "idfs"
' Save: idfname, description, maptop, mapleft
' No modelid
Case "server"
tableName = "servers"
' Save: modelid, idfid (optional), serialnumber, ipaddress, description, maptop, mapleft
Case "switch"
tableName = "switches"
' Save: modelid, idfid (required), serialnumber, ipaddress, description, maptop, mapleft
Case "camera"
tableName = "cameras"
' Save: modelid, idfid (required), serialnumber, macaddress, ipaddress, description, maptop, mapleft
End Select
%>
```
---
## Navigation Menu
```html
<li class="nav-header">INFRASTRUCTURE</li>
<li>
<a href="network_devices.asp">
<i class="zmdi zmdi-devices"></i> Network Devices
</a>
</li>
<li>
<a href="network_map.asp">
<i class="zmdi zmdi-map"></i> Network Map
</a>
</li>
```
---
## network_map.asp Integration
### Current State
Currently queries `machines` table filtering for infrastructure machine types.
### New Approach
Query both machines AND infrastructure tables:
```vbscript
<%
' Get infrastructure devices
strSQL = "SELECT 'IDF' as type, idfid as id, idfname as name, NULL as model, NULL as vendor, " & _
"maptop, mapleft, 'IDF' as device_type " & _
"FROM idfs WHERE isactive = 1 AND maptop IS NOT NULL " & _
"UNION ALL " & _
"SELECT 'Server' as type, serverid as id, description as name, m.modelnumber as model, v.vendor, " & _
"s.maptop, s.mapleft, 'Server' as device_type " & _
"FROM servers s " & _
"LEFT JOIN models m ON s.modelid = m.modelnumberid " & _
"LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE s.isactive = 1 AND s.maptop IS NOT NULL " & _
"UNION ALL " & _
"SELECT 'Switch' as type, switchid as id, description as name, m.modelnumber as model, v.vendor, " & _
"sw.maptop, sw.mapleft, 'Switch' as device_type " & _
"FROM switches sw " & _
"LEFT JOIN models m ON sw.modelid = m.modelnumberid " & _
"LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE sw.isactive = 1 AND sw.maptop IS NOT NULL " & _
"UNION ALL " & _
"SELECT 'Camera' as type, cameraid as id, description as name, m.modelnumber as model, v.vendor, " & _
"c.maptop, c.mapleft, 'Camera' as device_type " & _
"FROM cameras c " & _
"LEFT JOIN models m ON c.modelid = m.modelnumberid " & _
"LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE c.isactive = 1 AND c.maptop IS NOT NULL"
Set rs = objConn.Execute(strSQL)
' Output JSON for map markers
Response.Write("const devices = [")
Do While Not rs.EOF
Response.Write("{")
Response.Write("type: '" & rs("device_type") & "', ")
Response.Write("id: " & rs("id") & ", ")
Response.Write("name: '" & Replace(rs("name") & "", "'", "\'") & "', ")
Response.Write("model: '" & Replace(rs("model") & "", "'", "\'") & "', ")
Response.Write("vendor: '" & Replace(rs("vendor") & "", "'", "\'") & "', ")
Response.Write("x: " & rs("mapleft") & ", ")
Response.Write("y: " & rs("maptop"))
Response.Write("},")
rs.MoveNext
Loop
Response.Write("];")
%>
```
---
## Summary: Why This Approach?
**Hierarchical relationships** - Cameras/switches belong to IDFs
**Type-specific fields** - MAC address for cameras, idfname for IDFs
**Flexible** - Can add more fields per type later
**Clean data model** - Proper normalization
**Unified list view** - See all infrastructure in one place
**Type-specific edit** - Appropriate fields per device type
**Map integration** - All devices can be mapped
**Total Files:** 7 ASP files (1 list + 4 detail + 1 add + 1 save)
---
**Next Step:** Run enhanced migration script to add `idfid` to switches/servers, then create the 7 pages.

View File

@@ -0,0 +1,371 @@
# Infrastructure - Simplified Final Design
**Date:** 2025-10-23
**Scope:** Only cameras track IDF relationships
---
## Simplified Schema
### IDFs (Intermediate Distribution Frames)
```sql
idfs:
- idfid INT(11) PK
- idfname VARCHAR(100)
- description VARCHAR(255)
- maptop, mapleft INT(11)
- isactive BIT(1)
```
**Standalone** - Just a reference table for camera locations
### Cameras (Only device type with IDF relationship)
```sql
cameras:
- cameraid INT(11) PK
- modelid INT(11) models vendors
- idfid INT(11) idfs.idfid (already exists!)
- serialnumber VARCHAR(100)
- macaddress VARCHAR(17) (already exists!)
- ipaddress VARCHAR(45)
- description VARCHAR(255)
- maptop, mapleft INT(11)
- isactive BIT(1)
```
### Switches (No IDF)
```sql
switches:
- switchid INT(11) PK
- modelid INT(11) models vendors
- serialnumber VARCHAR(100)
- ipaddress VARCHAR(45)
- description VARCHAR(255)
- maptop, mapleft INT(11)
- isactive BIT(1)
```
### Servers (No IDF)
```sql
servers:
- serverid INT(11) PK
- modelid INT(11) models vendors
- serialnumber VARCHAR(100)
- ipaddress VARCHAR(45)
- description VARCHAR(255)
- maptop, mapleft INT(11)
- isactive BIT(1)
```
---
## Migration Needed
**Just run:** `add_infrastructure_vendor_model_support.sql`
This adds `modelid` to servers/switches/cameras (if not already present).
**No additional migrations needed!** Cameras already have `idfid` and `macaddress`.
---
## Edit Pages - Which Are Unique?
| Device | Unique Fields | Needs Custom Page? |
|--------|---------------|-------------------|
| **IDF** | idfname (no model/vendor) | ✅ YES - different structure |
| **Camera** | idfid dropdown, macaddress | ✅ YES - has IDF + MAC |
| **Server** | Standard fields only | ❌ NO - same as switch |
| **Switch** | Standard fields only | ❌ NO - same as server |
### Optimization: Combine Server/Switch Edit
Since servers and switches have **identical fields**, we can use:
- **1 generic edit page** for servers + switches
- **1 custom edit page** for cameras (has IDF + MAC)
- **1 custom edit page** for IDFs (no model/vendor)
---
## Page Architecture (5 Files Total!)
```
network_devices.asp → Unified list with tabs
network_device_detail_idf.asp?id=5 → IDF detail/edit (no model)
network_device_detail_generic.asp?type=server&id=3 → Server/Switch edit
network_device_detail_camera.asp?id=1 → Camera edit (IDF + MAC)
add_network_device.asp?type=server → Add form with type selector
save_network_device.asp → Universal save
```
**Wait, that's 6 files. Can we simplify more?**
Actually, let's use **4 files** by combining add into detail:
```
network_devices.asp → List with tabs
device_idf.asp?id=5 → IDF add/edit
device_generic.asp?type=server&id=3 → Server/Switch add/edit
device_camera.asp?id=1 → Camera add/edit (IDF + MAC)
```
Each detail page handles both **add (id=0)** and **edit (id>0)**.
---
## File 1: network_devices.asp (List)
### Features
- Tabs: All | IDFs | Servers | Switches | Cameras
- Unified table showing all devices
- Click device → route to appropriate detail page
### Routing
```vbscript
Select Case rs("device_type")
Case "IDF"
detailUrl = "device_idf.asp?id=" & rs("device_id")
Case "Server"
detailUrl = "device_generic.asp?type=server&id=" & rs("device_id")
Case "Switch"
detailUrl = "device_generic.asp?type=switch&id=" & rs("device_id")
Case "Camera"
detailUrl = "device_camera.asp?id=" & rs("device_id")
End Select
```
---
## File 2: device_idf.asp (IDF Add/Edit)
### Fields
- **idfname** (text input, required)
- **description** (textarea)
- **maptop, mapleft** (optional coordinates)
### No dropdowns
IDFs are just locations with names. No model, no vendor, no parent.
### Save endpoint
Posts to `save_network_device.asp` with `type=idf`
---
## File 3: device_generic.asp (Server/Switch Add/Edit)
### Type-aware
Uses `?type=server` or `?type=switch` parameter
### Fields (Same for both!)
- **Model dropdown** (modelid → shows vendor + model)
- **Serial number** (text)
- **IP address** (text, validated)
- **Description** (textarea)
- **maptop, mapleft** (optional coordinates)
### Dynamic labels
```vbscript
Dim deviceType, displayName
deviceType = Request.QueryString("type")
If deviceType = "server" Then
displayName = "Server"
ElseIf deviceType = "switch" Then
displayName = "Switch"
Else
Response.Redirect("network_devices.asp")
End If
%>
<h2><%If deviceId = 0 Then Response.Write("Add") Else Response.Write("Edit")%> <%=displayName%></h2>
```
### Save endpoint
Posts to `save_network_device.asp` with `type=server` or `type=switch`
---
## File 4: device_camera.asp (Camera Add/Edit)
### Fields (Camera-specific!)
- **Model dropdown** (modelid → shows vendor + model)
- **IDF dropdown** (idfid → required!)
- **Serial number** (text)
- **MAC address** (text, pattern validation)
- **IP address** (text, validated)
- **Description** (textarea)
- **maptop, mapleft** (optional coordinates)
### IDF Dropdown
```vbscript
<div class="form-group">
<label>IDF Location <span class="text-danger">*</span></label>
<select name="idfid" required class="form-control">
<option value="">-- Select IDF --</option>
<%
strSQL = "SELECT idfid, idfname FROM idfs WHERE isactive = 1 ORDER BY idfname"
Set rsIDFs = objConn.Execute(strSQL)
Do While Not rsIDFs.EOF
%>
<option value="<%=rsIDFs("idfid")%>"
<%If Not IsNull(rs("idfid")) And rs("idfid") = rsIDFs("idfid") Then Response.Write("selected")%>>
<%=Server.HTMLEncode(rsIDFs("idfname"))%>
</option>
<%
rsIDFs.MoveNext
Loop
%>
</select>
<small class="form-text text-muted">
Which IDF does this camera connect to?
</small>
</div>
```
### MAC Address Field
```vbscript
<div class="form-group">
<label>MAC Address</label>
<input type="text" name="macaddress" class="form-control"
pattern="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
placeholder="00:11:22:33:44:55">
</div>
```
### Save endpoint
Posts to `save_network_device.asp` with `type=camera`
---
## File 5: save_network_device.asp (Universal Save)
### Routes by type parameter
```vbscript
<%
Dim deviceType, deviceId
deviceType = Request.Form("type")
deviceId = GetSafeInteger("FORM", "id", 0, 0, 999999)
Select Case deviceType
Case "idf"
tableName = "idfs"
idField = "idfid"
' Fields: idfname, description, maptop, mapleft
' No modelid!
Case "server"
tableName = "servers"
idField = "serverid"
' Fields: modelid, serialnumber, ipaddress, description, maptop, mapleft
' No idfid!
Case "switch"
tableName = "switches"
idField = "switchid"
' Fields: modelid, serialnumber, ipaddress, description, maptop, mapleft
' No idfid!
Case "camera"
tableName = "cameras"
idField = "cameraid"
' Fields: modelid, idfid, serialnumber, macaddress, ipaddress, description, maptop, mapleft
' Has idfid and macaddress!
End Select
' Build INSERT or UPDATE query based on deviceId
If deviceId = 0 Then
' INSERT logic...
Else
' UPDATE logic...
End If
%>
```
---
## Add Flow (From network_devices.asp)
### "Add Device" Button
Shows modal or redirects to selection page:
```
[Add IDF] → device_idf.asp?id=0
[Add Server] → device_generic.asp?type=server&id=0
[Add Switch] → device_generic.asp?type=switch&id=0
[Add Camera] → device_camera.asp?id=0
```
Or use the existing approach with type selector in add_network_device.asp.
---
## Summary
### Field Comparison Table
| Field | IDF | Server | Switch | Camera |
|-------|-----|--------|--------|--------|
| idfname | ✅ | ❌ | ❌ | ❌ |
| modelid | ❌ | ✅ | ✅ | ✅ |
| idfid (parent) | ❌ | ❌ | ❌ | ✅ |
| macaddress | ❌ | ❌ | ❌ | ✅ |
| serialnumber | ❌ | ✅ | ✅ | ✅ |
| ipaddress | ❌ | ✅ | ✅ | ✅ |
| description | ✅ | ✅ | ✅ | ✅ |
| maptop, mapleft | ✅ | ✅ | ✅ | ✅ |
### Pages Needed
| Page | Handles | Reason |
|------|---------|--------|
| network_devices.asp | List all | Unified view |
| device_idf.asp | IDF add/edit | Different structure (no model) |
| device_generic.asp | Server + Switch add/edit | Identical fields |
| device_camera.asp | Camera add/edit | Unique fields (IDF + MAC) |
| save_network_device.asp | All saves | Universal endpoint |
**Total: 5 files** (or 6 if you separate add from edit)
---
## Navigation
```html
<li class="nav-header">INFRASTRUCTURE</li>
<li>
<a href="network_devices.asp">
<i class="zmdi zmdi-devices"></i> Network Devices
</a>
</li>
<li>
<a href="network_map.asp">
<i class="zmdi zmdi-map"></i> Network Map
</a>
</li>
```
---
## Migration Script
**Just run:** `/home/camp/projects/windows/shopdb/sql/add_infrastructure_vendor_model_support.sql`
**What it does:**
- Adds `modelid` to servers/switches/cameras (if not already present)
- Creates foreign keys to models table
- Creates `vw_network_devices` view
**What we DON'T need:**
- ❌ Add `idfid` to switches (not tracking)
- ❌ Add `idfid` to servers (not tracking)
- ✅ Cameras already have `idfid` and `macaddress`
---
## Ready to Build!
**Total:** 5 ASP files
**Estimated Time:** 8-12 hours
**Complexity:** Medium (simpler than original plan!)
Next step: Run migration, then create the 5 files.

View File

@@ -0,0 +1,562 @@
# Infrastructure Vendor/Model Support - Implementation Guide
**Date:** 2025-10-23
**Status:** Ready for Implementation
**Scope:** Add vendor/model tracking for servers, switches, and cameras
---
## Executive Summary
**Goal:** Extend the existing vendor/model system (currently used for PCs, Printers, and Machines) to also support infrastructure devices (Servers, Switches, Cameras).
**Decision:****Vendor types ABANDONED** - Keeping the simple vendors table as-is. No boolean flag refactoring needed.
### What We're Building
| Feature | Status | Impact |
|---------|--------|--------|
| Add `modelid` to servers/switches/cameras | ✅ Script ready | Database schema |
| Create `vw_network_devices` view | ✅ Script ready | Unified infrastructure query |
| Create server CRUD pages | ❌ New development | 4 files |
| Create switch CRUD pages | ❌ New development | 4 files |
| Create camera CRUD pages | ❌ New development | 4 files |
| Update navigation | ❌ New development | Menu items |
| Update network map | 🟡 Optional | Display vendor/model |
**Total New Files:** 12 ASP pages + nav updates
**Total Modified Files:** ~2-3 (navigation, possibly network_map.asp)
**Estimated Time:** 16-24 hours
---
## Part 1: Database Schema Changes
### Migration Script
**File:** `/home/camp/projects/windows/shopdb/sql/add_infrastructure_vendor_model_support.sql`
### What It Does
1. **Adds `modelid` column to infrastructure tables:**
```sql
servers.modelid → models.modelnumberid (FK)
switches.modelid → models.modelnumberid (FK)
cameras.modelid → models.modelnumberid (FK)
```
2. **Creates unified view for infrastructure:**
```sql
CREATE VIEW vw_network_devices AS
SELECT 'Server' AS device_type, serverid, modelid, modelnumber, vendor, ...
FROM servers LEFT JOIN models LEFT JOIN vendors
UNION ALL
SELECT 'Switch' AS device_type, switchid, modelid, modelnumber, vendor, ...
FROM switches LEFT JOIN models LEFT JOIN vendors
UNION ALL
SELECT 'Camera' AS device_type, cameraid, modelid, modelnumber, vendor, ...
FROM cameras LEFT JOIN models LEFT JOIN vendors
```
### Tables After Migration
**servers table:**
```
serverid INT(11) PK AUTO_INCREMENT
modelid INT(11) FK → models.modelnumberid ← NEW!
serialnumber VARCHAR(100)
ipaddress VARCHAR(15)
description VARCHAR(255)
maptop INT(11)
mapleft INT(11)
isactive BIT(1)
```
**switches table:**
```
switchid INT(11) PK AUTO_INCREMENT
modelid INT(11) FK → models.modelnumberid ← NEW!
serialnumber VARCHAR(100)
ipaddress VARCHAR(15)
description VARCHAR(255)
maptop INT(11)
mapleft INT(11)
isactive BIT(1)
```
**cameras table:**
```
cameraid INT(11) PK AUTO_INCREMENT
modelid INT(11) FK → models.modelnumberid ← NEW!
serialnumber VARCHAR(100)
ipaddress VARCHAR(15)
description VARCHAR(255)
maptop INT(11)
mapleft INT(11)
isactive BIT(1)
```
---
## Part 2: Required New Pages
### Server Management Pages (4 files)
#### 1. displayservers.asp - Server List View
**Purpose:** Display all servers in a searchable table
**Similar to:** displayprinters.asp, displaymachines.asp
**Key Features:**
- Sortable table with columns: ID, Model, Vendor, Serial, IP, Description, Status
- Search/filter functionality
- "Add New Server" button
- Click row → displayserver.asp (detail page)
**SQL Query:**
```sql
SELECT s.serverid, s.serialnumber, s.ipaddress, s.description, s.isactive,
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.isactive = 1
ORDER BY s.serverid DESC
```
#### 2. displayserver.asp - Server Detail with Inline Edit
**Purpose:** Show server details with inline edit form
**Similar to:** displayprinter.asp, displaymachine.asp
**Key Features:**
- Display mode: Show all server info with Edit button
- Edit mode: Inline form to update server
- Model/Vendor dropdown selection
- Save button → saveserver_direct.asp
- Delete/deactivate functionality
**SQL Query (Display):**
```sql
SELECT s.*, m.modelnumber, v.vendor, v.vendorid
FROM servers s
LEFT JOIN models m ON s.modelid = m.modelnumberid
LEFT JOIN vendors v ON m.vendorid = v.vendorid
WHERE s.serverid = ?
```
#### 3. addserver.asp - Add New Server Form
**Purpose:** Form to add a new server
**Similar to:** addprinter.asp, addmachine.asp
**Key Features:**
- Model dropdown (filtered from models table)
- Vendor dropdown (auto-filled based on model or separate selector)
- Serial number input (text)
- IP address input (validated)
- Description textarea
- Map coordinates (optional, maptop/mapleft)
- Submit → saveserver_direct.asp
**Model Dropdown Query:**
```sql
SELECT m.modelnumberid, m.modelnumber, v.vendor
FROM models m
INNER JOIN vendors v ON m.vendorid = v.vendorid
WHERE m.isactive = 1
ORDER BY v.vendor, m.modelnumber
```
**Or separate vendor/model selection:**
```sql
-- Step 1: Select vendor
SELECT vendorid, vendor FROM vendors WHERE isactive = 1 ORDER BY vendor
-- Step 2: Select model (filtered by vendorid)
SELECT modelnumberid, modelnumber FROM models
WHERE vendorid = ? AND isactive = 1 ORDER BY modelnumber
```
#### 4. saveserver_direct.asp - Server Save Endpoint
**Purpose:** Backend processor to insert/update server
**Similar to:** saveprinter_direct.asp, savemachine_direct.asp
**Key Features:**
- Validate all inputs using validation.asp functions
- INSERT for new server
- UPDATE for existing server
- Return JSON response or redirect
- Error handling
**Insert Query:**
```sql
INSERT INTO servers (modelid, serialnumber, ipaddress, description, maptop, mapleft, isactive)
VALUES (?, ?, ?, ?, ?, ?, 1)
```
**Update Query:**
```sql
UPDATE servers
SET modelid = ?, serialnumber = ?, ipaddress = ?, description = ?,
maptop = ?, mapleft = ?
WHERE serverid = ?
```
### Switch Management Pages (4 files)
Same structure as servers, just replace "server" with "switch":
- **displayswitches.asp** - Switch list
- **displayswitch.asp** - Switch detail with inline edit
- **addswitch.asp** - Add switch form
- **saveswitch_direct.asp** - Switch save endpoint
### Camera Management Pages (4 files)
Same structure, replace with "camera":
- **displaycameras.asp** - Camera list
- **displaycamera.asp** - Camera detail with inline edit
- **addcamera.asp** - Add camera form
- **savecamera_direct.asp** - Camera save endpoint
---
## Part 3: Navigation Updates
### Add Menu Items
**File to modify:** `includes/leftsidebar.asp` (or wherever main nav is)
**New menu section:**
```html
<!-- Infrastructure -->
<li class="nav-header">INFRASTRUCTURE</li>
<li><a href="displayservers.asp"><i class="zmdi zmdi-storage"></i> Servers</a></li>
<li><a href="displayswitches.asp"><i class="zmdi zmdi-device-hub"></i> Switches</a></li>
<li><a href="displaycameras.asp"><i class="zmdi zmdi-videocam"></i> Cameras</a></li>
```
Or add to existing "Network" or "Devices" section.
---
## Part 4: Optional Enhancements
### Update network_map.asp
If network_map.asp currently exists and displays network topology:
- Add server/switch/camera markers to the map
- Display vendor/model on hover/click
- Use vw_network_devices view for unified query
**Query for map:**
```sql
SELECT device_type, device_id, vendor, modelnumber,
ipaddress, description, maptop, mapleft
FROM vw_network_devices
WHERE isactive = 1 AND maptop IS NOT NULL AND mapleft IS NOT NULL
```
---
## Part 5: Code Templates
### Template 1: Infrastructure List Page (displayservers.asp)
```vbscript
<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/header.asp"-->
<!--#include file="./includes/leftsidebar.asp"-->
<!--#include file="./includes/topbarheader.asp"-->
<%
' Fetch all servers with model/vendor
Dim strSQL, rs
strSQL = "SELECT s.serverid, s.serialnumber, s.ipaddress, s.description, s.isactive, " & _
"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.isactive = 1 " & _
"ORDER BY s.serverid DESC"
Set rs = objConn.Execute(strSQL)
%>
<div class="content-wrapper">
<h2>Servers <a href="addserver.asp" class="btn btn-primary">Add Server</a></h2>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Vendor</th>
<th>Model</th>
<th>Serial Number</th>
<th>IP Address</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% Do While Not rs.EOF %>
<tr>
<td><%=rs("serverid")%></td>
<td><%=Server.HTMLEncode(rs("vendor") & "")%></td>
<td><%=Server.HTMLEncode(rs("modelnumber") & "")%></td>
<td><%=Server.HTMLEncode(rs("serialnumber") & "")%></td>
<td><%=Server.HTMLEncode(rs("ipaddress") & "")%></td>
<td><%=Server.HTMLEncode(rs("description") & "")%></td>
<td><a href="displayserver.asp?serverid=<%=rs("serverid")%>">View</a></td>
</tr>
<%
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
%>
</tbody>
</table>
</div>
<!--#include file="./includes/footer.asp"-->
```
### Template 2: Add Infrastructure Device Form (addserver.asp)
```vbscript
<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/header.asp"-->
<!--#include file="./includes/leftsidebar.asp"-->
<!--#include file="./includes/topbarheader.asp"-->
<div class="content-wrapper">
<h2>Add Server</h2>
<form method="post" action="saveserver_direct.asp">
<div class="form-group">
<label>Model</label>
<select name="modelid" required class="form-control">
<option value="">-- Select Model --</option>
<%
Dim strSQL, rsModels
strSQL = "SELECT m.modelnumberid, m.modelnumber, v.vendor " & _
"FROM models m " & _
"INNER JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE m.isactive = 1 " & _
"ORDER BY v.vendor, m.modelnumber"
Set rsModels = objConn.Execute(strSQL)
Do While Not rsModels.EOF
%>
<option value="<%=rsModels("modelnumberid")%>">
<%=Server.HTMLEncode(rsModels("vendor") & " - " & rsModels("modelnumber"))%>
</option>
<%
rsModels.MoveNext
Loop
rsModels.Close
Set rsModels = Nothing
%>
</select>
</div>
<div class="form-group">
<label>Serial Number</label>
<input type="text" name="serialnumber" class="form-control" maxlength="100">
</div>
<div class="form-group">
<label>IP Address</label>
<input type="text" name="ipaddress" class="form-control" maxlength="15"
pattern="^[0-9\.]+$" placeholder="192.168.1.100">
</div>
<div class="form-group">
<label>Description</label>
<textarea name="description" class="form-control" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-success">Save Server</button>
<a href="displayservers.asp" class="btn btn-secondary">Cancel</a>
</form>
</div>
<!--#include file="./includes/footer.asp"-->
```
### Template 3: Save Infrastructure Device (saveserver_direct.asp)
```vbscript
<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/error_handler.asp"-->
<!--#include file="./includes/validation.asp"-->
<!--#include file="./includes/db_helpers.asp"-->
<%
' Validate inputs
Dim modelid, serialnumber, ipaddress, description
modelid = GetSafeInteger("FORM", "modelid", 0, 1, 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, "")
' Validate required fields
If modelid = 0 Then
Response.Write("Error: Model is required")
Response.End
End If
' Insert server
Dim strSQL
strSQL = "INSERT INTO servers (modelid, serialnumber, ipaddress, description, isactive) " & _
"VALUES (?, ?, ?, ?, 1)"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(modelid, serialnumber, ipaddress, description))
Call CleanupResources()
' Redirect to list
Response.Redirect("displayservers.asp")
%>
```
---
## Part 6: Implementation Checklist
### Phase 1: Database Migration
- [ ] Review `add_infrastructure_vendor_model_support.sql`
- [ ] Backup database
- [ ] Run migration on test database
- [ ] Verify `modelid` columns added to servers/switches/cameras
- [ ] Verify foreign keys created
- [ ] Verify `vw_network_devices` view created
- [ ] Test view returns correct data
### Phase 2: Server Pages (Do This First)
- [ ] Create `displayservers.asp` (list view)
- [ ] Create `addserver.asp` (add form)
- [ ] Create `saveserver_direct.asp` (save endpoint)
- [ ] Create `displayserver.asp` (detail with inline edit)
- [ ] Test: Add new server
- [ ] Test: Edit existing server
- [ ] Test: View server list
### Phase 3: Switch Pages
- [ ] Create `displayswitches.asp` (list view)
- [ ] Create `addswitch.asp` (add form)
- [ ] Create `saveswitch_direct.asp` (save endpoint)
- [ ] Create `displayswitch.asp` (detail with inline edit)
- [ ] Test: Add/edit/view switches
### Phase 4: Camera Pages
- [ ] Create `displaycameras.asp` (list view)
- [ ] Create `addcamera.asp` (add form)
- [ ] Create `savecamera_direct.asp` (save endpoint)
- [ ] Create `displaycamera.asp` (detail with inline edit)
- [ ] Test: Add/edit/view cameras
### Phase 5: Navigation & Polish
- [ ] Add menu items to navigation
- [ ] Test all navigation links
- [ ] Update dashboard (optional - add infrastructure stats)
- [ ] Update search (optional - add infrastructure to search results)
### Phase 6: Optional Enhancements
- [ ] Update `network_map.asp` to show infrastructure devices
- [ ] Add infrastructure reports (count by vendor, etc.)
- [ ] Add bulk import for infrastructure (CSV upload)
### Phase 7: Documentation & Deployment
- [ ] Update user documentation
- [ ] Update technical documentation
- [ ] Test on production-like data
- [ ] Create deployment checklist
- [ ] Deploy to production
---
## Part 7: Testing Plan
### Unit Tests (Per Device Type)
- [ ] Can add device with valid model
- [ ] Can add device without model (modelid NULL)
- [ ] Can edit device and change model
- [ ] Can delete/deactivate device
- [ ] Form validation works (IP format, required fields)
- [ ] SQL injection prevention (parameterized queries)
### Integration Tests
- [ ] Device appears in list immediately after creation
- [ ] Device detail page shows vendor/model info correctly
- [ ] Model dropdown only shows active models
- [ ] Vendor name displays correctly (from model FK)
- [ ] Map coordinates save/display correctly
### Data Integrity Tests
- [ ] Foreign keys enforce referential integrity
- [ ] Deleting a model doesn't break device (ON DELETE SET NULL)
- [ ] View `vw_network_devices` returns all device types
- [ ] NULL model handling (device with no model assigned)
---
## Part 8: Rollback Plan
If issues arise:
1. Migration script is **non-destructive** - only adds columns, doesn't modify existing data
2. Can drop columns: `ALTER TABLE servers DROP COLUMN modelid`
3. Can drop view: `DROP VIEW vw_network_devices`
4. New ASP pages can be deleted without affecting existing functionality
5. Navigation changes can be reverted
**Risk Level:** LOW - This is pure additive functionality, no changes to existing code.
---
## Part 9: Time Estimates
| Task | Time | Notes |
|------|------|-------|
| Database migration | 30 min | Run script + verify |
| Server pages (4 files) | 4-6 hours | First set, establish pattern |
| Switch pages (4 files) | 2-3 hours | Copy/modify from servers |
| Camera pages (4 files) | 2-3 hours | Copy/modify from servers |
| Navigation updates | 30 min | Add menu items |
| Testing | 3-4 hours | Full testing cycle |
| Documentation | 1-2 hours | User guide updates |
| **Total** | **13-19 hours** | ~2-3 days of work |
---
## Part 10: Success Criteria
**Database:**
- All 3 tables have modelid column with FK to models
- vw_network_devices view returns data from all 3 tables
**Functionality:**
- Can add/edit/view/delete servers, switches, cameras
- Vendor/model information displays correctly
- Forms validate inputs properly
- No SQL errors
**User Experience:**
- Navigation easy to find
- Forms intuitive (like printer/machine forms)
- List views show relevant info at a glance
**Code Quality:**
- Follows existing coding standards (STANDARDS.md)
- Uses parameterized queries (no SQL injection)
- Proper error handling
- Consistent with printer/machine patterns
---
## Next Steps
1. **Get approval** on this simplified approach
2. **Run database migration** on test environment
3. **Start with server pages** - establish the pattern
4. **Copy/adapt for switches and cameras** - reuse code
5. **Test thoroughly**
6. **Document and deploy**
---
**Document Status:** Ready for Implementation
**Last Updated:** 2025-10-23
**Approved By:** _[Pending]_

View File

@@ -0,0 +1,214 @@
# Inventory.xlsx Column Mapping to Database
**Date:** 2025-11-06
**Coverage:** 100% (35/35 columns)
---
## Complete Column Mapping
| # | Inventory Column | Database Location | Notes |
|---|------------------|-------------------|-------|
| 1 | Operational Status | `machines.machinestatusid``machinestatus.machinestatus` | |
| 2 | OT Location Name | `machines.businessunitid``businessunits.businessunit` | |
| 3 | OT Location Liasion or Site OT Asset Manager | `businessunits.liaisonname` | |
| 4 | System Name | `machines.machinenumber` | |
| 5 | Hostname | `machines.hostname` | For PCs |
| 6 | OS Name | `machines.osid``operatingsystems.osname` (PCs) OR `machines.controllerosid``operatingsystems.osname` (CNCs) | Same table for both! |
| 7 | Serial # | `machines.serialnumber` | |
| 8 | Manufacturer | `machines.modelnumberid``models.vendorid``vendors.vendor` | |
| 9 | Model | `machines.modelnumberid``models.modelnumber` | |
| 10 | Ge Coreload | `compliance.gecoreload` | |
| 11 | Device Description | `machines.alias` OR `machinetypes.machinetype` | |
| 12 | Device Type | `machines.machinetypeid``machinetypes.machinetype` | |
| 13 | IP Address | `communications.address` (WHERE `comstypeid=1`) | First IP record |
| 14 | IP Address (interface 2) | `communications.address` | Second IP record |
| 15 | IP Address (Interface 3) | `communications.address` | Third IP record |
| 16 | MAC Address (interface1) | `communications.macaddress` | First MAC record |
| 17 | MAC address (Interface 2) | `communications.macaddress` | Second MAC record |
| 18 | MAC Address (Interface 3) | `communications.macaddress` | Third MAC record |
| 19 | On GE Network | `compliance.ongenetwork` | |
| 20 | Vlan | `communications.settings` JSON | `{"vlan": "100"}` |
| 21 | Asset Criticality(L/M/H) | `compliance.assetcriticality` | |
| 22 | CUI/CMMC Data Classification | `compliance.cuidataclassification` | |
| 23 | DoD Asset Type | `compliance.dodassettype` | |
| 24 | DoD Asset Sub-Type | `compliance.dodassetsubtype` | |
| 25 | OT Environment | `compliance.otenvironment` | |
| 26 | 3rd Party (Other) Managed (Y/N) | `compliance.managedbyvendorid``vendors.vendor` | FK to vendors table |
| 27 | Change Restricted (Y/N) | `compliance.changerestricted` | |
| 28 | Other Deployment Notes | `machines.machinenotes` | |
| 29 | Jump_Box | `compliance.jumpbox` | |
| 30 | MFT | `compliance.mft` | Managed File Transfer |
| 31 | Scan_Date_Status | `compliancescans.scanstatus` (latest) | Most recent scan |
| 32 | OT Asset Fields | `machinerelationships` table | "WJ 2023 WJ 2024" = dualpath relationship |
| 33 | Controller_Type1 | `machines.controllertypeid``controllertypes.controllertype` | For CNC machines |
| 34 | Controller_OS1 | `machines.controllerosid``operatingsystems.osname` | For CNC machines |
| 35 | PC_Type1 | `machines.machinetypeid``machinetypes.machinetype` | For PCs |
---
## Special Mapping Notes
### OT Asset Fields (Column 32) - Machine Relationships
**Inventory Pattern:**
```
Row 1: Machine 2023 (192.168.*.*) | OT Asset Fields: "WJ 2023 WJ 2024"
Row 2: PC / Attached PC (10.134.*.*) | OT Asset Fields: "WJ 2023 WJ 2024"
Row 3: Machine 2024 (192.168.*.*) | OT Asset Fields: "WJ 2023 WJ 2024"
Row 4: PC / Attached PC (10.134.*.*) | OT Asset Fields: "WJ 2023 WJ 2024"
```
**Database Representation:**
```sql
-- Machines 2023 and 2024 are dualpath
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2)
VALUES (1, 2023, 2024);
-- PC controls machine 2023
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2)
VALUES (2, 2023, [pc_machineid]);
-- PC controls machine 2024
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2)
VALUES (2, 2024, [pc_machineid]);
```
**Relationship Types:**
- relationshiptypeid = 1: Dualpath (machines share controller)
- relationshiptypeid = 2: Controlled By (machine controlled by PC)
---
### IP Address Pattern Recognition
**Machine IPs:** `192.168.*.*` → CNC machines
**PC IPs:** `10.134.*.*` → Controlling PCs
**System Name Pattern:**
- Machine: `2023`
- PC: `2023 / Attached PC` or `IT-LAB-01`
---
## Export Query for inventory.xlsx Format
```sql
SELECT
-- Columns 1-12: Basic Info
ms.machinestatus AS 'Operational Status',
bu.businessunit AS 'OT Location Name',
bu.liaisonname AS 'OT Location Liasion or Site OT Asset Manager',
m.machinenumber AS 'System Name',
m.hostname AS 'Hostname',
COALESCE(os_pc.osname, os_ctrl.osname) AS 'OS Name',
m.serialnumber AS 'Serial #',
v.vendor AS 'Manufacturer',
mo.modelnumber AS 'Model',
comp.gecoreload AS 'Ge Coreload',
m.alias AS 'Device Description',
mt.machinetype AS 'Device Type',
-- Columns 13-20: Network
c_ip1.address AS 'IP Address',
c_ip2.address AS 'IP Address (interface 2)',
c_ip3.address AS 'IP Address (Interface 3)',
c_mac1.macaddress AS 'MAC Address (interface1)',
c_mac2.macaddress AS 'MAC Address (interface 2)',
c_mac3.macaddress AS 'MAC Address (Interface 3)',
comp.ongenetwork AS 'On GE Network',
c_ip1.settings->>'$.vlan' AS 'Vlan',
-- Columns 21-32: Compliance
comp.assetcriticality AS 'Asset Criticality(L/M/H)',
comp.cuidataclassification AS 'CUI/CMMC Data Classification',
comp.dodassettype AS 'DoD Asset Type',
comp.dodassetsubtype AS 'DoD Asset Sub-Type',
comp.otenvironment AS 'OT Environment',
mv.vendor AS '3rd Party (Other) Managed (Y/N)',
comp.changerestricted AS 'Change Restricted (Y/N)',
m.machinenotes AS 'Other Deployment Notes',
comp.jumpbox AS 'Jump_Box',
comp.mft AS 'MFT',
(SELECT scanstatus FROM compliancescans cs
WHERE cs.machineid = m.machineid
ORDER BY scandate DESC LIMIT 1) AS 'Scan_Date_Status',
-- Column 32: OT Asset Fields (relationships)
GROUP_CONCAT(DISTINCT
CONCAT(mt_rel.machinetype, ' ', m_rel.machinenumber)
SEPARATOR ' '
) AS 'OT Asset Fields',
-- Columns 33-35: Controller/Type
ct.controllertype AS 'Controller_Type1',
os_ctrl.osname AS 'Controller_OS1',
mt.machinetype AS 'PC_Type1'
FROM machines m
LEFT JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN machinestatus ms ON m.machinestatusid = ms.machinestatusid
LEFT JOIN businessunits bu ON m.businessunitid = bu.businessunitid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
LEFT JOIN operatingsystems os_pc ON m.osid = os_pc.osid
LEFT JOIN controllertypes ct ON m.controllertypeid = ct.controllertypeid
LEFT JOIN operatingsystems os_ctrl ON m.controllerosid = os_ctrl.osid
-- Compliance
LEFT JOIN compliance comp ON m.machineid = comp.machineid
LEFT JOIN vendors mv ON comp.managedbyvendorid = mv.vendorid
-- Communications (IP addresses)
LEFT JOIN (SELECT * FROM communications WHERE comstypeid IN (1,3) ORDER BY comid) c_ip1
ON m.machineid = c_ip1.machineid AND c_ip1.rn = 1
LEFT JOIN (SELECT * FROM communications WHERE comstypeid IN (1,3) ORDER BY comid) c_ip2
ON m.machineid = c_ip2.machineid AND c_ip2.rn = 2
LEFT JOIN (SELECT * FROM communications WHERE comstypeid IN (1,3) ORDER BY comid) c_ip3
ON m.machineid = c_ip3.machineid AND c_ip3.rn = 3
-- MAC addresses
LEFT JOIN (SELECT * FROM communications WHERE macaddress IS NOT NULL ORDER BY comid) c_mac1
ON m.machineid = c_mac1.machineid AND c_mac1.rn = 1
LEFT JOIN (SELECT * FROM communications WHERE macaddress IS NOT NULL ORDER BY comid) c_mac2
ON m.machineid = c_mac2.machineid AND c_mac2.rn = 2
LEFT JOIN (SELECT * FROM communications WHERE macaddress IS NOT NULL ORDER BY comid) c_mac3
ON m.machineid = c_mac3.machineid AND c_mac3.rn = 3
-- Relationships (for OT Asset Fields)
LEFT JOIN machinerelationships mr ON (
mr.machineid1 = m.machineid OR mr.machineid2 = m.machineid
)
LEFT JOIN machines m_rel ON (
CASE
WHEN mr.machineid1 = m.machineid THEN mr.machineid2
ELSE mr.machineid1
END = m_rel.machineid
)
LEFT JOIN machinetypes mt_rel ON m_rel.machinetypeid = mt_rel.machinetypeid
WHERE m.isactive = 1
GROUP BY m.machineid;
```
---
## Coverage Summary
**100% Coverage** - All 35 inventory columns mapped to database
**Storage Distribution:**
- `machines` table: 11 columns
- `compliance` table: 9 columns
- `communications` table: 8 columns (includes VLAN in JSON)
- `businessunits` table: 2 columns
- `machinerelationships` table: 1 column (OT Asset Fields)
- `compliancescans` table: 1 column
- Various lookup tables: 3 columns
**Design Benefits:**
- ✅ Normalized structure (no redundancy)
- ✅ Flexible relationships (dualpath, controller associations)
- ✅ Extensible (easy to add new fields/types)
- ✅ Complete audit trail
- ✅ Single source of truth

View File

@@ -0,0 +1,342 @@
# Machine Relationships - Usage Examples
**Date:** 2025-11-06
**Purpose:** Show real-world examples of how machine relationships work
---
## Example Scenario: Dualpath Lathes with Shared Controller
**Setup:**
- Machine 2001 (CNC Lathe)
- Machine 2002 (CNC Lathe)
- PC 500 (Shopfloor PC, hostname: PC-CNC-01)
**Relationships:**
1. 2001 and 2002 share the same controller (dualpath)
2. Both machines communicate directly with PC 500
---
## Data Setup
### Step 1: Insert Relationship Types (one-time setup)
```sql
INSERT INTO relationshiptypes (relationshiptype, description, isbidirectional) VALUES
('Dualpath', 'Machines share same controller', 1),
('Controlled By', 'Machine controlled by PC/controller', 0),
('Controls', 'PC/controller controls machine', 0);
```
### Step 2: Link the dualpath machines
```sql
-- 2001 and 2002 are dualpath
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2, notes) VALUES
(1, 2001, 2002, 'Dualpath lathe pair - share controller');
```
### Step 3: Link machines to controlling PC
```sql
-- 2001 is controlled by PC 500
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2, notes) VALUES
(2, 2001, 500, 'Primary CNC controller');
-- 2002 is controlled by PC 500
INSERT INTO machinerelationships (relationshiptypeid, machineid1, machineid2, notes) VALUES
(2, 2002, 500, 'Primary CNC controller');
```
---
## Common Queries
### Q1: Is machine 2001 dualpath? With what machine?
```sql
SELECT
m.machinenumber AS this_machine,
CASE
WHEN m2.machineid IS NOT NULL THEN 'Yes'
ELSE 'No'
END AS is_dualpath,
m2.machinenumber AS dualpath_partner,
rt.relationshiptype
FROM machines m
LEFT JOIN machinerelationships mr ON (
(mr.machineid1 = m.machineid OR mr.machineid2 = m.machineid)
AND mr.relationshiptypeid = 1
)
LEFT JOIN machines m2 ON (
CASE
WHEN mr.machineid1 = m.machineid THEN mr.machineid2
ELSE mr.machineid1
END = m2.machineid
)
LEFT JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE m.machineid = 2001;
```
**Result:**
```
this_machine | is_dualpath | dualpath_partner | relationshiptype
2001 | Yes | 2002 | Dualpath
```
---
### Q2: Which PC controls machine 2001?
```sql
SELECT
m.machinenumber AS cnc_machine,
pc.machineid AS controller_id,
pc.machinenumber AS controller_location,
pc.hostname AS controller_hostname,
rt.relationshiptype
FROM machines m
JOIN machinerelationships mr ON mr.machineid1 = m.machineid
JOIN machines pc ON mr.machineid2 = pc.machineid
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE m.machineid = 2001
AND mr.relationshiptypeid = 2;
```
**Result:**
```
cnc_machine | controller_id | controller_location | controller_hostname | relationshiptype
2001 | 500 | IT-LAB-01 | PC-CNC-01 | Controlled By
```
---
### Q3: What machines does PC 500 control?
```sql
SELECT
pc.hostname AS controller,
m.machinenumber AS controlled_machine,
mt.machinetype,
mr.notes
FROM machines pc
JOIN machinerelationships mr ON mr.machineid2 = pc.machineid
JOIN machines m ON mr.machineid1 = m.machineid
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE pc.machineid = 500
AND mr.relationshiptypeid = 2
ORDER BY m.machinenumber;
```
**Result:**
```
controller | controlled_machine | machinetype | notes
PC-CNC-01 | 2001 | CNC - Lathe | Primary CNC controller
PC-CNC-01 | 2002 | CNC - Lathe | Primary CNC controller
```
---
### Q4: Complete picture - Machine + Dualpath + Controller + IP
```sql
SELECT
m.machinenumber AS machine,
mt.machinetype,
-- Dualpath info
m_dual.machinenumber AS dualpath_partner,
-- Controller info
pc.hostname AS controller,
pc.machinenumber AS controller_location,
-- Controller's IP
c.address AS controller_ip
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
-- Find dualpath partner
LEFT JOIN machinerelationships mr_dual ON (
(mr_dual.machineid1 = m.machineid OR mr_dual.machineid2 = m.machineid)
AND mr_dual.relationshiptypeid = 1
)
LEFT JOIN machines m_dual ON (
CASE
WHEN mr_dual.machineid1 = m.machineid THEN mr_dual.machineid2
ELSE mr_dual.machineid1
END = m_dual.machineid
)
-- Find controller
LEFT JOIN machinerelationships mr_ctrl ON (
mr_ctrl.machineid1 = m.machineid
AND mr_ctrl.relationshiptypeid = 2
)
LEFT JOIN machines pc ON mr_ctrl.machineid2 = pc.machineid
-- Get controller's IP address
LEFT JOIN communications c ON (
pc.machineid = c.machineid
AND c.comstypeid = 1 -- IP type
)
WHERE m.machineid IN (2001, 2002);
```
**Result:**
```
machine | machinetype | dualpath_partner | controller | controller_location | controller_ip
2001 | CNC - Lathe | 2002 | PC-CNC-01 | IT-LAB-01 | 192.168.1.50
2002 | CNC - Lathe | 2001 | PC-CNC-01 | IT-LAB-01 | 192.168.1.50
```
---
## Recommended View: vw_machine_relationships
Create a view for easy access:
```sql
CREATE VIEW vw_machine_relationships AS
SELECT
m.machineid,
m.machinenumber,
m.alias,
mt.machinetype,
-- Dualpath info
CASE WHEN m_dual.machineid IS NOT NULL THEN 1 ELSE 0 END AS is_dualpath,
m_dual.machineid AS dualpath_partner_id,
m_dual.machinenumber AS dualpath_partner,
-- Controller info
pc.machineid AS controller_id,
pc.hostname AS controller_hostname,
pc.machinenumber AS controller_location,
-- Controller IP
c.address AS controller_ip,
-- Relationship counts
(SELECT COUNT(*)
FROM machinerelationships mr
WHERE mr.machineid1 = m.machineid OR mr.machineid2 = m.machineid
) AS total_relationships
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
-- Dualpath partner
LEFT JOIN machinerelationships mr_dual ON (
(mr_dual.machineid1 = m.machineid OR mr_dual.machineid2 = m.machineid)
AND mr_dual.relationshiptypeid = 1
AND mr_dual.isactive = 1
)
LEFT JOIN machines m_dual ON (
CASE
WHEN mr_dual.machineid1 = m.machineid THEN mr_dual.machineid2
ELSE mr_dual.machineid1
END = m_dual.machineid
)
-- Controller PC
LEFT JOIN machinerelationships mr_ctrl ON (
mr_ctrl.machineid1 = m.machineid
AND mr_ctrl.relationshiptypeid = 2
AND mr_ctrl.isactive = 1
)
LEFT JOIN machines pc ON mr_ctrl.machineid2 = pc.machineid
-- Controller IP
LEFT JOIN communications c ON (
pc.machineid = c.machineid
AND c.comstypeid = 1
)
WHERE m.isactive = 1;
```
### Using the View:
```sql
-- Simple query for machine 2001
SELECT * FROM vw_machine_relationships WHERE machineid = 2001;
-- Show all dualpath machines
SELECT * FROM vw_machine_relationships WHERE is_dualpath = 1;
-- Show all machines controlled by PC 500
SELECT * FROM vw_machine_relationships WHERE controller_id = 500;
```
---
## ASP Code Examples
### Check if machine is dualpath:
```vbscript
strSQL = "SELECT * FROM vw_machine_relationships WHERE machineid = ?"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(CLng(machineId)))
If Not rs.EOF Then
isDualpath = rs("is_dualpath")
If isDualpath = 1 Then
Response.Write("Dualpath with: " & rs("dualpath_partner"))
End If
If Not IsNull(rs("controller_hostname")) Then
Response.Write("Controller: " & rs("controller_hostname"))
Response.Write(" (IP: " & rs("controller_ip") & ")")
End If
End If
```
### Display machine card with relationships:
```vbscript
<div class="machine-card">
<h3>Machine <%= rs("machinenumber") %></h3>
<p>Type: <%= rs("machinetype") %></p>
<% If rs("is_dualpath") = 1 Then %>
<div class="dualpath-badge">
<strong>Dualpath:</strong> Linked with <%= rs("dualpath_partner") %>
</div>
<% End If %>
<% If Not IsNull(rs("controller_hostname")) Then %>
<div class="controller-info">
<strong>Controller:</strong> <%= rs("controller_hostname") %><br>
<strong>Location:</strong> <%= rs("controller_location") %><br>
<strong>IP:</strong> <%= rs("controller_ip") %>
</div>
<% End If %>
</div>
```
---
## Benefits of This Design
**Flexible** - Any machine can relate to any other machine
**Bidirectional** - Dualpath works both ways automatically
**Directional** - "Controlled By" has clear direction
**Extensible** - Easy to add new relationship types
**Query-friendly** - Simple JOINs to get related machines
**Unified** - PCs are machines too, so PC-to-machine relationships use same table
**Future-proof** - Supports clusters, backups, master-slave, etc.
---
## Future Relationship Types (Examples)
```sql
INSERT INTO relationshiptypes (relationshiptype, description, isbidirectional) VALUES
('Backup', 'Backup/redundant machine', 1),
('Cluster Member', 'Member of machine cluster', 1),
('Feeds Into', 'Output feeds into next machine', 0),
('Mirror', 'Mirrored configuration', 1),
('Hot Standby', 'Hot standby/failover machine', 0);
```
These would all work with the same `machinerelationships` table!

View File

@@ -0,0 +1,197 @@
# Database Migration - Quick Reference
**Date:** 2025-11-06
**Status:** Design Finalized
---
## What We're Doing
Consolidating PCs into machines table + adding communications, compliance, and warranty tracking.
---
## New Tables (7)
1. **comstypes** (8 cols) - Communication type lookup (IP, Serial, Network, etc.)
2. **communications** (9 cols) - Universal comm tracking with generic `address` field
3. **compliance** (14 cols) - Security/compliance data from inventory.xlsx
4. **compliancescans** (5 cols) - AV/malware scan log
5. **warranties** (5 cols) - Simple warranty tracking (name + expiration)
6. **relationshiptypes** (5 cols) - Machine relationship types (Dualpath, Controlled By, etc.)
7. **machinerelationships** (6 cols) - Machine-to-machine relationships tracking
---
## Modified Tables (2)
1. **machines** - Add 9 PC-related columns (hostname, serialnumber, osid, etc.)
2. **businessunits** - Add 2 liaison fields (liaisonname, liaisonsso)
---
## Renamed Tables (1)
1. **pcstatus → machinestatus** - Consistent naming
---
## Deprecated Tables (4)
Drop after 30-day testing:
1. pc
2. pc_comm_config
3. pc_network_interfaces
4. pctype
---
## Key Design Decisions
**Generic `address` field** - One field for IP/COM/USB/etc (determined by comstypeid)
**Simplified warranties** - Just name + expiration date
**Liaison in businessunits** - No separate liaisons table
**Unified machine types** - PC types added to machinetypes
**Vendor FK** - compliance.managedbyvendorid → vendors table
**Minimal audit** - Removed unnecessary dateadded/lastupdated fields
**Machine relationships** - Generic table for dualpath, PC-to-machine control, future clusters
---
## Migration Volumes
| Source | Destination | Records |
|--------|-------------|---------|
| pc table | machines | 277 |
| machines.ipaddress1/2 | communications | ~266 |
| pc_network_interfaces | communications | ~277+ |
| pc_comm_config | communications | ~100+ |
| pc warranties | warranties | ~277+ |
| **Total** | | **~650+ comms** |
---
## Phase 1 Scripts (Ready to Create)
1. Create communications infrastructure
2. Extend machines table
3. Create PC machine types
4. Create warranty infrastructure
5. Create compliance infrastructure
6. Extend businessunits table
7. Rename pcstatus table
8. Create machine relationships infrastructure
**Time:** ~25 minutes
**Reversible:** Yes
---
## machines Table Final Structure (21 columns)
1. machineid (PK)
2. machinetypeid (now includes PC types!)
3. machinenumber
4. alias
5. hostname (NEW - for PCs)
6. serialnumber (NEW)
7. loggedinuser (NEW - for PCs)
8. modelnumberid
9. controllertypeid (NEW - for CNC machines)
10. controllerosid (NEW - controller OS, uses operatingsystems table)
11. osid (NEW - for PCs, uses operatingsystems table)
12. machinestatusid (NEW - renamed from pcstatusid)
13. businessunitid
14. printerid
15. mapleft
16. maptop
17. isactive
18. islocationonly
19. machinenotes
20. lastupdated (NEW)
21. dateadded (NEW)
**Key Design:** Both PC OS and Controller OS use the same `operatingsystems` table!
**Removed:** pctypeid, isvnc, requires_manual_machine_config, ipaddress1, ipaddress2
---
## communications Table Structure (9 columns)
1. comid (PK)
2. machineid (FK)
3. comstypeid (FK)
4. **address** (VARCHAR - universal: IP, COM1, eth0, etc.)
5. port (for IP types)
6. macaddress (for network types)
7. description
8. settings (JSON - type-specific config)
9. isactive
---
## compliance Table Structure (15 columns)
1. complianceid (PK)
2. machineid (FK)
3. ongenetwork
4. gecoreload
5. assetcriticality
6. cuidataclassification
7. dodassettype
8. dodassetsubtype
9. otenvironment
10. managedbyvendorid (FK to vendors)
11. changerestricted
12. jumpbox
13. mft (Managed File Transfer)
14. notes
15. isactive
---
## warranties Table Structure (5 columns)
1. warrantyid (PK)
2. machineid (FK)
3. warrantyname
4. enddate
5. isactive
---
## relationshiptypes Table Structure (5 columns)
1. relationshiptypeid (PK)
2. relationshiptype ('Dualpath', 'Controlled By', 'Controls', etc.)
3. description
4. isbidirectional (1 for symmetric, 0 for directional)
5. isactive
---
## machinerelationships Table Structure (6 columns)
1. relationshipid (PK)
2. relationshiptypeid (FK)
3. machineid1 (FK to machines)
4. machineid2 (FK to machines)
5. notes
6. isactive
**Key Feature:** Both machines and PCs are in machines table, so any machine can relate to any other machine.
**Example:**
- Machines 2001 & 2002 are dualpath (relationshiptypeid=1)
- Both controlled by PC 500 (relationshiptypeid=2)
---
## Next Step
**Create Phase 1 SQL Scripts** (8 scripts + 8 rollback scripts)
---
See **DATABASE_MIGRATION_FINAL_DESIGN.md** for complete specification.

View File

@@ -0,0 +1,214 @@
# Database Migration - Current Status Summary
**Date:** 2025-11-06
**Session End:** Ready to create Phase 1 SQL scripts
**Status:** Design 100% Complete ✅
---
## Design Complete ✅
All design decisions finalized and documented:
1. ✅ Communications infrastructure (generic `address` field)
2. ✅ Compliance tracking (from inventory.xlsx)
3. ✅ Warranty management (simplified)
4. ✅ Machine relationships (dualpath, controller associations)
5. ✅ Controller fields (controllertypeid, controllerosid)
6. ✅ 100% inventory.xlsx column coverage (35/35 columns)
---
## Final Table Counts
**New Tables:** 7
1. comstypes (8 columns)
2. communications (9 columns)
3. compliance (15 columns) - includes MFT
4. compliancescans (5 columns)
5. warranties (5 columns)
6. relationshiptypes (5 columns)
7. machinerelationships (6 columns)
**Modified Tables:** 2
1. machines (+11 columns) - now 21 total
2. businessunits (+2 columns) - now 6 total
**Renamed:** pcstatus → machinestatus
**Deprecated:** pc, pc_comm_config, pc_network_interfaces, pctype
**Total New Columns:** 61
---
## Final machines Table (21 columns)
1. machineid (PK)
2. machinetypeid (FK) - includes PC types
3. machinenumber
4. alias
5. hostname (NEW - for PCs)
6. serialnumber (NEW)
7. loggedinuser (NEW - for PCs)
8. modelnumberid (FK)
9. **controllertypeid (NEW - for CNCs)**
10. **controllerosid (NEW - controller OS)**
11. **osid (NEW - for PCs)**
12. machinestatusid (NEW)
13. businessunitid (FK)
14. printerid (FK)
15. mapleft
16. maptop
17. isactive
18. islocationonly
19. machinenotes
20. lastupdated (NEW)
21. dateadded (NEW)
**Key:** Both PC OS and Controller OS use the same `operatingsystems` table!
---
## Inventory.xlsx Coverage: 100% (35/35)
All columns mapped to database:
- ✅ VLAN → communications.settings JSON
- ✅ MFT → compliance.mft
- ✅ OT Asset Fields → machinerelationships table
See: `INVENTORY_COLUMN_MAPPING.md` for complete mapping
---
## Documentation Files
1. **DATABASE_MIGRATION_FINAL_DESIGN.md** (23KB)
- Complete specification
- All table structures
- Migration strategy
- Risk mitigation
2. **MIGRATION_QUICK_REFERENCE.md** (4.6KB)
- Quick lookup
- Table summaries
- Key decisions
3. **MACHINE_RELATIONSHIPS_EXAMPLES.md** (9.4KB)
- Real-world examples
- Query patterns
- ASP code samples
4. **INVENTORY_COLUMN_MAPPING.md** (NEW - 8KB)
- 100% column coverage
- Export query
- Mapping notes
---
## Next Steps (When Ready)
### Phase 1: Create SQL Scripts (8 scripts)
1. **Script 01:** Create communications infrastructure
- comstypes table
- communications table
2. **Script 02:** Extend machines table
- Add 11 new columns
- Add indexes and FKs
3. **Script 03:** Create PC machine types
- Insert into machinetypes table
4. **Script 04:** Create warranty infrastructure
- warranties table
5. **Script 05:** Create compliance infrastructure
- compliance table (15 columns - includes MFT)
- compliancescans table
6. **Script 06:** Extend businessunits table
- Add liaisonname, liaisonsso
7. **Script 07:** Rename pcstatus to machinestatus
- RENAME TABLE
- Rename columns
8. **Script 08:** Create machine relationships infrastructure
- relationshiptypes table
- machinerelationships table
**Plus:** 8 corresponding rollback scripts
**Estimated Time:** 25 minutes
**Reversibility:** Full (rollback scripts provided)
---
## Key Design Decisions
### ✅ Generic `address` Field
One field for IP, COM1, USB, etc. Type determined by `comstypeid`.
### ✅ Controller Fields in machines Table
- controllertypeid (FK → controllertypes)
- controllerosid (FK → operatingsystems)
- Same operatingsystems table used for both PC OS and Controller OS
### ✅ Machine Relationships
- Dualpath: Machines sharing controller
- Controlled By: PC controlling machine
- Future: Clusters, backups, master-slave, etc.
### ✅ VLAN in JSON
Stored in communications.settings instead of dedicated column.
### ✅ MFT Field
Added to compliance table for Managed File Transfer tracking.
### ✅ Simplified Warranties
Just warrantyname and enddate - minimal approach.
### ✅ Liaison in businessunits
No separate liaisons table - added directly to businessunits.
---
## Migration Volumes (Estimated)
- machines: 543 total (266 existing + 277 from PCs)
- communications: ~650+ records
- warranties: ~277+ records
- compliance: TBD (from inventory.xlsx import)
- compliancescans: TBD (ongoing logging)
- machinerelationships: ~50+ (dualpath pairs + PC controllers)
---
## Questions to Revisit (Optional)
None currently - all design decisions finalized!
---
## Session Notes
**What was accomplished:**
- Designed 7 new tables
- Extended 2 existing tables
- Achieved 100% inventory.xlsx coverage
- Documented machine relationships pattern
- Finalized controller field approach
- Created comprehensive documentation
**Ready for:**
- Phase 1 SQL script creation
- Dev environment testing
- Data migration planning
---
**Status:** Ready to proceed with implementation whenever you're ready!
**Last Updated:** 2025-11-06 (End of design session)

View File

@@ -0,0 +1,218 @@
# Nested Entity Creation Feature
## Overview
The application now supports creating new related entities (vendors, models, machine types, functional accounts, business units) directly from the main entity forms without leaving the page.
## Implementation Date
October 13, 2025
## Files Modified/Created
### Device/PC Management
#### `/home/camp/projects/windows/shopdb/editdevice.asp`
- **Purpose**: Edit existing device/PC records
- **Added Features**:
- "+ New" button for Model dropdown with nested vendor creation
- Bootstrap 4 input-group structure with visual form sections
- jQuery handlers for showing/hiding nested forms
- Removed PC Type creation (pctype is a simple lookup table)
#### `/home/camp/projects/windows/shopdb/updatedevice_direct.asp`
- **Purpose**: Process device/PC updates with nested entity creation
- **Added Features**:
- Validation allowing "new" as valid value for model
- New model creation with vendor association
- New vendor creation with `ispc=1` flag
- Proper EOF checks and CLng() conversions to prevent Type_mismatch errors
- **Bug Fixes**:
- Fixed Type_mismatch error at line 31 (added EOF check before accessing recordset)
- Fixed Type_mismatch error at line 67 (restructured validation to avoid CLng on empty strings)
#### `/home/camp/projects/windows/shopdb/displaypc.asp`
- **Purpose**: Display PC details with embedded edit form
- **Added Features**:
- "+ New" buttons for Vendor and Model dropdowns
- Nested form sections for creating new vendors and models
- jQuery handlers with slideDown/slideUp animations
- Auto-sync: when vendor is selected, automatically populates model's vendor dropdown
- Changed form action from `editmacine.asp` to `updatepc_direct.asp`
- Changed button type from "button" to "submit" to enable form submission
- Added hidden pcid field for form processing
- **Corrected Filters**:
- Changed vendor filter from `ismachine=1` to `ispc=1`
- Changed model filter from `ismachine=1` to `ispc=1`
#### `/home/camp/projects/windows/shopdb/updatepc_direct.asp` (NEW)
- **Purpose**: Process PC updates from displaypc.asp with nested entity creation
- **Features**:
- Handles PC updates with vendor and model modifications
- New vendor creation with `ispc=1` flag
- New model creation with vendor association
- Proper validation and error handling
- Redirects back to displaypc.asp after successful update
### Machine Management
#### `/home/camp/projects/windows/shopdb/addmachine.asp`
- **Added Features**:
- "+ New" buttons for Model, Vendor, Machine Type, Functional Account, and Business Unit dropdowns
- PC association dropdown with scanner support
- Barcode scanner input for PC serial number with auto-selection
- Visual feedback (green border) when scanner matches a PC
#### `/home/camp/projects/windows/shopdb/savemachine_direct.asp`
- **Added Features**:
- Validation allowing "new" as valid value for all entity dropdowns
- Nested entity creation: Model → Vendor, Machine Type → Functional Account
- PC linkage: updates PC's `machinenumber` field when associated
- Proper SQL injection protection with Replace() for single quotes
#### `/home/camp/projects/windows/shopdb/displaymachine.asp`
- **Bug Fixes**:
- Removed problematic includes (validation.asp, error_handler.asp, db_helpers.asp)
- Replaced ExecuteParameterizedQuery() with objConn.Execute()
- Added NULL checks to all Server.HTMLEncode() calls to prevent Type_mismatch errors
- Fixed HTTP 500 errors preventing page load
#### `/home/camp/projects/windows/shopdb/editmacine.asp`
- **Added Features**:
- Similar nested entity creation as addmachine.asp
- Allows updating machines with new vendors, models, types, etc.
## Key Technical Patterns
### Frontend (Bootstrap 4 + jQuery)
```html
<!-- Dropdown with "+ New" button -->
<div class="input-group">
<select class="form-control" id="modelid" name="modelid">
<option value="new">+ Add New Model</option>
<!-- Existing options -->
</select>
<div class="input-group-append">
<button type="button" class="btn btn-info" id="addModelBtn">
<i class="zmdi zmdi-plus"></i> New
</button>
</div>
</div>
<!-- Hidden nested form section -->
<div id="newModelSection" style="display:none; ...">
<h6>New Model Details</h6>
<input type="text" name="newmodelnumber" />
<!-- Nested vendor dropdown -->
</div>
```
### jQuery Handlers
```javascript
// Dropdown change handler
$('#modelid').on('change', function() {
if ($(this).val() === 'new') {
$('#newModelSection').slideDown();
} else {
$('#newModelSection').slideUp();
}
});
// "+ New" button click handler
$('#addModelBtn').on('click', function() {
$('#modelid').val('new').trigger('change');
});
```
### Backend (VBScript/ASP)
```vbscript
' Validate - allow "new" as valid value
If modelid <> "" And modelid <> "new" Then
If Not IsNumeric(modelid) Or CLng(modelid) < 1 Then
Response.Redirect("error page")
End If
End If
' Handle new entity creation
If modelid = "new" Then
' Validate required fields
If Len(newmodelnumber) = 0 Then
Response.Redirect("error page")
End If
' Escape single quotes
Dim escapedModelNumber
escapedModelNumber = Replace(newmodelnumber, "'", "''")
' Insert new entity
Dim sqlNewModel
sqlNewModel = "INSERT INTO models (modelnumber, vendorid, isactive) VALUES ('" & escapedModelNumber & "', " & vendorid & ", 1)"
On Error Resume Next
objConn.Execute sqlNewModel
If Err.Number <> 0 Then
Response.Redirect("error page with message")
End If
' Get newly created ID
Dim rsNewModel
Set rsNewModel = objConn.Execute("SELECT LAST_INSERT_ID() AS newid")
modelid = CLng(rsNewModel("newid"))
rsNewModel.Close
Set rsNewModel = Nothing
On Error Goto 0
End If
```
## Database Flags
### Vendor Table Flags
- `ispc = 1`: Vendor supplies PC/computer equipment
- `isprinter = 1`: Vendor supplies printer equipment
- `ismachine = 1`: Vendor supplies machine/industrial equipment
### Entity Relationships
- **Machines**: Model → Vendor (with `ismachine=1`)
- **PCs**: Model → Vendor (with `ispc=1`)
- **Printers**: Model → Vendor (with `isprinter=1`)
- **Machine Types**: References Functional Account
- **PC Types**: Simple lookup table (no functional account relationship)
## Known Limitations
1. **PC Type Creation**: Disabled because `pctype` table doesn't have `functionalaccountid` column
2. **Form Validation**: Client-side validation is minimal; relies mostly on server-side validation
3. **Error Messages**: Generic error redirects; could be improved with more specific error messages
## Bug Fixes Applied
### Type_mismatch Errors
1. **updatedevice_direct.asp line 31**: Added `If Not rsCheck.EOF Then` before accessing recordset
2. **updatedevice_direct.asp line 67**: Split validation into nested If statements to avoid CLng() on empty strings
3. **displaymachine.asp line 77**: Added `If Not IsNull()` checks before all `Server.HTMLEncode()` calls
### Form Submission Issues
1. **displaypc.asp**: Changed form action from `editmacine.asp` to `updatepc_direct.asp`
2. **displaypc.asp**: Changed button type from "button" to "submit"
3. **displaypc.asp**: Added hidden `pcid` field for proper form processing
## Testing Recommendations
1. Test creating new vendors from device edit form
2. Test creating new models with new vendors (nested creation)
3. Test scanner functionality in machine creation form
4. Test validation with empty fields
5. Test SQL injection protection with single quotes in entity names
6. Test updating existing entities without creating new ones
7. Test error handling when database constraints are violated
## Future Enhancements
1. Add client-side validation for better UX
2. Add AJAX submission to avoid page reloads
3. Add confirmation dialogs before creating new entities
4. Add ability to edit newly created entities inline
5. Add autocomplete for entity names to prevent duplicates
6. Add bulk import functionality for vendors/models

View File

@@ -0,0 +1,740 @@
# Network Devices - Unified Page Design
**Date:** 2025-10-23
**Approach:** Single "Network Devices" page showing all infrastructure with filtering
**Files Required:** 4 files total
---
## Concept: One Page to Rule Them All
Instead of separate pages per device type, create a unified **Network Devices** page that shows:
- 🖥️ Servers
- 🔌 Switches
- 📹 Cameras
- 📡 Access Points (if you add them later)
- 📦 IDFs (Intermediate Distribution Frames)
**User Experience:**
- Click "Network Devices" → See ALL devices in one table
- Filter by type using tabs/dropdown
- Click any device → Detail page (works for all types)
- "Add Device" button → Select type, then add
---
## Page Architecture
### Main Pages (4 files)
```
network_devices.asp → List all devices with type filter
network_device_detail.asp?type=server&id=5 → View/edit any device
add_network_device.asp?type=server → Add new device (any type)
save_network_device.asp → Universal save endpoint
```
### Navigation
```
Main Menu:
└─ Network Devices (single menu item)
└─ Opens network_devices.asp with tabs for filtering
```
---
## File 1: network_devices.asp (Main List View)
### Features
- **Tabs/Filter:** All | Servers | Switches | Cameras | Access Points | IDFs
- **Unified Table:** Shows all device types in one view
- **Device Type Badge:** Visual indicator (Server, Switch, Camera, etc.)
- **Search:** Filter by vendor, model, IP, serial number
- **Actions:** View/Edit/Delete per device
### UI Mockup
```
┌─────────────────────────────────────────────────────────────┐
│ Network Devices [+ Add Device] │
├─────────────────────────────────────────────────────────────┤
│ [ All ] [ Servers ] [ Switches ] [ Cameras ] [ More ▼ ] │
├─────────────────────────────────────────────────────────────┤
│ Type | Vendor | Model | Serial | IP │
├─────────────────────────────────────────────────────────────┤
│ [Server] | Dell | PowerEdge | ABC123 | 10.0.1.5 │
│ [Switch] | Cisco | Catalyst 2960| XYZ789 | 10.0.1.1 │
│ [Camera] | Hikvision | DS-2CD2142FWD| CAM001 | 10.0.2.10 │
│ [Server] | HP | ProLiant | SRV456 | 10.0.1.6 │
└─────────────────────────────────────────────────────────────┘
```
### Code Structure
```vbscript
<%
' Get filter parameter (default = all)
Dim filterType
filterType = Request.QueryString("filter")
If filterType = "" Then filterType = "all"
' Build query using vw_network_devices view
Dim strSQL
If filterType = "all" Then
strSQL = "SELECT * FROM vw_network_devices WHERE isactive = 1 ORDER BY device_type, device_id DESC"
Else
' Filter by specific type (server, switch, camera)
strSQL = "SELECT * FROM vw_network_devices WHERE device_type = '" & filterType & "' AND isactive = 1 ORDER BY device_id DESC"
End If
Set rs = objConn.Execute(strSQL)
%>
<!-- Tabs -->
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link <%If filterType="all" Then Response.Write("active")%>"
href="network_devices.asp?filter=all">All Devices</a>
</li>
<li class="nav-item">
<a class="nav-link <%If filterType="Server" Then Response.Write("active")%>"
href="network_devices.asp?filter=Server">Servers</a>
</li>
<li class="nav-item">
<a class="nav-link <%If filterType="Switch" Then Response.Write("active")%>"
href="network_devices.asp?filter=Switch">Switches</a>
</li>
<li class="nav-item">
<a class="nav-link <%If filterType="Camera" Then Response.Write("active")%>"
href="network_devices.asp?filter=Camera">Cameras</a>
</li>
</ul>
<!-- Table -->
<table class="table table-hover">
<thead>
<tr>
<th>Type</th>
<th>Vendor</th>
<th>Model</th>
<th>Serial Number</th>
<th>IP Address</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% Do While Not rs.EOF %>
<tr>
<td>
<%
' Device type badge with icon
Dim badgeClass, iconClass
Select Case rs("device_type")
Case "Server"
badgeClass = "badge-primary"
iconClass = "zmdi-storage"
Case "Switch"
badgeClass = "badge-success"
iconClass = "zmdi-device-hub"
Case "Camera"
badgeClass = "badge-info"
iconClass = "zmdi-videocam"
End Select
%>
<span class="badge <%=badgeClass%>">
<i class="zmdi <%=iconClass%>"></i> <%=rs("device_type")%>
</span>
</td>
<td><%=Server.HTMLEncode(rs("vendor") & "")%></td>
<td><%=Server.HTMLEncode(rs("modelnumber") & "")%></td>
<td><%=Server.HTMLEncode(rs("serialnumber") & "")%></td>
<td><%=Server.HTMLEncode(rs("ipaddress") & "")%></td>
<td><%=Server.HTMLEncode(rs("description") & "")%></td>
<td>
<a href="network_device_detail.asp?type=<%=LCase(rs("device_type"))%>&id=<%=rs("device_id")%>">
<i class="zmdi zmdi-edit"></i> View
</a>
</td>
</tr>
<%
rs.MoveNext
Loop
%>
</tbody>
</table>
```
---
## File 2: network_device_detail.asp (Detail/Edit View)
### Features
- Shows device details with vendor/model
- Inline edit form (click Edit button)
- Works for ANY device type
- Map coordinates (if provided)
- Link back to network_devices.asp
### Code Structure
```vbscript
<%
' Get type and ID from URL
Dim deviceType, deviceId
deviceType = Request.QueryString("type") ' server, switch, camera
deviceId = Request.QueryString("id")
' Validate type
If deviceType <> "server" AND deviceType <> "switch" AND deviceType <> "camera" Then
Response.Redirect("network_devices.asp")
Response.End
End If
' Map type to table/field names
Dim tableName, idField, displayName
Select Case deviceType
Case "server"
tableName = "servers"
idField = "serverid"
displayName = "Server"
Case "switch"
tableName = "switches"
idField = "switchid"
displayName = "Switch"
Case "camera"
tableName = "cameras"
idField = "cameraid"
displayName = "Camera"
End Select
' Fetch device with model/vendor
strSQL = "SELECT d.*, m.modelnumber, m.modelnumberid, v.vendor, v.vendorid " & _
"FROM " & tableName & " d " & _
"LEFT JOIN models m ON d.modelid = m.modelnumberid " & _
"LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE d." & idField & " = " & deviceId
Set rs = objConn.Execute(strSQL)
If rs.EOF Then
Response.Write("Device not found")
Response.End
End If
%>
<div class="content-wrapper">
<a href="network_devices.asp" class="btn btn-secondary">
<i class="zmdi zmdi-arrow-left"></i> Back to Network Devices
</a>
<h2><%=displayName%> #<%=deviceId%></h2>
<!-- Display Mode -->
<div id="displayMode">
<table class="table table-bordered">
<tr>
<th>Vendor</th>
<td><%=Server.HTMLEncode(rs("vendor") & "N/A")%></td>
</tr>
<tr>
<th>Model</th>
<td><%=Server.HTMLEncode(rs("modelnumber") & "N/A")%></td>
</tr>
<tr>
<th>Serial Number</th>
<td><%=Server.HTMLEncode(rs("serialnumber") & "")%></td>
</tr>
<tr>
<th>IP Address</th>
<td><%=Server.HTMLEncode(rs("ipaddress") & "")%></td>
</tr>
<tr>
<th>Description</th>
<td><%=Server.HTMLEncode(rs("description") & "")%></td>
</tr>
<tr>
<th>Map Position</th>
<td>
<% If Not IsNull(rs("maptop")) And Not IsNull(rs("mapleft")) Then %>
Top: <%=rs("maptop")%>, Left: <%=rs("mapleft")%>
<a href="network_map.asp?highlight=<%=deviceType%>_<%=deviceId%>" target="_blank">
<i class="zmdi zmdi-map"></i> View on Map
</a>
<% Else %>
Not mapped
<% End If %>
</td>
</tr>
</table>
<button class="btn btn-primary" onclick="showEditMode()">
<i class="zmdi zmdi-edit"></i> Edit <%=displayName%>
</button>
</div>
<!-- Edit Mode (hidden by default) -->
<div id="editMode" style="display:none;">
<form method="post" action="save_network_device.asp">
<input type="hidden" name="type" value="<%=deviceType%>">
<input type="hidden" name="id" value="<%=deviceId%>">
<div class="form-group">
<label>Model</label>
<select name="modelid" class="form-control">
<option value="">-- No Model --</option>
<%
Dim strSQL2, rsModels
strSQL2 = "SELECT m.modelnumberid, m.modelnumber, v.vendor " & _
"FROM models m " & _
"INNER JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE m.isactive = 1 " & _
"ORDER BY v.vendor, m.modelnumber"
Set rsModels = objConn.Execute(strSQL2)
Do While Not rsModels.EOF
Dim selected
If Not IsNull(rs("modelnumberid")) Then
If rsModels("modelnumberid") = rs("modelnumberid") Then
selected = "selected"
Else
selected = ""
End If
End If
%>
<option value="<%=rsModels("modelnumberid")%>" <%=selected%>>
<%=Server.HTMLEncode(rsModels("vendor") & " - " & rsModels("modelnumber"))%>
</option>
<%
rsModels.MoveNext
Loop
rsModels.Close
Set rsModels = Nothing
%>
</select>
</div>
<div class="form-group">
<label>Serial Number</label>
<input type="text" name="serialnumber" class="form-control"
value="<%=Server.HTMLEncode(rs("serialnumber") & "")%>" maxlength="100">
</div>
<div class="form-group">
<label>IP Address</label>
<input type="text" name="ipaddress" class="form-control"
value="<%=Server.HTMLEncode(rs("ipaddress") & "")%>"
maxlength="15" pattern="^[0-9\.]+$">
</div>
<div class="form-group">
<label>Description</label>
<textarea name="description" class="form-control" rows="3"><%=Server.HTMLEncode(rs("description") & "")%></textarea>
</div>
<div class="form-group">
<label>Map Position (Optional)</label>
<div class="row">
<div class="col-md-6">
<input type="number" name="maptop" class="form-control"
placeholder="Top (Y)" value="<%=rs("maptop") & ""%>">
</div>
<div class="col-md-6">
<input type="number" name="mapleft" class="form-control"
placeholder="Left (X)" value="<%=rs("mapleft") & ""%>">
</div>
</div>
</div>
<button type="submit" class="btn btn-success">
<i class="zmdi zmdi-save"></i> Save Changes
</button>
<button type="button" class="btn btn-secondary" onclick="showDisplayMode()">
Cancel
</button>
</form>
</div>
</div>
<script>
function showEditMode() {
document.getElementById('displayMode').style.display = 'none';
document.getElementById('editMode').style.display = 'block';
}
function showDisplayMode() {
document.getElementById('displayMode').style.display = 'block';
document.getElementById('editMode').style.display = 'none';
}
</script>
```
---
## File 3: add_network_device.asp (Add Form)
### Features
- **First:** Select device type (Server, Switch, Camera, etc.)
- **Then:** Show form with fields
- Model/vendor dropdown
- All standard fields
- Optional map coordinates
### Code Structure
```vbscript
<%
' Get device type (from URL or form)
Dim deviceType
deviceType = Request.QueryString("type")
' If no type selected, show type selector
If deviceType = "" OR (deviceType <> "server" AND deviceType <> "switch" AND deviceType <> "camera") Then
%>
<div class="content-wrapper">
<h2>Add Network Device</h2>
<p>Select the type of device you want to add:</p>
<div class="row">
<div class="col-md-4">
<div class="card text-center">
<div class="card-body">
<i class="zmdi zmdi-storage" style="font-size:48px;"></i>
<h5>Server</h5>
<a href="add_network_device.asp?type=server" class="btn btn-primary">
Add Server
</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card text-center">
<div class="card-body">
<i class="zmdi zmdi-device-hub" style="font-size:48px;"></i>
<h5>Switch</h5>
<a href="add_network_device.asp?type=switch" class="btn btn-success">
Add Switch
</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card text-center">
<div class="card-body">
<i class="zmdi zmdi-videocam" style="font-size:48px;"></i>
<h5>Camera</h5>
<a href="add_network_device.asp?type=camera" class="btn btn-info">
Add Camera
</a>
</div>
</div>
</div>
</div>
<a href="network_devices.asp" class="btn btn-secondary">Cancel</a>
</div>
<%
Response.End
End If
' Type is selected, show form
Dim displayName
Select Case deviceType
Case "server": displayName = "Server"
Case "switch": displayName = "Switch"
Case "camera": displayName = "Camera"
End Select
%>
<div class="content-wrapper">
<h2>Add <%=displayName%></h2>
<form method="post" action="save_network_device.asp">
<input type="hidden" name="type" value="<%=deviceType%>">
<div class="form-group">
<label>Model <span class="text-danger">*</span></label>
<select name="modelid" required class="form-control">
<option value="">-- Select Model --</option>
<%
strSQL = "SELECT m.modelnumberid, m.modelnumber, v.vendor " & _
"FROM models m " & _
"INNER JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE m.isactive = 1 " & _
"ORDER BY v.vendor, m.modelnumber"
Set rsModels = objConn.Execute(strSQL)
Do While Not rsModels.EOF
%>
<option value="<%=rsModels("modelnumberid")%>">
<%=Server.HTMLEncode(rsModels("vendor") & " - " & rsModels("modelnumber"))%>
</option>
<%
rsModels.MoveNext
Loop
rsModels.Close
Set rsModels = Nothing
%>
</select>
<small class="form-text text-muted">
Don't see your model? <a href="addmodel.asp" target="_blank">Add a new model first</a>
</small>
</div>
<div class="form-group">
<label>Serial Number</label>
<input type="text" name="serialnumber" class="form-control" maxlength="100">
</div>
<div class="form-group">
<label>IP Address</label>
<input type="text" name="ipaddress" class="form-control"
maxlength="15" pattern="^[0-9\.]+$" placeholder="192.168.1.100">
</div>
<div class="form-group">
<label>Description</label>
<textarea name="description" class="form-control" rows="3"
placeholder="Location, purpose, notes..."></textarea>
</div>
<div class="form-group">
<label>Map Position (Optional)</label>
<div class="row">
<div class="col-md-6">
<input type="number" name="maptop" class="form-control"
placeholder="Top (Y coordinate)">
</div>
<div class="col-md-6">
<input type="number" name="mapleft" class="form-control"
placeholder="Left (X coordinate)">
</div>
</div>
<small class="form-text text-muted">
Used for network map visualization. Leave blank if unknown.
</small>
</div>
<button type="submit" class="btn btn-success">
<i class="zmdi zmdi-save"></i> Add <%=displayName%>
</button>
<a href="network_devices.asp" class="btn btn-secondary">Cancel</a>
</form>
</div>
```
---
## File 4: save_network_device.asp (Universal Save)
### Features
- Handles INSERT and UPDATE for all device types
- Validates all inputs
- Redirects back to appropriate page
### Code Structure
```vbscript
<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/error_handler.asp"-->
<!--#include file="./includes/validation.asp"-->
<!--#include file="./includes/db_helpers.asp"-->
<%
' Get device type
Dim deviceType
deviceType = Request.Form("type")
' Validate type
If deviceType <> "server" AND deviceType <> "switch" AND deviceType <> "camera" Then
Response.Write("Error: Invalid device type")
Response.End
End If
' Map to table/field names
Dim tableName, idField
Select Case deviceType
Case "server"
tableName = "servers"
idField = "serverid"
Case "switch"
tableName = "switches"
idField = "switchid"
Case "camera"
tableName = "cameras"
idField = "cameraid"
End Select
' Get and validate form data
Dim deviceId, modelid, serialnumber, ipaddress, description, maptop, mapleft
deviceId = GetSafeInteger("FORM", "id", 0, 0, 999999)
modelid = GetSafeInteger("FORM", "modelid", 0, 0, 999999)
serialnumber = GetSafeString("FORM", "serialnumber", "", 0, 100, "^[A-Za-z0-9\-\s]*$")
ipaddress = GetSafeString("FORM", "ipaddress", "", 0, 15, "^[0-9\.]*$")
description = GetSafeString("FORM", "description", "", 0, 255, "")
maptop = GetSafeInteger("FORM", "maptop", 0, 0, 999999)
mapleft = GetSafeInteger("FORM", "mapleft", 0, 0, 999999)
' Convert 0 to NULL for optional fields
If modelid = 0 Then modelid = Null
If maptop = 0 Then maptop = Null
If mapleft = 0 Then mapleft = Null
' Validate required fields
If IsNull(modelid) Then
Response.Write("Error: Model is required")
Response.End
End If
' Build query
Dim strSQL
If deviceId = 0 Then
' INSERT - New device
strSQL = "INSERT INTO " & tableName & " " & _
"(modelid, serialnumber, ipaddress, description, maptop, mapleft, isactive) " & _
"VALUES (?, ?, ?, ?, ?, ?, 1)"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, _
Array(modelid, serialnumber, ipaddress, description, maptop, mapleft))
' Get new ID for redirect
deviceId = objConn.Execute("SELECT LAST_INSERT_ID() as newid")(0)
Else
' UPDATE - Existing device
strSQL = "UPDATE " & tableName & " " & _
"SET modelid = ?, serialnumber = ?, ipaddress = ?, description = ?, " & _
" maptop = ?, mapleft = ? " & _
"WHERE " & idField & " = ?"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, _
Array(modelid, serialnumber, ipaddress, description, maptop, mapleft, deviceId))
End If
Call CleanupResources()
' Redirect to detail page
Response.Redirect("network_device_detail.asp?type=" & deviceType & "&id=" & deviceId)
%>
```
---
## Navigation Update
### leftsidebar.asp
```html
<!-- Network Section -->
<li class="nav-header">NETWORK</li>
<li>
<a href="network_devices.asp">
<i class="zmdi zmdi-devices"></i> Network Devices
</a>
</li>
<li>
<a href="network_map.asp">
<i class="zmdi zmdi-map"></i> Network Map
</a>
</li>
<li>
<a href="displaysubnets.asp">
<i class="zmdi zmdi-network"></i> Subnets
</a>
</li>
```
---
## Database View: vw_network_devices
The migration script already creates this! It unifies all infrastructure:
```sql
CREATE VIEW vw_network_devices AS
SELECT
'Server' AS device_type,
serverid AS device_id,
modelid, modelnumber, vendor,
serialnumber, ipaddress, description,
maptop, mapleft, isactive
FROM servers
LEFT JOIN models ON servers.modelid = models.modelnumberid
LEFT JOIN vendors ON models.vendorid = vendors.vendorid
UNION ALL
SELECT
'Switch' AS device_type,
switchid AS device_id,
modelid, modelnumber, vendor,
serialnumber, ipaddress, description,
maptop, mapleft, isactive
FROM switches
LEFT JOIN models ON switches.modelid = models.modelnumberid
LEFT JOIN vendors ON models.vendorid = vendors.vendorid
UNION ALL
SELECT
'Camera' AS device_type,
cameraid AS device_id,
modelid, modelnumber, vendor,
serialnumber, ipaddress, description,
maptop, mapleft, isactive
FROM cameras
LEFT JOIN models ON cameras.modelid = models.modelnumberid
LEFT JOIN vendors ON models.vendorid = vendors.vendorid
```
---
## Future: Adding More Device Types
To add **Access Points** or **IDFs** later:
1. **Database:**
```sql
CREATE TABLE accesspoints (
accesspointid INT(11) PRIMARY KEY AUTO_INCREMENT,
modelid INT(11),
serialnumber VARCHAR(100),
ipaddress VARCHAR(15),
description VARCHAR(255),
maptop INT(11),
mapleft INT(11),
isactive BIT(1) DEFAULT b'1',
FOREIGN KEY (modelid) REFERENCES models(modelnumberid)
);
-- Add to view
ALTER VIEW vw_network_devices AS
-- ... existing unions ...
UNION ALL
SELECT 'Access Point' AS device_type, accesspointid AS device_id, ...
FROM accesspoints ...
```
2. **Code:** Just add new case to Select statements!
```vbscript
Case "accesspoint"
tableName = "accesspoints"
idField = "accesspointid"
displayName = "Access Point"
```
3. **UI:** Add new tab to network_devices.asp
**That's it!** The unified design makes it trivial to extend.
---
## Summary: Why This Is Better
✅ **Single source of truth** - One page for all infrastructure
✅ **Easy filtering** - Tabs to view by type or see all
✅ **Consistent UX** - Same interface for all device types
✅ **Uses existing view** - `vw_network_devices` already unifies them
**Only 4 files** - vs 12 separate files
**Easy to extend** - Add new device types without file duplication
**Matches mental model** - "Network Devices" is how users think
**Search/filter across all** - Find any device in one place
---
**Ready to build?** This is the cleanest approach!

View File

@@ -0,0 +1,780 @@
# PC to Machines Consolidation - Complete Design Document
**Date Created**: 2025-11-06
**Status**: DESIGN PHASE
**Complexity**: ⚠️ **CRITICAL - MASSIVE REFACTORING**
**Production Deployment**: All SQL changes must be tracked
---
## Executive Summary
This document outlines the plan to consolidate the `pc` table (277 records) into the `machines` table (266 records) and implement a generic communications infrastructure.
**Goals:**
1. Eliminate the `pc` table by migrating all data into `machines`
2. Create a generic `communications` system supporting multiple communication types (IP, Serial, etc.)
3. Maintain all existing functionality through views and code updates
4. Enable future extensibility for new communication types
**Impact:**
- **Tables to modify**: 12+
- **Views to update**: 19
- **ASP files**: TBD (likely 50+)
- **Data records**: 543 machines total (266 current + 277 PCs)
---
## Part 1: Current State Analysis
### 1.1 PC Table Structure (277 records)
```sql
CREATE TABLE pc (
pcid INT(11) PRIMARY KEY AUTO_INCREMENT,
hostname VARCHAR(100),
serialnumber VARCHAR(100),
loggedinuser VARCHAR(100),
pctypeid INT(11), -- FK to pctype
machinenumber VARCHAR(50), -- Links to machines.machinenumber
lastupdated DATETIME,
dateadded DATETIME,
warrantyenddate DATE,
warrantystatus VARCHAR(50),
warrantydaysremaining INT(11),
warrantyservicelevel VARCHAR(100),
warrantylastchecked DATETIME,
modelnumberid INT(11), -- FK to models
isactive TINYINT(1),
requires_manual_machine_config TINYINT(1),
osid INT(11), -- FK to operatingsystems
pcstatusid INT(11) -- FK to pcstatus
);
```
### 1.2 Machines Table Structure (266 records)
```sql
CREATE TABLE machines (
machineid INT(11) PRIMARY KEY AUTO_INCREMENT,
machinetypeid INT(11) NOT NULL DEFAULT 1, -- FK to machinetypes
machinenumber TINYTEXT,
printerid INT(11),
alias TINYTEXT,
businessunitid INT(11),
modelnumberid INT(11), -- FK to models
isactive INT(11),
ipaddress1 CHAR(50), -- Will be deprecated
ipaddress2 CHAR(50), -- Will be deprecated
machinenotes TEXT,
mapleft SMALLINT(6),
maptop SMALLINT(6),
isvnc BIT(1),
islocationonly BIT(1)
);
```
### 1.3 Existing Communication Tables
#### pc_comm_config (Serial/IP Configuration)
```sql
CREATE TABLE pc_comm_config (
configid INT(11) PRIMARY KEY,
pcid INT(11) NOT NULL, -- FK to pc
configtype VARCHAR(50), -- 'Serial', 'IP', etc.
portid VARCHAR(20), -- COM port
baud INT(11),
databits INT(11),
stopbits VARCHAR(5),
parity VARCHAR(10),
crlf VARCHAR(5),
ipaddress VARCHAR(45),
socketnumber INT(11),
additionalsettings TEXT,
lastupdated DATETIME
);
```
#### pc_network_interfaces (Network Cards)
```sql
CREATE TABLE pc_network_interfaces (
interfaceid INT(11) PRIMARY KEY,
pcid INT(11) NOT NULL, -- FK to pc
interfacename VARCHAR(255),
ipaddress VARCHAR(45),
subnetmask VARCHAR(45),
defaultgateway VARCHAR(45),
macaddress VARCHAR(17),
isdhcp TINYINT(1),
isactive TINYINT(1),
ismachinenetwork TINYINT(1),
lastupdated DATETIME
);
```
#### pc_dnc_config (DNC Configuration)
```sql
CREATE TABLE pc_dnc_config (
dncid INT(11) PRIMARY KEY,
pcid INT(11) NOT NULL UNIQUE, -- FK to pc
site VARCHAR(100),
cnc VARCHAR(100),
ncif VARCHAR(50),
machinenumber VARCHAR(50),
hosttype VARCHAR(50),
ftphostprimary VARCHAR(100),
ftphostsecondary VARCHAR(100),
ftpaccount VARCHAR(100),
debug VARCHAR(10),
uploads VARCHAR(10),
scanner VARCHAR(10),
dripfeed VARCHAR(10),
additionalsettings TEXT,
lastupdated DATETIME,
dualpath_enabled TINYINT(1),
path1_name VARCHAR(255),
path2_name VARCHAR(255),
ge_registry_32bit TINYINT(1),
ge_registry_64bit TINYINT(1),
ge_registry_notes TEXT
);
```
### 1.4 Dependencies
#### Tables Referencing PC:
1. **machine_overrides** - Manual machine number overrides for PCs
2. **machine_pc_relationships** - Many-to-many relationships
3. **pc_comm_config** - Serial/IP communication settings
4. **pc_dnc_config** - DNC configuration
5. **pc_dualpath_assignments** - Secondary machine assignments
6. **pc_network_interfaces** - Network interface details
#### Views Using PC (19 views):
1. vw_active_pcs
2. vw_dnc_config
3. vw_dualpath_management
4. vw_engineer_pcs
5. vw_ge_machines
6. vw_machine_assignment_status
7. vw_machine_assignments
8. vw_machine_type_stats
9. vw_multi_pc_machines
10. vw_pc_network_summary
11. vw_pc_resolved_machines
12. vw_pc_summary
13. vw_pcs_by_hardware
14. vw_recent_updates
15. vw_shopfloor_applications_summary
16. vw_shopfloor_comm_config
17. vw_shopfloor_pcs
18. vw_standard_pcs
19. vw_vendor_summary
20. vw_warranties_expiring
---
## Part 2: New Design - Communications Infrastructure
### 2.1 New Table: comstypes (Communication Types)
```sql
CREATE TABLE comstypes (
comstypeid INT(11) PRIMARY KEY AUTO_INCREMENT,
typename VARCHAR(50) NOT NULL UNIQUE, -- 'IP', 'Serial', 'USB', 'Parallel', 'Network', etc.
description VARCHAR(255),
requires_port TINYINT(1) DEFAULT 0, -- Does this type need a port? (Serial, Parallel)
requires_ipaddress TINYINT(1) DEFAULT 0, -- Does this type need an IP? (IP, Network)
isactive TINYINT(1) DEFAULT 1,
displayorder INT(11) DEFAULT 0,
dateadded DATETIME DEFAULT CURRENT_TIMESTAMP
);
```
**Initial Data:**
```sql
INSERT INTO comstypes (typename, description, requires_port, requires_ipaddress, displayorder) VALUES
('IP', 'TCP/IP Network Communication', 0, 1, 1),
('Serial', 'Serial Port Communication (RS-232)', 1, 0, 2),
('Network_Interface', 'Network Interface Card', 0, 1, 3),
('USB', 'USB Connection', 1, 0, 4),
('Parallel', 'Parallel Port Connection', 1, 0, 5),
('VNC', 'Virtual Network Computing', 0, 1, 6),
('FTP', 'File Transfer Protocol', 0, 1, 7);
```
### 2.2 New Table: communications
```sql
CREATE TABLE communications (
comid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL, -- FK to machines.machineid
comstypeid INT(11) NOT NULL, -- FK to comstypes.comstypeid
-- Generic fields applicable to multiple types
ipaddress VARCHAR(45), -- For IP-based communications
port INT(11), -- Port number (for IP) or COM port number
portname VARCHAR(20), -- COM1, COM2, LPT1, etc.
macaddress VARCHAR(17), -- MAC address for network interfaces
-- Network-specific fields
subnetmask VARCHAR(45),
defaultgateway VARCHAR(45),
dnsserver VARCHAR(45),
isdhcp TINYINT(1) DEFAULT 0,
-- Serial-specific fields
baud INT(11), -- 9600, 115200, etc.
databits INT(11), -- 7, 8
stopbits VARCHAR(5), -- '1', '1.5', '2'
parity VARCHAR(10), -- 'None', 'Even', 'Odd', 'Mark', 'Space'
flowcontrol VARCHAR(20), -- 'None', 'Hardware', 'Software'
-- Protocol-specific fields
protocol VARCHAR(50), -- 'TCP', 'UDP', 'FTP', 'HTTP', etc.
username VARCHAR(100), -- For authenticated protocols
password VARCHAR(255), -- Encrypted password
-- General metadata
interfacename VARCHAR(255), -- Network adapter name
description VARCHAR(255),
isprimary TINYINT(1) DEFAULT 0, -- Is this the primary communication method?
isactive TINYINT(1) DEFAULT 1,
ismachinenetwork TINYINT(1) DEFAULT 0, -- Is this for machine network (vs office network)?
-- Additional settings as JSON/TEXT
additionalsettings TEXT, -- JSON for future extensibility
-- Audit fields
lastupdated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,
-- Indexes
KEY idx_machineid (machineid),
KEY idx_comstypeid (comstypeid),
KEY idx_ipaddress (ipaddress),
KEY idx_isactive (isactive),
KEY idx_isprimary (isprimary),
-- Foreign Keys
CONSTRAINT fk_communications_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid),
CONSTRAINT fk_communications_comstypeid FOREIGN KEY (comstypeid) REFERENCES comstypes(comstypeid)
);
```
### 2.3 Modified Machines Table
Add new fields to support PC data and communications:
**NOTE**: Warranty fields are NOT added to machines table. See WARRANTY_MANAGEMENT_DESIGN.md for the separate warranty infrastructure.
```sql
ALTER TABLE machines
-- PC-specific fields
ADD COLUMN hostname VARCHAR(100) AFTER machinenumber,
ADD COLUMN serialnumber VARCHAR(100) AFTER modelnumberid,
ADD COLUMN loggedinuser VARCHAR(100) AFTER hostname,
ADD COLUMN osid INT(11) AFTER modelnumberid,
ADD COLUMN pcstatusid INT(11) AFTER osid,
ADD COLUMN pctypeid INT(11) AFTER machinetypeid,
-- Configuration flags
ADD COLUMN requires_manual_machine_config TINYINT(1) DEFAULT 0,
-- Audit fields
ADD COLUMN lastupdated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
ADD COLUMN dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,
-- Foreign keys
ADD CONSTRAINT fk_machines_osid FOREIGN KEY (osid) REFERENCES operatingsystems(osid),
ADD CONSTRAINT fk_machines_pcstatusid FOREIGN KEY (pcstatusid) REFERENCES pcstatus(pcstatusid),
ADD CONSTRAINT fk_machines_pctypeid FOREIGN KEY (pctypeid) REFERENCES pctype(pctypeid);
-- Indexes
CREATE INDEX idx_machines_hostname ON machines(hostname);
CREATE INDEX idx_machines_serialnumber ON machines(serialnumber);
CREATE INDEX idx_machines_pctypeid ON machines(pctypeid);
CREATE INDEX idx_machines_osid ON machines(osid);
CREATE INDEX idx_machines_lastupdated ON machines(lastupdated);
```
---
## Part 3: Migration Strategy
### 3.1 Migration Phases
**Phase 1: Schema Preparation** (REVERSIBLE)
1. Create `comstypes` table
2. Create `communications` table
3. Add new columns to `machines` table
4. Create backup tables
**Phase 2: Data Migration** (REVERSIBLE with backup)
1. Migrate PC records to machines table
2. Migrate PC communication data to communications table
3. Migrate network interface data to communications table
4. Migrate DNC configuration to communications or new DNC table
5. Update relationship tables
**Phase 3: Reference Updates** (REQUIRES TESTING)
1. Create compatibility views (pc → machines)
2. Update dependent tables
3. Update 19 existing views
**Phase 4: Application Updates** (REQUIRES EXTENSIVE TESTING)
1. Update ASP files
2. Update stored procedures (if any)
3. Test all functionality
**Phase 5: Cleanup** (IRREVERSIBLE - DO AFTER 30 DAYS)
1. Drop old `pc` table
2. Drop compatibility views
3. Remove deprecated ipaddress1/ipaddress2 from machines
### 3.2 Machine Type for PCs
We need to add machine types for PCs:
```sql
INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
VALUES
('PC - Standard', 'Standard office/engineering workstation', (SELECT functionalaccountid FROM functionalaccounts WHERE functionalaccount = 'IT' LIMIT 1), 1),
('PC - Shopfloor', 'Shopfloor machine control PC', (SELECT functionalaccountid FROM functionalaccounts WHERE functionalaccount = 'MFG' LIMIT 1), 1),
('PC - Engineer', 'Engineering workstation', (SELECT functionalaccountid FROM functionalaccounts WHERE functionalaccount = 'ENG' LIMIT 1), 1);
```
### 3.3 Data Mapping: PC → Machines
| PC Field | Machines Field | Notes |
|----------|---------------|-------|
| pcid | machineid | New auto-increment ID |
| hostname | hostname | NEW COLUMN |
| serialnumber | serialnumber | NEW COLUMN |
| loggedinuser | loggedinuser | NEW COLUMN |
| pctypeid | pctypeid | NEW COLUMN, FK preserved |
| machinenumber | machinenumber | Existing field |
| lastupdated | lastupdated | NEW COLUMN |
| dateadded | dateadded | NEW COLUMN |
| modelnumberid | modelnumberid | Existing field |
| isactive | isactive | Existing field |
| requires_manual_machine_config | requires_manual_machine_config | NEW COLUMN |
| osid | osid | NEW COLUMN, FK preserved |
| pcstatusid | pcstatusid | NEW COLUMN, FK preserved |
| N/A | machinetypeid | Map from pctypeid to new machine types |
| N/A | alias | Set to hostname for PCs |
| N/A | businessunitid | Default to 1 or derive from location |
**Warranty Fields**: Migrated to `warranties` table (see WARRANTY_MANAGEMENT_DESIGN.md)
- warrantyenddate → warranties.enddate
- warrantystatus → warranties.status
- warrantyservicelevel → warranties.servicelevel
- warrantylastchecked → warranties.lastcheckeddate
### 3.4 Data Mapping: pc_network_interfaces → communications
| Old Field | New Field | New comstypeid |
|-----------|-----------|----------------|
| interfaceid | comid | Auto-increment |
| pcid | machineid | Lookup from migrated PCs |
| N/A | comstypeid | 3 (Network_Interface) |
| ipaddress | ipaddress | Direct copy |
| subnetmask | subnetmask | Direct copy |
| defaultgateway | defaultgateway | Direct copy |
| macaddress | macaddress | Direct copy |
| interfacename | interfacename | Direct copy |
| isdhcp | isdhcp | Direct copy |
| isactive | isactive | Direct copy |
| ismachinenetwork | ismachinenetwork | Direct copy |
### 3.5 Data Mapping: pc_comm_config → communications
| Old Field | New Field | New comstypeid |
|-----------|-----------|----------------|
| configid | comid | Auto-increment |
| pcid | machineid | Lookup from migrated PCs |
| configtype | comstypeid | Map 'Serial'→2, 'IP'→1, etc. |
| portid | portname | Direct copy (COM1, etc.) |
| baud | baud | Direct copy |
| databits | databits | Direct copy |
| stopbits | stopbits | Direct copy |
| parity | parity | Direct copy |
| ipaddress | ipaddress | Direct copy |
| socketnumber | port | Direct copy |
| additionalsettings | additionalsettings | Direct copy |
---
## Part 4: SQL Migration Scripts
### 4.1 Script 01: Schema Creation
File: `sql/01_create_communications_infrastructure.sql`
```sql
-- =====================================================
-- SCRIPT 01: Create Communications Infrastructure
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Create comstypes and communications tables
-- Status: REVERSIBLE (has rollback script)
-- =====================================================
USE shopdb;
SET SQL_SAFE_UPDATES = 0;
-- Create comstypes table
CREATE TABLE IF NOT EXISTS comstypes (
comstypeid INT(11) PRIMARY KEY AUTO_INCREMENT,
typename VARCHAR(50) NOT NULL UNIQUE,
description VARCHAR(255),
requires_port TINYINT(1) DEFAULT 0,
requires_ipaddress TINYINT(1) DEFAULT 0,
isactive TINYINT(1) DEFAULT 1,
displayorder INT(11) DEFAULT 0,
dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,
KEY idx_isactive (isactive),
KEY idx_displayorder (displayorder)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Insert communication types
INSERT INTO comstypes (typename, description, requires_port, requires_ipaddress, displayorder) VALUES
('IP', 'TCP/IP Network Communication', 0, 1, 1),
('Serial', 'Serial Port Communication (RS-232)', 1, 0, 2),
('Network_Interface', 'Network Interface Card', 0, 1, 3),
('USB', 'USB Connection', 1, 0, 4),
('Parallel', 'Parallel Port Connection', 1, 0, 5),
('VNC', 'Virtual Network Computing', 0, 1, 6),
('FTP', 'File Transfer Protocol', 0, 1, 7);
-- Create communications table
CREATE TABLE IF NOT EXISTS communications (
comid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL,
comstypeid INT(11) NOT NULL,
-- Generic fields
ipaddress VARCHAR(45),
port INT(11),
portname VARCHAR(20),
macaddress VARCHAR(17),
-- Network-specific
subnetmask VARCHAR(45),
defaultgateway VARCHAR(45),
dnsserver VARCHAR(45),
isdhcp TINYINT(1) DEFAULT 0,
-- Serial-specific
baud INT(11),
databits INT(11),
stopbits VARCHAR(5),
parity VARCHAR(10),
flowcontrol VARCHAR(20),
-- Protocol-specific
protocol VARCHAR(50),
username VARCHAR(100),
password VARCHAR(255),
-- General metadata
interfacename VARCHAR(255),
description VARCHAR(255),
isprimary TINYINT(1) DEFAULT 0,
isactive TINYINT(1) DEFAULT 1,
ismachinenetwork TINYINT(1) DEFAULT 0,
-- Additional settings
additionalsettings TEXT,
-- Audit fields
lastupdated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,
-- Indexes
KEY idx_machineid (machineid),
KEY idx_comstypeid (comstypeid),
KEY idx_ipaddress (ipaddress),
KEY idx_isactive (isactive),
KEY idx_isprimary (isprimary),
-- Foreign Keys
CONSTRAINT fk_communications_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid),
CONSTRAINT fk_communications_comstypeid FOREIGN KEY (comstypeid) REFERENCES comstypes(comstypeid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Verification
SELECT 'comstypes table created' AS status, COUNT(*) AS type_count FROM comstypes;
SELECT 'communications table created' AS status;
SET SQL_SAFE_UPDATES = 1;
```
### 4.2 Script 02: Extend Machines Table
File: `sql/02_extend_machines_table.sql`
**NOTE**: Warranty fields removed - see script 04 for warranty infrastructure
```sql
-- =====================================================
-- SCRIPT 02: Extend Machines Table for PC Data
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Add PC-related columns to machines table
-- Status: REVERSIBLE (has rollback script)
-- NOTE: Warranty data goes to separate warranties table (script 04)
-- =====================================================
USE shopdb;
SET SQL_SAFE_UPDATES = 0;
-- Add PC-specific fields
ALTER TABLE machines
ADD COLUMN IF NOT EXISTS hostname VARCHAR(100) AFTER machinenumber,
ADD COLUMN IF NOT EXISTS serialnumber VARCHAR(100) AFTER modelnumberid,
ADD COLUMN IF NOT EXISTS loggedinuser VARCHAR(100) AFTER hostname,
ADD COLUMN IF NOT EXISTS osid INT(11) AFTER modelnumberid,
ADD COLUMN IF NOT EXISTS pcstatusid INT(11) AFTER osid,
ADD COLUMN IF NOT EXISTS pctypeid INT(11) AFTER machinetypeid;
-- Add configuration flags
ALTER TABLE machines
ADD COLUMN IF NOT EXISTS requires_manual_machine_config TINYINT(1) DEFAULT 0 AFTER islocationonly;
-- Add audit fields
ALTER TABLE machines
ADD COLUMN IF NOT EXISTS lastupdated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER requires_manual_machine_config,
ADD COLUMN IF NOT EXISTS dateadded DATETIME DEFAULT CURRENT_TIMESTAMP AFTER lastupdated;
-- Add foreign keys
ALTER TABLE machines
ADD CONSTRAINT fk_machines_osid FOREIGN KEY (osid) REFERENCES operatingsystems(osid);
ALTER TABLE machines
ADD CONSTRAINT fk_machines_pcstatusid FOREIGN KEY (pcstatusid) REFERENCES pcstatus(pcstatusid);
ALTER TABLE machines
ADD CONSTRAINT fk_machines_pctypeid FOREIGN KEY (pctypeid) REFERENCES pctype(pctypeid);
-- Create indexes
CREATE INDEX IF NOT EXISTS idx_machines_hostname ON machines(hostname);
CREATE INDEX IF NOT EXISTS idx_machines_serialnumber ON machines(serialnumber);
CREATE INDEX IF NOT EXISTS idx_machines_pctypeid ON machines(pctypeid);
CREATE INDEX IF NOT EXISTS idx_machines_osid ON machines(osid);
CREATE INDEX IF NOT EXISTS idx_machines_lastupdated ON machines(lastupdated);
-- Verification
DESCRIBE machines;
SET SQL_SAFE_UPDATES = 1;
```
### 4.3 Script 03: Create PC Machine Types
File: `sql/03_create_pc_machine_types.sql`
```sql
-- =====================================================
-- SCRIPT 03: Create Machine Types for PCs
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Add PC-related machine types
-- Status: REVERSIBLE
-- =====================================================
USE shopdb;
SET SQL_SAFE_UPDATES = 0;
-- Get or create functional accounts
INSERT IGNORE INTO functionalaccounts (functionalaccount, description, isactive)
VALUES
('IT', 'Information Technology', 1),
('MFG', 'Manufacturing', 1),
('ENG', 'Engineering', 1);
-- Create PC machine types
INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Standard', 'Standard office/engineering workstation', functionalaccountid, 1
FROM functionalaccounts WHERE functionalaccount = 'IT' LIMIT 1
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Standard');
INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Shopfloor', 'Shopfloor machine control PC', functionalaccountid, 1
FROM functionalaccounts WHERE functionalaccount = 'MFG' LIMIT 1
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Shopfloor');
INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Engineer', 'Engineering workstation', functionalaccountid, 1
FROM functionalaccounts WHERE functionalaccount = 'ENG' LIMIT 1
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Engineer');
-- Verification
SELECT * FROM machinetypes WHERE machinetype LIKE 'PC -%';
SET SQL_SAFE_UPDATES = 1;
```
---
## Part 5: Rollback Scripts
### 5.1 Rollback Script 01
File: `sql/ROLLBACK_01_communications_infrastructure.sql`
```sql
-- =====================================================
-- ROLLBACK 01: Remove Communications Infrastructure
-- =====================================================
USE shopdb;
SET SQL_SAFE_UPDATES = 0;
-- Drop tables in correct order (FK constraints)
DROP TABLE IF EXISTS communications;
DROP TABLE IF EXISTS comstypes;
SELECT 'Communications infrastructure removed' AS status;
SET SQL_SAFE_UPDATES = 1;
```
### 5.2 Rollback Script 02
File: `sql/ROLLBACK_02_machines_table_extensions.sql`
```sql
-- =====================================================
-- ROLLBACK 02: Remove Machines Table Extensions
-- =====================================================
USE shopdb;
SET SQL_SAFE_UPDATES = 0;
-- Drop foreign keys first
ALTER TABLE machines DROP FOREIGN KEY IF EXISTS fk_machines_osid;
ALTER TABLE machines DROP FOREIGN KEY IF EXISTS fk_machines_pcstatusid;
ALTER TABLE machines DROP FOREIGN KEY IF EXISTS fk_machines_pctypeid;
-- Drop indexes
DROP INDEX IF EXISTS idx_machines_hostname ON machines;
DROP INDEX IF EXISTS idx_machines_serialnumber ON machines;
DROP INDEX IF EXISTS idx_machines_pctypeid ON machines;
DROP INDEX IF EXISTS idx_machines_osid ON machines;
DROP INDEX IF EXISTS idx_machines_lastupdated ON machines;
-- Remove columns (MySQL 5.6 doesn't support IF EXISTS for columns)
ALTER TABLE machines
DROP COLUMN hostname,
DROP COLUMN serialnumber,
DROP COLUMN loggedinuser,
DROP COLUMN osid,
DROP COLUMN pcstatusid,
DROP COLUMN pctypeid,
DROP COLUMN requires_manual_machine_config,
DROP COLUMN lastupdated,
DROP COLUMN dateadded;
SELECT 'Machines table extensions removed' AS status;
SET SQL_SAFE_UPDATES = 1;
```
---
## Part 6: Next Steps & Considerations
### 6.1 Critical Decisions Needed
1. **DNC Configuration**:
- Keep pc_dnc_config as-is but reference machines instead of pc?
- Or migrate to communications table with comstypeid='DNC'?
- **Recommendation**: Keep separate, too complex for generic table
2. **Machine Overrides**:
- Keep machine_overrides table?
- **Recommendation**: Yes, but update FK to machines
3. **Warranty Management**:
- Extend to all machines or keep PC-specific?
- **Recommendation**: Available for all machines
4. **Machine Numbers**:
- PCs currently link to machines via machinenumber (string match)
- After migration, use direct machineid FK
- **Recommendation**: Add explicit FK relationships
### 6.2 Testing Requirements
1. **Data Integrity**:
- Verify all 277 PCs migrated
- Verify all communication records migrated
- Verify all FK relationships intact
2. **View Testing**:
- Test all 19 views return same data
- Performance testing for complex views
3. **Application Testing**:
- Test every ASP file that touches pc table
- Test all CRUD operations
- Test warranty tracking
- Test DNC configuration
### 6.3 Production Deployment Checklist
- [ ] Create full database backup
- [ ] Test migration on dev environment
- [ ] Create compatibility views for old queries
- [ ] Update all ASP files
- [ ] Schedule maintenance window (4-6 hours recommended)
- [ ] Prepare rollback procedure
- [ ] Document all changes
- [ ] Train users on any UI changes
---
## Part 7: Estimated Timeline
| Phase | Duration | Dependencies |
|-------|----------|--------------|
| Schema Design | 1 day | None |
| Create Migration Scripts | 2-3 days | Schema approval |
| Data Migration Testing | 2-3 days | Scripts complete |
| View Updates | 1-2 days | Data migration tested |
| ASP File Analysis | 2-3 days | View updates tested |
| ASP File Updates | 5-7 days | File analysis complete |
| Integration Testing | 3-5 days | All updates complete |
| User Acceptance Testing | 2-3 days | Integration testing passed |
| Production Deployment | 1 day | UAT passed |
| **Total** | **20-30 days** | |
---
## Appendix A: Risk Assessment
| Risk | Severity | Mitigation |
|------|----------|------------|
| Data loss during migration | CRITICAL | Full backups, test on dev first, rollback scripts |
| Application downtime | HIGH | Deploy during maintenance window, parallel cutover |
| Performance degradation | MEDIUM | Index optimization, query testing |
| View compatibility issues | HIGH | Create compatibility views, thorough testing |
| User training needed | LOW | Minimal UI changes expected |
---
## Appendix B: File Impact Analysis
*To be completed after code review*
Files to analyze:
- All files with "pc" in filename
- All files referencing pc table
- All files using pc views
---
**Document Status**: DRAFT
**Next Review Date**: TBD
**Approval Required From**: System Administrator, Lead Developer

View File

@@ -0,0 +1,489 @@
# Phase 3: Network Devices Migration to machines Table
**Date:** 2025-11-10
**Status:** 📋 PLANNING
**Follows:** Phase 2 (PC Migration - Completed)
---
## Executive Summary
Consolidate all network infrastructure devices (servers, switches, cameras, access points, IDFs) into the unified `machines` table. This completes the infrastructure unification started in Phase 2 with PCs, creating a single source of truth for all physical assets except printers.
---
## Scope
### ✅ Migrate INTO machines Table:
- **Servers** → machinetypeid 30
- **Switches** → machinetypeid 31
- **Cameras** → machinetypeid 32
- **Access Points** → machinetypeid 33
- **IDFs** → machinetypeid 34
- **Routers** → machinetypeid 35 (if exists)
- **Firewalls** → machinetypeid 36 (if exists)
### ❌ Keep SEPARATE:
- **Printers** → Stay in `printers` table (unique fields, workflows, APIs)
---
## Why This Migration?
### Current Problems:
1. **Data Fragmentation:** Network devices scattered across 5+ tables
2. **Code Duplication:** Separate pages for each device type
3. **Relationship Limitations:** Can't relate servers to switches to cameras
4. **Query Complexity:** UNION queries to find all devices on a network
5. **Inconsistent UI/UX:** Different interfaces for similar devices
### After Migration:
1.**Single Source of Truth:** All infrastructure in `machines` table
2.**Unified Relationships:** Camera → Switch → IDF using `machinerelationships`
3.**Unified Communications:** All IPs in `communications` table
4.**Consistent UI:** One set of pages for all devices
5.**Powerful Queries:** Cross-device reports and topology mapping
6.**Better Compliance:** All devices tracked in one place
---
## Architecture
### Current Structure:
```
machines table
├── Equipment (machinetypeid 1-24, pctypeid IS NULL)
└── PCs (machinetypeid 25-29, pctypeid IS NOT NULL)
servers table (separate)
switches table (separate)
cameras table (separate)
accesspoints table (separate)
idfs table (separate)
printers table (stays separate)
```
### Target Structure:
```
machines table
├── Equipment (machinetypeid 1-24, pctypeid IS NULL)
├── PCs (machinetypeid 25-29, pctypeid IS NOT NULL)
└── Network Devices (machinetypeid 30-36, pctypeid IS NULL) ← NEW
printers table (stays separate - by design)
```
---
## New Machine Types (30-36)
```sql
INSERT INTO machinetypes (machinetypeid, machinetype, category, description, displayorder) VALUES
(30, 'Server', 'Network', 'Physical or virtual server', 30),
(31, 'Switch', 'Network', 'Network switch', 31),
(32, 'Camera', 'Network', 'IP camera or security camera', 32),
(33, 'Access Point', 'Network', 'Wireless access point', 33),
(34, 'IDF', 'Network', 'Intermediate Distribution Frame', 34),
(35, 'Router', 'Network', 'Network router', 35),
(36, 'Firewall', 'Network', 'Network firewall', 36);
```
---
## Filtering Strategy
```sql
-- All PCs
SELECT * FROM machines WHERE pctypeid IS NOT NULL;
-- All Equipment (CNC machines, mills, lathes, etc.)
SELECT * FROM machines WHERE pctypeid IS NULL AND machinetypeid BETWEEN 1 AND 24;
-- All Network Devices
SELECT * FROM machines WHERE pctypeid IS NULL AND machinetypeid BETWEEN 30 AND 36;
-- Specific device types
SELECT * FROM machines WHERE machinetypeid = 30; -- Servers
SELECT * FROM machines WHERE machinetypeid = 31; -- Switches
SELECT * FROM machines WHERE machinetypeid = 32; -- Cameras
-- All non-printer infrastructure
SELECT * FROM machines; -- Everything except printers
```
---
## Data Migration Mapping
### Servers Table → Machines
```
servers.serverid → machines.machineid (new)
servers.modelid → machines.modelnumberid
servers.serialnumber → machines.serialnumber
servers.description → machines.alias
servers.maptop → machines.maptop
servers.mapleft → machines.mapleft
servers.isactive → machines.isactive
servers.ipaddress → communications.address (comstypeid=1)
30 (constant) → machines.machinetypeid
NULL → machines.pctypeid
'SERVER-{serverid}' → machines.machinenumber (generated)
```
### Switches Table → Machines
```
switches.switchid → machines.machineid (new)
switches.modelid → machines.modelnumberid
switches.serialnumber → machines.serialnumber
switches.description → machines.alias
switches.maptop → machines.maptop
switches.mapleft → machines.mapleft
switches.isactive → machines.isactive
switches.ipaddress → communications.address (comstypeid=1)
31 (constant) → machines.machinetypeid
NULL → machines.pctypeid
'SWITCH-{switchid}' → machines.machinenumber (generated)
```
### Cameras Table → Machines
```
cameras.cameraid → machines.machineid (new)
cameras.modelid → machines.modelnumberid
cameras.serialnumber → machines.serialnumber
cameras.description → machines.alias
cameras.maptop → machines.maptop
cameras.mapleft → machines.mapleft
cameras.isactive → machines.isactive
cameras.ipaddress → communications.address (comstypeid=1)
32 (constant) → machines.machinetypeid
NULL → machines.pctypeid
'CAMERA-{cameraid}' → machines.machinenumber (generated)
```
### Access Points Table → Machines (if exists)
```
accesspoints.accesspointid → machines.machineid (new)
accesspoints.modelid → machines.modelnumberid
accesspoints.serialnumber → machines.serialnumber
accesspoints.description → machines.alias
accesspoints.maptop → machines.maptop
accesspoints.mapleft → machines.mapleft
accesspoints.isactive → machines.isactive
accesspoints.ipaddress → communications.address (comstypeid=1)
33 (constant) → machines.machinetypeid
NULL → machines.pctypeid
'AP-{accesspointid}' → machines.machinenumber (generated)
```
### IDFs Table → Machines (if exists)
```
idfs.idfid → machines.machineid (new)
idfs.name → machines.machinenumber
idfs.description → machines.alias
idfs.maptop → machines.maptop
idfs.mapleft → machines.mapleft
idfs.isactive → machines.isactive
34 (constant) → machines.machinetypeid
NULL → machines.pctypeid
```
---
## New Relationship Types
```sql
INSERT INTO relationshiptypes (relationshiptype, description, isbidirectional) VALUES
-- Existing relationships
('Dualpath', 'Machines sharing the same controller', 1),
('Controlled By', 'Machine controlled by PC', 0),
('Controls', 'PC controls this machine', 0),
-- New network relationships
('Connected To', 'Device physically connected to infrastructure', 0),
('Powered By', 'Device powered by this power source', 0),
('Mounted In', 'Device mounted in rack/cabinet', 0),
('Feeds Video To', 'Camera feeds video to this server/NVR', 0),
('Provides Network', 'Infrastructure provides network to device', 0);
```
---
## Migration Scripts Structure
```
/home/camp/projects/windows/shopdb/sql/migration_phase3/
├── 01_create_network_machinetypes.sql
├── 02_migrate_servers_to_machines.sql
├── 03_migrate_switches_to_machines.sql
├── 04_migrate_cameras_to_machines.sql
├── 05_migrate_accesspoints_to_machines.sql
├── 06_migrate_idfs_to_machines.sql
├── 07_migrate_network_communications.sql
├── 08_create_network_relationships.sql
├── 09_update_views_for_network_devices.sql
├── 10_update_vendor_flags.sql
├── VERIFY_PHASE3_MIGRATION.sql
├── RUN_ALL_PHASE3_SCRIPTS.sql
└── ROLLBACK_PHASE3.sql
```
---
## Migration Order
### Pre-Migration:
1.**Backup database**
2.**Verify Phase 2 is stable**
3.**Document current table structures**
4.**Count records in each table**
### Migration Steps:
1. **Create new machinetypes** (30-36)
2. **Migrate servers** → machines + communications
3. **Migrate switches** → machines + communications
4. **Migrate cameras** → machines + communications
5. **Migrate access points** → machines + communications (if exists)
6. **Migrate IDFs** → machines (if exists)
7. **Create network relationships** (camera → switch → IDF)
8. **Update vendor flags** (isserver, isswitch, etc.)
9. **Update views** (create vw_network_devices if needed)
10. **Verify data integrity**
### Post-Migration:
1. **Run verification queries**
2. **Test UI pages**
3. **Update code references**
4. **Keep old tables temporarily** (for rollback safety)
5. **Monitor for issues**
6. **Drop old tables after 30 days** (if stable)
---
## Code Updates Required
### Pages to Update:
1.**displaymachines.asp** - Add filter tabs for network devices
2.**displaymachine.asp** - Already supports all types via machinetypeid
3.**machine_edit.asp** - Already supports all types
4.**adddevice.asp** - Update to include network device types
### Pages to Deprecate:
1.**displayservers.asp** → Redirect to displaymachines.asp?type=server
2.**displayswitches.asp** → Redirect to displaymachines.asp?type=switch
3.**displaycameras.asp** → Redirect to displaymachines.asp?type=camera
4.**network_devices.asp** → Redirect to displaymachines.asp?category=network
### Views to Update:
```sql
-- Update or create
CREATE OR REPLACE VIEW vw_all_infrastructure AS
SELECT
m.machineid,
m.machinenumber,
m.alias,
mt.machinetype,
mt.category,
mo.modelnumber,
v.vendor,
c.address AS ipaddress,
CASE
WHEN m.pctypeid IS NOT NULL THEN 'PC'
WHEN mt.category = 'Network' THEN 'Network Device'
ELSE 'Equipment'
END AS device_category
FROM machines m
LEFT JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.isprimary = 1
WHERE m.isactive = 1;
```
---
## Verification Queries
### Record Counts
```sql
-- Before migration
SELECT 'servers' AS table_name, COUNT(*) AS count FROM servers
UNION ALL
SELECT 'switches', COUNT(*) FROM switches
UNION ALL
SELECT 'cameras', COUNT(*) FROM cameras
UNION ALL
SELECT 'accesspoints', COUNT(*) FROM accesspoints
UNION ALL
SELECT 'idfs', COUNT(*) FROM idfs;
-- After migration
SELECT
mt.machinetype,
COUNT(*) AS count
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE mt.machinetypeid BETWEEN 30 AND 36
GROUP BY mt.machinetype;
```
### Data Integrity
```sql
-- Verify all network devices have machine types
SELECT COUNT(*) FROM machines
WHERE machinetypeid BETWEEN 30 AND 36
AND machinetypeid IS NULL;
-- Should be 0
-- Verify all network devices have pctypeid = NULL
SELECT COUNT(*) FROM machines
WHERE machinetypeid BETWEEN 30 AND 36
AND pctypeid IS NOT NULL;
-- Should be 0
-- Verify IP addresses migrated
SELECT
mt.machinetype,
COUNT(DISTINCT m.machineid) AS total_devices,
COUNT(DISTINCT c.machineid) AS devices_with_ip
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.comstypeid = 1
WHERE mt.machinetypeid BETWEEN 30 AND 36
GROUP BY mt.machinetype;
```
---
## Rollback Plan
### If Issues Found:
1. **Stop immediately**
2. **Run ROLLBACK_PHASE3.sql**
3. **Verify old tables intact**
4. **Restore from backup if needed**
5. **Review errors**
6. **Fix and retry**
### Rollback Script (High-Level):
```sql
-- Delete migrated network devices from machines
DELETE FROM machines WHERE machinetypeid BETWEEN 30 AND 36;
-- Delete migrated communications
DELETE FROM communications WHERE machineid NOT IN (SELECT machineid FROM machines);
-- Delete new relationship types
DELETE FROM relationshiptypes WHERE relationshiptypeid > 5;
-- Delete new machinetypes
DELETE FROM machinetypes WHERE machinetypeid BETWEEN 30 AND 36;
-- Verify old tables still have data
SELECT COUNT(*) FROM servers;
SELECT COUNT(*) FROM switches;
SELECT COUNT(*) FROM cameras;
```
---
## Success Criteria
### ✅ Migration Successful If:
1. All records migrated (counts match)
2. All IP addresses migrated to communications
3. All relationships preserved
4. No NULL values in required fields
5. UI pages display correctly
6. Queries perform well
7. No data loss
8. Rollback tested and works
---
## Timeline Estimate
- **Planning & Script Creation:** 2-3 hours
- **Testing on Dev/Backup:** 1-2 hours
- **Production Migration:** 30-45 minutes
- **Verification:** 1 hour
- **Code Updates:** 3-4 hours
- **Testing & Bug Fixes:** 2-3 hours
**Total:** ~10-15 hours of work
---
## Risk Assessment
### Low Risk:
- ✅ Pattern proven with Phase 2 PC migration
- ✅ Can be rolled back easily
- ✅ Old tables kept temporarily
- ✅ Comprehensive verification
### Medium Risk:
- ⚠️ Multiple tables being migrated
- ⚠️ Code references to update
- ⚠️ Testing required for all device types
### Mitigation:
-**Test on backup database first**
-**Migrate one device type at a time**
-**Verify after each migration**
-**Keep old tables for 30 days**
-**Update code incrementally**
---
## Benefits After Completion
### Immediate:
1. ✅ Single query for all infrastructure
2. ✅ Unified relationship management
3. ✅ Camera → IDF relationships work
4. ✅ Consistent UI across all devices
5. ✅ Better network topology visibility
### Long-Term:
1. ✅ Easier to add new device types
2. ✅ Less code duplication
3. ✅ Better reporting capabilities
4. ✅ Simplified maintenance
5. ✅ CMDB-style asset management
6. ✅ Better compliance tracking
---
## Next Steps
1.**Create migration SQL scripts**
2.**Create verification scripts**
3.**Create rollback scripts**
4.**Test on backup database**
5.**Review and approve plan**
6.**Schedule migration window**
7.**Execute migration**
8.**Update code**
9.**Monitor and verify**
---
**Status:** Ready for script creation
**Approval Required:** Yes
**Backup Required:** Yes
**Estimated Duration:** 30-45 minutes (migration only)
---
## Questions to Answer Before Migration
1. ✅ Do accesspoints and idfs tables exist?
2. ✅ Are there any custom fields in device tables we need to preserve?
3. ✅ Are there any foreign key constraints to old tables?
4. ✅ What's the maintenance window schedule?
5. ✅ Should we create camera → IDF relationships during migration or manually after?
---
**Ready to proceed with script creation!**

View File

@@ -0,0 +1,593 @@
# Printer Mapping Migration Report
**Date:** 2025-10-22
**Author:** Development Team
**Status:** Analysis Complete - Ready for Implementation
---
## Executive Summary
The `printers` table now has `maptop` and `mapleft` columns added for direct printer location mapping on the shop floor map. This migration report outlines the necessary code changes to transition from machine-based printer positioning to direct printer positioning.
### Database Changes Completed ✅
- Added `maptop INT(11)` column to `printers` table
- Added `mapleft INT(11)` column to `printers` table
- Both columns are nullable (default NULL)
- Positioned after `machineid` column
---
## Current Implementation Analysis
### 1. **printermap.asp** - Main Map View
**Current Behavior:**
- Queries printers joined with machines to get map coordinates
- Uses `machines.maptop` and `machines.mapleft` for printer positioning
- Shows printer at machine location
- Requires `printers.machineid != 1` (excludes unassigned printers)
**SQL Query (Lines 186-189):**
```sql
SELECT machines.mapleft, machines.maptop, machines.machinenumber,
printers.printerid, printers.printercsfname, printers.printerwindowsname,
models.modelnumber, models.image, printers.ipaddress, printers.fqdn,
machines.machinenotes, machines.alias
FROM machines, printers, models
WHERE printers.modelid = models.modelnumberid
AND printers.machineid != 1
AND printers.machineid = machines.machineid
AND printers.isactive = 1
```
**Location Display (Lines 202-207):**
```vbscript
' Uses alias if available, otherwise machinenumber
if NOT IsNull(rs("alias")) AND rs("alias") <> "" THEN
location = rs("alias")
else
location = rs("machinenumber")
end if
```
**Issues:**
- ❌ Printers without machine assignment (`machineid=1`) are excluded from map
- ❌ Multiple printers at same machine appear stacked on same coordinate
- ❌ Cannot position printer independently of machine
---
### 2. **addprinter.asp** - Add New Printer Form
**Current Behavior:**
- Form includes machine dropdown (required field)
- Uses machineid to determine printer location
- No map coordinate input fields
**Location Field (Lines 174-197):**
```vbscript
<div class="form-group">
<label for="machineid">Location (Machine) <span class="text-danger">*</span></label>
<select class="form-control" id="machineid" name="machineid" required>
<option value="">-- Select Location --</option>
<!-- Lists all machines -->
</select>
<small class="form-text text-muted">Which machine/location is this printer at?</small>
</div>
```
**Issues:**
- ❌ No way to set `maptop`/`mapleft` during printer creation
- ❌ Printer position tied to machine selection
- ❌ Cannot add printer without machine assignment
---
### 3. **saveprinter_direct.asp** - Save New Printer
**Current Behavior:**
- Inserts printer with machineid
- Does not handle maptop/mapleft
**INSERT Statement (Line 191-192):**
```vbscript
strSQL = "INSERT INTO printers (modelid, serialnumber, ipaddress, fqdn,
printercsfname, printerwindowsname, machineid, isactive) " & _
"VALUES (...)"
```
**Issues:**
- ❌ Does not insert `maptop`/`mapleft` values
- ❌ New printers won't have coordinates
---
### 4. **editprinter.asp** - Edit Printer Form
**Current Behavior:**
- Similar to addprinter.asp
- Shows machine dropdown
- No map coordinate fields
**Issues:**
- ❌ Cannot edit printer coordinates
- ❌ No map picker interface
---
### 5. **saveprinter.asp** - Update Printer
**Current Behavior:**
- Updates printer fields including machineid
- Does not update maptop/mapleft
**UPDATE Statement (Lines 168-176):**
```vbscript
strSQL = "UPDATE printers SET " & _
"modelid = " & modelid & ", " & _
"serialnumber = '" & serialnumber & "', " & _
"ipaddress = '" & ipaddress & "', " & _
"fqdn = '" & fqdn & "', " & _
"printercsfname = '" & printercsfname & "', " & _
"printerwindowsname = '" & printerwindowsname & "', " & _
"machineid = " & machineid & " " & _
"WHERE printerid = " & printerid
```
**Issues:**
- ❌ Does not update `maptop`/`mapleft`
---
### 6. **displayprinter.asp** - View Printer Details
**Current Behavior:**
- Shows printer details
- Displays location as machine number/alias
- Has clickable location link
**Location Display (Lines 87-91):**
```vbscript
<p class="mb-2">
<span class="location-link" data-machineid="<%Response.Write(machineid)%>"
style="cursor:pointer; color:#007bff;">
<i class="zmdi zmdi-pin" style="margin-right:4px;"></i>
<%Response.Write(rs("machinenumber"))%>
</span>
</p>
```
**Issues:**
- ❌ Still references machine location
- ❌ No display of printer's actual map coordinates
---
## Required Code Changes
### Priority 1: Core Map Functionality
#### 1. **printermap.asp** - Update Query to Use Printer Coordinates
**Change SQL Query (Lines 186-189):**
```vbscript
<%
' OLD (commented out):
' strSQL = "SELECT machines.mapleft, machines.maptop, machines.machinenumber, ... FROM machines, printers ..."
' NEW - Use printer coordinates, fallback to machine if not set
strSQL = "SELECT " &_
"COALESCE(printers.mapleft, machines.mapleft) AS mapleft, " &_
"COALESCE(printers.maptop, machines.maptop) AS maptop, " &_
"machines.machinenumber, machines.alias, " &_
"printers.printerid, printers.printercsfname, printers.printerwindowsname, " &_
"models.modelnumber, models.image, printers.ipaddress, printers.fqdn, " &_
"printers.maptop AS printer_maptop, printers.mapleft AS printer_mapleft " &_
"FROM printers " &_
"INNER JOIN models ON printers.modelid = models.modelnumberid " &_
"LEFT JOIN machines ON printers.machineid = machines.machineid " &_
"WHERE printers.isactive = 1 " &_
" AND (printers.maptop IS NOT NULL OR machines.maptop IS NOT NULL)"
set rs = objconn.Execute(strSQL)
while not rs.eof
mapleft = rs("mapleft")
maptop = rs("maptop")
maptop = 2550 - maptop ' Coordinate transformation
' ... rest of code
%>
```
**Benefits:**
- ✅ Uses printer coordinates if available
- ✅ Falls back to machine coordinates if printer coordinates not set
- ✅ Includes printers without machine assignment (if they have coordinates)
- ✅ Backward compatible during migration
---
#### 2. **addprinter.asp** & **editprinter.asp** - Add Map Picker
**Add New Form Fields (after line 197 in addprinter.asp):**
```html
<div class="form-group">
<label>Map Position</label>
<div class="row">
<div class="col-md-6">
<label for="maptop">Map Top</label>
<input type="number" class="form-control" id="maptop" name="maptop"
placeholder="Y coordinate (0-2550)" min="0" max="2550">
</div>
<div class="col-md-6">
<label for="mapleft">Map Left</label>
<input type="number" class="form-control" id="mapleft" name="mapleft"
placeholder="X coordinate (0-3300)" min="0" max="3300">
</div>
</div>
<small class="form-text text-muted">
Leave blank to use machine location.
<a href="./printermap.asp" target="_blank" style="color:#007bff;">
<i class="zmdi zmdi-map"></i> Open map in new tab
</a> to find coordinates.
</small>
</div>
<!-- Optional: Add map picker button -->
<div class="form-group">
<button type="button" class="btn btn-sm btn-info" id="openMapPicker">
<i class="zmdi zmdi-map"></i> Pick Location on Map
</button>
</div>
```
**Add JavaScript for Map Picker Modal (before closing `</body>`):**
```javascript
<script>
// Map picker functionality
$('#openMapPicker').on('click', function() {
// Option 1: Simple - open map in new window
window.open('./printermap.asp', 'mapPicker', 'width=1200,height=800');
// Option 2: Advanced - inline modal with Leaflet (future enhancement)
// Show modal with embedded map, allow clicking to select coordinates
});
// If machine is selected, optionally pre-fill coordinates from machine
$('#machineid').on('change', function() {
var machineid = $(this).val();
if (machineid && machineid != '1') {
// AJAX call to get machine coordinates
$.get('./api_machine_coordinates.asp?machineid=' + machineid, function(data) {
if (data.maptop && data.mapleft) {
// Optionally suggest coordinates
if (confirm('Use machine location coordinates?\nTop: ' + data.maptop + ', Left: ' + data.mapleft)) {
$('#maptop').val(data.maptop);
$('#mapleft').val(data.mapleft);
}
}
});
}
});
</script>
```
---
#### 3. **saveprinter_direct.asp** - Handle Map Coordinates on Insert
**Add Input Collection (after line 18):**
```vbscript
Dim maptop, mapleft
maptop = Trim(Request.Form("maptop"))
mapleft = Trim(Request.Form("mapleft"))
' Validate coordinates if provided
If maptop <> "" And Not IsNumeric(maptop) Then
Response.Write("<div class='alert alert-danger'>Error: Invalid map top coordinate.</div>")
Response.Write("<a href='addprinter.asp'>Go back</a>")
objConn.Close
Response.End
End If
If mapleft <> "" And Not IsNumeric(mapleft) Then
Response.Write("<div class='alert alert-danger'>Error: Invalid map left coordinate.</div>")
Response.Write("<a href='addprinter.asp'>Go back</a>")
objConn.Close
Response.End
End If
' Convert to integers or NULL
Dim maptopSQL, mapleftSQL
If maptop <> "" And IsNumeric(maptop) Then
maptopSQL = CLng(maptop)
Else
maptopSQL = "NULL"
End If
If mapleft <> "" And IsNumeric(mapleft) Then
mapleftSQL = CLng(mapleft)
Else
mapleftSQL = "NULL"
End If
```
**Update INSERT Statement (line 191):**
```vbscript
strSQL = "INSERT INTO printers (modelid, serialnumber, ipaddress, fqdn, " &_
"printercsfname, printerwindowsname, machineid, maptop, mapleft, isactive) " &_
"VALUES (" & modelid & ", '" & serialnumber & "', '" & ipaddress & "', " &_
"'" & fqdn & "', '" & printercsfname & "', '" & printerwindowsname & "', " &_
machineid & ", " & maptopSQL & ", " & mapleftSQL & ", 1)"
```
---
#### 4. **saveprinter.asp** - Handle Map Coordinates on Update
**Add Same Input Collection Code as saveprinter_direct.asp**
**Update UPDATE Statement (line 168):**
```vbscript
strSQL = "UPDATE printers SET " &_
"modelid = " & modelid & ", " &_
"serialnumber = '" & serialnumber & "', " &_
"ipaddress = '" & ipaddress & "', " &_
"fqdn = '" & fqdn & "', " &_
"printercsfname = '" & printercsfname & "', " &_
"printerwindowsname = '" & printerwindowsname & "', " &_
"machineid = " & machineid & ", " &_
"maptop = " & maptopSQL & ", " &_
"mapleft = " & mapleftSQL & " " &_
"WHERE printerid = " & printerid
```
---
### Priority 2: Enhanced Features
#### 5. **displayprinter.asp** - Show Map Coordinates
**Add to Settings Tab (after line 81):**
```html
<p class="mb-2"><strong>Map Position:</strong></p>
```
**Add to Values Column (after line 93):**
```vbscript
<p class="mb-2">
<%
If NOT IsNull(rs("maptop")) AND NOT IsNull(rs("mapleft")) Then
Response.Write("Top: " & rs("maptop") & ", Left: " & rs("mapleft"))
Response.Write(" <a href='./printermap.asp' target='_blank' style='color:#007bff;' title='View on Map'>")
Response.Write("<i class='zmdi zmdi-map'></i></a>")
ElseIf NOT IsNull(rs("machines.maptop")) Then
Response.Write("Using machine location")
Else
Response.Write("<span style='color:#999;'>Not set</span>")
End If
%>
</p>
```
---
#### 6. Create Helper API: **api_machine_coordinates.asp**
**New File:**
```vbscript
<%@ Language="VBScript" %>
<%
Response.ContentType = "application/json"
Response.Charset = "UTF-8"
<!--#include file="./includes/sql.asp"-->
Dim machineid
machineid = Request.QueryString("machineid")
If NOT IsNumeric(machineid) Then
Response.Write("{""error"":""Invalid machine ID""}")
objConn.Close
Response.End
End If
Dim strSQL, rs
strSQL = "SELECT maptop, mapleft FROM machines WHERE machineid = " & CLng(machineid)
Set rs = objConn.Execute(strSQL)
If NOT rs.EOF Then
Response.Write("{")
Response.Write("""maptop"":" & rs("maptop") & ",")
Response.Write("""mapleft"":" & rs("mapleft"))
Response.Write("}")
Else
Response.Write("{""error"":""Machine not found""}")
End If
rs.Close
Set rs = Nothing
objConn.Close
%>
```
---
### Priority 3: Data Migration
#### 7. Create Migration Script for Existing Printers
**New File: sql/migrate_printer_coordinates.sql**
```sql
-- ============================================================================
-- Migrate Printer Coordinates from Machine Locations
-- ============================================================================
-- This copies machine coordinates to printers that don't have their own coordinates
-- Run this ONCE after adding maptop/mapleft columns to printers
-- Update printers to inherit machine coordinates where not already set
UPDATE printers p
INNER JOIN machines m ON p.machineid = m.machineid
SET
p.maptop = m.maptop,
p.mapleft = m.mapleft
WHERE
p.maptop IS NULL
AND p.mapleft IS NULL
AND m.maptop IS NOT NULL
AND m.mapleft IS NOT NULL
AND p.isactive = 1;
-- Report: Show printers with coordinates
SELECT
'Printers with own coordinates' AS status,
COUNT(*) AS count
FROM printers
WHERE maptop IS NOT NULL AND mapleft IS NOT NULL AND isactive = 1
UNION ALL
SELECT
'Printers without coordinates' AS status,
COUNT(*) AS count
FROM printers
WHERE (maptop IS NULL OR mapleft IS NULL) AND isactive = 1;
-- List printers still missing coordinates
SELECT
p.printerid,
p.printerwindowsname,
p.ipaddress,
m.machinenumber,
p.machineid
FROM printers p
LEFT JOIN machines m ON p.machineid = m.machineid
WHERE (p.maptop IS NULL OR p.mapleft IS NULL)
AND p.isactive = 1
ORDER BY p.printerid;
```
---
## Implementation Plan
### Phase 1: Core Changes (Day 1)
1. ✅ Add maptop/mapleft to printers table (COMPLETE)
2. ⬜ Update printermap.asp query
3. ⬜ Update saveprinter_direct.asp INSERT
4. ⬜ Update saveprinter.asp UPDATE
5. ⬜ Run data migration SQL script
### Phase 2: Form Updates (Day 2)
1. ⬜ Add coordinate fields to addprinter.asp
2. ⬜ Add coordinate fields to editprinter.asp
3. ⬜ Test printer creation with coordinates
4. ⬜ Test printer editing with coordinates
### Phase 3: Enhanced Features (Day 3)
1. ⬜ Add map picker button functionality
2. ⬜ Create api_machine_coordinates.asp
3. ⬜ Update displayprinter.asp to show coordinates
4. ⬜ Test full workflow
### Phase 4: Testing & Documentation (Day 4)
1. ⬜ Test with various printer scenarios
2. ⬜ Update user documentation
3. ⬜ Train users on new feature
4. ⬜ Monitor for issues
---
## Testing Checklist
### Backward Compatibility
- ⬜ Existing printers without coordinates still appear on map (using machine location)
- ⬜ Machine dropdown still functions
- ⬜ Printers assigned to machineid=1 can now have coordinates
### New Functionality
- ⬜ Can add printer with custom coordinates
- ⬜ Can edit printer coordinates
- ⬜ Can leave coordinates blank (uses machine location)
- ⬜ Multiple printers at same machine can have different positions
- ⬜ Printers without machine assignment can appear on map
### Edge Cases
- ⬜ Printer with machineid=1 and no coordinates (should not appear on map)
- ⬜ Printer with coordinates but machineid=1 (should appear on map)
- ⬜ Invalid coordinate values (should be rejected)
- ⬜ Null/empty coordinate values (should use machine location)
---
## Benefits of This Approach
1. **Backward Compatible**: Existing printers continue to work using machine locations
2. **Flexible**: Printers can be positioned independently of machines
3. **Gradual Migration**: Can update printer positions over time
4. **No Data Loss**: Machine associations are preserved
5. **Better Accuracy**: Printers can show actual physical location
---
## Future Enhancements
### Interactive Map Picker
Create a modal with embedded Leaflet map where users can:
- Click to select printer location
- See existing printers and machines
- Drag printer icon to new position
- Visual grid/snap-to-grid option
### Bulk Update Tool
Create admin page to:
- List all printers with/without coordinates
- Bulk copy coordinates from machines
- Bulk adjust coordinates (offset all by X/Y)
- Import coordinates from CSV
### Map Filtering
Add printermap.asp filters for:
- Show only printers with custom coordinates
- Show only printers using machine locations
- Highlight printers without any location
- Filter by printer model/vendor
---
## Questions for Stakeholders
1. Should we automatically copy machine coordinates to all existing printers? (Recommended: YES)
2. Should machineid still be required? (Recommended: Make optional, but keep for reference)
3. Do we need coordinate validation beyond 0-2550/0-3300 ranges?
4. Should we add a "sync with machine" button to copy machine coords to printer?
5. Priority level for interactive map picker vs manual coordinate entry?
---
## Files to Modify Summary
| File | Priority | Changes Required |
|------|----------|------------------|
| printermap.asp | P1 | Update SQL query to use printer coordinates |
| saveprinter_direct.asp | P1 | Add maptop/mapleft to INSERT |
| saveprinter.asp | P1 | Add maptop/mapleft to UPDATE |
| addprinter.asp | P2 | Add coordinate input fields |
| editprinter.asp | P2 | Add coordinate input fields |
| displayprinter.asp | P2 | Show coordinates in settings |
| api_machine_coordinates.asp | P3 | New file - coordinate lookup API |
| sql/migrate_printer_coordinates.sql | P1 | New file - data migration |
---
**End of Report**

501
docs/QUICK_REFERENCE.md Normal file
View File

@@ -0,0 +1,501 @@
# ShopDB Quick Reference Guide
**For:** New team members and quick lookups
**See Also:** DEEP_DIVE_REPORT.md (comprehensive), ASP_DEVELOPMENT_GUIDE.md (development), STANDARDS.md (coding standards)
---
## Quick Access URLs
- **Production:** http://your-production-server/
- **Beta/Staging:** http://your-production-server/v2/
- **Dev Environment:** http://192.168.122.151:8080
---
## Database Quick Facts
| Item | Count | Notes |
|------|-------|-------|
| **Tables** | 29 | Base tables (actual data) |
| **Views** | 23 | Computed/joined data |
| **PCs** | 242 | Active PCs in inventory |
| **Machines** | 256 | CNC machines and locations |
| **Printers** | 40 | Network printers |
| **Applications** | 44 | Shopfloor software |
| **KB Articles** | 196 | Troubleshooting docs |
| **Network IFs** | 705 | Network interfaces tracked |
| **Total Size** | ~3.5 MB | Small but mighty! |
---
## Core Tables Cheat Sheet
### PC Management
```sql
-- Main PC table
pc (pcid, hostname, serialnumber, pctypeid, machinenumber, modelnumberid, osid)
-- PC Types
pctype (Standard, Engineer, Shopfloor, Server, Laptop, VM)
-- PC Status
pcstatus (In Use, Spare, Retired, Broken, Unknown)
-- Network
pc_network_interfaces (pcid, ipaddress, subnetmask, macaddress, isdhcp)
-- Communication
pc_comm_config (pcid, configtype, portid, baud, databits, ipaddress)
-- DNC
pc_dnc_config (pcid, site, cnc, ncif, dualpath_enabled, path1_name, path2_name)
```
### Machine Management
```sql
-- Machines
machines (machineid, machinenumber, alias, machinetypeid, printerid, ipaddress1/2)
-- Machine Types
machinetypes (Vertical Lathe, Horizontal Lathe, 5-Axis Mill, CMM, Part Washer, etc.)
-- Installed Apps
installedapps (appid, machineid) -- Junction table
```
### Applications & KB
```sql
-- Applications
applications (appid, appname, appdescription, supportteamid, isinstallable)
-- Knowledge Base
knowledgebase (linkid, shortdescription, keywords, appid, linkurl, clicks)
```
### Infrastructure
```sql
-- Printers
printers (printerid, printercsfname, modelid, serialnumber, ipaddress, fqdn, machineid)
-- Subnets
subnets (subnetid, ipaddress, subnet, vlan, gateway, subnettypeid)
-- Notifications
notifications (notificationid, notification, starttime, endtime, isactive)
```
### Reference Data
```sql
models (modelnumberid, modelnumber, vendorid)
vendors (vendorid, vendor)
operatingsystems (osid, operatingsystem)
supportteams (supportteamid, supportteam)
```
---
## File Structure Map
```
shopdb/
├── *.asp # Main pages (91 files)
│ ├── default.asp # Dashboard
│ ├── search.asp # Unified search
│ ├── calendar.asp # Notification calendar
│ ├── display*.asp # View pages
│ ├── add*.asp # Create forms
│ ├── edit*.asp # Update forms
│ ├── save*.asp # Backend processors
│ └── api_*.asp # JSON APIs
├── includes/ # Shared code
│ ├── sql.asp # DB connection
│ ├── header.asp # HTML head
│ ├── leftsidebar.asp # Navigation
│ ├── topbarheader.asp # Top bar
│ ├── error_handler.asp # Error handling
│ ├── validation.asp # Input validation
│ ├── db_helpers.asp # DB utilities
│ └── data_cache.asp # Caching system
├── assets/ # Frontend resources
│ ├── css/ # Stylesheets
│ ├── js/ # JavaScript
│ ├── images/ # Icons, logos
│ └── plugins/ # Third-party libs
├── images/ # Dashboard images
│ └── 1-9.jpg # Rotating images
├── sql/ # Database scripts
│ └── database_updates_for_production.sql
└── docs/ # Documentation
├── DEEP_DIVE_REPORT.md # Comprehensive guide
├── ASP_DEVELOPMENT_GUIDE.md # Dev setup
├── STANDARDS.md # Coding standards
├── NESTED_ENTITY_CREATION.md # Complex forms
└── QUICK_REFERENCE.md # This file
```
---
## Common Tasks
### Start Development Environment
```bash
cd ~/projects/windows/shopdb
~/start-dev-env.sh # Starts Docker + Windows VM
# Wait ~30 seconds for IIS to start
curl http://192.168.122.151:8080 # Test
```
### Database Access
```bash
# Connect to MySQL
docker exec -it dev-mysql mysql -u 570005354 -p570005354 shopdb
# Backup database
docker exec dev-mysql mysqldump -u 570005354 -p570005354 shopdb > backup.sql
# Restore database
docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < backup.sql
# Check table counts
docker exec dev-mysql mysql -u 570005354 -p570005354 shopdb \
-e "SELECT table_name, table_rows FROM information_schema.tables WHERE table_schema='shopdb' ORDER BY table_rows DESC;"
```
### Code Development
```bash
# Edit files (auto-syncs to Windows via Samba)
code ~/projects/windows/shopdb/
# Check syntax (if you have a validator)
# ASP doesn't have great linters, test by loading in browser
# View logs (Windows VM)
# C:\inetpub\logs\LogFiles\
```
### Testing Changes
1. Save file on Linux (auto-syncs to Z:\shopdb\ on Windows)
2. Refresh browser (http://192.168.122.151:8080/yourfile.asp)
3. Check browser console for JS errors
4. Check IIS Express console for ASP errors
5. Check database for data changes
---
## Search System Quick Guide
### Search Syntax
- **Exact match:** `"exact phrase"` (not yet implemented)
- **Multiple words:** `word1 word2` (finds both)
- **Short words:** < 4 characters use LIKE fallback automatically
### What's Searchable?
- **Applications:** Name
- **Knowledge Base:** Title, keywords, application name
- **Notifications:** Notification text
- **Machines:** Number, alias, type, vendor, notes
- **Printers:** CSF name, model, serial number
### Smart Redirects
- **Printer serial (exact):** → Printer detail page
- **Printer FQDN (exact):** → Printer detail page
- **Machine number (exact):** → Machine detail page
---
## Key VBScript Patterns
### Include Required Files
```vbscript
<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/error_handler.asp"-->
<!--#include file="./includes/validation.asp"-->
<!--#include file="./includes/db_helpers.asp"-->
```
### Safe Database Query
```vbscript
<%
' Get and validate input
Dim machineId
machineId = GetSafeInteger("QS", "machineid", 0, 1, 999999)
If machineId = 0 Then
Response.Redirect("error.asp?code=INVALID_ID")
Response.End
End If
' Parameterized query
strSQL = "SELECT * FROM machines WHERE machineid = ? AND isactive = 1"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(machineId))
' Use results
If Not rs.EOF Then
Response.Write Server.HTMLEncode(rs("machinenumber"))
End If
' Cleanup
rs.Close
Set rs = Nothing
Call CleanupResources()
%>
```
### Display a List
```vbscript
<%
strSQL = "SELECT machineid, machinenumber, alias FROM machines WHERE isactive=1 ORDER BY machinenumber"
Set rs = objConn.Execute(strSQL)
Do While Not rs.EOF
%>
<tr>
<td><%=Server.HTMLEncode(rs("machinenumber"))%></td>
<td><%=Server.HTMLEncode(rs("alias"))%></td>
<td><a href="displaymachine.asp?machineid=<%=rs("machineid")%>">View</a></td>
</tr>
<%
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
%>
```
### Form Handling
```vbscript
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
' Validate input
Dim machineName
machineName = GetSafeString("FORM", "machinename", "", 1, 50, "^[A-Za-z0-9\s\-]+$")
If machineName = "" Then
Call HandleValidationError("addmachine.asp", "REQUIRED_FIELD")
End If
' Insert into database
strSQL = "INSERT INTO machines (machinenumber) VALUES (?)"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(machineName))
Call CleanupResources()
Response.Redirect("displaymachines.asp")
Response.End
End If
%>
<!-- Show form -->
<form method="post">
<input type="text" name="machinename" required>
<button type="submit">Save</button>
</form>
```
---
## Important Views to Know
### PC-Related Views
- `vw_shopfloor_pcs` - Shopfloor PCs with machine assignments
- `vw_active_pcs` - PCs updated in last 30 days
- `vw_pc_summary` - Overall PC inventory
- `vw_pc_network_summary` - Network configuration overview
- `vw_warranty_status` - Warranty tracking
- `vw_warranties_expiring` - Expiring in next 90 days
### Machine-Related Views
- `vw_machine_assignments` - PC-to-machine relationships
- `vw_machine_type_stats` - Counts by machine type
- `vw_multi_pc_machines` - Machines with multiple PCs
- `vw_unmapped_machines` - Missing map coordinates
- `vw_dualpath_management` - DualPath CNCs
### Reporting Views
- `vw_vendor_summary` - PC counts by manufacturer
- `vw_pcs_by_hardware` - Hardware distribution
- `vw_pctype_config` - Configuration by PC type
- `vw_recent_updates` - Recent changes
---
## Database Credentials
**Development Database:**
- Host: 192.168.122.1 (from Windows VM)
- Port: 3306
- Database: shopdb
- User: 570005354
- Password: 570005354
**Production Database:**
- See production server documentation (credentials secured)
---
## Troubleshooting
### "Page Cannot Be Displayed"
1. Check IIS Express is running (Windows Task Manager)
2. Check Windows VM is running: `virsh list --all`
3. Check network: `ping 192.168.122.151`
4. Restart: `~/stop-dev-env.sh && ~/start-dev-env.sh`
### "Database Connection Failed"
1. Check MySQL container: `docker ps | grep mysql`
2. Check credentials in sql.asp
3. Test connection: `docker exec -it dev-mysql mysql -u 570005354 -p570005354 shopdb`
4. Check firewall: MySQL port 3306 must be open
### "ODBC Driver Not Found" (Windows)
1. Install MySQL ODBC 8.0 Driver on Windows VM
2. Verify in Control Panel → ODBC Data Sources
3. Restart IIS Express
### "Changes Not Appearing"
1. Hard refresh: Ctrl+F5
2. Check file actually saved: `ls -la ~/projects/windows/shopdb/filename.asp`
3. Check Samba: `sudo systemctl status smbd`
4. Check Windows can see Z: drive
### "SQL Injection Error"
1. You're using unsafe query patterns!
2. Use `ExecuteParameterizedQuery()` from db_helpers.asp
3. Review STANDARDS.md for correct patterns
---
## Security Checklist
Before deploying code, verify:
- [ ] All SQL queries use parameterization
- [ ] All user input validated (validation.asp)
- [ ] All output encoded (Server.HTMLEncode)
- [ ] Error messages don't expose internals
- [ ] No hard-coded credentials
- [ ] Resources cleaned up (Call CleanupResources())
- [ ] Tested on dev environment first
- [ ] Peer reviewed (if possible)
---
## Useful SQL Queries
### Find PCs by Machine Number
```sql
SELECT p.hostname, p.serialnumber, p.machinenumber, pt.typename
FROM pc p
JOIN pctype pt ON p.pctypeid = pt.pctypeid
WHERE p.machinenumber = '3104'
AND p.isactive = 1;
```
### Machines Without Assigned PCs
```sql
SELECT m.machinenumber, m.alias, mt.machinetype
FROM machines m
LEFT JOIN pc p ON p.machinenumber = m.machinenumber AND p.isactive = 1
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE p.pcid IS NULL
AND m.isactive = 1
AND m.islocationonly = 0;
```
### Most Clicked KB Articles
```sql
SELECT k.shortdescription, a.appname, k.clicks, k.linkurl
FROM knowledgebase k
JOIN applications a ON k.appid = a.appid
WHERE k.isactive = 1
ORDER BY k.clicks DESC
LIMIT 20;
```
### Warranties Expiring This Month
```sql
SELECT hostname, serialnumber, warrantyenddate, warrantydaysremaining
FROM vw_warranties_expiring
WHERE warrantyenddate BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 30 DAY)
ORDER BY warrantyenddate;
```
### DualPath PCs
```sql
SELECT p.hostname, d.primary_machine, d.secondary_machine, dnc.dualpath_enabled
FROM pc p
JOIN pc_dualpath_assignments d ON p.pcid = d.pcid
JOIN pc_dnc_config dnc ON p.pcid = dnc.pcid
WHERE dnc.dualpath_enabled = 1;
```
---
## Resources
### Documentation
- **Comprehensive Guide:** docs/DEEP_DIVE_REPORT.md
- **Development Setup:** docs/ASP_DEVELOPMENT_GUIDE.md
- **Coding Standards:** docs/STANDARDS.md
- **Complex Forms:** docs/NESTED_ENTITY_CREATION.md
### External Links
- **Classic ASP Reference:** https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525334(v=vs.90)
- **VBScript Reference:** https://learn.microsoft.com/en-us/previous-versions//d1wf56tt(v=vs.85)
- **MySQL 5.6 Docs:** https://dev.mysql.com/doc/refman/5.6/en/
- **Bootstrap 4 Docs:** https://getbootstrap.com/docs/4.6/getting-started/introduction/
### Tools
- **Database Management:** phpMyAdmin (http://localhost:8081)
- **API Testing:** Postman or curl
- **Code Editor:** VSCode with ASP/VBScript extensions
---
## Common Gotchas
1. **VBScript uses & for concatenation**, not +
2. **Comparison is = not ==**
3. **All Dim declarations must be at function/procedure top**
4. **Always close recordsets and connections**
5. **FULLTEXT requires words ≥ 4 characters** (we have LIKE fallback)
6. **bit(1) fields need CBool() conversion** to use in IF statements
7. **Request.QueryString/Form always returns strings** - validate/cast!
8. **Server.HTMLEncode() all output** to prevent XSS
9. **objConn is global** - don't redeclare, just use it
10. **File paths in Windows use backslash** \, Linux forward /
---
## Keyboard Shortcuts
### Browser
- **Ctrl+F5** - Hard refresh (bypass cache)
- **F12** - Open developer tools
- **Ctrl+Shift+I** - Open inspector
### VSCode
- **Ctrl+P** - Quick file open
- **Ctrl+Shift+F** - Search across all files
- **Ctrl+/** - Toggle comment
- **Alt+Up/Down** - Move line up/down
---
## Contact & Support
**Team Lead:** [Your name here]
**Documentation:** ~/projects/windows/shopdb/docs/
**Issues:** Create GitHub issue (once repo setup)
**Emergency:** [Contact info]
---
**Last Updated:** 2025-10-20
**Maintained By:** Development Team
**Review:** Update when major changes occur

346
docs/README.md Normal file
View File

@@ -0,0 +1,346 @@
# ShopDB Documentation
Welcome to the ShopDB documentation! This folder contains everything you need to understand, develop, and maintain the ShopDB application.
---
## Documentation Overview
### 📘 For New Team Members
**Start here in this order:**
1. **[QUICK_REFERENCE.md](QUICK_REFERENCE.md)** ⭐ START HERE
- Quick facts, common tasks, cheat sheets
- Perfect for daily reference
- **Time to read:** 15 minutes
2. **[GIT_WORKFLOW.md](GIT_WORKFLOW.md)** 🔧 MANDATORY
- Git workflow and commit standards
- How to commit and push changes
- **MUST READ before making any code changes**
- **Time to read:** 20 minutes
3. **[ASP_DEVELOPMENT_GUIDE.md](ASP_DEVELOPMENT_GUIDE.md)**
- Development environment setup
- How to start/stop the dev environment
- VBScript/ASP basics and patterns
- **Time to read:** 30 minutes
4. **[DEEP_DIVE_REPORT.md](DEEP_DIVE_REPORT.md)** 📚 COMPREHENSIVE
- Complete database schema documentation
- Application architecture deep dive
- Data flows and workflows
- Technical debt analysis
- Recommendations and roadmap
- **Time to read:** 2-3 hours (reference material)
5. **[STANDARDS.md](STANDARDS.md)** ⚠️ MANDATORY
- Coding standards (MUST follow)
- Security requirements
- Database access patterns
- Input validation rules
- Error handling standards
- **Time to read:** 45 minutes
6. **[NESTED_ENTITY_CREATION.md](NESTED_ENTITY_CREATION.md)**
- How to create complex forms
- Nested entity management (e.g., add printer + create new model inline)
- **Time to read:** 20 minutes
7. **[GIT_SETUP_GUIDE.md](GIT_SETUP_GUIDE.md)**
- Setting up Gitea (Git server with web UI)
- SSH key configuration
- First-time Git setup
- **Time to read:** 30 minutes (one-time setup)
8. **[GITEA_FEATURES_GUIDE.md](GITEA_FEATURES_GUIDE.md)**
- Using Gitea Projects (Kanban boards)
- Issue tracking and bug management
- Wiki for collaborative documentation
- Pull requests and code review
- Milestones and releases
- **Time to read:** 45 minutes
---
## Quick Navigation
### By Role
**Developers:**
1. Read: QUICK_REFERENCE.md
2. **MANDATORY: GIT_WORKFLOW.md** ⚠️
3. Setup: ASP_DEVELOPMENT_GUIDE.md, GIT_SETUP_GUIDE.md
4. Standards: STANDARDS.md
5. Deep dive: DEEP_DIVE_REPORT.md (sections 2, 3, 6)
6. Advanced: NESTED_ENTITY_CREATION.md
7. Project Management: GITEA_FEATURES_GUIDE.md
**Database Administrators:**
1. Read: QUICK_REFERENCE.md (Database section)
2. Read: DEEP_DIVE_REPORT.md (Section 1: Database Architecture)
3. Review: STANDARDS.md (Database Access Standards)
4. Reference: SQL queries in QUICK_REFERENCE.md
**System Administrators:**
1. Read: ASP_DEVELOPMENT_GUIDE.md (Prerequisites, Troubleshooting)
2. Read: DEEP_DIVE_REPORT.md (Section 7.3: For System Administrators)
3. Reference: QUICK_REFERENCE.md (Common Tasks)
**Business Analysts:**
1. Read: DEEP_DIVE_REPORT.md (Executive Summary, Section 1, Section 7.4)
2. Reference: QUICK_REFERENCE.md (Key Views, SQL Queries)
**Project Managers:**
1. Read: DEEP_DIVE_REPORT.md (Executive Summary, Section 4: Technical Debt, Section 6: Recommendations)
2. Read: GITEA_FEATURES_GUIDE.md (Projects, Issues, Milestones, Releases)
---
## By Topic
### Database
- **Schema Overview:** DEEP_DIVE_REPORT.md → Section 1
- **Quick Reference:** QUICK_REFERENCE.md → Core Tables Cheat Sheet
- **Access Patterns:** STANDARDS.md → Database Access Standards
- **Views:** DEEP_DIVE_REPORT.md → Section 1.3
- **Sample Queries:** QUICK_REFERENCE.md → Useful SQL Queries
### Development
- **Git Workflow:** GIT_WORKFLOW.md → Complete workflow guide ⚠️ MANDATORY
- **Git Setup:** GIT_SETUP_GUIDE.md → Gitea installation and SSH keys
- **Project Management:** GITEA_FEATURES_GUIDE.md → Issues, Projects, Wiki, PRs
- **Setup Environment:** ASP_DEVELOPMENT_GUIDE.md → Project Setup
- **Coding Patterns:** ASP_DEVELOPMENT_GUIDE.md → Common VBScript/ASP Patterns
- **Standards:** STANDARDS.md → All sections
- **Quick Reference:** QUICK_REFERENCE.md → Key VBScript Patterns
### Architecture
- **Overview:** DEEP_DIVE_REPORT.md → Section 2
- **File Structure:** DEEP_DIVE_REPORT.md → Section 2.2
- **Data Flows:** DEEP_DIVE_REPORT.md → Section 3
- **Diagrams:** DEEP_DIVE_REPORT.md → Sections 9, 10
### Security
- **Standards:** STANDARDS.md → Security Standards
- **Issues:** DEEP_DIVE_REPORT.md → Section 4.1
- **Checklist:** QUICK_REFERENCE.md → Security Checklist
### Troubleshooting
- **Dev Environment:** ASP_DEVELOPMENT_GUIDE.md → Troubleshooting
- **Quick Fixes:** QUICK_REFERENCE.md → Troubleshooting
- **Common Issues:** DEEP_DIVE_REPORT.md → Section 4
---
## Document Maintenance
### When to Update
**QUICK_REFERENCE.md:**
- New common task identified
- New frequently-used query
- New troubleshooting tip
**ASP_DEVELOPMENT_GUIDE.md:**
- Development environment changes
- New tools or dependencies
- Setup process changes
**DEEP_DIVE_REPORT.md:**
- Major schema changes
- New features added
- Architecture changes
- Quarterly review updates
**STANDARDS.md:**
- New coding standards adopted
- Security policy changes
- New validation patterns
- New error codes
**NESTED_ENTITY_CREATION.md:**
- New nested entity patterns
- Complex form examples
### How to Update
1. **Small Updates:** Edit the file directly, commit to Git (once setup)
2. **Major Updates:** Create a copy, edit, have peer review, then replace
3. **Always Update:** "Last Updated" date at bottom of each file
4. **Document Changes:** Note what changed in Git commit message
---
## Document Status
| Document | Last Updated | Status | Review Cycle |
|----------|--------------|--------|--------------|
| QUICK_REFERENCE.md | 2025-10-20 | ✅ Current | As needed |
| GIT_WORKFLOW.md | 2025-10-20 | ✅ Current | Quarterly |
| GIT_SETUP_GUIDE.md | 2025-10-20 | ✅ Current | Annually |
| GITEA_FEATURES_GUIDE.md | 2025-10-20 | ✅ Current | Quarterly |
| ASP_DEVELOPMENT_GUIDE.md | 2025-10-10 | ✅ Current | Quarterly |
| DEEP_DIVE_REPORT.md | 2025-10-20 | ✅ Current | Quarterly |
| STANDARDS.md | 2025-10-10 | ✅ Current | Semi-annually |
| NESTED_ENTITY_CREATION.md | 2025-10-10 | ✅ Current | Annually |
| README.md (this file) | 2025-10-20 | ✅ Current | As needed |
---
## Quick Start for New Developers
### Day 1 Checklist
- [ ] Read QUICK_REFERENCE.md (15 min)
- [ ] **Read GIT_WORKFLOW.md (20 min) - MANDATORY** ⚠️
- [ ] Follow ASP_DEVELOPMENT_GUIDE.md to setup environment (1-2 hours)
- [ ] Verify Git repository is initialized
- [ ] Browse application at http://192.168.122.151:8080
- [ ] Read STANDARDS.md (45 min)
- [ ] Make a test edit, commit, and push to Git
### Week 1 Checklist
- [ ] Read DEEP_DIVE_REPORT.md Executive Summary
- [ ] Read DEEP_DIVE_REPORT.md Section 1 (Database)
- [ ] Read DEEP_DIVE_REPORT.md Section 2 (Architecture)
- [ ] Read GITEA_FEATURES_GUIDE.md (Issues, Projects, Wiki)
- [ ] Create your first issue in Gitea
- [ ] Explore all display*.asp pages
- [ ] Run sample SQL queries from QUICK_REFERENCE.md
- [ ] Understand PC-to-machine assignment logic
### Month 1 Checklist
- [ ] Complete DEEP_DIVE_REPORT.md
- [ ] Implement a small feature end-to-end
- [ ] Review NESTED_ENTITY_CREATION.md
- [ ] Contribute a documentation improvement
- [ ] Pair program with experienced team member
---
## External Resources
### Classic ASP / VBScript
- [Microsoft ASP Reference](https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525334(v=vs.90))
- [VBScript Language Reference](https://learn.microsoft.com/en-us/previous-versions//d1wf56tt(v=vs.85))
- [W3Schools ASP Tutorial](https://www.w3schools.com/asp/)
### MySQL
- [MySQL 5.6 Reference Manual](https://dev.mysql.com/doc/refman/5.6/en/)
- [MySQL FULLTEXT Search](https://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html)
- [MySQL Performance Tuning](https://dev.mysql.com/doc/refman/5.6/en/optimization.html)
### Frontend
- [Bootstrap 4.6 Documentation](https://getbootstrap.com/docs/4.6/)
- [jQuery Documentation](https://api.jquery.com/)
- [Material Design Iconic Font](https://zavoloklom.github.io/material-design-iconic-font/)
- [FullCalendar v3](https://fullcalendar.io/docs/v3)
- [DataTables](https://datatables.net/)
---
## Getting Help
### Documentation Issues
- Document unclear? Create an issue or update it yourself!
- Found an error? Fix it and commit
- Missing information? Add it!
### Technical Questions
- Check QUICK_REFERENCE.md first
- Search DEEP_DIVE_REPORT.md
- Ask team lead
- Create documentation if answer isn't documented
### Code Questions
- Review STANDARDS.md
- Check ASP_DEVELOPMENT_GUIDE.md for patterns
- Look at similar existing code
- Ask for code review
---
## Contributing to Documentation
We encourage all team members to improve documentation!
### Guidelines
1. **Be Clear** - Write for someone who doesn't know the system
2. **Be Concise** - Respect the reader's time
3. **Be Accurate** - Test commands/code before documenting
4. **Be Current** - Update dates when you edit
5. **Be Helpful** - Include examples and context
### What to Document
- Solutions to problems you encountered
- Common tasks you perform
- Tricky patterns or gotchas
- New features or changes
- Helpful queries or scripts
### How to Contribute
1. Edit the relevant .md file
2. Update "Last Updated" date
3. Commit with descriptive message
4. (Optional) Have peer review for major changes
---
## Version History
**v1.3** - 2025-10-20
- Added GIT_WORKFLOW.md (mandatory Git workflow documentation)
- Added GIT_SETUP_GUIDE.md (Gitea setup guide)
- Updated README.md with Git workflow references
- Established mandatory commit-after-every-change policy
**v1.2** - 2025-10-20
- Added DEEP_DIVE_REPORT.md (comprehensive technical report)
- Added QUICK_REFERENCE.md (cheat sheets)
- Added this README.md
- Updated ASP_DEVELOPMENT_GUIDE.md with documentation references
**v1.1** - 2025-10-10
- Added STANDARDS.md (coding standards)
- Added NESTED_ENTITY_CREATION.md
- Updated ASP_DEVELOPMENT_GUIDE.md
**v1.0** - 2025-10-09
- Initial ASP_DEVELOPMENT_GUIDE.md created
---
## Future Documentation Plans
- [ ] API Documentation (when APIs expand)
- [ ] Deployment Guide (CI/CD pipeline)
- [ ] Security Audit Report
- [ ] Performance Optimization Guide
- [ ] Testing Guide (when tests implemented)
- [ ] Video tutorials (screen recordings)
- [ ] FAQ document
- [ ] Glossary of GE-specific terms
---
**Maintained By:** Development Team
**Questions?** Ask team lead or update docs directly
**Feedback?** Create issue or improve the docs yourself!
---
## Summary
You now have comprehensive documentation covering:
**Quick Reference** - Daily cheat sheet
**Git Workflow** - Mandatory version control workflow ⚠️
**Development Guide** - Environment setup
**Deep Dive Report** - Complete technical documentation
**Standards** - Mandatory coding rules
**Advanced Patterns** - Complex forms
**Start with QUICK_REFERENCE.md, then read GIT_WORKFLOW.md before making any code changes!**
Happy coding! 🚀

1232
docs/STANDARDS.md Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,450 @@
# Unified Infrastructure Pages - Design Document
**Approach:** Single set of pages that dynamically handles servers, switches, and cameras
**Files Required:** 4 files (vs 12 separate files)
---
## Architecture
### URL Structure
```
displayinfrastructure.asp?type=server → List all servers
displayinfrastructure.asp?type=switch → List all switches
displayinfrastructure.asp?type=camera → List all cameras
displayinfrastructure_detail.asp?type=server&id=5 → Server #5 detail/edit
displayinfrastructure_detail.asp?type=switch&id=12 → Switch #12 detail/edit
displayinfrastructure_detail.asp?type=camera&id=3 → Camera #3 detail/edit
addinfrastructure.asp?type=server → Add new server form
addinfrastructure.asp?type=switch → Add new switch form
addinfrastructure.asp?type=camera → Add new camera form
saveinfrastructure_direct.asp → Universal save endpoint
```
---
## File 1: displayinfrastructure.asp (List View)
### Logic Flow
```vbscript
<%
' Get device type from URL
Dim deviceType
deviceType = Request.QueryString("type")
' Validate type
If deviceType <> "server" AND deviceType <> "switch" AND deviceType <> "camera" Then
deviceType = "server" ' Default
End If
' Set display variables based on type
Dim tableName, idField, pageTitle, iconClass, addUrl
Select Case deviceType
Case "server"
tableName = "servers"
idField = "serverid"
pageTitle = "Servers"
iconClass = "zmdi-storage"
addUrl = "addinfrastructure.asp?type=server"
Case "switch"
tableName = "switches"
idField = "switchid"
pageTitle = "Switches"
iconClass = "zmdi-device-hub"
addUrl = "addinfrastructure.asp?type=switch"
Case "camera"
tableName = "cameras"
idField = "cameraid"
pageTitle = "Cameras"
iconClass = "zmdi-videocam"
addUrl = "addinfrastructure.asp?type=camera"
End Select
' Build query
Dim strSQL
strSQL = "SELECT d.*, m.modelnumber, v.vendor " & _
"FROM " & tableName & " d " & _
"LEFT JOIN models m ON d.modelid = m.modelnumberid " & _
"LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE d.isactive = 1 " & _
"ORDER BY d." & idField & " DESC"
Set rs = objConn.Execute(strSQL)
%>
<h5 class="card-title">
<i class="zmdi <%=iconClass%>"></i> <%=pageTitle%>
</h5>
<a href="<%=addUrl%>" class="btn btn-primary">
<i class="zmdi zmdi-plus-circle"></i> Add <%=pageTitle%>
</a>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Vendor</th>
<th>Model</th>
<th>Serial</th>
<th>IP Address</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% Do While Not rs.EOF %>
<tr>
<td><%=rs(idField)%></td>
<td><%=Server.HTMLEncode(rs("vendor") & "")%></td>
<td><%=Server.HTMLEncode(rs("modelnumber") & "")%></td>
<td><%=Server.HTMLEncode(rs("serialnumber") & "")%></td>
<td><%=Server.HTMLEncode(rs("ipaddress") & "")%></td>
<td><%=Server.HTMLEncode(rs("description") & "")%></td>
<td>
<a href="displayinfrastructure_detail.asp?type=<%=deviceType%>&id=<%=rs(idField)%>">
View
</a>
</td>
</tr>
<%
rs.MoveNext
Loop
%>
</tbody>
</table>
```
---
## File 2: displayinfrastructure_detail.asp (Detail/Edit View)
### Logic Flow
```vbscript
<%
' Get device type and ID
Dim deviceType, deviceId
deviceType = Request.QueryString("type")
deviceId = Request.QueryString("id")
' Validate
If deviceType <> "server" AND deviceType <> "switch" AND deviceType <> "camera" Then
Response.Redirect("displayinfrastructure.asp?type=server")
End If
' Set variables based on type
Dim tableName, idField, pageTitle, listUrl
Select Case deviceType
Case "server"
tableName = "servers"
idField = "serverid"
pageTitle = "Server"
listUrl = "displayinfrastructure.asp?type=server"
Case "switch"
tableName = "switches"
idField = "switchid"
pageTitle = "Switch"
listUrl = "displayinfrastructure.asp?type=switch"
Case "camera"
tableName = "cameras"
idField = "cameraid"
pageTitle = "Camera"
listUrl = "displayinfrastructure.asp?type=camera"
End Select
' Fetch device
strSQL = "SELECT d.*, m.modelnumber, v.vendor, v.vendorid " & _
"FROM " & tableName & " d " & _
"LEFT JOIN models m ON d.modelid = m.modelnumberid " & _
"LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE d." & idField & " = " & deviceId
Set rs = objConn.Execute(strSQL)
If rs.EOF Then
Response.Write("Device not found")
Response.End
End If
%>
<!-- Display Mode -->
<div id="displayMode">
<h3><%=pageTitle%> #<%=rs(idField)%></h3>
<p><strong>Vendor:</strong> <%=Server.HTMLEncode(rs("vendor") & "")%></p>
<p><strong>Model:</strong> <%=Server.HTMLEncode(rs("modelnumber") & "")%></p>
<p><strong>Serial:</strong> <%=Server.HTMLEncode(rs("serialnumber") & "")%></p>
<p><strong>IP:</strong> <%=Server.HTMLEncode(rs("ipaddress") & "")%></p>
<p><strong>Description:</strong> <%=Server.HTMLEncode(rs("description") & "")%></p>
<button onclick="showEditMode()">Edit</button>
<a href="<%=listUrl%>">Back to List</a>
</div>
<!-- Edit Mode -->
<div id="editMode" style="display:none;">
<h3>Edit <%=pageTitle%></h3>
<form method="post" action="saveinfrastructure_direct.asp">
<input type="hidden" name="type" value="<%=deviceType%>">
<input type="hidden" name="id" value="<%=rs(idField)%>">
<!-- Model dropdown -->
<select name="modelid">
<!-- Populate models... -->
</select>
<input type="text" name="serialnumber" value="<%=Server.HTMLEncode(rs("serialnumber") & "")%>">
<input type="text" name="ipaddress" value="<%=Server.HTMLEncode(rs("ipaddress") & "")%>">
<textarea name="description"><%=Server.HTMLEncode(rs("description") & "")%></textarea>
<button type="submit">Save</button>
<button type="button" onclick="showDisplayMode()">Cancel</button>
</form>
</div>
```
---
## File 3: addinfrastructure.asp (Add Form)
### Logic Flow
```vbscript
<%
' Get device type
Dim deviceType
deviceType = Request.QueryString("type")
' Validate
If deviceType <> "server" AND deviceType <> "switch" AND deviceType <> "camera" Then
deviceType = "server"
End If
' Set variables
Dim pageTitle, listUrl
Select Case deviceType
Case "server"
pageTitle = "Server"
listUrl = "displayinfrastructure.asp?type=server"
Case "switch"
pageTitle = "Switch"
listUrl = "displayinfrastructure.asp?type=switch"
Case "camera"
pageTitle = "Camera"
listUrl = "displayinfrastructure.asp?type=camera"
End Select
%>
<h2>Add <%=pageTitle%></h2>
<form method="post" action="saveinfrastructure_direct.asp">
<input type="hidden" name="type" value="<%=deviceType%>">
<div class="form-group">
<label>Model</label>
<select name="modelid" required class="form-control">
<option value="">-- Select Model --</option>
<%
strSQL = "SELECT m.modelnumberid, m.modelnumber, v.vendor " & _
"FROM models m " & _
"INNER JOIN vendors v ON m.vendorid = v.vendorid " & _
"WHERE m.isactive = 1 " & _
"ORDER BY v.vendor, m.modelnumber"
Set rsModels = objConn.Execute(strSQL)
Do While Not rsModels.EOF
%>
<option value="<%=rsModels("modelnumberid")%>">
<%=Server.HTMLEncode(rsModels("vendor") & " - " & rsModels("modelnumber"))%>
</option>
<%
rsModels.MoveNext
Loop
%>
</select>
</div>
<div class="form-group">
<label>Serial Number</label>
<input type="text" name="serialnumber" class="form-control" maxlength="100">
</div>
<div class="form-group">
<label>IP Address</label>
<input type="text" name="ipaddress" class="form-control" maxlength="15">
</div>
<div class="form-group">
<label>Description</label>
<textarea name="description" class="form-control" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-success">Save <%=pageTitle%></button>
<a href="<%=listUrl%>" class="btn btn-secondary">Cancel</a>
</form>
```
---
## File 4: saveinfrastructure_direct.asp (Universal Save)
### Logic Flow
```vbscript
<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/error_handler.asp"-->
<!--#include file="./includes/validation.asp"-->
<!--#include file="./includes/db_helpers.asp"-->
<%
' Get device type
Dim deviceType
deviceType = Request.Form("type")
' Validate type
If deviceType <> "server" AND deviceType <> "switch" AND deviceType <> "camera" Then
Response.Write("Error: Invalid device type")
Response.End
End If
' Set table name and ID field based on type
Dim tableName, idField, listUrl
Select Case deviceType
Case "server"
tableName = "servers"
idField = "serverid"
listUrl = "displayinfrastructure.asp?type=server"
Case "switch"
tableName = "switches"
idField = "switchid"
listUrl = "displayinfrastructure.asp?type=switch"
Case "camera"
tableName = "cameras"
idField = "cameraid"
listUrl = "displayinfrastructure.asp?type=camera"
End Select
' Get form data
Dim deviceId, modelid, serialnumber, ipaddress, description
deviceId = GetSafeInteger("FORM", "id", 0, 0, 999999)
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, "")
' Determine INSERT or UPDATE
Dim strSQL
If deviceId = 0 Then
' INSERT - New device
strSQL = "INSERT INTO " & tableName & " (modelid, serialnumber, ipaddress, description, isactive) " & _
"VALUES (?, ?, ?, ?, 1)"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(modelid, serialnumber, ipaddress, description))
Else
' UPDATE - Existing device
strSQL = "UPDATE " & tableName & " " & _
"SET modelid = ?, serialnumber = ?, ipaddress = ?, description = ? " & _
"WHERE " & idField & " = ?"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(modelid, serialnumber, ipaddress, description, deviceId))
End If
Call CleanupResources()
' Redirect back to list
Response.Redirect(listUrl)
%>
```
---
## Navigation Menu
### leftsidebar.asp Update
```html
<!-- Infrastructure Section -->
<li class="nav-header">INFRASTRUCTURE</li>
<li>
<a href="displayinfrastructure.asp?type=server">
<i class="zmdi zmdi-storage"></i> Servers
</a>
</li>
<li>
<a href="displayinfrastructure.asp?type=switch">
<i class="zmdi zmdi-device-hub"></i> Switches
</a>
</li>
<li>
<a href="displayinfrastructure.asp?type=camera">
<i class="zmdi zmdi-videocam"></i> Cameras
</a>
</li>
```
---
## Pros vs Cons
### Unified Approach (Option 2) - RECOMMENDED
**Pros:**
- ✅ Only 4 files to create (vs 12)
- ✅ DRY - no code duplication
- ✅ Easy to maintain - fix once, works for all
- ✅ Easy to extend - add "UPS" or "Firewall" by just adding cases
- ✅ Consistent UI across all infrastructure
- ✅ Matches database design (vw_network_devices already unifies them)
**Cons:**
- ⚠️ Slightly more complex logic (Select Case statements)
- ⚠️ URLs less intuitive (type parameter required)
- ⚠️ Harder to customize one type differently later
### Separate Pages Approach (Option 1)
**Pros:**
- ✅ URLs cleaner (displayservers.asp vs displayinfrastructure.asp?type=server)
- ✅ Simpler per-file logic (no branching)
- ✅ Easy to customize one type differently
- ✅ More explicit/clear what page does
**Cons:**
- ❌ 12 files instead of 4 (3x code duplication)
- ❌ Bug fixes need to be applied 3 times
- ❌ UI inconsistencies more likely
- ❌ Adding new type = 4 more files
---
## Hybrid Approach (Best of Both?)
**Could also do:**
- Use unified pages for LIST/ADD/SAVE (shared logic)
- Use separate pages for DETAIL if they differ significantly
Example:
```
displayinfrastructure.asp?type=server (unified list)
addinfrastructure.asp?type=server (unified add form)
saveinfrastructure_direct.asp (unified save)
displayserver.asp?id=5 (separate detail - if servers need special fields)
displayswitch.asp?id=12 (separate detail - if switches different)
displaycamera.asp?id=3 (separate detail - if cameras different)
```
But for infrastructure devices with identical schemas, I'd stick with **fully unified**.
---
## My Recommendation
**Go with Option 2 (Unified Pages) because:**
1. Servers, switches, and cameras have **identical schemas** (modelid, serialnumber, ipaddress, description, maptop, mapleft, isactive)
2. They have **identical CRUD operations** (add, edit, view, delete)
3. The database already unifies them (`vw_network_devices`)
4. Much faster to implement (4 files vs 12)
5. Easier to maintain long-term
---
**Ready to implement?** I can create the 4 unified infrastructure files now.

View File

@@ -0,0 +1,515 @@
# 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

View File

@@ -0,0 +1,481 @@
# Vendor Type Refactoring Plan
## Overview
Refactor the `vendors` table to use a normalized many-to-many relationship for vendor types instead of multiple boolean columns.
---
## Current Design (Problems)
### Vendors Table Structure:
```sql
vendorid INT(11) PK
vendor VARCHAR(50)
isactive CHAR(50) -- Should be BIT(1)!
isprinter BIT(1) -- Boolean flag
ispc BIT(1) -- Boolean flag
ismachine BIT(1) -- Boolean flag
isserver BIT(1) -- Boolean flag
isswitch BIT(1) -- Boolean flag
iscamera BIT(1) -- Boolean flag
```
### Issues:
1. **Not Normalized**: Multiple boolean columns for types
2. **Not Scalable**: Adding new device types requires ALTER TABLE
3. **Inefficient Queries**: Need to check multiple columns
4. **Data Type Issue**: `isactive` is CHAR(50) instead of BIT(1)
5. **No Multi-Type Support**: Hard to query "vendors that are both printer AND pc"
---
## Proposed Design (Solution)
### New Tables:
#### 1. `vendortypes` (Lookup Table)
```sql
CREATE TABLE vendortypes (
vendortypeid INT(11) PRIMARY KEY AUTO_INCREMENT,
vendortype VARCHAR(50) NOT NULL UNIQUE,
description VARCHAR(255),
isactive BIT(1) DEFAULT b'1'
);
-- Initial Data:
INSERT INTO vendortypes (vendortype, description) VALUES
('TBD', 'To be determined / Unassigned'),
('Printer', 'Printer manufacturers'),
('PC', 'Computer manufacturers'),
('Machine', 'CNC machine manufacturers'),
('Server', 'Server manufacturers'),
('Switch', 'Network switch manufacturers'),
('Camera', 'Security camera manufacturers');
```
#### 2. Updated `vendors` Table (One-to-Many):
```sql
-- Add vendortypeid, remove old flags, fix isactive
ALTER TABLE vendors
ADD COLUMN vendortypeid INT(11) DEFAULT 1 AFTER vendorid,
ADD INDEX idx_vendortypeid (vendortypeid),
ADD FOREIGN KEY (vendortypeid) REFERENCES vendortypes(vendortypeid) ON DELETE SET NULL,
DROP COLUMN isprinter,
DROP COLUMN ispc,
DROP COLUMN ismachine,
DROP COLUMN isserver,
DROP COLUMN isswitch,
DROP COLUMN iscamera,
MODIFY COLUMN isactive BIT(1) DEFAULT b'1';
```
**Note**: Each vendor has ONE type. Default is vendortypeid=1 (TBD).
---
## Benefits
**Normalized**: Proper relational design
**Scalable**: Add new types without schema changes
**Simpler**: One type per vendor (one-to-many relationship)
**Cleaner Queries**: `JOIN vendortypes WHERE vendortypeid = 2`
**Better Reporting**: Easy to query "all vendors by type"
**Maintainable**: Type list managed in one place
**TBD Support**: Default type for unassigned/unknown vendors
---
## Data Migration Strategy
### Step 1: Create New Tables
```sql
CREATE TABLE vendortypes (...);
CREATE TABLE vendor_vendortypes (...);
```
### Step 2: Migrate Existing Data
```sql
-- Set vendortypeid based on first TRUE flag found (priority order)
UPDATE vendors SET vendortypeid = 2 WHERE isprinter = 1; -- Printer
UPDATE vendors SET vendortypeid = 3 WHERE ispc = 1; -- PC
UPDATE vendors SET vendortypeid = 4 WHERE ismachine = 1; -- Machine
UPDATE vendors SET vendortypeid = 5 WHERE isserver = 1; -- Server
UPDATE vendors SET vendortypeid = 6 WHERE isswitch = 1; -- Switch
UPDATE vendors SET vendortypeid = 7 WHERE iscamera = 1; -- Camera
-- Vendors with all flags = 0 will remain vendortypeid = 1 (TBD)
```
### Step 3: Update Application Code (see below)
### Step 4: Drop Old Columns
```sql
ALTER TABLE vendors
DROP COLUMN isprinter,
DROP COLUMN ispc,
DROP COLUMN ismachine,
DROP COLUMN isserver,
DROP COLUMN isswitch,
DROP COLUMN iscamera;
```
---
## Code Changes Required
### Pattern: Old vs New
**OLD WAY:**
```sql
SELECT vendorid, vendor
FROM vendors
WHERE isprinter = 1 AND isactive = 1
```
**NEW WAY:**
```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
```
Or using vendortypeid directly (more efficient):
```sql
SELECT v.vendorid, v.vendor
FROM vendors v
WHERE v.vendortypeid = 2 AND v.isactive = 1 -- 2 = Printer
```
### Files Requiring Updates (31 files found):
#### Printer-Related (7 files):
- `/addprinter.asp` - Line 53, 90
- `/displayprinter.asp` - Line 291
- `/editprinter.asp`
- `/saveprinter_direct.asp`
- `/includes/data_cache.asp` - Line 30 (RenderVendorOptions function)
#### Machine-Related (4 files):
- `/addmachine.asp` - Line 62, 98
- `/displaymachine.asp` - Line 236
- `/editmacine.asp`
- `/savemachine_direct.asp`
#### PC-Related (3 files):
- `/displaypc.asp`
- `/editdevice.asp` - Line 158, 199
- `/updatedevice_direct.asp`
- `/updatepc_direct.asp`
#### Model/Vendor Management (6 files):
- `/addmodel.asp`
- `/savemodel.asp`
- `/savemodel_direct.asp`
- `/addvendor.asp`
- `/savevendor.asp`
- `/savevendor_direct.asp`
#### Application-Related (7 files):
- `/addapplication.asp`
- `/displayapplication.asp`
- `/editapplication.asp`
- `/editapplication_direct.asp`
- `/editapplication_v2.asp`
- `/editapp_standalone.asp`
- `/saveapplication.asp`
- `/saveapplication_direct.asp`
- `/quickadd_application.asp`
#### Knowledge Base (2 files):
- `/addlink_direct.asp`
- `/updatelink_direct.asp`
#### Search (1 file):
- `/search.asp` - Lines 493-556 (machine and printer search with vendor joins)
---
## Recommended Approach
### Option 1: Create Helper View (Easier Migration)
Create a view that mimics the old structure:
```sql
CREATE VIEW vw_vendors_with_types AS
SELECT
v.vendorid,
v.vendor,
v.isactive,
v.vendortypeid,
vt.vendortype,
CASE WHEN vt.vendortype = 'Printer' THEN 1 ELSE 0 END AS isprinter,
CASE WHEN vt.vendortype = 'PC' THEN 1 ELSE 0 END AS ispc,
CASE WHEN vt.vendortype = 'Machine' THEN 1 ELSE 0 END AS ismachine,
CASE WHEN vt.vendortype = 'Server' THEN 1 ELSE 0 END AS isserver,
CASE WHEN vt.vendortype = 'Switch' THEN 1 ELSE 0 END AS isswitch,
CASE WHEN vt.vendortype = 'Camera' THEN 1 ELSE 0 END AS iscamera
FROM vendors v
LEFT JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid;
```
**Benefit**: Minimal code changes - just replace `vendors` with `vw_vendors_with_types` in SELECT queries
### Option 2: Update All Queries (Better Long-Term)
Update all 30 files to use proper JOINs with new tables.
**Benefit**: Cleaner code, better performance, proper normalization
---
## Helper Functions Needed
### ASP Include: `/includes/vendor_helpers.asp`
```vbscript
<%
' Get vendors by type (returns recordset)
Function GetVendorsByType(vendorType)
Dim sql, rs
sql = "SELECT v.vendorid, v.vendor " & _
"FROM vendors v " & _
"INNER JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid " & _
"WHERE vt.vendortype = '" & Replace(vendorType, "'", "''") & "' " & _
"AND v.isactive = 1 " & _
"ORDER BY v.vendor ASC"
Set rs = objConn.Execute(sql)
Set GetVendorsByType = rs
End Function
' Get vendors by type ID (more efficient)
Function GetVendorsByTypeId(vendortypeid)
Dim sql, rs
sql = "SELECT vendorid, vendor " & _
"FROM vendors " & _
"WHERE vendortypeid = " & vendortypeid & " " & _
"AND isactive = 1 " & _
"ORDER BY vendor ASC"
Set rs = objConn.Execute(sql)
Set GetVendorsByTypeId = rs
End Function
' Get vendor type name for a vendor
Function GetVendorType(vendorId)
Dim sql, rs
sql = "SELECT vt.vendortype " & _
"FROM vendors v " & _
"INNER JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid " & _
"WHERE v.vendorid = " & vendorId
Set rs = objConn.Execute(sql)
If Not rs.EOF Then
GetVendorType = rs("vendortype")
Else
GetVendorType = "TBD"
End If
rs.Close
Set rs = Nothing
End Function
%>
```
---
## Example Code Updates
### Before (addprinter.asp line 90):
```vbscript
strSQL = "SELECT vendorid, vendor FROM vendors WHERE isprinter = 1 AND isactive = 1 ORDER BY vendor ASC"
Set rsVendors = objConn.Execute(strSQL)
```
### After (Option 1 - Using View):
```vbscript
strSQL = "SELECT vendorid, vendor FROM vw_vendors_with_types WHERE isprinter = 1 AND isactive = 1 ORDER BY vendor ASC"
Set rsVendors = objConn.Execute(strSQL)
```
### After (Option 2 - Using Helper):
```vbscript
Set rsVendors = GetVendorsByType("Printer")
```
### After (Option 3 - Direct Query with JOIN):
```vbscript
strSQL = "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"
Set rsVendors = objConn.Execute(strSQL)
```
### After (Option 4 - Direct Query with ID - FASTEST):
```vbscript
' Printer = vendortypeid 2
strSQL = "SELECT vendorid, vendor FROM vendors WHERE vendortypeid = 2 AND isactive = 1 ORDER BY vendor ASC"
Set rsVendors = objConn.Execute(strSQL)
```
### Search.asp Special Case:
The search.asp file (lines 493-556) searches machines and printers with vendor joins. Currently it searches by vendor name, which will continue to work. However, if we want to enable searching by vendor type (e.g., "printer vendors", "machine vendors"), we need to update the query:
**Current (machine search):**
```vbscript
strSQL = "SELECT m.machineid, m.machinenumber, m.alias, mt.machinetype " & _
"FROM machines m " & _
"INNER JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid " & _
"LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid " & _
"LEFT JOIN vendors v ON mo.vendorid = v.vendorid " & _
"WHERE (m.machinenumber LIKE ? OR m.alias LIKE ? OR m.machinenotes LIKE ? OR mt.machinetype LIKE ? OR v.vendor LIKE ?) " & _
" AND m.isactive = 1 " & _
"LIMIT 10"
```
**New (with vendortype support):**
```vbscript
strSQL = "SELECT m.machineid, m.machinenumber, m.alias, mt.machinetype " & _
"FROM machines m " & _
"INNER JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid " & _
"LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid " & _
"LEFT JOIN vendors v ON mo.vendorid = v.vendorid " & _
"LEFT JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid " & _
"WHERE (m.machinenumber LIKE ? OR m.alias LIKE ? OR m.machinenotes LIKE ? OR mt.machinetype LIKE ? OR v.vendor LIKE ? OR vt.vendortype LIKE ?) " & _
" AND m.isactive = 1 " & _
"LIMIT 10"
```
**Note**: This is optional - the search will continue to work with just vendor names. Only add vendortype searching if desired.
---
## Testing Plan
1. **Create migration script** with new tables
2. **Migrate data** from boolean flags to junction table
3. **Create view** for backward compatibility
4. **Test all 30 files** with view in place
5. **Gradually update** code to use new structure
6. **Drop view** once all code is updated
7. **Drop old columns** from vendors table
---
## Timeline Estimate
- **Database Migration**: 1 hour
- **Create Helper Functions**: 30 minutes
- **Update 30 Files**: 4-6 hours (depends on approach)
- **Testing**: 2-3 hours
- **Total**: ~1 day of development work
---
## Rollback Plan
If issues arise:
1. Keep old columns during testing phase
2. View provides backward compatibility
3. Can revert code changes easily
4. Only drop columns after full validation
---
## Recommendation
**Use Option 1 (Helper View) for initial migration:**
1. Create new tables and migrate data
2. Create compatibility view
3. Update queries to use view (minimal changes)
4. Keep old columns as backup
5. After validation, gradually refactor to use new structure directly
6. Drop old columns once confident
This provides a safe, gradual migration path with easy rollback capability.
---
## Implementation Checklist
See the TODO list for detailed tracking. High-level implementation order:
### Phase 1: Database Migration (Complete)
- ✅ Migration script created: `/sql/refactor_vendor_types.sql`
- ⏳ Run migration script on test database
- ⏳ Verify vendortypes table populated with 7 types (TBD, Printer, PC, Machine, Server, Switch, Camera)
- ⏳ Verify vendors.vendortypeid column added with proper foreign key
- ⏳ Verify data migrated correctly from boolean flags
- ⏳ Verify compatibility view `vw_vendors_with_types` works
- ⏳ Verify isactive column fixed (CHAR(50) → BIT(1))
### Phase 2: Code Updates (31 files)
Update all files to use new vendortypeid structure. Use one of these approaches:
- **Quick**: Replace table name `vendors` with `vw_vendors_with_types` (minimal changes)
- **Better**: Use `WHERE vendortypeid = X` (direct column check)
- **Best**: Use helper functions from vendor_helpers.asp
**File Groups**:
- ⏳ Data cache include (1 file) - **START HERE** (affects all dropdowns)
- ⏳ Printer files (7 files)
- ⏳ Machine files (4 files)
- ⏳ PC files (4 files)
- ⏳ Model/Vendor management (6 files)
- ⏳ Application files (9 files)
- ⏳ Knowledge base files (2 files)
- ⏳ Search file (1 file - optional enhancement)
### Phase 3: Testing
- ⏳ Test vendor dropdowns in all add/edit forms
- ⏳ Test filtering by vendor type works correctly
- ⏳ Test data integrity (vendors show correct type)
- ⏳ Test search functionality still works
- ⏳ Verify no SQL errors in any page
### Phase 4: Cleanup (FINAL STEP - ONLY AFTER FULL VALIDATION)
- ⏳ Create cleanup script to drop old boolean columns
- ⏳ Run cleanup script to remove isprinter, ispc, ismachine, isserver, isswitch, iscamera
- ⏳ Drop compatibility view if no longer needed
- ⏳ Update documentation
---
## Files Reference
**Migration Script**: `/home/camp/projects/windows/shopdb/sql/refactor_vendor_types.sql`
**Design Document**: `/home/camp/projects/windows/shopdb/docs/VENDOR_TYPE_REFACTORING_PLAN.md` (this file)
**Helper Functions** (to be created): `/home/camp/projects/windows/shopdb/includes/vendor_helpers.asp`
---
## Quick Reference
**Vendor Type IDs**:
- 1 = TBD (default for unassigned)
- 2 = Printer
- 3 = PC
- 4 = Machine
- 5 = Server
- 6 = Switch
- 7 = Camera
**Common Query Patterns**:
```sql
-- Get all printer vendors
SELECT * FROM vendors WHERE vendortypeid = 2 AND isactive = 1
-- Get vendor with type name
SELECT v.*, vt.vendortype
FROM vendors v
INNER JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid
WHERE v.vendorid = ?
-- Get all vendors of a specific type by name
SELECT v.* FROM vendors v
INNER JOIN vendortypes vt ON v.vendortypeid = vt.vendortypeid
WHERE vt.vendortype = 'Printer' AND v.isactive = 1
```
---
**Document Version**: 2.0
**Last Updated**: 2025-10-22
**Status**: Ready for Implementation

View File

@@ -0,0 +1,516 @@
# Warranty Management System Design
**Date Created**: 2025-11-06
**Status**: DESIGN PHASE
**Related Document**: PC_MACHINES_CONSOLIDATION_PLAN.md
---
## Executive Summary
Instead of storing warranty fields directly on the `machines` table, create a dedicated warranty management system that supports:
- Multiple warranties per machine (e.g., hardware warranty + extended support)
- Warranty history and renewals
- Different warranty providers
- Automatic expiration tracking
- Better reporting capabilities
---
## Part 1: New Warranty Infrastructure
### 1.1 Simple Warranty Table
**Design Decision**: Keep it simple - just track warranty name and expiration date.
**Rationale**:
- Most important info: when does warranty expire and what is it called
- Avoid over-engineering
- Easy to add more fields later if needed
### 1.2 New Table: warranties
Minimal warranty tracking - one or more warranties per machine
```sql
CREATE TABLE warranties (
warrantyid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL,
-- Core warranty info
warrantyname VARCHAR(100) NOT NULL, -- 'ProFlex Support', 'Next Business Day', 'Standard 3-Year', etc.
enddate DATE NOT NULL,
-- Optional metadata
notes TEXT,
isactive TINYINT(1) DEFAULT 1,
dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,
lastupdated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- Indexes
KEY idx_machineid (machineid),
KEY idx_enddate (enddate),
KEY idx_isactive (isactive),
-- Foreign Keys
CONSTRAINT fk_warranties_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
**Total Columns**: 8 (simple and clean!)
**Example Data:**
```sql
-- Dell PC with ProSupport
INSERT INTO warranties (machineid, warrantyname, enddate) VALUES
(123, 'Dell ProSupport Plus', '2026-03-15');
-- CNC Machine with extended warranty
INSERT INTO warranties (machineid, warrantyname, enddate) VALUES
(456, 'Okuma Extended Service Agreement', '2027-12-31');
-- Server with multiple warranties
INSERT INTO warranties (machineid, warrantyname, enddate) VALUES
(789, 'HP Standard Hardware Warranty', '2025-06-30'),
(789, 'HP 24/7 Support Contract', '2027-06-30');
```
---
## Part 2: Updated Machines Table Design
### 2.1 No Warranty Fields on Machines Table
**Design Decision**: Don't add any warranty fields to the machines table.
**Rationale**:
- Warranties are separate entities in their own table
- Use JOINs or views when you need warranty info
- Keeps machines table clean
- Supports multiple warranties per machine
---
## Part 3: Useful Views for Warranty Management
### 3.1 View: vw_machine_warranties
Show all machines with their warranties
```sql
CREATE VIEW vw_machine_warranties AS
SELECT
m.machineid,
m.machinenumber,
m.hostname,
m.serialnumber,
mt.machinetype,
mo.modelnumber,
v.vendor,
-- Warranty info
w.warrantyid,
w.warrantyname,
w.enddate AS warranty_enddate,
DATEDIFF(w.enddate, CURDATE()) AS days_remaining,
-- Status calculation
CASE
WHEN w.enddate IS NULL THEN 'No Warranty'
WHEN w.enddate < CURDATE() THEN 'Expired'
WHEN DATEDIFF(w.enddate, CURDATE()) <= 30 THEN 'Expiring Soon'
ELSE 'Active'
END AS warranty_status,
-- How many warranties total for this machine
(SELECT COUNT(*) FROM warranties w2
WHERE w2.machineid = m.machineid AND w2.isactive = 1) AS total_warranties,
w.notes AS warranty_notes
FROM machines m
LEFT JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
LEFT JOIN warranties w ON m.machineid = w.machineid AND w.isactive = 1
WHERE m.isactive = 1
ORDER BY m.machinenumber, w.enddate DESC;
```
### 3.2 View: vw_warranties_expiring
Show warranties expiring in the next 90 days
```sql
CREATE VIEW vw_warranties_expiring AS
SELECT
m.machineid,
m.machinenumber,
m.hostname,
m.serialnumber,
mt.machinetype,
mo.modelnumber,
v.vendor AS manufacturer,
w.warrantyname,
w.enddate AS warranty_enddate,
DATEDIFF(w.enddate, CURDATE()) AS days_remaining,
CASE
WHEN DATEDIFF(w.enddate, CURDATE()) <= 7 THEN 'Critical'
WHEN DATEDIFF(w.enddate, CURDATE()) <= 30 THEN 'Warning'
WHEN DATEDIFF(w.enddate, CURDATE()) <= 90 THEN 'Notice'
ELSE 'Active'
END AS urgency,
w.notes
FROM warranties w
INNER JOIN machines m ON w.machineid = m.machineid
LEFT JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
WHERE w.isactive = 1
AND w.enddate >= CURDATE()
AND DATEDIFF(w.enddate, CURDATE()) <= 90
AND m.isactive = 1
ORDER BY days_remaining ASC;
```
### 3.3 View: vw_warranty_summary
Summary statistics for reporting
```sql
CREATE VIEW vw_warranty_summary AS
SELECT
COUNT(*) AS total_warranties,
SUM(CASE WHEN w.enddate >= CURDATE() THEN 1 ELSE 0 END) AS active_warranties,
SUM(CASE WHEN w.enddate < CURDATE() THEN 1 ELSE 0 END) AS expired_warranties,
SUM(CASE WHEN DATEDIFF(w.enddate, CURDATE()) <= 90 AND w.enddate >= CURDATE() THEN 1 ELSE 0 END) AS expiring_soon,
MIN(w.enddate) AS earliest_expiration,
MAX(w.enddate) AS latest_expiration
FROM warranties w
WHERE w.isactive = 1;
```
### 3.4 View: vw_machines_without_warranty
Find machines with no warranty coverage
```sql
CREATE VIEW vw_machines_without_warranty AS
SELECT
m.machineid,
m.machinenumber,
m.hostname,
m.serialnumber,
mt.machinetype,
mo.modelnumber,
v.vendor AS manufacturer,
m.dateadded,
DATEDIFF(CURDATE(), m.dateadded) AS days_since_added
FROM machines m
LEFT JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
LEFT JOIN warranties w ON m.machineid = w.machineid AND w.isactive = 1 AND w.status = 'Active'
WHERE m.isactive = 1
AND w.warrantyid IS NULL
ORDER BY m.dateadded DESC;
```
---
## Part 4: Data Migration from PC Table
### 4.1 Migrate PC Warranty Data
```sql
-- Step 1: Insert warranty records for all PCs with warranty data
INSERT INTO warranties (
machineid,
warrantytypeid,
warrantyvendorid,
startdate,
enddate,
servicelevel,
servicetag,
status,
isprimary,
lastcheckeddate,
notes,
isactive
)
SELECT
-- Map to new machine ID (assuming PCs have been migrated to machines)
m.machineid,
-- Default to 'Standard Hardware' warranty type
(SELECT warrantytypeid FROM warrantytypes WHERE typename = 'Standard Hardware' LIMIT 1),
-- Map vendor from models table
(SELECT mo.vendorid
FROM models mo
WHERE mo.modelnumberid = p.modelnumberid
LIMIT 1),
-- Calculate start date (assume 3 years before end date, or use dateadded)
COALESCE(DATE_SUB(p.warrantyenddate, INTERVAL 3 YEAR), p.dateadded),
-- End date from PC table
p.warrantyenddate,
-- Service level
p.warrantyservicelevel,
-- Use serial number as service tag
p.serialnumber,
-- Status based on current date
CASE
WHEN p.warrantyenddate < CURDATE() THEN 'Expired'
WHEN p.warrantystatus = 'Unknown' THEN 'Pending'
ELSE 'Active'
END,
-- Set as primary warranty
1,
-- Last checked date
p.warrantylastchecked,
-- Notes
CONCAT('Migrated from PC table. Original status: ', COALESCE(p.warrantystatus, 'Unknown')),
-- Active flag
1
FROM pc p
INNER JOIN machines m ON p.hostname = m.hostname -- Link by hostname after PC migration
WHERE p.warrantyenddate IS NOT NULL;
```
---
## Part 5: SQL Migration Scripts
### 5.1 Script 04: Create Warranty Infrastructure
File: `sql/04_create_warranty_infrastructure.sql`
```sql
-- =====================================================
-- SCRIPT 04: Create Warranty Management Infrastructure
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Create warranty tables and views
-- Status: REVERSIBLE (has rollback script)
-- =====================================================
USE shopdb;
SET SQL_SAFE_UPDATES = 0;
-- Create warrantytypes table
CREATE TABLE IF NOT EXISTS warrantytypes (
warrantytypeid INT(11) PRIMARY KEY AUTO_INCREMENT,
typename VARCHAR(50) NOT NULL UNIQUE,
description VARCHAR(255),
isactive TINYINT(1) DEFAULT 1,
displayorder INT(11) DEFAULT 0,
KEY idx_isactive (isactive),
KEY idx_displayorder (displayorder)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Insert warranty types
INSERT INTO warrantytypes (typename, description, displayorder) VALUES
('Standard Hardware', 'Manufacturer standard warranty', 1),
('Extended Hardware', 'Extended manufacturer warranty', 2),
('Premium Support', 'Premium/ProSupport service level', 3),
('Onsite Service', 'Next business day onsite service', 4),
('Accidental Damage', 'Accidental damage protection', 5),
('Software Support', 'Software/OS support coverage', 6),
('Preventive Maintenance', 'Scheduled preventive maintenance', 7),
('Parts Only', 'Parts replacement only, no labor', 8);
-- Create warranties table
CREATE TABLE IF NOT EXISTS warranties (
warrantyid INT(11) PRIMARY KEY AUTO_INCREMENT,
machineid INT(11) NOT NULL,
warrantytypeid INT(11) NOT NULL,
vendorid INT(11),
startdate DATE NOT NULL,
enddate DATE NOT NULL,
servicelevel VARCHAR(100),
servicetag VARCHAR(100),
ordernumber VARCHAR(100),
coveragenotes TEXT,
cost DECIMAL(10,2),
status VARCHAR(50) DEFAULT 'Active',
isprimary TINYINT(1) DEFAULT 0,
autorenew TINYINT(1) DEFAULT 0,
lastcheckeddate DATETIME,
daysremaining INT(11) GENERATED ALWAYS AS (DATEDIFF(enddate, CURDATE())) VIRTUAL,
notify_60days TINYINT(1) DEFAULT 1,
notify_30days TINYINT(1) DEFAULT 1,
notify_7days TINYINT(1) DEFAULT 1,
notificationemail VARCHAR(255),
notes TEXT,
isactive TINYINT(1) DEFAULT 1,
dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,
lastupdated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
addedby VARCHAR(100),
KEY idx_machineid (machineid),
KEY idx_warrantytypeid (warrantytypeid),
KEY idx_vendorid (vendorid),
KEY idx_enddate (enddate),
KEY idx_status (status),
KEY idx_isprimary (isprimary),
KEY idx_isactive (isactive),
CONSTRAINT fk_warranties_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid),
CONSTRAINT fk_warranties_warrantytypeid FOREIGN KEY (warrantytypeid) REFERENCES warrantytypes(warrantytypeid),
CONSTRAINT fk_warranties_vendorid FOREIGN KEY (vendorid) REFERENCES vendors(vendorid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Create warrantyhistory table
CREATE TABLE IF NOT EXISTS warrantyhistory (
historyid INT(11) PRIMARY KEY AUTO_INCREMENT,
warrantyid INT(11) NOT NULL,
machineid INT(11) NOT NULL,
action VARCHAR(50) NOT NULL,
oldenddate DATE,
newenddate DATE,
oldstatus VARCHAR(50),
newstatus VARCHAR(50),
reason TEXT,
cost DECIMAL(10,2),
actiondate DATETIME DEFAULT CURRENT_TIMESTAMP,
actionby VARCHAR(100),
KEY idx_warrantyid (warrantyid),
KEY idx_machineid (machineid),
KEY idx_actiondate (actiondate),
CONSTRAINT fk_warrantyhistory_warrantyid FOREIGN KEY (warrantyid) REFERENCES warranties(warrantyid),
CONSTRAINT fk_warrantyhistory_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Verification
SELECT 'Warranty infrastructure created' AS status;
SELECT COUNT(*) AS warranty_type_count FROM warrantytypes;
SET SQL_SAFE_UPDATES = 1;
```
### 5.2 Rollback Script 04
File: `sql/ROLLBACK_04_warranty_infrastructure.sql`
```sql
-- =====================================================
-- ROLLBACK 04: Remove Warranty Infrastructure
-- =====================================================
USE shopdb;
SET SQL_SAFE_UPDATES = 0;
-- Drop tables in correct order (FK constraints)
DROP TABLE IF EXISTS warrantyhistory;
DROP TABLE IF EXISTS warranties;
DROP TABLE IF EXISTS warrantytypes;
SELECT 'Warranty infrastructure removed' AS status;
SET SQL_SAFE_UPDATES = 1;
```
---
## Part 6: Benefits of Warranty System
### 6.1 Advantages Over Field-Based Approach
| Feature | Old (fields on machines) | New (warranty tables) |
|---------|-------------------------|----------------------|
| Multiple warranties | ❌ No | ✅ Yes |
| Warranty history | ❌ No | ✅ Yes |
| Renewal tracking | ❌ No | ✅ Yes |
| Cost tracking | ❌ No | ✅ Yes |
| Different vendors | ❌ No | ✅ Yes |
| Auto-notifications | ❌ No | ✅ Yes |
| Reporting | ⚠️ Limited | ✅ Comprehensive |
| Audit trail | ❌ No | ✅ Yes |
### 6.2 Example Use Cases
**Use Case 1: PC with multiple warranties**
- Dell standard 3-year warranty (expires 2026-01-15)
- Extended ProSupport warranty (expires 2027-01-15)
- Accidental damage protection (expires 2026-01-15)
**Use Case 2: CNC Machine**
- Okuma manufacturer warranty (expired 2020-05-01)
- Extended service contract (expires 2026-12-31)
- Preventive maintenance agreement (expires 2025-06-30)
**Use Case 3: Server**
- HP standard warranty (expires 2025-03-15) - Primary
- Extended 24/7 support (expires 2027-03-15)
---
## Part 7: Integration with PC Migration
Update the machines table design from PC_MACHINES_CONSOLIDATION_PLAN.md:
### Remove These Fields:
```sql
-- DO NOT ADD:
-- warrantyenddate
-- warrantystatus
-- warrantydaysremaining
-- warrantyservicelevel
-- warrantylastchecked
```
### Keep Machines Table Clean:
The machines table should NOT have warranty fields. All warranty information will be in the `warranties` table and accessed via JOINs or views.
---
## Next Steps
1. ✅ Review warranty table design
2. ⏳ Create warranty management views
3. ⏳ Create data migration script for PC warranties
4. ⏳ Update PC migration plan to use warranty tables
5. ⏳ Create ASP pages for warranty management
6. ⏳ Create warranty expiration notification system
---
**Document Status**: DRAFT - Ready for Review
**Dependencies**: Requires machines table from PC consolidation
**Production Impact**: New tables, no breaking changes