Added a tool to remove an entire date and all records associated with it. Updated row deletion code to handle unbalanced tables or missing row IDs.

This commit is contained in:
2017-01-24 13:45:25 -06:00
parent d7229c9499
commit 2657523888
12 changed files with 277 additions and 242 deletions
+115 -39
View File
@@ -490,22 +490,6 @@ namespace AdvertsingProfitControl
table.Rows[rowIndex].Cells[(int)InvoiceTableColumns.IsDirty].Value = false;
table.Rows[rowIndex].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
//foreach (DataGridViewRow row in table.Rows)
//{
// if(row.IsNewRow) continue;
// if (!rowsAdded.Contains(row.Index)) continue;
// var supplierId = RetrieveSupplierId(row.Cells[(int)InvoiceTableColumns.Supplier].EditedFormattedValue.ToString());
// var invoiceIdNumber =
// RetrieveInvoiceId(
// row.Cells[(int)InvoiceTableColumns.InvoiceDate].EditedFormattedValue
// .ToString(),
// long.Parse(
// row.Cells[(int)InvoiceTableColumns.InvoiceNumber].EditedFormattedValue
// .ToString()), supplierId, dateId);
// row.Cells[(int)InvoiceTableColumns.Id].Value = invoiceIdNumber;
// row.Cells[(int)InvoiceTableColumns.IsDirty].Value = false;
// row.HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
//}
table.RefreshEdit();
//Set the status.
status.SetStatus(WritingOperationStatus.InsertionSuccessful);
@@ -820,6 +804,36 @@ namespace AdvertsingProfitControl
}
}
public bool DeleteTableEntry(int rowId, string tableName, string connectionString)
{
var oleDbConnection = new OleDbConnection(connectionString);
var status = false;
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection,
CommandText = "DELETE * FROM " + tableName + " WHERE ID = ?"
};
oleDbCommand.Parameters.AddWithValue("RowID", rowId);
try
{
oleDbConnection.Open();
oleDbCommand.ExecuteNonQuery();
status = true;
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to delete the entries in " + tableName + " with the row ID of " + rowId + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
}
finally
{
oleDbConnection.Close();
}
return status;
}
/// <summary>
/// Inserts a new ad item into the database and returns the new item's ID number.
/// Supports rolling back as to not corrupt the database.
@@ -1084,11 +1098,11 @@ namespace AdvertsingProfitControl
/// <returns>True if the operation was successful; false if not.</returns>
public bool DeleteApcRow(int projectionsRowId, int inventoryRowId, int actualSalesRowId)
{
var result = false;
var result = true;
var tableName = "Projections"; //Starting point.
var oleDbCommand = new OleDbCommand
{
CommandText = "DELETE FROM Projections WHERE ID = ?",
CommandText = "DELETE * FROM Projections WHERE ID = ?",
Connection = _oleDbConnection
};
oleDbCommand.Parameters.AddWithValue("ID", projectionsRowId);
@@ -1098,41 +1112,54 @@ namespace AdvertsingProfitControl
_oleDbConnection.Open();
oleDbTransaction = _oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
var rowsEffected = oleDbCommand.ExecuteNonQuery();
if (rowsEffected == 1)
var rowsEffected = 0;
if (projectionsRowId != 0)
{
tableName = "Inventory";
oleDbCommand.Parameters.Clear();
oleDbCommand.CommandText = "DELETE FROM Inventory WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("ID", inventoryRowId);
rowsEffected = oleDbCommand.ExecuteNonQuery();
if (rowsEffected == 1)
if (rowsEffected == 0)
{
tableName = "ActualSales";
oleDbCommand.Parameters.Clear();
oleDbCommand.CommandText = "DELETE FROM ActualSales WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("ID", inventoryRowId);
rowsEffected = oleDbCommand.ExecuteNonQuery();
if (rowsEffected == 1)
{
result = true;
}
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to delete row with the ID of " + projectionsRowId + " from projections. (No Exception Thrown)");
return false;
}
}
if (!result)
tableName = "Inventory";
oleDbCommand.Parameters.Clear();
if (inventoryRowId != 0)
{
oleDbTransaction?.Rollback();
oleDbCommand.CommandText = "DELETE * FROM Inventory WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("ID", inventoryRowId);
rowsEffected = oleDbCommand.ExecuteNonQuery();
if (rowsEffected == 0)
{
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to delete row with the ID of " + inventoryRowId + " from inventory. (No Exception Thrown)");
return false;
}
}
else
tableName = "ActualSales";
oleDbCommand.Parameters.Clear();
if (actualSalesRowId != 0)
{
oleDbTransaction.Commit();
oleDbCommand.CommandText = "DELETE * FROM ActualSales WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("ID", actualSalesRowId);
rowsEffected = oleDbCommand.ExecuteNonQuery();
if (rowsEffected == 0)
{
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to delete row with the ID of " + actualSalesRowId + " from actual sales. (No Exception Thrown)");
return false;
}
}
oleDbTransaction?.Commit();
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to delete row with the ID of " + inventoryRowId + " from " + tableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
oleDbTransaction?.Rollback();
result = false;
}
finally
{
@@ -1142,6 +1169,55 @@ namespace AdvertsingProfitControl
return result;
}
public bool ClearDateById(int dateId)
{
var successful = true;
var oleDbCommand = new OleDbCommand
{
CommandText = "DELETE * FROM ActualSales WHERE FK_DateID = ?",
Connection = _oleDbConnection
};
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
OleDbTransaction oleDbTransaction = null;
try
{
_oleDbConnection.Open();
oleDbTransaction = _oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM Comment WHERE FK_DateID = ?";
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM CostOfSalesAnalysis WHERE FK_DateID = ?";
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM Inventory WHERE FK_DateID = ?";
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM Invoice WHERE FK_DateID = ?";
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM Projections WHERE FK_DateID = ?";
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM Taxable WHERE FK_DateID = ?";
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM WeekEnding WHERE ID = ?";
oleDbCommand.ExecuteNonQuery();
oleDbCommand.CommandText = "DELETE * FROM WeeklySales WHERE FK_DateID = ?";
oleDbCommand.ExecuteNonQuery();
oleDbTransaction.Commit();
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
oleDbTransaction?.Rollback();
successful = false;
}
finally
{
_oleDbConnection.Close();
}
return successful;
}
public bool NormalizeApcTables(int dateId)
{
var successful = false;