From c1f4412a52985676ebe69147582f3701f4781f2c Mon Sep 17 00:00:00 2001 From: cproudlock Date: Mon, 27 Apr 2026 17:10:33 -0400 Subject: [PATCH] api.asp: archive stale PC->equipment relationships on new PC self-registration When a new PC POSTs updateCompleteAsset for a machine number, the previous PC's "Controls" relationship to that equipment is now soft-archived (isactive=0) before the new relationship is created. Prevents duplicate active relationships when PCs are replaced floor-wide. Display pages already filter on isactive=1 so archived rows just disappear from inventory views (history preserved). displaymachine.asp: added Controller field populated via the controllertypes join. displaypc.asp: fixed top-bar search bug - searching a machine number was returning the PC row instead of the machine. Three lookup queries now scope to pctypeid IS NOT NULL. Co-Authored-By: Claude Opus 4.7 (1M context) --- api.asp | 21 ++++++++++++++++++++- displaymachine.asp | 9 ++++++++- displaypc.asp | 6 +++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/api.asp b/api.asp index e9c35ef..380a52b 100644 --- a/api.asp +++ b/api.asp @@ -1816,9 +1816,28 @@ Function CreatePCMachineRelationship(pcMachineid, machineNumber) rsResult.Close Set rsResult = Nothing + ' Archive any existing active "Controls" relationships from OTHER PCs to + ' this equipment. When a new PC self-registers for a machine number, the + ' previous PC's relationship is stale (PC was replaced). Soft-archive via + ' isactive=0 keeps history but excludes from active inventory views (the + ' display pages already filter on isactive=1). + Dim archiveSQL + archiveSQL = "UPDATE machinerelationships " & _ + "SET isactive = 0, lastupdated = NOW() " & _ + "WHERE machineid = " & CLng(equipmentMachineid) & _ + " AND related_machineid <> " & CLng(pcMachineid) & _ + " AND relationshiptypeid = " & CLng(relationshiptypeid) & _ + " AND isactive = 1" + LogToFile "CreatePCMachineRelationship: Archiving stale relationships: " & archiveSQL + objConn.Execute archiveSQL + If Err.Number <> 0 Then + LogToFile "CreatePCMachineRelationship: WARNING archive failed: " & Err.Description + Err.Clear + End If + ' Check if relationship already exists (Equipment -> PC, matching existing data pattern) strSQL = "SELECT relationshipid FROM machinerelationships " & _ - "WHERE machineid = " & CLng(equipmentMachineid) & " AND related_machineid = " & CLng(pcMachineid) & " AND relationshiptypeid = " & CLng(relationshiptypeid) + "WHERE machineid = " & CLng(equipmentMachineid) & " AND related_machineid = " & CLng(pcMachineid) & " AND relationshiptypeid = " & CLng(relationshiptypeid) & " AND isactive = 1" LogToFile "CreatePCMachineRelationship: Checking for duplicate: " & strSQL Set rsResult = objConn.Execute(strSQL) diff --git a/displaymachine.asp b/displaymachine.asp index 22fa78b..ae66fc5 100644 --- a/displaymachine.asp +++ b/displaymachine.asp @@ -89,12 +89,14 @@ "businessunits.businessunit, " & _ "functionalaccounts.functionalaccount, functionalaccounts.functionalaccountid, " & _ "vendors.vendor, vendors.vendorid, " & _ + "controllertypes.controllertype, " & _ "printers.ipaddress AS printerip, " & _ "printers.printercsfname, printers.printerwindowsname, " & _ "machinestatus.machinestatus " & _ "FROM machines " & _ "LEFT JOIN models ON machines.modelnumberid = models.modelnumberid " & _ "LEFT JOIN machinetypes ON models.machinetypeid = machinetypes.machinetypeid " & _ + "LEFT JOIN controllertypes ON machines.controllertypeid = controllertypes.controllertypeid " & _ "LEFT JOIN businessunits ON machines.businessunitid = businessunits.businessunitid " & _ "LEFT JOIN functionalaccounts ON machinetypes.functionalaccountid = functionalaccounts.functionalaccountid " & _ "LEFT JOIN vendors ON models.vendorid = vendors.vendorid " & _ @@ -191,6 +193,7 @@

Model:

Function:

BU:

+

Controller:

IP Address:

MAC Address:

FQDN:

@@ -202,7 +205,7 @@
<% -Dim machineNumVal, vendorValM, modelValM, machineTypeVal, buVal, statusValM +Dim machineNumVal, vendorValM, modelValM, machineTypeVal, buVal, statusValM, controllerVal ' Get values and default to N/A if empty machineNumVal = rs("machinenumber") & "" @@ -222,6 +225,9 @@ If machineTypeVal = "" Then machineTypeVal = "N/A" buVal = rs("businessunit") & "" If buVal = "" Then buVal = "N/A" + +controllerVal = rs("controllertype") & "" +If controllerVal = "" Then controllerVal = "N/A" %>

<% @@ -241,6 +247,7 @@ End If

<%=Server.HTMLEncode(modelValM)%>

<%=Server.HTMLEncode(machineTypeVal)%>

<%=Server.HTMLEncode(buVal)%>

+

<%=Server.HTMLEncode(controllerVal)%>

<% ' Get primary communication (IP and MAC) from communications table Dim rsPrimaryCom, strPrimaryComSQL, primaryIP, primaryMAC diff --git a/displaypc.asp b/displaypc.asp index 56adb09..64f1839 100644 --- a/displaypc.asp +++ b/displaypc.asp @@ -37,7 +37,7 @@ IF machinenumber <> "" THEN ' Look up machineid by machinenumber Dim rsLookup, strLookupSQL - strLookupSQL = "SELECT machineid FROM machines WHERE machinenumber = ? AND isactive = 1" + strLookupSQL = "SELECT machineid FROM machines WHERE machinenumber = ? AND isactive = 1 AND pctypeid IS NOT NULL" Set rsLookup = ExecuteParameterizedQuery(objConn, strLookupSQL, Array(machinenumber)) IF NOT rsLookup.EOF THEN machineid = rsLookup("machineid") @@ -49,13 +49,13 @@ ' We have a machineid, but it might actually be a machine number ' Try to look it up as a machineid first Dim rsCheck - strLookupSQL = "SELECT machineid FROM machines WHERE machineid = ? AND isactive = 1" + strLookupSQL = "SELECT machineid FROM machines WHERE machineid = ? AND isactive = 1 AND pctypeid IS NOT NULL" Set rsCheck = ExecuteParameterizedQuery(objConn, strLookupSQL, Array(machineid)) ' If no machine found with that machineid, try treating it as a machine number IF rsCheck.EOF THEN rsCheck.Close - strLookupSQL = "SELECT machineid FROM machines WHERE machinenumber = ? AND isactive = 1" + strLookupSQL = "SELECT machineid FROM machines WHERE machinenumber = ? AND isactive = 1 AND pctypeid IS NOT NULL" Set rsCheck = ExecuteParameterizedQuery(objConn, strLookupSQL, Array(CStr(machineid))) IF NOT rsCheck.EOF THEN machineid = rsCheck("machineid")