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 AdvanceDAO : BaseDAO , IAdvanceDAO
{
public AdvanceDAO ( IConnectionDAO connection )
: base ( connection )
{ }
public void Insert ( AdvanceBO advance )
{
SqlCommand cmd = new SqlCommand ( @ "
2011-12-05 09:23:02 +00:00
SELECT @AdvanceID = NEWID ( ) , @DateIn = getdate ( ) ;
2010-03-02 17:56:21 +00:00
INSERT INTO Advances ( AdvanceID , Amount , Narration , CashierIn , DateIn , CashierOut , DateOut )
VALUES ( @AdvanceID , @Amount , @Narration , @CashierIn , @DateIn , @CashierOut , @DateOut )
");
cmd . Parameters . Add ( "@AdvanceID" , System . Data . SqlDbType . UniqueIdentifier ) ;
cmd . Parameters [ "@AdvanceID" ] . Direction = System . Data . ParameterDirection . Output ;
cmd . Parameters . AddWithValue ( "@Amount" , advance . Amount ) ;
cmd . Parameters . AddWithValue ( "@Narration" , advance . Narration ) ;
cmd . Parameters . AddWithValue ( "@CashierIn" , advance . CashierIn ) ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . Add ( "@DateIn" , System . Data . SqlDbType . DateTime ) ;
cmd . Parameters [ "@DateIn" ] . Direction = System . Data . ParameterDirection . Output ;
2010-03-02 17:56:21 +00:00
if ( advance . CashierOut . HasValue )
cmd . Parameters . AddWithValue ( "@CashierOut" , advance . CashierOut ) ;
else
cmd . Parameters . AddWithValue ( "@CashierOut" , DBNull . Value ) ;
if ( advance . DateOut . HasValue )
cmd . Parameters . AddWithValue ( "@DateOut" , advance . DateOut ) ;
else
cmd . Parameters . AddWithValue ( "@DateOut" , DBNull . Value ) ;
connection . ExecuteNonQuery ( cmd ) ;
advance . AdvanceID = ( Guid ) cmd . Parameters [ "@AdvanceID" ] . Value ;
}
2011-12-05 09:23:02 +00:00
public AdvanceBO Get ( Guid advanceID )
{
using ( var cmd = new SqlCommand ( "SELECT * FROM Advances WHERE AdvanceID = @AdvanceID" ) )
{
cmd . Parameters . AddWithValue ( "@AdvanceID" , advanceID ) ;
return BusinessObjectDAO < AdvanceBO > . GetBusinessObject ( connection . ExecuteReader ( cmd ) ) ;
}
}
2010-03-02 17:56:21 +00:00
public List < AdvanceDisplayBO > GetAdvances ( DateTime fromDate , DateTime toDate , bool all )
{
string query = "SELECT a.*, u.Name AS Cashier FROM Advances a INNER JOIN Users u ON a.CashierIn = u.UserID WHERE DateIn BETWEEN @StartDate AND @FinishDate" ;
if ( ! all )
query + = " AND CashierOut IS NULL" ;
2011-12-05 09:23:02 +00:00
var cmd = new SqlCommand ( query ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , fromDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , toDate ) ;
return BusinessObjectDAO < AdvanceDisplayBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
}
public void Adjust ( Guid advanceID , Guid userID )
{
2011-12-05 09:23:02 +00:00
var cmd = new SqlCommand ( "UPDATE Advances SET CashierOut = @CashierOut, DateOut = GETDATE() WHERE AdvanceID = @AdvanceID" ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@AdvanceID" , advanceID ) ;
cmd . Parameters . AddWithValue ( "@CashierOut" , userID ) ;
connection . ExecuteNonQuery ( cmd ) ;
}
}
}