using System; using System.Data.SqlClient; using System.Data; using Tanshu.Accounts.Contracts; using Tanshu.Accounts.DAOFactory; using Tanshu.Data.DAO; namespace Tanshu.Accounts.SqlDAO { public class CheckoutDAO : BaseDAO, ICheckoutDAO { 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 t.Date, s.BillID, c.Name, SUM(i.Amount) AS Amount, SUM(Quantity * Rate * Discount) AS Discount FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID INNER JOIN Inventory i ON t.VoucherID = i.VoucherID INNER JOIN Customers c ON s.CustomerID = c.CustomerID WHERE t.Type = 'S' AND t.LastEditDate BETWEEN @StartDate AND @FinishDate AND t.UserID = @UserID AND s.PaidStatus = @PaidStatus GROUP BY t.Date, c.Name, s.BillID "; 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(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 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 t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID INNER JOIN Inventory i ON t.VoucherID = i.VoucherID INNER JOIN Customers c ON s.CustomerID = c.CustomerID WHERE t.Type = 'S' AND t.LastEditDate BETWEEN @StartDate AND @FinishDate AND t.UserID = @UserID AND s.PaidStatus != @Pending AND s.PaidStatus != @Void "; 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("@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 t.Date, s.BillID, c.Name, SUM(i.Amount) AS Amount, SUM(Quantity * Rate * Discount) AS Discount FROM Vouchers t INNER JOIN SaleVoucher s ON t.VoucherID = s.VoucherID INNER JOIN Inventory i ON t.VoucherID = i.VoucherID INNER JOIN Customers c ON s.CustomerID = c.CustomerID WHERE t.Type = 'S' AND t.LastEditDate BETWEEN @StartDate AND @FinishDate AND t.UserID = @UserID AND s.PaidStatus != @Pending AND s.PaidStatus != @Void GROUP BY t.Date, c.Name, s.BillID, i.Discount HAVING i.Discount >= .05 "; 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("@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 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 < @StartDate AND t.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 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.CreationDate < @StartDate AND t.LastEditDate >= @StartDate AND t.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 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 = @Void AND t.CreationDate < @StartDate AND t.LastEditDate >= @StartDate AND t.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); } } } }