narsil/Tanshu.Accounts.SqlDAO/InventoryDAO.cs

103 lines
5.5 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 InventoryDAO : BaseDAO, IInventoryDAO
{
public InventoryDAO(IConnectionDAO connection)
: base(connection)
{ }
public bool Insert(InventoryBO inventory)
{
using (SqlCommand cmd = new SqlCommand(@"
SET @InventoryID = NEWID();
INSERT INTO Inventory (InventoryID, VoucherID, ProductID, ComplexProductID, Quantity, Rate, Tax, Discount, IsRateFinal) VALUES (@InventoryID, @VoucherID, @ProductID, @ComplexProductID, @Quantity, @Rate, @Tax, @Discount, @IsRateFinal);
SELECT @timestamp = timestamp, @Amount = Amount FROM Inventory WHERE InventoryID = @InventoryID;
"))
{
cmd.Parameters.Add("@InventoryID", System.Data.SqlDbType.UniqueIdentifier);
cmd.Parameters["@InventoryID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@VoucherID", inventory.VoucherID);
cmd.Parameters.AddWithValue("@ProductID", inventory.ProductID);
if (inventory.ComplexProductID.HasValue)
cmd.Parameters.AddWithValue("@ComplexProductID", inventory.ComplexProductID);
else
cmd.Parameters.AddWithValue("@ComplexProductID", DBNull.Value);
cmd.Parameters.AddWithValue("@Quantity", inventory.Quantity);
cmd.Parameters.AddWithValue("@Rate", inventory.Rate);
cmd.Parameters.AddWithValue("@Tax", inventory.Tax);
cmd.Parameters.AddWithValue("@Discount", inventory.Discount);
cmd.Parameters.Add("@Amount", System.Data.SqlDbType.Decimal);
cmd.Parameters["@Amount"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@IsRateFinal", inventory.IsRateFinal);
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
inventory.InventoryID = (Guid)cmd.Parameters["@InventoryID"].Value;
inventory.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
return true;
}
}
public bool Update(InventoryBO inventory)
{
using (SqlCommand cmd = new SqlCommand(@"
UPDATE Inventory SET VoucherID = @VoucherID, ProductID = @ProductID, ComplexProductID = @ComplexProductID, Quantity = @Quantity, Rate = @Rate, Tax = @Tax, Discount = @Discount, IsRateFinal = @IsRateFinal WHERE InventoryID = @InventoryID;
SELECT @timestamp = timestamp, @Amount = Amount FROM Inventory WHERE InventoryID = @InventoryID;"))
{
cmd.Parameters.AddWithValue("@InventoryID", inventory.InventoryID);
cmd.Parameters.AddWithValue("@VoucherID", inventory.VoucherID);
cmd.Parameters.AddWithValue("@ProductID", inventory.ProductID);
if (inventory.ComplexProductID.HasValue)
cmd.Parameters.AddWithValue("@ComplexProductID", inventory.ComplexProductID);
else
cmd.Parameters.AddWithValue("@ComplexProductID", DBNull.Value);
cmd.Parameters.AddWithValue("@Quantity", inventory.Quantity);
cmd.Parameters.AddWithValue("@Rate", inventory.Rate);
cmd.Parameters.AddWithValue("@Tax", inventory.Tax);
cmd.Parameters.AddWithValue("@Discount", inventory.Discount);
cmd.Parameters.Add("@Amount", System.Data.SqlDbType.Decimal);
cmd.Parameters["@Amount"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@IsRateFinal", inventory.IsRateFinal);
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
inventory.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
return true;
}
}
public bool Delete(Guid voucherID)
{
using (SqlCommand cmd = new SqlCommand("DELETE from Inventory where VoucherID = @VoucherID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
connection.ExecuteNonQuery(cmd);
return true;
}
}
public bool Delete(Guid voucherID, Guid productID)
{
using (SqlCommand cmd = new SqlCommand("DELETE from Inventory where VoucherID = @VoucherID and ProductID = @ProductID"))
{
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
cmd.Parameters.AddWithValue("@InventoryID", productID);
connection.ExecuteNonQuery(cmd);
return true;
}
}
public List<InventoryDisplayBO> GetInventories(Guid voucherID)
{
SqlCommand cmd = new SqlCommand("SELECT p.Name + ' (' + p.Units +')' AS ProductName, i.* FROM Inventory i INNER JOIN Products p ON i.ProductID = p.ProductID WHERE VoucherID = @VoucherID ORDER BY timestamp");
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
return BusinessObjectDAO<InventoryDisplayBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
}
}
}