Lot of changes. Now table must be explicitly updated and not from inside various functions. This makes for better understanding and lesser mistakes.

Product form now has an IsActive checkbox to show only active products.
This commit is contained in:
tanshu
2014-11-20 13:42:20 +05:30
parent e3f92da5ad
commit b1a9d2daae
9 changed files with 262 additions and 304 deletions

View File

@ -14,26 +14,24 @@ namespace Tanshu.Accounts.PointOfSale
public class BillController
{
private readonly BillDict _bill;
private Voucher _billInfo;
private Voucher _voucher;
private Guid? _editVoucherID;
private readonly bool _print;
private ISaleForm _saleForm;
public Waiter Waiter
{
get { return _billInfo.Waiter; }
set { _billInfo.Waiter = value; }
get { return _voucher.Waiter; }
set { _voucher.Waiter = value; }
}
public BillController(Guid? editVoucherID, bool print)
public BillController(Guid? editVoucherID)
{
this._editVoucherID = editVoucherID;
_print = print;
_bill = new BillDict();
_billInfo = new Voucher(Session.User);
_voucher = new Voucher(Session.User);
using (var bi = new CustomerBI())
_billInfo.Customer = bi.Get(x => x.CustomerID == Constants.CASH_CUSTOMER);
_voucher.Customer = bi.Get(x => x.CustomerID == Constants.CASH_CUSTOMER);
using (var bi = new WaiterBI())
_billInfo.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
_voucher.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
}
public BillItemValue CurrentProduct
{
@ -59,7 +57,7 @@ namespace Tanshu.Accounts.PointOfSale
public void InitGui(ISaleForm saleForm)
{
this._saleForm = saleForm;
this._saleForm.SetCustomerDisplay(_billInfo.Customer.Name);
this._saleForm.SetCustomerDisplay(_voucher.Customer.Name);
this._saleForm.SetUserName(Session.User.Name);
}
public void AddProduct(Product product)
@ -107,7 +105,7 @@ namespace Tanshu.Accounts.PointOfSale
public void SetDiscount()
{
if (!Session.IsAllowed("Discount"))
throw new PermissionException("Not Allowed to give Discount");
return; // throw new PermissionException("Not Allowed to give Discount");
using (var bi = new ProductGroupBI())
{
@ -119,7 +117,7 @@ namespace Tanshu.Accounts.PointOfSale
var discount = frm.Selection(out outList);
discount = discount / 100;
if (discount > 1 || discount < 0)
throw new ValidationException("Invalid Discount Amount");
return; // throw new ValidationException("Invalid Discount Amount");
foreach (var item in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && outList.Contains(x.Value.Product.ProductGroup.GroupType)))
{
@ -173,7 +171,7 @@ namespace Tanshu.Accounts.PointOfSale
}
public void ShowCustomers(bool reset)
{
if (!reset && ((_billInfo.Customer == null) || _billInfo.Customer.CustomerID == Constants.CASH_CUSTOMER))
if (!reset && ((_voucher.Customer == null) || _voucher.Customer.CustomerID == Constants.CASH_CUSTOMER))
{
using (var selectCustomer = new SelectCustomer(CustomerBI.StaticList, true))
{
@ -181,21 +179,21 @@ namespace Tanshu.Accounts.PointOfSale
selectCustomer.ShowDialog();
if (selectCustomer.SelectedItem != null)
{
_billInfo.Customer = selectCustomer.SelectedItem;
_voucher.Customer = selectCustomer.SelectedItem;
}
else
{
using (var bi = new CustomerBI())
_billInfo.Customer = bi.Get(x => x.CustomerID == Constants.CASH_CUSTOMER);
_voucher.Customer = bi.Get(x => x.CustomerID == Constants.CASH_CUSTOMER);
}
}
}
else
{
using (var bi = new CustomerBI())
_billInfo.Customer = bi.Get(x => x.CustomerID == Constants.CASH_CUSTOMER);
_voucher.Customer = bi.Get(x => x.CustomerID == Constants.CASH_CUSTOMER);
}
_saleForm.SetCustomerDisplay(_billInfo.Customer.Name);
_saleForm.SetCustomerDisplay(_voucher.Customer.Name);
}
private Customer selectCustomer_customerEvent(object sender, CustomerEventArgs e)
{
@ -210,7 +208,7 @@ namespace Tanshu.Accounts.PointOfSale
if (reset)
{
using (var bi = new WaiterBI())
_billInfo.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
_voucher.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
}
else
{
@ -220,16 +218,16 @@ namespace Tanshu.Accounts.PointOfSale
selectWaiter.ShowDialog();
if (selectWaiter.SelectedItem != null)
{
_billInfo.Waiter = selectWaiter.SelectedItem;
_voucher.Waiter = selectWaiter.SelectedItem;
}
else
{
using (var bi = new WaiterBI())
_billInfo.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
_voucher.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
}
}
}
_saleForm.SetWaiterDisplay(_billInfo.Waiter.Name);
_saleForm.SetWaiterDisplay(_voucher.Waiter.Name);
}
private Waiter selectWaiter_waiterEvent(object sender, SelectorEventArgs<Waiter> e)
{
@ -301,27 +299,6 @@ namespace Tanshu.Accounts.PointOfSale
return dbVoucher.Printed || dbVoucher.Void;
}
}
private bool? IsReprint(out decimal amount)
{
amount = 0;
if (_billInfo.VoucherID == Guid.Empty)
return false;
bool isPrinted, isVoid;
IsPrintedOrVoid(_billInfo, out isPrinted, out isVoid);
if (isVoid)
{
MessageBox.Show(string.Format("This Bill is already void.\nReason: {0}", _billInfo.VoidReason), "Bill already Voided", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
if (isPrinted && (!Session.IsAllowed("Edit Printed Bill")))
return null;
amount = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty).Sum(x => x.Value.GrossAmount);
return isPrinted;
}
private void InputBox_Validating(object sender, InputBoxValidatingArgs e)
{
}
@ -330,10 +307,10 @@ namespace Tanshu.Accounts.PointOfSale
ClearBill();
using (var bi = new VoucherBI())
{
_billInfo = bi.Get(x => x.VoucherID == voucherID);
_voucher = bi.Get(x => x.VoucherID == voucherID);
_bill.Clear();
_saleForm.ShowInfo(_billInfo);
_bill.Load(_billInfo);
_saleForm.ShowInfo(_voucher);
_bill.Load(_voucher);
var newKotKey = new BillItemKey(Guid.Empty);
var newKotItem = new BillItemValue();
_bill.Add(newKotKey, newKotItem);
@ -355,9 +332,9 @@ namespace Tanshu.Accounts.PointOfSale
using (var frm = new PaxForm())
{
frm.ShowDialog();
_billInfo.Table = table;
_billInfo.Pax = frm.Pax;
_saleForm.ShowInfo(_billInfo);
_voucher.Table = table;
_voucher.Pax = frm.Pax;
_saleForm.ShowInfo(_voucher);
}
}
}
@ -372,7 +349,7 @@ namespace Tanshu.Accounts.PointOfSale
}
public void ClearBill()
{
_billInfo = new Voucher(Session.User);
_voucher = new Voucher(Session.User);
ShowCustomers(true);
ShowWaiters(true);
_bill.Clear();
@ -386,31 +363,40 @@ namespace Tanshu.Accounts.PointOfSale
ClearBill();
if (_editVoucherID.HasValue)
{
LoadBill(_editVoucherID.Value);
FoodTable ft = new FoodTableBI().Get(x => x.VoucherID == _editVoucherID.Value);
if (ft == null)
LoadBill(_editVoucherID.Value);
else
{
_editVoucherID = null;
LoadBill(ft.Name);
}
return SaleFormState.Billing;
}
return SaleFormState.Waiting;
}
internal void SettleBill()
{
if (_billInfo.VoucherID == Guid.Empty)
if (_voucher.VoucherID == Guid.Empty)
return;
if (!_billInfo.Printed)
if (!_voucher.Printed)
return;
if (!Session.IsAllowed("Settle Bill"))
return;
IDictionary<SettleOption, decimal> options;
var amount = -1 * _billInfo.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
using (var frm = new SettleChoicesForm(Math.Round(amount) * -1, _billInfo.VoucherType))
var amount = -1 * _voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
using (var frm = new SettleChoicesForm(Math.Round(amount) * -1, _voucher.VoucherType))
{
frm.ShowDialog();
options = frm.OptionsChosen;
}
if (options.Count == 0)
return;
using (var bi = new VoucherSettlementBI())
using (var bi = new VoucherBI())
{
bi.SettleVoucher(Session.User, _billInfo.VoucherID, options);
bi.SettleVoucher(Session.User, _voucher.VoucherID, options);
if (!_editVoucherID.HasValue)
bi.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && x.VoucherID == _voucher.VoucherID, null, null);
bi.SaveChanges();
}
ClearBill();
@ -432,9 +418,9 @@ namespace Tanshu.Accounts.PointOfSale
}
internal void MoveKot()
{
if (_billInfo.VoucherID == Guid.Empty)
if (_voucher.VoucherID == Guid.Empty)
return;
if (IsPrintedOrVoid(_billInfo))
if (IsPrintedOrVoid(_voucher))
return;
var kot = CurrentKot;
if (kot == null)
@ -442,7 +428,7 @@ namespace Tanshu.Accounts.PointOfSale
var table = GetTableForMove(true);
if (table == null)
return;
if (table.FoodTableID == _billInfo.Table.FoodTableID)
if (table.FoodTableID == _voucher.Table.FoodTableID)
return;
if (table.VoucherID != null)
if (IsPrintedOrVoid(table.VoucherID.Value))
@ -466,15 +452,15 @@ namespace Tanshu.Accounts.PointOfSale
}
internal void MoveTable()
{
if (_billInfo.VoucherID == Guid.Empty)
if (_voucher.VoucherID == Guid.Empty)
return;
var allowMerge = !IsPrintedOrVoid(_billInfo);
var allowMerge = !IsPrintedOrVoid(_voucher);
var table = GetTableForMove(allowMerge);
if (table == null)
return;
if (table.FoodTableID == _billInfo.Table.FoodTableID)
if (table.FoodTableID == _voucher.Table.FoodTableID)
return;
if (table.VoucherID.HasValue)
if (IsPrintedOrVoid(table.VoucherID.Value))
@ -489,6 +475,7 @@ namespace Tanshu.Accounts.PointOfSale
using (var bi = new VoucherBI())
{
var newVoucherID = bi.MoveKot(kot.KotID, table.FoodTableID);
bi.UpdateTable(x => x.FoodTableID == table.FoodTableID && x.VoucherID == null, newVoucherID, "running");
bi.SaveChanges();
return newVoucherID;
}
@ -510,9 +497,11 @@ namespace Tanshu.Accounts.PointOfSale
return Guid.Empty;
using (var bi = new VoucherBI())
{
var newVoucherID = bi.Move(_billInfo.Table.FoodTableID, tableID);
bi.MoveTable(_voucher.VoucherID, tableID);
bi.UpdateTable(x => x.FoodTableID == tableID && x.VoucherID == null, _voucher.VoucherID, _voucher.Table.Status);
bi.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && x.VoucherID == _voucher.VoucherID, null, null);
bi.SaveChanges();
return newVoucherID;
return _voucher.VoucherID;
}
}
private Guid MergeTable(FoodTable table)
@ -521,7 +510,8 @@ namespace Tanshu.Accounts.PointOfSale
return Guid.Empty;
using (var bi = new VoucherBI())
{
var newVoucherID = bi.MergeTables(_billInfo.VoucherID, table.Name);
var newVoucherID = bi.MergeTables(_voucher.VoucherID, table.Name);
bi.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && x.VoucherID == _voucher.VoucherID, null, null);
bi.SaveChanges();
return newVoucherID;
}
@ -534,18 +524,18 @@ namespace Tanshu.Accounts.PointOfSale
#region Check if Allowed
if (!Session.IsAllowed("Print Kot"))
return;
if (_billInfo.VoucherID != Guid.Empty && IsPrintedOrVoid(_billInfo))
if (_voucher.VoucherID != Guid.Empty && IsPrintedOrVoid(_voucher))
return;
if (_bill.Count == 1) //new kot only
return;
#endregion
//Save
var saved = _billInfo.VoucherID == Guid.Empty ? InsertVoucher(false, !_editVoucherID.HasValue) : UpdateVoucher(false, !_editVoucherID.HasValue);
var saved = _voucher.VoucherID == Guid.Empty ? InsertVoucher(false, !_editVoucherID.HasValue) : UpdateVoucher(false, !_editVoucherID.HasValue);
//Print
if ((!_editVoucherID.HasValue || _print) && saved.HasValue)
Thermal.PrintKot(_billInfo.VoucherID, saved.Value);
if (!_editVoucherID.HasValue && saved.HasValue)
Thermal.PrintKot(_voucher.VoucherID, saved.Value);
//Cleanup
if (_editVoucherID.HasValue)
@ -557,54 +547,40 @@ namespace Tanshu.Accounts.PointOfSale
{
#region Check if Allowed
if (!Session.IsAllowed("Print Bill"))
throw new PermissionException("Printing not allowed");
return; // throw new PermissionException("Printing not allowed");
if (_bill.Count == 1) //new kot only
return;
bool isPrinted = false, isVoid = false;
if (_voucher.VoucherID != Guid.Empty)
IsPrintedOrVoid(_voucher, out isPrinted, out isVoid);
if (isVoid)
{
MessageBox.Show(string.Format("This Bill is already void.\nReason: {0}", _voucher.VoidReason), "Bill already Voided", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (isPrinted && !Session.IsAllowed("Edit Printed Bill"))
return;
#endregion
decimal amount;
var isReprint = IsReprint(out amount);
if (!isReprint.HasValue)
return;
else if (isReprint.Value)
var amount = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty).Sum(x => x.Value.GrossAmount);
SetDiscount();
if (isPrinted)
{
try
{
SetDiscount();
}
catch (Exception ex)
{
if (!(ex is ValidationException) && !(ex is PermissionException))
throw;
}
SaveReprintOrDiscountBill(amount);
}
else
{
try
{
SetDiscount();
}
catch (Exception ex)
{
if (!(ex is ValidationException) && !(ex is PermissionException))
throw;
}
// Ask for VoucherType only for new bill, if none, then cancel
VoucherType? voucherType;
using (var frm = new VoucherTypeForm())
{
frm.ShowDialog();
voucherType = frm.Selection;
if (!frm.Selection.HasValue)
return;
_voucher.VoucherType = frm.Selection.Value;
}
if (!voucherType.HasValue)
return;
_billInfo.VoucherType = voucherType.Value;
var saved = _billInfo.VoucherID == Guid.Empty ? InsertVoucher(true, !_editVoucherID.HasValue) : UpdateVoucher(true, !_editVoucherID.HasValue);
var saved = _voucher.VoucherID == Guid.Empty ? InsertVoucher(true, !_editVoucherID.HasValue) : UpdateVoucher(true, !_editVoucherID.HasValue);
}
if (!_editVoucherID.HasValue || _print)
Thermal.PrintBill(_billInfo.VoucherID);
Thermal.PrintBill(_voucher.VoucherID);
if (_editVoucherID.HasValue)
_saleForm.CloseWindow();
ClearBill();
@ -614,8 +590,8 @@ namespace Tanshu.Accounts.PointOfSale
#region Permissions
bool isPrinted, isVoid;
IsPrintedOrVoid(_billInfo, out isPrinted, out isVoid);
if (_billInfo.VoucherID == Guid.Empty || isVoid)
IsPrintedOrVoid(_voucher, out isPrinted, out isVoid);
if (_voucher.VoucherID == Guid.Empty || isVoid)
return; // must be existing non void bill
if (!Session.IsAllowed("Split Bill"))
@ -647,13 +623,13 @@ namespace Tanshu.Accounts.PointOfSale
#region new voucherFirst
var voucherFirst = new Voucher(Session.User)
{
Customer = _billInfo.Customer,
Customer = _voucher.Customer,
Table = table,
Waiter = _billInfo.Waiter,
Waiter = _voucher.Waiter,
Printed = isPrinted,
Void = false,
Narration = "",
VoucherType = _billInfo.VoucherType
VoucherType = _voucher.VoucherType
};
var kotFirst = GetKot(listFirst);
@ -664,13 +640,13 @@ namespace Tanshu.Accounts.PointOfSale
#region new voucherSecond
var voucherSecond = new Voucher(Session.User)
{
Customer = _billInfo.Customer,
Table = _billInfo.Table,
Waiter = _billInfo.Waiter,
Customer = _voucher.Customer,
Table = _voucher.Table,
Waiter = _voucher.Waiter,
Printed = isPrinted,
Void = false,
Narration = "",
VoucherType = _billInfo.VoucherType
VoucherType = _voucher.VoucherType
};
var kotSecond = GetKot(listSecond);
@ -679,7 +655,10 @@ namespace Tanshu.Accounts.PointOfSale
#endregion
using (var bi = new VoucherBI())
{
bi.SplitBill(_billInfo.VoucherID, voucherFirst, voucherSecond);
bi.SplitBill(_voucher.VoucherID, voucherFirst, voucherSecond);
var status = _voucher.Printed ? "printed" : "running";
bi.UpdateTable(x => x.FoodTableID == voucherFirst.Table.FoodTableID && x.VoucherID == null, voucherFirst.VoucherID, status);
bi.UpdateTable(x => x.FoodTableID == voucherSecond.Table.FoodTableID && x.VoucherID == _voucher.VoucherID, voucherSecond.VoucherID, status);
bi.SaveChanges();
}
if (isPrinted)
@ -692,11 +671,9 @@ namespace Tanshu.Accounts.PointOfSale
public void VoidBill()
{
#region Check conditions and Permissions
if (_billInfo.VoucherID == Guid.Empty)
if (_voucher.VoucherID == Guid.Empty)
return;
//if (!_billInfo.Printed)
// return;
if (_billInfo.Void)
if (_voucher.Void)
return;
if (!Session.IsAllowed("Void Bill"))
return;
@ -710,7 +687,9 @@ namespace Tanshu.Accounts.PointOfSale
{
using (var bi = new VoucherBI())
{
bi.VoidBill(_billInfo.VoucherID, voidReason.SelectedItem.Description, !_editVoucherID.HasValue);
bi.VoidBill(_voucher.VoucherID, voidReason.SelectedItem.Description);
if (!_editVoucherID.HasValue)
bi.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && x.VoucherID == _voucher.VoucherID, null, null);
bi.SaveChanges();
}
ClearBill();
@ -744,13 +723,13 @@ namespace Tanshu.Accounts.PointOfSale
#region new voucherFirst
var newVoucher = new Voucher(Session.User)
{
Customer = _billInfo.Customer,
Table = _billInfo.Table,
Waiter = _billInfo.Waiter,
Customer = _voucher.Customer,
Table = _voucher.Table,
Waiter = _voucher.Waiter,
Printed = true,
Void = false,
Narration = "",
VoucherType = _billInfo.VoucherType
VoucherType = _voucher.VoucherType
};
var kotNew = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product));
@ -760,32 +739,39 @@ namespace Tanshu.Accounts.PointOfSale
using (var bi = new VoucherBI())
{
bi.DiscountPrintedBill(_billInfo.VoucherID, newVoucher);
bi.DiscountPrintedBill(_voucher.VoucherID, newVoucher);
bi.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && (x.VoucherID == _voucher.VoucherID || x.VoucherID == null), newVoucher.VoucherID, "printed");
bi.SaveChanges();
}
_editVoucherID = null;
LoadBill(newVoucher.VoucherID);
}
else
{
using (var bi = new ReprintBI())
{
bi.Insert(new Reprint() { User = Session.User, Voucher = _billInfo });
bi.Insert(new Reprint() { User = Session.User, Voucher = _voucher });
bi.SaveChanges();
}
}
}
private Guid? InsertVoucher(bool finalBill, bool updateTable)
{
_billInfo.Printed = finalBill;
_billInfo.Void = false;
_billInfo.Narration = "";
_voucher.Printed = finalBill;
_voucher.Void = false;
_voucher.Narration = "";
var kot = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty && x.Value.Quantity != 0));
if (kot == null)
return null;
_billInfo.Kots.Add(kot);
_voucher.Kots.Add(kot);
using (var bi = new VoucherBI())
{
var kotID = bi.Insert(_billInfo, updateTable);
var kotID = bi.Insert(_voucher);
if (updateTable)
{
var status = !_voucher.Printed ? "running" : "printed";
bi.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && x.VoucherID == null, _voucher.VoucherID, status);
}
bi.SaveChanges();
return kotID;
}
@ -794,25 +780,31 @@ namespace Tanshu.Accounts.PointOfSale
{
using (var bi = new VoucherBI())
{
var voucher = bi.Get(x => x.VoucherID == _billInfo.VoucherID);
var voucher = bi.Get(x => x.VoucherID == _voucher.VoucherID);
voucher.User = Session.User;
voucher.Customer = _billInfo.Customer;
voucher.Waiter = _billInfo.Waiter;
voucher.Customer = _voucher.Customer;
voucher.Waiter = _voucher.Waiter;
voucher.Printed = finalBill;
voucher.VoucherType = _billInfo.VoucherType;
voucher.VoucherType = _voucher.VoucherType;
foreach (var item in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty))
{
var i = voucher.Kots.Single(x => x.KotID == item.Key.KotID).Inventories.Single(x => x.Product.ProductID == item.Key.ProductID);
i.Discount = item.Value.Discount;
i.Price = item.Value.Price;
}
if (!_billInfo.Printed)
if (!_voucher.Printed)
{
var kot = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty && x.Value.Quantity != 0));
if (kot != null)
voucher.Kots.Add(kot);
}
var kotID = bi.Update(voucher, updateTable);
var kotID = bi.Update(voucher);
if (updateTable)
{
var status = !voucher.Printed ? "running" : "printed";
bi.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && x.VoucherID == _voucher.VoucherID, _voucher.VoucherID, status);
}
bi.SaveChanges();
return kotID;
}