narsil/Tanshu.Accounts.Repository/SalesAnalysisBI.cs
tanshu 20eac3c216 Refactor: Instead of a concept of Price/FullPrice, happy hour is now a checkbox in the product.
This needed major refactor in all parts dealing with product or inventory.
2016-04-11 12:31:52 +05:30

167 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Common;
using Tanshu.Common.Helpers;
using System.Linq;
using NHibernate.Transform;
using NHibernate.Criterion;
using NHibernate;
namespace Tanshu.Accounts.Repository
{
public class SalesAnalysisBI
{
protected readonly ISession _session;
public SalesAnalysisBI()
{
_session = SessionManager.Session;
}
public IList<SalesAnalysis> GetSale(DateTime startDate, DateTime finishDate)
{
const string query = @"
select g.GroupType as GroupType, Sum(i.Quantity * i.EffectivePrice * (1 - i.Discount)) as Amount
from Voucher v
inner join v.Kots k
inner join k.Inventories i
inner join i.Product p
inner join p.ProductGroup g
where v.Date >= :startDate and v.Date <= :finishDate 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 vs.Settled != :staff)
group by g.GroupType
order by g.GroupType
";
var list = _session
.CreateQuery(query)
.SetParameter("startDate", startDate)
.SetParameter("finishDate", finishDate)
.SetParameter("noCharge", SettleOption.NoCharge)
.SetParameter("unsettled", SettleOption.Unsettled)
.SetParameter("amount", SettleOption.Amount)
.SetParameter("roundoff", SettleOption.RoundOff)
.SetParameter("staff", SettleOption.Staff)
.List<object[]>();
var outList = new List<SalesAnalysis>();
decimal amount = 0;
foreach (var item in list)
{
amount += (decimal)item[1];
outList.Add(new SalesAnalysis() { GroupType = (string)item[0], Amount = (decimal)item[1] });
}
outList.Add(new SalesAnalysis() { GroupType = "Total Settled", Amount = amount });
return outList;
}
public IList<SalesAnalysis> GetSettlements(DateTime startDate, DateTime finishDate)
{
const string query = @"
select s.Settled, Sum(s.Amount)
from Voucher v
inner join v.Settlements s
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false
group by s.Settled
order by s.Settled
";
var list = _session
.CreateQuery(query)
.SetParameter("startDate", startDate)
.SetParameter("finishDate", finishDate)
.List<object[]>();
var outList = new List<SalesAnalysis>();
decimal amount = 0;
foreach (var item in list)
{
amount += (decimal)item[1];
outList.Add(new SalesAnalysis() { GroupType = ((SettleOption)item[0]).Display(), Amount = (decimal)item[1] });
}
outList.Add(new SalesAnalysis() { GroupType = "Total", Amount = amount });
return outList;
}
public SalesAnalysis GetServiceCharge(DateTime startDate, DateTime finishDate)
{
var query = @"
select coalesce(Sum(i.Quantity * i.EffectivePrice * (1 - i.Discount) * i.ServiceCharge), 0)
from Voucher v
inner join v.Kots k
inner join k.Inventories i
where v.Date >= :startDate and v.Date <= :finishDate 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 vs.Settled != :staff)
";
var amt = _session
.CreateQuery(query)
.SetParameter("startDate", startDate)
.SetParameter("finishDate", finishDate)
.SetParameter("noCharge", SettleOption.NoCharge)
.SetParameter("unsettled", SettleOption.Unsettled)
.SetParameter("amount", SettleOption.Amount)
.SetParameter("roundoff", SettleOption.RoundOff)
.SetParameter("staff", SettleOption.Staff)
.UniqueResult<decimal>();
if (amt != 0)
return new SalesAnalysis() { GroupType = "Service Charge", Amount = amt };
return null;
}
public IList<TaxAnalysis> GetServiceTax(DateTime startDate, DateTime finishDate)
{
var outList = new List<TaxAnalysis>();
var query = @"
select i.ServiceTaxRate, coalesce(Sum(i.Quantity * i.EffectivePrice * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end) * i.ServiceTaxRate), 0),
coalesce(Sum(i.Quantity * i.EffectivePrice * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end)), 0)
from Voucher v
inner join v.Kots k
inner join k.Inventories i
where v.Date >= :startDate and v.Date <= :finishDate 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 vs.Settled != :staff)
group by i.ServiceTaxRate";
var list = _session
.CreateQuery(query)
.SetParameter("startDate", startDate)
.SetParameter("finishDate", finishDate)
.SetParameter("noCharge", SettleOption.NoCharge)
.SetParameter("unsettled", SettleOption.Unsettled)
.SetParameter("amount", SettleOption.Amount)
.SetParameter("roundoff", SettleOption.RoundOff)
.SetParameter("staff", SettleOption.Staff)
.List<object[]>();
foreach (var item in list)
outList.Add(new TaxAnalysis() { Name = string.Format("Service Tax - {0:#.##%;(#.##%);0%}", (decimal)item[0]), TaxRate = (decimal)item[0], TaxAmount = (decimal)item[1], NetSale = (decimal)item[2] });
return outList;
}
public IList<TaxAnalysis> GetVat(DateTime startDate, DateTime finishDate)
{
var outList = new List<TaxAnalysis>();
var query = @"
select va.Name, i.VatRate, coalesce(Sum(i.Quantity * i.EffectivePrice * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end) * i.VatRate), 0),
coalesce(Sum(i.Quantity * i.EffectivePrice * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end)), 0)
from Voucher v
inner join v.Kots k
inner join k.Inventories i
inner join i.Vat va
where v.Date >= :startDate and v.Date <= :finishDate 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 vs.Settled != :staff and vs.Settled != :void and vs.Settled != :tip)
group by i.VatRate, va.Name
";
var list = _session
.CreateQuery(query)
.SetParameter("startDate", startDate)
.SetParameter("finishDate", finishDate)
.SetParameter("noCharge", SettleOption.NoCharge)
.SetParameter("unsettled", SettleOption.Unsettled)
.SetParameter("amount", SettleOption.Amount)
.SetParameter("roundoff", SettleOption.RoundOff)
.SetParameter("staff", SettleOption.Staff)
.SetParameter("void", SettleOption.Void)
.SetParameter("tip", SettleOption.Tip)
.List<object[]>();
foreach (var item in list)
outList.Add(new TaxAnalysis() { Name = string.Format("{0} - {1:#.##%;(#.##%);0%}", (string)item[0], (decimal)item[1]), TaxRate = (decimal)item[1], TaxAmount = (decimal)item[2], NetSale = (decimal)item[3] });
return outList;
}
}
}