using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ubik.Engine.Client;
using StoresAndStockPricing.Controls;
using StoresAndStockPricing.Model;
namespace StoresAndStockPricing.Client{
public partial class StoresAndStockPricingForm : Form
{
private Session _session;
BoundComboBoxSource<RetailStore> _viewPriceAtComboSource;
BoundDataGridViewSource<Manufacturer> _manufacturerGridSource;
BoundDataGridViewSource<Brand> _brandGridSource;
BoundDataGridViewSource<StockItemSellingPrice> _stockItemGridSource;
public StoresAndStockPricingForm()
{
InitializeComponent();
}
public StoresAndStockPricingForm(Session session, string serverUri)
: this()
{
if (session == null)
throw new ArgumentNullException("session");
if (serverUri == null)
throw new ArgumentNullException("serverUri");
_session = session;
_serverUriStatusLabel.Text = serverUri;
// Bind the "Stock Item Selling Price at" combo box
_viewPriceAtComboSource = new BoundComboBoxSource<RetailStore>(
_session,
delegate(Session s, RetailStore store)
{
return store.Name;
});
_viewPriceAtComboSource.Filter = "";
_viewPriceAtCombo.DataSource = _viewPriceAtComboSource;
// Bind the "Manufacturer" data grid
_manufacturerGridSource = new BoundDataGridViewSource<Manufacturer>(_session);
_manufacturerGridSource.Columns.Add(new StringColumn<Manufacturer>(
_session,
StoresAndStockPricingFormResources.Manufacturer,
delegate(Session s, Manufacturer manufacturer)
{
return manufacturer.Name;
}));
_manufacturerGridSource.Filter = "";
_manufacturerGrid.BindingSource = _manufacturerGridSource;
// Bind the "Brand" data grid
_brandGridSource = new BoundDataGridViewSource<Brand>(_session);
_brandGridSource.Columns.Add(new StringColumn<Brand>(
_session,
StoresAndStockPricingFormResources.Brand,
delegate(Session s, Brand brand)
{
return brand.Name;
}));
_brandGrid.BindingSource = _brandGridSource;
// Bind the "Stock Item" data grid
_stockItemGridSource = new BoundDataGridViewSource<StockItemSellingPrice>(_session,
new Type[] { typeof(StockItem), typeof(Brand), typeof(Manufacturer) },
delegate(Session s, StockItemSellingPrice stockItemSellingPrice, Guid identity)
{
return identity == stockItemSellingPrice.StockItem.Identity ||
identity == stockItemSellingPrice.StockItem.Brand.Identity ||
identity == stockItemSellingPrice.StockItem.Brand.Manufacturer.Identity;
});
_stockItemGridSource.Columns.Add(new StringColumn<StockItemSellingPrice>(
_session,
StoresAndStockPricingFormResources.StockItem,
delegate(Session s, StockItemSellingPrice stockItemSellingPrice)
{
return stockItemSellingPrice.StockItem.Name;
},
delegate(Session s, StockItemSellingPrice stockItemSellingPrice, string value)
{
stockItemSellingPrice.StockItem.Name = value;
}));
_stockItemGridSource.Columns.Add(new StringColumn<StockItemSellingPrice>(
_session,
StoresAndStockPricingFormResources.SellingPrice,
delegate(Session s, StockItemSellingPrice stockItemSellingPrice)
{
return string.Format("{0:c}", stockItemSellingPrice.SellingPrice);
}));
_stockItemGridSource.Columns.Add(new StringColumn<StockItemSellingPrice>(
_session,
StoresAndStockPricingFormResources.EffectiveSince,
delegate(Session s, StockItemSellingPrice stockItemSellingPrice)
{
return stockItemSellingPrice.EffectiveSince.ToLocalTime().ToString(); ;
}));
_stockItemGridSource.Columns.Add(new StringColumn<StockItemSellingPrice>(
_session,
StoresAndStockPricingFormResources.Manufacturer,
delegate(Session s, StockItemSellingPrice stockItemSellingPrice)
{
return stockItemSellingPrice.StockItem.Brand.Manufacturer.Name;
}));
_stockItemGridSource.Columns.Add(new StringColumn<StockItemSellingPrice>(
_session,
StoresAndStockPricingFormResources.Brand,
delegate(Session s, StockItemSellingPrice stockItemSellingPrice)
{
return stockItemSellingPrice.StockItem.Brand.Name;
}));
_stockItemSellingPriceGrid.BindingSource = _stockItemGridSource;
// Bind Events
_manufacturerGrid.SelectionChanged += new System.EventHandler(_manufacturerGrid_SelectionChanged);
_brandGrid.SelectionChanged += new System.EventHandler(_brandGrid_SelectionChanged);
_viewPriceAtCombo.SelectedValueChanged += new System.EventHandler(_viewPriceAtCombo_SelectedValueChanged);
_stockItemSellingPriceGrid.SelectionChanged += new EventHandler(_stockItemSellingPriceGrid_SelectionChanged);
}
private void _manufacturerGrid_SelectionChanged(object sender, EventArgs e)
{
UpdateBrandFilter();
_manufacturerDetailsButton.Enabled = _manufacturerGrid.SelectedRows.Count == 1;
_newBrandButton.Enabled = _manufacturerGrid.SelectedRows.Count == 1;
}
private void UpdateBrandFilter()
{
string filter = "";
foreach (Manufacturer manufacturer in _manufacturerGrid.GetSelectedItems<Manufacturer>())
{
if (filter.Length != 0)
{
filter += " || ";
}
filter += "Manufacturer = {" + manufacturer.ToString() + "}";
}
if (filter.Length > 0)
{
_brandGridSource.Filter = filter;
}
else
{
_brandGridSource.Clear();
}
}
private void _brandGrid_SelectionChanged(object sender, EventArgs e)
{
UpdateStockItemFilter();
_brandDetailsButton.Enabled = _brandGrid.SelectedRows.Count == 1;
_newStockItemButton.Enabled = _brandGrid.SelectedRows.Count == 1;
}
private void UpdateStockItemFilter()
{
string filter = "";
foreach (Brand brand in _brandGrid.GetSelectedItems<Brand>())
{
if (filter.Length != 0)
{
filter += " || ";
}
filter += "RetailStore = {" + _viewPriceAtCombo.GetSelectedItem<RetailStore>().ToString() +
"} && StockItem.Brand = {" + brand.ToString() + "}";
}
if (filter.Length > 0)
{
_stockItemGridSource.Filter = filter;
}
else
{
_stockItemGridSource.Clear();
}
}
private void _newManufacturerButton_Click(object sender, EventArgs e)
{
using (Transaction txn = _session.BeginTransaction())
{
Manufacturer newManufacturer = new Manufacturer(_session, StoresAndStockPricingFormResources.NewManufacturerName);
newManufacturer.Insert();
ManufacturerDetailsForm mdf = new ManufacturerDetailsForm(_session, newManufacturer);
if (mdf.ShowDialog(this) == DialogResult.OK)
{
txn.Commit();
_manufacturerGrid.SelectItem<Manufacturer>(newManufacturer);
}
else
{
txn.Abort();
}
}
}
private void _manufacturerDetailsButton_Click(object sender, EventArgs e)
{
using (Transaction txn = _session.BeginTransaction())
{
Manufacturer manufacturer = _manufacturerGrid.GetSelectedItem<Manufacturer>();
ManufacturerDetailsForm mdf = new ManufacturerDetailsForm(_session, manufacturer);
if (mdf.ShowDialog(this) == DialogResult.OK)
{
txn.Commit();
}
else
{
txn.Abort();
}
}
}
private void _newBrandButton_Click(object sender, EventArgs e)
{
using (Transaction txn = _session.BeginTransaction())
{
Brand newBrand = new Brand(_session, StoresAndStockPricingFormResources.NewBrandName, _manufacturerGrid.GetSelectedItem<Manufacturer>());
newBrand.Insert();
BrandDetailsForm bdf = new BrandDetailsForm(_session, newBrand);
if (bdf.ShowDialog(this) == DialogResult.OK)
{
txn.Commit();
_brandGrid.SelectItem<Brand>(newBrand);
}
else
{
txn.Abort();
}
}
}
private void _brandDetailsButton_Click(object sender, EventArgs e)
{
using (Transaction txn = _session.BeginTransaction())
{
Brand brand = _brandGrid.GetSelectedItem<Brand>();
BrandDetailsForm bdf = new BrandDetailsForm(_session, brand);
if (bdf.ShowDialog(this) == DialogResult.OK)
{
txn.Commit();
}
else
{
txn.Abort();
}
}
}
private void _viewPriceAtCombo_SelectedValueChanged(object sender, EventArgs e)
{
UpdateStockItemFilter();
}
private void _deleteButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show(StoresAndStockPricingFormResources.DeleteConfirmation, Text, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using (Transaction txn = _session.BeginTransaction())
{
foreach (StockItemSellingPrice selection in _stockItemSellingPriceGrid.GetSelectedItems<StockItemSellingPrice>())
{
selection.StockItem.Delete();
}
txn.Commit();
}
}
}
void _stockItemSellingPriceGrid_SelectionChanged(object sender, EventArgs e)
{
_deleteButton.Enabled = _stockItemSellingPriceGrid.SelectedRows.Count > 0;
_schedulePriceChangeButton.Enabled = _stockItemSellingPriceGrid.SelectedRows.Count == 1;
}
private void _schedulePriceChangeButton_Click(object sender, EventArgs e)
{
using (Transaction txn = _session.BeginTransaction())
{
StockItem stockItem = _stockItemSellingPriceGrid.GetSelectedItem<StockItemSellingPrice>().StockItem;
RetailStoreGroup defaultGroup = _viewPriceAtCombo.GetSelectedItem<RetailStore>().SystemGroup;
SchedulePriceChangeForm pcf = new SchedulePriceChangeForm(_session, stockItem, defaultGroup);
if (pcf.ShowDialog(this) == DialogResult.OK)
{
txn.Commit();
}
else
{
txn.Abort();
}
}
}
private void _newStockItemButton_Click(object sender, EventArgs e)
{
StockItem newStockItem;
using (Transaction txn = _session.BeginTransaction())
{
newStockItem = new StockItem(_session,
StoresAndStockPricingFormResources.NewStockItemName,
_brandGrid.GetSelectedItem<Brand>());
newStockItem.Insert();
newStockItem.ChangeSellingPrice(0M);
txn.Commit();
}
_stockItemSellingPriceGrid.SelectItem<StockItemSellingPrice>(
newStockItem.GetPriceAt(_viewPriceAtCombo.GetSelectedItem<RetailStore>()));
_stockItemSellingPriceGrid.CurrentCell =
_stockItemSellingPriceGrid.SelectedRows[0].Cells[0];
_stockItemSellingPriceGrid.BeginEdit(true);
}
}
}
|