narsil/Tanshu.Accounts.SqlDAO/SalesAnalysisDAO.cs
2010-03-02 23:26:21 +05:30

240 lines
11 KiB
C#

using System.Collections.Generic;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using System;
using Tanshu.Data.DAO;
using Tanshu.Accounts.DAOFactory;
namespace Tanshu.Accounts.SqlDAO
{
public class SalesAnalysisDAO : BaseDAO, ISalesAnalysisDAO
{
public SalesAnalysisDAO(IConnectionDAO connection)
: base(connection)
{ }
public List<SalesAnalysisBO> GetSalesTaxReturn(DateTime startDate, DateTime finishDate, ref decimal freeSale, ref decimal voids, ref decimal pending, ref decimal net, ref decimal tax)
{
List<SalesAnalysisBO> list = new List<SalesAnalysisBO>();
using (SqlCommand cmd = new SqlCommand())
{
string query = @"
SELECT NEWID(),
CAST(i.Tax AS nvarchar(10)) AS Section,
SUM(i.Quantity * i.Rate * (1 - i.Discount) * (i.Tax)) AS Quantity,
SUM(i.Quantity * i.Rate * (1 - i.Discount)) AS Net,
SUM(i.Amount) AS Gross
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.Date BETWEEN @StartDate AND @FinishDate
GROUP BY i.Tax
ORDER BY Section DESC;";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
list = BusinessObjectDAO<SalesAnalysisBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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.Date BETWEEN @StartDate AND @FinishDate
AND i.Amount = 0";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
freeSale = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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.Date BETWEEN @StartDate AND @FinishDate
AND s.Void = 1";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
voids = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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.Date BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
pending = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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.Date BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
net = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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.Date BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
tax = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
return list;
}
public void GetAdditionalInfo(ref decimal freeSale, ref decimal voids, ref decimal pending, ref decimal net, ref decimal tax, DateTime startDate, DateTime finishDate)
{
using (SqlCommand cmd = new SqlCommand())
{
string 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";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
freeSale = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
voids = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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;";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
pending = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
net = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
using (SqlCommand cmd = new SqlCommand())
{
string 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";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
tax = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
}
public List<SalesAnalysisDetailBO> GetSaleDetail(DateTime startDate, DateTime finishDate, Guid costCenterID)
{
using (SqlCommand cmd = new SqlCommand())
{
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;";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
cmd.Parameters.AddWithValue("@CostCenterID", costCenterID);
return BusinessObjectDAO<SalesAnalysisDetailBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
}
}
public List<SalesAnalysisBO> GetSale(DateTime startDate, DateTime finishDate)
{
using (SqlCommand cmd = new SqlCommand())
{
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;";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
List<SalesAnalysisBO> list = BusinessObjectDAO<SalesAnalysisBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
decimal gross = 0, net = 0;
foreach (SalesAnalysisBO i in list)
{
gross += i.Gross;
net += i.Net;
}
SalesAnalysisBO item = new SalesAnalysisBO
{
Gross = gross,
Net = net,
};
item.Section = string.Format("Total: {0:#,##0.00;(#,##0.00);0}", item.Gross - item.Net);
list.Add(item);
return list;
}
}
}
}