<%
' Auto-generated Repository for table [OrderDetails]
' Generated on 8/20/2026 11:48:14 AM
' Generator: GenerateRepo.vbs v1.0
'
' Dependencies:
'   - core/lib.DAL.asp (DAL singleton for database access)
'   - core/lib.AutoMapper.asp (Automapper for object mapping)
'   - core/lib.Collections.asp (LinkedList_Class)
'   - core/lib.helpers.asp (KVUnzip, BuildOrderBy, QI, Destroy)


Class OrderDetailsRepository_Class

    Public Function FindByID(id)
        Dim sql : sql = "Select [BlueDeliveryDate], [BlueEnvelopeProvider], [BlueHasNonprofitStatus], [BlueNonprofitAuthCode], [BluePermitCity], [BluePermitClass], [BluePermitNumber], [BluePermitOwnership], [BluePrintPermit], [BluePrintReturnAddress], [BlueWantsQuote], [ContactName], [FutureVoterStickerRolls], [IVotedStickerRolls], [MailingEstimatedQuantity], [MailingHasNonprofitStatus], [MailingPickupDate], [MailingPostageOption], [MailingWantsService], [MailingWantsSorting], [Municipality], [OrderDetailID], [OrderID], [Phone], [PurpleBlueProvider], [PurpleBlueProviderOther], [PurpleColorCodingBy], [PurpleColorNames], [PurpleDeliveryDate], [PurpleEnvelopeProvider], [PurpleEnvelopeStock], [PurpleExtraEnvelopeQty], [PurplePermanentBallotListCount], [PurplePrecinctCount], [PurplePrintOption], [PurplePrintStyle], [PurpleTrackMIBallot], [PurpleWantsBlueEnvelopes], [PurpleWantsQuote], [SecrecySleevesQty], [SpecialRequests], [SubmittedAt] FROM [OrderDetails] WHERE [OrderDetailID] = ?"
        Dim rs : Set rs = DAL.Query(sql, Array(id))
        If rs.EOF Then
            Err.Raise 1, "OrderDetailsRepository_Class", RecordNotFoundException("OrderDetailID", id)
        Else
            Set FindByID = Automapper.AutoMap(rs, "POBO_OrderDetails")
        End If
        Destroy rs
    End Function

    Public Function GetAll(orderBy)
        Set GetAll = Find(Empty, orderBy)
    End Function

    Public Function Find(where_kvarray, order_string_or_array)
        Dim sql : sql = "Select [BlueDeliveryDate], [BlueEnvelopeProvider], [BlueHasNonprofitStatus], [BlueNonprofitAuthCode], [BluePermitCity], [BluePermitClass], [BluePermitNumber], [BluePermitOwnership], [BluePrintPermit], [BluePrintReturnAddress], [BlueWantsQuote], [ContactName], [FutureVoterStickerRolls], [IVotedStickerRolls], [MailingEstimatedQuantity], [MailingHasNonprofitStatus], [MailingPickupDate], [MailingPostageOption], [MailingWantsService], [MailingWantsSorting], [Municipality], [OrderDetailID], [OrderID], [Phone], [PurpleBlueProvider], [PurpleBlueProviderOther], [PurpleColorCodingBy], [PurpleColorNames], [PurpleDeliveryDate], [PurpleEnvelopeProvider], [PurpleEnvelopeStock], [PurpleExtraEnvelopeQty], [PurplePermanentBallotListCount], [PurplePrecinctCount], [PurplePrintOption], [PurplePrintStyle], [PurpleTrackMIBallot], [PurpleWantsBlueEnvelopes], [PurpleWantsQuote], [SecrecySleevesQty], [SpecialRequests], [SubmittedAt] FROM [OrderDetails]"
        Dim where_keys, where_values, i
        If Not IsEmpty(where_kvarray) Then
            KVUnzip where_kvarray, where_keys, where_values
            If Not IsEmpty(where_keys) Then
                sql = sql & " WHERE "
                For i = 0 To UBound(where_keys)
                    If i > 0 Then sql = sql & " AND "
                    sql = sql & " " & QI(where_keys(i)) & " = ?"
                Next
            End If
        End If
        sql = sql & BuildOrderBy(order_string_or_array, "[OrderDetailID]")
        Dim rs : Set rs = DAL.Query(sql, where_values)
        Dim list : Set list = new LinkedList_Class
        Do Until rs.EOF
            list.Push Automapper.AutoMap(rs, "POBO_OrderDetails")
            rs.MoveNext
        Loop
        Set Find = list
        Destroy rs
    End Function

    Public Function FindPaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)
        Dim sql : sql = "Select [BlueDeliveryDate], [BlueEnvelopeProvider], [BlueHasNonprofitStatus], [BlueNonprofitAuthCode], [BluePermitCity], [BluePermitClass], [BluePermitNumber], [BluePermitOwnership], [BluePrintPermit], [BluePrintReturnAddress], [BlueWantsQuote], [ContactName], [FutureVoterStickerRolls], [IVotedStickerRolls], [MailingEstimatedQuantity], [MailingHasNonprofitStatus], [MailingPickupDate], [MailingPostageOption], [MailingWantsService], [MailingWantsSorting], [Municipality], [OrderDetailID], [OrderID], [Phone], [PurpleBlueProvider], [PurpleBlueProviderOther], [PurpleColorCodingBy], [PurpleColorNames], [PurpleDeliveryDate], [PurpleEnvelopeProvider], [PurpleEnvelopeStock], [PurpleExtraEnvelopeQty], [PurplePermanentBallotListCount], [PurplePrecinctCount], [PurplePrintOption], [PurplePrintStyle], [PurpleTrackMIBallot], [PurpleWantsBlueEnvelopes], [PurpleWantsQuote], [SecrecySleevesQty], [SpecialRequests], [SubmittedAt] FROM [OrderDetails]"
        Dim where_keys, where_values, i
        If Not IsEmpty(where_kvarray) Then
            KVUnzip where_kvarray, where_keys, where_values
            If Not IsEmpty(where_keys) Then
                sql = sql & " WHERE "
                For i = 0 To UBound(where_keys)
                    If i > 0 Then sql = sql & " AND "
                    sql = sql & " " & QI(where_keys(i)) & " = ?"
                Next
            End If
        End If
        sql = sql & BuildOrderBy(order_string_or_array, "[OrderDetailID]")
        Dim rs : Set rs = DAL.PagedQuery(sql, where_values, per_page, page_num)
        If Not rs.EOF Then
            rs.PageSize     = per_page
            rs.AbsolutePage = page_num
            page_count      = rs.PageCount
            record_count    = rs.RecordCount
        End If
        Set FindPaged = PagedList(rs, per_page)
        Destroy rs
    End Function

    Public Function SearchTablePaged(columns_array, search_value, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)
        Dim sql : sql = "Select [BlueDeliveryDate], [BlueEnvelopeProvider], [BlueHasNonprofitStatus], [BlueNonprofitAuthCode], [BluePermitCity], [BluePermitClass], [BluePermitNumber], [BluePermitOwnership], [BluePrintPermit], [BluePrintReturnAddress], [BlueWantsQuote], [ContactName], [FutureVoterStickerRolls], [IVotedStickerRolls], [MailingEstimatedQuantity], [MailingHasNonprofitStatus], [MailingPickupDate], [MailingPostageOption], [MailingWantsService], [MailingWantsSorting], [Municipality], [OrderDetailID], [OrderID], [Phone], [PurpleBlueProvider], [PurpleBlueProviderOther], [PurpleColorCodingBy], [PurpleColorNames], [PurpleDeliveryDate], [PurpleEnvelopeProvider], [PurpleEnvelopeStock], [PurpleExtraEnvelopeQty], [PurplePermanentBallotListCount], [PurplePrecinctCount], [PurplePrintOption], [PurplePrintStyle], [PurpleTrackMIBallot], [PurpleWantsBlueEnvelopes], [PurpleWantsQuote], [SecrecySleevesQty], [SpecialRequests], [SubmittedAt] FROM [OrderDetails]"
        Dim i, params()
        If IsArray(columns_array) And UBound(columns_array) >= 0 Then
            sql = sql & " WHERE "
            ReDim params(UBound(columns_array))
            For i = 0 To UBound(columns_array)
                If i > 0 Then sql = sql & " OR "
                sql = sql & " " & QI(columns_array(i)) & " LIKE ?"
                params(i) = "%" & search_value & "%"
            Next
        End If
        sql = sql & BuildOrderBy(order_string_or_array, "[OrderDetailID]")
        Dim rs : Set rs = DAL.PagedQuery(sql, params, per_page, page_num)
        If Not rs.EOF Then
            rs.PageSize     = per_page
            rs.AbsolutePage = page_num
            page_count      = rs.PageCount
            record_count    = rs.RecordCount
        End If
        Set SearchTablePaged = PagedList(rs, per_page)
        Destroy rs
    End Function

    Private Function PagedList(rs, per_page)
        Dim list : Set list = new LinkedList_Class
        Dim x : x = 0
        Do While (per_page <= 0 Or x < per_page) And Not rs.EOF
            list.Push Automapper.AutoMap(rs, "POBO_OrderDetails")
            x = x + 1
            rs.MoveNext
        Loop
        Set PagedList = list
    End Function

    Public Sub AddNew(ByRef model)
        Dim sql : sql = "INSERT INTO [OrderDetails] ([BlueDeliveryDate], [BlueEnvelopeProvider], [BlueHasNonprofitStatus], [BlueNonprofitAuthCode], [BluePermitCity], [BluePermitClass], [BluePermitNumber], [BluePermitOwnership], [BluePrintPermit], [BluePrintReturnAddress], [BlueWantsQuote], [ContactName], [FutureVoterStickerRolls], [IVotedStickerRolls], [MailingEstimatedQuantity], [MailingHasNonprofitStatus], [MailingPickupDate], [MailingPostageOption], [MailingWantsService], [MailingWantsSorting], [Municipality], [OrderID], [Phone], [PurpleBlueProvider], [PurpleBlueProviderOther], [PurpleColorCodingBy], [PurpleColorNames], [PurpleDeliveryDate], [PurpleEnvelopeProvider], [PurpleEnvelopeStock], [PurpleExtraEnvelopeQty], [PurplePermanentBallotListCount], [PurplePrecinctCount], [PurplePrintOption], [PurplePrintStyle], [PurpleTrackMIBallot], [PurpleWantsBlueEnvelopes], [PurpleWantsQuote], [SecrecySleevesQty], [SpecialRequests], [SubmittedAt]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        DAL.Execute sql, Array(model.BlueDeliveryDate, model.BlueEnvelopeProvider, model.BlueHasNonprofitStatus, model.BlueNonprofitAuthCode, model.BluePermitCity, model.BluePermitClass, model.BluePermitNumber, model.BluePermitOwnership, model.BluePrintPermit, model.BluePrintReturnAddress, model.BlueWantsQuote, model.ContactName, model.FutureVoterStickerRolls, model.IVotedStickerRolls, model.MailingEstimatedQuantity, model.MailingHasNonprofitStatus, model.MailingPickupDate, model.MailingPostageOption, model.MailingWantsService, model.MailingWantsSorting, model.Municipality, model.OrderID, model.Phone, model.PurpleBlueProvider, model.PurpleBlueProviderOther, model.PurpleColorCodingBy, model.PurpleColorNames, model.PurpleDeliveryDate, model.PurpleEnvelopeProvider, model.PurpleEnvelopeStock, model.PurpleExtraEnvelopeQty, model.PurplePermanentBallotListCount, model.PurplePrecinctCount, model.PurplePrintOption, model.PurplePrintStyle, model.PurpleTrackMIBallot, model.PurpleWantsBlueEnvelopes, model.PurpleWantsQuote, model.SecrecySleevesQty, model.SpecialRequests, model.SubmittedAt)

        ' Retrieve the newly inserted ID
        On Error Resume Next
        Dim rsId : Set rsId = DAL.Query("SELECT @@IDENTITY AS NewID", Empty)
        If Err.Number <> 0 Then
            ' Fallback for Access databases
            Err.Clear
            Set rsId = DAL.Query("SELECT TOP 1 [OrderDetailID] FROM [OrderDetails] ORDER BY [OrderDetailID] DESC", Empty)
        End If
        On Error GoTo 0

        If Not rsId.EOF Then
            If Not IsNull(rsId(0)) Then model.OrderDetailID = rsId(0)
        End If
        Destroy rsId
    End Sub

    Public Sub Update(model)
        Dim sql : sql = "UPDATE [OrderDetails] SET [BlueDeliveryDate] = ?, [BlueEnvelopeProvider] = ?, [BlueHasNonprofitStatus] = ?, [BlueNonprofitAuthCode] = ?, [BluePermitCity] = ?, [BluePermitClass] = ?, [BluePermitNumber] = ?, [BluePermitOwnership] = ?, [BluePrintPermit] = ?, [BluePrintReturnAddress] = ?, [BlueWantsQuote] = ?, [ContactName] = ?, [FutureVoterStickerRolls] = ?, [IVotedStickerRolls] = ?, [MailingEstimatedQuantity] = ?, [MailingHasNonprofitStatus] = ?, [MailingPickupDate] = ?, [MailingPostageOption] = ?, [MailingWantsService] = ?, [MailingWantsSorting] = ?, [Municipality] = ?, [OrderID] = ?, [Phone] = ?, [PurpleBlueProvider] = ?, [PurpleBlueProviderOther] = ?, [PurpleColorCodingBy] = ?, [PurpleColorNames] = ?, [PurpleDeliveryDate] = ?, [PurpleEnvelopeProvider] = ?, [PurpleEnvelopeStock] = ?, [PurpleExtraEnvelopeQty] = ?, [PurplePermanentBallotListCount] = ?, [PurplePrecinctCount] = ?, [PurplePrintOption] = ?, [PurplePrintStyle] = ?, [PurpleTrackMIBallot] = ?, [PurpleWantsBlueEnvelopes] = ?, [PurpleWantsQuote] = ?, [SecrecySleevesQty] = ?, [SpecialRequests] = ?, [SubmittedAt] = ? WHERE [OrderDetailID] = ?"
        DAL.Execute sql, Array(model.BlueDeliveryDate, model.BlueEnvelopeProvider, model.BlueHasNonprofitStatus, model.BlueNonprofitAuthCode, model.BluePermitCity, model.BluePermitClass, model.BluePermitNumber, model.BluePermitOwnership, model.BluePrintPermit, model.BluePrintReturnAddress, model.BlueWantsQuote, model.ContactName, model.FutureVoterStickerRolls, model.IVotedStickerRolls, model.MailingEstimatedQuantity, model.MailingHasNonprofitStatus, model.MailingPickupDate, model.MailingPostageOption, model.MailingWantsService, model.MailingWantsSorting, model.Municipality, model.OrderID, model.Phone, model.PurpleBlueProvider, model.PurpleBlueProviderOther, model.PurpleColorCodingBy, model.PurpleColorNames, model.PurpleDeliveryDate, model.PurpleEnvelopeProvider, model.PurpleEnvelopeStock, model.PurpleExtraEnvelopeQty, model.PurplePermanentBallotListCount, model.PurplePrecinctCount, model.PurplePrintOption, model.PurplePrintStyle, model.PurpleTrackMIBallot, model.PurpleWantsBlueEnvelopes, model.PurpleWantsQuote, model.SecrecySleevesQty, model.SpecialRequests, model.SubmittedAt, model.OrderDetailID)
    End Sub

    Public Sub Delete(id)
        Dim sql : sql = "DELETE FROM [OrderDetails] WHERE [OrderDetailID] = ?"
        DAL.Execute sql, Array(id)
    End Sub

    ' Returns the POBO_OrderDetails row for the given OrderID, or Nothing if none exists yet.
    Public Function FindByOrderID(orderId)
        Dim rs : Set rs = DAL.Query("SELECT [OrderDetailID] FROM [OrderDetails] WHERE [OrderID] = ?", Array(orderId))
        If rs.EOF Then
            Set FindByOrderID = Nothing
        Else
            Set FindByOrderID = FindByID(rs("OrderDetailID").Value)
        End If
        Destroy rs
    End Function

    ' Inserts a new OrderDetails row for orderId if one doesn't exist yet, or updates the
    ' existing one (survey answers are 1:1 with an Order - re-submitting replaces them).
    Public Sub SaveForOrder(orderId, model)
        Dim existing : Set existing = FindByOrderID(orderId)
        model.OrderID = orderId

        If existing Is Nothing Then
            AddNew model
        Else
            model.OrderDetailID = existing.OrderDetailID
            Update model
        End If
    End Sub

    Private Function RecordNotFoundException(ByVal field_name, ByVal field_val)
        RecordNotFoundException = "OrderDetails record was not found with " & field_name & " = '" & field_val & "'."
    End Function

    Private Function QI(name)
        QI = "[" & Replace(CStr(name), "]", "]]") & "]"
    End Function

    Private Function BuildOrderBy(orderArg, defaultCol)
        Dim s : s = ""
        If IsEmpty(orderArg) Or IsNull(orderArg) Or orderArg = "" Then
            s = " ORDER BY " & defaultCol & " ASC"
        ElseIf IsArray(orderArg) Then
            Dim i : s = " ORDER BY "
            For i = 0 To UBound(orderArg)
                If i > 0 Then s = s & ", "
                s = s & QI(orderArg(i))
            Next
        Else
            s = " ORDER BY " & QI(orderArg)
        End If
        BuildOrderBy = s
    End Function
End Class

Dim OrderDetailsRepository__Singleton
Function OrderDetailsRepository()
    If IsEmpty(OrderDetailsRepository__Singleton) Then
        Set OrderDetailsRepository__Singleton = new OrderDetailsRepository_Class
    End If
    Set OrderDetailsRepository = OrderDetailsRepository__Singleton
End Function
%>
