374 lines
17 KiB
C#
374 lines
17 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;
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
{
|
|
public class SalesAnalysisBI
|
|
{
|
|
public IList<SalesAnalysisDetail> GetSaleDetail(DateTime startDate, DateTime finishDate)
|
|
{
|
|
startDate = startDate.Date.AddHours(6);
|
|
finishDate = finishDate.Date.AddDays(1).AddHours(5);
|
|
if (finishDate <= startDate)
|
|
return new List<SalesAnalysisDetail>();
|
|
|
|
using (var session = SessionManager.Session)
|
|
{
|
|
#region Sale
|
|
var query = @"
|
|
select concat(p.Name, ' ', p.Units) as Product, Sum(i.Quantity) as Amount
|
|
from Voucher v
|
|
inner join v.Kots k
|
|
inner join k.Inventories i
|
|
inner join i.Product p
|
|
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 concat(p.Name, ' ', p.Units), p.ProductGroup
|
|
order by p.ProductGroup, concat(p.Name, ' ', p.Units)
|
|
";
|
|
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 OrderedDictionary<string, SalesAnalysisDetail>();
|
|
foreach (var item in list)
|
|
outList.Add((string)item[0], new SalesAnalysisDetail() { Product = (string)item[0], Sale = (decimal)item[1] });
|
|
#endregion
|
|
#region NC
|
|
query = @"
|
|
select concat(p.Name, ' ', p.Units) as Product, Sum(i.Quantity) as Amount
|
|
from Voucher v
|
|
inner join v.Kots k
|
|
inner join k.Inventories i
|
|
inner join i.Product p
|
|
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)
|
|
group by concat(p.Name, ' ', p.Units), p.ProductGroup
|
|
order by p.ProductGroup, concat(p.Name, ' ', p.Units)
|
|
";
|
|
list = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("noCharge", SettleOption.NoCharge)
|
|
.List<object[]>();
|
|
foreach (var item in list)
|
|
if (outList.ContainsKey((string)item[0]))
|
|
outList[(string)item[0]].NC = (decimal)item[1];
|
|
else
|
|
outList.Add((string)item[0], new SalesAnalysisDetail() { Product = (string)item[0], NC = (decimal)item[1] });
|
|
#endregion
|
|
#region Staff
|
|
query = @"
|
|
select concat(p.Name, ' ', p.Units) as Product, Sum(i.Quantity) as Amount
|
|
from Voucher v
|
|
inner join v.Kots k
|
|
inner join k.Inventories i
|
|
inner join i.Product p
|
|
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 = :staff)
|
|
group by concat(p.Name, ' ', p.Units), p.ProductGroup
|
|
order by p.ProductGroup, concat(p.Name, ' ', p.Units)
|
|
";
|
|
list = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("staff", SettleOption.Staff)
|
|
.List<object[]>();
|
|
foreach (var item in list)
|
|
if (outList.ContainsKey((string)item[0]))
|
|
outList[(string)item[0]].Staff = (decimal)item[1];
|
|
else
|
|
outList.Add((string)item[0], new SalesAnalysisDetail() { Product = (string)item[0], Staff = (decimal)item[1] });
|
|
#endregion
|
|
return outList.Values.ToList();
|
|
}
|
|
}
|
|
public IList<SalesAnalysis> GetSale(DateTime startDate, DateTime finishDate)
|
|
{
|
|
startDate = startDate.Date.AddHours(6);
|
|
finishDate = finishDate.Date.AddDays(1).AddHours(5);
|
|
if (finishDate <= startDate)
|
|
return new List<SalesAnalysis>();
|
|
using (var session = SessionManager.Session)
|
|
{
|
|
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<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 GetSettlement(outList, startDate, finishDate);
|
|
}
|
|
}
|
|
public IList<BillDetail> GetBillDetails(DateTime startDate, DateTime finishDate)
|
|
{
|
|
startDate = startDate.Date.AddHours(6);
|
|
finishDate = finishDate.Date.AddDays(1).AddHours(5);
|
|
if (finishDate <= startDate)
|
|
return new List<BillDetail>();
|
|
using (var session = SessionManager.Session)
|
|
{
|
|
const string query = @"
|
|
select v.Date, v.BillID, s.Settled, s.Amount, v.Void, v.VoidReason
|
|
from Voucher v
|
|
inner join v.Settlements s
|
|
where v.Date >= :startDate and v.Date <= :finishDate
|
|
order by v.BillID, s.Settled
|
|
";
|
|
var list = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.List<object[]>();
|
|
var outList = new List<BillDetail>();
|
|
foreach (var item in list)
|
|
{
|
|
var settlement = ((SettleOption)item[2]).Display();
|
|
if ((bool)item[4])
|
|
settlement = string.Format("Void: {0}", (string)item[5]);
|
|
outList.Add(new BillDetail()
|
|
{
|
|
Date = (DateTime)item[0],
|
|
BillID = (string)item[1],
|
|
Settlement = settlement,
|
|
Amount = Math.Round((decimal)item[3],2)
|
|
});
|
|
}
|
|
return outList;
|
|
}
|
|
}
|
|
public IList<SalesAnalysis> DiscountReport(DateTime startDate, DateTime finishDate)
|
|
{
|
|
startDate = startDate.Date.AddHours(6);
|
|
finishDate = finishDate.Date.AddDays(1).AddHours(5);
|
|
if (finishDate <= startDate)
|
|
return new List<SalesAnalysis>();
|
|
using (var session = SessionManager.Session)
|
|
{
|
|
const string query = @"
|
|
select pg.GroupType, sum(i.Quantity * i.Price * i.Discount)
|
|
from Inventory i
|
|
inner join i.Kot k
|
|
inner join k.Voucher v
|
|
inner join i.Product p
|
|
inner join p.ProductGroup pg
|
|
where v.Date >= :startDate and v.Date <= :finishDate and i.Discount != 0
|
|
and v not in (select Voucher from VoucherSettlement vs where vs.Voucher = v and (vs.Settled = :noCharge or vs.Settled = :staff))
|
|
group by pg.GroupType
|
|
order by pg.GroupType
|
|
";
|
|
var list = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("noCharge", SettleOption.NoCharge)
|
|
.SetParameter("staff", SettleOption.Staff)
|
|
.List<object[]>();
|
|
return list.Select(item => new SalesAnalysis()
|
|
{
|
|
GroupType = (string)item[0],
|
|
Amount = (decimal)item[1]
|
|
}).ToList();
|
|
}
|
|
}
|
|
public IList<BillDetail> VoidOrReprintedBillsList(DateTime startDate, DateTime finishDate)
|
|
{
|
|
startDate = startDate.Date.AddHours(6);
|
|
finishDate = finishDate.Date.AddDays(1).AddHours(5);
|
|
if (finishDate <= startDate)
|
|
return new List<BillDetail>();
|
|
using (var session = SessionManager.Session)
|
|
{
|
|
const string query = @"
|
|
select v.Date, v.BillID, s.Amount, v.VoidReason
|
|
from Voucher v
|
|
inner join v.Settlements s
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = true and s.Settled = :settled
|
|
order by v.BillID, s.Settled
|
|
";
|
|
var listVoids = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("settled", SettleOption.Amount)
|
|
.List<object[]>();
|
|
var outList = new List<BillDetail>();
|
|
foreach (var item in listVoids)
|
|
{
|
|
outList.Add(new BillDetail()
|
|
{
|
|
Date = (DateTime)item[0],
|
|
BillID = (string)item[1],
|
|
Settlement = string.Format("Void: {0}", (string)item[3]),
|
|
Amount = (decimal)item[2] * -1
|
|
});
|
|
}
|
|
var listReprint = session.QueryOver<Reprint>()
|
|
.Where(x => x.Date >= startDate && x.Date <= finishDate)
|
|
.List();
|
|
foreach (var item in listReprint)
|
|
{
|
|
outList.Add(new BillDetail()
|
|
{
|
|
Date = item.Date,
|
|
BillID = item.Voucher.BillID,
|
|
Settlement = string.Format("Reprinted by {0}", item.User.Name),
|
|
Amount = item.Voucher.Settlements.Single(x => x.Settled == SettleOption.Amount).Amount * -1
|
|
});
|
|
}
|
|
|
|
return outList;
|
|
}
|
|
}
|
|
|
|
private static IList<SalesAnalysis> GetSettlement(IList<SalesAnalysis> outList, DateTime startDate, DateTime finishDate)
|
|
{
|
|
outList.Add(new SalesAnalysis() { GroupType = " -- ", Amount = 0 });
|
|
if (finishDate <= startDate)
|
|
return new List<SalesAnalysis>();
|
|
using (var session = SessionManager.Session)
|
|
{
|
|
//select v.Settled, Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + i.ServiceCharge) * (1 + i.Tax)) as Amount
|
|
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[]>();
|
|
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 GetOtherDetails(outList, startDate, finishDate);
|
|
}
|
|
}
|
|
private static IList<SalesAnalysis> GetOtherDetails(IList<SalesAnalysis> outList, DateTime startDate, DateTime finishDate)
|
|
{
|
|
outList.Add(new SalesAnalysis() { GroupType = " -- ", Amount = 0 });
|
|
if (finishDate <= startDate)
|
|
return new List<SalesAnalysis>();
|
|
using (var session = SessionManager.Session)
|
|
{
|
|
#region Service Charge
|
|
var query = @"
|
|
select Sum(i.Quantity * i.Price * (1 - i.Discount) * i.ServiceCharge)
|
|
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() ?? 0M;
|
|
outList.Add(new SalesAnalysis() { GroupType = "Service Charge", Amount = (decimal)amt });
|
|
#endregion
|
|
#region Service Tax
|
|
query = @"
|
|
select Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end) * i.ServiceTax)
|
|
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)
|
|
";
|
|
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() ?? 0M;
|
|
outList.Add(new SalesAnalysis() { GroupType = "Service Tax", Amount = (decimal)amt });
|
|
#endregion
|
|
#region Vat
|
|
query = @"
|
|
select i.Vat, Sum(i.Quantity * i.Price * (1 - i.Discount) * (1 + case when i.IsScTaxable then i.ServiceCharge else 0 end) * i.Vat)
|
|
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.Vat
|
|
";
|
|
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 SalesAnalysis() { GroupType = string.Format("Tax {0:P}", (decimal)item[0]), Amount = (decimal)item[1] });
|
|
#endregion
|
|
return outList;
|
|
}
|
|
}
|
|
}
|
|
}
|