2010-03-02 17:56:21 +00:00
using System ;
using System.Data.SqlClient ;
using System.Data ;
2011-12-05 09:23:02 +00:00
using Tanshu.Accounts.Contracts ;
2010-03-02 17:56:21 +00:00
using Tanshu.Accounts.DAOFactory ;
using Tanshu.Data.DAO ;
namespace Tanshu.Accounts.SqlDAO
{
public class CheckoutDAO : BaseDAO , ICheckoutDAO
{
2011-12-05 09:23:02 +00:00
private readonly DateTime _startDate ;
private readonly DateTime _finishDate ;
private readonly Guid _userID ;
2010-03-02 17:56:21 +00:00
public CheckoutDAO ( DateTime startDate , DateTime finishDate , Guid userID , IConnectionDAO connection )
: base ( connection )
{
2011-12-05 09:23:02 +00:00
_startDate = startDate ;
_finishDate = finishDate ;
_userID = userID ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
public decimal GetDetail ( ref string info , PaidStatus paidStatus , string infoLine )
2010-03-02 17:56:21 +00:00
{
decimal amount = 0 ;
info = "" ;
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
AND s . PaidStatus = @PaidStatus
2010-03-02 17:56:21 +00:00
GROUP BY t . Date , c . Name , s . BillID
";
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@PaidStatus" , ( int ) paidStatus ) ;
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
2011-12-05 09:23:02 +00:00
using ( var dr = connection . ExecuteReader ( cmd ) )
2010-03-02 17:56:21 +00:00
{
if ( dr . Read ( ) )
{
2011-12-05 09:23:02 +00:00
info = infoLine ;
2010-03-02 17:56:21 +00:00
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 ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetReceipts ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetPayments ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetAdditionalVoids ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetRetainedOvernight ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetAdvancesReceived ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetAdvancesAdjusted ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public string GetPaymentString ( )
{
2011-12-05 09:23:02 +00:00
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 ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
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 ) )
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
2010-03-02 17:56:21 +00:00
using ( IDataReader dr = connection . ExecuteReader ( cmd ) )
{
while ( dr . Read ( ) )
{
cashiers + = string . Format ( "{0}, " , dr . GetString ( 0 ) ) ;
}
}
}
return cashiers ;
}
public decimal GetNetSales ( )
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
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
2011-12-05 09:23:02 +00:00
AND s . PaidStatus ! = @Pending AND s . PaidStatus ! = @Void
2010-03-02 17:56:21 +00:00
";
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
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 ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetDiscountsBills ( ref string info )
{
decimal amount = 0 ;
info = "" ;
2011-12-05 09:23:02 +00:00
const string query = @ "
2012-04-06 20:07:58 +00:00
SELECT v . Date , s . BillID , c . Name , SUM ( i . Amount ) AS Amount , SUM ( i . Quantity * i . Rate * i . Discount ) AS Discount
FROM Vouchers v INNER JOIN SaleVoucher s ON v . VoucherID = s . VoucherID
INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
2010-03-02 17:56:21 +00:00
INNER JOIN Customers c ON s . CustomerID = c . CustomerID
2012-04-06 20:07:58 +00:00
WHERE v . VoucherID IN (
SELECT v . VoucherID FROM Vouchers v INNER JOIN SaleVoucher s ON v . VoucherID = s . VoucherID
INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . Type = 'S'
AND v . LastEditDate BETWEEN @StartDate AND @FinishDate AND v . UserID = @UserID
AND s . PaidStatus ! = @Void
AND i . Discount > = . 05
)
GROUP BY v . Date , s . BillID , c . Name
2010-03-02 17:56:21 +00:00
";
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , _finishDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
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 ( )
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
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'
2011-12-05 09:23:02 +00:00
AND s . PaidStatus = @Pending
2010-03-02 17:56:21 +00:00
AND t . LastEditDate < @StartDate AND t . UserID = @UserID
";
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetOldReceipts ( )
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
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'
2011-12-05 09:23:02 +00:00
AND s . PaidStatus ! = @Pending AND s . PaidStatus ! = @Void
2010-03-02 17:56:21 +00:00
AND t . CreationDate < @StartDate AND t . LastEditDate > = @StartDate AND t . UserID = @UserID
";
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetOldVoided ( )
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
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'
2011-12-05 09:23:02 +00:00
AND s . PaidStatus = @Void
2010-03-02 17:56:21 +00:00
AND t . CreationDate < @StartDate AND t . LastEditDate > = @StartDate AND t . UserID = @UserID
";
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
cmd . Parameters . AddWithValue ( "@StartDate" , _startDate ) ;
cmd . Parameters . AddWithValue ( "@UserID" , _userID ) ;
2010-03-02 17:56:21 +00:00
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
}
}
}