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 GetSaleAnalysis(DateTime startDate, DateTime finishDate) { var start = startDate.Date.AddHours(6); var finish = finishDate.Date.AddDays(1).AddHours(5); List list = new List(); if (finish <= start) return list; list.AddRange(GetSale(start, finish)); list.Add(new SalesAnalysis() { GroupType = " -- ", Amount = 0 }); list.AddRange(GetSettlements(start, finish)); list.Add(new SalesAnalysis() { GroupType = " -- ", Amount = 0 }); var sc = GetServiceCharge(start, finish); if (sc != null) list.Add(sc); var st = GetServiceTax(start, finish); if (st != null) list.Add(st); list.AddRange(GetVat(start, finish)); return list; } private IList GetSale(DateTime startDate, DateTime finishDate) { const string query = @" select g.GroupType as GroupType, Sum(i.Quantity * i.Price * (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(); var outList = new List(); 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; } private IList 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(); var outList = new List(); 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; } private SalesAnalysis GetServiceCharge(DateTime startDate, DateTime finishDate) { var query = @" select coalesce(Sum(i.Quantity * i.Price * (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(); if (amt != 0) return new SalesAnalysis() { GroupType = "Service Charge", Amount = amt }; return null; } private SalesAnalysis GetServiceTax(DateTime startDate, DateTime finishDate) { var query = @" select coalesce(Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end) * i.ServiceTaxRate), 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(); if (amt != 0) return new SalesAnalysis() { GroupType = "Service Tax", Amount = amt }; return null; } private IList GetVat(DateTime startDate, DateTime finishDate) { var outList = new List(); var query = @" select va.Name, i.VatRate, coalesce(Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end) * i.VatRate), 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) 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) .List(); foreach (var item in list) if ((decimal)item[2] != 0) outList.Add(new SalesAnalysis() { GroupType = string.Format("{0} - {1:#.##%}", (string)item[0], (decimal)item[1]), Amount = (decimal)item[2] }); return outList; } } }