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

112 lines
3.6 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;
using (var bi = new ModifierBI())
{
var groups = bi.ProductGroupList();
clbGroups.DataSource = groups;
if (_modifierID.HasValue)
{
modifier = bi.Get(x => x.ModifierID == _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];
if (item.ProductGroupModifiers.Any(x => x.Modifier == modifier))
{
clbGroups.SetItemChecked(i, true);
}
}
}
else
{
txtName.Focus();
}
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
Modifier modifier;
using (var bi = new ModifierBI())
{
if (_modifierID.HasValue)
modifier = bi.Get(x => x.ModifierID == _modifierID.Value);
else
modifier = new Modifier();
if (string.IsNullOrEmpty(txtName.Text.Trim()))
return;
modifier.Name = txtName.Text.Trim();
decimal price;
if (!decimal.TryParse(txtPrice.Text, out price))
return;
if (price < 0)
return;
modifier.Price = price;
modifier.ShowInBill = chkShowInBill.Checked;
if (_modifierID.HasValue)
bi.Update(modifier, chkShowForAll.Checked, clbGroups.CheckedItems.OfType<ProductGroup>());
else
bi.Insert(modifier, chkShowForAll.Checked, clbGroups.CheckedItems.OfType<ProductGroup>());
bi.SaveChanges();
}
MessageBox.Show("Update / Save Successful");
this.Close();
}
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();
}
}
}