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_Bus.Account"
Const m_errDuplicatedAccount As Long = &H80040E14

Public Function VerifyLogin(ByVal Email As String, ByVal Password As String, _
    ByRef FullName As Variant, ByRef AccountID As Variant) As Boolean

    On Error GoTo errorHandler
    VerifyLogin = False

    Dim objDB As FMStocks_DB.Account
    Set objDB = New FMStocks_DB.Account

    VerifyLogin = objDB.VerifyUser(Email, Password, AccountID, FullName)

    Set objDB = Nothing

    
    Exit Function

errorHandler:
    
    Set objDB = Nothing
    
    RaiseError m_modName, "VerifyLogin(" & Email & ",...)"
End Function

Public Function ListPositionsForSale(ByVal AccountID As Long) As Recordset
    On Error GoTo errorHandler
    
    Dim obj As FMStocks_DB.Position
    Set obj = New FMStocks_DB.Position
    
    Set ListPositionsForSale = obj.ListForSale(AccountID)
    
    
    Exit Function

errorHandler:
    
    Set obj = Nothing
    
    RaiseError m_modName, "ListPositionsForSale(" & AccountID & ")"
End Function

Public Function ListPositions(ByVal AccountID As Long) As Recordset
    On Error GoTo errorHandler
    
    Dim objDB As FMStocks_DB.Position
    Set objDB = New FMStocks_DB.Position
    
    Set ListPositions = objDB.ListSummary(AccountID)
    
    Set objDB = Nothing
    
    Exit Function

errorHandler:
    
    Set objDB = Nothing
    
    RaiseError m_modName, "ListPositions(" & AccountID & ")"
End Function

Public Function GetSummary(ByVal AccountID As Long, _
    ByRef MarketValue As Variant, _
    ByRef TotalInvestment As Variant, _
    ByRef CashBalance As Variant)

    On Error GoTo errorHandler
        
    Dim objAccount As FMStocks_DB.Account
    Set objAccount = New FMStocks_DB.Account
    
    Dim rs As ADODB.Recordset
    Set rs = objAccount.Summary(AccountID)
    Set objAccount = Nothing
     
    MarketValue = NullsToZero(rs("MarketValue").Value)
    TotalInvestment = NullsToZero(rs("TotalInvestment").Value)
    CashBalance = NullsToZero(rs("CashBalance").Value)
    
    
    Exit Function

errorHandler:

    MarketValue = 0
    TotalInvestment = 0
    CashBalance = 0
    
    Set rs = Nothing
    Set objAccount = Nothing
    
    
    RaiseError m_modName, "GetSummary(" & AccountID & ")"
End Function

Public Function Add(ByVal FirstName As String, _
            ByVal LastName As String, _
            ByVal Password As String, _
            ByVal Email As String) As Long
            
    On Error GoTo errorHandler
    
    Dim db As FMStocks_DB.Account
    Set db = New FMStocks_DB.Account
      
    Dim defBalance As Currency
    defBalance = "10000"
    
    Dim NewAccountID As Long
        
    NewAccountID = db.Add(FirstName, LastName, Password, Email, defBalance)
    
    'set return value
    Add = NewAccountID
    
    Set db = Nothing
    
    Exit Function
    
errorHandler:
    
    Set db = Nothing
    
    
    If Err.Number <> m_errDuplicatedAccount Then
        '-- Only raise an error if not trying to add a duplicate account...something we can handle
        RaiseError m_modName, "Add(" & FirstName & "," & LastName & _
                              ",...," & Email & ")"
    End If
    
End Function

Public Sub GetAccountInfo(ByVal AccountID As Long, ByRef FullName As String, ByRef Email As String, ByRef Balance As Currency)
    On Error GoTo errorHandler
    Dim objDB As FMStocks_DB.Account
    Set objDB = New FMStocks_DB.Account
    
    objDB.GetAccountInfo AccountID, FullName, Email, Balance
    Set objDB = Nothing

    
    Exit Sub

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

Private Sub Class_Initialize()
    Debug.Print "INIT"
End Sub
