'' asp code Generated by Basic Processor
'' Original Page Name: C:\gmTestBed\FMStocks\src\FMSWeb\ASP\BuyStock.asp

Imports System.Web
NameSpace FMSWeb
Public Partial class _BuyStock : Inherits System.Web.UI.Page
   Public _t_head1 As FMSWeb._t_head
   Public _t_begin1 As FMSWeb._t_begin
   Public _t_end1 As FMSWeb._t_end

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      _t_head1 = New FMSWeb._t_head()
      _t_begin1 = New FMSWeb._t_begin()
      _t_end1 = New FMSWeb._t_end()
      _t_begin1.Include_Load(_t_head1)
      _t_end1.Include_Load(_t_head1)
   End Sub
   Dim ObjAccount As New FMStocks_Bus.Ticker
   Dim ObjBroker As New FMStocks_Bus.Broker
   Public Ticker As String = ""
   Public Shares As String = ""
   Public MainPrompt As String = ""
   ' Side effects: Sets Ticker and Shares from the Request object
   Public Sub ProcessPage()
      ' Get the form values into page variables (if any)
      Ticker = Strings.Trim(HttpContext.Current.Request("Ticker"))
      Shares = Strings.Trim(HttpContext.Current.Request("Shares"))
      'First we need to see if the user is trying to input information
      If (HttpContext.Current.Request.Form("BuyStock") = "Yes") Then
         If (IsFormValid()) Then
            If (Not Convert.ToBoolean(ValidateTicker(Ticker))) Then
               MainPrompt = FMSWeb._strings.BUYSTOCK_INVALID_TICKER
               Exit Sub
            End If
            If (PlaceBuyOrder() > 0) Then
               'Update account returns a 1 if the transaction completed and a 0 if the transaction failed
               MainPrompt = "Your order has been placed. <a href='portfolio.aspx'>Click here to view your portfolio</a>."
               Ticker = ""
               Shares = ""
            End If
         Else
            MainPrompt = "<FONT COLOR=Black>" & FMSWeb._strings.FILL_ALL_FIELDS & "</FONT>"
         End If
      End If
   End Sub
   ' Perform server-side validation on the fields before they're passed to
   ' the middle-tier.
   Public Function IsFormValid() As Boolean
      IsFormValid = True
      If (String.IsNullOrEmpty(Ticker) Or String.IsNullOrEmpty(Shares)) Then
         IsFormValid = False
      End If
   End Function
   ' Make a trip to the database to see if the ticker is valid.
   ' If we didn't have this call
   Public Function ValidateTicker(ByVal t As String) As Boolean
      On Error Resume Next
      ValidateTicker = ObjAccount.VerifySymbol(t)
      If (Err.Number <> 0) Then
         _t_head1.BuildErrorMessage()
         ValidateTicker = False
      End If
   End Function
   ' Attempt to place the buy order
   ' Return the new TransactionID (>0) on success, otherwise 0 to indicate failure.
   Public Function PlaceBuyOrder() As Integer
      On Error Resume Next
      PlaceBuyOrder = 0                  ' assume it fails
      'this will be a long to represent the number of shares.
      Dim lngShares As Integer = 0
      'convert to a long incase of fractions of shares.
      lngShares = CInt(Shares)
      PlaceBuyOrder = ObjBroker.BuyStock(CInt(_t_head1.g_AccountID),Ticker,lngShares)
      If (Err.Number <> 0 Or PlaceBuyOrder = 0) Then
         MainPrompt = FMSWeb._strings.BUYSTOCK_PURCHASE_FAILED
         ' PlaceBuyOrder = 0 doesn't necessarily mean there was an error
         If (Err.Number <> 0) Then
            _t_head1.BuildErrorMessage()
         End If
         Exit Function
      End If
   End Function
   Public Sub OnTransactionAbort()
      ' No extra processing is required for this file.
      ' The error emitting code in t_head.asp shows the actual database message
   End Sub
End Class
End Namespace
