Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace FMStocks_DB
   Public Class Broker : Implements System.IDisposable
   Dim _disposed As Boolean = False
   Private Const m_modName As String = "FMStocks_DB.Broker"
   Private m_dbh As DBHelper = Nothing
   Public Sub New()
      MyBase.New()
      Class_Initialize()
   End Sub
   Private Sub Class_Initialize()
      'must call CreateObject or COM+ will
      'not call IObjectConstructor inferface that DBHelper needs
      m_dbh = New FMStocks_DB.DBHelper
   End Sub
   Public Sub Dispose() Implements System.IDisposable.Dispose
      Dispose(True)
      GC.SuppressFinalize(Me)
   End Sub
   Protected Overridable Sub Dispose(disposing As Boolean)
      If _disposed Then
         Return
      End If
      If (disposing) Then
         Class_Terminate()
      End If
      _disposed = True
   End Sub
   Private Sub Class_Terminate()
      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
      Buy = 0

      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         Dim x As Integer = 0

         x = m_dbh.RunSPReturnInteger("Broker_Buy",New Object(){Helpers.mp("@TxID",System.Data.SqlDbType.Int,4,TxID),Helpers.mp("@AccountID",System.Data.SqlDbType.Int,4,AccountID),Helpers.mp("@Ticker",System.Data.SqlDbType.Char,12,Ticker),Helpers.mp("@Shares",System.Data.SqlDbType.Int,4,Shares),Helpers.mp("@Price",System.Data.SqlDbType.Money,8,Price),Helpers.mp("@Commission",System.Data.SqlDbType.Money,8,Commission)})

         Buy = CInt(Helpers.NullsToZero(x)) 'changes a null return value into a zero, or returns x


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         Buy = 0

         Helpers.RaiseError(m_modName,"Buy(" & TxID & "," & AccountID & "," & Ticker & "," & Shares & "," & Price & "," & Commission & ")")
      End Try
   End Function
   Public Function Sell(ByVal TxID As Integer,ByVal Price As Decimal,ByVal Commission As Decimal) As Boolean
      Sell = False

      Try
         gmRTL.Core.GlobalException.ClearErrorObject()
         Sell = True

         Dim x As Integer = 0
         x = m_dbh.RunSPReturnInteger("Broker_Sell",New Object(){Helpers.mp("@TxID",System.Data.SqlDbType.Int,4,TxID),Helpers.mp("@Price",System.Data.SqlDbType.Money,8,Price),Helpers.mp("@Commission",System.Data.SqlDbType.Money,8,Commission)})

         If (x = 0) Then
            Sell = False
            Throw New System.Exception()
         Else
            Sell = True
         End If


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         Sell = False

         Helpers.RaiseError(m_modName,"Sell(" & TxID & "," & Price & "," & Commission & ")")
      End Try
   End Function
   End Class
End Namespace
