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

430 lines
16 KiB
C#

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;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Helpers;
using Tanshu.Common;
using Tanshu.Accounts.Entities;
namespace Tanshu.Accounts.PointOfSale
{
public partial class SalesForm : Form, ISaleForm
{
BillController billController;
List<Button> buttonList = new List<Button>();
List<Button> buttonHeads = new List<Button>();
public SalesForm(BillController billController)
{
InitializeComponent();
this.billController = billController;
billController.InitGui(this);
}
public SalesForm(int voucherID, BillController billController)
: this(billController)
{
billController.SetNewBillID(voucherID);
}
public void SetUserName(string name)
{
this.Text = name;
}
private void SalesForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.F2:
{
if (dgvProducts.Rows.Count > 0)
{
billController.SetQuantity(billController.CurrentProduct, 0, false, true);
}
break;
}
case Keys.F3:
{
btnDiscount_Click(sender, new EventArgs());
break;
}
case Keys.F4:
{
if (!e.Alt)
billController.ShowCustomerList(false);
break;
}
case Keys.F5:
{
btnWaiter_Click(sender, new EventArgs());
break;
}
case Keys.F7:
{
using (SelectProduct selectProduct = new SelectProduct(ProductBI.GetFilteredProducts, true))
{
selectProduct.ShowDialog();
if (selectProduct.SelectedItem != null)
billController.AddProductToGrid(selectProduct.SelectedItem.ProductID);
}
break;
}
case Keys.F8:
{
billController.LoadBillFromTable(null);
break;
}
case Keys.F9:
{
if (dgvProducts.Rows.Count > 0)
{
billController.SetAmount(billController.CurrentProduct, -1);
}
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);
break;
}
case Keys.Add:
{
if (dgvProducts.Rows.Count > 0)
billController.SetQuantity(billController.CurrentProduct, 1, false, false);
break;
}
case Keys.Subtract:
{
if (dgvProducts.Rows.Count > 0)
billController.SetQuantity(billController.CurrentProduct, -1, false, false);
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();
break;
}
}
}
#region Helper Functions
public void ClearBill(OrderedDictionary<BillItemKey, BillInventory> bill)
{
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";
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)
{
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().List(), new ButtonClickDelegate(tableButton_Click));
}
}
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));
}
}
private void productButton_Click(object sender, EventArgs e)
{
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);
}
}
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().List();
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);
}
}
public void ShowAmount(decimal discountAmount, decimal grossAmount, decimal serviceChargeAmount, decimal taxAmount, decimal valueAmount, List<BillInventory> bill)
{
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;
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);
}
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);
}
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);
}
private void btnCancel_Click(object sender, EventArgs e)
{
billController.CancelBillChanges();
}
private void btnQuantity_Click(object sender, EventArgs e)
{
if (dgvProducts.Rows.Count > 0)
{
billController.SetQuantity(billController.CurrentProduct, 0, false, true);
}
}
private void btnDiscount_Click(object sender, EventArgs e)
{
billController.ShowDiscount();
//if (dgvProducts.Rows.Count > 0)
// billController.SetDiscount(billController.CurrentProduct, -1);
}
#endregion
private void SalesForm_Load(object sender, EventArgs e)
{
billController.FormLoad();
ChangeFormState(SaleFormState.Waiting);
}
private void btnCustomer_Click(object sender, EventArgs e)
{
billController.ShowCustomerList(false);
}
public void SetCustomerDisplay(string name)
{
btnCustomer.Text = name;
}
public void ShowInfo(string billID, string kotID, DateTime creationDate, DateTime date, DateTime lastEditDate, string customer, string tableID, int waiterID, string waiter)
{
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);
}
public BindingSource BindingSource
{
get
{
return bindingSource;
}
}
private void btnVoid_Click(object sender, EventArgs e)
{
try
{
billController.VoidBill();
}
catch (PermissionException ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnRate_Click(object sender, EventArgs e)
{
if (dgvProducts.Rows.Count > 0)
{
billController.ChangeRate();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
billController.ClearBill();
}
private void dgvProducts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
BillInventory data = dgv.Rows[e.RowIndex].DataBoundItem as BillInventory;
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))
{
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;
}
}
}
Waiter selectWaiter_waiterEvent(object sender, WaiterEventArgs e)
{
Waiter waiter = e.Waiter;
if (!Thread.CurrentPrincipal.IsInRole("Waiter/Master"))
return waiter;
switch (e.Action)
{
case 1: // Add
WaiterBI.Insert(waiter);
e.Handled = true;
return waiter;
case 2: // Edit
WaiterBI.Update(waiter);
e.Handled = true;
return waiter;
case 3: // Delete
e.Handled = WaiterBI.Delete(waiter.WaiterID);
return WaiterBI.GetWaiter(1);
default:
throw new ArgumentException();
}
}
public void CloseWindow()
{
this.Close();
}
private void btnSettle_Click(object sender, EventArgs e)
{
billController.SettleBill();
}
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);
}
}
}