narsil/Tanshu.Accounts.SqlDAO/InventoryDAO.cs

88 lines
4.3 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Data.DAO;
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.SqlDAO
{
public class InventoryDAO : BaseDAO
2010-03-02 17:56:21 +00:00
{
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, Quantity, Rate, Vat, ServiceTax, Discount) VALUES (@InventoryID, @VoucherID, @ProductID, @Quantity, @Rate, @Vat, @ServiceTax, @Discount);
SELECT @Amount = Amount FROM Inventory WHERE InventoryID = @InventoryID;
2010-03-02 17:56:21 +00:00
"))
{
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);
cmd.Parameters.AddWithValue("@Quantity", inventory.Quantity);
cmd.Parameters.AddWithValue("@Rate", inventory.Rate);
cmd.Parameters.AddWithValue("@Vat", inventory.Vat);
cmd.Parameters.AddWithValue("@ServiceTax", inventory.ServiceTax);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@Discount", inventory.Discount);
cmd.Parameters.Add("@Amount", System.Data.SqlDbType.Decimal);
cmd.Parameters["@Amount"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
inventory.InventoryID = (Guid)cmd.Parameters["@InventoryID"].Value;
return true;
}
}
public bool Update(InventoryBO inventory)
{
using (SqlCommand cmd = new SqlCommand(@"
UPDATE Inventory SET VoucherID = @VoucherID, ProductID = @ProductID, Quantity = @Quantity, Rate = @Rate, Vat = @Vat, ServiceTax = @ServiceTax, Discount = @Discount WHERE InventoryID = @InventoryID;
SELECT @Amount = Amount FROM Inventory WHERE InventoryID = @InventoryID;"))
2010-03-02 17:56:21 +00:00
{
cmd.Parameters.AddWithValue("@InventoryID", inventory.InventoryID);
cmd.Parameters.AddWithValue("@VoucherID", inventory.VoucherID);
cmd.Parameters.AddWithValue("@ProductID", inventory.ProductID);
cmd.Parameters.AddWithValue("@Quantity", inventory.Quantity);
cmd.Parameters.AddWithValue("@Rate", inventory.Rate);
cmd.Parameters.AddWithValue("@Vat", inventory.Vat);
cmd.Parameters.AddWithValue("@ServiceTax", inventory.ServiceTax);
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@Discount", inventory.Discount);
cmd.Parameters.Add("@Amount", System.Data.SqlDbType.Decimal);
cmd.Parameters["@Amount"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
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<InventoryBO> GetInventories(Guid voucherID)
2010-03-02 17:56:21 +00:00
{
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");
2010-03-02 17:56:21 +00:00
cmd.Parameters.AddWithValue("@VoucherID", voucherID);
return BusinessObjectDAO<InventoryBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
2010-03-02 17:56:21 +00:00
}
}
}