narsil/Tanshu.Accounts.Repository/BusinessLayer/VoucherBI.cs
unknown d8ecec8bb6 Added inverse Attribute to ProductGroup.
BillInventory Renamed.
Refactored Bill to be more usable.
Added Bill Detail Report.
Added Open Bill and Bill Details Roles.
Zero Rate Products have Yellow background Color.
Refactored UserBI, FoodTableBI, ModifierBI, PrintLocationBI, ProductBI, ProductGroupBI, TaxBI, UserBI,
Cached the Products List.
Product and Product Group Form Working.
2011-06-23 18:17:48 +05:30

267 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using NHibernate.Criterion;
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 bool IsBillPrinted(int voucherID)
{
using (var session = SessionManager.Session)
{
return session.Get<Voucher>(voucherID).Printed;
}
}
static public int? Insert(Voucher voucher, bool updateTable)
{
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.Save(voucher);
if (updateTable)
using (var ft = new FoodTableBI(session, false))
ft.UpdateStatus(voucher);
trans.Commit();
return addedKot == null ? (int?)null : addedKot.KotID;
}
}
}
private static void UpdateSettlements(Voucher voucher)
{
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)
{
if (balance != 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())
{
var voucher = session.Get<Voucher>(voucherID);
session.Delete(voucher);
using (var ft = new FoodTableBI(session, false))
{
//var table = ft.Get(x=>x.VoucherID == voucherID);
ft.UpdateStatus(voucher.TableID, voucherID, null);
}
trans.Commit();
}
}
}
static public int? Update(Voucher voucher, bool updateTable)
{
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.Update(voucher);
if (updateTable)
using (var ft = new FoodTableBI(session, false))
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);
NHibernateUtil.Initialize(kot.User);
foreach (var item in kot.Inventories)
{
NHibernateUtil.Initialize(item);
NHibernateUtil.Initialize(item.Product);
NHibernateUtil.Initialize(item.Product.ProductGroup);
foreach (var inmod in item.InventoryModifier)
NHibernateUtil.Initialize(inmod.Modifier);
}
}
foreach (var item in voucher.Settlements)
NHibernateUtil.Initialize(item);
return voucher;
}
}
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;
}
}
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);
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);
voucher.User = user;
voucher.LastEditDate = DbValues.Date;
session.Update(voucher);
using (var ft = new FoodTableBI(session, false))
ft.TableSettled(voucher);
trans.Commit();
}
}
}
public static int MergeKot(int kotID, FoodTable foodTable)
{
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;
}
}
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;
}
}
}
}