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

262 lines
10 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 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 saleVoucher)
{
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;
Kot addedKot = null;
foreach (var item in saleVoucher.Kots)
{
if (item.KotID != 0)
continue;
addedKot = item;
item.Date = dt;
item.Printed = true;
item.TableID = saleVoucher.TableID;
item.User = saleVoucher.User;
item.Code = DbValues.KotCode;
}
session.Save(saleVoucher);
using (var ft = new FoodTableBI(session, true))
{
string status;
if (!saleVoucher.Printed)
status = "running";
else if (saleVoucher.Settled == SettleOption.Unsettled && saleVoucher.Void == false)
status = "printed";
else
status = null;
ft.UpdateStatus(saleVoucher.TableID, saleVoucher.VoucherID, status);
}
trans.Commit();
if (addedKot == null)
return null;
else
return addedKot.KotID;
}
}
}
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 saleVoucher)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
DateTime dt = DbValues.Date;
saleVoucher.LastEditDate = dt;
if (saleVoucher.Date == null)
{
saleVoucher.Date = dt;
saleVoucher.BillID = DbValues.BillID;
}
if (!saleVoucher.Printed)
saleVoucher.Date = dt;
Kot addedKot = null;
foreach (var item in saleVoucher.Kots)
{
if (item.KotID != 0)
continue;
addedKot = item;
item.Date = dt;
item.Printed = true;
item.TableID = saleVoucher.TableID;
item.User = saleVoucher.User;
item.Code = DbValues.KotCode;
}
session.Update(saleVoucher);
using (var ft = new FoodTableBI(session, true))
{
string status;
if (!saleVoucher.Printed)
status = "running";
else if (saleVoucher.Settled == SettleOption.Unsettled && saleVoucher.Void == false)
status = "printed";
else
status = null;
ft.UpdateStatus(saleVoucher.TableID, saleVoucher.VoucherID, status);
}
trans.Commit();
if (addedKot == null)
return null;
else
return addedKot.KotID;
}
}
}
static public Voucher GetSaleVoucher(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);
}
}
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);
using (var ft = new FoodTableBI(session, true))
{
string status;
if (!saleVoucher.Printed)
status = "running";
else if (saleVoucher.Settled == SettleOption.Unsettled && saleVoucher.Void == false)
status = "printed";
else
status = null;
ft.UpdateStatus(saleVoucher.TableID, saleVoucher.VoucherID, status);
}
trans.Commit();
}
}
}
static public void SettleVoucher(User user, int voucherID, SettleOption settleOption)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
var saleVoucher = session.Get<Voucher>(voucherID);
saleVoucher.Settled = (SettleOption)settleOption;
saleVoucher.User = user;
saleVoucher.LastEditDate = DbValues.Date;
session.Update(saleVoucher);
var table = session.CreateCriteria<FoodTable>()
.Add(Restrictions.Eq("Name", saleVoucher.TableID))
.UniqueResult<FoodTable>();
table.Status = null;
session.Update(table);
using (var ft = new FoodTableBI(session, true))
{
string status;
if (!saleVoucher.Printed)
status = "running";
else if (saleVoucher.Settled == SettleOption.Unsettled && saleVoucher.Void == false)
status = "printed";
else
status = null;
ft.UpdateStatus(saleVoucher.TableID, saleVoucher.VoucherID, status);
}
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;
}
}
}
}