Feature: Added Machine Locations so that setting the location in the config file is not needed.

Feature: Settings database table added to store string based settings.
         It is right now used to store bill header and footer.
         Hard Coded header/footer removed from file.
Feature: Tax Analysis form created to easily show the tax calculation.
Feature: Management form uses background workers.  Dont' know if it is functional though.
Chore: Reorder Table form moved to masters from sales folder.
Refactor: ManagementBI and SalesAnalysisBI
This commit is contained in:
tanshu
2016-01-04 10:52:01 +05:30
parent 0456135497
commit caf9b3106c
37 changed files with 1893 additions and 507 deletions

View File

@ -15,9 +15,23 @@ namespace Tanshu.Accounts.Management
{
Stopwatch _stopwatch;
Stopwatch _totalStopwatch;
private BackgroundWorker bwGo = new BackgroundWorker();
private BackgroundWorker bwExcel = new BackgroundWorker();
public ManagementForm()
{
InitializeComponent();
bwGo.WorkerReportsProgress = true;
bwGo.WorkerSupportsCancellation = true;
bwGo.DoWork += new DoWorkEventHandler(DoGo);
bwGo.ProgressChanged += new ProgressChangedEventHandler(bwGo_ProgressChanged);
bwGo.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwGo_RunWorkerCompleted);
bwExcel.WorkerReportsProgress = true;
bwExcel.WorkerSupportsCancellation = true;
bwExcel.DoWork += new DoWorkEventHandler(DoExcel);
bwExcel.ProgressChanged += new ProgressChangedEventHandler(bwGo_ProgressChanged);
bwExcel.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwExcel_RunWorkerCompleted);
}
private void Sale_Analysis_Form_Load(object sender, EventArgs e)
@ -29,91 +43,207 @@ namespace Tanshu.Accounts.Management
#region Go
private void btnGo_Click(object sender, EventArgs e)
{
txtStatus.Text = "";
btnGo.Enabled = false;
_stopwatch = Stopwatch.StartNew();
_totalStopwatch = Stopwatch.StartNew();
bwGo.RunWorkerAsync();
}
private void DoGo(BackgroundWorker worker)
{
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var beer = Path.Combine(currentDirectory, "beer.json");
var sale = Path.Combine(currentDirectory, "sale.json");
var credit = Path.Combine(currentDirectory, "credit.json");
var error = string.Empty;
if (!File.Exists(beer))
error += "Beer not found! ";
if (!File.Exists(sale))
error += "Sale not found! ";
if (!File.Exists(credit))
error += "Credit not found";
if (!string.IsNullOrEmpty(error))
if (btnGo.Text == "Go")
{
MessageBox.Show(error);
txtStatus.Text = "";
btnGo.Text = "Cancel";
_stopwatch = Stopwatch.StartNew();
_totalStopwatch = Stopwatch.StartNew();
bwGo.RunWorkerAsync();
}
else
bwGo.CancelAsync();
}
private void btnExcel_Click(object sender, EventArgs e)
{
if (btnExcel.Text == "Excel")
{
btnGo.Enabled = false;
var beerDates = GetBeer(beer);
var saleDates = GetSale(sale);
var creditDates = GetCredit(credit);
txtStatus.Text = "";
btnExcel.Text = "Cancel";
_stopwatch = Stopwatch.StartNew();
_totalStopwatch = Stopwatch.StartNew();
bwExcel.RunWorkerAsync();
}
else
bwExcel.CancelAsync();
}
var info = string.Empty;
foreach (var item in saleDates)
private void DoGo(object sender, DoWorkEventArgs e)
{
var beer = GetBeer();
var sale = GetSale();
var credit = GetCredit();
var info = string.Empty;
foreach (var item in sale)
{
var startDate = item.StartDate;
var finishDate = item.FinishDate;
var sDate = startDate.AddHours(7);
var fDate = finishDate.AddDays(1).AddHours(7);
int count = 0;
bwGo.ReportProgress(++count, "Starting on " + startDate.ToShortDateString() + " to " + finishDate.ToShortDateString());
using (var bi = new ManagementBI())
{
var startDate = item.SDate.Value;
var finishDate = item.FDate.Value;
var sDate = startDate.AddHours(7);
var fDate = finishDate.AddDays(1).AddHours(7);
Console.WriteLine("Starting on " + startDate.ToShortDateString() + " to " + finishDate.ToShortDateString());
int count = 0;
using (var bi = new ManagementBI())
bi.DeleteVoid(sDate, fDate);
bwGo.ReportProgress(++count, "Voids Deleted");
if (bwGo.CancellationPending == true)
{
worker.ReportProgress(++count, "Deleting Voids");
bi.DeleteVoid(sDate, fDate);
worker.ReportProgress(++count, "Deleting Staff");
bi.MoveStaffToNc(sDate, fDate);
worker.ReportProgress(++count, "Clearing Modifiers");
bi.ClearModifiers(sDate, fDate);
worker.ReportProgress(++count, "Combining Kots");
bi.CombineKots(sDate, fDate);
worker.ReportProgress(++count, "Removing Blank Kots");
bi.RemoveBlankKots(sDate, fDate);
worker.ReportProgress(++count, "Starting beer");
foreach (var beerDate in beerDates)
{
if (beerDate.bDate < startDate || beerDate.bDate > finishDate)
continue;
var stDt = beerDate.bDate.Value.AddHours(7);
var fiDt = stDt.AddDays(1);
worker.ReportProgress(++count, "Setting beer for " + stDt.ToShortDateString());
bi.SetBeer(stDt, fiDt, beerDate.Quantity);
}
bi.MoveToNc(sDate, fDate, item.Sale.Where(x => x.IsLiq).Sum(x => x.Amount) / .75M); // Do not put all in NC this will allow for about 25% discount on the rest of non nc liqour
worker.ReportProgress(++count, "Starting sale");
foreach (var saleItem in item.Sale)
{
worker.ReportProgress(++count, "Setting sale for " + saleItem.Rate.ToString());
if (saleItem.IsLiq)
bi.SetSaleDiscount(saleItem.Rate, saleItem.Amount, sDate, fDate);
else
bi.SetSaleQuantity(saleItem.Rate, saleItem.Amount, sDate, fDate);
}
worker.ReportProgress(++count, "Removing Blank Kots");
bi.RemoveBlankKots(sDate, fDate);
worker.ReportProgress(++count, "Starting cleanup");
bi.SetPayments(sDate, fDate);
bi.SaveChanges();
e.Cancel = true;
return;
}
worker.ReportProgress(++count, "Cleanup done");
bi.MoveStaffToNc(sDate, fDate);
bwGo.ReportProgress(++count, "Staff Moved");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
bi.ClearModifiers(sDate, fDate);
bwGo.ReportProgress(++count, "Modifiers Cleared");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
bi.CombineKots(sDate, fDate);
bwGo.ReportProgress(++count, "Kots Combined");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
bi.RemoveBlankKots(sDate, fDate);
bwGo.ReportProgress(++count, "Blank Kots Removed");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
bwGo.ReportProgress(++count, "Starting beer");
foreach (var beerDate in beer)
{
if (beerDate.Date < startDate || beerDate.Date > finishDate)
continue;
bi.SetBeer(beerDate.Date.AddHours(7), beerDate.Date.AddDays(1).AddHours(7), beerDate.Quantity);
bwGo.ReportProgress(++count, "Beer set for " + beerDate.Date.ToShortDateString());
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
}
bi.RemoveBlankKots(sDate, fDate);
bwGo.ReportProgress(++count, "Blank Kots Removed");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
bi.MoveToNc(sDate, fDate, item.Sale.Where(x => x.IsLiq).Sum(x => x.Amount) / .75M); // Do not put all in NC this will allow for about 25% discount on the rest of non nc liqour
bwGo.ReportProgress(++count, "Moved to Nc");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
IList<CreditJson> creditJ = credit.Where(x => x.Date >= startDate && x.Date <= finishDate).ToList();
bi.SetQuantityAndDiscount(item.Sale, creditJ, startDate, finishDate);
bwGo.ReportProgress(++count, "Sale Done");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
bi.RemoveBlankKots(sDate, fDate);
bwGo.ReportProgress(++count, "Blank Kots Removed");
if (bwGo.CancellationPending == true)
{
e.Cancel = true;
return;
}
bi.SaveChanges();
}
bwGo.ReportProgress(++count, "Cleanup done");
}
}
private void bwGo_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
private void DoExcel(object sender, DoWorkEventArgs e)
{
DoGo(sender as BackgroundWorker);
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
string sheet;
int count = 0;
var info = new List<ExcelInfo>();
var rates = new List<decimal>();
using (var bi = new ManagementBI())
{
for (var date = startDate; date <= finishDate; date = date.AddDays(1))
{
if (bwExcel.CancellationPending == true)
{
e.Cancel = true;
return;
}
bwExcel.ReportProgress(++count, "Getting data for " + date.ToShortDateString());
var currentStart = date.AddHours(7);
var currentFinish = date.AddDays(1).AddHours(7);
var bills = bi.GetMinMaxBills(currentStart, currentFinish);
if (bills == null)
continue;
var saleList = bi.GetSaleAndVat(currentStart, currentFinish);
var serviceTax = bi.GetServiceTax(currentStart, currentFinish);
var ei = new ExcelInfo()
{
Date = date,
StartBill = bi.FullBillID(bills.StartBill, Tanshu.Accounts.Entities.VoucherType.Regular),
FinishBill = bi.FullBillID(bills.FinishBill, Tanshu.Accounts.Entities.VoucherType.Regular),
SaleAndVat = new Dictionary<decimal, SaleInfo>(),
ServiceTax = serviceTax
};
foreach (var item in saleList)
{
if (!rates.Contains(item.Rate))
rates.Add(item.Rate);
ei.SaleAndVat.Add(item.Rate, item);
}
info.Add(ei);
}
rates.Sort();
sheet = "Date\tBill Start\tBill Final\t";
foreach (var item in rates)
{
sheet += string.Format("Sale {0:#0.00}%\tVat {0:#0.00}%\t", item * 100);
}
sheet += "Service Tax\n";
foreach (var item in info)
{
if (bwExcel.CancellationPending == true)
{
e.Cancel = true;
return;
}
bwExcel.ReportProgress(++count, "Compiling data for " + item.Date.ToShortDateString());
sheet += string.Format("{0:dd-MMM-yyyy}\t'{1}\t'{2}\t", item.Date, item.StartBill, item.FinishBill);
foreach (var rate in rates)
{
if (item.SaleAndVat.ContainsKey(rate))
sheet += string.Format("{0:#0}\t{1:#0}\t", Math.Round(item.SaleAndVat[rate].Net), Math.Round(item.SaleAndVat[rate].Vat));
else
sheet += "0\t0\t";
}
sheet += string.Format("{0:#0}\n", Math.Round(item.ServiceTax));
}
}
e.Result = sheet;
}
private void bwGo_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
var time = (_stopwatch.ElapsedMilliseconds / 1000).ToString() + "s / " + (_totalStopwatch.ElapsedMilliseconds / 1000).ToString() + "s";
@ -123,16 +253,41 @@ namespace Tanshu.Accounts.Management
}
private void bwGo_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
txtStatus.Text += "Done !!!";
_stopwatch.Stop();
_totalStopwatch.Stop();
btnGo.Enabled = true;
}
#endregion
#region Get Data
private IList<SaleJson> GetSale(string sale)
btnGo.Text = "Go";
if (!e.Cancelled)
txtStatus.Text = "Done !!!\r\n" + txtStatus.Text;
else
txtStatus.Text = "Cancelled :(\r\n" + txtStatus.Text;
}
private void bwExcel_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
_stopwatch.Stop();
_totalStopwatch.Stop();
btnExcel.Text = "Excel";
if (!e.Cancelled)
{
txtStatus.Text = "Done !!!\r\n" + txtStatus.Text;
var sheet = (string)e.Result;
Clipboard.SetText(sheet, TextDataFormat.Text);
}
else
{
txtStatus.Text = "Cancelled :(\r\n" + txtStatus.Text;
}
}
#endregion
#region Get Data
private IList<SaleJson> GetSale()
{
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var sale = Path.Combine(currentDirectory, "sale.json");
if (!File.Exists(sale))
throw new ArgumentException("Sale not found!");
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
var fileContents = new StreamReader(File.OpenRead(sale)).ReadToEnd();
@ -141,16 +296,18 @@ namespace Tanshu.Accounts.Management
IList<SaleJson> list = new List<SaleJson>();
foreach (var item in data)
{
if (!item.SDate.HasValue || !item.FDate.HasValue)
continue;
if (item.FDate.Value.Date < startDate.Date || item.SDate.Value.Date > finishDate.Date)
if (item.FinishDate.Date < startDate.Date || item.StartDate.Date > finishDate.Date)
continue;
list.Add(item);
}
return list;
}
private IList<CreditJson> GetCredit(string credit)
private IList<CreditJson> GetCredit()
{
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var credit = Path.Combine(currentDirectory, "credit.json");
if (!File.Exists(credit))
throw new ArgumentException("Credit not found!");
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
var fileContents = new StreamReader(File.OpenRead(credit)).ReadToEnd();
@ -159,16 +316,21 @@ namespace Tanshu.Accounts.Management
IList<CreditJson> list = new List<CreditJson>();
foreach (var item in data)
{
if (!item.CDate.HasValue)
continue;
if (item.CDate.Value.Date < startDate.Date || item.CDate.Value.Date > finishDate.Date)
if (item.Date.Date < startDate.Date || item.Date.Date > finishDate.Date)
continue;
list.Add(item);
}
var am = list.GroupBy(x => x.Date).Where(x => x.Count() > 1).Select(x => x.Key);
if (am.Count() > 0)
throw new ArgumentException("Duplicate dates in credit: " + am.Select(x => x.ToString("dd-MMM-yyyy")).Aggregate((c, n) => c + ", " + n));
return list;
}
private IList<BeerJson> GetBeer(string beer)
private IList<BeerJson> GetBeer()
{
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var beer = Path.Combine(currentDirectory, "beer.json");
if (!File.Exists(beer))
throw new ArgumentException("Beer not found!");
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
var fileContents = new StreamReader(File.OpenRead(beer)).ReadToEnd();
@ -177,9 +339,7 @@ namespace Tanshu.Accounts.Management
IList<BeerJson> list = new List<BeerJson>();
foreach (var item in data)
{
if (!item.bDate.HasValue)
continue;
if (item.bDate.Value.Date < startDate.Date || item.bDate.Value.Date > finishDate.Date)
if (item.Date.Date < startDate.Date || item.Date.Date > finishDate.Date)
continue;
list.Add(item);
}
@ -375,83 +535,14 @@ namespace Tanshu.Accounts.Management
private void btnFinalSanction_Click(object sender, EventArgs e)
{
var startDate = dtpStart.Value.Date.AddHours(7);
var finishDate = dtpFinish.Value.Date.AddDays(1).AddHours(7);
using (var bi = new ManagementBI())
{
bi.UpdateBillID(dtpStart.Value.Date.AddHours(7), dtpFinish.Value.Date.AddDays(1).AddHours(7));
bi.SetPayments(startDate, finishDate);
bi.UpdateBillID(startDate, finishDate);
bi.SaveChanges();
}
}
#region Excel
private void btnExcel_Click(object sender, EventArgs e)
{
btnExcel.Enabled = false;
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
var sheet = GetExcel(startDate, finishDate);
Clipboard.SetText(sheet, TextDataFormat.Text);
btnExcel.Enabled = true;
}
private static string GetExcel(DateTime startDate, DateTime finishDate)
{
var info = new List<ExcelInfo>();
var rates = new List<decimal>();
using (var bi = new ManagementBI())
{
for (var date = startDate; date <= finishDate; date = date.AddDays(1))
{
var currentStart = date.AddHours(7);
var currentFinish = date.AddDays(1).AddHours(7);
var bills = bi.GetMinMaxBills(currentStart, currentFinish);
if (bills == null)
continue;
var saleList = bi.GetSaleAndVat(currentStart, currentFinish);
var serviceTax = bi.GetServiceTax(currentStart, currentFinish);
var ei = new ExcelInfo()
{
Date = date,
StartBill = bi.FullBillID(bills.StartBill, Tanshu.Accounts.Entities.VoucherType.Regular),
FinishBill = bi.FullBillID(bills.FinishBill, Tanshu.Accounts.Entities.VoucherType.Regular),
SaleAndVat = new Dictionary<decimal, SaleInfo>(),
ServiceTax = serviceTax
};
foreach (var item in saleList)
{
if (!rates.Contains(item.Rate))
rates.Add(item.Rate);
ei.SaleAndVat.Add(item.Rate, item);
}
info.Add(ei);
}
rates.Sort();
var sheet = "Date\tBill Start\tBill Final\t";
foreach (var item in rates)
{
sheet += string.Format("Sale {0:#0.00}%\tVat {0:#0.00}%\t", item * 100);
}
sheet += "Service Tax\n";
foreach (var item in info)
{
sheet += string.Format("{0:dd-MMM-yyyy}\t'{1}\t'{2}\t", item.Date, item.StartBill, item.FinishBill);
foreach (var rate in rates)
{
if (item.SaleAndVat.ContainsKey(rate))
sheet += string.Format("{0:#0}\t{1:#0}\t", Math.Round(item.SaleAndVat[rate].Net), Math.Round(item.SaleAndVat[rate].Vat));
else
sheet += "0\t0\t";
}
sheet += string.Format("{0:#0}\n", Math.Round(item.ServiceTax));
}
return sheet;
}
}
#endregion
}
}

View File

@ -35,7 +35,6 @@
this.btnTally = new System.Windows.Forms.Button();
this.btnFinalSanction = new System.Windows.Forms.Button();
this.btnExcel = new System.Windows.Forms.Button();
this.bwGo = new System.ComponentModel.BackgroundWorker();
this.txtStatus = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
@ -106,13 +105,6 @@
this.btnExcel.UseVisualStyleBackColor = true;
this.btnExcel.Click += new System.EventHandler(this.btnExcel_Click);
//
// bwGo
//
this.bwGo.WorkerReportsProgress = true;
this.bwGo.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bwGo_DoWork);
this.bwGo.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bwGo_RunWorkerCompleted);
this.bwGo.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bwGo_ProgressChanged);
//
// txtStatus
//
this.txtStatus.AcceptsReturn = true;
@ -156,7 +148,6 @@
private System.Windows.Forms.Button btnTally;
private System.Windows.Forms.Button btnFinalSanction;
private System.Windows.Forms.Button btnExcel;
private System.ComponentModel.BackgroundWorker bwGo;
private System.Windows.Forms.TextBox txtStatus;
}
}

View File

@ -117,7 +117,4 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="bwGo.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>