using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

namespace AdvertsingProfitControl
{
    internal class DatabaseReader
    {
        public string GetDatabaseVersion(string connectionString)
        {
            var versionNumber = "0.0.0.0";
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT Version.VersionNumber FROM Version"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            versionNumber = reader[0].ToString();
                        }
                    }
                }
            }

            return versionNumber;
        }

        #region Date Functions
        /// <summary>
        /// Retrieves the most recent date's ID from the database.
        /// </summary>
        /// <param name="connectionString">The connection string for the database.</param>
        /// <returns>The ID number as a string.</returns>
        public int RetrieveMostRecentDateId(string connectionString)
        {
            int dateId = 0;
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT WeekEnding.ID FROM WeekEnding WHERE WeekEnding.EndOfWeekDate = (SELECT MAX(WeekEnding.EndOfWeekDate) FROM WeekEnding)"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            dateId = int.Parse(reader[0].ToString());
                        }
                    }
                }
            }

            return dateId;
        }

        public DateTime RetrieveMostRecentDate(string connectionString)
        {
            var date = new DateTime();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT WeekEnding.EndOfWeekDate FROM WeekEnding WHERE WeekEnding.EndOfWeekDate = (SELECT MAX(WeekEnding.EndOfWeekDate) FROM WeekEnding)"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            date = DateTime.Parse(reader[0].ToString());
                        }
                    }
                }
            }

            return date;
        }

        /// <summary>
        /// Returns the date ID of the date string that is passed.
        /// Preferred format is short date (1/20/2017).
        /// </summary>
        /// <param name="dateString">The date to be looked up.</param>
        /// <param name="connectionString">The connection to the database.</param>
        /// <returns>The date ID or zero (0) on fail.</returns>
        public int RetrieveDateIdByDateString(string dateString, string connectionString)
        {
            var dateId = 0;
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT WeekEnding.ID FROM WeekEnding WHERE WeekEnding.EndOfWeekDate = ?"
            };
            oleDbCommand.Parameters.AddWithValue("DateString", dateString);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            dateId = int.Parse(reader[0].ToString());
                        }
                    }
                }
            }

            return dateId;
        }

        public string RetrieveDateStringById(int dateId, string connectionString)
        {
            string dateString = "";
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT WeekEnding.EndOfWeekDate FROM WeekEnding WHERE WeekEnding.ID = ? ORDER BY EndOfWeekDate ASC"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            dateString = reader[0].ToString();
                        }
                    }
                }
            }

            return dateString;
        }

        public List<DateTime> RetrieveDates(string connectionString)
        {
            var dates = new List<DateTime>();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT WeekEnding.EndOfWeekDate FROM WeekEnding"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            dates.Add(DateTime.Parse(reader[0].ToString()));
                        }
                    }
                }
            }

            return dates;
        }

        public List<string> RetrieveUniqueYearsList(string connectionString)
        {
            var datesList = new List<string>();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT Distinct YEAR(EndOfWeekDate) FROM WeekEnding"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            datesList.Add(reader[0].ToString());
                        }
                    }
                }
            }

            return datesList;
        }

        public List<string> RetrieveUniqueMonthsList(string year, string connectionString)
        {
            var datesList = new List<string>();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT Distinct MONTH(EndOfWeekDate) FROM WeekEnding WHERE YEAR(EndOfWeekDate) = ?"
            };
            oleDbCommand.Parameters.AddWithValue("Year", year);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            datesList.Add(reader[0].ToString());
                        }
                    }
                }
            }

            return datesList;
        }

        public DateTime RetrieveMostRecentDateString(string connectionString)
        {
            var dateTimeObject = new DateTime();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT WeekEnding.EndOfWeekDate FROM WeekEnding WHERE WeekEnding.EndOfWeekDate = (SELECT MAX(WeekEnding.EndOfWeekDate) FROM WeekEnding)"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            dateTimeObject = (DateTime) reader[0];
                        }
                    }
                }
            }

            return dateTimeObject;
        }

        /// <summary>
        /// Returns a list of dates by the year supplied.
        /// </summary>
        /// <returns></returns>
        public List<DateTime> RetrieveDateListByYear(string year, string connectionString)
        {
            var datesList = new List<DateTime>();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT EndOfWeekDate FROM WeekEnding WHERE YEAR(EndOfWeekDate) = ? ORDER BY EndOfWeekDate ASC"
            };
            oleDbCommand.Parameters.AddWithValue("Year", year);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            datesList.Add((DateTime)reader[0]);
                        }
                    }
                }
            }

            return datesList;
        }

        #endregion

        #region Ad Item Functions

        public Dictionary<int, string> GetAdItemsSuggestionDictionary(string connectionString)
        {
            var adItemDictionary = new Dictionary<int, string>();

            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdItem.ID, AdItem.AdItem FROM AdItem ORDER BY AdItem.AdItem ASC"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            adItemDictionary.Add(int.Parse(reader["ID"].ToString()), reader["AdItem"].ToString());
                        }
                    }
                }
            }

            return adItemDictionary;
        }

        public int GetLargestKeyValueForAdItems(string connectionString)
        {
            var id = -1;
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT MAX(AdItem.ID) FROM AdItem"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            int.TryParse(reader[0].ToString(), out id);
                        }
                    }
                }
            }

            return id;
        }

        public string RetrieveAdItemId(string adItemName, string connectionString)
        {
            var id = "0";
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdItem.ID FROM AdItem WHERE UCASE(AdItem.AdItem) = UCASE(?)"
            };
            oleDbCommand.Parameters.AddWithValue("AdItemName", adItemName);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            id = reader[0].ToString();
                        }
                    }
                    if (id != "0") return id;
                    {
                        oleDbCommand.CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)";
                        oleDbCommand.Parameters.Clear();
                        oleDbCommand.Parameters.AddWithValue("AdItem", TextFormat.FormatAdItemText(adItemName));

                        try
                        {
                            var rowsEffected = oleDbCommand.ExecuteNonQuery();
                            if (rowsEffected == 1)
                            {
                                oleDbCommand.Parameters.Clear();
                                oleDbCommand.CommandText = "SELECT AdItem.ID FROM AdItem WHERE AdItem.AdItem = ?";
                                oleDbCommand.Parameters.AddWithValue("ad", TextFormat.FormatAdItemText(adItemName));
                                var reader = oleDbCommand.ExecuteReader();
                                while (reader != null && reader.Read())
                                {
                                    id = reader[0].ToString();
                                }
                            }
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
            }
            return id;
        }

        public List<string> RetrieveUsedAdItemListByDateId(int dateId, string connectionString)
        {
            var adItemList = new List<string>();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT AdItem.AdItem, ActualSales.RowAttribute, ActualSales.FK_AdSpecialGroupName FROM (ActualSales INNER JOIN AdItem ON ActualSales.FK_AdItemID = AdItem.ID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            if (reader["FK_AdSpecialGroupName"].ToString() != "" && int.Parse(reader["FK_AdSpecialGroupName"].ToString()) >= 3)
                            {
                                adItemList.Add(reader[0] + ":2");
                            }
                            else
                            {
                                adItemList.Add(reader[0] + ":1");                                
                            }

                        }
                    }
                }
            }

            return adItemList;
        }

        public List<string> RetrieveAdItemListByLetter(string connectionString, string letterFilter = "All")
        {
            var adItemsList = new List<string>();
            var oleDbCommand = new OleDbCommand();
            if(letterFilter == "All")
            {
                oleDbCommand.CommandText = "SELECT AdItem FROM AdItem ORDER BY AdItem ASC";
            }
            else
            {
                oleDbCommand.CommandText = "SELECT AdItem FROM AdItem WHERE LEFT(AdItem, 1) = ? ORDER BY AdItem ASC";
                oleDbCommand.Parameters.AddWithValue("Filter", letterFilter);
            }
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            adItemsList.Add((reader[0].ToString()));
                        }
                    }
                }
            }

            return adItemsList;
        } 

        #endregion

        #region Comment Functions

        /// <summary>
        /// Created for the new modify record form, returns the ID of the comment being grabbed.
        /// </summary>
        /// <param name="dateId"></param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public List<string> RetrieveComments(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT Comment.ID, Comment.Comment FROM Comment WHERE Comment.FK_DateID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }
            var comments = new List<string>();
            if (dataTable.Rows.Count != 1) return comments;
            comments.Add(dataTable.Rows[0].ItemArray[0].ToString());
            comments.Add(dataTable.Rows[0].ItemArray[1].ToString());
            return comments;
        }

        #endregion

        #region Ad Special Functions

        public string RetrieveGroupIdByString(string groupName, string connectionString)
        {
            var groupId = "0";
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdSpecialName.ID FROM AdSpecialName WHERE AdSpecialName.AdSpecialName = ?"
            };
            oleDbCommand.Parameters.AddWithValue("AdSpecialName", groupName);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            groupId = reader[0].ToString();
                        }
                    }
                }
            }

            return groupId;
        }

        public string ReturnAdSpecialDescriptionByAdSpecialName(string adSpecialString, string connectionString)
        {
            var descriptionString = "";
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdSpecialName.AdSpecialDescription FROM AdSpecialName WHERE AdSpecialName.AdSpecialDescription = ?"
            };
            oleDbCommand.Parameters.AddWithValue("AdspecialName", adSpecialString);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            descriptionString = reader[0].ToString();
                        }
                    }
                }
            }

            return descriptionString;
        }

        public List<string> ReturnGroupNameList(string connectionString)
        {
            var groupNames = new List<string>();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdSpecialName.AdSpecialName FROM AdSpecialName"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            groupNames.Add(reader[0].ToString());
                        }
                    }
                }
            }

            return groupNames;
        }

        public string ReturnGroupNameFromGroupId(string id, string connectionString)
        {
            var name = "";
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdSpecialName FROM AdSpecialName WHERE ID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("AdspecialName", id);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            name = reader[0].ToString();
                        }
                    }
                }
            }

            return name;
        }

        public AutoCompleteStringCollection RetrieveAdSpecialList(string connectionString)
        {
            var adSpecialList = new AutoCompleteStringCollection();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "Select AdSpecialName FROM AdSpecialName"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            adSpecialList.Add(reader[0].ToString());
                        }
                    }
                }
            }

            return adSpecialList;
        }
        #endregion

        #region AutoComplete Functions

        public List<string> GetAdItemsSuggestionList(string connectionString)
        {
            var autoCompleteList = new List<string>();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdItem.AdItem FROM AdItem ORDER BY AdItem.AdItem ASC"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            autoCompleteList.Add(reader[0].ToString());
                        }
                    }
                }
            }
            
            return autoCompleteList;
        }

        public AutoCompleteStringCollection GetSupplierSuggestionList(string connectionString)
        {
            var autoCompleteList = new AutoCompleteStringCollection();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT SupplierName FROM Supplier ORDER BY SupplierName ASC"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            autoCompleteList.Add(reader[0].ToString());
                        }
                    }
                }
            }

            return autoCompleteList;
        }

        #endregion

        #region Table Return Functions

        public DataTable ReturnApcTableForReport(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT AdItem.AdItem, Projections.Sold, Projections.SalePrice, Projections.TotalSales, Projections.Cost, Projections.ProfitReturn, Projections.TotalProfitReturn, Inventory.BeginingInventory, Inventory.Received, Inventory.TotalInventory, Inventory.EndingInventory, ActualSales.Sold, ActualSales.SalePrice, ActualSales.TotalSales, ActualSales.Cost, ActualSales.ProfitReturn, ActualSales.TotalProfitReturn, ActualSales.FK_AdSpecialGroupName FROM (AdItem INNER JOIN Projections ON AdItem.ID = Projections.FK_AdItemID INNER JOIN Inventory ON Inventory.ID = Projections.ID INNER JOIN ActualSales ON ActualSales.ID = Projections.ID) WHERE Projections.FK_DateID = ? ORDER BY Projections.RowPosition ASC"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }

            return dataTable;
        }

        public DataTable ReturnProjectionsTable(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT Projections.ID, AdItem.AdItem, Projections.Sold, Projections.SalePrice, Projections.TotalSales, Projections.Cost, Projections.ProfitReturn, Projections.TotalProfitReturn, Projections.RowAttribute, Projections.FK_AdSpecialGroupName FROM (AdItem INNER JOIN Projections ON AdItem.ID = Projections.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }
            return dataTable;
        }

        public DataTable ReturnActualSales(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT ActualSales.ID, AdItem.AdItem, ActualSales.Sold, ActualSales.SalePrice, ActualSales.TotalSales, ActualSales.Cost, ActualSales.ProfitReturn, ActualSales.TotalProfitReturn, ActualSales.RowAttribute, ActualSales.FK_AdSpecialGroupName FROM (AdItem INNER JOIN ActualSales ON AdItem.ID = ActualSales.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }
            return dataTable;
        }

        public DataTable ReturnInventoryTable(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT Inventory.ID, AdItem.AdItem, Inventory.BeginningInventory, Inventory.Received, Inventory.TotalInventory, Inventory.EndingInventory, Inventory.RowAttribute, Inventory.FK_AdSpecialGroupName FROM (AdItem INNER JOIN Inventory ON AdItem.ID = Inventory.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }
            return dataTable;
        }


        public DataTable ReturnInvoiceTable(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT Invoice.ID, Invoice.InvoiceDate, Supplier.SupplierName, Invoice.InvoiceNumber,  Invoice.InvoiceNetAmountAtCost, Invoice.InvoiceNetAmount, Invoice.InvoiceNote FROM (Supplier INNER JOIN Invoice ON Supplier.ID = Invoice.FK_Supplier) WHERE FK_DateID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    using(var adapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        adapter.Fill(dataTable);
                    }
                }
            }

            return dataTable;
        }

        public DataTable ReturnWeeklySalesFromDateId(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT WeeklySales.ID, WeeklySales.Sunday, WeeklySales.Monday, WeeklySales.Tuesday, WeeklySales.Wednesday, WeeklySales.Thursday, WeeklySales.Friday, WeeklySales.Saturday, WeeklySales.TotalSales FROM WeeklySales WHERE FK_DateID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }

            return dataTable;
        }

        public DataTable ReturnTaxableFromDateId(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT Taxable.ID, Taxable.Sunday, Taxable.Monday, Taxable.Tuesday, Taxable.Wednesday, Taxable.Thursday, Taxable.Friday, Taxable.Saturday, Taxable.Total FROM Taxable WHERE FK_DateID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }

            return dataTable;
        }

        public DataTable ReturnCostAnalysis(int dateId, string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT CostOfSalesAnalysis.ID, CostOfSalesAnalysis.SalesPerManHour, CostOfSalesAnalysis.SalaryPercentage, CostOfSalesAnalysis.SalaryDollars, CostOfSalesAnalysis.Supplies FROM CostOfSalesAnalysis WHERE FK_DateID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("DateID", dateId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }

            return dataTable;
        }

        public DataTable ReturnWeekEndingTable(string connectionString)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT EndOfWeekDate FROM WeekEnding ORDER BY EndOfWeekDate ASC"
            };
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var dataAdapter = new OleDbDataAdapter(oleDbCommand))
                    {
                        dataAdapter.Fill(dataTable);
                    }
                }
            }

            return dataTable;

        }

        #endregion

        #region Supplier And Invoices Functions

        public string RetrieveSupplierNameById(int supplierId, string connectionString)
        {
            var supplierName = "";
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT SupplierName FROM Supplier WHERE ID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("ID", supplierId);
            var connection = new OleDbConnection(connectionString);
            oleDbCommand.Connection = connection;

            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using(var reader = oleDbCommand.ExecuteReader())
                    {
                        while(reader != null && reader.Read())
                        {
                            supplierName = reader[0].ToString();
                        }
                    }
                }
            }

            return supplierName;
        }

        #endregion
    }
}
