2011-02-18 16:54:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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 BillInventory GetDefaultSaleBillItem(int productID)
|
|
|
|
|
{
|
|
|
|
|
Product product;
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
product = session.Get<Product>(productID);
|
|
|
|
|
return new BillInventory()
|
|
|
|
|
{
|
|
|
|
|
ProductID = product.ProductID,
|
|
|
|
|
Name = product.Units == string.Empty ? product.Name : product.Name + " (" + product.Units + ")",
|
|
|
|
|
Price = product.SalePrice,
|
|
|
|
|
Tax = product.Tax.Rate,
|
|
|
|
|
ServiceCharge = product.ServiceCharge,
|
|
|
|
|
Discount = 0,
|
|
|
|
|
Printed = false,
|
|
|
|
|
Quantity = 1,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static public decimal GetProductDiscountLimit(int productID)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
return session.Get<Product>(productID).ProductGroup.DiscountLimit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static public bool IsBillPrinted(int voucherID)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
return session.Get<Voucher>(voucherID).Printed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-11 18:49:48 +00:00
|
|
|
|
static public int? Insert(Voucher voucher)
|
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-03-11 18:49:48 +00:00
|
|
|
|
UpdateSettlements(voucher, session);
|
|
|
|
|
session.Save(voucher);
|
|
|
|
|
UpdateTable(voucher, session);
|
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
|
|
|
|
|
|
|
|
|
private static void UpdateSettlements(Voucher voucher, ISession session)
|
|
|
|
|
{
|
|
|
|
|
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-03-12 11:42:02 +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-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)
|
|
|
|
|
voucher.Settlements.Add(new VoucherSettlement() { Amount = balance, Settled = SettleOption.Unsettled });
|
|
|
|
|
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())
|
|
|
|
|
{
|
|
|
|
|
session.Delete(session.Get<Voucher>(voucherID));
|
|
|
|
|
using (var ft = new FoodTableBI(session, true))
|
|
|
|
|
{
|
|
|
|
|
var table = session.QueryOver<FoodTable>()
|
|
|
|
|
.Where(x => x.VoucherID == voucherID)
|
|
|
|
|
.SingleOrDefault();
|
|
|
|
|
ft.UpdateStatus(table.Name, voucherID, null);
|
|
|
|
|
}
|
|
|
|
|
trans.Commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-11 18:49:48 +00:00
|
|
|
|
static public int? Update(Voucher voucher)
|
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-03-11 18:49:48 +00:00
|
|
|
|
UpdateSettlements(voucher, session);
|
|
|
|
|
session.Update(voucher);
|
|
|
|
|
UpdateTable(voucher, session);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
trans.Commit();
|
|
|
|
|
if (addedKot == null)
|
|
|
|
|
return null;
|
|
|
|
|
else
|
|
|
|
|
return addedKot.KotID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-11 18:49:48 +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 GetVoucher(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);
|
|
|
|
|
foreach (var item in kot.Inventories)
|
|
|
|
|
{
|
|
|
|
|
NHibernateUtil.Initialize(item);
|
|
|
|
|
NHibernateUtil.Initialize(item.Product);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static public void VoidBill(int voucherID, string reason)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
var saleVoucher = session.Get<Voucher>(voucherID);
|
|
|
|
|
saleVoucher.Void = true;
|
|
|
|
|
saleVoucher.VoidReason = reason;
|
|
|
|
|
session.Save(saleVoucher);
|
2011-03-11 18:49:48 +00:00
|
|
|
|
UpdateTable(saleVoucher, session);
|
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);
|
|
|
|
|
foreach (var item in voucher.Settlements.Where(x => x.Settled != SettleOption.Amount && x.Settled != SettleOption.RoundOff && x.Settled != SettleOption.Unsettled))
|
|
|
|
|
session.Delete(item);
|
|
|
|
|
foreach (var item in values.Where(x => x.Key != SettleOption.Amount && x.Key != SettleOption.RoundOff && x.Key != SettleOption.Unsettled))
|
|
|
|
|
voucher.Settlements.Add(new VoucherSettlement { Settled = item.Key, Amount = item.Value });
|
|
|
|
|
UpdateSettlements(voucher, session);
|
|
|
|
|
voucher.User = user;
|
|
|
|
|
voucher.LastEditDate = DbValues.Date;
|
|
|
|
|
session.Update(voucher);
|
|
|
|
|
UpdateTable(voucher, session);
|
2011-02-18 16:54:48 +00:00
|
|
|
|
trans.Commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static int MoveKot(int kotID, FoodTable foodTable)
|
|
|
|
|
{
|
|
|
|
|
var session = SessionManager.Session;
|
|
|
|
|
using (var trans = session.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
var kot = session.Get<Kot>(kotID);
|
|
|
|
|
foodTable = session.Get<FoodTable>(foodTable.FoodTableID);
|
|
|
|
|
var voucher = session.Get<Voucher>(foodTable.VoucherID);
|
|
|
|
|
voucher.Kots.Add(kot);
|
|
|
|
|
session.Update(voucher);
|
|
|
|
|
trans.Commit();
|
|
|
|
|
return voucher.VoucherID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|