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

148 lines
5.0 KiB
C#

using System;
using System.Windows.Forms;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Repository;
using System.Collections.Generic;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ProductListForm : Form
{
private IList<Product> _list;
public ProductListForm()
{
InitializeComponent();
dgvProducts.CellClick += new DataGridViewCellEventHandler(dgvProducts_CellClick);
}
private void btnAdd_Click(object sender, EventArgs e)
{
using (var frm = new ProductForm(null))
frm.ShowDialog();
using (var bi = new ProductBI())
_list = bi.List();
bsList.DataSource = _list;
}
private void ProductListForm_Load(object sender, EventArgs e)
{
ShowGrid();
}
private void ShowGrid()
{
using (var bi = new ProductBI())
switch (chkIsActive.CheckState)
{
case CheckState.Checked:
_list = bi.List(x => x.IsActive == true);
break;
case CheckState.Unchecked:
_list = bi.List(x => x.IsActive == false);
break;
default:
_list = bi.List();
break;
}
bsList.DataSource = _list;
}
private void btnEdit_Click(object sender, EventArgs e)
{
var id = ((Product)bsList.Current).ProductID;
using (var frm = new ProductForm(id))
frm.ShowDialog();
using (var bi = new ProductBI())
_list = bi.List();
bsList.DataSource = _list;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void dgvProductTypes_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var data = dgvProducts.Rows[e.RowIndex].DataBoundItem as Product;
if (data == null)
return;
var tax = e.Value as Tax;
if (tax != null)
{
e.Value = string.Format("{1:P} - {0}", tax.Name, tax.Rate); ;
return;
}
var productGroup = e.Value as ProductGroup;
if (productGroup != null)
{
e.Value = productGroup.Name;
}
}
private void dgvProducts_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Ignore clicks that are not on button cells.
if (e.RowIndex < 0)
return;
// Ignore clicks that are not on button cells.
if (e.ColumnIndex == dgvProducts.Columns["moveUp"].Index)
{
var product = dgvProducts.Rows[e.RowIndex].DataBoundItem as Product;
var index = _list.IndexOf(product);
if (index == 0)
return;
var previousProduct = _list[index - 1];
if (previousProduct.ProductGroup.ProductGroupID != product.ProductGroup.ProductGroupID)
return;
_list.RemoveAt(index);
_list.Insert(index - 1, product);
bsList.DataSource = _list;
bsList.ResetBindings(false);
bsList.CurrencyManager.Position -= 1;
}
if (e.ColumnIndex == dgvProducts.Columns["moveDown"].Index)
{
var product = dgvProducts.Rows[e.RowIndex].DataBoundItem as Product;
var index = _list.IndexOf(product);
if (index == _list.Count + 1)
return;
var nextProduct = _list[index + 1];
if (product.ProductGroup.ProductGroupID != nextProduct.ProductGroup.ProductGroupID)
return;
_list.RemoveAt(index);
_list.Insert(index + 1, product);
bsList.DataSource = _list;
bsList.ResetBindings(false);
bsList.CurrencyManager.Position += 1;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
using (var bi = new ProductBI())
{
bi.UpdateSortOrder(_list);
bi.SaveChanges();
switch (chkIsActive.CheckState)
{
case CheckState.Checked:
_list = bi.List(x => x.IsActive == true);
break;
case CheckState.Unchecked:
_list = bi.List(x => x.IsActive == false);
break;
default:
_list = bi.List();
break;
}
bsList.DataSource = _list;
}
}
private void chkIsActive_CheckStateChanged(object sender, EventArgs e)
{
ShowGrid();
}
}
}