narsil/Tanshu.Accounts.SqlDAO/BusinessLayer/SalesAnalysisBI.cs
2011-02-01 02:03:22 +05:30

176 lines
7.6 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;
namespace Tanshu.Accounts.Repository
{
public class SalesAnalysisBI
{
public IList<SalesAnalysisDetail> GetSaleDetail(DateTime startDate, DateTime finishDate, int costCenterID)
{
string query = @"
SELECT p.Name AS Product, pt.Name AS Section, SUM(i.Quantity) AS Quantity, SUM(Amount) AS Amount
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
INNER JOIN Products p ON i.ProductID = p.ProductID
INNER JOIN ProductTypes pt ON p.ProductTypeID = pt.ProductTypeID
INNER JOIN Ledgers l ON p.SaleLedgerID = l.LedgerID
WHERE t.Type = 'S' AND s.Paid = 1 AND s.Void = 0
AND t.LastEditDate BETWEEN :StartDate AND :FinishDate AND l.CostCenterID = :CostCenterID
GROUP BY p.Name, pt.Name
ORDER BY Amount DESC;";
using (var session = SessionManager.Session)
{
return session
.CreateSQLQuery(query)
.AddEntity(typeof(SalesAnalysisDetail))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.SetInt32("CostCenterID", costCenterID)
.List<SalesAnalysisDetail>();
}
}
public IList<SalesAnalysis> GetSale(DateTime startDate, DateTime finishDate)
{
using (var session = SessionManager.Session)
{
var temp = from sal in session.QueryOver<SaleVoucher>()
where sal.Date >= startDate && sal.Date <= finishDate
&& sal.Settled != SettleOptionFactory.ONoCharge && sal.Void == false
select sal.Inventories;
string query = @"
SELECT c.CostCenterID AS TypeID, c.Name AS Section, SUM(i.Quantity) AS Quantity, SUM(Amount) AS Gross, SUM(i.Quantity * i.Rate * (1 - Discount)) AS Net
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
INNER JOIN Products p ON i.ProductID = p.ProductID
INNER JOIN Ledgers l ON p.SaleLedgerID = l.LedgerID
INNER JOIN CostCenters c ON l.CostCenterID = c.CostCenterID
WHERE t.Type = 's' AND s.Paid = 1 AND s.Void = 0
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
GROUP BY c.CostCenterID, c.Name
ORDER BY Net DESC;";
return session
.CreateSQLQuery(query)
.AddEntity(typeof(SalesAnalysis))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.List<SalesAnalysis>();
}
}
public void GetAdditionalInfo(ref decimal freeSale, ref decimal voids, ref decimal pending, ref decimal net, ref decimal tax, DateTime startDate, DateTime finishDate)
{
string query;
using (var session = SessionManager.Session)
{
query = @"
SELECT ISNULL(SUM(Quantity * Rate), 0) AS Amount
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
WHERE t.Type = 's' AND s.Paid = 1 AND s.Void = 0
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
AND i.Amount = 0";
freeSale = session
.CreateSQLQuery(query)
.AddEntity(typeof(decimal))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.UniqueResult<decimal>();
freeSale = Math.Round(freeSale);
query = @"
SELECT ISNULL(SUM(Amount), 0) AS Amount
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
WHERE t.Type = 's'
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
AND s.Void = 1";
voids = session
.CreateSQLQuery(query)
.AddEntity(typeof(decimal))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.UniqueResult<decimal>();
voids = Math.Round(voids);
query = @"
SELECT ISNULL(SUM(Amount), 0) AS Amount
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
WHERE t.Type = 's' AND s.Paid = 0 AND s.Void = 0
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
ORDER BY Amount DESC;";
pending = session
.CreateSQLQuery(query)
.AddEntity(typeof(decimal))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.UniqueResult<decimal>();
pending = Math.Round(pending);
query = @"
SELECT ISNULL(SUM(Quantity * Rate * (1 - Discount )), 0) AS Amount
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
WHERE t.Type = 's' AND s.Paid = 1 AND s.Void = 0
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate";
net = session
.CreateSQLQuery(query)
.AddEntity(typeof(decimal))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.UniqueResult<decimal>();
net = Math.Round(net);
query = @"
SELECT ISNULL(SUM(Amount), 0) AS Amount
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
WHERE t.Type = 's' AND s.Paid = 1 AND s.Void = 0
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate";
tax = session
.CreateSQLQuery(query)
.AddEntity(typeof(decimal))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.UniqueResult<decimal>();
tax = Math.Round(tax);
}
}
public IList<SalesAnalysis> GetSalesTaxReturn(DateTime startDate, DateTime finishDate, ref decimal freeSale, ref decimal voids, ref decimal pending, ref decimal net, ref decimal tax)
{
IList<SalesAnalysis> list = new List<SalesAnalysis>();
string query;
using (var session = SessionManager.Session)
{
query = @"
SELECT p.Name AS Product, pt.Name AS Section, SUM(i.Quantity) AS Quantity, SUM(Amount) AS Amount
FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN Inventory i ON t.VoucherID = i.VoucherID
INNER JOIN Products p ON i.ProductID = p.ProductID
INNER JOIN ProductTypes pt ON p.ProductTypeID = pt.ProductTypeID
INNER JOIN Ledgers l ON p.SaleLedgerID = l.LedgerID
WHERE t.Type = 'S' AND s.Paid = 1 AND s.Void = 0
AND t.LastEditDate BETWEEN :StartDate AND :FinishDate AND l.CostCenterID = :CostCenterID
GROUP BY p.Name, pt.Name
ORDER BY Amount DESC;";
list = session
.CreateSQLQuery(query)
.AddEntity(typeof(SalesAnalysis))
.SetDateTime("StartDate", startDate)
.SetDateTime("FinishDate", finishDate)
.List<SalesAnalysis>();
}
GetAdditionalInfo(ref freeSale, ref voids, ref pending, ref net, ref tax, startDate, finishDate);
return list;
}
}
}