Updated the main form's tools to include an option to delete the entire active year. Tweaked the ListViews in the three tool forms to disallow multi-select and made a slight change to the behavior of the filter combo box.

This commit is contained in:
2017-04-12 03:36:05 -05:00
parent 932ee01bf3
commit 3567727caa
8 changed files with 189 additions and 20 deletions
+171 -6
View File
@@ -1,4 +1,5 @@
using System;
using System.Data.Entity.Core;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.Drawing.Printing;
@@ -231,11 +232,6 @@ namespace AdvertsingProfitControl
}
}
private void DisplayRecordDeletionRangeForm(object sender, EventArgs e)
{
//TODO: Create a form to allow the user to delete a range of dates.
}
private void DisplayAdItemManager(object sender, EventArgs e)
{
var adItemManager = new FrmManageAdItems();
@@ -884,6 +880,8 @@ namespace AdvertsingProfitControl
modifyRecordMainMenu.ToolTipText = string.Empty;
clearCurrentRecordSelectedDateToolsMainMenu.Enabled = true;
clearCurrentRecordSelectedDateToolsMainMenu.ToolTipText = string.Empty;
clearCurrentActiveYearTools.Enabled = true;
clearCurrentActiveYearTools.ToolTipText = string.Empty;
}
else
{
@@ -892,6 +890,8 @@ namespace AdvertsingProfitControl
modifyRecordMainMenu.ToolTipText = @"There are no records to modify.";
clearCurrentRecordSelectedDateToolsMainMenu.Enabled = false;
clearCurrentRecordSelectedDateToolsMainMenu.ToolTipText = @"There is no date to clear.";
clearCurrentActiveYearTools.Enabled = false;
clearCurrentActiveYearTools.ToolTipText = @"There are no records to clear.";
}
}
@@ -927,6 +927,171 @@ namespace AdvertsingProfitControl
}
#endregion
#endregion
private void ClearActiveDateYearRecords(object sender, EventArgs e)
{
var result = MessageBox.Show(@"Are you sure you want to remove all records for the year of " + _currentActiveDate.Year + @"? This cannot be undone!", @"Clear " + _currentActiveDate.Year + @"'s Records", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result != DialogResult.Yes) return;
var db = new AdvertisingProfitControlModel();
var distinctYears = db.WeekEndingDates.Select(x => x.EndingDate.Year).Distinct().ToArray();
if (distinctYears.Length == 1)
{
result = MessageBox.Show(_currentActiveDate.Year + @" appears to be the only year, clearing it will wipe all data in the database. Are you sure you wish to continue?", @"Clear " + _currentActiveDate.Year + @"'s Records", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result != DialogResult.Yes) return;
}
if (ClearSelectedDate(_currentActiveDate.Year))
{
RefreshDateListing();
}
else
{
MessageBox.Show(@"Failed to clear the records for " + _currentActiveDate.Year + @".", @"Update Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#region Writing Operations
private bool ClearSelectedDate(int year)
{
var db = new AdvertisingProfitControlModel();
var status = true;
using (var scope = new TransactionScope())
{
try
{
var dateRecords = db.WeekEndingDates.Select(x => x).Where(x => x.EndingDate.Year == year).ToArray();
foreach (var dateRecord in dateRecords)
{
ClearProjectionRecords(dateRecord);
ClearInventoyRecords(dateRecord);
ClearActualSalesRecords(dateRecord);
ClearInvoices(dateRecord);
ClearWeeklySales(dateRecord);
ClearTaxableRecords(dateRecord);
ClearCostAnalysisRecords(dateRecord);
ClearCommentsRecords(dateRecord);
db.WeekEndingDates.Remove(dateRecord);
}
db.SaveChanges();
scope.Complete();
}
catch (DbUpdateException e)
{
_console.WriteToLog(FrmLogConsole.Level.Error, e.Message);
status = false;
}
}
return status;
}
private void ClearProjectionRecords(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var projections = db.Projections.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.Projections.RemoveRange(projections);
db.SaveChanges();
scope.Complete();
}
}
private void ClearInventoyRecords(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var inventories = db.Inventories.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.Inventories.RemoveRange(inventories);
db.SaveChanges();
scope.Complete();
}
}
private void ClearActualSalesRecords(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var actualSales = db.ActualSales.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.ActualSales.RemoveRange(actualSales);
db.SaveChanges();
scope.Complete();
}
}
private void ClearInvoices(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var invoices = db.Invoices.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.Invoices.RemoveRange(invoices);
db.SaveChanges();
scope.Complete();
}
}
private void ClearWeeklySales(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var weeklySales = db.WeeklySales.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.WeeklySales.RemoveRange(weeklySales);
db.SaveChanges();
scope.Complete();
}
}
private void ClearTaxableRecords(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var taxables = db.Taxables.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.Taxables.RemoveRange(taxables);
db.SaveChanges();
scope.Complete();
}
}
private void ClearCostAnalysisRecords(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var costAnalysis = db.CostAnalysis.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.CostAnalysis.RemoveRange(costAnalysis);
db.SaveChanges();
scope.Complete();
}
}
private void ClearCommentsRecords(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
using (var scope = new TransactionScope())
{
var comments = db.Notes.Select(x => x).Where(x => x.FkDateId == dateRecord.Id);
db.Notes.RemoveRange(comments);
db.SaveChanges();
scope.Complete();
}
}
#endregion
}
}