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

147 lines
5.0 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 ProductForm_Load(object sender, EventArgs e)
{
FillCombos();
ShowServiceChargeControls((bool)SettingBI.Get("Service Charge").Details["Enabled"]);
if (_productID.HasValue)
{
Product product = ProductBI.Get(_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()
{
bsProductGroups.DataSource = ProductGroupBI.List();
bsServiceTax.DataSource = TaxBI.List();
bsVat.DataSource = TaxBI.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());
product.HasHappyHour = chkHasHappyHour.Checked;
// 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;
//Role
if (cmbProductGroup.SelectedItem == null)
{
MessageBox.Show("Please choose Product Role");
cmbProductGroup.Focus();
return;
}
product.ProductGroup = (ProductGroup)cmbProductGroup.SelectedItem;
if (_productID.HasValue)
ProductBI.Update(product);
else
ProductBI.Insert(product);
MessageBox.Show("Update / Save Successful");
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}