Files
advertising-profit-control/AdvertsingProfitControl/FrmMain.cs
T

799 lines
38 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AdvertsingProfitControl
{
public partial class FrmMain : Form
{
private decimal _salesProducedByAdItems;
private decimal _totalProfitReturnFromAdItems;
private decimal _totalInvoicePurchases; //Total Purchases
private decimal _departmentSales; //weekly sales total
private DateTime _currentActiveDate;
private readonly FrmLogConsole _console = FrmLogConsole.GetStaticInstance;
private int _LazyPageCounter;
private bool isDebug;
public FrmMain(bool isDebug)
{
InitializeComponent();
this.isDebug = isDebug;
isDebug = true;
debugToolStripMenuItem.Visible = isDebug;
var db = new AdvertisingProfitControlModel();
db.Versions.Count();
}
private void frmMain_Load(object sender, EventArgs e)
{
var db = new AdvertisingProfitControlModel();
monthCalendar.BoldedDates = db.WeekEndingDates.Select(zdate => zdate.EndingDate).ToArray();
ConstructApcDataGridViews();
ConstructInvoicesDataGridView();
RowParsing.AdSpecialGroups.AddRange(db.AdSpecials.Select(x => x.Name));
//Select the most recent date from the database.
var recentDate = db.WeekEndingDates.OrderByDescending(x => x.EndingDate).FirstOrDefault();
if (recentDate != null)
{
_currentActiveDate = recentDate.EndingDate;
LoadDate(recentDate.EndingDate);
}
monthCalendar.DateChanged += ValidateDateChanged;
//projectionsDataGridView.KeyDown += FrmMain_KeyDown;
//var margin = new Margins(50, 50, 0, 0);
//_printDoc.DefaultPageSettings.Margins = margin;
}
private void CalculateProfitAnalysis(decimal shrink = 0.30m)
{
if (actualSalesDataGridView.Rows.Count == 0)
{
salesProducedLabel.Text = @"Sales Produced By Ad Items (A): No Data";
totalProfitFromAdItemsLabel.Text = @"Total Profit Return " + Environment.NewLine + @" From Ad Items (B): No Data";
if (Math.Abs(_departmentSales) < 1)
{
departmentSalesLabel.Text = @"Department Sales: No Data";
}
return;
}
var incompleteData = false;
//First grab the total sales and the total profit return generated by the actual sales of product.
if (
decimal.TryParse(
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[3].EditedFormattedValue
.ToString(), out _salesProducedByAdItems))
{
//The sales parsed properly so there is data in the cell. Next try to grab the total profit return.
if (decimal.TryParse(
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[6].EditedFormattedValue
.ToString(), out _totalProfitReturnFromAdItems))
{
}
}
if (Math.Abs(_salesProducedByAdItems) < 1)
{
salesProducedLabel.Text = @"Sales Produced By Ad Items (A): No Data";
incompleteData = true;
}
if (Math.Abs(_totalProfitReturnFromAdItems) < 1)
{
totalProfitFromAdItemsLabel.Text = @"Total Profit Return " + Environment.NewLine + @"From Ad Items (B): No Data";
incompleteData = true;
}
if (Math.Abs(_departmentSales) < 1)
{
departmentSalesLabel.Text = @"Department Sales: No Data";
incompleteData = true;
}
if (incompleteData)
{
return;
}
departmentSalesLabel.Text = @"Department Sales: " + _departmentSales.ToString("C");
salesProducedLabel.Text = @"Sales Produced By Ad Items (A): " + _salesProducedByAdItems.ToString("C");
//Calculate the remaining sales.
var remainingSales = _departmentSales - _salesProducedByAdItems;
remainingSalesLabel.Text = @"Remaining Sales: " + remainingSales.ToString("C");
//Again assume the total profit return is larger then zero (0).
totalProfitFromAdItemsLabel.Text = @"Total Profit Return" + Environment.NewLine + @" From Ad Items (B): " + _totalProfitReturnFromAdItems.ToString("C");
//Shrink is being used as a place holder for Cross Profit % which is obtained by dividing the total profit return from the department weekly retail sales.
var totalProfitReturnFromRemainingSales = shrink * remainingSales;
//
totalProfitReturnFromRemaingLabel.Text = @"Total Profit Return" + Environment.NewLine + @" From Remaining Sales: " + totalProfitReturnFromRemainingSales.ToString("C");
var totalProfitReturn = _totalProfitReturnFromAdItems + totalProfitReturnFromRemainingSales;
totalProfitReturnLabel.Text = @"Total Profit Return: " + totalProfitReturn.ToString("C");
}
private void CalculateGrossProfit()
{
if (Math.Abs(_totalInvoicePurchases) < 1)
{
//IF Cost of Sales hasn't been calculated, then clear labels and return.
if (Math.Abs(_departmentSales) < 1)
{
grossProfitTotalSales.Text = @"Total Sales: No Data";
}
grossProfitLessCostOfSales.Text = @"Less Cost of Sales: No Data";
return;
}
grossProfitTotalSales.Text = @"Total Sales: " + _departmentSales.ToString("C");
grossProfitLessCostOfSales.Text = @"Less Cost of Sales: " + _totalInvoicePurchases.ToString("C");
var dollarGrossProfit = _departmentSales - _totalInvoicePurchases;
grossProfitDollarGrossProfitLabel.Text = @"Dollar Gross Profit: " + dollarGrossProfit.ToString("C");
var grossProfitPercent = dollarGrossProfit/_departmentSales;
perfectGrossProfitLabel.Text = @"Percent Gross Profit: " + grossProfitPercent.ToString("P");
}
private void addRecordToolStripMenuItem_Click(object sender, EventArgs e)
{
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var form = new NewModifyRecord();
form.ShowDialog();
//One the user has closed the add record form, check to see if there is a newer date
//available. If so, reload the form.
var date = databaseReader.RetrieveMostRecentDate(databaseTracker.DatabaseConnectionString);
if (date == _currentActiveDate) return;
LoadDate(date);
}
private void showHideConsoleHelpMainMenu_Click(object sender, EventArgs e)
{
if (_console.IsVisable)
{
_console.Hide();
}
else
{
_console.Show();
}
}
private void modifyRecordMainMenu_Click(object sender, EventArgs e)
{
var form = new NewModifyRecord(_currentActiveDate);
form.ShowDialog();
//On return reload the date that was just modified by the modify record form.
LoadDate(monthCalendar.SelectionStart);
}
private void manageItemsToolsMainMenu_Click(object sender, EventArgs e)
{
var adItemManager = new FrmManageAdItems();
adItemManager.ShowDialog();
}
private void dbVersionHelpMainMenu_Click(object sender, EventArgs e)
{
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var version = databaseReader.GetDatabaseVersion(databaseTracker.DatabaseConnectionString);
if (version != "0.0.0.0")
{
MessageBox.Show(@"The current database's version is " + version + @".", @"Database Version Number", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bitMap;
if (_LazyPageCounter == 0)
{
bitMap = new Bitmap(Application.StartupPath + "\\FrontPage.png");
}
else
{
bitMap = new Bitmap(Application.StartupPath + "\\BackPage.png");
}
var rect = e.MarginBounds;
if ((double) bitMap.Width/(double) bitMap.Height > (double) rect.Width/(double) rect.Height)
// image is wider
{
rect.Height = (int) ((double) bitMap.Height/(double) bitMap.Width*(double) rect.Width);
}
else
{
rect.Width = (int) ((double) bitMap.Width/(double) bitMap.Height*(double) rect.Height);
}
if (_LazyPageCounter == 0)
{
e.Graphics.DrawImage(bitMap, new Rectangle(0, 25, 850, 1050));
_LazyPageCounter++;
e.HasMorePages = true;
}
else
{
e.Graphics.DrawImage(bitMap, new Rectangle(0, 0, 850, 1100));
_LazyPageCounter = 0;
e.HasMorePages = false;
}
}
private void DisplayPrintPreview(object sender, EventArgs e)
{
//Build and render the front and back forms of the document.
var test = new FrontPageGenerator();
var backPageTest = new BackPageGenerator();
var printTestPage = new PrintDocument();
var printDialog = new PrintPreviewDialog();
if (!usePreRenderedFilesCheckbox.Checked)
{
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var dateId =
databaseReader.RetrieveDateIdByDateString(monthCalendar.SelectionStart.ToShortDateString(),
databaseTracker.DatabaseConnectionString);
var remaingingSales = _departmentSales - _salesProducedByAdItems;
var totalProfitReturnFromReminaingSales = remaingingSales * .3m;
var totalProfitReturn = _totalProfitReturnFromAdItems + totalProfitReturnFromReminaingSales;
//test.BuildFormFrontCompressedLayout(dateId, _departmentSales, _salesProducedByAdItems, remaingingSales,
//_totalProfitReturnFromAdItems, totalProfitReturnFromReminaingSales, totalProfitReturn,
//commentsTextBox.Text);
test.RenderHtmlToImage();
backPageTest.GenerateWeeklyInventoryControlPage(dateId);
backPageTest.RenderHtmlToImage();
}
else
{
if (File.Exists(Application.StartupPath + "\\" + "FrontPage.html") &&
File.Exists(Application.StartupPath + "\\" + "Backpage.html"))
{
test.RenderHtmlToImage();
backPageTest.RenderHtmlToImage();
}
}
//var margin = new Margins(50, 50, 0, 0);
//printDoc.DefaultPageSettings.Margins = margin;
printTestPage.PrintPage += PrintPage;
printDialog.Document = printTestPage;
printDialog.ShowDialog();
printDialog.Document = new PrintDocument();
}
/// <summary>
/// Fills the APC DataGridViews with the appropriate columns and starting row for the user to start
/// entering data.
/// </summary>
private void ConstructApcDataGridViews()
{
//Construct a list of column names for the inventory/actual sales DataGridViews and the inventory DataGirdView.
string[] saleColumnNames =
{
"AdItem", "Sold", "SalePrice", "TotalSales", "Cost", "ProfitReturn",
"TotalProfitReturn"
};
string[] inventoryColumnNames =
{
"AdItem", "BeginningInventory", "Received", "Total", "EndingInventory"
};
foreach (var name in saleColumnNames)
{
var column = new DataGridViewTextBoxColumn
{
Name = name,
HeaderText = TextFormat.AddSpacesToSentence(name, false),
ValueType = typeof(string),
SortMode = DataGridViewColumnSortMode.NotSortable,
MaxInputLength = 20
};
projectionsDataGridView.Columns.Add(column);
}
//Add the columns into the inventory DataGridView after setting their types.
foreach (var name in inventoryColumnNames)
{
var column = new DataGridViewTextBoxColumn
{
Name = name,
HeaderText = TextFormat.AddSpacesToSentence(name, false),
ValueType = typeof(string),
SortMode = DataGridViewColumnSortMode.NotSortable,
MaxInputLength = 20
};
inventoryDataGridView.Columns.Add(column);
}
//
foreach (var name in saleColumnNames)
{
var column = new DataGridViewTextBoxColumn
{
Name = name,
HeaderText = TextFormat.AddSpacesToSentence(name, false),
ValueType = typeof(string),
SortMode = DataGridViewColumnSortMode.NotSortable,
MaxInputLength = 20
};
actualSalesDataGridView.Columns.Add(column);
}
}
/// <summary>
/// Constructs the invoices DataGridView.
/// </summary>
private void ConstructInvoicesDataGridView()
{
string[] invoicesColumnNames =
{
"InvoiceDate", "Supplier", "InvoiceNumber", "InvoiceNetAmountAtCost",
"InvoiceNetAmountExtendedRetail", "InvoiceNote"
};
foreach (var name in invoicesColumnNames)
{
var column = new DataGridViewTextBoxColumn
{
Name = name,
HeaderText = TextFormat.AddSpacesToSentence(name, false),
ValueType = typeof(string),
SortMode = DataGridViewColumnSortMode.NotSortable,
MaxInputLength = 20
};
invoicesDataGridView.Columns.Add(column);
}
}
private void LoadDate(DateTime date)
{
var db = new AdvertisingProfitControlModel();
if (monthCalendar.BoldedDates.Length == 0)
{
//The database is most likely empty so halt everything.
dateTimeGroupBox.Text = @"The database appears to be empty.";
modifyRecordMainMenu.Enabled = false;
modifyRecordMainMenu.ToolTipText = @"There are no records to modify.";
return;
}
modifyRecordMainMenu.Enabled = true;
modifyRecordMainMenu.ToolTipText = @"";
var dateRecord = db.WeekEndingDates.Single(x => x.EndingDate == date);
ClearForm();
LoadProjectionsTable(dateRecord);
LoadInventoryTable(dateRecord);
LoadActualSalesTable(dateRecord);
LoadInvoices(dateRecord);
if (db.Notes.Any(x => x.FkDateId == dateRecord.Id))
{
var note = db.Notes.Single(x => x.FkDateId == dateRecord.Id);
commentsTextBox.Text = note.Remark;
}
LoadWeeklySales(dateRecord);
LoadTaxable(dateRecord);
LoadCostAnalysis(dateRecord);
CalculateProfitAnalysis();
CalculateGrossProfit();
monthCalendar.BoldedDates = db.WeekEndingDates.Select(zdate => zdate.EndingDate).ToArray();
monthCalendar.SelectionStart = date;
dateTimeGroupBox.Text = @"Loaded " + date.ToString("d");
}
private void LoadProjectionsTable(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
var adSpecialIndex = -1;
decimal totalSales = 0;
decimal totalProfitReturn = 0;
var projections = db.Projections.Where(x => x.WeekEndingDate.Id == dateRecord.Id).Select(x => x);
foreach (var p in projections)
{
projectionsDataGridView.Rows.Add();
var index = projectionsDataGridView.RowCount - 1;
projectionsDataGridView.Rows[index].Cells[0].Value = p.AdItem.Name;
projectionsDataGridView.Rows[index].Cells[1].Value = p.Sold;
projectionsDataGridView.Rows[index].Cells[2].Value = p.SalePrice;
projectionsDataGridView.Rows[index].Cells[3].Value = $@"{p.TotalSales:N2}";
if (p.TotalSales != null) totalSales += (decimal)p.TotalSales;
projectionsDataGridView.Rows[index].Cells[4].Value = $@"{p.Cost:N2}";
projectionsDataGridView.Rows[index].Cells[5].Value = $@"{p.ProfitReturn:N2}";
projectionsDataGridView.Rows[index].Cells[6].Value = $@"{p.TotalProfitReturn:N2}";
if (p.TotalProfitReturn != null) totalProfitReturn += (decimal)p.TotalProfitReturn;
if (p.RowAttribute == 1)
{
projectionsDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.HeaderRow;
}
else if (p.RowAttribute == 2)
{
projectionsDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.MemberRow;
}
if (p.FkAdSpecialId != 0 && adSpecialIndex == -1)
{
//TODO: Fix null reference when no object is found.
adSpecialIndex = index;
projectionsDataGridView.Rows.Insert(index, 1);
projectionsDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.AdSpecial;
projectionsDataGridView.Rows[index].Cells[3].Value = db.AdSpecials.Single(x => x.Id == p.FkAdSpecialId).Name;
}
}
//Now add the totals row.
if (projectionsDataGridView.RowCount == 0) return;
var totalsRow = new DataGridViewRow();
projectionsDataGridView.Rows.Add(totalsRow);
projectionsDataGridView.Rows[projectionsDataGridView.Rows.Count - 1].Cells[0].Value = "Total";
projectionsDataGridView.Rows[projectionsDataGridView.Rows.Count - 1].Cells[2].Style.Alignment =
DataGridViewContentAlignment.MiddleRight;
projectionsDataGridView.Rows[projectionsDataGridView.Rows.Count - 1].Cells[2].Value = @"(A)";
if (Math.Abs(totalSales) > 0)
{
projectionsDataGridView.Rows[projectionsDataGridView.Rows.Count - 1].Cells[3].Value =
totalSales.ToString("N2");
}
projectionsDataGridView.Rows[projectionsDataGridView.Rows.Count - 1].Cells[5].Style.Alignment =
DataGridViewContentAlignment.MiddleRight;
projectionsDataGridView.Rows[projectionsDataGridView.Rows.Count - 1].Cells[5].Value = @"(B)";
if (Math.Abs(totalProfitReturn) > 0)
{
projectionsDataGridView.Rows[projectionsDataGridView.Rows.Count - 1].Cells[6].Value =
totalProfitReturn.ToString("N2");
}
}
private void LoadInventoryTable(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
var adSpecialIndex = -1;
var inventories = db.Inventories.Where(x => x.WeekEndingDate.Id == dateRecord.Id).Select(x => x);
foreach (var i in inventories)
{
inventoryDataGridView.Rows.Add();
var index = inventoryDataGridView.RowCount - 1;
inventoryDataGridView.Rows[index].Cells[0].Value = i.AdItem.Name;
inventoryDataGridView.Rows[index].Cells[1].Value = i.BeginningInventory;
inventoryDataGridView.Rows[index].Cells[2].Value = i.Recieved;
inventoryDataGridView.Rows[index].Cells[3].Value = i.TotalInventory;
inventoryDataGridView.Rows[index].Cells[4].Value = i.EndingInventory;
if (i.RowAttribute == 1)
{
inventoryDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.HeaderRow;
}
else if (i.RowAttribute == 2)
{
inventoryDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.MemberRow;
}
if (i.FkAdSpecialId != 0 && adSpecialIndex == -1)
{
adSpecialIndex = index;
inventoryDataGridView.Rows.Insert(index, 1);
inventoryDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.AdSpecial;
inventoryDataGridView.Rows[index].Cells[2].Value = db.AdSpecials.Single(x => x.Id == i.FkAdSpecialId).Name;
}
}
}
private void LoadActualSalesTable(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
var adSpecialIndex = -1;
decimal totalSales = 0;
decimal totalProfitReturn = 0;
var actualSales = db.ActualSales.Where(x => x.WeekEndingDate.Id == dateRecord.Id).Select(x => x);
foreach (var a in actualSales)
{
actualSalesDataGridView.Rows.Add();
var index = actualSalesDataGridView.RowCount - 1;
actualSalesDataGridView.Rows[index].Cells[0].Value = a.AdItem.Name;
actualSalesDataGridView.Rows[index].Cells[1].Value = a.Sold;
actualSalesDataGridView.Rows[index].Cells[2].Value = a.SalePrice;
actualSalesDataGridView.Rows[index].Cells[3].Value = $@"{a.TotalSales:N2}";
if (a.TotalSales != null) totalSales += (decimal)a.TotalSales;
actualSalesDataGridView.Rows[index].Cells[4].Value = $@"{a.Cost:N2}";
actualSalesDataGridView.Rows[index].Cells[5].Value = $@"{a.ProfitReturn:N2}";
actualSalesDataGridView.Rows[index].Cells[6].Value = $@"{a.TotalProfitReturn:N2}";
if (a.TotalProfitReturn != null) totalProfitReturn += (decimal)a.TotalProfitReturn;
if (a.RowAttribute == 1)
{
actualSalesDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.HeaderRow;
}
else if (a.RowAttribute == 2)
{
actualSalesDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.MemberRow;
}
if (a.FkAdSpecialId != 0 && adSpecialIndex == -1)
{
adSpecialIndex = index;
actualSalesDataGridView.Rows.Insert(index, 1);
actualSalesDataGridView.Rows[index].DefaultCellStyle.BackColor = ApplicationColors.AdSpecial;
actualSalesDataGridView.Rows[index].Cells[3].Value = db.AdSpecials.Single(x => x.Id == a.FkAdSpecialId).Name;
}
}
//Now add the totals row.
if (actualSalesDataGridView.RowCount == 0) return;
var totalsRow = new DataGridViewRow();
actualSalesDataGridView.Rows.Add(totalsRow);
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[0].Value = "Total";
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[2].Style.Alignment =
DataGridViewContentAlignment.MiddleRight;
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[2].Value = @"(A)";
if (Math.Abs(totalSales) > 0)
{
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[3].Value =
totalSales.ToString("N2");
}
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[5].Style.Alignment =
DataGridViewContentAlignment.MiddleRight;
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[5].Value = @"(B)";
if (Math.Abs(totalProfitReturn) > 0)
{
actualSalesDataGridView.Rows[actualSalesDataGridView.Rows.Count - 1].Cells[6].Value =
totalProfitReturn.ToString("N2");
}
}
private void LoadInvoices(WeekEndingDate dateRecord)
{
_totalInvoicePurchases = 0;
var db = new AdvertisingProfitControlModel();
var invoices = db.Invoices.Where(x => x.WeekEndingDate.Id == dateRecord.Id).Select(x => x);
foreach (var invoice in invoices)
{
invoicesDataGridView.Rows.Add();
var index = invoicesDataGridView.RowCount - 1;
invoicesDataGridView.Rows[index].Cells[0].Value = invoice.InvoiceDate.ToString("d");
invoicesDataGridView.Rows[index].Cells[1].Value = invoice.Supplier.Name;
invoicesDataGridView.Rows[index].Cells[2].Value = invoice.InvoiceNumber;
invoicesDataGridView.Rows[index].Cells[3].Value = $@"{invoice.InvoiceNetAmountAtCost:N2}";
invoicesDataGridView.Rows[index].Cells[4].Value = $@"{invoice.InvoiceNetAmount:N2}";
if (invoice.InvoiceNetAmount != null) _totalInvoicePurchases += (decimal) invoice.InvoiceNetAmount;
invoicesDataGridView.Rows[index].Cells[5].Value = invoice.InvoiceNote;
}
//Add the total purchases row to the invoice table.
var totalPurchaesRow = new DataGridViewRow();
invoicesDataGridView.Rows.Add(totalPurchaesRow);
invoicesDataGridView.Rows[invoicesDataGridView.Rows.Count - 1].Cells[0].Value = @"Total Purchases";
if (Math.Abs(_totalInvoicePurchases) > 0)
{
invoicesDataGridView.Rows[invoicesDataGridView.Rows.Count - 1].Cells[4].Value = _totalInvoicePurchases.ToString("N2");
}
}
private void LoadWeeklySales(WeekEndingDate dateRecord)
{
//Clear the department sales and prepare to add up a total.
_departmentSales = 0;
var db = new AdvertisingProfitControlModel();
var weeklySales = db.WeeklySales.Where(x => x.WeekEndingDate.Id == dateRecord.Id).Select(x => x);
foreach (var sale in weeklySales)
{
if (sale.Sunday != null) _departmentSales += (decimal) sale.Sunday;
sundayWeeklySalesLabel.Text = @"Sunday: " + $@"{sale.Sunday:c}";
if (sale.Monday != null) _departmentSales += (decimal) sale.Monday;
mondayWeeklySalesLabel.Text = @"Monday: " + $@"{sale.Monday:c}";
if (sale.Tuesday != null) _departmentSales += (decimal) sale.Tuesday;
tuesadayWeeklySalesLabel.Text = @"Tuesday: " + $@"{sale.Tuesday:c}";
if (sale.Wednesday != null) _departmentSales += (decimal) sale.Wednesday;
wednesdayWeeklySalesLabel.Text = @"Wednesday: " + $@"{sale.Wednesday:c}";
if (sale.Thursday != null) _departmentSales += (decimal)sale.Thursday;
thursdayWeeklySalesLabel.Text = @"Thursday: " + $@"{sale.Thursday:c}";
if (sale.Friday != null) _departmentSales += (decimal) sale.Friday;
fridayWeeklySalesLabel.Text = @"Friday: " + $@"{sale.Friday:c}";
if (sale.Saturday != null) _departmentSales += (decimal) sale.Saturday;
saturdayWeeklySalesLabel.Text = @"Saturday: " + $@"{sale.Saturday:c}";
totalWeeklySalesLabel.Text = @"Total Sales: " + $@"{sale.TotalSales:c}";
}
}
private void LoadTaxable(WeekEndingDate dateRecord)
{
//TODO: Check for holidays
var db = new AdvertisingProfitControlModel();
var taxables = db.Taxables.Where(x => x.WeekEndingDate.Id == dateRecord.Id).Select(x => x);
foreach (var taxable in taxables)
{
sundayTaxableLabel.Text = @"Sunday: " + $@"{taxable.Sunday:c}";
mondayTaxableLabel.Text = @"Monday: " + $@"{taxable.Monday:c}";
tuesdayTaxableLabel.Text = @"Tuesday: " + $@"{taxable.Tuesday:c}";
wednesdayTaxableLabel.Text = @"Wednesday: " + $@"{taxable.Wednesday:c}";
thursdayTaxableLabel.Text = @"Thursday: " + $@"{taxable.Thursday:c}";
fridayTaxableLabel.Text = @"Friday: " + $@"{taxable.Friday:c}";
saturdayTaxableLabel.Text = @"Saturday: " + $@"{taxable.Saturday:c}";
totalTaxableLabel.Text = @"Total: " + $@"{taxable.Total:c}";
}
////See if there are any items and see if any of them are holidays (have a value of zero).
//if (taxables.ToList().Count == 1)
//{
// var Spam = taxables.Select(x => x.)
//}
}
private void LoadCostAnalysis(WeekEndingDate dateRecord)
{
var db = new AdvertisingProfitControlModel();
var analysis = db.CostAnalysis.Where(x => x.WeekEndingDate.Id == dateRecord.Id).Select(x => x);
foreach (var a in analysis)
{
salesPerManHourLabel.Text = @"Sales Per Man Hour: " + $@"{a.SalesPerManHour:c}";
salaryPercentageLabel.Text = @"Salary Percentage: " + $@"{a.SalaryPercentage:p}";
salaryDollarsLabel.Text = @"Salary Dollars: " + $@"{a.SalaryDollar:c}";
suppliesLabel.Text = @"Supplies: " + $@"{a.Supplies}";
}
}
private string CheckForHoliday(DayOfWeek day)
{
var holidayText = string.Empty;
//Get the currently selected date.
var date = monthCalendar.SelectionStart;
//Now make sure the selected day on the calendar (the date that came from the database) is not the same
//as the day provided, unless the day is Saturday. If the end of week date is not on a Saturday and the
//supplied day is Monday, than subtracting 5 would yield an incorrect date.
if (date.DayOfWeek == DayOfWeek.Saturday)
{
switch (day)
{
case DayOfWeek.Sunday:
date = date.AddDays(-6);
break;
case DayOfWeek.Monday:
date = date.AddDays(-5);
break;
case DayOfWeek.Tuesday:
date = date.AddDays(-4);
break;
case DayOfWeek.Wednesday:
date = date.AddDays(-3);
break;
case DayOfWeek.Thursday:
date = date.AddDays(-2);
break;
case DayOfWeek.Friday:
date = date.AddDays(-1);
break;
}
}
var holiday = Holiday.IsHoliday(date);
switch (holiday)
{
case Holidays.Thanksgiving:
case Holidays.Christmas:
case Holidays.NewYearsDay:
holidayText = @"Closed for " + TextFormat.AddSpacesToSentence(holiday.ToString(), false);
break;
}
return holidayText;
}
private void ValidateDateChanged(object sender, DateRangeEventArgs e)
{
if (e.Start == _currentActiveDate || !monthCalendar.BoldedDates.Contains(e.Start)) return;
_currentActiveDate = e.Start;
//Load the specified date from the database.
LoadDate(e.Start);
}
private void ClearForm()
{
projectionsDataGridView.Rows.Clear();
inventoryDataGridView.Rows.Clear();
actualSalesDataGridView.Rows.Clear();
invoicesDataGridView.Rows.Clear();
commentsTextBox.Text = string.Empty;
commentMainGroupBox.Text = @"Comments";
dateTimeGroupBox.Text = string.Empty;
salesPerManHourLabel.Text = @"Sales Per Man Hour:";
salaryPercentageLabel.Text = @"Salary Percentage:";
salaryDollarsLabel.Text = @"Salary Dollars:";
suppliesLabel.Text = @"Supplies:";
costAnalysisGroupBox.Text = @"Cost Analysis";
sundayWeeklySalesLabel.Text = @"Sunday: ";
mondayWeeklySalesLabel.Text = @"Monday: ";
tuesadayWeeklySalesLabel.Text = @"Tuesday: ";
wednesdayWeeklySalesLabel.Text = @"Wednesday: ";
thursdayWeeklySalesLabel.Text = @"Wednesday: ";
fridayWeeklySalesLabel.Text = @"Friday: ";
saturdayWeeklySalesLabel.Text = @"Saturday: ";
totalWeeklySalesLabel.Text = @"Total Sales: ";
weeklySalesGroupBox.Text = @"Weekly Sales";
sundayTaxableLabel.Text = @"Sunday: ";
mondayTaxableLabel.Text = @"Monday: ";
tuesdayTaxableLabel.Text = @"Tuesday: ";
wednesdayTaxableLabel.Text = @"Wednesday: ";
thursdayTaxableLabel.Text = @"Wednesday: ";
fridayTaxableLabel.Text = @"Friday: ";
saturdayTaxableLabel.Text = @"Saturday: ";
totalTaxableLabel.Text = @"Total: ";
taxableGroupBox.Text = @"Taxable";
departmentSalesLabel.Text = @"Department Sales: ";
salesProducedLabel.Text = @"Sales Produced By Ad Items (A):";
remainingSalesLabel.Text = @"Remaining Sales:";
totalProfitFromAdItemsLabel.Text = @"Total Profit Return" + Environment.NewLine + @"From Ad Items (B): ";
totalProfitReturnFromRemaingLabel.Text = @"Total Profit Return" + Environment.NewLine + @"From Remaining Sales: ";
totalProfitReturnLabel.Text = @"Total Profit Return: ";
grossProfitTotalSales.Text = @"Total Sales:";
grossProfitLessCostOfSales.Text = @"Less Cost of Sales: ";
grossProfitDollarGrossProfitLabel.Text = @"Dollar Gross Profit: ";
perfectGrossProfitLabel.Text = @"Percent Gross Profit: ";
grossProfitEstimatedWeeklyDeptmartmentExpenseLabel.Text = @"Estimated Weekly" + Environment.NewLine + @"Department Expense: ";
}
private void debugToolStripMenuItem1_Click(object sender, EventArgs e)
{
var form = new DebugDatabaseConverter();
form.ShowDialog();
RefreshDateListing();
}
private void RefreshDateListing()
{
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
monthCalendar.BoldedDates = databaseReader.RetrieveDates(databaseTracker.DatabaseConnectionString).ToArray();
}
public enum FormDefaultRoll
{
AddNewRecord = 0,
ModifyExistingRecord = 1
}
private void clearSelectedDateToolsMainMenu_Click(object sender, EventArgs e)
{
var result = MessageBox.Show(@"Are you sure you want to delete all records for the date " + _currentActiveDate.ToShortDateString() + @"?", @"Clear Date", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
{
return;
}
var dbT = new DatabaseTracker();
var dbW = new DatabaseWriter(dbT.DatabaseConnectionString);
var dbR = new DatabaseReader();
var dateId = dbR.RetrieveDateIdByDateString(_currentActiveDate.ToShortDateString(), dbT.DatabaseConnectionString);
if (dateId != 0)
{
if (dbW.ClearDateById(dateId))
{
RefreshDateListing();
var date = dbR.RetrieveMostRecentDate(dbT.DatabaseConnectionString);
LoadDate(date);
}
else
{
errorLabel.Text = @"Failed to clear the selected date.";
}
}
else
{
errorLabel.Text = @"Failed to get the date ID number.";
}
}
}
}
//http://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children
internal class DrawingControl
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
public static void SuspendDrawing(Control parent)
{
SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
}
public static void ResumeDrawing(Control parent)
{
SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
parent.Refresh();
}
}