' ------------------------------------------------------------
' Code generated automatically by Code Architects' VB Migration Partner
' ------------------------------------------------------------

Option Strict Off      ' Code migrated from VB6 has Option Strict disabled by default

<System.Runtime.InteropServices.ProgID("FMStocks_Bus.Account")> _ 
<Transaction(TransactionOption.Required)> _
Public Class Account
	Inherits ServicedComponent

	#Region "Constructor"
	
	'A public default constructor
	Public Sub New()
		' Add initialization code here
	End Sub
	
	#End Region
	
	#Region "Static constructor"
	
	' This static constructor ensures that the VB6 support library be initialized before using current class.
	Shared Sub New()
		EnsureVB6LibraryInitialization()
	End Sub
	
	#End Region

	Private Const m_modName As String = "FMStocks_Bus.Account"
	Private Const m_errDuplicatedAccount As Integer = &H80040E14I
	
	Public Function VerifyLogin(ByVal Email As String, ByVal Password As String, ByRef FullName As Object, ByRef AccountID As Object) As Boolean
		
		On Error GoTo errorHandler 
		VerifyLogin = False
		
		Dim objDB As FMStocks_DB.Account = New FMStocks_DB.Account()
		
		VerifyLogin = objDB.VerifyUser(Email, Password, AccountID, FullName)
		
		objDB = Nothing
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		
		objDB = Nothing
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "VerifyLogin(" & Email & ",...)")
	End Function

	Public Function ListPositionsForSale(ByVal AccountID As Integer) As ADODB.Recordset
		On Error GoTo errorHandler 
		
		Dim obj As FMStocks_DB.Position = New FMStocks_DB.Position()
		
		ListPositionsForSale = obj.ListForSale(AccountID)
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		
		obj = Nothing
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "ListPositionsForSale(" & AccountID & ")")
	End Function

	Public Function ListPositions(ByVal AccountID As Integer) As ADODB.Recordset
		On Error GoTo errorHandler 
		
		Dim objDB As FMStocks_DB.Position = New FMStocks_DB.Position()
		
		ListPositions = objDB.ListSummary(AccountID)
		
		objDB = Nothing
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		
		objDB = Nothing
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "ListPositions(" & AccountID & ")")
	End Function

	Public Function GetSummary(ByVal AccountID As Integer, ByRef MarketValue As Object, ByRef TotalInvestment As Object, ByRef CashBalance As Object) As Object
		
		On Error GoTo errorHandler 
		
		Dim objAccount As FMStocks_DB.Account = New FMStocks_DB.Account()
		
		Dim rs As ADODB.Recordset = objAccount.Summary(AccountID)
		objAccount = Nothing
		
		MarketValue = NullsToZero(rs.Fields("MarketValue").Value)
		TotalInvestment = NullsToZero(rs.Fields("TotalInvestment").Value)
		CashBalance = NullsToZero(rs.Fields("CashBalance").Value)
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		
		MarketValue = 0
		TotalInvestment = 0
		CashBalance = 0
		
		rs = Nothing
		objAccount = Nothing
		
		VB6MtsMethods.GetObjectContext().SetAbort
		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 Integer
		
		On Error GoTo errorHandler 
		
		Dim db As FMStocks_DB.Account = New FMStocks_DB.Account()
		
		Dim defBalance As Decimal = "10000"
		
		Dim NewAccountID As Integer = db.Add(FirstName, LastName, Password, Email, defBalance)

		'set return value
		Add = NewAccountID
		
		db = Nothing
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		
		db = Nothing
		VB6MtsMethods.GetObjectContext().SetAbort
		
		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 Integer, ByRef FullName As String, ByRef Email As String, ByRef Balance As Decimal)
		On Error GoTo errorHandler 
		Dim objDB As FMStocks_DB.Account = New FMStocks_DB.Account()
		
		objDB.GetAccountInfo(AccountID, FullName, Email, Balance)
		objDB = Nothing
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Sub
		
errorHandler:
		
		objDB = Nothing
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "GetAccountInfo(" & AccountID & ")")
	End Sub

End Class
