narsil/Tanshu.Accounts.Repository/BusinessLayer/VoucherBI.cs

141 lines
5.1 KiB
C#
Raw Normal View History

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 bool IsVoucherPrinted(int voucherID)
{
return Session.Get<Voucher>(voucherID).Printed;
}
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.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;
}
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 void Delete(int voucherID)
{
var voucher = Session.Get<Voucher>(voucherID);
Session.Delete(voucher);
using (var ft = new FoodTableBI(Session, false))
ft.UpdateStatus(voucher.TableID, voucherID, null);
}
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 );
}
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;
}
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(int voucherID)
{
var voucher = Session.Get<Voucher>(voucherID);
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 Voucher Get(string billID)
{
var voucher = Session.CreateCriteria<Voucher>()
.Add(Restrictions.Eq("BillID", billID))
.UniqueResult<Voucher>();
return voucher != null ? Get(voucher.VoucherID) : null;
}
public void VoidBill(int voucherID, string reason)
{
var voucher = Session.Get<Voucher>(voucherID);
voucher.Void = true;
voucher.VoidReason = reason;
Session.Save(voucher);
}
public int MergeKot(int kotID, FoodTable foodTable)
{
var kot = Session.Get<Kot>(kotID);
var voucher = Session.Get<Voucher>(foodTable.VoucherID);
voucher.Kots.Add(kot);
Session.Update(voucher);
return voucher.VoucherID;
}
public int MergeKot(int kotID, Voucher voucher)
{
var kot = Session.Get<Kot>(kotID);
voucher.Kots.Add(kot);
Session.Update(voucher);
return voucher.VoucherID;
}
}
}