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 VoucherDAO : BaseDAO, IVoucherDAO { 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 WHERE Type = @Type SELECT @VoucherID = NEWID(), @CreationDate = GETDATE(), @LastEditDate = GETDATE(), @Date = COALESCE(@Date, GETDATE()) INSERT INTO [Vouchers] ([VoucherID], [Code], [Ref], [Date], [Narration], [UserID], [CreationDate], [LastEditDate], [Type]) VALUES (@VoucherID, @Code, @Ref, @Date, @Narration, @UserID, @CreationDate, @LastEditDate, @Type) SELECT @timestamp = timestamp FROM [dbo].[Vouchers] WHERE VoucherID = @VoucherID ")) { 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.AddWithValue("@Ref", voucher.Ref); 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; cmd.Parameters.AddWithValue("@Type", voucher.Type); cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp); cmd.Parameters["@timestamp"].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; voucher.timestamp = (byte[])cmd.Parameters["@timestamp"].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.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 [Ref] = @Ref, [Date] = @Date, [Narration] = @Narration, [UserID] = @UserID, [LastEditDate] = @LastEditDate, [Type] = @Type WHERE [VoucherID] = @VoucherID SELECT @Code = Code, @CreationDate = CreationDate, @timestamp = timestamp 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.AddWithValue("@Ref", voucher.Ref); 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; cmd.Parameters.AddWithValue("@Type", voucher.Type); cmd.Parameters.AddWithValue("@timestamp", voucher.timestamp); 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; voucher.timestamp = (byte[])cmd.Parameters["@timestamp"].Value; return true; } } } }