170 lines
7.2 KiB
C#
170 lines
7.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Data.SqlClient;
|
|
using Tanshu.Accounts.Contracts;
|
|
using Tanshu.Data.DAO;
|
|
using Tanshu.Accounts.SqlDAO;
|
|
using Tanshu.Accounts.Entities;
|
|
using NHibernate.Criterion;
|
|
using Tanshu.Common;
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
{
|
|
public class SalesAnalysisBI
|
|
{
|
|
public ICollection<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)
|
|
{
|
|
var query = @"
|
|
select concat(p.Name, ' ', p.Units) as Product, Sum(i.Quantity) as Amount
|
|
from SaleVoucher v
|
|
inner join v.Inventories i
|
|
inner join i.Product p
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false and v.Settled != :noCharge
|
|
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", SettleOptionFactory.NoCharge)
|
|
.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] });
|
|
|
|
query = @"
|
|
select concat(p.Name, ' ', p.Units) as Product, Sum(i.Quantity) as Amount
|
|
from SaleVoucher v
|
|
inner join v.Inventories i
|
|
inner join i.Product p
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false and v.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", SettleOptionFactory.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] });
|
|
|
|
return outList.Values;
|
|
}
|
|
}
|
|
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)
|
|
{
|
|
var query = @"
|
|
select g.GroupType as GroupType, Sum(i.Quantity * i.Rate * (1 - i.Discount)) as Amount
|
|
from SaleVoucher v
|
|
inner join v.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 v.Settled != :noCharge
|
|
group by g.GroupType
|
|
order by g.GroupType
|
|
";
|
|
var list = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("noCharge", SettleOptionFactory.NoCharge)
|
|
.List<object[]>();
|
|
var outList = new List<SalesAnalysis>();
|
|
foreach (var item in list)
|
|
outList.Add(new SalesAnalysis() { GroupType = (string)item[0], Amount = (decimal)item[1] });
|
|
return GetSettlement(outList, startDate, finishDate);
|
|
}
|
|
}
|
|
private 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)
|
|
{
|
|
var query = @"
|
|
select s.Name, Sum(i.Quantity * i.Rate * (1 - i.Discount) * (1 + i.ServiceCharge) * (1 + i.Tax)) as Amount
|
|
from SaleVoucher v
|
|
inner join v.Inventories i
|
|
inner join v.Settled s
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false and v.Settled != :noCharge
|
|
group by s.Name
|
|
order by s.Name
|
|
";
|
|
var list = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("noCharge", SettleOptionFactory.NoCharge)
|
|
.List<object[]>();
|
|
foreach (var item in list)
|
|
outList.Add(new SalesAnalysis() { GroupType = (string)item[0], Amount = (decimal)item[1] });
|
|
return GetOtherDetails(outList, startDate, finishDate);
|
|
}
|
|
}
|
|
private 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.Rate * (1 - i.Discount) * i.ServiceCharge)
|
|
from SaleVoucher v
|
|
inner join v.Inventories i
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false and v.Settled != :noCharge
|
|
";
|
|
var amt = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("noCharge", SettleOptionFactory.NoCharge)
|
|
.UniqueResult();
|
|
outList.Add(new SalesAnalysis() { GroupType = "Service Charge", Amount = (decimal)amt });
|
|
#endregion
|
|
#region Tax
|
|
query = @"
|
|
select i.Tax, Sum(i.Quantity * i.Rate * (1 - i.Discount) * (1 + i.ServiceCharge) * i.Tax)
|
|
from SaleVoucher v
|
|
inner join v.Inventories i
|
|
where v.Date >= :startDate and v.Date <= :finishDate and v.Void = false and v.Settled != :noCharge
|
|
group by i.Tax
|
|
";
|
|
var list = session
|
|
.CreateQuery(query)
|
|
.SetParameter("startDate", startDate)
|
|
.SetParameter("finishDate", finishDate)
|
|
.SetParameter("noCharge", SettleOptionFactory.NoCharge)
|
|
.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;
|
|
}
|
|
}
|
|
}
|
|
}
|