69617949bd
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.
336 lines
13 KiB
C#
336 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq.Expressions;
|
|
using Tanshu.Accounts.Entities;
|
|
using Tanshu.Common.Helpers;
|
|
using NHibernate;
|
|
using System.Linq;
|
|
using Tanshu.Accounts.Contracts;
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
{
|
|
public class VoucherBI : UnitOfWork<Voucher>
|
|
{
|
|
public new Guid Insert(Voucher voucher)
|
|
{
|
|
var dt = DbValues.Date;
|
|
voucher.CreationDate = dt;
|
|
voucher.LastEditDate = dt;
|
|
voucher.Date = dt;
|
|
voucher.KotID = DbValues.KotID;
|
|
voucher.BillID = voucher.Printed ? DbValues.BillID(voucher.VoucherType) : voucher.KotID;
|
|
_session.Save(voucher);
|
|
|
|
Kot addedKot = null;
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
|
|
{
|
|
addedKot = item;
|
|
item.Voucher = voucher;
|
|
item.Date = dt;
|
|
item.Printed = true;
|
|
item.TableID = voucher.TableID;
|
|
item.User = voucher.User;
|
|
item.Code = DbValues.KotCode;
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
|
|
VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount);
|
|
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)
|
|
{
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.Name == voucher.TableID).SingleOrDefault();
|
|
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;
|
|
}
|
|
public new Guid? Update(Voucher voucher)
|
|
{
|
|
var dt = DbValues.Date;
|
|
voucher.LastEditDate = dt;
|
|
if (voucher.Date == null)
|
|
{
|
|
voucher.Date = dt;
|
|
voucher.BillID = DbValues.BillID(voucher.VoucherType);
|
|
}
|
|
_session.Update(voucher);
|
|
|
|
Kot addedKot = null;
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
|
|
{
|
|
addedKot = item;
|
|
item.Voucher = voucher;
|
|
item.Date = dt;
|
|
item.Printed = true;
|
|
item.TableID = voucher.TableID;
|
|
item.User = voucher.User;
|
|
item.Code = DbValues.KotCode;
|
|
_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);
|
|
}
|
|
}
|
|
}
|
|
UpdateBillType(voucher);
|
|
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
|
|
VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount);
|
|
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;
|
|
}
|
|
public Guid? Update(Voucher voucher, bool updateTable)
|
|
{
|
|
var kotID = Update(voucher);
|
|
if (updateTable)
|
|
{
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.Name == voucher.TableID).SingleOrDefault();
|
|
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>()
|
|
.Where(query)
|
|
.SingleOrDefault();
|
|
NHibernateUtil.Initialize(voucher.Customer);
|
|
NHibernateUtil.Initialize(voucher.Waiter);
|
|
NHibernateUtil.Initialize(voucher.User);
|
|
foreach (var kot in voucher.Kots)
|
|
{
|
|
NHibernateUtil.Initialize(kot);
|
|
NHibernateUtil.Initialize(kot.User);
|
|
foreach (var item in kot.Inventories)
|
|
{
|
|
NHibernateUtil.Initialize(item);
|
|
NHibernateUtil.Initialize(item.Product);
|
|
NHibernateUtil.Initialize(item.Product.ProductGroup);
|
|
foreach (var inmod in item.InventoryModifier)
|
|
NHibernateUtil.Initialize(inmod.Modifier);
|
|
}
|
|
}
|
|
foreach (var item in voucher.Settlements)
|
|
NHibernateUtil.Initialize(item);
|
|
return voucher;
|
|
}
|
|
public void VoidBill(Guid voucherID, string reason, bool updateTable)
|
|
{
|
|
var voucher = _session.Get<Voucher>(voucherID);
|
|
voucher.Void = true;
|
|
voucher.VoidReason = reason;
|
|
_session.Update(voucher);
|
|
|
|
if (updateTable)
|
|
{
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.Name == voucher.TableID).SingleOrDefault();
|
|
table.VoucherID = null;
|
|
table.Status = null;
|
|
_session.Update(table);
|
|
}
|
|
}
|
|
|
|
public Guid MergeKot(Guid kotID, string tableName)
|
|
{
|
|
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;
|
|
}
|
|
|
|
public Guid MergeTables(Guid voucherID, string tableName)
|
|
{
|
|
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);
|
|
|
|
|
|
var oldTable = _session.QueryOver<FoodTable>().Where(x => x.Name == oldVoucher.TableID).SingleOrDefault();
|
|
foreach (var item in oldVoucher.Settlements)
|
|
{
|
|
_session.Delete(item);
|
|
}
|
|
_session.Delete(oldVoucher);
|
|
oldTable.VoucherID = null;
|
|
oldTable.Status = null;
|
|
_session.Update(oldTable);
|
|
return newVoucher.VoucherID;
|
|
}
|
|
private static void UpdateBillType(Voucher voucher)
|
|
{
|
|
switch (voucher.VoucherType)
|
|
{
|
|
case VoucherType.Regular:
|
|
break;
|
|
case VoucherType.NoCharge:
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
|
{
|
|
item.ServiceCharge = 0;
|
|
item.ServiceTaxRate = 0;
|
|
item.VatRate = 0;
|
|
}
|
|
break;
|
|
case VoucherType.TakeAway:
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
|
{
|
|
item.ServiceCharge = 0;
|
|
item.ServiceTaxRate = 0;
|
|
}
|
|
break;
|
|
case VoucherType.Staff:
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
|
{
|
|
item.ServiceCharge = 0;
|
|
item.ServiceTaxRate = 0;
|
|
item.VatRate = 0;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public Guid MoveKot(Guid kotID, string tableName)
|
|
{
|
|
var kot = _session.Get<Kot>(kotID);
|
|
var oldVoucher = _session.Get<Voucher>(kot.Voucher.VoucherID);
|
|
var newVoucher = new Voucher(Session.User, oldVoucher.Customer, tableName, oldVoucher.Waiter, false, false, "");
|
|
Insert(newVoucher);
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.Name == tableName).SingleOrDefault();
|
|
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";
|
|
var tableFirst = _session.QueryOver<FoodTable>().Where(x => x.Name == first.TableID).SingleOrDefault();
|
|
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);
|
|
|
|
var tableSecond = _session.QueryOver<FoodTable>().Where(x => x.Name == second.TableID).SingleOrDefault();
|
|
tableSecond.VoucherID = first.VoucherID;
|
|
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;
|
|
oldVoucher.VoidReason = string.Format("Bill Discounted / Changed. New Bill ID is {0}", newVoucher.BillID);
|
|
|
|
Insert(newVoucher);
|
|
Update(oldVoucher);
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.Name == oldVoucher.TableID).SingleOrDefault();
|
|
table.VoucherID = newVoucher.VoucherID;
|
|
table.Status = "printed";
|
|
_session.Update(table);
|
|
}
|
|
public Guid Move(string oldTableName, string newTableName)
|
|
{
|
|
var oldTable = _session.QueryOver<FoodTable>().Where(x => x.Name == oldTableName).SingleOrDefault();
|
|
var newTable = _session.QueryOver<FoodTable>().Where(x => x.Name == newTableName).SingleOrDefault();
|
|
var voucher = _session.Get<Voucher>(oldTable.VoucherID);
|
|
|
|
newTable.Status = oldTable.Status;
|
|
newTable.VoucherID = oldTable.VoucherID;
|
|
|
|
oldTable.Status = null;
|
|
oldTable.VoucherID = null;
|
|
|
|
voucher.TableID = newTable.Name;
|
|
|
|
_session.Update(newTable);
|
|
_session.Update(oldTable);
|
|
_session.Update(voucher);
|
|
return voucher.VoucherID;
|
|
}
|
|
}
|
|
}
|