Fixed a bug where the row row would get reported as the last row processed in all the database insert and update methods. Fixed a bug where the NewRow would get set as dirty if an ad special row was renamed with no members in it. Sales table compression and database methods tested and working.

This commit is contained in:
2016-11-11 12:43:47 -06:00
parent d0c6ce5e42
commit 9105429fcb
13 changed files with 321 additions and 584 deletions
+188 -486
View File
@@ -615,21 +615,34 @@ namespace AdvertsingProfitControl
//Add overflow protection
if (index < projectionsDataGridView.RowCount)
{
projectionsDataGridView.Rows[index].Cells[(int) SalesTableColumns.IsDirty].Value = true;
if (!projectionsDataGridView.Rows[index].IsNewRow)
{
projectionsDataGridView.Rows[index].Cells[(int) SalesTableColumns.IsDirty].Value = true;
}
}
if (index < actualSalesDataGridView.RowCount)
{
actualSalesDataGridView.Rows[index].Cells[(int)SalesTableColumns.IsDirty].Value = true;
if (!actualSalesDataGridView.Rows[index].IsNewRow)
{
actualSalesDataGridView.Rows[index].Cells[(int)SalesTableColumns.IsDirty].Value = true;
}
}
if (index < inventoryDataGridView.RowCount)
{
inventoryDataGridView.Rows[index].Cells[(int)InventoryTableColumns.IsDirty].Value = true;
if (!inventoryDataGridView.Rows[index].IsNewRow)
{
inventoryDataGridView.Rows[index].Cells[(int)InventoryTableColumns.IsDirty].Value = true;
}
}
}
}
_usedAdItems[_adSpecialIndex == -1 || e.RowIndex < _adSpecialIndex ? 0 : 1].Remove(_beginningCellValue);
dataGridView.Rows[e.RowIndex].Cells[(int) SalesTableColumns.IsDirty].Value = true;
}
else if (userInput != _beginningCellValue)
{
dataGridView.Rows[e.RowIndex].Cells[(int)SalesTableColumns.IsDirty].Value = true;
}
double parsedNumber;
//Check to see if the current column is the ad item column.
switch (e.ColumnIndex)
@@ -1041,21 +1054,27 @@ namespace AdvertsingProfitControl
dataGridView.RefreshEdit();
return;
}
else
//Mark all ad special members as dirty since the user changed the ad special.
for (var index = _adSpecialIndex + 1; index < dataGridView.RowCount; index++)
{
//Mark all ad special members as dirty since the user changed the ad special.
for (var index = _adSpecialIndex + 1; index < dataGridView.RowCount; index++)
//Add overflow protection
if (index < projectionsDataGridView.RowCount)
{
//Add overflow protection
if (index < projectionsDataGridView.RowCount)
if (!projectionsDataGridView.Rows[index].IsNewRow)
{
projectionsDataGridView.Rows[index].Cells[(int)SalesTableColumns.IsDirty].Value = true;
}
if (index < actualSalesDataGridView.RowCount)
}
if (index < actualSalesDataGridView.RowCount)
{
if (!actualSalesDataGridView.Rows[index].IsNewRow)
{
actualSalesDataGridView.Rows[index].Cells[(int)SalesTableColumns.IsDirty].Value = true;
}
if (index < inventoryDataGridView.RowCount)
}
if (index < inventoryDataGridView.RowCount)
{
if (!inventoryDataGridView.Rows[index].IsNewRow)
{
inventoryDataGridView.Rows[index].Cells[(int)InventoryTableColumns.IsDirty].Value = true;
}
@@ -1065,6 +1084,10 @@ namespace AdvertsingProfitControl
_usedAdItems[_adSpecialIndex == -1 || e.RowIndex < _adSpecialIndex ? 0 : 1].Remove(_beginningCellValue);
dataGridView.Rows[e.RowIndex].Cells[(int) InventoryTableColumns.IsDirty].Value = true;
}
else if (userInput != _beginningCellValue)
{
dataGridView.Rows[e.RowIndex].Cells[(int)InventoryTableColumns.IsDirty].Value = true;
}
//Check to see if the current column is the ad item column.
switch (e.ColumnIndex)
{
@@ -1838,27 +1861,70 @@ namespace AdvertsingProfitControl
}
//Run the row parsing engine on all the APC tables.
//Create the cleaned table objects that will be sent to the database.
var trimmedTable = new DataTable();
var updateTable = new DataTable();
//var operationStatus = ConstructCleanedProjectionsTable(dateId, out trimmed, out update);
//operationStatus = ConstructCleanedInventoryTable(dateId);
//operationgStatus = ConstructCleanedActualSalesTable(dateId);
DataTable trimmedTable;
DataTable updateTable;
DbWriterStatus dbWriterStatus;
informationLabel.Text = @"Preparing data tables...";
var operationStatus = ConstructCleanedSalesTable("Projections", dateId, out trimmedTable, out updateTable);
switch (operationStatus)
{
case TrimmingOperationResult.NoChangesRequired:
informationLabel.Text += Environment.NewLine + @"No changes for the Projections table detected.";
break;
case TrimmingOperationResult.CreatedNewInsertionTable:
informationLabel.Text += Environment.NewLine + @"New table created.";
dbWriterStatus = dbW.InsertIntoSalesTable(trimmedTable, dbT.DatabaseConnectionString);
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.Id].Value = rowIndex.Value;
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsInDatabase].Value = true;
}
break;
case TrimmingOperationResult.CreatedUpdateTable:
informationLabel.Text += Environment.NewLine + @"Updates required.";
dbWriterStatus = dbW.UpdateSalesTable(updateTable, dbT.DatabaseConnectionString);
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Only reset the IsDirty value to false since the updates when through.
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
}
break;
case TrimmingOperationResult.CreatedNewInsertionAndUpdateTables:
//Insert the new values...
dbWriterStatus = dbW.InsertIntoSalesTable(trimmedTable, dbT.DatabaseConnectionString);
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.Id].Value = rowIndex.Value;
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsInDatabase].Value = true;
}
//... and update the existing values.
dbWriterStatus = dbW.UpdateSalesTable(updateTable, dbT.DatabaseConnectionString);
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Only reset the IsDirty value to false since the updates when through.
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
}
break;
case TrimmingOperationResult.FailedToTrim:
errorLabel.Text = @"Failed to trim.";
informationLabel.Text = "";
return;
}
//Create the transaction scope.
//By default the TransactionScopeOption is "Required", so if an ambient transaction does not
//exist then the new transaction that is made (in the first method) becomes the root transaction.
//Transaction Scope: https://msdn.microsoft.com/en-us/library/ms172152.aspx
//var projectionsAdditions = dbW.InsertIntoSalesTable(trimmedTable, dbT.DatabaseConnectionString);
//foreach (var rowIndex in projectionsAdditions)
//{
// projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int) SalesTableColumns.Id].Value = rowIndex.Value;
// projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
// projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsInDatabase].Value = true;
//}
}
#region APC Table Trimming
private TrimmingOperationResult ConstructCleanedProjectionsTable(int dateId, out DataTable insertNewTable, out DataTable updateExistingTable)
private TrimmingOperationResult ConstructCleanedSalesTable(string tableName, int dateId, out DataTable insertNewTable, out DataTable updateExistingTable)
{
//Create two DataTables one for the new items to be added to the database
//and one for items that have to be updated.
@@ -1866,12 +1932,14 @@ namespace AdvertsingProfitControl
//0:Sold 1:SalePrice 2:TotalSales 3:cost 4:ProfitReturn 5:TotalProfitReturn
//6:FK_AdItemID 7:RowAttribute 8:FK_AdSpecialGroupName (ID) 9:RowPosition (not index based)
//10:FK_DateID
insertNewTable = new DataTable("Projections");
insertNewTable = new DataTable(tableName);
//Update Sales Table Layout (Based on the Database's Physical Layout)
//0:ID 1:Sold 2:SalePrice 3:TotalSales 4:cost 5:ProfitReturn 6:TotalProfitReturn
//7:FK_AdItemID 8:RowAttribute 9:FK_AdSpecialGroupName (ID) 10:RowPosition (not index based)
//11:FK_DateID
updateExistingTable = new DataTable("Projections");
updateExistingTable = new DataTable(tableName);
//Create a reference to the DataGridView that will be used (either Projections or Actual Sales).
var dataGridView = tableName == "Projections" ? projectionsDataGridView : actualSalesDataGridView;
//Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView.
string[] saleColumnNames =
{
@@ -1899,11 +1967,11 @@ namespace AdvertsingProfitControl
{
//An ad special does exist so grab its ID from the database.
int.TryParse(databaseReader.RetrieveGroupIdByString(
projectionsDataGridView.Rows[_adSpecialIndex].Cells[(int)SalesTableColumns.AdItem]
dataGridView.Rows[_adSpecialIndex].Cells[(int)SalesTableColumns.AdItem]
.EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString), out adSpecialId);
}
//Begin spinning through all the rows in the projections table.
foreach (DataGridViewRow row in projectionsDataGridView.Rows)
foreach (DataGridViewRow row in dataGridView.Rows)
{
//Always check for new row.
if (row.IsNewRow) break;
@@ -1934,6 +2002,7 @@ namespace AdvertsingProfitControl
//AdItemInsertionFailedException
insertNewTable.Rows.Clear(); //Work around for now
updateExistingTable.Rows.Clear();
errorLabel.Text = @"Failed to ad item to database.";
return TrimmingOperationResult.FailedToTrim;
}
}
@@ -1941,116 +2010,70 @@ namespace AdvertsingProfitControl
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
//Attempt to grab the index of an ad item, if it is not found then the return value is -1.
var repeatedItemIndex =
_usedAdItems[0].FindIndex(x => x == row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString());
//Check if a repeat was found.
var repeatedItemIndex = _usedAdItems[0].FindIndex(x => x == row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString());
//Skip checking this row if an index is found, and use the values in the row at the previous index found.
if (repeatedItemIndex != -1)
{
//A row index has been found so check to see if the row in question has already been added to any of the trimmed tables.
if ((bool)projectionsDataGridView.Rows[repeatedItemIndex].Cells[(int) SalesTableColumns.IsDirty].Value)
//Add the values to their respective data table.
if (rowIdNumber == 0)
{
//If the row is dirty then its going to be found in either...
if (!(bool)projectionsDataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.IsInDatabase].Value)
//This table is full of data not in the database so the ID column isn't needed.
var newRow = new object[11];
newRow[0] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.Sold].EditedFormattedValue.ToString();//Sold, is string
newRow[1] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.SalePrice].EditedFormattedValue.ToString();//SalePrice, is string
newRow[2] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString());//TotalSales, must be a number
newRow[3] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString());//Cost, must be a number
newRow[4] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString());//ProfitReturn, must be a number
newRow[5] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString());//TotalProfitReturn, must be a number
newRow[6] = adItemId;
if ((bool)dataGridView.Rows[repeatedItemIndex].Cells[(int) SalesTableColumns.IsHeaderRow].Value)
{
//... or the add new items table.
//If the ad item is being used in section one then locate it and update it in the trimmed table.
var foundRow = insertNewTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int) SalesTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed new table.");
}
//Else error condition?
else
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < insertNewTable.Rows.Count; i++)
{
//Check to see if the selected row is equal.
if (foundRow[0] != insertNewTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
insertNewTable.Rows[i][8] = adSpecialId;
}
//Once ad special ID has been updated jump to the next row.
continue;
newRow[7] = 1;
}
else if ((bool)dataGridView.Rows[repeatedItemIndex].Cells[(int) SalesTableColumns.IsMemberRow].Value)
{
newRow[7] = 2;
}
else
{
//... the update existing items table
//If the ad item is being used in section two then locate it and update it in the trimmed table.
var foundRow = updateExistingTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int) SalesTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed update table.");
}
//Else error condition?
else
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < updateExistingTable.Rows.Count; i++)
{
//Check to see if the selected row is equal.
if (foundRow[0] != updateExistingTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
updateExistingTable.Rows[i][8] = adSpecialId;
}
//Once ad special ID has been updated jump to the next row.
continue;
newRow[7] = 0;
}
newRow[8] = adSpecialId;
newRow[9] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
newRow[10] = dateId;
insertNewTable.Rows.Add(newRow);
continue;
}
//If the row is not dirty then it would not be included in any trimmed table just yet.
if ((bool)projectionsDataGridView.Rows[repeatedItemIndex].Cells[(int) SalesTableColumns.IsInDatabase].Value)
else
{
//If its in the database already then add the row to the update table, including the ad special ID number.
//If the ad item is being used in section two then locate it and update it in the trimmed table.
var foundRow = updateExistingTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
//This table is full of data that is already in the database.
var newRow = new object[11];
newRow[0] = rowIdNumber;
newRow[1] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.Sold].EditedFormattedValue.ToString();//Sold, is string
newRow[2] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.SalePrice].EditedFormattedValue.ToString();//SalePrice, is string
newRow[3] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString());//TotalSales, must be a number
newRow[4] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString());//Cost, must be a number
newRow[5] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString());//ProfitReturn, must be a number
newRow[6] = dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString());//TotalProfitReturn, must be a number
newRow[7] = adItemId;
if ((bool)dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.IsHeaderRow].Value)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed update table.");
newRow[8] = 1;
}
//Else error condition?
else
else if ((bool)dataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.IsMemberRow].Value)
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
newRow[8] = 2;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < updateExistingTable.Rows.Count; i++)
else
{
//Check to see if the selected row is equal.
if (foundRow[0] != updateExistingTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
updateExistingTable.Rows[i][8] = adSpecialId;
newRow[8] = 0;
}
//Once ad special ID has been updated jump to the next row.
newRow[9] = adSpecialId;
newRow[10] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
newRow[11] = dateId;
insertNewTable.Rows.Add(newRow);
continue;
}
//else
//{
// //Otherwise add it to the add new item trimmed table (should never happen).
//}
LogConsole.WriteToLog(FrmLogConsole.Level.Info, "Repeated ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue + "' found.");
LogConsole.WriteToLog(FrmLogConsole.Level.Info, "Its row index is " + repeatedItemIndex + ".");
}
}
//Determine the row's attribute.
@@ -2146,11 +2169,11 @@ namespace AdvertsingProfitControl
//New inventory Table Layout (Based on the Database's Physical Layout)
//0:Beginning Inventory 1:Received 2:Total Inventory 3:Ending Inventory
//4:AdItemID 5:RowAttribute 6:GroupID 7:RowPosition 8:DateID
insertNewTable = new DataTable("Projections");
insertNewTable = new DataTable("Inventory");
//Update Sales Table Layout (Based on the Database's Physical Layout)
//0:ID 1:BeginningInventory 2:Received 3:TotalInventory 4:EndingInventory
//5:AdItemID 6:RowAttribute 7:GroupID 8:RowPosition 9:DateID
updateExistingTable = new DataTable("Projections");
updateExistingTable = new DataTable("Inventory");
//Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView.
string[] saleColumnNames =
{
@@ -2222,114 +2245,68 @@ namespace AdvertsingProfitControl
//Attempt to grab the index of an ad item, if it is not found then the return value is -1.
var repeatedItemIndex =
_usedAdItems[0].FindIndex(x => x == row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue.ToString());
//Check if a repeat was found.
//Skip checking this row if an index is found, and use the values in the row at the previous index found.
if (repeatedItemIndex != -1)
{
//A row index has been found so check to see if the row in question has already been added to any of the trimmed tables.
if ((bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.IsDirty].Value)
//Add the values to their respective data table.
if (rowIdNumber == 0)
{
//If the row is dirty then its going to be found in either...
if (!(bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.IsInDatabase].Value)
//This table is full of data not in the database so the ID column isn't needed.
//0:Beginning Inventory 1:Received 2:Total Inventory 3:Ending Inventory
//4:AdItemID 5:RowAttribute 6:GroupID 7:RowPosition 8:DateID
var newRow = new object[9];
newRow[0] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Sold, is string
newRow[1] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.Recieved].EditedFormattedValue.ToString();//SalePrice, is string
newRow[2] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int) InventoryTableColumns.Total].EditedFormattedValue.ToString(); //Total Inventory is string
newRow[3] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString();//Ending Inventory, is string
newRow[4] = adItemId;
if ((bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.IsHeaderRow].Value)
{
//... or the add new items table.
//If the ad item is being used in section one then locate it and update it in the trimmed table.
var foundRow = insertNewTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed new table.");
}
//Else error condition?
else
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < insertNewTable.Rows.Count; i++)
{
//Check to see if the selected row is equal.
if (foundRow[0] != insertNewTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
insertNewTable.Rows[i][8] = adSpecialId;
}
//Once ad special ID has been updated jump to the next row.
continue;
newRow[5] = 1;
}
else if ((bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.IsMemberRow].Value)
{
newRow[5] = 2;
}
else
{
//... the update existing items table
//If the ad item is being used in section two then locate it and update it in the trimmed table.
var foundRow = updateExistingTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed update table.");
}
//Else error condition?
else
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < updateExistingTable.Rows.Count; i++)
{
//Check to see if the selected row is equal.
if (foundRow[0] != updateExistingTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
updateExistingTable.Rows[i][8] = adSpecialId;
}
//Once ad special ID has been updated jump to the next row.
continue;
newRow[5] = 0;
}
newRow[6] = adSpecialId;
newRow[7] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
newRow[8] = dateId;
insertNewTable.Rows.Add(newRow);
continue;
}
//If the row is not dirty then it would not be included in any trimmed table just yet.
if ((bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.IsInDatabase].Value)
else
{
//If its in the database already then add the row to the update table, including the ad special ID number.
//If the ad item is being used in section two then locate it and update it in the trimmed table.
var foundRow = updateExistingTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
//This table is full of data that is already in the database.
//0:ID 1:BeginningInventory 2:Received 3:TotalInventory 4:EndingInventory
//5:AdItemID 6:RowAttribute 7:GroupID 8:RowPosition 9:DateID
var newRow = new object[10];
newRow[0] = rowIdNumber;
newRow[1] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Sold, is string
newRow[2] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.Recieved].EditedFormattedValue.ToString();//SalePrice, is string
newRow[3] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.Total].EditedFormattedValue.ToString(); //Total Inventory is string
newRow[4] = inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString();//Ending Inventory, is string
newRow[5] = adItemId;
if ((bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.IsHeaderRow].Value)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed update table.");
newRow[6] = 1;
}
//Else error condition?
else
else if ((bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.IsMemberRow].Value)
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
newRow[6] = 2;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < updateExistingTable.Rows.Count; i++)
else
{
//Check to see if the selected row is equal.
if (foundRow[0] != updateExistingTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
updateExistingTable.Rows[i][8] = adSpecialId;
newRow[6] = 0;
}
//Once ad special ID has been updated jump to the next row.
newRow[7] = adSpecialId;
newRow[8] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
newRow[9] = dateId;
continue;
}
//else
//{
// //Otherwise add it to the add new item trimmed table (should never happen).
//}
LogConsole.WriteToLog(FrmLogConsole.Level.Info, "Repeated ad item '" + row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue + "' found.");
LogConsole.WriteToLog(FrmLogConsole.Level.Info, "Its row index is " + repeatedItemIndex + ".");
}
}
//Determine the row's attribute.
@@ -2406,281 +2383,6 @@ namespace AdvertsingProfitControl
}
return TrimmingOperationResult.CreatedNewInsertionAndUpdateTables;
}
private TrimmingOperationResult ConstructCleanedActualSalesTable(int dateId, out DataTable insertNewTable, out DataTable updateExistingTable)
{
//Create two DataTables one for the new items to be added to the database
//and one for items that have to be updated.
//New Sales Table Layout (Based on the Database's Physical Layout)
//0:Sold 1:SalePrice 2:TotalSales 3:cost 4:ProfitReturn 5:TotalProfitReturn
//6:FK_AdItemID 7:RowAttribute 8:FK_AdSpecialGroupName (ID) 9:RowPosition (not index based)
//10:FK_DateID
insertNewTable = new DataTable("Projections");
//Update Sales Table Layout (Based on the Database's Physical Layout)
//0:ID 1:Sold 2:SalePrice 3:TotalSales 4:cost 5:ProfitReturn 6:TotalProfitReturn
//7:FK_AdItemID 8:RowAttribute 9:FK_AdSpecialGroupName (ID) 10:RowPosition (not index based)
//11:FK_DateID
updateExistingTable = new DataTable("Projections");
//Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView.
string[] saleColumnNames =
{
"ID", "Sold", "SalePrice", "TotalSales", "Cost", "ProfitReturn",
"TotalProfitReturn", "AdItemID", "RowAttribute", "AdSpecialID", "RowPosition", "DateID"
};
foreach (var columnName in saleColumnNames)
{
if (columnName == "ID") continue;
var column = new DataColumn(columnName);
insertNewTable.Columns.Add(column);
}
foreach (var columnName in saleColumnNames)
{
var column = new DataColumn(columnName);
updateExistingTable.Columns.Add(column);
}
//Create the database interaction objects.
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
var adSpecialId = 0; //Entries in the database are not allowed to be zero (unique ID wise that is).
//Grab the ad special ID, assuming there is one.
if (_adSpecialIndex != -1)
{
//An ad special does exist so grab its ID from the database.
int.TryParse(databaseReader.RetrieveGroupIdByString(
actualSalesDataGridView.Rows[_adSpecialIndex].Cells[(int)SalesTableColumns.AdItem]
.EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString), out adSpecialId);
}
//Begin spinning through all the rows in the projections table.
foreach (DataGridViewRow row in actualSalesDataGridView.Rows)
{
//Always check for new row.
if (row.IsNewRow) break;
//Check to see if the current row is the ad special row.
if (row.Index == _adSpecialIndex)
{
//LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Ad Special Row found at index " + row.Index + ".");
continue;
}
//Check to see if the row is dirty.
if (!(bool)row.Cells[(int)SalesTableColumns.IsDirty].Value)
{
//If it is not then continue on to the next row.
continue;
}
//Try grabbing the row's ID number (the number that it is in the database).
var rowIdNumber = row.Cells[(int)SalesTableColumns.Id].EditedFormattedValue.ToString() == "" ? 0 : int.Parse(row.Cells[(int)SalesTableColumns.Id].EditedFormattedValue.ToString());
//Grab the ad item ID.
var adItemId = int.Parse(databaseReader.RetrieveAdItemId(row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString));
//If the return value is zero (0) then the ad item is not in the database so try to add it.
if (adItemId == 0)
{
//Add the item to the database.
adItemId = databaseWriter.InsertNewAdItem(row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString());
if (adItemId == 0)
{
//TODO: Throw an exception, this can not be allowed.
//AdItemInsertionFailedException
insertNewTable.Rows.Clear(); //Work around for now
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
}
//Check for repeated ad items in the ad special section
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
//Attempt to grab the index of an ad item, if it is not found then the return value is -1.
var repeatedItemIndex =
_usedAdItems[0].FindIndex(x => x == row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString());
//Check if a repeat was found.
if (repeatedItemIndex != -1)
{
//A row index has been found so check to see if the row in question has already been added to any of the trimmed tables.
if ((bool)actualSalesDataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.IsDirty].Value)
{
//If the row is dirty then its going to be found in either...
if (!(bool)actualSalesDataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.IsInDatabase].Value)
{
//... or the add new items table.
//If the ad item is being used in section one then locate it and update it in the trimmed table.
var foundRow = insertNewTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed new table.");
}
//Else error condition?
else
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < insertNewTable.Rows.Count; i++)
{
//Check to see if the selected row is equal.
if (foundRow[0] != insertNewTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
insertNewTable.Rows[i][8] = adSpecialId;
}
//Once ad special ID has been updated jump to the next row.
continue;
}
else
{
//... the update existing items table
//If the ad item is being used in section two then locate it and update it in the trimmed table.
var foundRow = updateExistingTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed update table.");
}
//Else error condition?
else
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < updateExistingTable.Rows.Count; i++)
{
//Check to see if the selected row is equal.
if (foundRow[0] != updateExistingTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
updateExistingTable.Rows[i][8] = adSpecialId;
}
//Once ad special ID has been updated jump to the next row.
continue;
}
}
//If the row is not dirty then it would not be included in any trimmed table just yet.
if ((bool)actualSalesDataGridView.Rows[repeatedItemIndex].Cells[(int)SalesTableColumns.IsInDatabase].Value)
{
//If its in the database already then add the row to the update table, including the ad special ID number.
//If the ad item is being used in section two then locate it and update it in the trimmed table.
var foundRow = updateExistingTable.Select("AdItemID = '" + adItemId + "'");
if (foundRow.Length == 1)
{
LogConsole.WriteToLog(FrmLogConsole.Level.Info,
"Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue +
"' found in the trimmed update table.");
}
//Else error condition?
else
{
insertNewTable.Rows.Clear();
updateExistingTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
//With the row found obtain the row's index and modify it in the table to
//include the ad special ID number. No other modifications are required so we can return.
//Begin spinning through all the rows to find the one selected above.
for (var i = 0; i < updateExistingTable.Rows.Count; i++)
{
//Check to see if the selected row is equal.
if (foundRow[0] != updateExistingTable.Rows[i]) continue;
//If so then update that row index with the group ID number.
updateExistingTable.Rows[i][8] = adSpecialId;
}
//Once ad special ID has been updated jump to the next row.
continue;
}
//else
//{
// //Otherwise add it to the add new item trimmed table (should never happen).
//}
LogConsole.WriteToLog(FrmLogConsole.Level.Info, "Repeated ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue + "' found.");
LogConsole.WriteToLog(FrmLogConsole.Level.Info, "Its row index is " + repeatedItemIndex + ".");
}
}
//Determine the row's attribute.
var rowAttribute = 0; //Zero (0) means no grouping, its not a header nor a member.
if ((bool)row.Cells[(int)SalesTableColumns.IsHeaderRow].Value && !(bool)row.Cells[(int)SalesTableColumns.IsMemberRow].Value)
{
rowAttribute = 1;
}
else if ((bool)row.Cells[(int)SalesTableColumns.IsMemberRow].Value && !(bool)row.Cells[(int)SalesTableColumns.IsHeaderRow].Value)
{
rowAttribute = 2;
}
//Add the values to their respective data table.
if (rowIdNumber == 0)
{
//This table is full of data not in the database so the ID column isn't needed.
var newRow = new object[11];
newRow[0] = row.Cells[(int)SalesTableColumns.Sold].EditedFormattedValue.ToString();//Sold, is string
newRow[1] = row.Cells[(int)SalesTableColumns.SalePrice].EditedFormattedValue.ToString();//SalePrice, is string
newRow[2] = row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString());//TotalSales, must be a number
newRow[3] = row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString());//Cost, must be a number
newRow[4] = row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString());//ProfitReturn, must be a number
newRow[5] = row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString());//TotalProfitReturn, must be a number
newRow[6] = adItemId;
newRow[7] = rowAttribute;
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
newRow[8] = adSpecialId;
newRow[9] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
}
else
{
newRow[8] = 0;
newRow[9] = row.Index + 1;
}
newRow[10] = dateId;
insertNewTable.Rows.Add(newRow);
}
else
{
//This table is full of data that is already in the database.
var newRow = new object[12];
newRow[0] = rowIdNumber;
newRow[1] = row.Cells[(int)SalesTableColumns.Sold].EditedFormattedValue.ToString();//Sold, is string
newRow[2] = row.Cells[(int)SalesTableColumns.SalePrice].EditedFormattedValue.ToString();//SalePrice, is string
newRow[3] = row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString());//TotalSales, must be a number
newRow[4] = row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString());//Cost, must be a number
newRow[5] = row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString());//ProfitReturn, must be a number
newRow[6] = row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString());//TotalProfitReturn, must be a number
newRow[7] = adItemId;
newRow[8] = rowAttribute;
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
newRow[9] = adSpecialId;
newRow[10] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
}
else
{
newRow[9] = 0;
newRow[10] = row.Index + 1;
}
newRow[11] = dateId;
updateExistingTable.Rows.Add(newRow);
}
}
if (insertNewTable.Rows.Count == 0 && updateExistingTable.Rows.Count == 0)
{
return TrimmingOperationResult.NoChangesRequired;
}
if (insertNewTable.Rows.Count > 0 && updateExistingTable.Rows.Count == 0)
{
return TrimmingOperationResult.CreatedNewInsertionTable;
}
if (insertNewTable.Rows.Count == 0 && updateExistingTable.Rows.Count > 0)
{
return TrimmingOperationResult.CreatedUpdateTable;
}
return TrimmingOperationResult.CreatedNewInsertionAndUpdateTables;
}
#endregion
}
}