narsil/Tanshu.Accounts.Repository/BusinessLayer/VoucherBI.cs

252 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
using NHibernate;
using System.Linq;
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;
}
}
static public int? Insert(Voucher voucher)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
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;
}
UpdateSettlements(voucher, session);
session.Save(voucher);
using (var ft = new FoodTableBI(session, true))
ft.UpdateStatus(voucher);
trans.Commit();
return addedKot == null ? (int?)null : addedKot.KotID;
}
}
}
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;
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;
var balance = voucher.Settlements.Where(x => x.Settled != SettleOption.Unsettled).Sum(x => x.Amount) * -1;
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;
}
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();
}
}
}
static public int? Update(Voucher voucher)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
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;
}
UpdateSettlements(voucher, session);
session.Update(voucher);
using (var ft = new FoodTableBI(session, true))
ft.UpdateStatus(voucher);
trans.Commit();
if (addedKot == null)
return null;
else
return addedKot.KotID;
}
}
}
//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)
{
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);
}
}
foreach (var item in voucher.Settlements)
NHibernateUtil.Initialize(item);
return voucher;
}
}
static public void VoidBill(int voucherID, string reason)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
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);
trans.Commit();
}
}
}
static public void SettleVoucher(User user, int voucherID, IDictionary<SettleOption, decimal> values)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
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);
using (var ft = new FoodTableBI(session, true))
ft.UpdateStatus(voucher);
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;
}
}
}
}