narsil/Tanshu.Accounts.Repository/BusinessLayer/CheckoutBI.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

367 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
using NHibernate.Criterion;
using Tanshu.Accounts.Contracts;
using Tanshu.Common.Helpers;
namespace Tanshu.Accounts.Repository
{
public class CheckoutBI
{
#region Properties
public decimal Opening { get; private set; }
public decimal Receipts { get; private set; }
public decimal AdvanceReceipts { get; private set; }
public decimal CcReceipts { get; private set; }
public decimal NcReceipts { get; private set; }
public decimal BtcReceipts { get; private set; }
public decimal AdvanceAdjusted { get; private set; }
public decimal CashPayments { get; private set; }
public decimal AdditionalVoids { get; private set; }
public decimal VoidsInSystem { get; private set; }
public decimal Discount { get; private set; }
public decimal PendingBills { get; private set; }
public decimal CashReceipts { get; private set; }
public decimal ClosingBalance { get; private set; }
public decimal RetainedOvernight { get; private set; }
public decimal CashDeposited { get; private set; } //
public decimal Excess { get; private set; } //
public string Status { get; private set; } //
public string Cashiers { get; private set; } //
public User Cashier { get; private set; }
public decimal OldPending { get; private set; }
public decimal OldReceipts { get; private set; }
public decimal OldVoided { get; private set; }
public DateTime StartDate { get; private set; }
public DateTime FinishDate { get; private set; }
public string PendingString { get; private set; }
public string CcString { get; private set; }
public string NcString { get; private set; }
public string BtcString { get; private set; }
public string VoidsString { get; private set; }
public string DiscountString { get; private set; }
public string PaymentString { get; private set; }
public string Manager
{
get { return Session.User.Name; }
}
#endregion
public CheckoutBI(int cashier, DateTime startDate, DateTime finishDate)
{
using (var session = SessionManager.Session)
{
using (var bi = new UserBI(session, false))
this.Cashier = bi.Get(x => x.UserID == cashier);
this.StartDate = startDate.Date.AddHours(6);
this.FinishDate = finishDate.Date.AddDays(1).AddHours(5);
string info;
PendingBills = GetPrintInfo(out info, SettleOption.Unsettled);
PendingString = info;
CcReceipts = GetPrintInfo(out info, SettleOption.CreditCard);
CcString = info;
NcReceipts = GetPrintInfo(out info, SettleOption.NoCharge);
NcString = info;
BtcReceipts = GetPrintInfo(out info, SettleOption.BillToCompany);
BtcString = info;
VoidsInSystem = GetVoids(out info);
VoidsString = info;
// Opening = dao.GetOpenings();
// Receipts = GetReceipts();
PaymentString = "";
// CashPayments = dao.GetPayments();
// AdditionalVoids = dao.GetAdditionalVoids();
// RetainedOvernight = dao.GetRetainedOvernight();
AdvanceReceipts = GetAdvancesReceived();
AdvanceAdjusted = GetAdvancesAdjusted();
// PaymentString = dao.GetPaymentString();
// OldPending = dao.GetOldPending();
// OldReceipts = dao.GetOldReceipts();
// OldVoided = dao.GetOldVoided();
Cashiers = GetActiveCashiers();
CashReceipts = GetPrintInfo(out info, SettleOption.Cash);
Discount = GetDiscountBills(.1M, out info);
DiscountString = info;
ClosingBalance = Opening
+ Receipts
+ AdvanceReceipts
- CashPayments
- AdditionalVoids
+ CashReceipts;
}
}
#region Advances
private decimal GetAdvancesAdjusted()
{
using (var session = SessionManager.Session)
{
var amt = session.QueryOver<Advance>()
.Where(i => i.DateOut >= StartDate && i.DateOut <= FinishDate && i.CashierOut == Cashier)
.SelectList(k => k.SelectSum(j => j.Amount))
.SingleOrDefault<decimal>();
return amt;
}
}
private decimal GetAdvancesReceived()
{
using (var session = SessionManager.Session)
{
var amt = session.QueryOver<Advance>()
.Where(i => i.DateIn >= StartDate && i.DateIn <= FinishDate && i.CashierIn == Cashier)
.SelectList(k => k.SelectSum(j => j.Amount))
.SingleOrDefault<decimal>();
return amt;
}
}
#endregion
public void Calculate(decimal cashDeposited, decimal retainedOvernight)
{
this.CashDeposited = cashDeposited;
this.RetainedOvernight = retainedOvernight;
Excess = CashDeposited - ClosingBalance;
Status = string.Format("{0:Extra Cash Rs #,##0.00; Cash Short Rs #,##0.00;Cash Perfect}", Excess);
}
private decimal GetPrintInfo(out string info, SettleOption settleOption)
{
using (var session = SessionManager.Session)
{
Voucher voucher = null;
decimal amount;
decimal discount;
info = string.Format("\n\r--- {0} ", settleOption.Display()).PadRight(44, '-');
var list = session.QueryOver<Voucher>(() => voucher)
.Where(i => i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == false)
.WithSubquery.WhereExists(QueryOver.Of<VoucherSettlement>().Where(x => x.Voucher.VoucherID == voucher.VoucherID && x.Settled == settleOption).Select(x => x.Voucher))
.List();
GetInfo(list, settleOption, out amount, out discount, ref info);
return amount;
}
}
//private decimal GetPending(out string info)
//{
// using (var session = SessionManager.Session)
// {
// Voucher voucher = null;
// decimal amount;
// decimal discount;
// info = "\n\r--- Pending Bills ------------------------";
// var list = session.QueryOver<Voucher>(() => voucher)
// .Where(i => i.LastEditDate >= StartDate &&
// i.LastEditDate <= FinishDate &&
// i.User == Cashier &&
// i.Void == false)
// .WithSubquery.WhereExists(QueryOver.Of<VoucherSettlement>().Where(x => x.Voucher.VoucherID == voucher.VoucherID && x.Settled == SettleOption.Unsettled).Select(x => x.Voucher))
// .List();
// GetInfo(list, SettleOption.Unsettled, out amount, out discount, ref info);
// return amount;
// }
//}
//private decimal GetNoCharge(out string info)
//{
// using (var session = SessionManager.Session)
// {
// Voucher voucher = null;
// decimal amount;
// decimal discount;
// info = "\n\r--- No Charge ----------------------------";
// var list = session.QueryOver<Voucher>(() => voucher)
// .Where(i => i.LastEditDate >= StartDate &&
// i.LastEditDate <= FinishDate &&
// i.User == Cashier &&
// i.Void == false)
// .WithSubquery.WhereExists(QueryOver.Of<VoucherSettlement>().Where(x => x.Voucher.VoucherID == voucher.VoucherID && x.Settled == SettleOption.NoCharge).Select(x => x.Voucher))
// .List();
// GetInfo(list, SettleOption.NoCharge, out amount, out discount, ref info);
// return amount;
// }
//}
//private decimal GetCashReceipts(out string info)
//{
// using (var session = SessionManager.Session)
// {
// Voucher voucher = null;
// decimal amount;
// decimal discount;
// info = "\n\r--- Cash Settlement ----------------------";
// var list = session.QueryOver<Voucher>(() => voucher)
// .Where(i => i.LastEditDate >= StartDate &&
// i.LastEditDate <= FinishDate &&
// i.User == Cashier &&
// i.Void == false)
// .WithSubquery.WhereExists(QueryOver.Of<VoucherSettlement>().Where(x => x.Voucher.VoucherID == voucher.VoucherID && x.Settled == SettleOption.Cash).Select(x => x.Voucher))
// .List();
// GetInfo(list, SettleOption.Cash, out amount, out discount, ref info);
// return amount;
// }
//}
//private decimal GetBillToCompany(out string info)
//{
// using (var session = SessionManager.Session)
// {
// Voucher voucher = null;
// decimal amount;
// decimal discount;
// info = "\n\r--- Bill To Company ----------------------";
// var list = session.QueryOver<Voucher>(() => voucher)
// .Where(i => i.LastEditDate >= StartDate &&
// i.LastEditDate <= FinishDate &&
// i.User == Cashier &&
// i.Void == false)
// .WithSubquery.WhereExists(QueryOver.Of<VoucherSettlement>().Where(x => x.Voucher.VoucherID == voucher.VoucherID && x.Settled == SettleOption.BillToCompany).Select(x => x.Voucher))
// .List();
// GetInfo(list, SettleOption.BillToCompany, out amount, out discount, ref info);
// return amount;
// }
//}
//private decimal GetCreditCard(out string info)
//{
// using (var session = SessionManager.Session)
// {
// Voucher voucher = null;
// decimal amount;
// decimal discount;
// info = "\n\r--- Credit Card Bills --------------------";
// var list = session.QueryOver<Voucher>(() => voucher)
// .Where(i => i.LastEditDate >= StartDate &&
// i.LastEditDate <= FinishDate &&
// i.User == Cashier &&
// i.Void == false)
// .WithSubquery.WhereExists(QueryOver.Of<VoucherSettlement>().Where(x => x.Voucher.VoucherID == voucher.VoucherID && x.Settled == SettleOption.CreditCard).Select(x => x.Voucher))
// .List();
// GetInfo(list, SettleOption.CreditCard, out amount, out discount, ref info);
// return amount;
// }
//}
private decimal GetVoids(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- Void Bills ---------------------------";
var list = (from i in session.QueryOver<Voucher>()
where i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == true
select i).List();
GetInfo(list, out amount, out discount, ref info);
return amount;
}
}
private decimal GetDiscountBills(decimal disount, out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- High Discount Bills ------------------";
const string query = @"
select distinct(v) from Voucher v
inner join v.Kots k
inner join k.Inventories i
where v.Date >= :startDate and v.Date <= :finishDate and v.User = :cashierID and v.Void = false
and exists (select Voucher from VoucherSettlement vs where vs.Voucher = v
and vs.Settled != :noCharge and vs.Settled != :unSettled and vs.Settled != :amount and vs.Settled != :roundoff)
and i.Discount >= :discount";
var list = session.CreateQuery(query)
.SetParameter("startDate", StartDate)
.SetParameter("finishDate", FinishDate)
.SetParameter("cashierID", Cashier.UserID)
.SetParameter("unSettled", SettleOption.Unsettled)
.SetParameter("noCharge", SettleOption.NoCharge)
.SetParameter("amount", SettleOption.Amount)
.SetParameter("roundoff", SettleOption.RoundOff)
.SetParameter("discount", disount)
.List<Voucher>();
GetInfo(list, out amount, out discount, ref info);
return discount;
}
}
private static void GetInfo(ICollection<Voucher> list, out decimal amount, out decimal discount, ref string info)
{
amount = 0;
discount = 0;
if (list.Count == 0)
{
info = string.Empty;
return;
}
foreach (var item in list)
{
var amt = item.Settlements.Where(x => x.Settled == SettleOption.Amount).Sum(x => x.Amount) * -1;
var disc = item.Kots.Sum(x => x.Inventories.Sum(y => y.Quantity * y.Rate * y.Discount));
info += string.Format("\n\r{0:dd-MMM-yyyy HH:mm:ss} {1} {2}", item.Date, item.BillID, item.Customer.Name);
info += string.Format("\n\rAmount: {0:#0.00} :: Discount: {1:#0.00}", amt, disc);
info += "\n\r------------------------------------------";
amount += amt;
discount += disc;
}
}
private static void GetInfo(ICollection<Voucher> list, SettleOption settleOption, out decimal amount, out decimal discount, ref string info)
{
amount = 0;
discount = 0;
if (list.Count == 0)
{
info = string.Empty;
return;
}
foreach (var item in list)
{
var amt = item.Settlements.Where(x => x.Settled == settleOption).Sum(x => x.Amount);
var disc = item.Kots.Sum(x => x.Inventories.Sum(y => y.Quantity * y.Rate * y.Discount));
info += string.Format("\n\r{0:dd-MMM-yyyy HH:mm:ss} {1} {2}", item.Date, item.BillID, item.Customer.Name);
info += string.Format("\n\rAmount: {0:#0.00} :: Discount: {1:#0.00}", amt, disc);
info += "\n\r------------------------------------------";
amount += amt;
discount += disc;
}
}
private string GetActiveCashiers()
{
var cashiers = "";
using (var session = SessionManager.Session)
{
const string query = @"
select distinct(u.Name) from Voucher v
inner join v.User u
where v.Date >= :startDate and v.Date <= :finishDate";
var list = session.CreateQuery(query)
.SetParameter("startDate", StartDate)
.SetParameter("finishDate", FinishDate)
.List<string>();
foreach (var item in list)
cashiers += item + ", ";
return cashiers;
}
}
}
}