Completely removed all the old database interaction code, save the database converter form. Updated the formatting in the weekly and taxable areas on the modify record form. Re-enabled holiday checking on the main form.

This commit is contained in:
2017-04-07 04:50:02 -05:00
parent e5fa0a8238
commit 2106879ee8
21 changed files with 842 additions and 18090 deletions
+44 -296
View File
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
namespace AdvertsingProfitControl
@@ -35,22 +33,19 @@ namespace AdvertsingProfitControl
public FrmAddRecord()
{
InitializeComponent();
//Set the maximum and minimum sizes for the form.
MaximumSize = new Size(1200, 650);
MinimumSize = new Size(1000, 550);
}
private void frmAddRecord_Load(object sender, EventArgs e)
{
//Start by grabbing all the AdItems and putting them into memory.
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
_gAdItemCollection = databaseReader.GetAdItemsSuggestionList(databaseTracker.DatabaseConnectionString);
//var databaseTracker = new DatabaseTracker();
//var databaseReader = new DatabaseReader();
//_gAdItemCollection = databaseReader.GetAdItemsSuggestionList(databaseTracker.DatabaseConnectionString);
//
weekEndingDateMaskedTextBox.Leave += OnWeekEndingDateMaskedTextBoxLeave;
//Now pull all the suppliers into memory.
_gSupplierCollection = databaseReader.GetSupplierSuggestionList(databaseTracker.DatabaseConnectionString);
_AdSpecialList = databaseReader.RetrieveAdSpecialList(databaseTracker.DatabaseConnectionString);
//_gSupplierCollection = databaseReader.GetSupplierSuggestionList(databaseTracker.DatabaseConnectionString);
//_AdSpecialList = databaseReader.RetrieveAdSpecialList(databaseTracker.DatabaseConnectionString);
//Event handlers for the Projections DataGridView
projectionsDataGridView.CellValidating += OnCellValidating;
//projectionsDataGridView.RowEnter += DetectAndDisplayIncompleteRows;
@@ -1190,22 +1185,22 @@ namespace AdvertsingProfitControl
return;
}
//Initialize the database reader and writer classes.
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
//var databaseTracker = new DatabaseTracker();
//var databaseReader = new DatabaseReader();
//var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
if (weekEndingDateMaskedTextBox.MaskCompleted)
{
if (databaseReader.RetrieveDateIdByDateString(weekEndingDateMaskedTextBox.Text, databaseTracker.DatabaseConnectionString) == 0)
{
//Assume the date is correct and add it to the database.
//TODO: Check the date vs the last date entered (obtained by sorting dates) and check to see if the two are seven (7) or more days apart.
if (!databaseWriter.InsertIntoWeekEnding(weekEndingDateMaskedTextBox.Text))
{
//IF the date failed to add for whatever reason, inform the user and break.
MessageBox.Show("An error has occurred attempting to write the new date into the database.", "Insert Error");
return;
}
}
//if (databaseReader.RetrieveDateIdByDateString(weekEndingDateMaskedTextBox.Text, databaseTracker.DatabaseConnectionString) == 0)
//{
// //Assume the date is correct and add it to the database.
// //TODO: Check the date vs the last date entered (obtained by sorting dates) and check to see if the two are seven (7) or more days apart.
// if (!databaseWriter.InsertIntoWeekEnding(weekEndingDateMaskedTextBox.Text))
// {
// //IF the date failed to add for whatever reason, inform the user and break.
// MessageBox.Show("An error has occurred attempting to write the new date into the database.", "Insert Error");
// return;
// }
//}
}
else
{
@@ -1213,34 +1208,34 @@ namespace AdvertsingProfitControl
return;
}
//Most recent date ID, the hell was I thinking..
var dateIdString = databaseReader.RetrieveDateIdByDateString(weekEndingDateMaskedTextBox.Text, databaseTracker.DatabaseConnectionString);
var commentsText = commentsTextBox.Text;
if (Regex.Replace(commentsText, @"\s+", "") != "")
{
databaseWriter.RedundantlessInsertIntoComments(commentsTextBox.Text, dateIdString.ToString());
}
//var dateIdString = databaseReader.RetrieveDateIdByDateString(weekEndingDateMaskedTextBox.Text, databaseTracker.DatabaseConnectionString);
//var commentsText = commentsTextBox.Text;
//if (Regex.Replace(commentsText, @"\s+", "") != "")
//{
// databaseWriter.RedundantlessInsertIntoComments(commentsTextBox.Text, dateIdString.ToString());
//}
//Send the table's data to their respective functions.
_gLogConsole.WriteToLog(FrmLogConsole.Level.Info, "Added the following rows to the APC table:");
var rowsEffected = BuildAPCAndSendToDatabase(dateIdString.ToString());
foreach (var i in rowsEffected)
{
_gLogConsole.WriteToLog(FrmLogConsole.Level.Info, (i + 1).ToString());
}
_gLogConsole.WriteToLog(FrmLogConsole.Level.Info, "Added the following rows to the Invoices table:");
rowsEffected = BuildInvoiceTableAndSendToDatabase();
foreach (int i in rowsEffected)
{
_gLogConsole.WriteToLog(FrmLogConsole.Level.Info, (i + 1).ToString());
}
if (InsertWeeklySalesIntoDatabase())
{
_gLogConsole.WriteToLog(FrmLogConsole.Level.Info, "Successfully added weekly sales to the database.");
}
else
{
_gLogConsole.WriteToLog(FrmLogConsole.Level.Info, "Failed to add weekly sales to the database.");
}
//var rowsEffected = BuildAPCAndSendToDatabase(dateIdString.ToString());
//foreach (var i in rowsEffected)
//{
// _gLogConsole.WriteToLog(FrmLogConsole.Level.Info, (i + 1).ToString());
//}
//_gLogConsole.WriteToLog(FrmLogConsole.Level.Info, "Added the following rows to the Invoices table:");
//rowsEffected = BuildInvoiceTableAndSendToDatabase();
//foreach (int i in rowsEffected)
//{
// _gLogConsole.WriteToLog(FrmLogConsole.Level.Info, (i + 1).ToString());
//}
//if (InsertWeeklySalesIntoDatabase())
//{
// _gLogConsole.WriteToLog(FrmLogConsole.Level.Info, "Successfully added weekly sales to the database.");
//}
//else
//{
// _gLogConsole.WriteToLog(FrmLogConsole.Level.Info, "Failed to add weekly sales to the database.");
//}
}
private void closeFileMainMenu_Click(object sender, EventArgs e)
@@ -1248,253 +1243,6 @@ namespace AdvertsingProfitControl
Close();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private List<int> BuildAPCAndSendToDatabase(string dateIdString)
{
var rowsAffected = new List<int>();
//Initialize the database reader and writer classes.
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
var parser = new RowParsing();
//The layout of the table to be sent shall reflect the physical layout of the database's table
//Rows 0-5 are projections, 6-9 are inventory, 10-15 are the actual sales and 16-21 are the row attributes, position, ad item (FK), group (FK) and then the date id (FK).
var parameters = new object[21];
var rowIndexNumber = 0;
var groupId = "";
//Negative one (-1) shows that the variable is not set.
var lastHeaderRowNumber = -1;
var rowPosition = 1;
var table = new DataTable();
//Build the table's columns.
for (var i = 0; i <= 20; i++)
{
var column = new DataColumn { ColumnName = "Column" + i };
table.Columns.Add(column);
}
foreach (var row in projectionsDataGridView.Rows.Cast<DataGridViewRow>().TakeWhile(row => !row.IsNewRow))
{
//Get the current row's attribute.
var currentRowAttribute = parser.GetRowAttribute(row);
//IF the current row is invalid then skip it.
if (currentRowAttribute == RowAttribute.IncompleteRow || currentRowAttribute == RowAttribute.MemberRow && rowIndexNumber == 0)
{
continue;
}
//IF the current row is the ad special row, then fill the group ID, increment the row index and move on.
if(rowIndexNumber == _gAdSpecialIndex)
{
//Get the group ID for the Ad Special Row.
groupId =
databaseReader.RetrieveGroupIdByString(
parser.CheckForGroupKeyWord(row.Cells[0].EditedFormattedValue.ToString()),
databaseTracker.DatabaseConnectionString);
rowIndexNumber++;
continue;
}
//Add the ad item into it's respective column.
parameters[18] = row.Cells[0].EditedFormattedValue.ToString();
//BEGIN row attribute check.
//Do an in-depth check to determine if the row should be marked with a "Header Row" group tag.
//Only rows that have members will be explicitly marked as such.
if (currentRowAttribute == RowAttribute.HeaderRow)
{
if ((rowIndexNumber + 1) <= projectionsDataGridView.Rows.Count)
{
//Check to see if the next row is a Member Row.
if ((parser.GetRowAttribute(projectionsDataGridView.Rows[rowIndexNumber + 1])) == RowAttribute.MemberRow)
{
//Set the current row's attribute to HeaderRow in the database.
parameters[16] = "1";
lastHeaderRowNumber = rowIndexNumber;
}
//IF the next row is not a member row, then do not apply any attribute to this row.
else
{
//Clear the row number to prevent any mix ups down the line and the row attribute.
lastHeaderRowNumber = -1;
parameters[16] = "0";
}
}
}
else if (currentRowAttribute == RowAttribute.MemberRow && lastHeaderRowNumber != -1)
{
//ELSE IF this row is a member, then mark it as such.
parameters[16] = "2";
}
//END row attribute check.
//BEGIN checking to see if the current row is an ad special row.
if (rowIndexNumber > _gAdSpecialIndex && _gAdSpecialIndex != -1)
{
//IF the ad item is already in the used ad item list, then skip it as it has already been added to the data table.
if (_gUsedAdItems.Contains(parameters[18] + ":1"))
{
//IF this item has already been used, then search for its row that's in the data table and update it.
var indexOfUsedItem = table.Rows.IndexOf(table.Select("column18 = '" + row.Cells[0].EditedFormattedValue + "'").First());
_gLogConsole.WriteToLog(FrmLogConsole.Level.Debug,
"Matched index at " + indexOfUsedItem + " with the item " +
row.Cells[0].EditedFormattedValue + ".");
if (indexOfUsedItem != -1)
{
table.Rows[indexOfUsedItem][19] = databaseReader.RetrieveGroupIdByString(groupId,
databaseTracker.DatabaseConnectionString);
}
}
else
{
//ELSE if the ad item isn't used, then simply add the group id to the current row.
parameters[19] = groupId;
}
}
else
{
//Add a zero to ensure that null is not sent to the database.
parameters[19] = "0";
}
//Now add the contents of the cells to the parameters.
parameters[20] = dateIdString;
var cellNumberTotal = 0;
var currentCellIndex = 0;
foreach (DataGridViewCell cell in projectionsDataGridView.Rows[rowIndexNumber].Cells)
{
if (currentCellIndex != 0)
{
var formattedValue = row.Cells[currentCellIndex].FormattedValue;
if (formattedValue != null && formattedValue.ToString() == "")
{
parameters[cellNumberTotal] = "0";
}
else
{
var value = row.Cells[currentCellIndex].FormattedValue;
if (value != null)
parameters[cellNumberTotal] = value.ToString();
}
cellNumberTotal++;
}
currentCellIndex++;
}
currentCellIndex = 0;
foreach (DataGridViewCell cell in actualSalesDataGridView.Rows[rowIndexNumber].Cells)
{
if (currentCellIndex != 0)
{
var formattedValue = actualSalesDataGridView.Rows[rowIndexNumber].Cells[currentCellIndex].FormattedValue;
if (formattedValue != null && formattedValue.ToString() == "")
{
parameters[cellNumberTotal] = "0";
}
else
{
var value = actualSalesDataGridView.Rows[rowIndexNumber].Cells[currentCellIndex].FormattedValue;
if (value !=
null)
parameters[cellNumberTotal] = value.ToString();
}
cellNumberTotal++;
}
currentCellIndex++;
}
//Finally add the row's position to the parameters. converting to a non zero indexed number.
parameters[17] = rowPosition;
table.Rows.Add(parameters);
rowPosition++;
rowIndexNumber++;
}
rowsAffected = databaseWriter.RedundantlessInsertIntoApc(table);
return rowsAffected;
}
private List<int> BuildInvoiceTableAndSendToDatabase()
{
var databaseTracker = new DatabaseTracker();
var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
var databaseReader = new DatabaseReader();
var supplierTable = new DataTable();
//Build the new DataTable so that the scheme is the same.
for (var i = 0; i <= 9; i++)
{
var column = new DataColumn {ColumnName = "Column" + i};
supplierTable.Columns.Add(column);
}
foreach (DataGridViewRow row in suppliersDataGridView.Rows)
{
var newRow = supplierTable.NewRow();
newRow[0] = row.Cells[0].EditedFormattedValue.ToString();
newRow[1] = row.Cells[1].EditedFormattedValue.ToString();
newRow[2] = row.Cells[2].EditedFormattedValue.ToString();
newRow[3] = row.Cells[3].EditedFormattedValue.ToString();
newRow[4] = row.Cells[4].EditedFormattedValue.ToString();
newRow[5] = row.Cells[5].EditedFormattedValue.ToString();
newRow[6] = databaseReader.RetrieveDateIdByDateString(weekEndingDateMaskedTextBox.Text, databaseTracker.DatabaseConnectionString);
supplierTable.Rows.Add(newRow);
}
var rowsAdded = databaseWriter.RedundantlessInsertIntoInvoice(supplierTable);
return rowsAdded;
}
private bool InsertWeeklySalesIntoDatabase()
{
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var wasSuccessful = false;
var rowsEffected = 0;
var dateId = databaseReader.RetrieveDateIdByDateString(weekEndingDateMaskedTextBox.Text, databaseTracker.DatabaseConnectionString);
var tracker = new DatabaseTracker();
var writer = new DatabaseWriter(tracker.DatabaseConnectionString);
var weeklySalesTable = new DataTable();
weeklySalesTable.Columns.Add("Sunday", typeof(string));
weeklySalesTable.Columns.Add("Monday", typeof(string));
weeklySalesTable.Columns.Add("Tuesday", typeof(string));
weeklySalesTable.Columns.Add("Wednesday", typeof(string));
weeklySalesTable.Columns.Add("Thursday", typeof(string));
weeklySalesTable.Columns.Add("Friday", typeof(string));
weeklySalesTable.Columns.Add("Saturday", typeof(string));
weeklySalesTable.Columns.Add("TotalSales", typeof(string));
weeklySalesTable.Columns.Add("DateID", typeof(string));
var sunday = weeklySalesDataGridView.Rows[0].Cells[0].EditedFormattedValue.ToString();
var monday = weeklySalesDataGridView.Rows[0].Cells[1].EditedFormattedValue.ToString();
var tuesday = weeklySalesDataGridView.Rows[0].Cells[2].EditedFormattedValue.ToString();
var wednesday = weeklySalesDataGridView.Rows[0].Cells[3].EditedFormattedValue.ToString();
var thursday = weeklySalesDataGridView.Rows[0].Cells[4].EditedFormattedValue.ToString();
var friday = weeklySalesDataGridView.Rows[0].Cells[5].EditedFormattedValue.ToString();
var saturday = weeklySalesDataGridView.Rows[0].Cells[6].EditedFormattedValue.ToString();
var totalSales = weeklySalesDataGridView.Rows[0].Cells[7].EditedFormattedValue.ToString();
weeklySalesTable.Rows.Add(sunday, monday, tuesday, wednesday, thursday, friday, saturday, totalSales, dateId);
rowsEffected = writer.RedundantlessInsertIntoWeeklySales(weeklySalesTable.Rows[0]);
if (rowsEffected == 1)
{
wasSuccessful = true;
}
return wasSuccessful;
}
private List<int> InsertSalesAndInventoryIntoDatabase()
{
var rowsAdded = new List<int>();
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
return rowsAdded;
}
private void clearFormFileMainMenu_Click(object sender, EventArgs e)
{
projectionsDataGridView.Rows.Clear();