From f60cc5ab8bd4ad967040a697918819b11e00235b Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sun, 6 Nov 2016 22:51:36 -0600 Subject: [PATCH] First implementation of ad item repeat detection for all APC tables. Still need to update comments in inventory and actual sales methods. No testing done as of now. --- AdvertsingProfitControl/NewAddRecord.cs | 535 +++++++++++++++--------- 1 file changed, 334 insertions(+), 201 deletions(-) diff --git a/AdvertsingProfitControl/NewAddRecord.cs b/AdvertsingProfitControl/NewAddRecord.cs index dabe105..7a5f789 100644 --- a/AdvertsingProfitControl/NewAddRecord.cs +++ b/AdvertsingProfitControl/NewAddRecord.cs @@ -1673,7 +1673,7 @@ namespace AdvertsingProfitControl #region APC Table Trimming - private TrimmingOperationResult ConstructCleanedProjectionsTable(int dateId, out DataTable trimmedNewProjectionsTable, out DataTable trimmedUpdateProjectionsTable) + private TrimmingOperationResult ConstructCleanedProjectionsTable(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. @@ -1681,12 +1681,12 @@ 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 - trimmedNewProjectionsTable = new DataTable("Projections"); + 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 - trimmedUpdateProjectionsTable = new DataTable("Projections"); + updateExistingTable = new DataTable("Projections"); //Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView. string[] saleColumnNames = { @@ -1697,12 +1697,12 @@ namespace AdvertsingProfitControl { if (columnName == "ID") continue; var column = new DataColumn(columnName); - trimmedNewProjectionsTable.Columns.Add(column); + insertNewTable.Columns.Add(column); } foreach (var columnName in saleColumnNames) { var column = new DataColumn(columnName); - trimmedUpdateProjectionsTable.Columns.Add(column); + updateExistingTable.Columns.Add(column); } //Create the database interaction objects. var databaseTracker = new DatabaseTracker(); @@ -1747,8 +1747,8 @@ namespace AdvertsingProfitControl { //TODO: Throw an exception, this can not be allowed. //AdItemInsertionFailedException - trimmedNewProjectionsTable.Rows.Clear(); //Work around for now - trimmedUpdateProjectionsTable.Rows.Clear(); + insertNewTable.Rows.Clear(); //Work around for now + updateExistingTable.Rows.Clear(); return TrimmingOperationResult.FailedToTrim; } } @@ -1769,95 +1769,104 @@ namespace AdvertsingProfitControl { //... 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 = trimmedNewProjectionsTable.Select("AdItemID = '" + adItemId + "'"); + var foundRow = insertNewTable.Select("AdItemID = '" + adItemId + "'"); if (foundRow.Length == 1) { LogConsole.WriteToLog(FrmLogConsole.Level.Info, - "Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue + + "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 = trimmedUpdateProjectionsTable.Select("AdItemID = '" + adItemId + "'"); + var foundRow = updateExistingTable.Select("AdItemID = '" + adItemId + "'"); if (foundRow.Length == 1) { LogConsole.WriteToLog(FrmLogConsole.Level.Info, - "Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue + + "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 + //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) { - //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) + //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) { - //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 = trimmedUpdateProjectionsTable.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? - //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. + LogConsole.WriteToLog(FrmLogConsole.Level.Info, + "Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue + + "' found in the trimmed update table."); } - //else - //{ - // //Otherwise add it to the add new item trimmed table (should never happen). - //} + //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 + "."); } - //if (_usedAdItems[0].Contains(row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString())) - //{ - // //If the ad item is being used in section one then locate it and update it in the trimmed table. - // var foundRow = trimmedTables[tableIndex].Select("AdItemID = '" + adItemId + "'"); - // if (foundRow.Length == 1) - // { - // LogConsole.WriteToLog(FrmLogConsole.Level.Info, - // "Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue + - // "' found in the trimmed table."); - // } - // else - // { - // //TODO: Throw an exception, this can not be allowed. - // //InvalidTrimmedRowCountException - // trimmedTables[0].Rows.Clear(); //Work around for now - // trimmedTables[1].Rows.Clear(); - // return TrimmingOperationResult.FailedToTrim; - // } - // //Begin spinning through all the rows to find the one selected above. - // for (var i = 0; i < trimmedTables[tableIndex].Rows.Count; i++) - // { - // //Check to see if the selected row is equal. - // if (foundRow[0] != trimmedTables[tableIndex].Rows[i]) continue; - // //If so then update that row index with the group ID number. - // if (tableIndex == 0) - // { - // trimmedTables[0].Rows[i][8] = adSpecialId; - // } - // else - // { - // trimmedTables[1].Rows[i][9] = adSpecialId; - // } - // } - // //Once ad special ID has been updated jump to the next row. - // continue; - //} } //Determine the row's attribute. var rowAttribute = 0; //Zero (0) means no grouping, its not a header nor a member. @@ -1869,11 +1878,6 @@ namespace AdvertsingProfitControl { rowAttribute = 2; } - //Since we've made it this far, add the ad item into the dictionary if we're not in the ad special group. - if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex) - { - //usedAdItems.Add(row.Index, "s"); - } //Add the values to their respective data table. if (rowIdNumber == 0) { @@ -1898,7 +1902,7 @@ namespace AdvertsingProfitControl newRow[9] = row.Index + 1; } newRow[10] = dateId; - trimmedNewProjectionsTable.Rows.Add(newRow); + insertNewTable.Rows.Add(newRow); } else { @@ -1924,7 +1928,7 @@ namespace AdvertsingProfitControl newRow[10] = row.Index + 1; } newRow[11] = dateId; - trimmedUpdateProjectionsTable.Rows.Add(newRow); + updateExistingTable.Rows.Add(newRow); } //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Row Dump for row number " + (row.Index + 1) + "."); //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "ID : " + rowIdNumber + " Sold: " + row.Cells[(int)SalesTableColumns.Sold].EditedFormattedValue); @@ -1938,42 +1942,42 @@ namespace AdvertsingProfitControl return TrimmingOperationResult.CreatedNewInsertionAndUpdateTables; } - private IEnumerable ConstructCleanedInventoryTable(int dateId) + private TrimmingOperationResult ConstructCleanedInventoryTable(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:BeginingInventory 1:Received 2:TotalInventory 3:EndingInventory 4:AdItemID 5:RowAttribute - //6:FK_AdSpecialGroupName (ID) 7:RowPosition (not index based) 8:DateID - var trimmedNewInventoryTable = new DataTable("Inventory"); + //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:BeginingInventory 2:Received 3:TotalInventory 4:EndingInventory 5:AdItemID 6:RowAttribute - //7:FK_AdSpecialGroupName (ID) 8:RowPosition (not index based) 9:DateID - var trimmedUpdateInventoryTable = new DataTable("Inventory"); - DataTable[] trimmedTables = { trimmedNewInventoryTable, trimmedUpdateInventoryTable }; + //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", "BeginingInventory", "Received", "TotalInventory", "EndingInventory", "AdItemID", "RowAttribute", "AdSpecialID", "RowPosition", "DateID" + "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); - trimmedTables[0].Columns.Add(column); + insertNewTable.Columns.Add(column); } foreach (var columnName in saleColumnNames) { var column = new DataColumn(columnName); - trimmedTables[1].Columns.Add(column); + 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). - //Dictionary The table ID is zero (0) for the new table and one (1) for the update table. - var usedAdItems = new Dictionary(); //Contains ad items that are used in section one, used for checking for repeats. //Grab the ad special ID, assuming there is one. if (_adSpecialIndex != -1) { @@ -1993,10 +1997,10 @@ namespace AdvertsingProfitControl //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Ad Special Row found at index " + row.Index + "."); continue; } - //Check to see if the row is not dirty. + //Check to see if the row is dirty. if (!(bool)row.Cells[(int)InventoryTableColumns.IsDirty].Value) { - //If so simply continue. + //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). @@ -2010,51 +2014,128 @@ namespace AdvertsingProfitControl adItemId = databaseWriter.InsertNewAdItem(row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue.ToString()); if (adItemId == 0) { - errorLabel.Text = @"Failed to insert new ad item."; //TODO: Throw an exception, this can not be allowed. //AdItemInsertionFailedException - trimmedTables[0].Rows.Clear(); //Work around for now - trimmedTables[1].Rows.Clear(); - break; + insertNewTable.Rows.Clear(); //Work around for now + updateExistingTable.Rows.Clear(); + return TrimmingOperationResult.FailedToTrim; } } - int tableIndex; //Check for repeated ad items in the ad special section - if (usedAdItems.TryGetValue(adItemId, out tableIndex)) + if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex) { - //If the ad item is being used in section one then locate it and update it in the trimmed table. - var foundRow = trimmedTables[tableIndex].Select("AdItemID = '" + adItemId + "'"); - if (foundRow.Length == 1) + //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. + if (repeatedItemIndex != -1) { - LogConsole.WriteToLog(FrmLogConsole.Level.Info, - "Ad item '" + row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue + - "' found in the trimmed table."); - } - else - { - //TODO: Throw an exception, this can not be allowed. - //InvalidTrimmedRowCountException - trimmedTables[0].Rows.Clear(); //Work around for now - trimmedTables[1].Rows.Clear(); - break; - } - //Begin spinning through all the rows to find the one selected above. - for (var i = 0; i < trimmedTables[tableIndex].Rows.Count; i++) - { - //Check to see if the selected row is equal. - if (foundRow[0] != trimmedTables[tableIndex].Rows[i]) continue; - //If so then update that row index with the group ID number. - if (tableIndex == 0) + //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) { - trimmedTables[0].Rows[i][8] = adSpecialId; + //If the row is dirty then its going to be found in either... + if (!(bool)inventoryDataGridView.Rows[repeatedItemIndex].Cells[(int)InventoryTableColumns.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)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; + } + 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; + } } - else + //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) { - trimmedTables[1].Rows[i][9] = adSpecialId; + //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)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; } + //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 + "."); } - //Once ad special ID has been updated jump to the next row. - continue; } //Determine the row's attribute. var rowAttribute = 0; //Zero (0) means no grouping, its not a header nor a member. @@ -2066,20 +2147,15 @@ namespace AdvertsingProfitControl { rowAttribute = 2; } - //Since we've made it this far, add the ad item into the dictionary if we're not in the ad special group. - if (_adSpecialIndex != -1 && row.Index < _adSpecialIndex) - { - usedAdItems.Add(row.Index, adItemId); - } //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)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Sold, is string + var newRow = new object[9]; + newRow[0] = row.Cells[(int)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Beginning inventory, is string newRow[1] = row.Cells[(int)InventoryTableColumns.Recieved].EditedFormattedValue.ToString();//Received, is string - newRow[2] = row.Cells[(int)InventoryTableColumns.Total].EditedFormattedValue.ToString();//Total, is string - newRow[3] = row.Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString();//EndingInventory, is string + newRow[2] = row.Cells[(int)InventoryTableColumns.Total].EditedFormattedValue.ToString();//Total inventory. is string + newRow[3] = row.Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString(); //Ending inventory, is string newRow[4] = adItemId; newRow[5] = rowAttribute; if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex) @@ -2093,18 +2169,17 @@ namespace AdvertsingProfitControl newRow[7] = row.Index + 1; } newRow[8] = dateId; - trimmedTables[0].Rows.Add(newRow); - usedAdItems.Add(adItemId, 0); + insertNewTable.Rows.Add(newRow); } else { //This table is full of data that is already in the database. - var newRow = new object[12]; + var newRow = new object[10]; newRow[0] = rowIdNumber; - newRow[1] = row.Cells[(int)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Sold, is string + newRow[1] = row.Cells[(int)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Beginning inventory, is string newRow[2] = row.Cells[(int)InventoryTableColumns.Recieved].EditedFormattedValue.ToString();//Received, is string - newRow[3] = row.Cells[(int)InventoryTableColumns.Total].EditedFormattedValue.ToString();//Total, is string - newRow[4] = row.Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString();//EndingInventory, is string + newRow[3] = row.Cells[(int)InventoryTableColumns.Total].EditedFormattedValue.ToString();//Total inventory. is string + newRow[4] = row.Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString(); //Ending inventory, is string newRow[5] = adItemId; newRow[6] = rowAttribute; if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex) @@ -2118,15 +2193,14 @@ namespace AdvertsingProfitControl newRow[8] = row.Index + 1; } newRow[9] = dateId; - trimmedTables[1].Rows.Add(newRow); - usedAdItems.Add(adItemId, 1); + updateExistingTable.Rows.Add(newRow); } } - return trimmedTables; + return TrimmingOperationResult.CreatedNewInsertionAndUpdateTables; } - private IEnumerable ConstructCleanedActualSalesTable(int dateId) + 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. @@ -2134,13 +2208,12 @@ 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 - var trimmedNewProjectionsTable = new DataTable("ACtualSales"); + 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 - var trimmedUpdateProjectionsTable = new DataTable("ActualSales"); - DataTable[] trimmedTables = { trimmedNewProjectionsTable, trimmedUpdateProjectionsTable }; + updateExistingTable = new DataTable("Projections"); //Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView. string[] saleColumnNames = { @@ -2151,26 +2224,24 @@ namespace AdvertsingProfitControl { if (columnName == "ID") continue; var column = new DataColumn(columnName); - trimmedTables[0].Columns.Add(column); + insertNewTable.Columns.Add(column); } foreach (var columnName in saleColumnNames) { var column = new DataColumn(columnName); - trimmedTables[1].Columns.Add(column); + 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). - //Dictionary The table ID is zero (0) for the new table and one (1) for the update table. - var usedAdItems = new Dictionary(); //Contains ad items that are used in section one, used for checking for repeats. //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( - projectionsDataGridView.Rows[_adSpecialIndex].Cells[(int)SalesTableColumns.AdItem] + actualSalesDataGridView.Rows[_adSpecialIndex].Cells[(int)SalesTableColumns.AdItem] .EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString), out adSpecialId); } //Begin spinning through all the rows in the projections table. @@ -2184,10 +2255,10 @@ namespace AdvertsingProfitControl //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Ad Special Row found at index " + row.Index + "."); continue; } - //Check to see if the row is not dirty. + //Check to see if the row is dirty. if (!(bool)row.Cells[(int)SalesTableColumns.IsDirty].Value) { - //If so simply continue. + //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). @@ -2201,51 +2272,128 @@ namespace AdvertsingProfitControl adItemId = databaseWriter.InsertNewAdItem(row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString()); if (adItemId == 0) { - errorLabel.Text = @"Failed to insert new ad item."; //TODO: Throw an exception, this can not be allowed. //AdItemInsertionFailedException - trimmedTables[0].Rows.Clear(); //Work around for now - trimmedTables[1].Rows.Clear(); - break; + insertNewTable.Rows.Clear(); //Work around for now + updateExistingTable.Rows.Clear(); + return TrimmingOperationResult.FailedToTrim; } } - int tableIndex; //Check for repeated ad items in the ad special section - if (usedAdItems.TryGetValue(adItemId, out tableIndex)) + if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex) { - //If the ad item is being used in section one then locate it and update it in the trimmed table. - var foundRow = trimmedTables[tableIndex].Select("AdItemID = '" + adItemId + "'"); - if (foundRow.Length == 1) + //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) { - LogConsole.WriteToLog(FrmLogConsole.Level.Info, - "Ad item '" + row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue + - "' found in the trimmed table."); - } - else - { - //TODO: Throw an exception, this can not be allowed. - //InvalidTrimmedRowCountException - trimmedTables[0].Rows.Clear(); //Work around for now - trimmedTables[1].Rows.Clear(); - break; - } - //Begin spinning through all the rows to find the one selected above. - for (var i = 0; i < trimmedTables[tableIndex].Rows.Count; i++) - { - //Check to see if the selected row is equal. - if (foundRow[0] != trimmedTables[tableIndex].Rows[i]) continue; - //If so then update that row index with the group ID number. - if (tableIndex == 0) + //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) { - trimmedTables[0].Rows[i][8] = adSpecialId; + //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; + } } - else + //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) { - trimmedTables[1].Rows[i][9] = adSpecialId; + //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 + "."); } - //Once ad special ID has been updated jump to the next row. - continue; } //Determine the row's attribute. var rowAttribute = 0; //Zero (0) means no grouping, its not a header nor a member. @@ -2257,11 +2405,6 @@ namespace AdvertsingProfitControl { rowAttribute = 2; } - //Since we've made it this far, add the ad item into the dictionary if we're not in the ad special group. - if (_adSpecialIndex != -1 && row.Index < _adSpecialIndex) - { - usedAdItems.Add(row.Index, adItemId); - } //Add the values to their respective data table. if (rowIdNumber == 0) { @@ -2286,8 +2429,7 @@ namespace AdvertsingProfitControl newRow[9] = row.Index + 1; } newRow[10] = dateId; - trimmedTables[0].Rows.Add(newRow); - usedAdItems.Add(adItemId, 0); + insertNewTable.Rows.Add(newRow); } else { @@ -2313,19 +2455,10 @@ namespace AdvertsingProfitControl newRow[10] = row.Index + 1; } newRow[11] = dateId; - trimmedTables[1].Rows.Add(newRow); - usedAdItems.Add(adItemId, 1); + updateExistingTable.Rows.Add(newRow); } - //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Row Dump for row number " + (row.Index + 1) + "."); - //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "ID : " + rowIdNumber + " Sold: " + row.Cells[(int)SalesTableColumns.Sold].EditedFormattedValue); - //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Sale Price : " + row.Cells[(int)SalesTableColumns.SalePrice].EditedFormattedValue + " Total Sales: " + row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue); - //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Cost: " + row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue + " Profit Return: " + row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue); - //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Total Profit Return: " + row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue + " Ad Item ID: " + adItemId); - //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Row Attribute: " + rowAttribute + " Ad Special ID: " + adSpecialId); - //LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Row Position: " + row.Index + (_adSpecialIndex != -1 && row.Index > _adSpecialIndex ? 1 : 0) + " Date ID: " + dateId); } - - return trimmedTables; + return TrimmingOperationResult.CreatedNewInsertionAndUpdateTables; } #endregion