narsil/Tanshu.Accounts.SqlDAO/SalesAnalysisDAO.cs

281 lines
14 KiB
C#

using System.Collections.Generic;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using System;
using Tanshu.Data.DAO;
namespace Tanshu.Accounts.SqlDAO
{
public class SalesAnalysisDAO : BaseDAO
{
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 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 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.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 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 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 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 decimal GetFreeSale(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 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);
return Math.Round((decimal)connection.ExecuteScalar(cmd));
}
}
public List<SalesAnalysisDetailBO> GetSaleDetail(DateTime startDate, DateTime finishDate, Guid productGroupID)
{
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 ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID
WHERE s.PaidStatus != @Pending AND s.PaidStatus != @Void
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate AND p.ProductGroupID = @ProductGroupID
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("@ProductGroupID", productGroupID);
return BusinessObjectDAO<SalesAnalysisDetailBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
}
}
public List<SalesAnalysisBO> GetSale(DateTime startDate, DateTime finishDate)
{
using (var cmd = new SqlCommand())
{
const string query = @"
SELECT pg.ProductGroupID, pg.Name, SUM(i.Quantity * i.Rate * (1 - i.Discount)) AS Net, SUM(i.Quantity * i.Rate * (1 - i.Discount) * i.Vat) AS Vat, SUM(i.Quantity * i.Rate * (1 - i.Discount) * i.ServiceTax) AS ServiceTax
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 ProductGroups pg ON p.ProductGroupID = pg.ProductGroupID
WHERE s.PaidStatus != @Pending AND s.PaidStatus != @Void
AND t.LastEditDate BETWEEN @StartDate AND @FinishDate
GROUP BY pg.ProductGroupID, pg.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 net = 0, vat = 0, serviceTax = 0;
foreach (var i in list)
{
net += i.Net;
vat += i.Vat;
serviceTax += i.ServiceTax;
}
list.Add(new SalesAnalysisBO
{
Name = "Total",
Net = net,
Vat = vat,
ServiceTax = serviceTax
});
list.Add(new SalesAnalysisBO() { Name = " -- " });
return GetSettlement(list, startDate, finishDate);
}
}
private List<SalesAnalysisBO> GetSettlement(List<SalesAnalysisBO> outList, DateTime startDate, DateTime finishDate)
{
outList.Add(new SalesAnalysisBO() { Name = " -- " });
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() { Name = item.Status.ToString(), Net = item.Amount });
}
outList.Add(new SalesAnalysisBO() { Name = "Total", Net = amount });
return GetAdvances(outList, startDate, finishDate);
}
}
private List<SalesAnalysisBO> GetAdvances(List<SalesAnalysisBO> outList, DateTime startDate, DateTime finishDate)
{
outList.Add(new SalesAnalysisBO() { Name = " -- " });
if (finishDate <= startDate)
return new List<SalesAnalysisBO>();
using (var cmd = new SqlCommand())
{
string query = "SELECT a.*, ui.Name AS Cashier, ISNULL(uo.Name, '') AS UserOut FROM Advances a INNER JOIN Users ui ON a.CashierIn = ui.UserID LEFT OUTER JOIN Users uo ON a.CashierOut = uo.UserID WHERE DateIn BETWEEN @StartDate AND @FinishDate OR (DateOut is not null AND DateOut BETWEEN @StartDate AND @FinishDate)";
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
var list = BusinessObjectDAO<AdvanceDisplayBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
decimal amount = 0;
foreach (var item in list)
{
if (item.DateIn >= startDate && item.DateIn <= finishDate)
{
amount += item.Amount;
outList.Add(new SalesAnalysisBO() { Name = string.Format("Advance In {0}", item.Cashier), Net = item.Amount });
}
if (item.DateOut.HasValue && item.DateOut >= startDate && item.DateOut <= finishDate)
{
amount -= item.Amount;
outList.Add(new SalesAnalysisBO() { Name = string.Format("Advance Out {0}", item.UserOut), Net = item.Amount });
}
}
outList.Add(new SalesAnalysisBO() { Name = "Total Advance Taken", 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.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);
}
}
}
}