narsil/Tanshu.Accounts.SqlDAO/SalesAnalysisDAO.cs

316 lines
15 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;
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.SqlDAO
{
public class SalesAnalysisDAO : BaseDAO
2010-03-02 17:56:21 +00:00
{
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;
}
list.Add(new SalesAnalysisBO
2010-03-02 17:56:21 +00:00
{
Section = "Total",
2010-03-02 17:56:21 +00:00
Gross = gross,
Net = net
});
list.Add(new SalesAnalysisBO() { Section = " -- ", Gross = 0, Net = 0 });
list.Add(new SalesAnalysisBO
{
Section = "Tax",
Net = gross - net
});
return GetSettlement(list, startDate, finishDate);
}
}
private List<SalesAnalysisBO> GetSettlement(List<SalesAnalysisBO> outList, DateTime startDate, DateTime finishDate)
{
outList.Add(new SalesAnalysisBO() { Section = " -- ", Gross = 0, Net = 0 });
if (finishDate <= startDate)
return new List<SalesAnalysisBO>();
using (var cmd = new SqlCommand())
{
const string query = @"
SELECT s.PaidStatus AS Status, Sum(i.Amount) AS Amount
FROM Vouchers v INNER JOIN SaleVoucher s ON v.VoucherID = s.VoucherID
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
WHERE v.LastEditDate BETWEEN @StartDate AND @FinishDate
GROUP BY s.PaidStatus
ORDER BY s.PaidStatus
";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
var list = BusinessObjectDAO<SettlementReportBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
decimal amount = 0;
foreach (var item in list)
{
amount += item.Amount;
outList.Add(new SalesAnalysisBO(){ Section = item.Status.ToString() , Net = item.Amount});
}
outList.Add(new SalesAnalysisBO() { Section = "Total", Net = amount });
return outList;
}
}
public decimal GetDetail(PaidStatus paidStatus, DateTime startDate, DateTime finishDate)
{
const string query = @"
SELECT SUM(i.Amount) AS Amount, SUM(i.Quantity * i.Rate * i.Discount) AS Discount 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.PaidStatus = @PaidStatus
";
using (var cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@PaidStatus", (int)paidStatus);
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
return (decimal)connection.ExecuteScalar(cmd);
2010-03-02 17:56:21 +00:00
}
}
}
}