narsil/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs
tanshu 69617949bd Important! : Need to update to new schema using SQL Scripts
Important! : This version will not work.  It is pre-alpha and saved in case of catastrophic failure
Refactor: Remove dependency on Fluent Nhibernate.
Refactor: All Primary keys are now Guids.
Refactor: Class Mappings changed from AutoMap to Explicit Mappings.
Breakage: All Cascading is now disabled and entities must be explicitly saved/updated/deleted
Breakage: Auto Commiting is now off and "SaveChanges()" needs to be called on all BIs.
Refactor: Changed the pattern where all relevant db code for an operation is basically in the same function.
Chore: Removed Advance and Payments options.
2014-10-12 15:11:45 +05:30

883 lines
34 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Helpers;
using Tanshu.Accounts.Print;
using Tanshu.Accounts.Repository;
using Tanshu.Common;
using System;
namespace Tanshu.Accounts.PointOfSale
{
public class BillController
{
private readonly BillDict _bill;
private Voucher _billInfo;
private Guid? _editVoucherID;
private readonly bool _print;
private ISaleForm _saleForm;
public Waiter Waiter
{
get { return _billInfo.Waiter; }
set { _billInfo.Waiter = value; }
}
public BillController(Guid? editVoucherID, bool print)
{
this._editVoucherID = editVoucherID;
_print = print;
_bill = new BillDict();
_billInfo = new Voucher(Session.User);
using (var bi = new CustomerBI())
_billInfo.Customer = bi.Get(x => x.CustomerID == Constants.CASH_CUSTOMER);
using (var bi = new WaiterBI())
_billInfo.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
}
public BillItemValue CurrentProduct
{
get
{
if (_saleForm.BindingSource.Position == -1)
return null;
var item = _bill.ElementAt(_saleForm.BindingSource.Position);
return item.Key.BillItemType == BillItemType.Product ? item.Value : null;
}
}
public BillItemKey CurrentKot
{
get
{
if (_saleForm.BindingSource.Position == -1)
return null;
var item = _bill.ElementAt(_saleForm.BindingSource.Position);
return item.Key.BillItemType == BillItemType.Kot ? item.Key : null;
}
}
public void InitGui(ISaleForm saleForm)
{
this._saleForm = saleForm;
this._saleForm.SetCustomerDisplay(_billInfo.Customer.Name);
this._saleForm.SetUserName(Session.User.Name);
}
public void AddProduct(Product product)
{
var newKey = new BillItemKey(product.ProductID, Guid.Empty);
if (_bill.ContainsKey(newKey))
{
_saleForm.BindingSource.CurrencyManager.Position = _bill.IndexOfKey(newKey);
_bill[newKey].Quantity += 1;
}
else
{
var billItemValue = new BillItemValue(product);
var old = _bill.FirstOrDefault(x => x.Key.BillItemType == BillItemType.Product && x.Key.ProductID == newKey.ProductID);
if (old.Key != null)
{
billItemValue.Discount = old.Value.Discount;
billItemValue.Price = old.Value.Price;
}
_bill.Add(newKey, billItemValue);
_saleForm.BindingSource.DataSource = _bill.Values.ToList();
_saleForm.BindingSource.CurrencyManager.Position = _saleForm.BindingSource.CurrencyManager.Count - 1;
using (var bi = new ProductGroupModifierBI())
if (bi.HasCompulsoryModifier(product.ProductGroup.ProductGroupID))
{
var item = CurrentProduct;
ShowModifiers(product.ProductGroup.ProductGroupID, item);
}
}
ShowAmount();
}
public void ShowModifiers(Guid productGroupID, BillItemValue item)
{
if (item.Printed)
return;
using (var bi = new ProductGroupModifierBI())
{
using (var frm = new ModifierForm(bi.List(productGroupID), item.Modifiers))
{
frm.ShowDialog();
item.Modifiers = frm.Selection;
}
}
ShowAmount();
}
public void SetDiscount()
{
if (!Session.IsAllowed(RoleConstants.DISCOUNT))
throw new PermissionException("Not Allowed to give Discount");
using (var bi = new ProductGroupBI())
{
using (var frm = new DiscountForm(bi.GetProductGroupTypes()))
{
if (frm.ShowDialog() == DialogResult.OK)
{
HashSet<string> outList;
var discount = frm.Selection(out outList);
discount = discount / 100;
if (discount > 1 || discount < 0)
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)))
{
var product = item.Value.Product;
var maxDiscount = product.ProductGroup.DiscountLimit;
if (discount > item.Value.Product.ProductGroup.DiscountLimit)
MessageBox.Show(string.Format("Maximum discount for {0} is {1:P}", product.Name, maxDiscount),
"Excessive Discount", MessageBoxButtons.OK, MessageBoxIcon.Warning);
item.Value.Discount = discount;
}
}
}
}
ShowAmount();
}
public void SetQuantity(decimal quantity, bool prompt)
{
var item = CurrentProduct;
if (!Allowed(item))
return;
if (item.Printed)
return;
if (prompt && !GetInput("Quantity", ref quantity))
return;
if (!prompt)
quantity += item.Quantity;
if (quantity < 0 && !Session.IsAllowed(RoleConstants.EDIT_PRINTED_PRODUCT))
return;
var total = quantity + _bill.Where(x => x.Key.ProductID == item.ProductID && x.Key.BillItemType == BillItemType.Product && x.Value.Printed).Sum(x => x.Value.Quantity);
if (total < 0)
quantity -= total;
item.Quantity = quantity;
ShowAmount();
}
public void SetRate()
{
var item = CurrentProduct;
if (item == null)
throw new ValidationException("No Product Selected");
if (!Session.IsAllowed(RoleConstants.CHANGE_RATE))
throw new PermissionException("Rate Change not Allowed");
var rate = item.Price;
if (!GetInput("Price", ref rate))
return;
if (rate == 0 && !Session.IsAllowed(RoleConstants.ZERO_RATE))
throw new PermissionException("Zero Rate not Allowed");
foreach (var sub in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.ProductID == item.ProductID))
sub.Value.Price = rate;
ShowAmount();
}
public void ShowCustomers(bool reset)
{
if (!reset && ((_billInfo.Customer == null) || _billInfo.Customer.CustomerID == Constants.CASH_CUSTOMER))
{
using (var selectCustomer = new SelectCustomer(CustomerBI.StaticList, true))
{
selectCustomer.CustomerEvent += selectCustomer_customerEvent;
selectCustomer.ShowDialog();
if (selectCustomer.SelectedItem != null)
{
_billInfo.Customer = selectCustomer.SelectedItem;
}
else
{
using (var bi = new CustomerBI())
_billInfo.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);
}
_saleForm.SetCustomerDisplay(_billInfo.Customer.Name);
}
private Customer selectCustomer_customerEvent(object sender, CustomerEventArgs e)
{
using (var form = new CustomersForm(e.CustomerID, e.Phone))
{
form.ShowDialog();
return form.Customer;
}
}
public void ShowWaiters(bool reset)
{
if (reset)
{
using (var bi = new WaiterBI())
_billInfo.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
}
else
{
using (var selectWaiter = new SelectWaiter(WaiterBI.StaticList, true))
{
selectWaiter.WaiterEvent += selectWaiter_waiterEvent;
selectWaiter.ShowDialog();
if (selectWaiter.SelectedItem != null)
{
_billInfo.Waiter = selectWaiter.SelectedItem;
}
else
{
using (var bi = new WaiterBI())
_billInfo.Waiter = bi.Get(x => x.WaiterID == Constants.WAITER);
}
}
}
_saleForm.SetWaiterDisplay(_billInfo.Waiter.Name);
}
private Waiter selectWaiter_waiterEvent(object sender, SelectorEventArgs<Waiter> e)
{
var waiter = e.Item;
//if (!Thread.CurrentPrincipal.IsInRole("Waiter/Master"))
// return waiter;
using (var bi = new WaiterBI())
{
switch (e.Action)
{
case SelectorAction.Insert: // Add
bi.Insert(waiter);
bi.SaveChanges();
e.Handled = true;
return waiter;
case SelectorAction.Update: // Edit
bi.Update(waiter);
bi.SaveChanges();
e.Handled = true;
return waiter;
case SelectorAction.Delete: // Delete
bi.Delete(x => x.WaiterID == waiter.WaiterID);
bi.SaveChanges();
e.Handled = true;
return bi.Get(x => x.WaiterID == Constants.WAITER);
default:
e.Handled = true;
return bi.Get(x => x.WaiterID == Constants.WAITER);
}
}
}
private void ShowAmount()
{
var taxAmount = _bill.Values.Sum(b => b.ServiceTaxAmount + b.VatAmount);
var discountAmount = _bill.Values.Sum(b => b.DiscountAmount);
var grossAmount = _bill.Values.Sum(b => b.GrossAmount);
var valueAmount = _bill.Values.Sum(b => b.Value);
var serviceChargeAmount = _bill.Values.Sum(b => b.ServiceChargeAmount);
_saleForm.ShowAmount(discountAmount, grossAmount, serviceChargeAmount, taxAmount, valueAmount,
_bill.Values.ToList());
}
private static bool Allowed(BillItemValue item, RoleConstants role)
{
return item != null && Session.IsAllowed(role);
}
private static bool Allowed(BillItemValue item)
{
return item != null;
}
private static void IsPrintedOrVoid(Voucher voucher, out bool isPrinted, out bool isVoid)
{
using (var bi = new VoucherBI())
{
var dbVoucher = bi.Get(x => x.VoucherID == voucher.VoucherID);
isPrinted = dbVoucher.Printed;
isVoid = dbVoucher.Void;
}
}
private static bool IsPrintedOrVoid(Voucher voucher)
{
using (var bi = new VoucherBI())
{
var dbVoucher = bi.Get(x => x.VoucherID == voucher.VoucherID);
return dbVoucher.Printed || dbVoucher.Void;
}
}
private static bool IsPrintedOrVoid(Guid voucherID)
{
using (var bi = new VoucherBI())
{
var dbVoucher = bi.Get(x => x.VoucherID == voucherID);
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(RoleConstants.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)
{
}
private void LoadBill(Guid voucherID)
{
ClearBill();
using (var bi = new VoucherBI())
{
_billInfo = bi.Get(x => x.VoucherID == voucherID);
_bill.Clear();
_saleForm.ShowInfo(_billInfo);
_bill.Load(_billInfo);
var newKotKey = new BillItemKey(Guid.Empty);
var newKotItem = new BillItemValue();
_bill.Add(newKotKey, newKotItem);
ShowAmount();
_saleForm.FormState = SaleFormState.Billing;
}
}
public void LoadBill(string tableName)
{
FoodTable table;
using (var bi = new FoodTableBI())
table = bi.Get(x => x.Name == tableName);
if (table != null && table.VoucherID.HasValue)
{
LoadBill(table.VoucherID.Value);
}
else
{
using (var frm = new PaxForm())
{
frm.ShowDialog();
_billInfo.TableID = tableName;
_billInfo.Pax = frm.Pax;
_saleForm.ShowInfo(_billInfo);
}
}
}
public void CancelBillChanges()
{
if (_bill.Values.Any(i => i.Printed == false) &&
MessageBox.Show("Abandon Changes?", "Abandon Changes", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
return;
ClearBill();
}
public void ClearBill()
{
_billInfo = new Voucher(Session.User);
ShowCustomers(true);
ShowWaiters(true);
_bill.Clear();
var newKotKey = new BillItemKey(Guid.Empty);
var newKotItem = new BillItemValue();
_bill.Add(newKotKey, newKotItem);
_saleForm.ClearBill(_bill.Values.ToList());
}
public SaleFormState FormLoad()
{
ClearBill();
if (_editVoucherID.HasValue)
{
LoadBill(_editVoucherID.Value);
return SaleFormState.Billing;
}
return SaleFormState.Waiting;
}
internal void SettleBill()
{
if (_billInfo.VoucherID == Guid.Empty)
return;
if (!_billInfo.Printed)
return;
if (!Session.IsAllowed(RoleConstants.SETTLE_BILL))
return;
IDictionary<SettleOption, decimal> options;
var amount = -1 * _billInfo.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
VoucherSettlementBI.UpdateSettlements(_billInfo.Settlements, amount);
using (var frm = new SettleChoicesForm(Math.Round(amount) * -1, _billInfo.VoucherType))
{
frm.ShowDialog();
options = frm.OptionsChosen;
}
if (options.Count == 0)
return;
using (var bi = new VoucherSettlementBI())
{
bi.SettleVoucher(Session.User, _billInfo.VoucherID, options);
}
ClearBill();
}
#region Move Table(s) / Kot(s)
private static FoodTable GetTableForMove(bool allowMerge)
{
using (var bi = new FoodTableBI())
{
using (var frm = new MoveTableForm(bi.List(), allowMerge))
{
frm.ShowDialog();
if (frm.Selection != null)
return frm.Selection;
}
return null;
}
}
internal void MoveKot()
{
if (_billInfo.VoucherID == Guid.Empty)
return;
if (IsPrintedOrVoid(_billInfo))
return;
var kot = CurrentKot;
if (kot == null)
return;
var table = GetTableForMove(true);
if (table == null)
return;
if (table.Name == _billInfo.TableID)
return;
if (table.VoucherID != null)
if (IsPrintedOrVoid(table.VoucherID.Value))
return;
var kotCount = _bill.Keys.Count(x => x.BillItemType == BillItemType.Kot && x.KotID != Guid.Empty);
var voucherID = Guid.Empty;
if (table.VoucherID == null && kotCount > 1)
//Move Kot
voucherID = MoveKot(kot, table);
else if (table.VoucherID != null && kotCount > 1)
//Merge Kot
voucherID = MergeKot(kot, table);
else if (table.VoucherID == null && kotCount == 1)
//Move Table
voucherID = MoveTable(table.Name);
else if (table.VoucherID != null && kotCount == 1)
//Merge Table
voucherID = MergeTable(table);
if (voucherID != Guid.Empty)
LoadBill(voucherID);
}
internal void MoveTable()
{
if (_billInfo.VoucherID == null)
return;
var allowMerge = !IsPrintedOrVoid(_billInfo);
var table = GetTableForMove(allowMerge);
if (table == null)
return;
if (table.Name == _billInfo.TableID)
return;
if (table.VoucherID.HasValue)
if (IsPrintedOrVoid(table.VoucherID.Value))
return;
LoadBill(table.VoucherID.HasValue ? MergeTable(table) : MoveTable(table.Name));
}
private Guid MoveKot(BillItemKey kot, FoodTable table)
{
if (!Session.IsAllowed(RoleConstants.MOVE_KOT))
return Guid.Empty;
using (var bi = new VoucherBI())
{
var newVoucherID = bi.MoveKot(kot.KotID, table.Name);
bi.SaveChanges();
return newVoucherID;
}
}
private static Guid MergeKot(BillItemKey kot, FoodTable table)
{
if (!Session.IsAllowed(RoleConstants.MERGE_KOT))
return Guid.Empty;
using (var bi = new VoucherBI())
{
var newVoucherID = bi.MergeKot(kot.KotID, table.Name);
bi.SaveChanges();
return newVoucherID;
}
}
private Guid MoveTable(string tableName)
{
if (!Session.IsAllowed(RoleConstants.MOVE_TABLE))
return Guid.Empty;
using (var bi = new VoucherBI())
{
var newVoucherID = bi.Move(_billInfo.TableID, tableName);
bi.SaveChanges();
return newVoucherID;
}
}
private Guid MergeTable(FoodTable table)
{
if (!Session.IsAllowed(RoleConstants.MERGE_TABLE))
return Guid.Empty;
using (var bi = new VoucherBI())
{
var newVoucherID = bi.MergeTables(_billInfo.VoucherID, table.Name);
bi.SaveChanges();
return newVoucherID;
}
}
#endregion
#region Save
public void SaveKot()
{
#region Check if Allowed
if (!Session.IsAllowed(RoleConstants.PRINT_KOT))
return;
if (_billInfo.VoucherID != Guid.Empty && IsPrintedOrVoid(_billInfo))
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);
//Print
if ((!_editVoucherID.HasValue || _print) && saved.HasValue)
Thermal.PrintKot(_billInfo.VoucherID, saved.Value);
//Cleanup
if (_editVoucherID.HasValue)
_saleForm.CloseWindow();
else
ClearBill();
}
public void SaveBill()
{
#region Check if Allowed
if (!Session.IsAllowed(RoleConstants.PRINT_BILL))
throw new PermissionException("Printing not allowed");
if (_bill.Count == 1) //new kot only
return;
#endregion
decimal amount;
var isReprint = IsReprint(out amount);
if (!isReprint.HasValue)
return;
else if (isReprint.Value)
{
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 (!voucherType.HasValue)
return;
_billInfo.VoucherType = voucherType.Value;
var saved = _billInfo.VoucherID == Guid.Empty ? InsertVoucher(true, !_editVoucherID.HasValue) : UpdateVoucher(true, !_editVoucherID.HasValue);
}
if (!_editVoucherID.HasValue || _print)
Thermal.PrintBill(_billInfo.VoucherID);
if (_editVoucherID.HasValue)
_saleForm.CloseWindow();
ClearBill();
}
public void SplitBill()
{
#region Permissions
bool isPrinted, isVoid;
IsPrintedOrVoid(_billInfo, out isPrinted, out isVoid);
if (_billInfo.VoucherID == Guid.Empty || isVoid)
return; // must be existing non void bill
if (!Session.IsAllowed(RoleConstants.SPLIT_BILL))
return;
#endregion
#region Get Move List
HashSet<string> splitList = null;
using (var bi = new ProductGroupBI())
{
using (var frm = new DiscountForm(bi.GetProductGroupTypes()))
if (frm.ShowDialog() == DialogResult.OK)
frm.Selection(out splitList);
}
if (splitList == null || splitList.Count == 0)
return;
var listFirst = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty && splitList.Contains(x.Value.Product.ProductGroup.GroupType));
var listSecond = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty && !splitList.Contains(x.Value.Product.ProductGroup.GroupType));
if (listFirst.Count() == 0 || listSecond.Count() == 0)
return; // all or none items selected to be moved
#endregion
var table = GetTableForMove(false);
if (table == null)
return;
#region new voucherFirst
var voucherFirst = new Voucher(Session.User)
{
Customer = _billInfo.Customer,
TableID = table.Name,
Waiter = _billInfo.Waiter,
Printed = isPrinted,
Void = false,
Narration = "",
VoucherType = _billInfo.VoucherType
};
var kotFirst = GetKot(listFirst);
if (kotFirst != null)
voucherFirst.Kots.Add(kotFirst);
#endregion
#region new voucherSecond
var voucherSecond = new Voucher(Session.User)
{
Customer = _billInfo.Customer,
TableID = _billInfo.TableID,
Waiter = _billInfo.Waiter,
Printed = isPrinted,
Void = false,
Narration = "",
VoucherType = _billInfo.VoucherType
};
var kotSecond = GetKot(listSecond);
if (kotSecond != null)
voucherSecond.Kots.Add(kotSecond);
#endregion
using (var bi = new VoucherBI())
{
bi.SplitBill(_billInfo.VoucherID, voucherFirst, voucherSecond);
bi.SaveChanges();
}
if (isPrinted)
{
Thermal.PrintBill(voucherFirst.VoucherID);
Thermal.PrintBill(voucherSecond.VoucherID);
}
LoadBill(voucherFirst.VoucherID);
}
public void VoidBill()
{
#region Check conditions and Permissions
if (_billInfo.VoucherID == Guid.Empty)
return;
//if (!_billInfo.Printed)
// return;
if (_billInfo.Void)
return;
if (!Session.IsAllowed(RoleConstants.VOID_BILL))
return;
if (MessageBox.Show("Are you sure you want to void this bill?", "Void Bill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
return;
#endregion
var voidReason = new SelectVoidReason(GetVoidReason, true);
voidReason.ShowDialog();
if (voidReason.SelectedItem != null)
{
using (var bi = new VoucherBI())
{
bi.VoidBill(_billInfo.VoucherID, voidReason.SelectedItem.Description, !_editVoucherID.HasValue);
bi.SaveChanges();
}
ClearBill();
}
else
{
MessageBox.Show("Please Select a reason if you want to void the bill", "Bill NOT Voided", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private static List<StringType> GetVoidReason(Dictionary<string, string> filter)
{
var list = new List<StringType>
{
new StringType("Discount"),
new StringType("Printing Fault"),
new StringType("Item Changed"),
new StringType("Quantity Reduced"),
new StringType("Costing Bill for Party"),
new StringType("Cashier Mistake"),
new StringType("Management Freesale"),
new StringType("Other")
};
return list.Where(i => i.Description.ToLower().Contains(filter["Name"].ToLower().Trim())).ToList();
}
private void SaveReprintOrDiscountBill(decimal oldAmount)
{
var amountChanged = oldAmount != _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty).Sum(x => x.Value.GrossAmount);
var itemsChanged = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty).Count() != 0;
if (amountChanged || itemsChanged) // Discount or Products changed
{
#region new voucherFirst
var newVoucher = new Voucher(Session.User)
{
Customer = _billInfo.Customer,
TableID = _billInfo.TableID,
Waiter = _billInfo.Waiter,
Printed = true,
Void = false,
Narration = "",
VoucherType = _billInfo.VoucherType
};
var kotNew = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product));
if (kotNew != null)
newVoucher.Kots.Add(kotNew);
#endregion
using (var bi = new VoucherBI())
{
bi.DiscountPrintedBill(_billInfo.VoucherID, newVoucher);
bi.SaveChanges();
}
LoadBill(newVoucher.VoucherID);
}
else
{
using (var bi = new ReprintBI())
{
bi.Insert(new Reprint() { Date = DbValues.Date, User = Session.User, Voucher = _billInfo });
bi.SaveChanges();
}
}
}
private Guid? InsertVoucher(bool finalBill, bool updateTable)
{
_billInfo.Printed = finalBill;
_billInfo.Void = false;
_billInfo.Narration = "";
//UpdateKotDiscountAndPrice();
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);
using (var bi = new VoucherBI())
{
var kotID = bi.Insert(_billInfo, updateTable);
bi.SaveChanges();
return kotID;
}
}
private Guid? UpdateVoucher(bool finalBill, bool updateTable)
{
using (var bi = new VoucherBI())
{
var voucher = bi.Get(x => x.VoucherID == _billInfo.VoucherID);
voucher.User = Session.User;
voucher.Customer = _billInfo.Customer;
voucher.Waiter = _billInfo.Waiter;
voucher.Printed = finalBill;
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)
{
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);
bi.SaveChanges();
return kotID;
}
}
private static Kot GetKot(IEnumerable<KeyValuePair<BillItemKey, BillItemValue>> list)
{
var kot = new Kot();
foreach (var item in list)
{
var oldInv = kot.Inventories.SingleOrDefault(x => x.Product.ProductID == item.Key.ProductID);
if (oldInv != null)
{
oldInv.Quantity += item.Value.Quantity;
if (oldInv.Quantity == 0)
kot.Inventories.Remove(oldInv);
}
else
{
var inv = new Inventory
{
Product = item.Value.Product,
Quantity = item.Value.Quantity,
Price = item.Value.Price,
FullPrice = item.Value.FullPrice,
Discount = item.Value.Discount,
ServiceCharge = item.Value.ServiceCharge,
IsScTaxable = item.Value.IsScTaxable,
ServiceTaxRate = item.Value.ServiceTaxRate,
VatRate = item.Value.VatRate,
ServiceTax = item.Value.ServiceTax,
Vat = item.Value.Vat
};
foreach (var mod in item.Value.Modifiers)
inv.InventoryModifier.Add(new InventoryModifier { Modifier = mod });
kot.Inventories.Add(inv);
}
}
return kot.Inventories.Count == 0 ? null : kot;
}
#endregion
#region InputBox
private bool GetInput(string prompt, ref decimal amount)
{
var result = InputBox.Show(prompt, amount.ToString(), InputBox_Validating);
if (!result.OK)
return false;
return decimal.TryParse(result.Text, out amount);
}
private bool GetInput(string prompt, ref string info)
{
var result = InputBox.Show(prompt, info, InputBox_Validating);
if (!result.OK)
return false;
info = result.Text.Trim();
return !string.IsNullOrEmpty(info);
}
#endregion
}
}