narsil/Tanshu.Accounts.PointOfSale/Sales/SalesForm.cs
2016-03-31 12:27:39 +05:30

453 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Helpers;
using Tanshu.Accounts.Repository;
namespace Tanshu.Accounts.PointOfSale.Sales
{
public partial class SalesForm : Form, ISaleForm
{
private ProductGroup _currentProductGroup;
public SalesForm(BillController billController)
{
InitializeComponent();
_billController = billController;
billController.InitGui(this);
}
#region ISaleForm Members
public void SetUserName(string name)
{
Text = name;
}
private void ShowInfo(Voucher voucher, BillDict bill)
{
if (voucher.VoucherID == Guid.Empty)
{
txtBillID.Text = "";
txtKotID.Text = "";
txtCreationDate.Text = "";
txtDate.Text = "";
txtLastEditDate.Text = "";
}
else
{
txtBillID.Text = voucher.FullBillID;
txtKotID.Text = "K-" + voucher.KotID.ToString();
txtCreationDate.Text = voucher.CreationDate.ToString("HH:mm dd-MMM-yyyy");
txtDate.Text = voucher.Date.ToString("HH:mm dd-MMM-yyyy");
txtLastEditDate.Text = voucher.LastEditDate.ToString("HH:mm dd-MMM-yyyy");
}
btnCustomer.Text = voucher.Customer.Name;
txtTableID.Text = voucher.Table == null ? "" : voucher.Table.Name;
txtPax.Text = voucher.Pax.ToString();
bindingSource.CurrencyManager.Position = Math.Min(bindingSource.CurrencyManager.Position, bill.Count - 1);
txtGrossTax.Text = string.Format("{0:#0.00}", bill.Tax);
txtDiscount.Text = string.Format("{0:#0.00}", bill.Discount);
txtServiceCharge.Text = string.Format("{0:#0.00}", bill.ServiceCharge);
txtGrossAmount.Text = string.Format("{0:#0.00}", bill.NetAmount);
txtAmount.Text = string.Format("{0:#0.00}", bill.Amount);
bindingSource.DataSource = bill.ToList();
dgvProducts.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
MoreButton(false);
}
public BillItemValue CurrentProduct
{
get
{
if (bindingSource.Position == -1)
return null;
var item = _billController._bill.ElementAt(bindingSource.Position);
return item.Key.BillItemType != BillItemType.Kot ? item.Value : null;
}
}
public BillItemKey CurrentKey
{
get
{
if (bindingSource.Position == -1)
return null;
var item = _billController._bill.ElementAt(bindingSource.Position);
return item.Key;
}
}
public BillItemKey CurrentKot
{
get
{
if (bindingSource.Position == -1)
return null;
var item = _billController._bill.ElementAt(bindingSource.Position);
return item.Key.BillItemType == BillItemType.Kot ? item.Key : null;
}
}
public void CloseWindow()
{
Close();
}
#endregion
private void SalesForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.F2:
{
btnQuantity_Click(sender, new EventArgs());
break;
}
case Keys.F3:
{
btnDiscount_Click(sender, new EventArgs());
break;
}
case Keys.F4:
{
if (!e.Alt)
{
_billController.ShowCustomers();
ShowInfo(_billController._voucher, _billController._bill);
}
break;
}
case Keys.F8:
{
_billController.LoadBill(null);
ShowInfo(_billController._voucher, _billController._bill);
FormState = SaleFormState.Billing;
break;
}
case Keys.F11:
{
btnPrintBill_Click(sender, e);
break;
}
case Keys.F12:
{
btnPrintKot_Click(sender, e);
break;
}
case Keys.Delete:
{
_billController.SetQuantity(CurrentKey, CurrentProduct, -1, false);
ShowInfo(_billController._voucher, _billController._bill);
break;
}
case Keys.Add:
{
_billController.SetQuantity(CurrentKey, CurrentProduct, 1, false);
ShowInfo(_billController._voucher, _billController._bill);
break;
}
case Keys.Subtract:
{
_billController.SetQuantity(CurrentKey, CurrentProduct, -1, false);
ShowInfo(_billController._voucher, _billController._bill);
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:
{
var canceled = _billController.CancelBillChanges();
ShowInfo(_billController._voucher, _billController._bill);
if (canceled)
FormState = SaleFormState.Waiting;
break;
}
}
}
private void SalesForm_Load(object sender, EventArgs e)
{
using (var bis = new SettingBI())
{
var showSC = bis.Get(x => x.Name == "Service Charge Enabled").Details == "yes";
txtServiceCharge.Visible = showSC;
lblServiceCharge.Visible = showSC;
}
_billController.ClearBill();
var state = _billController.FormLoad();
ShowInfo(_billController._voucher, _billController._bill);
FormState = state;
}
private void btnCustomer_Click(object sender, EventArgs e)
{
_billController.ShowCustomers();
ShowInfo(_billController._voucher, _billController._bill);
}
private void btnVoid_Click(object sender, EventArgs e)
{
var voided = _billController.VoidBill();
ShowInfo(_billController._voucher, _billController._bill);
if (voided)
FormState = SaleFormState.Waiting;
}
private void btnPrice_Click(object sender, EventArgs e)
{
var item = CurrentProduct;
if (item == null)
return;
_billController.SetPrice(CurrentProduct);
ShowInfo(_billController._voucher, _billController._bill);
}
private void btnClear_Click(object sender, EventArgs e)
{
var canceled = _billController.CancelBillChanges();
ShowInfo(_billController._voucher, _billController._bill);
if (canceled)
FormState = SaleFormState.Waiting;
}
private void dgvProducts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var dgv = sender as DataGridView;
var data = (KeyValuePair<BillItemKey, BillItemValue>)dgv.Rows[e.RowIndex].DataBoundItem;
if (data.Key.BillItemType == BillItemType.Kot)
{
e.CellStyle.SelectionBackColor = Color.Blue;
e.CellStyle.BackColor = Color.LightBlue;
}
else if (data.Value.inventory.Kot != null)
{
e.CellStyle.SelectionBackColor = Color.HotPink;
e.CellStyle.BackColor = Color.LightPink;
}
else
{
e.CellStyle.SelectionBackColor = Color.Green;
e.CellStyle.BackColor = Color.LightGreen;
}
if (dgv.Columns[e.ColumnIndex].Name.Equals("DisplayColumn"))
e.Value = data.Value.Display;
else if (dgv.Columns[e.ColumnIndex].Name.Equals("QuantityColumn"))
{
if (data.Key.BillItemType != BillItemType.Kot)
e.Value = data.Value.inventory.Quantity;
else
e.Value = "";
}
}
private void btnSettle_Click(object sender, EventArgs e)
{
var settled = _billController.SettleBill();
ShowInfo(_billController._voucher, _billController._bill);
if (settled)
FormState = SaleFormState.Waiting;
}
private void btnModifier_Click(object sender, EventArgs e)
{
var item = CurrentProduct;
if (item == null || CurrentKey.KotID != Guid.Empty)
return; // No Product or Old Product
_billController.ShowModifiers(item);
ShowInfo(_billController._voucher, _billController._bill);
}
private void btnDelete_Click(object sender, EventArgs e)
{
_billController.SetQuantity(CurrentKey, CurrentProduct, -1, false);
ShowInfo(_billController._voucher, _billController._bill);
}
private void btnMoveTable_Click(object sender, EventArgs e)
{
_billController.MoveTable();
ShowInfo(_billController._voucher, _billController._bill);
}
private void btnMore_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
if (button.Text == "More")
MoreButton(true);
else if (button.Text == "Less")
MoreButton(false);
else
throw new InvalidOperationException("Button State incorrect");
}
private void MoreButton(bool more)
{
btnMore.Text = more ? "Less" : "More";
btnQuantity.Visible = !more;
btnPrice.Visible = more;
btnDelete.Visible = !more;
btnDiscount.Visible = !more;
btnModifier.Visible = !more;
btnMoveTable.Visible = more;
btnMoveKot.Visible = more;
btnVoid.Visible = more;
btnSplitBill.Visible = more;
}
private void btnMoveKot_Click(object sender, EventArgs e)
{
_billController.MoveKot(CurrentKot);
ShowInfo(_billController._voucher, _billController._bill);
}
#region Helper Functions
private SaleFormState FormState
{
set
{
flpGroup.Controls.Clear();
flpMain.Controls.Clear();
if (value == SaleFormState.Billing)
ControlFactory.GenerateGroups(ref flpGroup, new Point(75, 75), 0, Cache.ProductGroups(), productTypeButton_Click, productTypePage_Click);
else
using (var bi = new FoodTableBI())
ControlFactory.GenerateTables(ref flpMain, new Point(75, 75), 0, bi.List(x => x.IsActive), tableButton_Click, tablePage_Click);
}
}
private void productTypeButton_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
var item = button.Tag as ProductGroup;
if (item == null)
return;
_currentProductGroup = item;
ControlFactory.GenerateProducts(ref flpMain, new Point(75, 75), 0,
item.Products, productButton_Click, productPage_Click);
}
private void productTypePage_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
var start = (int)button.Tag;
if (start < 0)
start = 0;
ControlFactory.GenerateGroups(ref flpGroup, new Point(75, 75), start, Cache.ProductGroups(), productTypeButton_Click, productTypePage_Click);
}
private void productButton_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
var item = button.Tag as Product;
if (item.IsNotAvailable)
return;
_billController.AddProduct(item);
bindingSource.DataSource = _billController._bill.ToList();
bindingSource.CurrencyManager.Position = _billController._bill.IndexOfKey(new BillItemKey(item.ProductID, Guid.Empty));
var showModifier = false;
using (var bi = new ProductGroupModifierBI())
showModifier = bi.HasCompulsoryModifier(CurrentProduct.inventory.Product.ProductGroup.ProductGroupID);
if (showModifier)
_billController.ShowModifiers(CurrentProduct);
ShowInfo(_billController._voucher, _billController._bill);
}
private void productPage_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
var item = button.Tag as Product;
var start = (int)button.Tag;
if (start < 0)
start = 0;
ControlFactory.GenerateProducts(ref flpMain, new Point(75, 75), start, _currentProductGroup.Products, productButton_Click, productPage_Click);
}
private void tableButton_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
var item = button.Tag as FoodTable;
var tableName = item.Name;
_billController.LoadBill(tableName);
ShowInfo(_billController._voucher, _billController._bill);
FormState = SaleFormState.Billing;
}
private void tablePage_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null)
return;
var start = (int)button.Tag;
if (start < 0)
start = 0;
using (var bi = new FoodTableBI())
ControlFactory.GenerateTables(ref flpMain, new Point(75, 75), start, bi.List(x => x.IsActive), tableButton_Click, tablePage_Click);
}
private void btnPrintBill_Click(object sender, EventArgs e)
{
var printed = _billController.SaveAndPrintBill();
ShowInfo(_billController._voucher, _billController._bill);
if (printed)
FormState = SaleFormState.Waiting;
}
private void btnPrintKot_Click(object sender, EventArgs e)
{
var printed = _billController.SaveAndPrintKot();
ShowInfo(_billController._voucher, _billController._bill);
if (printed)
FormState = SaleFormState.Waiting;
}
private void btnQuantity_Click(object sender, EventArgs e)
{
_billController.SetQuantity(CurrentKey, CurrentProduct, 0, true);
ShowInfo(_billController._voucher, _billController._bill);
}
private void btnDiscount_Click(object sender, EventArgs e)
{
_billController.SetDiscount();
ShowInfo(_billController._voucher, _billController._bill);
}
#endregion
private void btnSplitBill_Click(object sender, EventArgs e)
{
_billController.SplitBill();
ShowInfo(_billController._voucher, _billController._bill);
}
}
}