2011-12-05 09:41:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq.Expressions;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
using Tanshu.Accounts.Entities;
|
2011-08-23 07:10:05 +00:00
|
|
|
|
using Tanshu.Common.Helpers;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
using NHibernate;
|
2011-03-11 18:49:48 +00:00
|
|
|
|
using System.Linq;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
using Tanshu.Accounts.Contracts;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public class VoucherBI : UnitOfWork<Voucher>
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public new Guid Insert(Voucher voucher)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Save(voucher);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
Kot addedKot = null;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
addedKot = item;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.Voucher = voucher;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.Printed = true;
|
2014-10-16 11:11:55 +00:00
|
|
|
|
item.Table = voucher.Table;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.User = voucher.User;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
UpdateBillType(voucher);
|
|
|
|
|
_session.Save(item);
|
|
|
|
|
foreach (var inv in item.Inventories)
|
|
|
|
|
{
|
|
|
|
|
inv.Kot = item;
|
|
|
|
|
_session.Save(inv);
|
|
|
|
|
foreach (var modifier in inv.InventoryModifier)
|
|
|
|
|
{
|
|
|
|
|
modifier.Inventory = inv;
|
|
|
|
|
_session.Save(modifier);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
|
|
|
|
|
VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var settlement in voucher.Settlements.Where(x => x.Amount != 0 || x.Settled == SettleOption.Amount))
|
|
|
|
|
{
|
|
|
|
|
settlement.Voucher = voucher;
|
|
|
|
|
_session.Save(settlement);
|
|
|
|
|
}
|
|
|
|
|
return addedKot == null ? Guid.Empty : addedKot.KotID;
|
|
|
|
|
}
|
|
|
|
|
public Guid Insert(Voucher voucher, bool updateTable)
|
|
|
|
|
{
|
|
|
|
|
var kotID = Insert(voucher);
|
|
|
|
|
if (updateTable)
|
|
|
|
|
{
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == voucher.Table.FoodTableID).SingleOrDefault();
|
2014-10-12 09:41:45 +00:00
|
|
|
|
if (table.VoucherID.HasValue)
|
|
|
|
|
throw new ValidationException("A bill exists on this table, cannot overwrite");
|
|
|
|
|
var status = !voucher.Printed ? "running" : "printed";
|
|
|
|
|
table.VoucherID = voucher.VoucherID;
|
|
|
|
|
table.Status = status;
|
|
|
|
|
_session.Update(table);
|
|
|
|
|
}
|
|
|
|
|
return kotID;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public new Guid? Update(Voucher voucher)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Update(voucher);
|
2011-08-23 07:10:05 +00:00
|
|
|
|
|
2011-06-29 20:27:07 +00:00
|
|
|
|
Kot addedKot = null;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
addedKot = item;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.Voucher = voucher;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.Printed = true;
|
2014-10-16 11:11:55 +00:00
|
|
|
|
item.Table = voucher.Table;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.User = voucher.User;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Save(item);
|
|
|
|
|
foreach (var inv in item.Inventories)
|
|
|
|
|
{
|
|
|
|
|
inv.Kot = item;
|
|
|
|
|
_session.Save(inv);
|
|
|
|
|
foreach (var modifier in inv.InventoryModifier)
|
|
|
|
|
{
|
|
|
|
|
modifier.Inventory = inv;
|
|
|
|
|
_session.Save(modifier);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
UpdateBillType(voucher);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
|
|
|
|
|
VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var settlement in voucher.Settlements)
|
|
|
|
|
{
|
|
|
|
|
settlement.Voucher = voucher;
|
|
|
|
|
if (settlement.Amount == 0 && settlement.Settled != SettleOption.Amount)
|
|
|
|
|
{
|
|
|
|
|
if (settlement.VoucherSettlementID != Guid.Empty)
|
|
|
|
|
_session.Delete(settlement);
|
|
|
|
|
}
|
|
|
|
|
else if (settlement.VoucherSettlementID == Guid.Empty)
|
|
|
|
|
_session.Save(settlement);
|
|
|
|
|
else
|
|
|
|
|
_session.Update(settlement);
|
|
|
|
|
}
|
|
|
|
|
return addedKot == null ? (Guid?)null : addedKot.KotID;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public Guid? Update(Voucher voucher, bool updateTable)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
var kotID = Update(voucher);
|
|
|
|
|
if (updateTable)
|
|
|
|
|
{
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == voucher.Table.FoodTableID).SingleOrDefault();
|
2014-10-12 09:41:45 +00:00
|
|
|
|
var status = !voucher.Printed ? "running" : "printed";
|
|
|
|
|
table.VoucherID = voucher.VoucherID;
|
|
|
|
|
table.Status = status;
|
|
|
|
|
_session.Update(table);
|
|
|
|
|
}
|
|
|
|
|
return kotID;
|
|
|
|
|
}
|
|
|
|
|
public new Voucher Get(Expression<Func<Voucher, bool>> query)
|
|
|
|
|
{
|
|
|
|
|
var voucher = _session.QueryOver<Voucher>()
|
2011-12-05 09:41:02 +00:00
|
|
|
|
.Where(query)
|
|
|
|
|
.SingleOrDefault();
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (voucher == null)
|
|
|
|
|
return voucher;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
NHibernateUtil.Initialize(voucher.Customer);
|
|
|
|
|
NHibernateUtil.Initialize(voucher.Waiter);
|
|
|
|
|
NHibernateUtil.Initialize(voucher.User);
|
2014-10-16 11:11:55 +00:00
|
|
|
|
NHibernateUtil.Initialize(voucher.Table);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
foreach (var kot in voucher.Kots)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
NHibernateUtil.Initialize(kot);
|
|
|
|
|
NHibernateUtil.Initialize(kot.User);
|
|
|
|
|
foreach (var item in kot.Inventories)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-03-11 18:49:48 +00:00
|
|
|
|
NHibernateUtil.Initialize(item);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
NHibernateUtil.Initialize(item.Product);
|
|
|
|
|
NHibernateUtil.Initialize(item.Product.ProductGroup);
|
|
|
|
|
foreach (var inmod in item.InventoryModifier)
|
|
|
|
|
NHibernateUtil.Initialize(inmod.Modifier);
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
foreach (var item in voucher.Settlements)
|
|
|
|
|
NHibernateUtil.Initialize(item);
|
|
|
|
|
return voucher;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public void VoidBill(Guid voucherID, string reason, bool updateTable)
|
2011-12-05 09:41:02 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
var voucher = _session.Get<Voucher>(voucherID);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
voucher.Void = true;
|
|
|
|
|
voucher.VoidReason = reason;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Update(voucher);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
|
2014-10-12 09:41:45 +00:00
|
|
|
|
if (updateTable)
|
|
|
|
|
{
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == voucher.Table.FoodTableID).SingleOrDefault();
|
2014-10-12 09:41:45 +00:00
|
|
|
|
table.VoucherID = null;
|
|
|
|
|
table.Status = null;
|
|
|
|
|
_session.Update(table);
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
|
|
|
|
public Guid MergeKot(Guid kotID, string tableName)
|
2011-12-05 09:41:02 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
var kot = _session.Get<Kot>(kotID);
|
|
|
|
|
var oldVoucher = kot.Voucher;
|
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.Name == tableName).SingleOrDefault();
|
|
|
|
|
var newVoucher = _session.Get<Voucher>(table.VoucherID);
|
|
|
|
|
|
|
|
|
|
oldVoucher.Kots.Remove(kot);
|
|
|
|
|
kot.Voucher = newVoucher;
|
|
|
|
|
newVoucher.Kots.Add(kot);
|
|
|
|
|
|
|
|
|
|
_session.Update(kot);
|
|
|
|
|
_session.Update(oldVoucher);
|
|
|
|
|
_session.Update(newVoucher);
|
|
|
|
|
return newVoucher.VoucherID;
|
2011-12-05 09:41:02 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
|
|
|
|
public Guid MergeTables(Guid voucherID, string tableName)
|
2011-06-23 12:47:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
var newTable = _session.QueryOver<FoodTable>().Where(x => x.Name == tableName).SingleOrDefault();
|
|
|
|
|
var newVoucher = _session.Get<Voucher>(newTable.VoucherID);
|
|
|
|
|
var oldVoucher = _session.Get<Voucher>(voucherID);
|
|
|
|
|
for (var i = oldVoucher.Kots.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
var kot = oldVoucher.Kots[i];
|
|
|
|
|
oldVoucher.Kots.Remove(kot);
|
|
|
|
|
kot.Voucher = newVoucher;
|
|
|
|
|
newVoucher.Kots.Add(kot);
|
|
|
|
|
_session.Update(kot);
|
|
|
|
|
}
|
|
|
|
|
_session.Update(newVoucher);
|
|
|
|
|
|
2011-08-23 07:10:05 +00:00
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var oldTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == oldVoucher.Table.FoodTableID).SingleOrDefault();
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var item in oldVoucher.Settlements)
|
|
|
|
|
{
|
|
|
|
|
_session.Delete(item);
|
|
|
|
|
}
|
|
|
|
|
_session.Delete(oldVoucher);
|
|
|
|
|
oldTable.VoucherID = null;
|
|
|
|
|
oldTable.Status = null;
|
|
|
|
|
_session.Update(oldTable);
|
|
|
|
|
return newVoucher.VoucherID;
|
|
|
|
|
}
|
2014-11-06 10:39:11 +00:00
|
|
|
|
private void UpdateBillType(Voucher voucher)
|
2011-12-05 09:41:02 +00:00
|
|
|
|
{
|
|
|
|
|
switch (voucher.VoucherType)
|
|
|
|
|
{
|
|
|
|
|
case VoucherType.Regular:
|
|
|
|
|
break;
|
|
|
|
|
case VoucherType.NoCharge:
|
|
|
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
|
|
|
|
{
|
|
|
|
|
item.ServiceCharge = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.ServiceTaxRate = 0;
|
|
|
|
|
item.VatRate = 0;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (item.InventoryID != Guid.Empty)
|
|
|
|
|
_session.Update(item);
|
2011-12-05 09:41:02 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case VoucherType.TakeAway:
|
|
|
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
2012-04-08 12:28:15 +00:00
|
|
|
|
{
|
2011-12-05 09:41:02 +00:00
|
|
|
|
item.ServiceCharge = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.ServiceTaxRate = 0;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (item.InventoryID != Guid.Empty)
|
|
|
|
|
_session.Update(item);
|
2012-04-08 12:28:15 +00:00
|
|
|
|
}
|
2011-12-05 09:41:02 +00:00
|
|
|
|
break;
|
|
|
|
|
case VoucherType.Staff:
|
|
|
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
|
|
|
|
{
|
|
|
|
|
item.ServiceCharge = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.ServiceTaxRate = 0;
|
|
|
|
|
item.VatRate = 0;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (item.InventoryID != Guid.Empty)
|
|
|
|
|
_session.Update(item);
|
2011-12-05 09:41:02 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
public Guid MoveKot(Guid kotID, Guid tableID)
|
2014-10-12 09:41:45 +00:00
|
|
|
|
{
|
|
|
|
|
var kot = _session.Get<Kot>(kotID);
|
|
|
|
|
var oldVoucher = _session.Get<Voucher>(kot.Voucher.VoucherID);
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == tableID).SingleOrDefault();
|
|
|
|
|
var newVoucher = new Voucher(Session.User, oldVoucher.Customer, table, oldVoucher.Waiter, false, false, "");
|
2014-10-12 09:41:45 +00:00
|
|
|
|
Insert(newVoucher);
|
|
|
|
|
table.VoucherID = newVoucher.VoucherID;
|
|
|
|
|
table.Status = "running";
|
|
|
|
|
_session.Update(table);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oldVoucher.Kots.Remove(kot);
|
|
|
|
|
kot.Voucher = newVoucher;
|
|
|
|
|
newVoucher.Kots.Add(kot);
|
|
|
|
|
|
|
|
|
|
_session.Update(kot);
|
|
|
|
|
_session.Update(oldVoucher);
|
|
|
|
|
_session.Update(newVoucher);
|
|
|
|
|
|
|
|
|
|
return newVoucher.VoucherID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SplitBill(Guid oldVoucherID, Voucher first, Voucher second)
|
|
|
|
|
{
|
|
|
|
|
var oldVoucher = _session.Get<Voucher>(oldVoucherID);
|
|
|
|
|
oldVoucher.User = Session.User;
|
|
|
|
|
oldVoucher.Void = true;
|
|
|
|
|
oldVoucher.VoidReason = "Bill Split";
|
|
|
|
|
|
|
|
|
|
Insert(first);
|
|
|
|
|
Insert(second);
|
|
|
|
|
Update(oldVoucher);
|
|
|
|
|
|
|
|
|
|
var status = oldVoucher.Printed ? "printed" : "running";
|
2014-11-02 08:03:31 +00:00
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var tableFirst = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == first.Table.FoodTableID).SingleOrDefault();
|
2014-10-12 09:41:45 +00:00
|
|
|
|
if (tableFirst.VoucherID.HasValue)
|
|
|
|
|
throw new ValidationException("A bill exists on this table, cannot overwrite");
|
|
|
|
|
tableFirst.VoucherID = first.VoucherID;
|
|
|
|
|
tableFirst.Status = status;
|
|
|
|
|
_session.Update(tableFirst);
|
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var tableSecond = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == second.Table.FoodTableID).SingleOrDefault();
|
2014-11-02 08:12:32 +00:00
|
|
|
|
tableSecond.VoucherID = second.VoucherID;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
tableSecond.Status = status;
|
|
|
|
|
_session.Update(tableFirst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DiscountPrintedBill(Guid oldVoucherID, Voucher newVoucher)
|
|
|
|
|
{
|
|
|
|
|
var oldVoucher = _session.Get<Voucher>(oldVoucherID);
|
|
|
|
|
oldVoucher.User = Session.User;
|
|
|
|
|
oldVoucher.Void = true;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
oldVoucher.VoidReason = string.Format("Bill Discounted / Changed. New Bill ID is {0}", newVoucher.FullBillID);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
|
|
|
|
Insert(newVoucher);
|
|
|
|
|
Update(oldVoucher);
|
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == oldVoucher.Table.FoodTableID).SingleOrDefault();
|
2014-10-12 09:41:45 +00:00
|
|
|
|
table.VoucherID = newVoucher.VoucherID;
|
|
|
|
|
table.Status = "printed";
|
|
|
|
|
_session.Update(table);
|
|
|
|
|
}
|
2014-10-16 11:11:55 +00:00
|
|
|
|
public Guid Move(Guid oldTableID, Guid newTableID)
|
2014-10-12 09:41:45 +00:00
|
|
|
|
{
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var oldTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == oldTableID).SingleOrDefault();
|
|
|
|
|
var newTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == newTableID).SingleOrDefault();
|
2014-10-12 09:41:45 +00:00
|
|
|
|
var voucher = _session.Get<Voucher>(oldTable.VoucherID);
|
|
|
|
|
|
|
|
|
|
newTable.Status = oldTable.Status;
|
|
|
|
|
newTable.VoucherID = oldTable.VoucherID;
|
|
|
|
|
|
|
|
|
|
oldTable.Status = null;
|
|
|
|
|
oldTable.VoucherID = null;
|
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
voucher.Table = newTable;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
|
|
|
|
_session.Update(newTable);
|
|
|
|
|
_session.Update(oldTable);
|
|
|
|
|
_session.Update(voucher);
|
|
|
|
|
return voucher.VoucherID;
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|