narsil/Tanshu.Accounts.SqlDAO/LedgerDAO.cs

103 lines
4.9 KiB
C#
Raw Normal View History

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 LedgerDAO : BaseDAO , ILedgerDAO
{
public LedgerDAO(IConnectionDAO connection)
: base(connection)
{ }
public bool Insert(LedgerBO ledger)
{
ledger.LedgerID = Guid.NewGuid();
using (SqlCommand cmd = new SqlCommand(@"
SELECT @Code = ISNULL(MAX(Code),0) + 1 FROM Ledgers WHERE Type = @Type;
INSERT INTO Ledgers (LedgerID, Code, Name, Type, ParentLedgerID, CostCenterID) VALUES (@LedgerID, @Code, @Name, @Type, @ParentLedgerID, @CostCenterID);
SELECT @timestamp = timestamp FROM Ledgers WHERE LedgerID = @LedgerID;
"))
{
cmd.Parameters.AddWithValue("@LedgerID", ledger.LedgerID);
cmd.Parameters.Add("@Code", System.Data.SqlDbType.Int);
cmd.Parameters["@Code"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@Name", ledger.Name);
cmd.Parameters.AddWithValue("@Type", ledger.Type);
if (ledger.ParentLedgerID != new Guid())
cmd.Parameters.AddWithValue("@ParentLedgerID", ledger.ParentLedgerID);
else
cmd.Parameters.AddWithValue("@ParentLedgerID", DBNull.Value);
cmd.Parameters.AddWithValue("@CostCenterID", ledger.CostCenterID);
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
ledger.Code = (int)cmd.Parameters["@Code"].Value;
ledger.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
return true;
}
}
public LedgerBO GetLedger(Guid ledgerID)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Ledgers WHERE LedgerID = @LedgerID");
cmd.Parameters.AddWithValue("@LedgerID", ledgerID);
return BusinessObjectDAO<LedgerBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public LedgerBO GetLedger(string name)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Ledgers WHERE Name = @Name");
cmd.Parameters.AddWithValue("@Name", name);
return BusinessObjectDAO<LedgerBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public List<LedgerDisplayBO> GetLedgers(char type)
{
SqlCommand cmd = new SqlCommand("SELECT c.Name as CostCenter, l.* FROM CostCenters c INNER JOIN Ledgers l ON c.CostCenterID = l.CostCenterID WHERE Type = @Type");
cmd.Parameters.AddWithValue("@Type", type);
return BusinessObjectDAO<LedgerDisplayBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
}
public bool Delete(Guid ledgerID)
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Ledgers WHERE LedgerID = @LedgerID"))
{
cmd.Parameters.AddWithValue("@LedgerID", ledgerID);
connection.ExecuteNonQuery(cmd);
return true;
}
}
public bool Update(LedgerBO ledger)
{
using (SqlCommand cmd = new SqlCommand(@"
UPDATE Ledgers SET Code = @Code, Name = @Name, Type = @Type, ParentLedgerID = @ParentLedgerID, CostCenterID = @CostCenterID WHERE LedgerID = @LedgerID
SELECT @timestamp = timestamp FROM Ledgers WHERE LedgerID = @LedgerID;
"))
{
cmd.Parameters.AddWithValue("@LedgerID", ledger.LedgerID);
cmd.Parameters.AddWithValue("@Code", ledger.Code);
cmd.Parameters.AddWithValue("@Name", ledger.Name);
cmd.Parameters.AddWithValue("@Type", ledger.Type);
if (ledger.ParentLedgerID != new Guid())
cmd.Parameters.AddWithValue("@ParentLedgerID", ledger.ParentLedgerID);
else
cmd.Parameters.AddWithValue("@ParentLedgerID", DBNull.Value);
cmd.Parameters.AddWithValue("@CostCenterID", ledger.CostCenterID);
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
ledger.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
return true;
}
}
public List<LedgerDisplayBO> GetLedgers()
{
string query = "SELECT c.Name as CostCenter, l.* FROM CostCenters c INNER JOIN Ledgers l ON c.CostCenterID = l.CostCenterID WHERE Type <> 'S'";
return BusinessObjectDAO<LedgerDisplayBO>.GetBusinessObjects(connection.ExecuteReader(query));
}
}
}