using System; using System.Collections.Generic; using System.Data.SqlClient; using Tanshu.Accounts.Contracts; using Tanshu.Data.DAO; namespace Tanshu.Accounts.SqlDAO { public class InventoryDAO : BaseDAO { 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; ")) { 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); 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;")) { 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); 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 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"); cmd.Parameters.AddWithValue("@VoucherID", voucherID); return BusinessObjectDAO.GetBusinessObjects(connection.ExecuteReader(cmd)); } } }