narsil/Tanshu.Accounts.Repository/VoucherBI.cs

250 lines
9.2 KiB
C#
Raw Normal View History

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 partial class VoucherBI : UnitOfWork<Voucher>
{
public new Guid Insert(Voucher voucher)
{
_session.Save(voucher);
Kot addedKot = null;
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
{
addedKot = item;
item.Voucher = voucher;
item.Printed = true;
item.Table = voucher.Table;
item.User = voucher.User;
UpdateBillType(voucher);
_session.Save(item);
int index = 0;
foreach (var inv in item.Inventories)
{
inv.Kot = item;
inv.SortOrder = index;
index++;
_session.Save(inv);
foreach (var modifier in inv.InventoryModifier)
{
modifier.Inventory = inv;
_session.Save(modifier);
}
}
}
var options = GetSettlementOptions(voucher);
UpdateSettlements(voucher, options);
return addedKot == null ? Guid.Empty : addedKot.KotID;
}
public new Guid? Update(Voucher voucher)
{
_session.Update(voucher);
Kot addedKot = null;
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
{
addedKot = item;
item.Voucher = voucher;
item.Printed = true;
item.Table = voucher.Table;
item.User = voucher.User;
_session.Save(item);
int index = 0;
foreach (var inv in item.Inventories)
{
inv.Kot = item;
inv.SortOrder = index;
index++;
_session.Save(inv);
foreach (var modifier in inv.InventoryModifier)
{
modifier.Inventory = inv;
_session.Save(modifier);
}
}
}
UpdateBillType(voucher);
var options = GetSettlementOptions(voucher);
UpdateSettlements(voucher, options);
return addedKot == null ? (Guid?)null : addedKot.KotID;
}
public new Voucher Get(Expression<Func<Voucher, bool>> query)
{
var voucher = _session.QueryOver<Voucher>()
.Where(query)
.SingleOrDefault();
if (voucher == null)
return voucher;
NHibernateUtil.Initialize(voucher.Customer);
NHibernateUtil.Initialize(voucher.User);
NHibernateUtil.Initialize(voucher.Table);
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)
{
var voucher = _session.Get<Voucher>(voucherID);
voucher.Void = true;
voucher.VoidReason = reason;
_session.Update(voucher);
}
public void UpdateTable(Expression<Func<FoodTable, bool>> query, Guid? newVoucherID, string status)
{
var table = _session.QueryOver<FoodTable>().Where(query).SingleOrDefault();
if (table == null)
throw new ValidationException("Old Voucher on the table does not match");
table.Status = status;
table.VoucherID = newVoucherID;
_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);
foreach (var item in oldVoucher.Settlements)
{
_session.Delete(item);
}
_session.Delete(oldVoucher);
return newVoucher.VoucherID;
}
private 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;
if (item.InventoryID != Guid.Empty)
_session.Update(item);
}
break;
case VoucherType.TakeAway:
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
{
item.ServiceCharge = 0;
item.ServiceTaxRate = 0;
if (item.InventoryID != Guid.Empty)
_session.Update(item);
}
break;
case VoucherType.Staff:
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
{
item.ServiceCharge = 0;
item.ServiceTaxRate = 0;
item.VatRate = 0;
if (item.InventoryID != Guid.Empty)
_session.Update(item);
}
break;
}
}
public Guid MoveKot(Guid kotID, Guid tableID)
{
var kot = _session.Get<Kot>(kotID);
var oldVoucher = _session.Get<Voucher>(kot.Voucher.VoucherID);
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == tableID).SingleOrDefault();
var newVoucher = new Voucher(Session.User, oldVoucher.Customer, table, false, false, "");
Insert(newVoucher);
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);
}
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.FullBillID);
Insert(newVoucher);
Update(oldVoucher);
}
public void MoveTable(Guid voucherID, Guid tableID)
{
var voucher = _session.Get<Voucher>(voucherID);
var newTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == tableID).SingleOrDefault();
voucher.Table = newTable;
_session.Update(voucher);
}
}
}