public void UpdateBalanceSheet(BalanceSheet bs)
 {
     if (BalanceSheetExists(bs.Symbol, bs.Year))
     {
         _dao.UpdateBalanceSheet(bs);
     }
     else
     {
         _dao.InsertBalanceSheet(bs);
     }
 }
        internal BalanceSheet PopulateFinancialStatement(BalanceSheetFilterItem item, BalanceSheet bs)
        {
            bs.CurrentAssets = GetMaxValue(item.CurrentAssetsItems);
            bs.CurrentLiabilities = GetMaxValue(item.CurrentLiabilitiesItems);
            bs.Debt = GetMaxValue(item.DebtItems);
            bs.Ppe = GetMaxValue(item.PpeItems);
            bs.ShareholdersEquity = GetMaxValue(item.ShareholderEquityItems);
            bs.Cash = GetMaxValue(item.CashItems);
            bs.TotalAssets = GetMaxValue(item.TotalAssetsItems);

            return bs;
        }
        public BalanceSheetFilterItem(string symbol, int year)
        {
            this._bal = new BalanceSheet();
            this._bal.Symbol = symbol;
            this._bal.Year = year;

            this._cashItems = new List<Item>();
            this._currentAssetsItems = new List<Item>();
            this._currentLiabilitiesItems = new List<Item>();
            this._debtItems = new List<Item>();
            this._debtNodes = new XbrlNodeCollection();
            this._ppeItems = new List<Item>();
            this._shareholderEquityItems = new List<Item>();
            this._totalAssetsItems = new List<Item>();
            
        }
        public BalanceSheet Populate(XbrlDocument xbrlDoc, string symbol, int year, Dictionary<string, List<string>> xbrlTaxonomyTree)
        {
            BalanceSheet bal = new BalanceSheet();
            bal.Symbol = symbol;
            bal.Year = year;

            BalanceSheetFilterItem item = new BalanceSheetFilterItem();

            foreach (XbrlFragment frag in xbrlDoc.XbrlFragments)
            {
                foreach (Item xbrlItem in frag.Facts)
                {
                    if (xbrlItem.ContextRef.InstantPeriod && xbrlItem.Type.Name.Equals("monetaryItemType"))
                    {
                        if (xbrlItem.ContextRef.InstantDate.Year == bal.Year)
                        {
                            item = CheckItem(xbrlItem, item, xbrlTaxonomyTree);
                        }
                    }
                }
            }
            return PopulateFinancialStatement(item,bal);
        }
        public void ProcessXbrlInstanceDocument(string xmlUrl, Dictionary<string, List<string>> xbrlTaxonomyTree)
        {
            JeffFerguson.Gepsio.XbrlDocument xbrlDoc = new XbrlDocument();
            xbrlDoc.Load(xmlUrl);

            //AddXbrlNodes(xbrlDoc);

            int yearsToLoad = 3;

            string fileName = System.IO.Path.GetFileNameWithoutExtension(xmlUrl);
            string tickerSymbol = fileName.Split(new[] { '-' })[0];
            int year = GetFiscalYear(tickerSymbol, xbrlDoc.XbrlFragments[0]);

            IncomeStatement incToAdd = new IncomeStatement();
            BalanceSheet balToAdd = new BalanceSheet();
            CompanyAnnualData compAnnToAdd = new CompanyAnnualData();

            Dictionary<string, Object> yahooStats = GetYahooStatistics(tickerSymbol);

            CreateCompanyObject(tickerSymbol, xbrlDoc, yahooStats);

            for (int y = year; y > year - yearsToLoad; y--)
            {
                XbrlIncomeStatementFilter incFilter = new XbrlIncomeStatementFilter();
                XbrlBalanceSheetFilter bsFilter = new XbrlBalanceSheetFilter();
                XbrlCompanyAnnualFilter compAnFilter = new XbrlCompanyAnnualFilter();
                
                incToAdd = incFilter.Populate(this, xbrlDoc, tickerSymbol, y, xbrlTaxonomyTree);
                balToAdd = bsFilter.Populate(xbrlDoc, tickerSymbol, y, xbrlTaxonomyTree);
                compAnnToAdd = compAnFilter.Populate(xbrlDoc, tickerSymbol, y);

                compAnnToAdd.LeveredBeta = Convert.ToDecimal(yahooStats["Beta"]);
                compAnnToAdd.DividendYield = Convert.ToDecimal(yahooStats["DividendYield"]);

                IncomeStatementBL.Instance.UpdateIncomeStatement(incToAdd);
                BalanceSheetBL.Instance.UpdateBalanceSheet(balToAdd);
                CompanyAnnualDataBL.Instance.UpdateCompanyAnnual(compAnnToAdd);
            }
        }
 private static void SetBalanceSheetParameters(BalanceSheet bs, MySqlParameter[] parms)
 {
     parms[0].Value = bs.Symbol;
     parms[1].Value = bs.Year;
     parms[2].Value = bs.CurrentAssets;
     parms[3].Value = bs.CurrentLiabilities;
     parms[4].Value = bs.Debt;
     parms[5].Value = bs.ShareholdersEquity;
     parms[6].Value = bs.Ppe;
     parms[7].Value = bs.Cash;
     parms[8].Value = bs.TotalAssets;
 }
        private BalanceSheet ConvertReaderToBalanceSheetObject(MySqlDataReader rdr)
        {
            BalanceSheet bs = new BalanceSheet();
            bs.Symbol = MySqlHelper.ConvertReaderToString(rdr, "SYMBOL");
            bs.Year = MySqlHelper.ConvertReaderToInt(rdr, "YEAR");
            bs.CurrentAssets = MySqlHelper.ConvertReaderToDecimal(rdr, "CURRENT_ASSETS");
            bs.CurrentLiabilities = MySqlHelper.ConvertReaderToDecimal(rdr, "CURRENT_LIABILITIES");
            bs.Debt = MySqlHelper.ConvertReaderToDecimal(rdr, "DEBT");
            bs.ShareholdersEquity = MySqlHelper.ConvertReaderToDecimal(rdr, "SHAREHOLDER_EQUITY");
            bs.Ppe = MySqlHelper.ConvertReaderToDecimal(rdr, "PPE");
            bs.Cash = MySqlHelper.ConvertReaderToDecimal(rdr, "CASH");
            bs.TotalAssets = MySqlHelper.ConvertReaderToDecimal(rdr, "TOTAL_ASSETS");

            return bs;
        }
 public void UpdateBalanceSheet(BalanceSheet bs)
 {
     MySqlParameter[] parms = GetBalanceSheetParameters();
     SetBalanceSheetParameters(bs, parms);
     using (MySqlConnection conn = new MySqlConnection(MySqlHelper.SV_CONN_STRING))
     {
         conn.Open();
         MySqlHelper.ExecuteNonQuery(conn, SQL_UPDATE_BALANCE_SHEET, parms);
     }
 }
 private void Initialize()
 {
     this._bal = new BalanceSheet();
     this._cashItems = new List<Item>();
     this._currentAssetsItems = new List<Item>();
     this._currentLiabilitiesItems = new List<Item>();
     this._debtItems = new List<Item>();
     this._debtNodes = new XbrlNodeCollection();
     this._ppeItems = new List<Item>();
     this._shareholderEquityItems = new List<Item>();
     this._totalAssetsItems = new List<Item>();
 }
        public void PopulateCompanyFinancialStatisticsFromStatements(CompanyFinancialStatistics finStats, IncomeStatement inc, BalanceSheet bs, CompanyAnnualData cad)
        {
            finStats.BookValuePerShare = bs.ShareholdersEquity / cad.SharesOutstanding;
            finStats.DebtToEquity = bs.Debt / bs.ShareholdersEquity;
            finStats.ProfitMargin = inc.NetIncome / inc.Revenue;
            finStats.ReturnOnAssets = inc.NetIncome / bs.TotalAssets;

            if (bs.ShareholdersEquity == 0)
            {
                finStats.ReturnOnEquity = 0;
            }
            else
            {
                finStats.ReturnOnEquity = inc.NetIncome / bs.ShareholdersEquity;
            }
            
            finStats.RevenuePerShare = inc.Revenue / cad.SharesOutstanding;
            finStats.TotalDebt = bs.Debt;
        }