VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Broker"
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.Broker"
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

' Returns the PositionID or 0 if it failed
Public Function Buy(ByVal TxID As Long, _
    ByVal AccountID As Long, _
    ByVal Ticker As String, _
    ByVal Shares As Long, _
    ByVal Price As Currency, _
    ByVal Commission As Currency) As Long
    
    On Error GoTo errorHandler
    
    Dim x As Variant
    
    x = m_dbh.RunSPReturnInteger("Broker_Buy", Array( _
        mp("@TxID", adInteger, 4, TxID), _
        mp("@AccountID", adInteger, 4, AccountID), _
        mp("@Ticker", adChar, 12, Ticker), _
        mp("@Shares", adInteger, 4, Shares), _
        mp("@Price", adCurrency, 8, Price), _
        mp("@Commission", adCurrency, 8, Commission)))
        
    Buy = NullsToZero(x) 'changes a null return value into a zero, or returns x
    
    
    Exit Function
    
errorHandler:
    Buy = 0
    
    RaiseError m_modName, "Buy(" & TxID & "," & AccountID _
               & "," & Ticker & "," & Shares _
               & "," & Price & "," & Commission & ")"
End Function
    
    
Public Function Sell(ByVal TxID As Long, _
    ByVal Price As Currency, _
    ByVal Commission As Currency) As Boolean
    
    On Error GoTo errorHandler
    Sell = True
    
    Dim x As Variant
    x = m_dbh.RunSPReturnInteger("Broker_Sell", Array( _
        mp("@TxID", adInteger, 4, TxID), _
        mp("@Price", adCurrency, 8, Price), _
        mp("@Commission", adCurrency, 8, Commission)))
    
    If x = 0 Then
        Sell = False
        GoTo errorHandler
    Else
        Sell = True
    End If
       
    
    Exit Function
    
errorHandler:
    Sell = False
    
    RaiseError m_modName, "Sell(" & TxID & "," & Price & "," & Commission & ")"
End Function

