2011-06-29 20:27:07 +00:00
|
|
|
|
using NHibernate.Criterion;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
using Tanshu.Accounts.Entities;
|
2011-08-23 07:10:05 +00:00
|
|
|
|
using Tanshu.Common.Helpers;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
using NHibernate;
|
2011-03-11 18:49:48 +00:00
|
|
|
|
using System.Linq;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public class VoucherBI : FluentBasicBase
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
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)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
return Session.Get<Voucher>(voucherID).Printed;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-08-28 12:17:15 +00:00
|
|
|
|
public bool IsVoucherVoid(int voucherID)
|
|
|
|
|
{
|
|
|
|
|
return Session.Get<Voucher>(voucherID).Void;
|
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public int? Insert(Voucher voucher)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var dt = DbValues.Date;
|
2011-08-23 07:10:05 +00:00
|
|
|
|
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);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
Kot addedKot = null;
|
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == 0))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
addedKot = item;
|
|
|
|
|
item.Date = dt;
|
|
|
|
|
item.Printed = true;
|
|
|
|
|
item.TableID = voucher.TableID;
|
|
|
|
|
item.User = voucher.User;
|
|
|
|
|
item.Code = DbValues.KotCode;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
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;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public void Delete(int voucherID)
|
2011-03-11 18:49:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var voucher = Session.Get<Voucher>(voucherID);
|
|
|
|
|
Session.Delete(voucher);
|
|
|
|
|
using (var ft = new FoodTableBI(Session, false))
|
|
|
|
|
ft.UpdateStatus(voucher.TableID, voucherID, null);
|
2011-03-11 18:49:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public int? Update(Voucher voucher)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var dt = DbValues.Date;
|
2011-08-23 07:10:05 +00:00
|
|
|
|
voucher.SetValue(VoucherFields.LastEditDate, dt);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
if (voucher.Date == null)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-08-23 07:10:05 +00:00
|
|
|
|
voucher.SetValue(VoucherFields.Date, dt);
|
|
|
|
|
voucher.SetValue(VoucherFields.BillID, DbValues.BillID );
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-08-23 07:10:05 +00:00
|
|
|
|
|
2011-06-29 20:27:07 +00:00
|
|
|
|
Kot addedKot = null;
|
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == 0))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
addedKot = item;
|
|
|
|
|
item.Date = dt;
|
|
|
|
|
item.Printed = true;
|
|
|
|
|
item.TableID = voucher.TableID;
|
|
|
|
|
item.User = voucher.User;
|
|
|
|
|
item.Code = DbValues.KotCode;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
|
|
|
|
|
VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount);
|
|
|
|
|
Session.Update(voucher);
|
2011-08-23 07:10:05 +00:00
|
|
|
|
return addedKot == null ? (int?) null : addedKot.KotID;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
|
|
|
|
|
public Voucher Get(int voucherID)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var voucher = Session.Get<Voucher>(voucherID);
|
|
|
|
|
NHibernateUtil.Initialize(voucher.Customer);
|
|
|
|
|
NHibernateUtil.Initialize(voucher.Waiter);
|
|
|
|
|
NHibernateUtil.Initialize(voucher.User);
|
|
|
|
|
foreach (var kot in voucher.Kots)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
NHibernateUtil.Initialize(kot);
|
|
|
|
|
NHibernateUtil.Initialize(kot.User);
|
|
|
|
|
foreach (var item in kot.Inventories)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-03-11 18:49:48 +00:00
|
|
|
|
NHibernateUtil.Initialize(item);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
NHibernateUtil.Initialize(item.Product);
|
|
|
|
|
NHibernateUtil.Initialize(item.Product.ProductGroup);
|
|
|
|
|
foreach (var inmod in item.InventoryModifier)
|
|
|
|
|
NHibernateUtil.Initialize(inmod.Modifier);
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
foreach (var item in voucher.Settlements)
|
|
|
|
|
NHibernateUtil.Initialize(item);
|
|
|
|
|
return voucher;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public Voucher Get(string billID)
|
2011-06-23 12:47:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var voucher = Session.CreateCriteria<Voucher>()
|
|
|
|
|
.Add(Restrictions.Eq("BillID", billID))
|
|
|
|
|
.UniqueResult<Voucher>();
|
|
|
|
|
return voucher != null ? Get(voucher.VoucherID) : null;
|
2011-06-23 12:47:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public void VoidBill(int voucherID, string reason)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var voucher = Session.Get<Voucher>(voucherID);
|
|
|
|
|
voucher.Void = true;
|
|
|
|
|
voucher.VoidReason = reason;
|
|
|
|
|
Session.Save(voucher);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public int MergeKot(int kotID, FoodTable foodTable)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var kot = Session.Get<Kot>(kotID);
|
|
|
|
|
var voucher = Session.Get<Voucher>(foodTable.VoucherID);
|
|
|
|
|
voucher.Kots.Add(kot);
|
|
|
|
|
Session.Update(voucher);
|
|
|
|
|
return voucher.VoucherID;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
public int MergeKot(int kotID, Voucher voucher)
|
2011-06-23 12:47:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
var kot = Session.Get<Kot>(kotID);
|
|
|
|
|
voucher.Kots.Add(kot);
|
|
|
|
|
Session.Update(voucher);
|
|
|
|
|
return voucher.VoucherID;
|
2011-06-23 12:47:48 +00:00
|
|
|
|
}
|
2011-08-23 07:10:05 +00:00
|
|
|
|
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|