narsil/Tanshu.Accounts.PointOfSale/Products/ModifierEditForm.cs

132 lines
4.1 KiB
C#
Raw Normal View History

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;
2018-08-24 10:41:33 +00:00
var groups = ProductGroupBI.List();
clbGroups.DataSource = groups;
if (_modifierID.HasValue)
{
2018-08-24 10:41:33 +00:00
modifier = ModifierBI.Get(_modifierID.Value);
2018-08-24 10:41:33 +00:00
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];
2018-08-24 10:41:33 +00:00
clbGroups.SetItemChecked(i, modifier.ProductGroupModifiers.Any(x => x.ProductGroup.ProductGroupID == item.ProductGroupID));
}
}
2018-08-24 10:41:33 +00:00
}
else
{
txtName.Focus();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
2018-08-24 10:41:33 +00:00
decimal price;
if (string.IsNullOrEmpty(txtName.Text.Trim()))
{
2018-08-24 10:41:33 +00:00
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();
2018-08-24 10:41:33 +00:00
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 });
}
2018-08-24 10:41:33 +00:00
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();
}
}
}