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

387 lines
15 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.BI;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Helpers;
using Tanshu.Common;
namespace Tanshu.Accounts.PointOfSale
{
public partial class SalesForm : Form, ISaleForm
{
BillController billController;
List<Button> buttonList = new List<Button>();
List<Button> buttonHeads = new List<Button>();
int page = 0;
int pageSize = 6;
public SalesForm(BillController billController)
{
InitializeComponent();
this.billController = billController;
billController.InitGui(this);
}
public SalesForm(Guid 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(dgvProducts.CurrentRow.Index), bindingSource, 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(new ProductBI().GetFilteredProducts, true))
{
selectProduct.ShowDialog();
if (selectProduct.SelectedItem != null)
billController.AddProductToGrid(selectProduct.SelectedItem.ProductID, bindingSource);
}
break;
}
case Keys.F8:
{
billController.LoadBillFromTable(null);
break;
}
case Keys.F9:
{
if (dgvProducts.Rows.Count > 0)
billController.SetAmount(billController.CurrentProduct(dgvProducts.CurrentRow.Index), bindingSource, -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(dgvProducts.CurrentRow.Index));
break;
}
case Keys.Add:
{
if (dgvProducts.Rows.Count > 0)
billController.SetQuantity(billController.CurrentProduct(dgvProducts.CurrentRow.Index), bindingSource, 1, false, false);
break;
}
case Keys.Subtract:
{
if (dgvProducts.Rows.Count > 0)
billController.SetQuantity(billController.CurrentProduct(dgvProducts.CurrentRow.Index), bindingSource, -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(Dictionary<BillItemKey, SalesBillItemBO> 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";
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)
{
btnPrevious.Visible = true;
btnNext.Visible = true;
page = 0;
var list = new ProductTypeBI().GetProductTypes();
ControlFactory.GenerateButtons(ref pnlBilling, ref buttonHeads, new Rectangle(889, 90, 85, 498), 1, pageSize, 2, page, list, new ButtonClickDelegate(productTypeButton_Click));
}
else
{
btnPrevious.Visible = false;
btnNext.Visible = false;
ControlFactory.GenerateTables(ref pnlBilling, ref buttonHeads, new Rectangle(390, 94, 499, 385), 6, 10, 2, new FoodTableBI().GetFoodTables(), new ButtonClickDelegate(tableButton_Click));
}
}
private void productTypeButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null)
return;
Guid tag = (Guid)button.Tag;
ControlFactory.GenerateButtons(ref pnlBilling, ref buttonList, new Rectangle(390, 94, 499, 385), 5, 6, 2, new ProductBI().GetProducts(tag), new ButtonClickDelegate(productButton_Click));
}
private void productButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null)
return;
Guid tag = (Guid)button.Tag;
billController.AddProductToGrid(tag, bindingSource);
}
private void tableButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null)
return;
string tableName = button.Text;
billController.LoadBillFromTable(tableName);
txtTableID.Text = tableName;
ChangeFormState(SaleFormState.Billing);
}
public void ShowAmount(decimal grossTax, decimal discount, decimal grossAmount, decimal amount, List<SalesBillItemBO> bill)
{
txtGrossTax.Text = string.Format("{0:#0.00}", grossTax);
txtDiscount.Text = string.Format("{0:#0.00}", discount);
txtGrossAmount.Text = string.Format("{0:#0.00}", grossAmount);
txtAmount.Text = string.Format("{0:#0.00}", Math.Round(amount));
bindingSource.DataSource = bill;
dgvProducts.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
}
private void btnPrintBill_Click(object sender, EventArgs e)
{
if (btnWaiter.Tag == null)
btnWaiter.Tag = new WaiterBI().GetWaiters()[0].WaiterID;
billController.Save(true, (Guid)btnWaiter.Tag, txtTableID.Text);
}
private void btnPrintKot_Click(object sender, EventArgs e)
{
if (btnWaiter.Tag == null)
btnWaiter.Tag = new WaiterBI().GetWaiters()[0].WaiterID;
billController.Save(false, (Guid)btnWaiter.Tag, txtTableID.Text);
}
private void btnMultiPrint_Click(object sender, EventArgs e)
{
if (btnWaiter.Tag == null)
btnWaiter.Tag = new WaiterBI().GetWaiters()[0].WaiterID;
billController.Save(true, (Guid)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(dgvProducts.CurrentRow.Index), bindingSource, 0, false, true);
}
private void btnDiscount_Click(object sender, EventArgs e)
{
if (dgvProducts.Rows.Count > 0)
billController.SetDiscount(billController.CurrentProduct(dgvProducts.CurrentRow.Index), -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, Guid 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);
}
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(dgvProducts.CurrentRow.Index);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
billController.ClearBill();
}
private void dgvProducts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
SalesBillItemBO data = dgv.Rows[e.RowIndex].DataBoundItem as SalesBillItemBO;
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(new 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 = new WaiterBI().GetWaiters()[0].WaiterID;
}
}
}
WaiterBO selectWaiter_waiterEvent(object sender, WaiterEventArgs e)
{
WaiterBO waiter = e.Waiter;
if (!Thread.CurrentPrincipal.IsInRole("Waiter/Master"))
return waiter;
switch (e.Action)
{
case 1: // Add
new WaiterBI().Insert(waiter);
e.Handled = true;
return waiter;
case 2: // Edit
new WaiterBI().Update(waiter);
e.Handled = true;
return waiter;
case 3: // Delete
e.Handled = new WaiterBI().Delete(waiter.WaiterID);
return new WaiterBI().GetWaiter(1);
default:
throw new ArgumentException();
}
}
public void CloseWindow()
{
this.Close();
}
#region Dynamic Buttons
private void btnPrevious_Click(object sender, EventArgs e)
{
page--;
var list = new ProductTypeBI().GetProductTypes();
if (page == -1)
page = 0;
ControlFactory.GenerateButtons(ref pnlBilling, ref buttonHeads, new Rectangle(889, 90, 85, 498), 1, pageSize, 2, page, list, new ButtonClickDelegate(productTypeButton_Click));
}
private void btnNext_Click(object sender, EventArgs e)
{
page++;
var list = new ProductTypeBI().GetProductTypes();
int pages = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(list.Count) / Convert.ToDouble(pageSize)));
if (page == pages)
page--;
ControlFactory.GenerateButtons(ref pnlBilling, ref buttonHeads, new Rectangle(889, 90, 85, 498), 1, pageSize, 2, page, list, new ButtonClickDelegate(productTypeButton_Click));
}
#endregion
}
}