using System; using System.Collections.Generic; using System.Linq.Expressions; using NHibernate.Criterion; using Tanshu.Accounts.Entities; using Tanshu.Common.Helpers; using NHibernate; using System.Linq; namespace Tanshu.Accounts.Repository { public class VoucherBI : FluentBasicBase { public VoucherBI() : base() { } public VoucherBI(bool beginTransaction) : base(beginTransaction) { } public VoucherBI(ISession session) : base(session) { } public VoucherBI(ISession session, bool beginTransaction) : base(session, beginTransaction) { } public IList List(Expression> query) { return Session.QueryOver() .Where(query) .List(); } public int? Insert(Voucher voucher) { var dt = DbValues.Date; voucher.SetValue(VoucherFields.CreationDate, dt); voucher.SetValue(VoucherFields.LastEditDate, dt); voucher.SetValue(VoucherFields.Date, dt); voucher.SetValue(VoucherFields.KotID, DbValues.KotID); voucher.SetValue(VoucherFields.BillID, voucher.Printed ? DbValues.BillID(voucher.VoucherType) : voucher.KotID); Kot addedKot = null; foreach (var item in voucher.Kots.Where(item => item.KotID == 0)) { addedKot = item; item.Date = dt; item.Printed = true; item.TableID = voucher.TableID; item.User = voucher.User; item.Code = DbValues.KotCode; } UpdateBill(voucher); var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount)); VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount); Session.Save(voucher); return addedKot == null ? (int?)null : addedKot.KotID; } public int? Update(Voucher voucher) { var dt = DbValues.Date; voucher.SetValue(VoucherFields.LastEditDate, dt); if (voucher.Date == null) { voucher.SetValue(VoucherFields.Date, dt); voucher.SetValue(VoucherFields.BillID, DbValues.BillID(voucher.VoucherType)); } Kot addedKot = null; foreach (var item in voucher.Kots.Where(item => item.KotID == 0)) { addedKot = item; item.Date = dt; item.Printed = true; item.TableID = voucher.TableID; item.User = voucher.User; item.Code = DbValues.KotCode; } UpdateBill(voucher); var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount)); VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount); Session.Update(voucher); return addedKot == null ? (int?)null : addedKot.KotID; } public Voucher Get(Expression> query) { var voucher = Session.QueryOver() .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 Delete(int voucherID) { var voucher = Session.Get(voucherID); Delete(voucher); } public void Delete(Voucher voucher) { Session.Delete(voucher); using (var ft = new FoodTableBI(Session, false)) ft.UpdateStatus(voucher.TableID, voucher.VoucherID, null); } public void VoidBill(int voucherID, string reason) { var voucher = Session.Get(voucherID); voucher.Void = true; voucher.VoidReason = reason; Session.Save(voucher); } public int MergeKot(int kotID, FoodTable foodTable) { var kot = Session.Get(kotID); var voucher = Session.Get(foodTable.VoucherID); voucher.Kots.Add(kot); Session.Update(voucher); return voucher.VoucherID; } public int MergeTables(int voucherID, FoodTable foodTable) { var voucher = Session.Get(foodTable.VoucherID); var oldVoucher = Session.Get(voucherID); foreach (var kot in oldVoucher.Kots) voucher.Kots.Add(kot); Session.Update(voucher); return voucher.VoucherID; } public int MergeKot(int kotID, Voucher voucher) { var kot = Session.Get(kotID); voucher.Kots.Add(kot); Session.Update(voucher); return voucher.VoucherID; } private static void UpdateBill(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.Tax = 0; } break; case VoucherType.TakeAway: foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories)) item.ServiceCharge = 0; break; case VoucherType.Staff: foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories)) { item.ServiceCharge = 0; item.Tax = 0; } break; } } } }