narsil/Tanshu.Accounts.PointOfSale/Updates/UpdateForm.cs
2011-12-05 14:53:02 +05:30

114 lines
4.9 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Tanshu.Accounts.Helpers;
using System.Collections.Generic;
using Tanshu.Accounts.BI;
using Tanshu.Accounts.Contracts;
namespace Tanshu.Accounts.PointOfSale
{
public partial class UpdateForm : Form
{
private delegate decimal GetTextboxValueDelegate(TextBox textbox);
private delegate decimal ProcessDelegate(DateTime startDate, DateTime endDate, decimal target, decimal tax);
ProcessDelegate processDelegate;
public UpdateForm()
{
InitializeComponent();
}
public void ShowProgress(decimal minimum, decimal total, decimal current, string message)
{
if (!txtStatus.InvokeRequired)
{
txtStatus.Text = string.Format("{0:#,###} / {1:#,###} :: {2}", current, total, message);
}
else
{
ShowProgessDelegate showProgress = new ShowProgessDelegate(ShowProgress);
BeginInvoke(showProgress, new object[] { minimum, total, current, message });
}
}
private void btnGo_Click(object sender, EventArgs e)
{
dtpFrom.Value = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 00:00:00", dtpFrom.Value));
dtpTo.Value = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 23:59:59", dtpTo.Value));
if (!Thread.CurrentPrincipal.IsInRole("Security/CreateUser"))
return;
RunUpdate(-1);
}
private void RunUpdate(decimal last)
{
processDelegate = new ProcessDelegate(Process);
if (last == -1)
processDelegate.BeginInvoke(dtpFrom.Value, dtpTo.Value, GetTextboxValue(txtThirteenTwoFive), .13125M, new AsyncCallback(CallbackMethod), null);
if (last == .13125M)
processDelegate.BeginInvoke(dtpFrom.Value, dtpTo.Value, GetTextboxValue(txtTwelve), .125M, new AsyncCallback(CallbackMethod), null);
if (last == .125M)
processDelegate.BeginInvoke(dtpFrom.Value, dtpTo.Value, GetTextboxValue(txtFiveTwoFive), .0525M, new AsyncCallback(CallbackMethod), null);
if (last == .0525M)
processDelegate.BeginInvoke(dtpFrom.Value, dtpTo.Value, GetTextboxValue(txtFive), .05M, new AsyncCallback(CallbackMethod), null);
if (last == .05M)
processDelegate.BeginInvoke(dtpFrom.Value, dtpTo.Value, GetTextboxValue(txtFour), .04M, new AsyncCallback(CallbackMethod), null);
if (last == .04M)
processDelegate.BeginInvoke(dtpFrom.Value, dtpTo.Value, GetTextboxValue(txtZero), 0M, new AsyncCallback(CallbackMethod), null);
if (last == 0M)
new ManagementBI().Reorder(dtpFrom.Value, dtpTo.Value, ShowProgress);
}
private void CallbackMethod(IAsyncResult ar)
{
decimal tax = processDelegate.EndInvoke(ar);
RunUpdate(tax);
}
private decimal GetTextboxValue(TextBox textbox)
{
if (!textbox.InvokeRequired)
{
decimal value;
if (!decimal.TryParse(textbox.Text, out value))
value = 0;
return value;
}
else
{
GetTextboxValueDelegate getTextboxValueDelegate = new GetTextboxValueDelegate(GetTextboxValue);
return (decimal)Invoke(getTextboxValueDelegate, new object[] { textbox });
}
}
private decimal Process(DateTime startDate, DateTime endDate, decimal target, decimal tax)
{
ShowProgress(0, 0, 0, string.Format("Starting work on {0:00.00%}. Target : {1:#,###}", tax, target));
if (target == 0)
return tax;
ManagementBI man = new ManagementBI();
List<Guid> list = man.GetUpdateBillList(tax, false, true, false, dtpFrom.Value, dtpTo.Value);
int totalBills = list.Count;
decimal existing = man.GetBalance(tax, dtpFrom.Value, dtpTo.Value);
int i = 0;
decimal work = existing / (existing - target);
while ((existing > target) && (totalBills > 0))
{
int runner = Convert.ToInt32(Math.Floor(work * (i + 1) - i));
if (runner < totalBills - 1)
{
existing -= man.Update(list[runner + 1], tax, dtpFrom.Value, dtpTo.Value);
totalBills--;
i++;
list.Remove(list[runner + 1]);
}
else
{
i = -1;
work = existing / (existing - target);
}
ShowProgress(0, target, existing, string.Format("Working on {0:00.00%}", tax));
}
ShowProgress(0, target, existing, string.Format("Finished {0:00.00%}", tax));
return tax;
}
}
}