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

135 lines
4.9 KiB
C#

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 readonly IDictionary<Button, int> _list;
private decimal _amount;
public SettleChoicesForm(decimal amount)
{
InitializeComponent();
_amount = amount;
OptionsChosen = new Dictionary<SettleOption, decimal>();
_list = new Dictionary<Button, int>();
}
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null || button.Tag == null)
return;
if (button.Tag is SettleOption)
{
var settleOption = (SettleOption)button.Tag;
using (var frm = new SettleAmountsForm(new NumpadControl(), settleOption, _amount))
{
frm.ShowDialog();
UpdateChoice(settleOption, frm.AmountSettled, _list[button]);
}
}
else
{
if ((string)button.Tag == "Cancel")
_optionsChosen.Clear();
this.Close();
}
}
private void UpdateChoice(SettleOption settleOption, decimal amount, int group)
{
var oldAmount = _optionsChosen.ContainsKey(settleOption) ? _optionsChosen[settleOption] : 0;
if (amount == 0 && _optionsChosen.ContainsKey(settleOption))
_optionsChosen.Remove(settleOption);
else if (_optionsChosen.ContainsKey(settleOption))
_optionsChosen[settleOption] = amount;
else if (amount != 0)
_optionsChosen.Add(settleOption, amount);
_amount += oldAmount - amount;
txtAmount.Text = string.Format("Pending Amount: Rs. {0}", _amount);
if (_optionsChosen.Count == 0)
foreach (var item in _list.Where(item => item.Value != 0))
item.Key.Enabled = true;
else
foreach (var item in _list.Where(item => item.Value != group && item.Value != 0))
item.Key.Enabled = false;
}
private IDictionary<SettleOption, decimal> _optionsChosen;
public IDictionary<SettleOption, decimal> OptionsChosen
{
get
{
return _amount == 0 ? _optionsChosen : new Dictionary<SettleOption, decimal>();
}
private set { _optionsChosen = value; }
}
private void SettleChoicesFormLoad(object sender, EventArgs e)
{
txtAmount.Text = string.Format("Pending Amount: Rs. {0}", _amount);
var count = 0;
foreach (SettleOption item in Enum.GetValues(typeof(SettleOption)))
{
var attribute = item.Attribute();
if (!attribute.ShowInChoices)
continue;
var button = new Button
{
Name = item.ToString(),
Text = attribute.Name,
Size = new System.Drawing.Size(75, 75),
TabIndex = count,
UseVisualStyleBackColor = true,
Tag = item
};
button.Click += new EventHandler(ButtonClick);
flpSettlement.Controls.Add(button);
_list.Add(button, attribute.Group);
count++;
}
var controlButton = new Button
{
Name = "btnOK",
Text = "OK",
Size = new System.Drawing.Size(75, 75),
TabIndex = count,
UseVisualStyleBackColor = true,
Tag = "OK"
};
controlButton.Click += new EventHandler(ButtonClick);
flpSettlement.Controls.Add(controlButton);
_list.Add(controlButton, 0);
count++;
controlButton = new Button
{
Name = "btnCancel",
Text = "Cancel",
Size = new System.Drawing.Size(75, 75),
TabIndex = count,
UseVisualStyleBackColor = true,
Tag = "Cancel"
};
controlButton.Click += new EventHandler(ButtonClick);
flpSettlement.Controls.Add(controlButton);
_list.Add(controlButton, 0);
count++;
txtAmount.TabIndex = count;
this.Size = this.SizeFromClientSize(new Size(_list.Count * (75 + 6), 3 + txtAmount.Height + 6 + 75 + 3));
txtAmount.Width = flpSettlement.Width - 6;
}
}
}