using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Transactions;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    public partial class DebugDatabaseConverter : Form
    {
        private string _filePath;
        private string _connectionString;
        public DebugDatabaseConverter()
        {
            InitializeComponent();
            existingDatesListBox.SelectedIndexChanged += EnableButton;
        }

        private void EnableButton(object sender, EventArgs e)
        {
            moveOverButton.Enabled = true;
        }

        private void DebugDatabaseConverter_Load(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = "accdb";
            openFileDialog1.Filter = @"Access Files (*.accdb) | *.accdb";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var filePath = openFileDialog1.FileName;
                if (File.Exists(filePath))
                {
                    _filePath = filePath;
                    //Get the list of dates.
                    var connectionString =
                        "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Persist Security Info=False";
                    _connectionString = connectionString;
                    var dates = new List<DateTime>();
                    var oleDbCommand = new OleDbCommand
                    {
                        CommandText = "SELECT 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()));
                                }
                            }
                        }
                        connection.Close();
                    }

                    foreach (var date in dates)
                    {
                        existingDatesListBox.Items.Add(date.ToShortDateString());
                    }
                }
            }
        }

        private void MoveOverButton_Click(object sender, EventArgs e)
        {
            if (existingDatesListBox.SelectedIndex == -1) return;
            foreach (var index in existingDatesListBox.SelectedItems)
            {
                datesToConvertListBox.Items.Add(index);
                //existingDatesListBox.Items.Remove(index);
            }
            moveOverButton.Enabled = false;
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            if (datesToConvertListBox.Items.Count == 0)
            {
                return;
            }
            //Start by bringing in all the ad items then the suppliers.
            var adItems = GetAdItems();
            foreach (var adItem in adItems)
            {
                var db = new AdvertisingProfitControlModel();
                if (db.AdItems.Any(x => x.Name == adItem)) continue;
                var temp = new AdItem { Name = TextFormat.FormatAdItemText(adItem) };
                db.AdItems.Add(temp);
                db.SaveChanges();
            }
            var suppliers = GetSuppliers();
            foreach (var supplier in suppliers)
            {             
                var db = new AdvertisingProfitControlModel();
                if (db.Suppliers.Any(x => x.Name == supplier)) continue;
                var temp = new Supplier { Name = supplier };
                db.Suppliers.Add(temp);
                db.SaveChanges();
            }
            var adSpecials = GetAdSpecials();
            foreach (var adSpecial in adSpecials)
            {
                var db = new AdvertisingProfitControlModel();
                if(db.AdSpecials.Any(x => x.Name == adSpecial)) continue;
                var tempAd = new AdSpecial
                {
                    Name = adSpecial
                };
                db.AdSpecials.Add(tempAd);
                db.SaveChanges();
            }
            foreach (var date in datesToConvertListBox.Items)
            {
                var oldDateId = GetDateIdByDateString(date.ToString());
                //Get the tables.
                var projectionsTable = ReturnProjections(oldDateId);
                var inventoryTable = ReturnInventory(oldDateId);
                var actualSales = ReturnActualSales(oldDateId);
                var invoices = ReturnInvoiceTable(oldDateId);
                var weeklySales = ReturnWeeklySalesFromDateId(oldDateId);
                var taxable = ReturnTaxableFromDateId(oldDateId, _connectionString);
                var comment = GetComments(oldDateId);
                var costs = ReturnCostAnalysis(oldDateId, _connectionString);
                MassiveWriteFunction(date.ToString(), projectionsTable, inventoryTable, actualSales, invoices, weeklySales, taxable, costs, comment);
            }
        }

        private void MassiveWriteFunction(string dateString, DataTable projections, DataTable inventory, DataTable actualSales, DataTable invoice, DataTable weeklySales, DataTable taxableTable, DataTable costOfSales, string comments)
        {
            try
            {             
                using (var transaction = new TransactionScope())
                {
                    var db = new AdvertisingProfitControlModel();
                    var date = new WeekEndingDate {EndingDate = DateTime.Parse(dateString)};
                    if (db.WeekEndingDates.Any(x => x.EndingDate == date.EndingDate))
                    {
                        date = db.WeekEndingDates.First(x => x.EndingDate == date.EndingDate);
                    }
                    else
                    {
                        db.WeekEndingDates.Add(date);
                    }
                    var adSpecialId = -1;
                    for (var rowIndex = 0; rowIndex < projections.Rows.Count; rowIndex++)
                    {
                        var projectionedSale = new Projection();
                        var adItem = new AdItem();
                        var adItemId = 0;
                        //var id = int.Parse(projections.Rows[rowIndex][0].ToString());
                        var spam = TextFormat.FormatAdItemText(projections.Rows[rowIndex][0].ToString());
                        if (db.AdItems.Any(x => x.Name == spam))
                        {
                            //Exists
                            adItem = db.AdItems.First(x => x.Name == spam);
                            adItemId = adItem.Id;
                        }
                        else
                        {
                            //Doesn't
                            adItem.Name = projections.Rows[rowIndex][0].ToString();
                            db.AdItems.Add(adItem);
                            adItemId = adItem.Id;
                        }
                        projectionedSale.Sold = projections.Rows[rowIndex][1].ToString();
                        projectionedSale.SalePrice = projections.Rows[rowIndex][2].ToString();
                        projectionedSale.TotalSales = decimal.Parse(projections.Rows[rowIndex][3].ToString());
                        projectionedSale.Cost = decimal.Parse(projections.Rows[rowIndex][4].ToString());
                        projectionedSale.ProfitReturn = decimal.Parse(projections.Rows[rowIndex][5].ToString());
                        projectionedSale.TotalProfitReturn = decimal.Parse(projections.Rows[rowIndex][6].ToString());
                        projectionedSale.FkAdItemId = db.AdItems.First(x => x.Id == adItemId).Id;
                        projectionedSale.RowAttribute = int.Parse(projections.Rows[rowIndex][7].ToString());
                        
                        if ((int) projections.Rows[rowIndex][8] == 0 && adSpecialId == -1)
                        {
                            projectionedSale.FkAdSpecialId = null;
                        }
                        else
                        {
                            if ((int) (projections.Rows[rowIndex][8]) == 0)
                            {
                                projectionedSale.FkAdSpecialId = adSpecialId;
                            }
                            else
                            {
                                adSpecialId = (int) projections.Rows[rowIndex][8];
                                projectionedSale.FkAdSpecialId = (int) projections.Rows[rowIndex][8];     
                            }
                        }
                        projectionedSale.RowPosition = rowIndex + 1;
                        projectionedSale.FkDateId = date.Id;
                        db.Projections.Add(projectionedSale);
                    }
                    //inventory
                    adSpecialId = -1;
                    for (var i = 0; i < inventory.Rows.Count; i++)
                    {
                        var inventoryObject = new Inventory();
                        AdItem adItem;
                        var adItemId = 0;
                        var spam = TextFormat.FormatAdItemText(inventory.Rows[i][0].ToString());
                        if (db.AdItems.Any(x => x.Name == spam))
                        {
                            //Exists
                            adItem = db.AdItems.First(x => x.Name == spam);
                            adItemId = adItem.Id;
                        }
                        inventoryObject.BeginningInventory = inventory.Rows[i][1].ToString();
                        inventoryObject.Recieved = inventory.Rows[i][2].ToString();
                        inventoryObject.TotalInventory = inventory.Rows[i][3].ToString();
                        inventoryObject.EndingInventory = inventory.Rows[i][4].ToString();
                        inventoryObject.FkAdItemId = adItemId;
                        inventoryObject.RowAttribute = int.Parse(inventory.Rows[i][5].ToString());
                        if ((int)inventory.Rows[i][6] == 0 && adSpecialId == -1)
                        {
                            inventoryObject.FkAdSpecialId = null;
                        }
                        else
                        {
                            if ((int)(inventory.Rows[i][6]) == 0)
                            {
                                inventoryObject.FkAdSpecialId = adSpecialId;
                            }
                            else
                            {
                                adSpecialId = (int)inventory.Rows[i][6];
                                inventoryObject.FkAdSpecialId = (int)inventory.Rows[i][6];
                            }
                        }
                        inventoryObject.RowPosition = i + 1;
                        inventoryObject.FkDateId = date.Id;
                        db.Inventories.Add(inventoryObject);
                    }

                    //actual sales
                    adSpecialId = -1;
                    for (var rowIndex = 0; rowIndex < actualSales.Rows.Count; rowIndex++)
                    {
                        var actualSale = new ActualSale();
                        //Get the ID number of the ad item.
                        var adItemId = 0;
                        var spam = TextFormat.FormatAdItemText(actualSales.Rows[rowIndex][0].ToString());
                        if (db.AdItems.Any(x => x.Name == spam))
                        {
                            //Exists
                            var adItem = db.AdItems.First(x => x.Name == spam);
                            adItemId = adItem.Id;
                        }
                        //adspecialId = GetAdSpecialId(int.Parse(actualSales.Rows[rowIndex][8].ToString()));
                        //oleDbCommand.CommandText =
                        //    "INSERT INTO ActualSales (Sold, SalePrice, TotalSales, Cost, ProfitReturn, TotalProfitReturn, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                        //SELECT AdItem.AdItem, APC.ProjectionSold, APC.ProjectionSalePrice, APC.ProjectionTotalSales, APC.ProjectionCost, APC.ProjectionProfitReturn, APC.ProjectionTotalProfitReturn, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC

                        //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
                        actualSale.Sold = actualSales.Rows[rowIndex][1].ToString();
                        actualSale.SalePrice = actualSales.Rows[rowIndex][2].ToString();
                        actualSale.TotalSales = decimal.Parse(actualSales.Rows[rowIndex][3].ToString());
                        actualSale.Cost = decimal.Parse(actualSales.Rows[rowIndex][4].ToString());
                        actualSale.ProfitReturn = decimal.Parse(actualSales.Rows[rowIndex][5].ToString());
                        actualSale.TotalProfitReturn = decimal.Parse(actualSales.Rows[rowIndex][6].ToString());
                        actualSale.FkAdItemId = adItemId;
                        actualSale.RowAttribute = int.Parse(actualSales.Rows[rowIndex][7].ToString());
                        if ((int)actualSales.Rows[rowIndex][8] == 0 && adSpecialId == -1)
                        {
                            actualSale.FkAdSpecialId = null;
                        }
                        else
                        {
                            if ((int)(projections.Rows[rowIndex][8]) == 0)
                            {
                                actualSale.FkAdSpecialId = adSpecialId;
                            }
                            else
                            {
                                adSpecialId = (int)actualSales.Rows[rowIndex][8];
                                actualSale.FkAdSpecialId = (int)actualSales.Rows[rowIndex][8];
                            }
                        }
                        actualSale.RowPosition = rowIndex + 1;
                        actualSale.FkDateId = date.Id; //db.WeekEndingDates.First(x => x.Id == dateId);
                        //oleDbCommand.Parameters.AddWithValue("Sold", actualSales.Rows[rowIndex][1]);
                        //oleDbCommand.Parameters.AddWithValue("SalesPrice", actualSales.Rows[rowIndex][2]);
                        //oleDbCommand.Parameters.AddWithValue("TotalSales", actualSales.Rows[rowIndex][3]);
                        //oleDbCommand.Parameters.AddWithValue("Cost", actualSales.Rows[rowIndex][4]);
                        //oleDbCommand.Parameters.AddWithValue("ProfitReturn", actualSales.Rows[rowIndex][5]);
                        //oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", actualSales.Rows[rowIndex][6]);
                        //oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
                        //oleDbCommand.Parameters.AddWithValue("RowAttribute",
                        //    int.Parse(projections.Rows[rowIndex][7].ToString()));
                        //oleDbCommand.Parameters.AddWithValue("adSpecialID", adspecialId);
                        //oleDbCommand.Parameters.AddWithValue("RowPosition", (rowIndex + 1));
                        //oleDbCommand.Parameters.AddWithValue("dateID", dateId);
                        //oleDbCommand.ExecuteNonQuery();
                        //oleDbCommand.Parameters.Clear();
                        db.ActualSales.Add(actualSale);
                    }

                    for (var i = 0; i < invoice.Rows.Count; i++)
                    {
                        var invoiceObject = new Invoice();
                        var supplier = new Supplier();
                        var spam = invoice.Rows[i][1].ToString();
                        if (db.Suppliers.Any(x => x.Name == spam))
                        {
                            supplier = db.Suppliers.First(x => x.Name == spam);
                        }
                        else
                        {
                            supplier.Name = invoice.Rows[i][1].ToString();
                            db.Suppliers.Add(supplier);
                        }
                        invoiceObject.InvoiceDate = DateTime.Parse(invoice.Rows[i][0].ToString());
                        invoiceObject.InvoiceNumber = invoice.Rows[i][2].ToString();
                        invoiceObject.InvoiceNetAmountAtCost = decimal.Parse(invoice.Rows[i][3].ToString());
                        invoiceObject.InvoiceNetAmount = decimal.Parse(invoice.Rows[i][4].ToString());
                        invoiceObject.InvoiceNote = invoice.Rows[i][5].ToString();
                        invoiceObject.FkSupplierId = supplier.Id;
                        invoiceObject.FkDateId = date.Id;
                        db.Invoices.Add(invoiceObject);
                        //    //SELECT 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.CommandText =
                        //        "INSERT INTO Invoice (InvoiceDate, InvoiceNumber, InvoiceNetAmountAtCost, InvoiceNetAmount, InvoiceNote, FK_Supplier, FK_DateID) VALUES(?,?,?,?,?,?,?)";
                        //    oleDbCommand.Parameters.AddWithValue("InvoiceDate", invoice.Rows[i][0].ToString());
                        //    oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoice.Rows[i][2].ToString());
                        //    oleDbCommand.Parameters.AddWithValue("InvoiceNetAmountAtCost",
                        //        invoice.Rows[i][3].ToString() == "" ? 0 : double.Parse(invoice.Rows[i][3].ToString()));
                        //    oleDbCommand.Parameters.AddWithValue("InvoiceNetAmount",
                        //        invoice.Rows[i][4].ToString() == "" ? 0 : double.Parse(invoice.Rows[i][4].ToString()));
                        //    oleDbCommand.Parameters.AddWithValue("InvoiceNote", invoice.Rows[i][5].ToString());
                        //    oleDbCommand.Parameters.AddWithValue("FK_Supplier", supplier.Id);
                        //    oleDbCommand.Parameters.AddWithValue("FK_DateID", dateId);
                        //    oleDbCommand.ExecuteNonQuery();
                        //    oleDbCommand.Parameters.Clear();
                    }
                    if (weeklySales.Rows.Count == 1)
                    {
                        var weeklySale = new WeeklySale
                        {
                            Sunday = decimal.Parse(weeklySales.Rows[0][0].ToString()),
                            Monday = decimal.Parse(weeklySales.Rows[0][1].ToString()),
                            Tuesday = decimal.Parse(weeklySales.Rows[0][2].ToString()),
                            Wednesday = decimal.Parse(weeklySales.Rows[0][3].ToString()),
                            Thursday = decimal.Parse(weeklySales.Rows[0][4].ToString()),
                            Friday = decimal.Parse(weeklySales.Rows[0][5].ToString()),
                            Saturday = decimal.Parse(weeklySales.Rows[0][6].ToString()),
                            TotalSales = decimal.Parse(weeklySales.Rows[0][7].ToString()),
                            FkDateId = date.Id
                        };
                        db.WeeklySales.Add(weeklySale);                        
                    }

                    if (taxableTable.Rows.Count == 1)
                    {
                        //Comes from DatabaseReader class, so offset by one because zero is the ID number.
                        var taxable = new Taxable
                        {
                            Sunday = decimal.Parse(taxableTable.Rows[0][1].ToString()),
                            Monday = decimal.Parse(taxableTable.Rows[0][2].ToString()),
                            Tuesday = decimal.Parse(taxableTable.Rows[0][3].ToString()),
                            Wednesday = decimal.Parse(taxableTable.Rows[0][4].ToString()),
                            Thursday = decimal.Parse(taxableTable.Rows[0][5].ToString()),
                            Friday = decimal.Parse(taxableTable.Rows[0][6].ToString()),
                            Saturday = decimal.Parse(taxableTable.Rows[0][7].ToString()),
                            Total = decimal.Parse(taxableTable.Rows[0][8].ToString()),
                            FkDateId = date.Id                        
                        };
                        db.Taxables.Add(taxable);                        
                    }
                    //SELECT CostOfSalesAnalysis.ID, CostOfSalesAnalysis.SalesPerManHour, CostOfSalesAnalysis.SalaryPercentage, CostOfSalesAnalysis.SalaryDollars, CostOfSalesAnalysis.Supplies FROM CostOfSalesAnalysis WHERE FK_DateID = ?
                    if (costOfSales.Rows.Count == 1)
                    {
                       var cost = new CostAnalysi
                        {
                            SalesPerManHour = decimal.Parse(costOfSales.Rows[0][1].ToString()),
                            SalaryPercentage = decimal.Parse(costOfSales.Rows[0][2].ToString()),
                            SalaryDollar = decimal.Parse(costOfSales.Rows[0][3].ToString()),
                            Supplies = decimal.Parse(costOfSales.Rows[0][4].ToString()),
                            FkDateId = date.Id
                        };
                        db.CostAnalysis.Add(cost);                        
                    }
 
                    if (!string.IsNullOrEmpty(comments))
                    {
                        var comment = new Note
                        {
                            Remark = comments,
                            FkDateId = date.Id
                        };
                        db.Notes.Add(comment);
                    }
                    //oleDbCommand.Parameters.Clear();
                    //oleDbCommand.CommandText = "INSERT INTO Comment (Comment, FK_DateID) VALUES (?, ?)";
                    //oleDbCommand.Parameters.AddWithValue("Com", comments);
                    //oleDbCommand.Parameters.AddWithValue("FK", dateId);
                    //oleDbCommand.ExecuteNonQuery();
                    //oleDbTransaction.Commit(); 
                    db.SaveChanges();
                    transaction.Complete();
                }
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.Message);
                //oleDbTransaction?.Rollback();
            }
        }

        public List<string> GetAdItems()
        {
            var adItems = 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();
                    var reader = oleDbCommand.ExecuteReader();
                    while (reader != null && reader.Read())
                    {
                        adItems.Add(reader[0].ToString());
                    }
                }
            }
            return adItems;
        }

        public List<string> GetSuppliers()
        {
            var adItems = new List<string>();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT Supplier.SupplierName FROM Supplier ORDER BY Supplier.SupplierName ASC"
            };
            var connection = new OleDbConnection(_connectionString);
            oleDbCommand.Connection = connection;
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    var reader = oleDbCommand.ExecuteReader();
                    while (reader != null && reader.Read())
                    {
                        adItems.Add(reader[0].ToString());
                    }
                }
            }
            return adItems;
        }

        public List<string> GetAdSpecials()
        {
            var adItems = 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();
                    var reader = oleDbCommand.ExecuteReader();
                    while (reader != null && reader.Read())
                    {
                        adItems.Add(reader[0].ToString());
                    }
                }
            }
            return adItems;
        }

        public int GetSupplierId(string supplierName)
        {
            var supplier = new Supplier();
            var db = new AdvertisingProfitControlModel();
            try
            {
                if (db.Suppliers.Any(x => x.Name == supplierName))
                {
                    return db.Suppliers.First(x => x.Name == supplierName).Id;
                }
                //No match
                supplier.Name = supplierName;
                db.Suppliers.Add(supplier);
                db.SaveChanges();
                return supplier.Id;
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.Message);
            }
            return 0;
        }

        private string GetComments(int dateId)
        {
            var comments = "";
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT 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();
                    var reader = oleDbCommand.ExecuteReader();
                    while (reader != null && reader.Read())
                    {
                        comments = reader[0].ToString();
                    }
                }
            }

            return comments;
        }

        public string RetrieveAdItemName(int id)
        {
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT AdItem.AdItem FROM AdItem WHERE AdItem.ID = ?"
            };
            oleDbCommand.Parameters.AddWithValue("ding", id);
            var connection = new OleDbConnection(_connectionString);
            oleDbCommand.Connection = connection;
            var adItemName = "";
            using (connection)
            {
                using (oleDbCommand)
                {
                    connection.Open();
                    using (var reader = oleDbCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            adItemName = reader[0].ToString();
                        }
                    }
                }
                connection.Close();
            }

            return adItemName;
        }

        private int GetDateIdByDateString(string dateString)
        {
            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());
                        }
                    }
                }
                connection.Close();
            }

            return dateId;
        }

        private DataTable ReturnProjections(int dateId)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand
            {
                CommandText = "SELECT 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);
                    }
                }
                connection.Close();
            }
            return dataTable;
        }

        public DataTable ReturnInventory(int dateId)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT 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);
                    }
                }
                connection.Close();
            }
            return dataTable;
        }

        public DataTable ReturnActualSales(int dateId)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT 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);
                    }
                }
                connection.Close();
            }
            return dataTable;
        }

        public DataTable ReturnInvoiceTable(int dateId)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT 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);
                    }
                }
                connection.Close();
            }

            return dataTable;
        }

        public DataTable ReturnWeeklySalesFromDateId(int dateId)
        {
            var dataTable = new DataTable();
            var oleDbCommand = new OleDbCommand()
            {
                CommandText = "SELECT 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);
                    }
                }
                connection.Close();
            }

            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;
        }
    }
}
