VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Account"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
Const m_modName As String = "FMStocks_DB.Account"
Private m_dbh As DBHelper

Private Sub Class_Initialize()
    'must call CreateObject or COM+ will
    'not call IObjectConstructor inferface that DBHelper needs
    Set m_dbh = CreateObject("FMStocks_DB.DBHelper")
End Sub

Private Sub Class_Terminate()
    Set m_dbh = Nothing
End Sub

Public Function Add(ByVal FirstName As String, _
            ByVal LastName As String, _
            ByVal password As String, _
            ByVal Email As String, _
            ByVal Balance As Currency) As Long
            
    On Error GoTo errorHandler
        
    Add = m_dbh.RunSPReturnInteger("Account_Add", Array( _
        mp("@FirstName", adVarChar, 30, FirstName), _
        mp("@LastName", adVarChar, 30, LastName), _
        mp("@Password", adVarChar, 15, password), _
        mp("@EMail", adVarChar, 50, Email), _
        mp("@Balance", adCurrency, 8, Balance)))
    
    
    Exit Function
    
errorHandler:
    
    RaiseError m_modName, "Add"
End Function


Public Function Summary(ByVal AccountID As Long) As ADODB.Recordset
    On Error GoTo errorHandler
    
    Set Summary = m_dbh.RunSPReturnRS("Account_Summary", Array( _
        mp("@AccountID", adInteger, 4, AccountID)))
       
    
    Exit Function
        
errorHandler:
    
    RaiseError m_modName, "Summary(" & AccountID & ")"
End Function


Public Function VerifyUser(ByVal Email As String, ByVal password As String, _
    ByRef AccountID As Variant, ByRef FullName As Variant) As Boolean
    
    On Error GoTo errorHandler
    Dim strUserInfo As String
              
    ' We couldn't use the generic RunSP* functions because this stored proc has 2 output parameters
    ' The following code is a modified version of RunSPWithString to support 2 outputs and 1 return value

    Dim cmd As ADODB.Command
    Set cmd = New ADODB.Command
    
    'Run the procedure
    With cmd
         Dim connectionString as string
         connectionString = m_dbh.GetConnectionString()
        .ActiveConnection = connectionString
        .CommandText = "Account_VerifyLogin"
        .CommandType = adCmdStoredProc

        'set Params
        .Parameters.Append .CreateParameter("@retval", adInteger, adParamOutput)
        .Parameters.Append .CreateParameter("@EMail", adVarChar, adParamInput, 50, Email)
        .Parameters.Append .CreateParameter("@Password", adVarChar, adParamInput, 50, password)
        .Parameters.Append .CreateParameter("@AccountID", adInteger, adParamOutput, 4)
        .Parameters.Append .CreateParameter("@FullName", adVarChar, adParamOutput, 50)
    
        'execute Stored Proc
        .Execute , , ADODB.adExecuteNoRecords
        
        If .Parameters("@retval").Value = 0 Then
            VerifyUser = False
        Else
            AccountID = .Parameters(3).Value
            FullName = .Parameters(4).Value
            VerifyUser = True
        End If
    End With
     
    Set cmd = Nothing
    
    
    Exit Function

errorHandler:
    Set cmd = Nothing
    
    RaiseError m_modName, "VerifyUser(" & Email & ",...)" & "  [** TURN OFF FIREWALL **]"
End Function

Public Sub GetAccountInfo(ByVal AccountID As Long, ByRef FullName As Variant, ByRef Email As Variant, ByRef Balance As Variant)
    On Error GoTo errorHandler
    
    Dim rs As Recordset

    Set rs = m_dbh.RunSPReturnRS("Account_GetInfo", Array(mp("AccountID", adInteger, 4, AccountID)))

    FullName = rs("Firstname") & " " & rs("Lastname")
    Email = rs("email")
    Balance = rs("Balance")

    Set rs = Nothing
    Exit Sub
    
errorHandler:
    Set rs = Nothing
    
    RaiseError m_modName, "GetAccountInfo(" & AccountID & ")"
End Sub
