0172fc4e01
Added Nc Option in settlement Merged Vouchers and SaleVoucher table. Need to update the Sql Schema
316 lines
14 KiB
C#
316 lines
14 KiB
C#
using System;
|
|
using System.Data.SqlClient;
|
|
using System.Data;
|
|
using Tanshu.Accounts.Contracts;
|
|
using Tanshu.Data.DAO;
|
|
|
|
namespace Tanshu.Accounts.SqlDAO
|
|
{
|
|
public class CheckoutDAO : BaseDAO
|
|
{
|
|
private readonly DateTime _startDate;
|
|
private readonly DateTime _finishDate;
|
|
private readonly Guid _userID;
|
|
public CheckoutDAO(DateTime startDate, DateTime finishDate, Guid userID, IConnectionDAO connection)
|
|
: base(connection)
|
|
{
|
|
_startDate = startDate;
|
|
_finishDate = finishDate;
|
|
_userID = userID;
|
|
}
|
|
public decimal GetDetail(ref string info, PaidStatus paidStatus, string infoLine)
|
|
{
|
|
decimal amount = 0;
|
|
info = "";
|
|
|
|
const string query = @"
|
|
SELECT v.Date, v.BillID, c.Name, v.LastEditDate, 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
|
|
INNER JOIN Customers c ON v.CustomerID = c.CustomerID
|
|
WHERE v.LastEditDate BETWEEN @StartDate AND @FinishDate AND v.UserID = @UserID
|
|
AND v.PaidStatus = @PaidStatus
|
|
GROUP BY v.Date, c.Name, v.BillID, v.LastEditDate
|
|
";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@PaidStatus", (int)paidStatus);
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
|
|
using (var dr = connection.ExecuteReader(cmd))
|
|
{
|
|
if (dr.Read())
|
|
{
|
|
info = infoLine;
|
|
|
|
do
|
|
{
|
|
amount += dr.GetDecimal(4);
|
|
info += string.Format("\n\r{0:dd-MMM-yyyy HH:mm:ss} {1} {2}", dr.GetDateTime(0), dr.GetString(1), dr.GetString(2));
|
|
info += string.Format("\n\rPaid / Void Time {0:dd-MMM-yyyy HH:mm:ss}", dr.GetDateTime(3));
|
|
info += string.Format("\n\rAmount: {0:#0.00} :: Discount: {1:#0.00}", dr.GetDecimal(4), dr.GetDecimal(5));
|
|
info += "\n\r------------------------------------------";
|
|
} while (dr.Read());
|
|
}
|
|
}
|
|
}
|
|
return amount;
|
|
}
|
|
|
|
public decimal GetOpenings()
|
|
{
|
|
const string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Opening' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetReceipts()
|
|
{
|
|
const string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Receipt' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetPayments()
|
|
{
|
|
const string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Payment' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetAdditionalVoids()
|
|
{
|
|
const string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Additional Void' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetRetainedOvernight()
|
|
{
|
|
const string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Retained at Night' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetAdvancesReceived()
|
|
{
|
|
const string query = "SELECT ISNULL(SUM(Amount), 0) FROM Advances WHERE DateIn BETWEEN @StartDate AND @FinishDate AND CashierIn = @UserID";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetAdvancesAdjusted()
|
|
{
|
|
const string query = "SELECT ISNULL(SUM(Amount), 0) FROM Advances WHERE DateOut BETWEEN @StartDate AND @FinishDate AND CashierOut = @UserID";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public string GetPaymentString()
|
|
{
|
|
var paymentString = "";
|
|
const string query = "SELECT Date, Type, Amount, Narration FROM Payments WHERE Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID ORDER BY Type";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
using (IDataReader dr = connection.ExecuteReader(cmd))
|
|
{
|
|
string currentType = "";
|
|
while (dr.Read())
|
|
{
|
|
if (currentType != dr.GetString(1))
|
|
{
|
|
paymentString += string.Format("\n\r--- {0,-30} -------", dr.GetString(1));
|
|
currentType = dr.GetString(1);
|
|
}
|
|
paymentString += string.Format("\n\r{0:HH:mm} => {1:#0.00} => {2}", dr.GetDateTime(0), dr.GetDecimal(2), dr.GetString(3));
|
|
}
|
|
}
|
|
}
|
|
return paymentString;
|
|
}
|
|
public string GetActiveCashiers()
|
|
{
|
|
string cashiers = "";
|
|
string query = @"
|
|
SELECT Name FROM Users WHERE UserID IN
|
|
(
|
|
SELECT DISTINCT UserID FROM Vouchers WHERE Date BETWEEN @StartDate AND @FinishDate
|
|
UNION
|
|
SELECT DISTINCT CashierID FROM Payments WHERE Date BETWEEN @StartDate AND @FinishDate
|
|
UNION
|
|
SELECT DISTINCT CashierIn FROM Advances WHERE DateIn BETWEEN @StartDate AND @FinishDate
|
|
UNION
|
|
SELECT DISTINCT CashierOut FROM Advances WHERE DateOut BETWEEN @StartDate AND @FinishDate
|
|
)";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
|
|
using (IDataReader dr = connection.ExecuteReader(cmd))
|
|
{
|
|
while (dr.Read())
|
|
{
|
|
cashiers += string.Format("{0}, ", dr.GetString(0));
|
|
}
|
|
}
|
|
}
|
|
return cashiers;
|
|
}
|
|
public decimal GetNetSales()
|
|
{
|
|
const string query = @"
|
|
SELECT ISNULL(SUM(i.Amount), 0) AS Amount FROM Vouchers v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
INNER JOIN Customers c ON v.CustomerID = c.CustomerID
|
|
WHERE v.LastEditDate BETWEEN @StartDate AND @FinishDate AND v.UserID = @UserID
|
|
AND v.PaidStatus NOT IN (@Pending, @Void, @Nc)
|
|
";
|
|
using (var cmd = new SqlCommand(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);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetDiscountsBills(ref string info)
|
|
{
|
|
decimal amount = 0;
|
|
info = "";
|
|
|
|
const string query = @"
|
|
SELECT v.Date, v.BillID, c.Name, 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
|
|
INNER JOIN Customers c ON v.CustomerID = c.CustomerID
|
|
WHERE v.VoucherID IN (
|
|
SELECT v.VoucherID FROM Vouchers v INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.LastEditDate BETWEEN @StartDate AND @FinishDate AND v.UserID = @UserID
|
|
AND v.PaidStatus != @Void
|
|
AND i.Discount >= .05
|
|
)
|
|
GROUP BY v.Date, v.BillID, c.Name
|
|
";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@FinishDate", _finishDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
using (IDataReader dr = connection.ExecuteReader(cmd))
|
|
{
|
|
if (dr.Read())
|
|
{
|
|
info = "\n\r--- High Discounts Bills -----------------";
|
|
|
|
do
|
|
{
|
|
amount += dr.GetDecimal(3);
|
|
info += string.Format("\n\r{0:dd-MMM-yyyy HH:mm:ss} {1} {2}", dr.GetDateTime(0), dr.GetString(1), dr.GetString(2));
|
|
info += string.Format("\n\rAmount: {0:#0.00} :: Discount: {1:#0.00}", dr.GetDecimal(3), dr.GetDecimal(4));
|
|
info += "\n\r------------------------------------------";
|
|
} while (dr.Read());
|
|
}
|
|
}
|
|
}
|
|
return amount;
|
|
}
|
|
|
|
public decimal GetOldPending()
|
|
{
|
|
const string query = @"
|
|
SELECT ISNULL(SUM(i.Amount), 0) FROM Vouchers v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus = @Pending
|
|
AND v.LastEditDate < @StartDate AND v.UserID = @UserID
|
|
";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetOldReceipts()
|
|
{
|
|
const string query = @"
|
|
SELECT ISNULL(SUM(i.Amount), 0) FROM Vouchers v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus != @Pending AND v.PaidStatus != @Void
|
|
AND v.CreationDate < @StartDate AND v.LastEditDate >= @StartDate AND v.UserID = @UserID
|
|
";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@Pending", (int)PaidStatus.Pending);
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
|
|
public decimal GetOldVoided()
|
|
{
|
|
const string query = @"
|
|
SELECT ISNULL(SUM(i.Amount), 0) FROM Vouchers v
|
|
INNER JOIN Inventory i ON v.VoucherID = i.VoucherID
|
|
WHERE v.PaidStatus = @Void
|
|
AND v.CreationDate < @StartDate AND v.LastEditDate >= @StartDate AND v.UserID = @UserID
|
|
";
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@Void", (int)PaidStatus.Void);
|
|
cmd.Parameters.AddWithValue("@StartDate", _startDate);
|
|
cmd.Parameters.AddWithValue("@UserID", _userID);
|
|
return (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
}
|
|
}
|
|
}
|