narsil/Tanshu.Accounts.SqlDAO/VoucherDAO.cs

116 lines
5.1 KiB
C#

using System;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Data.DAO;
namespace Tanshu.Accounts.SqlDAO
{
public class VoucherDAO : BaseDAO
{
public VoucherDAO(IConnectionDAO connection)
: base(connection)
{ }
public bool Insert(VoucherBO voucher)
{
using (SqlCommand cmd = new SqlCommand(@"
SELECT @Code = ISNULL(MAX(Code), 0) + 1 FROM Vouchers;
SELECT @VoucherID = NEWID(),
@CreationDate = GETDATE(),
@LastEditDate = GETDATE(),
@Date = COALESCE(@Date, GETDATE());
INSERT INTO [Vouchers] ([VoucherID], [Code], [Date], [Narration], [UserID], [CreationDate], [LastEditDate])
VALUES (@VoucherID, @Code, @Date, @Narration, @UserID, @CreationDate, @LastEditDate);
"))
{
cmd.Parameters.Add("@VoucherID", System.Data.SqlDbType.UniqueIdentifier);
cmd.Parameters["@VoucherID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@Code", System.Data.SqlDbType.Int);
cmd.Parameters["@Code"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@Date", System.Data.SqlDbType.DateTime);
if (voucher.Date.HasValue)
cmd.Parameters["@Date"].Value = voucher.Date.Value;
else
cmd.Parameters["@Date"].Value = DBNull.Value;
cmd.Parameters["@Date"].Direction = System.Data.ParameterDirection.InputOutput;
cmd.Parameters.AddWithValue("@Narration", voucher.Narration);
cmd.Parameters.AddWithValue("@UserID", voucher.UserID);
cmd.Parameters.Add("@CreationDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@CreationDate"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@LastEditDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@LastEditDate"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
voucher.VoucherID = (Guid)cmd.Parameters["@VoucherID"].Value;
voucher.Code = (int)cmd.Parameters["@Code"].Value;
voucher.CreationDate = (DateTime)cmd.Parameters["@CreationDate"].Value;
voucher.LastEditDate = (DateTime)cmd.Parameters["@LastEditDate"].Value;
voucher.Date = (DateTime)cmd.Parameters["@Date"].Value;
return true;
}
}
public VoucherBO GetVoucher(Guid voucherID)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Vouchers WHERE VoucherID = @VoucherID");
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
return BusinessObjectDAO<VoucherBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public bool Delete(Guid voucherID)
{
using (SqlCommand cmd = new SqlCommand("DELETE from Vouchers where VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
connection.ExecuteNonQuery(cmd);
return true;
}
}
public bool Update(VoucherBO voucher)
{
using (SqlCommand cmd = new SqlCommand(@"
SELECT @LastEditDate = GETDATE(),
@Date = COALESCE(@Date, GETDATE())
UPDATE [Vouchers]
SET [Date] = @Date,
[Narration] = @Narration,
[UserID] = @UserID,
[LastEditDate] = @LastEditDate
WHERE [VoucherID] = @VoucherID
SELECT @Code = Code, @CreationDate = CreationDate
FROM [Vouchers]
WHERE [VoucherID] = @VoucherID
"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucher.VoucherID);
cmd.Parameters.AddWithValue("@Code", voucher.Code);
cmd.Parameters["@Code"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@Date", System.Data.SqlDbType.DateTime);
if (voucher.Date.HasValue)
cmd.Parameters["@Date"].Value = voucher.Date.Value;
else
cmd.Parameters["@Date"].Value = DBNull.Value;
cmd.Parameters["@Date"].Direction = System.Data.ParameterDirection.InputOutput;
cmd.Parameters.AddWithValue("@Narration", voucher.Narration);
cmd.Parameters.AddWithValue("@UserID", voucher.UserID);
cmd.Parameters.Add("@CreationDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@CreationDate"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@LastEditDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@LastEditDate"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
voucher.Code = (int)cmd.Parameters["@Code"].Value;
voucher.CreationDate = (DateTime)cmd.Parameters["@CreationDate"].Value;
voucher.LastEditDate = (DateTime)cmd.Parameters["@LastEditDate"].Value;
voucher.Date = (DateTime)cmd.Parameters["@Date"].Value;
return true;
}
}
}
}