narsil/Tanshu.Accounts.PointOfSale/Sales/SalesForm.cs

430 lines
16 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Tanshu.Accounts.Repository;
2010-03-02 17:56:21 +00:00
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Helpers;
using Tanshu.Common;
using Tanshu.Accounts.Entities;
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.PointOfSale
{
public partial class SalesForm : Form, ISaleForm
2010-03-02 17:56:21 +00:00
{
BillController billController;
2011-01-09 23:36:24 +00:00
List<Button> buttonList = new List<Button>();
List<Button> buttonHeads = new List<Button>();
public SalesForm(BillController billController)
2010-03-02 17:56:21 +00:00
{
InitializeComponent();
this.billController = billController;
billController.InitGui(this);
2010-03-02 17:56:21 +00:00
}
public SalesForm(int voucherID, BillController billController)
: this(billController)
2010-03-02 17:56:21 +00:00
{
billController.SetNewBillID(voucherID);
2010-03-02 17:56:21 +00:00
}
public void SetUserName(string name)
{
this.Text = name;
}
2010-03-02 17:56:21 +00:00
private void SalesForm_KeyDown(object sender, KeyEventArgs e)
{
2011-01-09 23:36:24 +00:00
switch (e.KeyCode)
2010-03-02 17:56:21 +00:00
{
2011-01-09 23:36:24 +00:00
case Keys.F2:
{
if (dgvProducts.Rows.Count > 0)
{
billController.SetQuantity(billController.CurrentProduct, 0, false, true);
}
2011-01-09 23:36:24 +00:00
break;
}
case Keys.F3:
{
btnDiscount_Click(sender, new EventArgs());
break;
}
case Keys.F4:
{
if (!e.Alt)
billController.ShowCustomerList(false);
2011-01-09 23:36:24 +00:00
break;
}
case Keys.F5:
{
btnWaiter_Click(sender, new EventArgs());
break;
}
case Keys.F7:
{
2011-01-22 12:38:30 +00:00
using (SelectProduct selectProduct = new SelectProduct(ProductBI.GetFilteredProducts, true))
2010-03-02 17:56:21 +00:00
{
2011-01-09 23:36:24 +00:00
selectProduct.ShowDialog();
if (selectProduct.SelectedItem != null)
billController.AddProductToGrid(selectProduct.SelectedItem.ProductID);
2010-03-02 17:56:21 +00:00
}
2011-01-09 23:36:24 +00:00
break;
}
case Keys.F8:
{
billController.LoadBillFromTable(null);
2011-01-09 23:36:24 +00:00
break;
}
case Keys.F9:
{
if (dgvProducts.Rows.Count > 0)
{
billController.SetAmount(billController.CurrentProduct, -1);
}
2011-01-09 23:36:24 +00:00
break;
}
case Keys.F11:
{
btnPrintBill_Click(sender, e);
break;
}
case Keys.F12:
{
btnPrintKot_Click(sender, e);
break;
}
case Keys.Delete:
{
if (dgvProducts.Rows.Count > 0)
billController.ProductRemove(billController.CurrentProduct);
2011-01-09 23:36:24 +00:00
break;
}
case Keys.Add:
{
if (dgvProducts.Rows.Count > 0)
billController.SetQuantity(billController.CurrentProduct, 1, false, false);
2011-01-09 23:36:24 +00:00
break;
}
case Keys.Subtract:
{
if (dgvProducts.Rows.Count > 0)
billController.SetQuantity(billController.CurrentProduct, -1, false, false);
2011-01-09 23:36:24 +00:00
break;
}
case Keys.Up:
{
if ((bindingSource.Position >= 1) && (!dgvProducts.Focused))
bindingSource.Position -= 1;
break;
}
case Keys.Down:
{
if ((bindingSource.Position < bindingSource.Count - 1) && (!dgvProducts.Focused))
bindingSource.Position += 1;
break;
}
case Keys.Escape:
{
billController.CancelBillChanges();
2011-01-09 23:36:24 +00:00
break;
}
2010-03-02 17:56:21 +00:00
}
}
#region Helper Functions
public void ClearBill(OrderedDictionary<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
this.txtBillID.Text = "";
this.txtKotID.Text = "";
this.txtCreationDate.Text = "";
this.txtDate.Text = "";
this.txtLastEditDate.Text = "";
this.txtTableID.Text = "";
this.btnWaiter.Text = "Waiter - F5";
this.btnWaiter.Tag = null;
txtGrossTax.Text = "0.00";
txtDiscount.Text = "0.00";
txtServiceCharge.Text = "0.00";
2010-03-02 17:56:21 +00:00
txtGrossAmount.Text = "0.00";
txtAmount.Text = "0.00";
bindingSource.DataSource = bill.Values;
ChangeFormState(SaleFormState.Waiting);
}
private void ChangeFormState(SaleFormState state)
{
foreach (var button in buttonList)
button.Dispose();
foreach (var button in buttonHeads)
button.Dispose();
buttonList = new List<Button>();
buttonHeads = new List<Button>();
if (state == SaleFormState.Billing)
{
2011-01-22 12:38:30 +00:00
var list = new ProductGroupBI().GetProductGroups();
ControlFactory.GenerateGroups(ref flpGroup, ref buttonHeads, new Point(75, 75), 0, list, new ButtonClickDelegate(productTypeButton_Click));
}
else
{
ControlFactory.GenerateTables(ref flpMain, ref buttonHeads, new Point(75, 75), 0, new FoodTableBI().GetFoodTables(), new ButtonClickDelegate(tableButton_Click));
}
2010-03-02 17:56:21 +00:00
}
2011-01-22 12:38:30 +00:00
2011-01-09 23:36:24 +00:00
private void productTypeButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null)
return;
var item = button.Tag as ProductGroup;
if (item.Name == "Previous" || item.Name == "Next")
{
int start = item.ProductGroupID;
if (start < 0)
start = 0;
var list = new ProductGroupBI().GetProductGroups();
ControlFactory.GenerateGroups(ref flpGroup, ref buttonHeads, new Point(75, 75), start, list, new ButtonClickDelegate(productTypeButton_Click));
}
else
{
ControlFactory.GenerateProducts(ref flpMain, ref buttonList, new Point(75, 75), 0, ProductBI.GetProducts(item.ProductGroupID), new ButtonClickDelegate(productButton_Click));
}
2011-01-09 23:36:24 +00:00
}
private void productButton_Click(object sender, EventArgs e)
2010-03-02 17:56:21 +00:00
{
Button button = sender as Button;
if (button == null)
return;
var item = button.Tag as Product;
if (item.Name == "Previous" || item.Name == "Next")
{
int start = item.ProductID;
if (start < 0)
start = 0;
var list = ProductBI.GetProducts();
ControlFactory.GenerateProducts(ref flpMain, ref buttonList, new Point(75, 75), start, list, new ButtonClickDelegate(productButton_Click));
}
else
{
billController.AddProductToGrid(item.ProductID);
}
2010-03-02 17:56:21 +00:00
}
2011-01-09 23:36:24 +00:00
private void tableButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null)
return;
var item = button.Tag as FoodTable;
if (item.Name == "Previous" || item.Name == "Next")
{
int start = item.FoodTableID;
if (start < 0)
start = 0;
var list = new FoodTableBI().GetFoodTables();
ControlFactory.GenerateTables(ref flpMain, ref buttonHeads, new Point(75, 75), start, list, new ButtonClickDelegate(tableButton_Click));
}
else
{
string tableName = item.Name;
billController.LoadBillFromTable(tableName);
txtTableID.Text = tableName;
ChangeFormState(SaleFormState.Billing);
}
2011-01-09 23:36:24 +00:00
}
public void ShowAmount(decimal discountAmount, decimal grossAmount, decimal serviceChargeAmount, decimal taxAmount, decimal valueAmount, List<BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
txtGrossTax.Text = string.Format("{0:#0.00}", taxAmount);
txtDiscount.Text = string.Format("{0:#0.00}", discountAmount);
txtServiceCharge.Text = string.Format("{0:#0.00}", serviceChargeAmount);
txtGrossAmount.Text = string.Format("{0:#0.00}", grossAmount);
txtAmount.Text = string.Format("{0:#0.00}", Math.Round(valueAmount));
bindingSource.DataSource = bill;
2010-03-02 17:56:21 +00:00
dgvProducts.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
}
private void btnPrintBill_Click(object sender, EventArgs e)
{
if (btnWaiter.Tag == null)
btnWaiter.Tag = WaiterBI.GetWaiters()[0].WaiterID;
billController.Save(true, (int)btnWaiter.Tag, txtTableID.Text);
2010-03-02 17:56:21 +00:00
}
private void btnPrintKot_Click(object sender, EventArgs e)
{
if (btnWaiter.Tag == null)
btnWaiter.Tag = WaiterBI.GetWaiters()[0].WaiterID;
billController.Save(false, (int)btnWaiter.Tag, txtTableID.Text);
2010-03-02 17:56:21 +00:00
}
private void btnMultiPrint_Click(object sender, EventArgs e)
{
if (btnWaiter.Tag == null)
btnWaiter.Tag = WaiterBI.GetWaiters()[0].WaiterID;
billController.Save(true, (int)btnWaiter.Tag, txtTableID.Text);
2010-03-02 17:56:21 +00:00
}
private void btnCancel_Click(object sender, EventArgs e)
{
billController.CancelBillChanges();
2010-03-02 17:56:21 +00:00
}
private void btnQuantity_Click(object sender, EventArgs e)
{
if (dgvProducts.Rows.Count > 0)
{
billController.SetQuantity(billController.CurrentProduct, 0, false, true);
}
2010-03-02 17:56:21 +00:00
}
private void btnDiscount_Click(object sender, EventArgs e)
{
billController.ShowDiscount();
//if (dgvProducts.Rows.Count > 0)
// billController.SetDiscount(billController.CurrentProduct, -1);
2010-03-02 17:56:21 +00:00
}
#endregion
private void SalesForm_Load(object sender, EventArgs e)
{
billController.FormLoad();
ChangeFormState(SaleFormState.Waiting);
2010-03-02 17:56:21 +00:00
}
private void btnCustomer_Click(object sender, EventArgs e)
2010-03-02 17:56:21 +00:00
{
billController.ShowCustomerList(false);
2010-03-02 17:56:21 +00:00
}
public void SetCustomerDisplay(string name)
2010-03-02 17:56:21 +00:00
{
btnCustomer.Text = name;
2010-03-02 17:56:21 +00:00
}
public void ShowInfo(string billID, string kotID, DateTime creationDate, DateTime date, DateTime lastEditDate, string customer, string tableID, int waiterID, string waiter)
2010-03-02 17:56:21 +00:00
{
this.txtBillID.Text = billID;
this.txtKotID.Text = kotID;
this.txtCreationDate.Text = creationDate.ToString("HH:mm dd-MMM-yyyy");
this.txtDate.Text = date.ToString("HH:mm dd-MMM-yyyy");
this.txtLastEditDate.Text = lastEditDate.ToString("HH:mm dd-MMM-yyyy");
this.btnCustomer.Text = customer;
this.txtTableID.Text = tableID;
this.btnWaiter.Tag = waiterID;
this.btnWaiter.Text = string.Format("{0} - F5", waiter);
2010-03-02 17:56:21 +00:00
}
public BindingSource BindingSource
{
get
{
return bindingSource;
}
}
private void btnVoid_Click(object sender, EventArgs e)
2010-03-02 17:56:21 +00:00
{
try
{
billController.VoidBill();
2010-03-02 17:56:21 +00:00
}
catch (PermissionException ex)
2010-03-02 17:56:21 +00:00
{
MessageBox.Show(ex.Message);
2010-03-02 17:56:21 +00:00
}
}
private void btnRate_Click(object sender, EventArgs e)
{
if (dgvProducts.Rows.Count > 0)
2010-03-02 17:56:21 +00:00
{
billController.ChangeRate();
2010-03-02 17:56:21 +00:00
}
}
private void btnClear_Click(object sender, EventArgs e)
{
billController.ClearBill();
2010-03-02 17:56:21 +00:00
}
private void dgvProducts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
BillInventory data = dgv.Rows[e.RowIndex].DataBoundItem as BillInventory;
2010-03-02 17:56:21 +00:00
if (data.Printed > 0)
{
e.CellStyle.SelectionBackColor = Color.HotPink;
e.CellStyle.BackColor = Color.LightPink;
}
else
{
e.CellStyle.SelectionBackColor = Color.Green;
e.CellStyle.BackColor = Color.LightGreen;
}
}
private void btnWaiter_Click(object sender, EventArgs e)
{
using (SelectWaiter selectWaiter = new SelectWaiter(WaiterBI.GetFilteredWaiters, true))
2010-03-02 17:56:21 +00:00
{
selectWaiter.waiterEvent += new WaiterEventHandler(selectWaiter_waiterEvent);
selectWaiter.ShowDialog();
if (selectWaiter.SelectedItem != null)
{
btnWaiter.Text = string.Format("{0} - F5", selectWaiter.SelectedItem.Name);
btnWaiter.Tag = selectWaiter.SelectedItem.WaiterID;
}
else
{
btnWaiter.Text = "Select Waiter - F5";
btnWaiter.Tag = WaiterBI.GetWaiters()[0].WaiterID;
2010-03-02 17:56:21 +00:00
}
}
}
Waiter selectWaiter_waiterEvent(object sender, WaiterEventArgs e)
2010-03-02 17:56:21 +00:00
{
Waiter waiter = e.Waiter;
2010-03-02 17:56:21 +00:00
if (!Thread.CurrentPrincipal.IsInRole("Waiter/Master"))
return waiter;
switch (e.Action)
{
case 1: // Add
WaiterBI.Insert(waiter);
2010-03-02 17:56:21 +00:00
e.Handled = true;
return waiter;
case 2: // Edit
WaiterBI.Update(waiter);
2010-03-02 17:56:21 +00:00
e.Handled = true;
return waiter;
case 3: // Delete
e.Handled = WaiterBI.Delete(waiter.WaiterID);
return WaiterBI.GetWaiter(1);
2010-03-02 17:56:21 +00:00
default:
throw new ArgumentException();
}
}
public void CloseWindow()
{
this.Close();
}
2011-01-09 23:36:24 +00:00
private void btnSettle_Click(object sender, EventArgs e)
2010-03-02 17:56:21 +00:00
{
billController.SettleBill();
2010-03-02 17:56:21 +00:00
}
private void btnModifier_Click(object sender, EventArgs e)
{
var item = billController.CurrentProduct;
var id = new ProductGroupBI().GetProductGroupOfProduct(item.ProductID).ProductGroupID;
billController.ShowModifiers(id, item);
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (dgvProducts.Rows.Count > 0)
billController.ProductRemove(billController.CurrentProduct);
}
2010-03-02 17:56:21 +00:00
}
}