narsil/Tanshu.Accounts.PointOfSale/Sales/SettleChoicesForm.cs

129 lines
4.6 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Tanshu.Accounts.Entities;
using Tanshu.Common.Helpers;
using Tanshu.Common.KeyboardControl;
namespace Tanshu.Accounts.PointOfSale
{
public partial class SettleChoicesForm : Form
{
private decimal _totalAmount;
private readonly VoucherType _voucherType;
private IDictionary<SettleOption, decimal> _optionsChosen;
public SettleChoicesForm(decimal totalAmount, VoucherType voucherType)
{
InitializeComponent();
_totalAmount = totalAmount;
_voucherType = voucherType;
_optionsChosen = new Dictionary<SettleOption, decimal>();
}
private void SettleChoicesFormLoad(object sender, EventArgs e)
{
txtAmount.Text = string.Format("Pending Amount: Rs. {0}", PendingAmount);
var count = 0;
foreach (SettleOption item in Enum.GetValues(typeof(SettleOption)))
{
var attribute = item.Attribute();
if (!attribute.ShowInChoices || attribute.Group != _voucherType.Attribute().Group)
continue;
var button = new Button
{
Name = item.ToString(),
Text = attribute.Name,
Size = new Size(75, 75),
TabIndex = count,
UseVisualStyleBackColor = true,
Tag = item,
};
button.Click += new EventHandler(ButtonClick);
flpSettlement.Controls.Add(button);
count++;
}
var controlButton = new Button
{
Name = "btnOK",
Text = "OK",
Size = new Size(75, 75),
TabIndex = count,
UseVisualStyleBackColor = true,
Tag = "OK"
};
controlButton.Click += new EventHandler(OkClick);
flpSettlement.Controls.Add(controlButton);
count++;
controlButton = new Button
{
Name = "btnCancel",
Text = "Cancel",
Size = new Size(75, 75),
TabIndex = count,
UseVisualStyleBackColor = true,
Tag = "Cancel"
};
controlButton.Click += new EventHandler(CancelClick);
flpSettlement.Controls.Add(controlButton);
count++;
txtAmount.TabIndex = count;
this.Size = this.SizeFromClientSize(new Size(count * (75 + 6), 3 + txtAmount.Height + 6 + 75 + 3));
txtAmount.Width = flpSettlement.Width - 6;
}
private void ButtonClick(object sender, EventArgs e)
{
var settleOption = (SettleOption)((sender as Button).Tag);
using (var frm = new SettleAmountsForm(new NumpadControl(), settleOption, PendingAmount))
{
frm.ShowDialog();
UpdateChoice(settleOption, frm.AmountSettled);
}
}
private void UpdateChoice(SettleOption settleOption, decimal amount)
{
if (amount == 0 && _optionsChosen.ContainsKey(settleOption))
_optionsChosen.Remove(settleOption);
else if (amount != 0 && _optionsChosen.ContainsKey(settleOption))
_optionsChosen[settleOption] = amount;
else if (amount != 0 && !_optionsChosen.ContainsKey(settleOption))
_optionsChosen.Add(settleOption, amount);
else if (_totalAmount == 0 && amount == 0 && !_optionsChosen.ContainsKey(settleOption))
_optionsChosen.Add(settleOption, amount);
txtAmount.Text = string.Format("Pending Amount: Rs. {0}", PendingAmount);
}
private decimal PendingAmount
{
get
{
decimal amount = _totalAmount;
foreach (var item in _optionsChosen)
{
amount -= item.Value;
}
return amount;
}
}
public IDictionary<SettleOption, decimal> OptionsChosen
{
get
{
return PendingAmount == 0 ? _optionsChosen : new Dictionary<SettleOption, decimal>();
}
}
private void OkClick(object sender, EventArgs e)
{
this.Close();
}
private void CancelClick(object sender, EventArgs e)
{
_optionsChosen.Clear();
this.Close();
}
}
}