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

namespace FMSWeb
{
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using VBNET=Microsoft.VisualBasic;
using gmRTL.Core;

public partial class _BuyStock : System.Web.UI.Page
{
   public FMSWeb._t_head _t_head1 = null;
   public FMSWeb._t_begin _t_begin1 = null;
   public FMSWeb._t_end _t_end1 = null;

   protected void Page_Load(object sender, EventArgs e)
   {
      _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);
   }
   private FMStocks_Bus.Ticker ObjAccount = new FMStocks_Bus.Ticker();
   private FMStocks_Bus.Broker ObjBroker = new FMStocks_Bus.Broker();
   public string Ticker = "";
   public string Shares = "";
   public string MainPrompt = "";
   //  Side effects: Sets Ticker and Shares from the Request object
   public void ProcessPage()
   {
      //  Get the form values into page variables (if any)
      Ticker = HttpContext.Current.Request["Ticker"].TrimB();
      Shares = HttpContext.Current.Request["Shares"].TrimB();
      // First we need to see if the user is trying to input information
      if (HttpContext.Current.Request.Form["BuyStock"] == "Yes")
      {
         if (IsFormValid().ToBool())
         {
            if (!ValidateTicker(Ticker).ToBool())
            {
               MainPrompt = FMSWeb._strings.BUYSTOCK_INVALID_TICKER;
               return;
            }
            if (PlaceBuyOrder().ToInt() > 0)
            {
               // 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 = "";
            }
         }
         else
         {
            MainPrompt = "<FONT COLOR=Black>" + FMSWeb._strings.FILL_ALL_FIELDS + "</FONT>";
         }
      }
   }
   //  Perform server-side validation on the fields before they're passed to
   //  the middle-tier.
   public bool IsFormValid()
   {
      bool IsFormValid = false;

      IsFormValid = true;
      if (String.IsNullOrEmpty(Ticker) || String.IsNullOrEmpty(Shares))
      {
         IsFormValid = false;
      }
      return IsFormValid;
   }
   //  Make a trip to the database to see if the ticker is valid.
   //  If we didn't have this call
   public bool ValidateTicker(string t)
   {
      bool ValidateTicker = false;

      gmRTL.Core.ErrorHandling.ResumeNext(() =>
      {
         ValidateTicker = ObjAccount.VerifySymbol(t);
         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            _t_head1.BuildErrorMessage();
            ValidateTicker = false;
         }
      });
      return ValidateTicker;
   }
   //  Attempt to place the buy order
   //  Return the new TransactionID (>0) on success, otherwise 0 to indicate failure.
   public int PlaceBuyOrder()
   {
      int PlaceBuyOrder = 0;

      // UPGRADE_TODO: OnErrorResumeNext();
      PlaceBuyOrder = 0;     //  assume it fails
      // this will be a long to represent the number of shares.
      int lngShares = 0;
      // convert to a long incase of fractions of shares.
      lngShares = Shares.ToInt();
      PlaceBuyOrder = ObjBroker.BuyStock(_t_head1.g_AccountID.ToInt(),Ticker,lngShares);
      if (gmRTL.Core.GlobalException.Instance.Number != 0 || PlaceBuyOrder == 0)
      {
         MainPrompt = FMSWeb._strings.BUYSTOCK_PURCHASE_FAILED;
         //  PlaceBuyOrder = 0 doesn't necessarily mean there was an error
         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            _t_head1.BuildErrorMessage();
         }
         return PlaceBuyOrder;
      }
      return PlaceBuyOrder;
   }
   public void OnTransactionAbort()
   {
      //  No extra processing is required for this file.
      //  The error emitting code in t_head.asp shows the actual database message
   }
}
}
