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
|
|
|
|
|
2016-01-04 05:22:01 +00:00
|
|
|
|
public 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
|
|
|
|
}
|
2016-01-04 05:22:01 +00:00
|
|
|
|
public 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
|
|
|
|
}
|
2016-01-04 05:22:01 +00:00
|
|
|
|
public 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;
|
|
|
|
|
}
|
2016-01-04 05:22:01 +00:00
|
|
|
|
public IList<TaxAnalysis> GetServiceTax(DateTime startDate, DateTime finishDate)
|
2014-11-11 10:23:54 +00:00
|
|
|
|
{
|
2016-01-04 05:22:01 +00:00
|
|
|
|
var outList = new List<TaxAnalysis>();
|
2014-11-11 10:23:54 +00:00
|
|
|
|
var query = @"
|
2016-01-04 05:22:01 +00:00
|
|
|
|
select i.ServiceTaxRate, coalesce(Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end) * i.ServiceTaxRate), 0),
|
|
|
|
|
coalesce(Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end)), 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)
|
2016-01-04 05:22:01 +00:00
|
|
|
|
group by i.ServiceTaxRate";
|
|
|
|
|
var list = _session
|
2014-11-11 10:23:54 +00:00
|
|
|
|
.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)
|
2016-01-04 05:22:01 +00:00
|
|
|
|
.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;
|
2014-11-11 10:23:54 +00:00
|
|
|
|
}
|
2016-01-04 05:22:01 +00:00
|
|
|
|
public IList<TaxAnalysis> GetVat(DateTime startDate, DateTime finishDate)
|
2014-11-11 10:23:54 +00:00
|
|
|
|
{
|
2016-01-04 05:22:01 +00:00
|
|
|
|
var outList = new List<TaxAnalysis>();
|
2014-11-11 10:23:54 +00:00
|
|
|
|
var query = @"
|
2016-01-04 05:22:01 +00:00
|
|
|
|
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),
|
|
|
|
|
coalesce(Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end)), 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
|
2016-01-04 05:22:01 +00:00
|
|
|
|
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)
|
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)
|
2016-01-04 05:22:01 +00:00
|
|
|
|
.SetParameter("void", SettleOption.Void)
|
|
|
|
|
.SetParameter("tip", SettleOption.Tip)
|
2014-11-11 10:23:54 +00:00
|
|
|
|
.List<object[]>();
|
|
|
|
|
foreach (var item in list)
|
2016-01-04 05:22:01 +00:00
|
|
|
|
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] });
|
2014-11-11 10:23:54 +00:00
|
|
|
|
return outList;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|