Finished validation code for the cost analysis, weekly sales and taxable. Fixed a bug with the update flag not being set if an ID number was set. Wrote comment and weekly sales database writing functions; untested.

This commit is contained in:
2016-12-02 23:12:05 -06:00
parent 8dcc290aef
commit 6813bbb341
7 changed files with 315 additions and 25 deletions
Binary file not shown.
+130
View File
@@ -524,6 +524,136 @@ namespace AdvertsingProfitControl
return status;
}
public DbWriterStatus ProcessComments(string comments, int dateId, string connectionString, int id = 0)
{
var status = new DbWriterStatus();
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
if (id == 0)
{
oleDbCommand.CommandText = "INSERT INTO Comment (Comment, FK_DateID) VALUES (?, ?)";
oleDbCommand.Parameters.AddWithValue("Comment", comments);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
//Now grab the ID number of the comment that was just added.
oleDbCommand.CommandText = "SELECT ID FROM Comment WHERE FK_DateID = ?";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
//Now read the ID number and add it to the collection with a default key.
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
status.AddRowId(0, int.Parse(reader[0].ToString()));
}
}
else
{
oleDbCommand.CommandText = "UPDATE Comment SET Comment = ? WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("Comment", comments);
oleDbCommand.Parameters.AddWithValue("ID", id);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
}
}
catch (OleDbException e)
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to process the comment(s): " + e.Message);
status.SetErrorMessage("Failed to update or insert comments into the database.");
oleDbTransaction?.Rollback();
}
finally
{
oleDbConnection.Close();
}
return status;
}
public DbWriterStatus ProcessWeeklySales(double[] weeklySales, int dateId, string connectionString, int id = 0)
{
var status = new DbWriterStatus();
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
if (id == 0)
{
oleDbCommand.CommandText = "INSERT INTO WeeklySales (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, TotalSales, FK_DateID) VALUES (?,?,?,?,?,?,?,?,?)";
oleDbCommand.Parameters.AddWithValue("Sunday", weeklySales[0]);
oleDbCommand.Parameters.AddWithValue("Monday", weeklySales[1]);
oleDbCommand.Parameters.AddWithValue("Tuesday", weeklySales[2]);
oleDbCommand.Parameters.AddWithValue("Wednesday", weeklySales[3]);
oleDbCommand.Parameters.AddWithValue("Thursday", weeklySales[4]);
oleDbCommand.Parameters.AddWithValue("Friday", weeklySales[5]);
oleDbCommand.Parameters.AddWithValue("Saturday", weeklySales[6]);
oleDbCommand.Parameters.AddWithValue("TotalSales", weeklySales[7]);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
//Now grab the ID number of the comment that was just added.
oleDbCommand.CommandText = "SELECT ID FROM WeeklySales WHERE FK_DateID = ?";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
//Now read the ID number and add it to the collection with a default key.
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
status.AddRowId(0, int.Parse(reader[0].ToString()));
}
}
else
{
oleDbCommand.CommandText = "UPDATE WeeklySales SET Sunday = ?, Monday = ?, Tuesday = ?, Wednesday = ?, Thursday = ?, Friday = ?, Saturday = ?, TotalSales = ? WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("Sunday", weeklySales[0]);
oleDbCommand.Parameters.AddWithValue("Monday", weeklySales[1]);
oleDbCommand.Parameters.AddWithValue("Tuesday", weeklySales[2]);
oleDbCommand.Parameters.AddWithValue("Wednesday", weeklySales[3]);
oleDbCommand.Parameters.AddWithValue("Thursday", weeklySales[4]);
oleDbCommand.Parameters.AddWithValue("Friday", weeklySales[5]);
oleDbCommand.Parameters.AddWithValue("Saturday", weeklySales[6]);
oleDbCommand.Parameters.AddWithValue("TotalSales", weeklySales[7]);
oleDbCommand.Parameters.AddWithValue("ID", id);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
}
}
catch (OleDbException e)
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to process the weekly sales: " + e.Message);
status.SetErrorMessage("Failed to update or insert weekly sales into the database.");
oleDbTransaction?.Rollback();
}
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.
+50 -11
View File
@@ -105,8 +105,11 @@
this.isTaxableDirtyCheckBox = new System.Windows.Forms.CheckBox();
this.isCostAnalysisDirtyCheckBox = new System.Windows.Forms.CheckBox();
this.clearFormFileMainMenu = new System.Windows.Forms.ToolStripMenuItem();
this.idButton = new System.Windows.Forms.Button();
this.generateCommentIdButton = new System.Windows.Forms.Button();
this.reminderLabel = new System.Windows.Forms.Label();
this.generateWeeklySalesIdButton = new System.Windows.Forms.Button();
this.generateTaxableIdButton = new System.Windows.Forms.Button();
this.generateCostAnalysisIdButton = new System.Windows.Forms.Button();
this.commentsGroupBox.SuspendLayout();
this.mainTabControl.SuspendLayout();
this.projectionTabPage.SuspendLayout();
@@ -857,8 +860,11 @@
//
// debugPanel
//
this.debugPanel.Controls.Add(this.generateCostAnalysisIdButton);
this.debugPanel.Controls.Add(this.generateTaxableIdButton);
this.debugPanel.Controls.Add(this.generateWeeklySalesIdButton);
this.debugPanel.Controls.Add(this.reminderLabel);
this.debugPanel.Controls.Add(this.idButton);
this.debugPanel.Controls.Add(this.generateCommentIdButton);
this.debugPanel.Controls.Add(this.isCostAnalysisDirtyCheckBox);
this.debugPanel.Controls.Add(this.isTaxableDirtyCheckBox);
this.debugPanel.Controls.Add(this.isWeeklySalesDirtyCheckBox);
@@ -915,15 +921,15 @@
this.clearFormFileMainMenu.Size = new System.Drawing.Size(240, 34);
this.clearFormFileMainMenu.Text = "&Clear Form";
//
// idButton
// generateCommentIdButton
//
this.idButton.Location = new System.Drawing.Point(54, 283);
this.idButton.Name = "idButton";
this.idButton.Size = new System.Drawing.Size(286, 62);
this.idButton.TabIndex = 4;
this.idButton.Text = "Create / Clear Comments ID";
this.idButton.UseVisualStyleBackColor = true;
this.idButton.Click += new System.EventHandler(this.idButton_Click);
this.generateCommentIdButton.Location = new System.Drawing.Point(54, 283);
this.generateCommentIdButton.Name = "generateCommentIdButton";
this.generateCommentIdButton.Size = new System.Drawing.Size(286, 62);
this.generateCommentIdButton.TabIndex = 4;
this.generateCommentIdButton.Text = "Create / Clear Comments ID";
this.generateCommentIdButton.UseVisualStyleBackColor = true;
this.generateCommentIdButton.Click += new System.EventHandler(this.idButton_Click);
//
// reminderLabel
//
@@ -934,6 +940,36 @@
this.reminderLabel.TabIndex = 5;
this.reminderLabel.Text = "These checkboxes Tag properties hold the ID for their respective elements.";
//
// generateWeeklySalesIdButton
//
this.generateWeeklySalesIdButton.Location = new System.Drawing.Point(346, 283);
this.generateWeeklySalesIdButton.Name = "generateWeeklySalesIdButton";
this.generateWeeklySalesIdButton.Size = new System.Drawing.Size(286, 62);
this.generateWeeklySalesIdButton.TabIndex = 6;
this.generateWeeklySalesIdButton.Text = "Create / Clear Weekly Sales ID";
this.generateWeeklySalesIdButton.UseVisualStyleBackColor = true;
this.generateWeeklySalesIdButton.Click += new System.EventHandler(this.generateWeeklySalesIdButton_Click);
//
// generateTaxableIdButton
//
this.generateTaxableIdButton.Location = new System.Drawing.Point(638, 283);
this.generateTaxableIdButton.Name = "generateTaxableIdButton";
this.generateTaxableIdButton.Size = new System.Drawing.Size(286, 62);
this.generateTaxableIdButton.TabIndex = 7;
this.generateTaxableIdButton.Text = "Create / Clear Taxable ID";
this.generateTaxableIdButton.UseVisualStyleBackColor = true;
this.generateTaxableIdButton.Click += new System.EventHandler(this.generateTaxableIdButton_Click);
//
// generateCostAnalysisIdButton
//
this.generateCostAnalysisIdButton.Location = new System.Drawing.Point(931, 283);
this.generateCostAnalysisIdButton.Name = "generateCostAnalysisIdButton";
this.generateCostAnalysisIdButton.Size = new System.Drawing.Size(299, 62);
this.generateCostAnalysisIdButton.TabIndex = 8;
this.generateCostAnalysisIdButton.Text = "Create / Clear Cost Analysis ID";
this.generateCostAnalysisIdButton.UseVisualStyleBackColor = true;
this.generateCostAnalysisIdButton.Click += new System.EventHandler(this.generateCostAnalysisIdButton_Click);
//
// NewAddRecord
//
this.AutoScaleDimensions = new System.Drawing.SizeF(168F, 168F);
@@ -1060,7 +1096,10 @@
private System.Windows.Forms.CheckBox isTaxableDirtyCheckBox;
private System.Windows.Forms.CheckBox isWeeklySalesDirtyCheckBox;
private System.Windows.Forms.CheckBox isCommentDirtyCheckBox;
private System.Windows.Forms.Button idButton;
private System.Windows.Forms.Button generateCommentIdButton;
private System.Windows.Forms.Label reminderLabel;
private System.Windows.Forms.Button generateCostAnalysisIdButton;
private System.Windows.Forms.Button generateTaxableIdButton;
private System.Windows.Forms.Button generateWeeklySalesIdButton;
}
}
+128 -10
View File
@@ -135,6 +135,77 @@ namespace AdvertsingProfitControl
saturdayTaxableTextBox.Validating += ValidateTaxableFields;
totalTaxableTextBox.Enter += StoreBeginningTextBoxValue;
totalTaxableTextBox.Validating += ValidateTaxableFields;
//Subscribe the Cost Analysis text boxes to the validation and update events.
salesPerManHourTextBox.Enter += StoreBeginningTextBoxValue;
salesPerManHourTextBox.Validating += ValidateCostAnalysisValues;
salaryPercentageTextBox.Enter += StoreBeginningTextBoxValue;
salaryPercentageTextBox.Validating += ValidateCostAnalysisValues;
salaryDollarsTextBox.Enter += StoreBeginningTextBoxValue;
salaryDollarsTextBox.Validating += ValidateCostAnalysisValues;
suppliesTextBox.Enter += StoreBeginningTextBoxValue;
suppliesTextBox.Validating += ValidateCostAnalysisValues;
}
private void ValidateCostAnalysisValues(object sender, CancelEventArgs e)
{
var textBox = (TextBox) sender;
if (_beginningCellValue == textBox.Text) return;
//There was a change to the starting value of the text box if we've made it this far.
if (textBox.Text != "")
{
double value;
if (double.TryParse(textBox.Text.Trim(), out value))
{
//Input is valid so mark the section as dirty.
isCostAnalysisDirtyCheckBox.Checked = true;
//And apply formatting.
textBox.Text = Math.Round(value, 2).ToString("N", new CultureInfo("en-US"));
//Check to see if the number is equal to zero (0) i.e. "0.00".
if (textBox.Text == @"0.00") textBox.Text = "";
}
else
{
MessageBox.Show(@"The value must be numeric.", @"Input Must be Numeric");
textBox.Text = "";
}
}
else
{
//Since the text box that fired this event is empty check to see if this group has been committed to the database.
if (isCostAnalysisDirtyCheckBox.Tag == null)
{
//Since it hasn't check to see if the other text boxes are empty.
var isDirty = salesPerManHourTextBox.Text.Trim() == "";
if (isDirty)
{
isCostAnalysisDirtyCheckBox.Checked = false;
return;
}
isDirty = salaryPercentageTextBox.Text.Trim() == "";
if (isDirty)
{
isCostAnalysisDirtyCheckBox.Checked = false;
return;
}
isDirty = salaryDollarsTextBox.Text.Trim() == "";
if (isDirty)
{
isCostAnalysisDirtyCheckBox.Checked = false;
return;
}
isDirty = suppliesTextBox.Text.Trim() == "";
if (isDirty)
{
isCostAnalysisDirtyCheckBox.Checked = false;
}
}
else if (isCostAnalysisDirtyCheckBox.Tag != null && textBox.Text == "")
{
//If the user has already added the fields to the database but has removed a value
//update that accordingly.
isCostAnalysisDirtyCheckBox.Checked = true;
}
}
}
private void ValidateTaxableFields(object sender, CancelEventArgs e)
@@ -153,8 +224,6 @@ namespace AdvertsingProfitControl
textBox.Text = Math.Round(dollarValue, 2).ToString("N", new CultureInfo("en-US"));
//Check to see if the number is equal to zero (0) i.e. "0.00".
if (textBox.Text == @"0.00") textBox.Text = "";
//Check to see if this is not the total weekly sales text box.
if (textBox.Name == totalTaxableTextBox.Name) return;
}
else
{
@@ -183,6 +252,12 @@ namespace AdvertsingProfitControl
//If the tag doesn't have an ID in it then we're clear to simply clear the is dirty flag.
isTaxableDirtyCheckBox.Checked = false;
}
else if (isTaxableDirtyCheckBox.Tag != null && textBox.Text == "")
{
//If the user has already added the fields to the database but has removed a value
//update that accordingly.
isTaxableDirtyCheckBox.Checked = true;
}
totalTaxableTextBox.Text = "";
}
}
@@ -209,8 +284,6 @@ namespace AdvertsingProfitControl
textBox.Text = Math.Round(dollarValue, 2).ToString("N", new CultureInfo("en-US"));
//Check to see if the number is equal to zero (0) i.e. "0.00".
if (textBox.Text == @"0.00") textBox.Text = "";
//Check to see if this is not the total weekly sales text box.
if (textBox.Name == totalWeeklySalesTextBox.Name) return;
}
else
{
@@ -239,6 +312,12 @@ namespace AdvertsingProfitControl
//If the tag doesn't have an ID in it then we're clear to simply clear the is dirty flag.
isWeeklySalesDirtyCheckBox.Checked = false;
}
else if(isWeeklySalesDirtyCheckBox.Tag != null && textBox.Text == "")
{
//If the user has already added the fields to the database but has removed a value
//update that accordingly.
isWeeklySalesDirtyCheckBox.Checked = true;
}
totalWeeklySalesTextBox.Text = "";
}
}
@@ -1709,12 +1788,6 @@ namespace AdvertsingProfitControl
}
}
private void StoreBeginningTextValue(object sender, EventArgs e)
{
//Reduce and reuse.
_beginningCellValue = commentsTextBox.Text;
}
#endregion
#region Weekly Sales Events
@@ -2583,5 +2656,50 @@ namespace AdvertsingProfitControl
MessageBox.Show(@"ID cleared from comments.", @"Clear ID Debug");
}
}
private void generateWeeklySalesIdButton_Click(object sender, EventArgs e)
{
if (isWeeklySalesDirtyCheckBox.Tag == null)
{
var ran = new Random();
isWeeklySalesDirtyCheckBox.Tag = ran.Next();
MessageBox.Show(@"Weekly Sales ID is now " + isWeeklySalesDirtyCheckBox.Tag, @"Debug ID Test");
}
else
{
isWeeklySalesDirtyCheckBox.Tag = null;
MessageBox.Show(@"ID cleared from Weekly Sales.", @"Clear ID Debug");
}
}
private void generateTaxableIdButton_Click(object sender, EventArgs e)
{
if (isTaxableDirtyCheckBox.Tag == null)
{
var ran = new Random();
isTaxableDirtyCheckBox.Tag = ran.Next();
MessageBox.Show(@"Taxable ID is now " + isTaxableDirtyCheckBox.Tag, @"Debug ID Test");
}
else
{
isTaxableDirtyCheckBox.Tag = null;
MessageBox.Show(@"ID cleared from Taxable.", @"Clear ID Debug");
}
}
private void generateCostAnalysisIdButton_Click(object sender, EventArgs e)
{
if (isCostAnalysisDirtyCheckBox.Tag == null)
{
var ran = new Random();
isCostAnalysisDirtyCheckBox.Tag = ran.Next();
MessageBox.Show(@"Cost Analysis ID is now " + isCostAnalysisDirtyCheckBox.Tag, @"Debug ID Test");
}
else
{
isCostAnalysisDirtyCheckBox.Tag = null;
MessageBox.Show(@"ID cleared from Cost Analysis.", @"Clear ID Debug");
}
}
}
}
@@ -120,6 +120,9 @@
<metadata name="mainMenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="mainMenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
@@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>sGFYIVV7/W3lYCXVUKinGP8+HnzRtuBko/4mLniApzs=</dsig:DigestValue>
<dsig:DigestValue>sJa3N/qFPRThckha4aReaPFLpwQDio7tNBSSDURtnBk=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
@@ -43,14 +43,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="AdvertsingProfitControl.exe" size="3533824">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="AdvertsingProfitControl.exe" size="3538944">
<assemblyIdentity name="AdvertsingProfitControl" version="0.9.5.2" language="neutral" processorArchitecture="amd64" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>usHPTgWtHUZgtHEvPN4oe9BDB+EUrs2IjXXHTcpDJnI=</dsig:DigestValue>
<dsig:DigestValue>KJrIBRsbaX/71nFWLgdvuorK6BWoYWZ05mGNqVbM0Y0=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
@@ -84,7 +84,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>EXv832xgnrJTLbEvUhk+7ekkWAMvfBMCvVv0Y6BILg0=</dsig:DigestValue>
<dsig:DigestValue>dX2Dundhh4161Q3VUoRkM0RkUhpDPmRvgqyLNfiU3Nk=</dsig:DigestValue>
</hash>
</file>
<file name="Stretched Logo Collection.ico" size="370070">