2011-02-18 16:54:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-06-23 12:47:48 +00:00
|
|
|
|
using NHibernate.Criterion;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
using Tanshu.Accounts.Contracts;
|
|
|
|
|
using Tanshu.Accounts.Entities;
|
|
|
|
|
using Tanshu.Accounts.Entities.Auth;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public static class VoucherBI
|
|
|
|
|
{
|
|
|
|
|
static public bool IsBillPrinted(int voucherID)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
return session.Get<Voucher>(voucherID).Printed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
static public int? Insert(Voucher voucher, bool updateTable)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
2011-03-11 18:49:48 +00:00
|
|
|
|
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;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
Kot addedKot = null;
|
2011-03-11 18:49:48 +00:00
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == 0))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
addedKot = item;
|
|
|
|
|
item.Date = dt;
|
|
|
|
|
item.Printed = true;
|
2011-03-11 18:49:48 +00:00
|
|
|
|
item.TableID = voucher.TableID;
|
|
|
|
|
item.User = voucher.User;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
item.Code = DbValues.KotCode;
|
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
UpdateSettlements(voucher);
|
2011-03-11 18:49:48 +00:00
|
|
|
|
session.Save(voucher);
|
2011-06-23 12:47:48 +00:00
|
|
|
|
if (updateTable)
|
|
|
|
|
using (var ft = new FoodTableBI(session, false))
|
|
|
|
|
ft.UpdateStatus(voucher);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
trans.Commit();
|
2011-03-11 18:49:48 +00:00
|
|
|
|
return addedKot == null ? (int?)null : addedKot.KotID;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-11 18:49:48 +00:00
|
|
|
|
|
2011-06-23 12:47:48 +00:00
|
|
|
|
private static void UpdateSettlements(Voucher voucher)
|
2011-03-11 18:49:48 +00:00
|
|
|
|
{
|
|
|
|
|
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
|
|
|
|
|
amount = Math.Round(amount, 5);
|
|
|
|
|
if (voucher.Settlements.Count(x => x.Settled == SettleOption.Amount) == 0)
|
|
|
|
|
voucher.Settlements.Add(new VoucherSettlement() { Amount = amount, Settled = SettleOption.Amount });
|
|
|
|
|
else
|
|
|
|
|
voucher.Settlements.Single(x => x.Settled == SettleOption.Amount).Amount = amount;
|
2011-04-11 12:55:45 +00:00
|
|
|
|
|
2011-03-11 18:49:48 +00:00
|
|
|
|
var roundoff = Math.Round(amount) - amount;
|
|
|
|
|
if (voucher.Settlements.Count(x => x.Settled == SettleOption.RoundOff) == 0)
|
|
|
|
|
voucher.Settlements.Add(new VoucherSettlement() { Amount = roundoff, Settled = SettleOption.RoundOff });
|
|
|
|
|
else
|
|
|
|
|
voucher.Settlements.Single(x => x.Settled == SettleOption.RoundOff).Amount = roundoff;
|
2011-04-11 12:55:45 +00:00
|
|
|
|
|
2011-03-12 11:42:02 +00:00
|
|
|
|
var balance = voucher.Settlements.Where(x => x.Settled != SettleOption.Unsettled).Sum(x => x.Amount) * -1;
|
2011-03-11 18:49:48 +00:00
|
|
|
|
if (voucher.Settlements.Count(x => x.Settled == SettleOption.Unsettled) == 0)
|
2011-06-23 12:47:48 +00:00
|
|
|
|
{
|
|
|
|
|
if (balance != 0)
|
|
|
|
|
voucher.Settlements.Add(new VoucherSettlement() { Amount = balance, Settled = SettleOption.Unsettled });
|
|
|
|
|
}
|
2011-03-11 18:49:48 +00:00
|
|
|
|
else if (balance == 0)
|
|
|
|
|
voucher.Settlements.Remove(voucher.Settlements.Single(x => x.Settled == SettleOption.Unsettled));
|
|
|
|
|
else
|
|
|
|
|
voucher.Settlements.Single(x => x.Settled == SettleOption.Unsettled).Amount = balance;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 16:54:48 +00:00
|
|
|
|
public static void Delete(int voucherID)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
2011-06-23 12:47:48 +00:00
|
|
|
|
var voucher = session.Get<Voucher>(voucherID);
|
|
|
|
|
session.Delete(voucher);
|
|
|
|
|
using (var ft = new FoodTableBI(session, false))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-06-23 12:47:48 +00:00
|
|
|
|
//var table = ft.Get(x=>x.VoucherID == voucherID);
|
|
|
|
|
ft.UpdateStatus(voucher.TableID, voucherID, null);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
trans.Commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-23 12:47:48 +00:00
|
|
|
|
static public int? Update(Voucher voucher, bool updateTable)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
2011-03-11 18:49:48 +00:00
|
|
|
|
var dt = DbValues.Date;
|
|
|
|
|
voucher.LastEditDate = dt;
|
|
|
|
|
if (voucher.Date == null)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
2011-03-11 18:49:48 +00:00
|
|
|
|
voucher.Date = dt;
|
|
|
|
|
voucher.BillID = DbValues.BillID;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
2011-03-11 18:49:48 +00:00
|
|
|
|
if (!voucher.Printed)
|
|
|
|
|
voucher.Date = dt;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
Kot addedKot = null;
|
2011-03-11 18:49:48 +00:00
|
|
|
|
foreach (var item in voucher.Kots.Where(item => item.KotID == 0))
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
addedKot = item;
|
|
|
|
|
item.Date = dt;
|
|
|
|
|
item.Printed = true;
|
2011-03-11 18:49:48 +00:00
|
|
|
|
item.TableID = voucher.TableID;
|
|
|
|
|
item.User = voucher.User;
|
2011-02-18 16:54:48 +00:00
|
|
|
|
item.Code = DbValues.KotCode;
|
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
UpdateSettlements(voucher);
|
2011-03-11 18:49:48 +00:00
|
|
|
|
session.Update(voucher);
|
2011-06-23 12:47:48 +00:00
|
|
|
|
if (updateTable)
|
|
|
|
|
using (var ft = new FoodTableBI(session, false))
|
|
|
|
|
ft.UpdateStatus(voucher);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
trans.Commit();
|
|
|
|
|
if (addedKot == null)
|
|
|
|
|
return null;
|
|
|
|
|
else
|
|
|
|
|
return addedKot.KotID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-11 12:55:45 +00:00
|
|
|
|
//static private void UpdateTable(Voucher voucher, ISession session)
|
|
|
|
|
//{
|
|
|
|
|
// using (var ft = new FoodTableBI(session, true))
|
|
|
|
|
// {
|
|
|
|
|
// string status;
|
|
|
|
|
// if (!voucher.Printed)
|
|
|
|
|
// status = "running";
|
|
|
|
|
// else if (voucher.Settlements.Count(x => x.Settled == SettleOption.Unsettled) != 0 && voucher.Void == false)
|
|
|
|
|
// status = "printed";
|
|
|
|
|
// else
|
|
|
|
|
// status = null;
|
|
|
|
|
// ft.UpdateStatus(voucher.TableID, voucher.VoucherID, status);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
static public Voucher Get(int voucherID)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
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);
|
2011-06-23 12:47:48 +00:00
|
|
|
|
NHibernateUtil.Initialize(kot.User);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
foreach (var item in kot.Inventories)
|
|
|
|
|
{
|
|
|
|
|
NHibernateUtil.Initialize(item);
|
|
|
|
|
NHibernateUtil.Initialize(item.Product);
|
2011-06-23 12:47:48 +00:00
|
|
|
|
NHibernateUtil.Initialize(item.Product.ProductGroup);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
foreach (var inmod in item.InventoryModifier)
|
|
|
|
|
NHibernateUtil.Initialize(inmod.Modifier);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-11 18:49:48 +00:00
|
|
|
|
foreach (var item in voucher.Settlements)
|
|
|
|
|
NHibernateUtil.Initialize(item);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
return voucher;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-23 12:47:48 +00:00
|
|
|
|
static public Voucher Get(string billID)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
var voucher = session.CreateCriteria<Voucher>()
|
|
|
|
|
.Add(Restrictions.Eq("BillID", billID))
|
|
|
|
|
.UniqueResult<Voucher>();
|
|
|
|
|
return voucher != null ? Get(voucher.VoucherID) : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 16:54:48 +00:00
|
|
|
|
static public void VoidBill(int voucherID, string reason)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
2011-04-11 12:55:45 +00:00
|
|
|
|
var voucher = session.Get<Voucher>(voucherID);
|
|
|
|
|
voucher.Void = true;
|
|
|
|
|
voucher.VoidReason = reason;
|
|
|
|
|
session.Save(voucher);
|
|
|
|
|
using (var ft = new FoodTableBI(session, true))
|
|
|
|
|
ft.UpdateStatus(voucher);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
trans.Commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-11 18:49:48 +00:00
|
|
|
|
static public void SettleVoucher(User user, int voucherID, IDictionary<SettleOption, decimal> values)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
2011-03-11 18:49:48 +00:00
|
|
|
|
var voucher = session.Get<Voucher>(voucherID);
|
2011-06-23 12:47:48 +00:00
|
|
|
|
|
|
|
|
|
for (var i = voucher.Settlements.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
var item = voucher.Settlements[i];
|
|
|
|
|
if (item.Settled == SettleOption.Amount || item.Settled == SettleOption.RoundOff || item.Settled == SettleOption.Unsettled)
|
|
|
|
|
continue;
|
|
|
|
|
if (!values.ContainsKey(item.Settled))
|
|
|
|
|
voucher.Settlements.Remove(item);
|
|
|
|
|
else
|
|
|
|
|
item.Amount = values[item.Settled];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
foreach (var item in values)
|
|
|
|
|
{
|
|
|
|
|
if (voucher.Settlements.Count(x => x.Settled == item.Key) == 0)
|
|
|
|
|
voucher.Settlements.Add(new VoucherSettlement { Settled = item.Key, Amount = item.Value });
|
|
|
|
|
}
|
|
|
|
|
UpdateSettlements(voucher);
|
2011-03-11 18:49:48 +00:00
|
|
|
|
voucher.User = user;
|
|
|
|
|
voucher.LastEditDate = DbValues.Date;
|
|
|
|
|
session.Update(voucher);
|
2011-06-23 12:47:48 +00:00
|
|
|
|
using (var ft = new FoodTableBI(session, false))
|
|
|
|
|
ft.TableSettled(voucher);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
trans.Commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
public static int MergeKot(int kotID, FoodTable foodTable)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
var session = SessionManager.Session;
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
var kot = session.Get<Kot>(kotID);
|
|
|
|
|
var voucher = session.Get<Voucher>(foodTable.VoucherID);
|
|
|
|
|
voucher.Kots.Add(kot);
|
|
|
|
|
session.Update(voucher);
|
|
|
|
|
trans.Commit();
|
|
|
|
|
return voucher.VoucherID;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
public static int MergeKot(int kotID, Voucher voucher)
|
|
|
|
|
{
|
|
|
|
|
var session = SessionManager.Session;
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
var kot = session.Get<Kot>(kotID);
|
|
|
|
|
voucher.Kots.Add(kot);
|
|
|
|
|
session.Update(voucher);
|
|
|
|
|
trans.Commit();
|
|
|
|
|
return voucher.VoucherID;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-18 16:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|