narsil/Tanshu.Accounts.Repository/BusinessLayer/VoucherBI.cs
unknown 59909a5ee7 Updated NH, Code Refactoring, made all DB transactions atomic.
Must use the Repositories with Using or else bad things will happen.
2011-06-30 01:57:07 +05:30

146 lines
5.0 KiB
C#

using NHibernate.Criterion;
using Tanshu.Accounts.Entities;
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.CreationDate = dt;
voucher.LastEditDate = dt;
voucher.Date = dt;
voucher.KotID = DbValues.KotID;
voucher.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.LastEditDate = dt;
if (voucher.Date == null)
{
voucher.Date = dt;
voucher.BillID = DbValues.BillID;
}
if (!voucher.Printed)
voucher.Date = dt;
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);
if (addedKot == null)
return null;
else
return 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;
}
}
}