narsil/Tanshu.Accounts.SqlDAO/SalesAnalysisDAO.cs

262 lines
13 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
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)
{
2011-12-05 09:23:02 +00:00
var list = new List<SalesAnalysisBO>();
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 'S' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.Date BETWEEN @StartDate AND @FinishDate
GROUP BY i.Tax
ORDER BY Section DESC;";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
list = BusinessObjectDAO<SalesAnalysisBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.Date BETWEEN @StartDate AND @FinishDate
AND i.Amount = 0";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
freeSale = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
AND s.PaidStatus = @Void";
2010-03-02 17:56:21 +00:00
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
voids = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus = @Pending
2010-03-02 17:56:21 +00:00
AND t.Date BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
pending = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.Date BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
net = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.Date BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
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)
{
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
AND i.Amount = 0";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
freeSale = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
AND s.PaidStatus = @Void";
2010-03-02 17:56:21 +00:00
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
voids = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus = @Pending
2010-03-02 17:56:21 +00:00
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
ORDER BY Amount DESC;";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
pending = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
net = Math.Round((decimal)connection.ExecuteScalar(cmd));
}
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate AND l.CostCenterID = @CostCenterID
GROUP BY p.Name, pt.Name
ORDER BY Amount DESC;";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
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)
{
2011-12-05 09:23:02 +00:00
using (var cmd = new SqlCommand())
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @"
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
WHERE t.Type = 's' AND s.PaidStatus != @Pending AND s.PaidStatus != @Void
2010-03-02 17:56:21 +00:00
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
GROUP BY c.CostCenterID, c.Name
ORDER BY Net DESC;";
cmd.CommandText = query;
2011-12-05 09:23:02 +00:00
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
2011-12-05 09:23:02 +00:00
var list = BusinessObjectDAO<SalesAnalysisBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
2010-03-02 17:56:21 +00:00
decimal gross = 0, net = 0;
2011-12-05 09:23:02 +00:00
foreach (var i in list)
2010-03-02 17:56:21 +00:00
{
gross += i.Gross;
net += i.Net;
}
2011-12-05 09:23:02 +00:00
var item = new SalesAnalysisBO
2010-03-02 17:56:21 +00:00
{
Gross = gross,
Net = net,
};
item.Section = string.Format("Total: {0:#,##0.00;(#,##0.00);0}", item.Gross - item.Net);
list.Add(item);
return list;
}
}
}
}