2010-03-02 17:56:21 +00:00
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.Data.SqlClient ;
using Tanshu.Accounts.Contracts ;
using Tanshu.Data.DAO ;
using Tanshu.Accounts.DAOFactory ;
namespace Tanshu.Accounts.SqlDAO
{
public class PaymentDAO : BaseDAO , IPaymentDAO
{
public PaymentDAO ( IConnectionDAO connection )
: base ( connection )
{ }
public void Insert ( PaymentBO payment )
{
using ( SqlCommand cmd = new SqlCommand ( @ "
2011-12-05 09:23:02 +00:00
SELECT @PaymentID = NEWID ( ) , @Date = getdate ( ) ;
2010-03-02 17:56:21 +00:00
INSERT INTO Payments ( PaymentID , Date , Type , CashierID , Amount , Narration )
VALUES ( @PaymentID , @Date , @Type , @CashierID , @Amount , @Narration )
"))
{
cmd . Parameters . Add ( "@PaymentID" , System . Data . SqlDbType . UniqueIdentifier ) ;
cmd . Parameters [ "@PaymentID" ] . Direction = System . Data . ParameterDirection . Output ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . Add ( "@Date" , System . Data . SqlDbType . DateTime ) ;
cmd . Parameters [ "@Date" ] . Direction = System . Data . ParameterDirection . Output ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@Type" , payment . Type ) ;
cmd . Parameters . AddWithValue ( "@CashierID" , payment . CashierID ) ;
cmd . Parameters . AddWithValue ( "@Amount" , payment . Amount ) ;
cmd . Parameters . AddWithValue ( "@Narration" , payment . Narration ) ;
connection . ExecuteNonQuery ( cmd ) ;
payment . PaymentID = ( Guid ) cmd . Parameters [ "@PaymentID" ] . Value ;
}
}
public List < PaymentDisplayBO > GetPayments ( Guid ? userID , DateTime fromDate , DateTime toDate )
{
string query = "SELECT p.*, u.Name AS Cashier FROM Payments p INNER JOIN Users u ON p.CashierID = u.UserID WHERE Date BETWEEN @StartDate AND @FinishDate" ;
if ( userID . HasValue )
query + = " AND CashierID = @CashierID" ;
SqlCommand cmd = new SqlCommand ( query ) ;
cmd . Parameters . AddWithValue ( "@StartDate" , fromDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , toDate ) ;
if ( userID . HasValue )
cmd . Parameters . AddWithValue ( "@CashierID" , userID . Value ) ;
return BusinessObjectDAO < PaymentDisplayBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
}
public void Delete ( Guid paymentID )
{
using ( SqlCommand cmd = new SqlCommand ( "DELETE FROM Payments WHERE PaymentID = @PaymentID" ) )
{
cmd . Parameters . AddWithValue ( "@PaymentID" , paymentID ) ;
connection . ExecuteNonQuery ( cmd ) ;
}
}
}
}