0172fc4e01
Added Nc Option in settlement Merged Vouchers and SaleVoucher table. Need to update the Sql Schema
273 lines
13 KiB
C#
273 lines
13 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 voids, ref decimal pending, ref decimal net, ref decimal vat, ref decimal serviceTax, ref decimal nc)
|
|
{
|
|
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 v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus NOT IN (@Pending, @Void, @Nc)
|
|
AND v.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("@Nc", (int)PaidStatus.Nc);
|
|
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(i.Quantity * i.Rate), 0) AS Amount
|
|
FROM Vouchers v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus = @Nc
|
|
AND v.Date BETWEEN @StartDate AND @FinishDate
|
|
";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Nc", (int)PaidStatus.Nc);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
nc = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const string query = @"
|
|
SELECT ISNULL(SUM(i.Amount), 0) AS Amount
|
|
FROM Vouchers v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.Date BETWEEN @StartDate AND @FinishDate
|
|
AND v.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(i.Amount), 0) AS Amount
|
|
FROM Vouchers v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus = @Pending
|
|
AND v.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 v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus NOT IN (@Pending, @Void, @Nc)
|
|
AND v.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("@Nc", (int)PaidStatus.Nc);
|
|
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(i.Quantity * i.Rate * (1 - i.Discount) * i.Vat), 0) AS Amount
|
|
FROM Vouchers v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus NOT IN (@Pending, @Void, @Nc)
|
|
AND v.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("@Nc", (int)PaidStatus.Nc);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
vat = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
using (var cmd = new SqlCommand())
|
|
{
|
|
const string query = @"
|
|
SELECT ISNULL(SUM(i.Quantity * i.Rate * (1 - i.Discount) * i.ServiceTax), 0) AS Amount
|
|
FROM Vouchers v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus NOT IN (@Pending, @Void, @Nc)
|
|
AND v.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("@Nc", (int)PaidStatus.Nc);
|
|
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
|
|
serviceTax = Math.Round((decimal)connection.ExecuteScalar(cmd));
|
|
}
|
|
return list;
|
|
}
|
|
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(i.Amount) AS Amount
|
|
FROM Vouchers v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
INNER JOIN Products p ON i.ProductID = p.ProductID
|
|
INNER JOIN ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID
|
|
WHERE v.PaidStatus != @Pending AND v.PaidStatus != @Void
|
|
AND v.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 v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
INNER JOIN Products p ON i.ProductID = p.ProductID
|
|
INNER JOIN ProductGroups pg ON p.ProductGroupID = pg.ProductGroupID
|
|
WHERE v.PaidStatus != @Pending AND v.PaidStatus != @Void
|
|
AND v.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 v.PaidStatus AS Status, Sum(i.Amount) AS Amount
|
|
FROM Vouchers v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.LastEditDate BETWEEN @StartDate AND @FinishDate
|
|
GROUP BY v.PaidStatus
|
|
ORDER BY v.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 v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.LastEditDate BETWEEN @StartDate AND @FinishDate
|
|
AND v.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);
|
|
}
|
|
}
|
|
}
|
|
}
|