narsil/Tanshu.Accounts.PointOfSale/Products/ModifierEditForm.cs
2018-08-24 16:11:33 +05:30

132 lines
4.1 KiB
C#

using System;
using System.Windows.Forms;
using Tanshu.Accounts.Repository;
using Tanshu.Accounts.Entities;
using System.Linq;
using System.Collections.Generic;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ModifierEditForm : Form
{
private Guid? _modifierID;
public ModifierEditForm(Guid modifierID)
: this()
{
_modifierID = modifierID;
}
public ModifierEditForm()
{
InitializeComponent();
}
private void ModifierEditForm_Load(object sender, EventArgs e)
{
Modifier modifier;
var groups = ProductGroupBI.List();
clbGroups.DataSource = groups;
if (_modifierID.HasValue)
{
modifier = ModifierBI.Get(_modifierID.Value);
txtName.Text = modifier.Name;
chkShowInBill.Checked = modifier.ShowInBill;
txtPrice.Text = modifier.Price.ToString();
if (modifier.ProductGroupModifiers.Any(x => x.ProductGroup == null))
{
chkShowForAll.Checked = true;
clbGroups.Hide();
}
else
{
chkShowForAll.Checked = false;
for (int i = 0; i < clbGroups.Items.Count; i++)
{
var item = (ProductGroup)clbGroups.Items[i];
clbGroups.SetItemChecked(i, modifier.ProductGroupModifiers.Any(x => x.ProductGroup.ProductGroupID == item.ProductGroupID));
}
}
}
else
{
txtName.Focus();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
decimal price;
if (string.IsNullOrEmpty(txtName.Text.Trim()))
{
MessageBox.Show("Name cannot be blank.");
txtName.Focus();
}
else if (!decimal.TryParse(txtPrice.Text, out price) || price < 0)
{
MessageBox.Show("Price has to be a number >= 0.");
txtName.Focus();
}
else
{
Save();
MessageBox.Show("Update / Save Successful");
this.Close();
}
}
private void Save()
{
Modifier modifier;
if (_modifierID.HasValue)
modifier = ModifierBI.Get(_modifierID.Value);
else
modifier = new Modifier();
modifier.Name = txtName.Text.Trim();
modifier.Price = decimal.Parse(txtPrice.Text);
modifier.ShowInBill = chkShowInBill.Checked;
modifier.ProductGroupModifiers = GetProductGroups(modifier);
if (_modifierID.HasValue)
ModifierBI.Update(modifier);
else
ModifierBI.Insert(modifier);
}
private List<ProductGroupModifier> GetProductGroups(Modifier modifier)
{
var list = new List<ProductGroupModifier>();
if (chkShowForAll.Checked)
{
list.Add(new ProductGroupModifier() { ShowAutomatically = false });
}
else
{
foreach (var item in clbGroups.CheckedItems.OfType<ProductGroup>())
{
list.Add(new ProductGroupModifier() { ProductGroup = item });
}
clbGroups.CheckedItems.OfType<ProductGroup>();
}
return list;
}
private void clbGroups_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = ((ProductGroup)e.ListItem).Name;
}
private void chkShowForAll_CheckedChanged(object sender, EventArgs e)
{
if (chkShowForAll.Checked)
clbGroups.Hide();
else
clbGroups.Show();
}
}
}