narsil/Tanshu.Accounts.SqlDAO/BusinessLayer/SaleVoucherBI.cs

199 lines
7.4 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;
namespace Tanshu.Accounts.Repository
{
public class SaleVoucherBI
{
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.Name + " (" + product.Units + ")",
Price = product.SalePrice,
Tax = product.Tax.Rate,
ServiceCharge = product.ServiceCharge,
Discount = 0,
Printed = 0,
Quantity = 1,
};
}
public decimal GetProductDiscountLimit(int productID)
{
using (var session = SessionManager.Session)
{
return session.Get<Product>(productID).ProductGroup.DiscountLimit;
}
}
public bool IsBillPrinted(int voucherID)
{
using (var session = SessionManager.Session)
{
return session.Get<SaleVoucher>(voucherID).Printed;
}
}
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;
session.Save(item);
}
trans.Commit();
return true;
}
}
}
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;
session.Save(item);
}
session.Merge(saleVoucher);
trans.Commit();
return true;
}
}
}
public bool GetSaleVoucher(int voucherID, out SaleVoucher saleVoucher, out IList<Inventory> iList)
{
using (var session = SessionManager.Session)
{
saleVoucher = session.Get<SaleVoucher>(voucherID);
iList = saleVoucher.Inventories;
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);
// }
// }
//}
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();
}
}
//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);
// }
// }
//}
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();
}
}
}
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();
}
}
}
}
}