narsil/Tanshu.Accounts.PointOfSale/Products/ProductForm.cs
tanshu 20eac3c216 Refactor: Instead of a concept of Price/FullPrice, happy hour is now a checkbox in the product.
This needed major refactor in all parts dealing with product or inventory.
2016-04-11 12:31:52 +05:30

156 lines
5.3 KiB
C#

using System;
using System.Windows.Forms;
using Tanshu.Accounts.Repository;
using Tanshu.Accounts.Entities;
using System.Text.RegularExpressions;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ProductForm : Form
{
private Guid? _productID;
public ProductForm(Guid? productID)
{
_productID = productID;
InitializeComponent();
}
private void Products_Load(object sender, EventArgs e)
{
FillCombos();
using (var bis = new SettingBI())
ShowServiceChargeControls(bis.Get(x => x.Name == "Service Charge Enabled").Details == "yes");
if (_productID.HasValue)
{
Product product;
using (var bi = new ProductBI())
product = bi.Get(x => x.ProductID == _productID.Value);
txtName.Text = product.Name;
txtUnits.Text = product.Units;
txtPrice.Text = product.Price.ToString("#.##");
chkHasHappyHour.Checked = product.HasHappyHour;
cmbVat.SelectedValue = product.Vat.TaxID;
cmbServiceTax.SelectedValue = product.ServiceTax.TaxID;
txtServiceCharge.Text = product.ServiceCharge.ToString("#.##");
chkIsActive.Checked = product.IsActive;
chkIsScTaxable.Checked = product.IsScTaxable;
chkIsNotAvailable.Checked = product.IsNotAvailable;
cmbProductGroup.SelectedValue = product.ProductGroup.ProductGroupID;
}
else
{
txtName.Focus();
}
}
private void ShowServiceChargeControls(bool enabled)
{
lblServiceCharge.Visible = enabled;
txtServiceCharge.Visible = enabled;
chkIsScTaxable.Visible = enabled;
}
private void FillCombos()
{
using (var bi = new ProductGroupBI())
bsProductGroups.DataSource = bi.List();
using (var bi = new TaxBI())
{
bsServiceTax.DataSource = bi.List();
bsVat.DataSource = bi.List();
}
}
private void btnAddCategory_Click(object sender, EventArgs e)
{
using (var frm = new ProductGroupListForm())
frm.ShowDialog();
FillCombos();
cmbProductGroup.SelectedIndex = -1;
}
private void btnOk_Click(object sender, EventArgs e)
{
var product = new Product();
if (_productID.HasValue)
product.ProductID = _productID.Value;
if (string.IsNullOrEmpty(txtName.Text.Trim()))
{
MessageBox.Show("Product name cannot be blank.");
txtName.Focus();
return;
}
product.Name = txtName.Text.Trim();
product.Units = txtUnits.Text.Trim();
if (string.IsNullOrEmpty(txtPrice.Text.Trim()))
txtPrice.Text = "0";
if (!Regex.IsMatch(txtPrice.Text, @"^\d*([.]\d{1,5})?$"))
{
MessageBox.Show("Price is not valid, it must be a decimal >= 0");
txtPrice.Focus();
return;
}
else
product.Price = decimal.Parse(txtPrice.Text.Trim());
// Tax
if (cmbVat.SelectedItem == null)
{
MessageBox.Show("Please choose VAT");
cmbVat.Focus();
return;
}
product.Vat = (Tax)cmbVat.SelectedItem;
if (cmbServiceTax.SelectedItem == null)
{
MessageBox.Show("Please choose Service Tax");
cmbServiceTax.Focus();
return;
}
product.ServiceTax = (Tax)cmbServiceTax.SelectedItem;
if (string.IsNullOrEmpty(txtServiceCharge.Text.Trim()))
txtServiceCharge.Text = "0";
if (!Regex.IsMatch(txtServiceCharge.Text, @"^0*([.]\d{1,5})?$"))
{
MessageBox.Show("Service Charge is not valid, it must be a decimal >= 0 and < 1");
txtServiceCharge.Focus();
return;
}
else
product.ServiceCharge = decimal.Parse(txtServiceCharge.Text.Trim());
product.IsScTaxable = chkIsScTaxable.Checked;
product.IsActive = chkIsActive.Checked;
product.IsNotAvailable = chkIsNotAvailable.Checked;
//Group
if (cmbProductGroup.SelectedItem == null)
{
MessageBox.Show("Please choose Product Group");
cmbProductGroup.Focus();
return;
}
product.ProductGroup = (ProductGroup)cmbProductGroup.SelectedItem;
using (var bi = new ProductBI())
{
if (_productID.HasValue)
bi.Update(product);
else
bi.Insert(product);
bi.SaveChanges();
}
MessageBox.Show("Update / Save Successful");
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}