2011-01-30 07:14:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Tanshu.Accounts.Contracts;
|
|
|
|
|
using Tanshu.Accounts.Entities;
|
2011-02-09 12:03:22 +00:00
|
|
|
|
using Tanshu.Common;
|
2011-06-23 12:47:48 +00:00
|
|
|
|
using Tanshu.Common.Helpers;
|
2011-08-28 12:17:15 +00:00
|
|
|
|
using System.Linq;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
using NHibernate.Transform;
|
|
|
|
|
using NHibernate.Criterion;
|
2014-11-11 10:23:54 +00:00
|
|
|
|
using NHibernate;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
|
|
|
|
public class SalesAnalysisBI
|
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
protected readonly ISession _session;
|
|
|
|
|
public SalesAnalysisBI()
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
_session = SessionManager.Session;
|
|
|
|
|
}
|
2011-02-09 12:03:22 +00:00
|
|
|
|
|
2014-11-11 10:23:54 +00:00
|
|
|
|
public IList<SalesAnalysis> GetSaleAnalysis(DateTime startDate, DateTime finishDate)
|
|
|
|
|
{
|
|
|
|
|
var start = startDate.Date.AddHours(6);
|
|
|
|
|
var finish = finishDate.Date.AddDays(1).AddHours(5);
|
|
|
|
|
List<SalesAnalysis> list = new List<SalesAnalysis>();
|
|
|
|
|
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;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
2014-11-11 10:23:54 +00:00
|
|
|
|
private IList<SalesAnalysis> GetSale(DateTime startDate, DateTime finishDate)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
const string query = @"
|
2011-08-28 12:17:15 +00:00
|
|
|
|
select g.GroupType as GroupType, Sum(i.Quantity * i.Price * (1 - i.Discount)) as Amount
|
2011-02-18 16:54:48 +00:00
|
|
|
|
from Voucher v
|
|
|
|
|
inner join v.Kots k
|
|
|
|
|
inner join k.Inventories i
|
2011-02-09 12:03:22 +00:00
|
|
|
|
inner join i.Product p
|
|
|
|
|
inner join p.ProductGroup g
|
2011-03-11 18:49:48 +00:00
|
|
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false
|
|
|
|
|
and exists (select Voucher from VoucherSettlement vs where vs.Voucher = v
|
2011-12-05 09:41:02 +00:00
|
|
|
|
and vs.Settled != :noCharge and vs.Settled != :unsettled and vs.Settled != :amount and vs.Settled != :roundoff and vs.Settled != :staff)
|
2011-02-09 12:03:22 +00:00
|
|
|
|
group by g.GroupType
|
|
|
|
|
order by g.GroupType
|
|
|
|
|
";
|
2014-11-11 10:23:54 +00:00
|
|
|
|
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)
|
2011-08-28 12:17:15 +00:00
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
amount += (decimal)item[1];
|
|
|
|
|
outList.Add(new SalesAnalysis() { GroupType = (string)item[0], Amount = (decimal)item[1] });
|
2011-08-28 12:17:15 +00:00
|
|
|
|
}
|
2014-11-11 10:23:54 +00:00
|
|
|
|
outList.Add(new SalesAnalysis() { GroupType = "Total Settled", Amount = amount });
|
|
|
|
|
return outList;
|
2011-08-28 12:17:15 +00:00
|
|
|
|
}
|
2014-11-11 10:23:54 +00:00
|
|
|
|
private IList<SalesAnalysis> GetSettlements(DateTime startDate, DateTime finishDate)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
const string query = @"
|
2011-03-11 18:49:48 +00:00
|
|
|
|
select s.Settled, Sum(s.Amount)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
from Voucher v
|
2011-03-11 18:49:48 +00:00
|
|
|
|
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
|
2011-02-09 12:03:22 +00:00
|
|
|
|
";
|
2014-11-11 10:23:54 +00:00
|
|
|
|
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] });
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
2014-11-11 10:23:54 +00:00
|
|
|
|
outList.Add(new SalesAnalysis() { GroupType = "Total", Amount = amount });
|
|
|
|
|
return outList;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
2014-11-11 10:23:54 +00:00
|
|
|
|
private SalesAnalysis GetServiceCharge(DateTime startDate, DateTime finishDate)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
var query = @"
|
|
|
|
|
select coalesce(Sum(i.Quantity * i.Price * (1 - i.Discount) * i.ServiceCharge), 0)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
from Voucher v
|
|
|
|
|
inner join v.Kots k
|
|
|
|
|
inner join k.Inventories i
|
2011-03-11 18:49:48 +00:00
|
|
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false
|
|
|
|
|
and exists (select Voucher from VoucherSettlement vs where vs.Voucher = v
|
2011-12-05 09:41:02 +00:00
|
|
|
|
and vs.Settled != :noCharge and vs.Settled != :unsettled and vs.Settled != :amount and vs.Settled != :roundoff and vs.Settled != :staff)
|
2011-02-09 12:03:22 +00:00
|
|
|
|
";
|
2014-11-11 10:23:54 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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)
|
2011-02-18 16:54:48 +00:00
|
|
|
|
from Voucher v
|
|
|
|
|
inner join v.Kots k
|
|
|
|
|
inner join k.Inventories i
|
2011-03-11 18:49:48 +00:00
|
|
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false
|
|
|
|
|
and exists (select Voucher from VoucherSettlement vs where vs.Voucher = v
|
2011-12-05 09:41:02 +00:00
|
|
|
|
and vs.Settled != :noCharge and vs.Settled != :unsettled and vs.Settled != :amount and vs.Settled != :roundoff and vs.Settled != :staff)
|
2012-04-08 12:28:15 +00:00
|
|
|
|
";
|
2014-11-11 10:23:54 +00:00
|
|
|
|
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 Tax", Amount = amt };
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
private IList<SalesAnalysis> GetVat(DateTime startDate, DateTime finishDate)
|
|
|
|
|
{
|
|
|
|
|
var outList = new List<SalesAnalysis>();
|
|
|
|
|
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)
|
2012-04-08 12:28:15 +00:00
|
|
|
|
from Voucher v
|
|
|
|
|
inner join v.Kots k
|
|
|
|
|
inner join k.Inventories i
|
2014-11-11 10:23:54 +00:00
|
|
|
|
inner join i.Vat va
|
2012-04-08 12:28:15 +00:00
|
|
|
|
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)
|
2014-11-11 10:23:54 +00:00
|
|
|
|
group by i.VatRate, va.Name
|
2011-02-09 12:03:22 +00:00
|
|
|
|
";
|
2014-11-11 10:23:54 +00:00
|
|
|
|
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)
|
|
|
|
|
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;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|