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

161 lines
5.0 KiB
C#
Raw Normal View History

using System;
using System.Windows.Forms;
using Tanshu.Accounts.Repository;
using Tanshu.Accounts.Entities;
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();
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("#.##");
txtFullPrice.Text = product.FullPrice.ToString("#.##");
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 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 Product IsFormValid()
{
var product = new Product();
if (_productID.HasValue)
product.ProductID = _productID.Value;
if (string.IsNullOrEmpty(txtName.Text.Trim()))
return null;
product.Name = txtName.Text.Trim();
product.Units = txtUnits.Text.Trim();
decimal price;
if (string.IsNullOrEmpty(txtPrice.Text.Trim()))
{
price = 0;
}
else
{
if (!decimal.TryParse(txtPrice.Text.Trim(), out price))
return null;
}
if (price < 0)
return null;
product.Price = price;
if (string.IsNullOrEmpty(txtFullPrice.Text.Trim()))
{
price = 0;
}
else
{
if (!decimal.TryParse(txtFullPrice.Text.Trim(), out price))
return null;
}
if (price < 0 || price < product.Price)
return null;
product.FullPrice = price;
// Tax
if (cmbVat.SelectedItem == null)
return null;
product.Vat = (Tax)cmbVat.SelectedItem;
if (cmbServiceTax.SelectedItem == null)
return null;
product.ServiceTax = (Tax)cmbServiceTax.SelectedItem;
decimal serviceCharge;
if (string.IsNullOrEmpty(txtServiceCharge.Text.Trim()))
{
serviceCharge = 0;
}
else
{
if (!decimal.TryParse(txtServiceCharge.Text.Trim(), out serviceCharge))
return null;
}
if (serviceCharge < 0 || serviceCharge > 1)
return null;
product.ServiceCharge = serviceCharge;
product.IsScTaxable = chkIsScTaxable.Checked;
product.IsActive = chkIsActive.Checked;
product.IsNotAvailable = chkIsNotAvailable.Checked;
//Group
if (cmbProductGroup.SelectedItem == null)
return null;
product.ProductGroup = (ProductGroup)cmbProductGroup.SelectedItem;
return product;
}
private void btnOk_Click(object sender, EventArgs e)
{
var product = IsFormValid();
if (product != null)
{
using (var bi = new ProductBI())
{
if (_productID.HasValue)
bi.Update(product);
else
bi.Insert(product);
bi.SaveChanges();
}
MessageBox.Show("Update / Save Successful");
this.Close();
}
else
MessageBox.Show("The form is not valid");
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}