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 ;
2013-11-16 06:01:58 +00:00
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.SqlDAO
{
2013-11-16 06:01:58 +00:00
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 ( ) ;
2013-11-28 10:39:33 +00:00
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 ) ;
2013-11-28 10:39:33 +00:00
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 ( @ "
2013-11-28 10:39:33 +00:00
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 ) ;
2013-11-28 10:39:33 +00:00
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 ;
}
}
2014-08-08 12:05:38 +00:00
public List < InventoryBO > GetInventories ( Guid voucherID )
2010-03-02 17:56:21 +00:00
{
2013-11-28 10:39:33 +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 ) ;
2014-08-08 12:05:38 +00:00
return BusinessObjectDAO < InventoryBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
2010-03-02 17:56:21 +00:00
}
}
}