Added the methods to insert and update invoice records in the database, as well as fixing a bug where the Invoice Notes would disappear on row validation.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace AdvertsingProfitControl
|
||||
{
|
||||
@@ -64,7 +65,7 @@ namespace AdvertsingProfitControl
|
||||
oleDbTransaction.Commit();
|
||||
foreach (DataRow row in salesTable.Rows)
|
||||
{
|
||||
var rowId = RetrieveRowId(salesTable.TableName, int.Parse(row[6].ToString()), int.Parse(row[10].ToString()));
|
||||
var rowId = RetrieveRowId(salesTable.TableName, int.Parse(row[6].ToString()), int.Parse(row[10].ToString()), int.Parse(salesTable.Rows[8].ToString()));
|
||||
if (int.Parse(row[8].ToString()) != 0)
|
||||
{
|
||||
//Account for the ad special row.
|
||||
@@ -266,7 +267,7 @@ namespace AdvertsingProfitControl
|
||||
oleDbTransaction.Commit();
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
var rowId = RetrieveRowId(table.TableName, int.Parse(row[4].ToString()), int.Parse(row[8].ToString()));
|
||||
var rowId = RetrieveRowId(table.TableName, int.Parse(row[4].ToString()), int.Parse(row[8].ToString()), int.Parse(table.Rows[6].ToString()));
|
||||
if (int.Parse(row[6].ToString()) != 0)
|
||||
{
|
||||
//Account for the ad special row.
|
||||
@@ -358,7 +359,7 @@ namespace AdvertsingProfitControl
|
||||
oleDbTransaction.Commit();
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
var rowId = RetrieveRowId(table.TableName, int.Parse(row[4].ToString()), int.Parse(row[8].ToString()));
|
||||
var rowId = RetrieveRowId(table.TableName, int.Parse(row[4].ToString()), int.Parse(row[8].ToString()), int.Parse(table.Rows[7].ToString()));
|
||||
if (int.Parse(row[7].ToString()) != 0)
|
||||
{
|
||||
//Account for the ad special row.
|
||||
@@ -406,6 +407,123 @@ namespace AdvertsingProfitControl
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
public DbWriterStatus ProccessInvoiceTable(DataGridView table, int dateId, string connectionString)
|
||||
{
|
||||
var status = new DbWriterStatus();
|
||||
var lastRowProcessed = 0;
|
||||
var rowsAdded = new List<int>();
|
||||
var oleDbConnection = new OleDbConnection(connectionString);
|
||||
var oleDbCommand = new OleDbCommand
|
||||
{
|
||||
Connection = oleDbConnection
|
||||
};
|
||||
OleDbTransaction oleDbTransaction = null;
|
||||
try
|
||||
{
|
||||
oleDbConnection.Open();
|
||||
oleDbTransaction = oleDbConnection.BeginTransaction();
|
||||
oleDbCommand.Transaction = oleDbTransaction;
|
||||
for (var i = 0; i < table.Rows.Count - 1; i++)
|
||||
{
|
||||
//Check to see if the row requires processing to start with.
|
||||
if(!(bool) table.Rows[i].Cells[(int)InvoiceTableColumns.IsDirty].Value)
|
||||
{
|
||||
lastRowProcessed = i + 1;
|
||||
continue;
|
||||
}
|
||||
//Grab the supplier ID number before moving forward.
|
||||
var supplierId = RetrieveSupplierId(
|
||||
table.Rows[i].Cells[(int) InvoiceTableColumns.Supplier].EditedFormattedValue.ToString());
|
||||
if (supplierId == 0)
|
||||
{
|
||||
status.SetErrorMessage("Failed to obtain supplier ID number for " + table.Rows[i].Cells[(int)InvoiceTableColumns.Supplier].EditedFormattedValue + ".");
|
||||
status.SetStatus(WritingOperationStatus.Failed);
|
||||
return status;
|
||||
}
|
||||
//Check whether or not the row is already in the database by seeing if anything is in the ID number cell.
|
||||
if (table.Rows[i].Cells[(int) InvoiceTableColumns.Id].EditedFormattedValue.ToString() == "")
|
||||
{
|
||||
//If the invoice doesn't exist in the database add it.
|
||||
oleDbCommand.CommandText =
|
||||
"INSERT INTO Invoice (InvoiceDate, InvoiceNumber, InvoiceNetAmountAtCost, InvoiceNetAmount, InvoiceNote, FK_Supplier, FK_DateID) VALUES(?,?,?,?,?,?,?)";
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceDate", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceDate].EditedFormattedValue.ToString());
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", long.Parse(table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNumber].EditedFormattedValue.ToString()));
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNetAmountAtCost", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmountAtCost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmountAtCost].EditedFormattedValue.ToString()));
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNetAmount", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmount].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmount].EditedFormattedValue.ToString()));
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNote", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNote].EditedFormattedValue.ToString());
|
||||
oleDbCommand.Parameters.AddWithValue("FK_Supplier", supplierId);
|
||||
oleDbCommand.Parameters.AddWithValue("FK_DateID", dateId);
|
||||
oleDbCommand.ExecuteNonQuery();
|
||||
oleDbCommand.Parameters.Clear();
|
||||
rowsAdded.Add(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
//If the invoice already exists in the database then simply update the record.
|
||||
oleDbCommand.CommandText =
|
||||
"UPDATE Invoice SET InvoiceDate = ?, InvoiceNumber = ?, InvoiceNetAmountAtCost = ?, InvoiceNetAmount = ?, InvoiceNote = ?, FK_Supplier = ?, FK_DateID = ? WHERE ID = ?";
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceDate", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceDate].EditedFormattedValue.ToString());
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", long.Parse(table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNumber].EditedFormattedValue.ToString()));
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNetAmountAtCost", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmountAtCost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmountAtCost].EditedFormattedValue.ToString()));
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNetAmount", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmount].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNetAmount].EditedFormattedValue.ToString()));
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNote", table.Rows[i].Cells[(int)InvoiceTableColumns.InvoiceNote].EditedFormattedValue.ToString());
|
||||
oleDbCommand.Parameters.AddWithValue("FK_Supplier", supplierId);
|
||||
oleDbCommand.Parameters.AddWithValue("FK_DateID", dateId);
|
||||
oleDbCommand.Parameters.AddWithValue("ID", int.Parse(table.Rows[i].Cells[(int)InvoiceTableColumns.Id].EditedFormattedValue.ToString()));
|
||||
oleDbCommand.ExecuteNonQuery();
|
||||
oleDbCommand.Parameters.Clear();
|
||||
}
|
||||
//Row index is only used to keep track of what row failed to update.
|
||||
lastRowProcessed = i + 1;
|
||||
}
|
||||
oleDbTransaction.Commit();
|
||||
foreach (DataGridViewRow row in table.Rows)
|
||||
{
|
||||
if (rowsAdded.Contains(row.Index))
|
||||
{
|
||||
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;
|
||||
}
|
||||
table.RefreshEdit();
|
||||
//Set the status.
|
||||
status.SetStatus(WritingOperationStatus.InsertionSuccessful);
|
||||
}
|
||||
catch (OleDbException e)
|
||||
{
|
||||
status.SetStatus(WritingOperationStatus.Failed);
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to process the Invoice table: " + e.Message);
|
||||
if (lastRowProcessed <= 0)
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
|
||||
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
|
||||
status.SetErrorMessage("Failed to begin parsing rows.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed on row " + (lastRowProcessed + 1) + " due to the above error. Dumping contents of row " + (lastRowProcessed + 1) + " from Invoices.");
|
||||
status.SetErrorMessage("Failed to write to the database on row " + (lastRowProcessed + 1) + ".");
|
||||
}
|
||||
oleDbTransaction?.Rollback();
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, Invoice table failed on update.");
|
||||
}
|
||||
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.
|
||||
@@ -498,18 +616,20 @@ namespace AdvertsingProfitControl
|
||||
/// <param name="tableName">The name of the table to check for the row in.</param>
|
||||
/// <param name="adItemId">The ID of the ad item in the row.</param>
|
||||
/// <param name="dateId">The ID of the date in the row.</param>
|
||||
/// <param name="groupId">Ad Special ID of the row.</param>
|
||||
/// <returns>The ID of the row, zero(0) if no rows are found.</returns>
|
||||
private int RetrieveRowId(string tableName, int adItemId, int dateId)
|
||||
private int RetrieveRowId(string tableName, int adItemId, int dateId, int groupId)
|
||||
{
|
||||
var id = 0;
|
||||
var rowsEffected = 0;
|
||||
var oleDbCommand = new OleDbCommand
|
||||
{
|
||||
CommandText = "SELECT ID FROM " + tableName + " WHERE FK_AdItemID = ? AND FK_DateID = ?",
|
||||
CommandText = "SELECT ID FROM " + tableName + " WHERE FK_AdItemID = ? AND FK_DateID = ? AND FK_AdSpecialGroupName = ?",
|
||||
Connection = _oleDbConnection
|
||||
};
|
||||
oleDbCommand.Parameters.AddWithValue("AdItemID", adItemId);
|
||||
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
|
||||
oleDbCommand.Parameters.AddWithValue("GroupID", groupId);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -525,7 +645,7 @@ namespace AdvertsingProfitControl
|
||||
if (rowsEffected > 1)
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
|
||||
"Redundancy found row with the ad item ID " + adItemId + " with the date ID " + dateId + ".");
|
||||
"Redundancy found row with the ad item ID " + adItemId + " with the date ID " + dateId + " and a group ID of " + groupId + ".");
|
||||
}
|
||||
}
|
||||
catch (OleDbException e)
|
||||
@@ -537,6 +657,128 @@ namespace AdvertsingProfitControl
|
||||
return id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to grab the supplier ID from the database.
|
||||
/// If the name is not found it is added to the database
|
||||
/// and the ID of the newly added supplier is returned.
|
||||
/// </summary>
|
||||
/// <param name="supplierName">The name of the supplier from whom the invoice is from.</param>
|
||||
/// <returns></returns>
|
||||
private int RetrieveSupplierId(string supplierName)
|
||||
{
|
||||
var id = 0;
|
||||
var rowsEffected = 0;
|
||||
var oleDbCommand = new OleDbCommand
|
||||
{
|
||||
CommandText = "SELECT ID FROM Supplier WHERE SupplierName = ?",
|
||||
Connection = _oleDbConnection
|
||||
};
|
||||
oleDbCommand.Parameters.AddWithValue("SupplierName", supplierName);
|
||||
try
|
||||
{
|
||||
_oleDbConnection.Open();
|
||||
var reader = oleDbCommand.ExecuteReader();
|
||||
while (reader != null && reader.Read())
|
||||
{
|
||||
rowsEffected++;
|
||||
int.TryParse(reader[0].ToString(), out id);
|
||||
}
|
||||
reader?.Close();
|
||||
|
||||
if (rowsEffected == 0)
|
||||
{
|
||||
//Attempt to add the supplier to the database.
|
||||
oleDbCommand.Parameters.Clear();
|
||||
oleDbCommand.CommandText = "INSERT INTO Supplier (SupplierName) VALUES (?)";
|
||||
oleDbCommand.Parameters.AddWithValue("SupplierName", supplierName);
|
||||
rowsEffected = oleDbCommand.ExecuteNonQuery();
|
||||
if (rowsEffected == 1)
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Info, "Added the supplier '" + supplierName + "' to the database.");
|
||||
}
|
||||
//Now try grabbing the ID of the supplier that was just added.
|
||||
oleDbCommand.Parameters.Clear();
|
||||
oleDbCommand.CommandText = "SELECT ID FROM Supplier WHERE SupplierName = ?";
|
||||
oleDbCommand.Parameters.AddWithValue("SupplierName", supplierName);
|
||||
reader = oleDbCommand.ExecuteReader();
|
||||
while (reader != null && reader.Read())
|
||||
{
|
||||
int.TryParse(reader[0].ToString(), out id);
|
||||
}
|
||||
}
|
||||
|
||||
if (rowsEffected > 1)
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
|
||||
"The supplier " + supplierName + " has multiple entries.");
|
||||
}
|
||||
}
|
||||
catch (OleDbException e)
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
|
||||
"Failed to look up the existence of the supplier " + supplierName + ".");
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_oleDbConnection.Close();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the ID number of the invoice with the specified Date, Number and date ID numbers.
|
||||
/// Returns zero if now records are found.
|
||||
/// </summary>
|
||||
/// <param name="invoiceDate">The date on the invoice.</param>
|
||||
/// <param name="invoiceNumber">The number for the invoice.</param>
|
||||
/// <param name="supplierId">The supplier's ID number.</param>
|
||||
/// <param name="dateId">The date ID number of the weekending date.</param>
|
||||
/// <returns></returns>
|
||||
private int RetrieveInvoiceId(string invoiceDate, long invoiceNumber, int supplierId, int dateId)
|
||||
{
|
||||
var id = 0;
|
||||
var rowsEffected = 0;
|
||||
var oleDbCommand = new OleDbCommand
|
||||
{
|
||||
CommandText = "SELECT ID FROM Invoice WHERE InvoiceDate = ? AND InvoiceNumber = ? AND FK_Supplier = ? AND FK_DateID = ?",
|
||||
Connection = _oleDbConnection
|
||||
};
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceDate", invoiceDate);
|
||||
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoiceNumber);
|
||||
oleDbCommand.Parameters.AddWithValue("FK_Supplier", supplierId);
|
||||
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
|
||||
try
|
||||
{
|
||||
_oleDbConnection.Open();
|
||||
var reader = oleDbCommand.ExecuteReader();
|
||||
while (reader != null && reader.Read())
|
||||
{
|
||||
rowsEffected++;
|
||||
int.TryParse(reader[0].ToString(), out id);
|
||||
}
|
||||
reader?.Close();
|
||||
|
||||
if (rowsEffected > 1)
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
|
||||
"Redundancy found row with the ad item ID with the date ID " + dateId + ".");
|
||||
}
|
||||
}
|
||||
catch (OleDbException e)
|
||||
{
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
|
||||
"Failed to look up the existence of an invoice with the number of " + invoiceNumber +
|
||||
" and a date of " + invoiceDate + ".");
|
||||
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_oleDbConnection.Close();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
//10 to 1 ratio of tries to code
|
||||
|
||||
Reference in New Issue
Block a user