narsil/Tanshu.Accounts.SqlDAO/CheckoutDAO.cs
2010-03-02 23:26:21 +05:30

378 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using System.Data;
using Tanshu.Accounts.DAOFactory;
using Tanshu.Data.DAO;
namespace Tanshu.Accounts.SqlDAO
{
public class CheckoutDAO : BaseDAO, ICheckoutDAO
{
private DateTime startDate;
private DateTime finishDate;
private Guid userID;
public CheckoutDAO(DateTime startDate, DateTime finishDate, Guid userID, IConnectionDAO connection)
: base(connection)
{
this.startDate = startDate;
this.finishDate = finishDate;
this.userID = userID;
}
public decimal GetPending(ref string info)
{
decimal amount = 0;
info = "";
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.Void = 0 AND s.Paid = 0
GROUP BY t.Date, c.Name, s.BillID
";
using (SqlCommand 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))
{
if (dr.Read())
{
info = "\n\r--- Pending 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 GetCreditCard(ref string info)
{
decimal amount = 0;
info = "";
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.Void = 0 AND s.Paid = 1 AND s.CreditCard = 1
GROUP BY t.Date, c.Name, s.BillID
";
using (SqlCommand 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))
{
if (dr.Read())
{
info = "\n\r--- Credit Card 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 GetVoids(ref string info)
{
decimal amount = 0;
info = "";
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.Void = 1
GROUP BY t.Date, c.Name, s.BillID
";
using (SqlCommand 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))
{
if (dr.Read())
{
info = "\n\r--- Void 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 GetOpenings()
{
string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Opening' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
using (SqlCommand 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()
{
string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Receipt' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
using (SqlCommand 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()
{
string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Payment' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
using (SqlCommand 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()
{
string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Additional Void' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
using (SqlCommand 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()
{
string query = "SELECT ISNULL(SUM(Amount), 0) FROM Payments WHERE Type = 'Retained at Night' AND Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID";
using (SqlCommand 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()
{
string query = "SELECT ISNULL(SUM(Amount), 0) FROM Advances WHERE DateIn BETWEEN @StartDate AND @FinishDate AND CashierIn = @UserID";
using (SqlCommand 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()
{
string query = "SELECT ISNULL(SUM(Amount), 0) FROM Advances WHERE DateOut BETWEEN @StartDate AND @FinishDate AND CashierOut = @UserID";
using (SqlCommand 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()
{
string paymentString = "";
string query = "SELECT Date, Type, Amount, Narration FROM Payments WHERE Date BETWEEN @StartDate AND @FinishDate AND CashierID = @UserID ORDER BY Type";
using (SqlCommand 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()
{
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.Void = 0 AND s.Paid = 1
";
using (SqlCommand 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 GetDiscountsBills(ref string info)
{
decimal amount = 0;
info = "";
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.Void = 1
GROUP BY t.Date, c.Name, s.BillID, i.Discount
HAVING i.Discount > .2
";
using (SqlCommand 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))
{
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()
{
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.Void = 0
AND s.Paid = 0
AND t.LastEditDate < @StartDate AND t.UserID = @UserID
";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@UserID", userID);
return (decimal)connection.ExecuteScalar(cmd);
}
}
public decimal GetOldReceipts()
{
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.Void = 0
AND s.Paid = 1
AND t.CreationDate < @StartDate AND t.LastEditDate >= @StartDate AND t.UserID = @UserID
";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@UserID", userID);
return (decimal)connection.ExecuteScalar(cmd);
}
}
public decimal GetOldVoided()
{
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.Void = 1
AND t.CreationDate < @StartDate AND t.LastEditDate >= @StartDate AND t.UserID = @UserID
";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@UserID", userID);
return (decimal)connection.ExecuteScalar(cmd);
}
}
}
}