' ------------------------------------------------------------
' 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_DB.Broker")> _ 
<Transaction(TransactionOption.Supported)> _
Public Class Broker
	Inherits ServicedComponent

	#Region "Constructor"
	
	'A public default constructor
	Public Sub New()
		Class_Initialize_VB6()
		' 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_DB.Broker"
	Private m_dbh As DBHelper
	
	Private Sub Class_Initialize_VB6()
		'must call CreateObject or COM+ will
		'not call IObjectConstructor inferface that DBHelper needs
		m_dbh = CreateObject6("FMStocks_DB.DBHelper")
	End Sub

	Private Sub Class_Terminate_VB6()
		m_dbh = Nothing
	End Sub

	' Returns the PositionID or 0 if it failed
	Public Function Buy(ByVal TxID As Integer, ByVal AccountID As Integer, ByVal Ticker As String, ByVal Shares As Integer, ByVal Price As Decimal, ByVal Commission As Decimal) As Integer
		
		On Error GoTo errorHandler 
		
		Dim x As Object = m_dbh.RunSPReturnInteger("Broker_Buy", Array6(mp("@TxID", ADODB.DataTypeEnum.adInteger, 4, TxID), mp("@AccountID", ADODB.DataTypeEnum.adInteger, 4, AccountID), mp("@Ticker", ADODB.DataTypeEnum.adChar, 12, Ticker), mp("@Shares", ADODB.DataTypeEnum.adInteger, 4, Shares), mp("@Price", ADODB.DataTypeEnum.adCurrency, 8, Price), mp("@Commission", ADODB.DataTypeEnum.adCurrency, 8, Commission)))

		Buy = NullsToZero(x) 'changes a null return value into a zero, or returns x
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		Buy = 0
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "Buy(" & TxID & "," & AccountID & "," & Ticker & "," & Shares & "," & Price & "," & Commission & ")")
	End Function

	Public Function Sell(ByVal TxID As Integer, ByVal Price As Decimal, ByVal Commission As Decimal) As Boolean
		
		On Error GoTo errorHandler 
		Sell = True
		
		Dim x As Object = m_dbh.RunSPReturnInteger("Broker_Sell", Array6(mp("@TxID", ADODB.DataTypeEnum.adInteger, 4, TxID), mp("@Price", ADODB.DataTypeEnum.adCurrency, 8, Price), mp("@Commission", ADODB.DataTypeEnum.adCurrency, 8, Commission)))
		
		If x = 0 Then 
			Sell = False
			GoTo errorHandler
		Else
			Sell = True
		End If
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		Sell = False
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "Sell(" & TxID & "," & Price & "," & Commission & ")")
	End Function

	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
				Class_Terminate_VB6()
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

End Class
