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

58 lines
2.4 KiB
C#

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(@"
SET @PaymentID = NEWID();
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;
cmd.Parameters.AddWithValue("@Date", payment.Date);
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);
}
}
}
}