narsil/Tanshu.Accounts.SqlDAO/BusinessLayer/SaleVoucherBI.cs
2011-02-01 02:03:22 +05:30

236 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Data.DAO;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.SqlDAO;
using NHibernate.Criterion;
using Tanshu.Accounts.Entities.Auth;
using NHibernate;
namespace Tanshu.Accounts.Repository
{
public static class SaleVoucherBI
{
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 = 0,
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<SaleVoucher>(voucherID).Printed;
}
}
static public bool Insert(SaleVoucher saleVoucher, IList<Inventory> inventory)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
DateTime dt = DbValues.Date;
saleVoucher.CreationDate = dt;
saleVoucher.LastEditDate = dt;
saleVoucher.Date = dt;
saleVoucher.KotID = DbValues.KotID;
if (saleVoucher.Printed)
saleVoucher.BillID = DbValues.BillID;
else
saleVoucher.BillID = saleVoucher.KotID;
session.Save(saleVoucher);
foreach (var item in inventory)
{
item.Voucher = saleVoucher;
IList<Modifier> list = new List<Modifier>();
foreach (var mod in item.InventoryModifier)
list.Add(mod.Modifier);
item.InventoryModifier = new List<InventoryModifier>();
session.Save(item);
foreach (var mod in list)
session.Save(new InventoryModifier() { Inventory = item, Modifier = mod });
}
trans.Commit();
return true;
}
}
}
static public bool Update(SaleVoucher saleVoucher, IList<Inventory> inventory)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
DateTime dt = DbValues.Date;
saleVoucher.LastEditDate = dt;
saleVoucher.Inventories = inventory;
if (saleVoucher.Date == null)
{
saleVoucher.Date = dt;
saleVoucher.BillID = DbValues.BillID;
}
if (!saleVoucher.Printed)
saleVoucher.Date = dt;
foreach (var item in inventory)
{
item.Voucher = saleVoucher;
IList<Modifier> list = new List<Modifier>();
foreach (var mod in item.InventoryModifier)
list.Add(mod.Modifier);
item.InventoryModifier = new List<InventoryModifier>();
session.Save(item);
foreach (var mod in list)
session.Save(new InventoryModifier() { Inventory = item, Modifier = mod });
}
session.Merge(saleVoucher);
trans.Commit();
return true;
}
}
}
static public bool GetSaleVoucher(int voucherID, out SaleVoucher saleVoucher, out IList<Inventory> iList)
{
using (var session = SessionManager.Session)
{
saleVoucher = session.Get<SaleVoucher>(voucherID);
foreach (var item in saleVoucher.Inventories)
{
NHibernateUtil.Initialize(item);
foreach (var inmod in item.InventoryModifier)
NHibernateUtil.Initialize(inmod.Modifier);
}
iList = saleVoucher.Inventories;
NHibernateUtil.Initialize(saleVoucher.Customer);
NHibernateUtil.Initialize(saleVoucher.Waiter);
NHibernateUtil.Initialize(saleVoucher.User);
return true;
}
}
//public List<PendingBillsBO> GetPendingBills(PendingType list)
//{
// GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
// using (IConnectionDAO connection = factory.Connection)
// {
// using (ISaleVoucherMixDAO dao = factory.GetSaleVoucherMixDAO(connection))
// {
// return dao.GetPendingBills(list);
// }
// }
//}
static public SaleVoucher GetPendingVoucherID(string tableID)
{
using (var session = SessionManager.Session)
{
return (from v in session.QueryOver<SaleVoucher>()
where v.Void == false && v.TableID == tableID && v.Settled.SettleOptionID == SettleOptionFactory.Unsettled
select v).SingleOrDefault();
}
}
static public string GetTableStatus(string tableID)
{
using (var session = SessionManager.Session)
{
var s = (from v in session.QueryOver<SaleVoucher>()
where v.Void == false && v.TableID == tableID && v.Settled.SettleOptionID == SettleOptionFactory.Unsettled
select v).SingleOrDefault();
if (s == null)
return "default";
else if (s.Printed == true)
return "printed";
else
return "running";
}
}
//public List<InventoryBO> SaleInventory(Dictionary<BillItemKey, BillInventoryBO>.ValueCollection list, Guid? voucherID)
//{
// GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
// using (IConnectionDAO connection = factory.Connection)
// {
// using (ISaleVoucherMixDAO dao = factory.GetSaleVoucherMixDAO(connection))
// {
// return dao.SaleInventory(list, voucherID);
// }
// }
//}
//public void GetComplexBillInformation(Guid voucherID, Guid complexProductID, ref decimal rate, ref decimal quantity, ref string name)
//{
// GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
// using (IConnectionDAO connection = factory.Connection)
// {
// using (ISaleVoucherMixDAO dao = factory.GetSaleVoucherMixDAO(connection))
// {
// dao.GetComplexBillInformation(voucherID, complexProductID, ref rate, ref quantity, ref name);
// }
// }
//}
//public void SetAlarm(Guid voucherID, DateTime? alarmTime)
//{
// GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
// using (IConnectionDAO connection = factory.Connection)
// {
// using (ISaleVoucherDAO dao = factory.GetSaleVoucherDAO(connection))
// {
// dao.SetAlarm(voucherID, alarmTime);
// }
// }
//}
static public void VoidBill(int voucherID, string reason)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
var saleVoucher = session.Get<SaleVoucher>(voucherID);
saleVoucher.Void = true;
saleVoucher.VoidReason = reason;
session.Save(saleVoucher);
trans.Commit();
}
}
}
static public void SettleVoucher(User user, int voucherID, int settleOption)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
var saleVoucher = session.Get<SaleVoucher>(voucherID);
saleVoucher.Settled = SettleOptionBI.GetSettleOption(settleOption);
saleVoucher.User = user;
saleVoucher.LastEditDate = DbValues.Date;
session.Merge(saleVoucher);
trans.Commit();
}
}
}
}
}