narsil/Tanshu.Accounts.Repository/CheckoutBI.cs

113 lines
4.5 KiB
C#
Raw Normal View History

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;
using NHibernate;
using NHibernate.Transform;
namespace Tanshu.Accounts.Repository
{
public class CheckoutBI
{
#region Properties
public Dictionary<SettleOption, int> amounts = new Dictionary<SettleOption, int>();
public Dictionary<SettleOption, string> info = new Dictionary<SettleOption, string>();
public string Cashiers { get; private set; }
public User Cashier { get; private set; }
public DateTime StartDate { get; private set; }
public DateTime FinishDate { get; private set; }
public string Manager
{
get { return Session.User.Name; }
}
#endregion
protected readonly ISession _session;
private IDictionary<SettleOption, IList<Voucher>> vList = new Dictionary<SettleOption, IList<Voucher>>();
public CheckoutBI(Guid cashier, DateTime startDate, DateTime finishDate)
{
_session = SessionManager.Session;
StartDate = startDate.Date.AddHours(6);
FinishDate = finishDate.Date.AddDays(1).AddHours(5);
Cashier = _session.QueryOver<User>()
.Where(x => x.UserID == cashier)
.SingleOrDefault();
Cashiers = GetActiveCashiers();
GetBillDetails();
}
public void GetBillDetails()
{
if (FinishDate <= StartDate)
return;
Voucher vAlias = null;
VoucherSettlement vsAlias = null;
var list = _session.QueryOver<Voucher>(() => vAlias)
.Left.JoinAlias(x => x.Settlements, () => vsAlias)
.Where(x => x.Date >= StartDate && x.Date <= FinishDate && x.User == Cashier)
.OrderBy(x => x.VoucherType).Asc
.ThenBy(x => x.BillID).Asc
.TransformUsing(Transformers.DistinctRootEntity)
.List();
//voidInfo = "\n\r--- Void Bills ---------------------------";
foreach (var item in list)
{
if (item.Void)
{
if (!amounts.ContainsKey(SettleOption.Void))
{
amounts.Add(SettleOption.Void, 0);
info.Add(SettleOption.Void, string.Format("\n\r--- {0} ", SettleOption.Void.Display()).PadRight(44, '-'));
}
var amount = (int)(item.Settlements.Single(x => x.Settled == SettleOption.Amount).Amount * -1);
amounts[SettleOption.Void] += amount;
info[SettleOption.Void] += string.Format("\n\r{0:dd-MMM-yyyy HH:mm:ss} {1} {2}", item.Date, item.FullBillID, item.Customer.Name);
info[SettleOption.Void] += string.Format("\n\rAmount: {0:#0.00}", amount);
info[SettleOption.Void] += "\n\r------------------------------------------";
}
else
{
foreach (var so in item.Settlements.Where(x => x.Settled.Visible()))
{
if (!amounts.ContainsKey(so.Settled))
{
amounts.Add(so.Settled, 0);
info.Add(so.Settled, string.Format("\n\r--- {0} ", so.Settled.Display()).PadRight(44, '-'));
}
var amount = (int)so.Amount;
amounts[so.Settled] += amount;
info[so.Settled] += string.Format("\n\r{0:dd-MMM-yyyy HH:mm:ss} {1} {2}", item.Date, item.FullBillID, item.Customer.Name);
info[so.Settled] += string.Format("\n\rAmount: {0:#0.00}", amount);
info[so.Settled] += "\n\r------------------------------------------";
}
}
}
}
private string GetActiveCashiers()
{
var cashiers = "";
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;
}
}
}