Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace FMStocks_Bus
   Public Class Ticker
   Private Const m_modName As String = "FMStocks_Bus.Ticker"
   'VerifySymbol

   Public Function VerifySymbol(ByVal Ticker As String) As Boolean
      VerifySymbol = False
      Dim obj As FMStocks_DB.Ticker = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()
         obj = New FMStocks_DB.Ticker

         VerifySymbol = obj.VerifySymbol(Ticker)
         obj = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"VerifySymbol(" & Ticker & ")",String.Empty,0,False)

      End Try
   End Function
   ' ListByTicker

   Public Function ListByTicker(ByVal Ticker As String) As gmRTL.DataLib.SqlClient.Recordset
      ListByTicker = Nothing
      Dim obj As FMStocks_DB.Ticker = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()
         obj = New FMStocks_DB.Ticker

         ListByTicker = obj.ListByTicker(Ticker)
         obj = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"ListByTicker(" & Ticker & ")",String.Empty,0,False)
      End Try
   End Function
   ' ListByCompany

   Public Function ListByCompany(ByVal company As String) As gmRTL.DataLib.SqlClient.Recordset
      ListByCompany = Nothing
      Dim obj As FMStocks_DB.Ticker = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()
         obj = New FMStocks_DB.Ticker

         ListByCompany = obj.ListByCompany(company)
         obj = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"ListByCompany(" & company & ")",String.Empty,0,False)
      End Try
   End Function
   ' GetFundamentals
   '
   ' Takes a ticker symbol and returns an array of fundamental metric values,
   ' the company name and the trading exchange.
   Public Function GetFundamentals(ByVal Ticker As String,ByRef company As Object,ByRef Exchange As Object) As gmRTL.DataLib.SqlClient.Recordset
      GetFundamentals = Nothing
      Dim obj As FMStocks_DB.Ticker = Nothing
      Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()
         obj = New FMStocks_DB.Ticker

         rs = obj.GetFundamentals(Ticker)

         ' Construct a new recordset based on the results from the fundamental queries

         Dim rsnew As gmRTL.DataLib.SqlClient.Recordset = Nothing
         rsnew = New gmRTL.DataLib.SqlClient.Recordset

         rsnew.CursorLocation = gmRTL.DataLib.CursorLocationEnum.adUseClient

         'Add Some Fields
         rsnew.Fields.Append("ShortDesc",System.Data.SqlDbType.VarChar,50)
         rsnew.Fields.Append("LongDesc",System.Data.SqlDbType.VarChar,100)
         rsnew.Fields.Append("Criteria",System.Data.SqlDbType.VarChar,50)
         rsnew.Fields.Append("Value",System.Data.SqlDbType.VarChar,50)
         rsnew.Fields.Append("Status",System.Data.SqlDbType.Int,4)

         rsnew.Open(Nothing, Nothing, gmRTL.DataLib.CursorTypeEnum.adOpenStatic, gmRTL.DataLib.LockTypeEnum.adLockBatchOptimistic)

         Do While Not rs.EOF
            company = rs.Fields("Company").Value
            Exchange = rs.Fields("Exchange").Value

            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("MarketCap")) < 1000),"MarketCap","$" & FMStocks_DB.Helpers.NullsToZero(rs.Fields("MarketCap")) & "M","< $1B","Total market capitalization less than $1 Billion.")
            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("Sales")) < 100),"Sales","$" & FMStocks_DB.Helpers.NullsToZero(rs.Fields("Sales")) & "M","< $100M","Gross revenue (total sales) less than $100 Million.")
            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("Price")) > 7),"Price","$" & FMStocks_DB.Helpers.NullsToZero(rs.Fields("Price")),"> $7/share","Stock price greater than $7 per share.")
            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("DailyDollarVol")) > 1),"Daily dollar volume","$" & FMStocks_DB.Helpers.NullsToZero(rs.Fields("DailyDollarVol")) & "M","> $1M per day","At least $100M in shares move on an average day.")
            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("SalesGrowth")) > 25),"Sales growth",FMStocks_DB.Helpers.NullsToZero(rs.Fields("SalesGrowth")) & "%","> 25%","Gross revenue growth of 25% trailing-twelve months (TTM).")
            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("EPSGrowth")) > 25),"Earnings growth",FMStocks_DB.Helpers.NullsToZero(rs.Fields("EPSGrowth")) & "%","> 25%","Earnings-per-share growth over TTM.")
            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("NetProfitMargin")) > 7),"Profit margin",FMStocks_DB.Helpers.NullsToZero(rs.Fields("NetProfitMargin")) & "%","> 7%","Net profit margin at least 7%.")
            FormatItem(rsnew,CInt(FMStocks_DB.Helpers.NullsToZero(rs.Fields("InsiderShares")) > 10),"Insider holdings",FMStocks_DB.Helpers.NullsToZero(rs.Fields("InsiderShares")) & "%","> 10%","At least 10% of the shares should be held by insiders.")

            rs.MoveNext()
         Loop
         rsnew.UpdateBatch(gmRTL.DataLib.AffectEnum.adAffectAll)
         rsnew.MoveFirst()

         obj = Nothing
         rs = Nothing
         GetFundamentals = rsnew

         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing
         rs = Nothing


         FMStocks_DB.Helpers.RaiseError(m_modName,"GetFundamentals(" & Ticker & ")",String.Empty,0,False)
      End Try
   End Function
   'Helper sub to add the new records
   Private Sub FormatItem(ByVal rs As gmRTL.DataLib.SqlClient.Recordset,ByVal Status As Integer,ByVal ShortDesc As String,ByVal Value As String,ByVal Criteria As String,ByVal LongDesc As String)
      rs.AddNew(New Object(){"ShortDesc","LongDesc","Criteria","Value","Status"},New Object(){ShortDesc,LongDesc,Criteria,Value,Status})
   End Sub
   End Class
End Namespace
