262 lines
13 KiB
C#
262 lines
13 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)
|
|
{
|
|
var list = new List<SalesAnalysisBO>();
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.Date BETWEEN @StartDate AND @FinishDate
|
|
GROUP BY i.Tax
|
|
ORDER BY Section DESC;";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
list = BusinessObjectDAO<SalesAnalysisBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
|
|
}
|
|
|
|
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.Date BETWEEN @StartDate AND @FinishDate
|
|
AND i.Amount = 0";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
freeSale = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus = @Void";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
voids = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus = @Pending
|
|
AND t.Date BETWEEN @StartDate AND @FinishDate";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
pending = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.Date BETWEEN @StartDate AND @FinishDate";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
net = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.Date BETWEEN @StartDate AND @FinishDate";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
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 (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
|
|
AND i.Amount = 0";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
freeSale = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus = @Void";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
voids = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus = @Pending
|
|
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
|
|
ORDER BY Amount DESC;";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
pending = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
net = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
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("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
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 (var cmd = new SqlCommand())
|
|
{
|
|
const 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.PaidStatus != @Pending AND s.PaidStatus != @Void
|
|
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
|
|
GROUP BY c.CostCenterID, c.Name
|
|
ORDER BY Net DESC;";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
var list = BusinessObjectDAO<SalesAnalysisBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
|
|
decimal gross = 0, net = 0;
|
|
foreach (var i in list)
|
|
{
|
|
gross += i.Gross;
|
|
net += i.Net;
|
|
}
|
|
var 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;
|
|
}
|
|
}
|
|
}
|
|
}
|