2011-12-05 09:41:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq.Expressions;
|
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;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
using Tanshu.Accounts.Contracts;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
2014-11-20 08:12:20 +00:00
|
|
|
|
public partial class VoucherBI : UnitOfWork<Voucher>
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public new Guid Insert(Voucher voucher)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Save(voucher);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
Kot addedKot = null;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
addedKot = item;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.Voucher = voucher;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.Printed = true;
|
2014-10-16 11:11:55 +00:00
|
|
|
|
item.Table = voucher.Table;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.User = voucher.User;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
UpdateBillType(voucher);
|
|
|
|
|
_session.Save(item);
|
2014-11-10 11:06:49 +00:00
|
|
|
|
int index = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var inv in item.Inventories)
|
|
|
|
|
{
|
|
|
|
|
inv.Kot = item;
|
2014-11-10 11:06:49 +00:00
|
|
|
|
inv.SortOrder = index;
|
|
|
|
|
index++;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Save(inv);
|
|
|
|
|
foreach (var modifier in inv.InventoryModifier)
|
|
|
|
|
{
|
|
|
|
|
modifier.Inventory = inv;
|
|
|
|
|
_session.Save(modifier);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-11-20 08:12:20 +00:00
|
|
|
|
var options = GetSettlementOptions(voucher);
|
|
|
|
|
UpdateSettlements(voucher, options);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
return addedKot == null ? Guid.Empty : addedKot.KotID;
|
|
|
|
|
}
|
|
|
|
|
public new Guid? Update(Voucher voucher)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Update(voucher);
|
2011-08-23 07:10:05 +00:00
|
|
|
|
|
2011-06-29 20:27:07 +00:00
|
|
|
|
Kot addedKot = null;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == Guid.Empty))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
addedKot = item;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.Voucher = voucher;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.Printed = true;
|
2014-10-16 11:11:55 +00:00
|
|
|
|
item.Table = voucher.Table;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.User = voucher.User;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Save(item);
|
2014-11-10 11:06:49 +00:00
|
|
|
|
int index = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var inv in item.Inventories)
|
|
|
|
|
{
|
|
|
|
|
inv.Kot = item;
|
2014-11-10 11:06:49 +00:00
|
|
|
|
inv.SortOrder = index;
|
|
|
|
|
index++;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Save(inv);
|
|
|
|
|
foreach (var modifier in inv.InventoryModifier)
|
|
|
|
|
{
|
|
|
|
|
modifier.Inventory = inv;
|
|
|
|
|
_session.Save(modifier);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
UpdateBillType(voucher);
|
2014-11-20 08:12:20 +00:00
|
|
|
|
var options = GetSettlementOptions(voucher);
|
|
|
|
|
UpdateSettlements(voucher, options);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
return addedKot == null ? (Guid?)null : addedKot.KotID;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public new Voucher Get(Expression<Func<Voucher, bool>> query)
|
|
|
|
|
{
|
|
|
|
|
var voucher = _session.QueryOver<Voucher>()
|
2011-12-05 09:41:02 +00:00
|
|
|
|
.Where(query)
|
|
|
|
|
.SingleOrDefault();
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (voucher == null)
|
|
|
|
|
return voucher;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
NHibernateUtil.Initialize(voucher.Customer);
|
|
|
|
|
NHibernateUtil.Initialize(voucher.User);
|
2014-10-16 11:11:55 +00:00
|
|
|
|
NHibernateUtil.Initialize(voucher.Table);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
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
|
|
|
|
}
|
2014-11-20 08:12:20 +00:00
|
|
|
|
public void VoidBill(Guid voucherID, string reason)
|
2011-12-05 09:41:02 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
var voucher = _session.Get<Voucher>(voucherID);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
voucher.Void = true;
|
|
|
|
|
voucher.VoidReason = reason;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Update(voucher);
|
2014-11-20 08:12:20 +00:00
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
|
2014-11-20 08:12:20 +00:00
|
|
|
|
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);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
|
|
|
|
public Guid MergeKot(Guid kotID, string tableName)
|
2011-12-05 09:41:02 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
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;
|
2011-12-05 09:41:02 +00:00
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
|
|
|
|
public Guid MergeTables(Guid voucherID, string tableName)
|
2011-06-23 12:47:48 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2011-08-23 07:10:05 +00:00
|
|
|
|
|
2014-10-12 09:41:45 +00:00
|
|
|
|
foreach (var item in oldVoucher.Settlements)
|
|
|
|
|
{
|
|
|
|
|
_session.Delete(item);
|
|
|
|
|
}
|
|
|
|
|
_session.Delete(oldVoucher);
|
|
|
|
|
return newVoucher.VoucherID;
|
|
|
|
|
}
|
2014-11-06 10:39:11 +00:00
|
|
|
|
private void UpdateBillType(Voucher voucher)
|
2011-12-05 09:41:02 +00:00
|
|
|
|
{
|
|
|
|
|
switch (voucher.VoucherType)
|
|
|
|
|
{
|
|
|
|
|
case VoucherType.Regular:
|
|
|
|
|
break;
|
|
|
|
|
case VoucherType.NoCharge:
|
|
|
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
|
|
|
|
{
|
|
|
|
|
item.ServiceCharge = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.ServiceTaxRate = 0;
|
|
|
|
|
item.VatRate = 0;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (item.InventoryID != Guid.Empty)
|
|
|
|
|
_session.Update(item);
|
2011-12-05 09:41:02 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case VoucherType.TakeAway:
|
|
|
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
2012-04-08 12:28:15 +00:00
|
|
|
|
{
|
2011-12-05 09:41:02 +00:00
|
|
|
|
item.ServiceCharge = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.ServiceTaxRate = 0;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (item.InventoryID != Guid.Empty)
|
|
|
|
|
_session.Update(item);
|
2012-04-08 12:28:15 +00:00
|
|
|
|
}
|
2011-12-05 09:41:02 +00:00
|
|
|
|
break;
|
|
|
|
|
case VoucherType.Staff:
|
|
|
|
|
foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories))
|
|
|
|
|
{
|
|
|
|
|
item.ServiceCharge = 0;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
item.ServiceTaxRate = 0;
|
|
|
|
|
item.VatRate = 0;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
if (item.InventoryID != Guid.Empty)
|
|
|
|
|
_session.Update(item);
|
2011-12-05 09:41:02 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
public Guid MoveKot(Guid kotID, Guid tableID)
|
2014-10-12 09:41:45 +00:00
|
|
|
|
{
|
|
|
|
|
var kot = _session.Get<Kot>(kotID);
|
|
|
|
|
var oldVoucher = _session.Get<Voucher>(kot.Voucher.VoucherID);
|
2014-10-16 11:11:55 +00:00
|
|
|
|
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == tableID).SingleOrDefault();
|
2016-01-18 10:43:43 +00:00
|
|
|
|
var newVoucher = new Voucher(Session.User, oldVoucher.Customer, table, false, false, "");
|
2014-10-12 09:41:45 +00:00
|
|
|
|
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;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
oldVoucher.VoidReason = string.Format("Bill Discounted / Changed. New Bill ID is {0}", newVoucher.FullBillID);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
|
|
|
|
|
Insert(newVoucher);
|
|
|
|
|
Update(oldVoucher);
|
|
|
|
|
}
|
2014-11-20 08:12:20 +00:00
|
|
|
|
public void MoveTable(Guid voucherID, Guid tableID)
|
2014-10-12 09:41:45 +00:00
|
|
|
|
{
|
2014-11-20 08:12:20 +00:00
|
|
|
|
var voucher = _session.Get<Voucher>(voucherID);
|
|
|
|
|
var newTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == tableID).SingleOrDefault();
|
2014-10-16 11:11:55 +00:00
|
|
|
|
voucher.Table = newTable;
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Update(voucher);
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|