narsil/Tanshu.Accounts.PointOfSale/Products/ProductForm.cs
unknown d8ecec8bb6 Added inverse Attribute to ProductGroup.
BillInventory Renamed.
Refactored Bill to be more usable.
Added Bill Detail Report.
Added Open Bill and Bill Details Roles.
Zero Rate Products have Yellow background Color.
Refactored UserBI, FoodTableBI, ModifierBI, PrintLocationBI, ProductBI, ProductGroupBI, TaxBI, UserBI,
Cached the Products List.
Product and Product Group Form Working.
2011-06-23 18:17:48 +05:30

135 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Tanshu.Accounts.Helpers;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Repository;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ProductForm : Form
{
private int? _productID;
public ProductForm(int? 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);
txtProductID.Text = _productID.Value.ToString();
txtCode.Text = product.Code.ToString();
txtName.Text = product.Name;
txtUnits.Text = product.Units;
txtSalePrice.Text = product.SalePrice.ToString("#.##");
cmbTax.SelectedValue = product.Tax.TaxID;
txtServiceCharge.Text = product.ServiceCharge.ToString("#.##");
chkDiscontinued.Checked = product.Discontinued;
cmbProductGroup.SelectedValue = product.ProductGroup.ProductGroupID;
}
else
{
txtProductID.Text = "(Auto)";
txtName.Focus();
}
}
private void FillCombos()
{
using (var bi = new ProductGroupBI())
bsProductGroups.DataSource = bi.List();
using (var bi = new TaxBI())
bsTax.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;
int code;
if (!int.TryParse(txtCode.Text, out code))
return null;
if (code < 0)
return null;
product.Code = code;
if (string.IsNullOrEmpty(txtName.Text.Trim()))
return null;
product.Name = txtName.Text.Trim();
if (string.IsNullOrEmpty(txtUnits.Text.Trim()))
return null;
product.Units = txtUnits.Text.Trim();
decimal salePrice;
if (!decimal.TryParse(txtSalePrice.Text, out salePrice))
return null;
if (salePrice < 0)
return null;
product.SalePrice = salePrice;
// Tax
if (cmbTax.SelectedItem == null)
return null;
product.Tax = (Tax)cmbTax.SelectedItem;
decimal serviceCharge;
if (!decimal.TryParse(txtServiceCharge.Text, out serviceCharge))
return null;
if (serviceCharge < 0 || serviceCharge > 1)
return null;
product.ServiceCharge = serviceCharge;
product.Discontinued = chkDiscontinued.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);
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();
}
}
}