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 ;
using System.Data ;
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 ProductDAO : BaseDAO
2010-03-02 17:56:21 +00:00
{
public ProductDAO ( IConnectionDAO connection )
: base ( connection )
{ }
public bool Insert ( ProductBO product )
{
product . ProductID = Guid . NewGuid ( ) ;
SqlCommand cmd = new SqlCommand ( @ "
SELECT @Code = ISNULL ( MAX ( Code ) , 0 ) + 1 FROM Products ;
2013-11-30 11:12:08 +00:00
INSERT INTO Products ( ProductID , Code , Name , ShortName , BarCode , Units , ProductGroupID , VatID , SalePrice , ServiceTaxID , Discontinued , SortOrder ) VALUES ( @ProductID , @Code , @Name , ' ' , ' ' , @Units , @ProductGroupID , @VatID , @SalePrice , @ServiceTaxID , @Discontinued , @SortOrder ) ; ");
2010-03-02 17:56:21 +00:00
{
cmd . Parameters . AddWithValue ( "@ProductID" , product . ProductID ) ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . Add ( "@Code" , SqlDbType . Int ) ;
cmd . Parameters [ "@Code" ] . Direction = ParameterDirection . Output ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@Name" , product . Name ) ;
cmd . Parameters . AddWithValue ( "@Units" , product . Units ) ;
2013-11-28 10:39:33 +00:00
cmd . Parameters . AddWithValue ( "@ProductGroupID" , product . ProductGroupID ) ;
cmd . Parameters . AddWithValue ( "@VatID" , product . VatID ) ;
cmd . Parameters . AddWithValue ( "@ServiceTaxID" , product . ServiceTaxID ) ;
2014-08-08 12:05:38 +00:00
cmd . Parameters . AddWithValue ( "@SalePrice" , product . SalePrice ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@Discontinued" , product . Discontinued ) ;
cmd . Parameters . AddWithValue ( "@SortOrder" , product . SortOrder ) ;
connection . ExecuteNonQuery ( cmd ) ;
product . Code = ( int ) cmd . Parameters [ "@Code" ] . Value ;
return true ;
}
}
public ProductBO GetProduct ( Guid productID )
{
SqlCommand cmd = new SqlCommand ( "SELECT * FROM Products WHERE ProductID = @ProductID" ) ;
cmd . Parameters . AddWithValue ( "@ProductID" , productID ) ;
return BusinessObjectDAO < ProductBO > . GetBusinessObject ( connection . ExecuteReader ( cmd ) ) ;
}
public bool Delete ( Guid productID )
{
SqlCommand cmd = new SqlCommand ( "DELETE FROM Products WHERE ProductID = @ProductID" ) ;
cmd . Parameters . AddWithValue ( "@ProductID" , productID ) ;
connection . ExecuteNonQuery ( cmd ) ;
return true ;
}
public bool Update ( ProductBO product )
{
2013-11-30 11:12:08 +00:00
SqlCommand cmd = new SqlCommand ( @"UPDATE Products SET Code = @Code ,Name = @Name ,Units = @Units ,ProductGroupID = @ProductGroupID ,VatID = @VatID ,SalePrice = @SalePrice ,ServiceTaxID = @ServiceTaxID ,Discontinued = @Discontinued ,SortOrder=@SortOrder WHERE ProductID = @ProductID;" ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@ProductID" , product . ProductID ) ;
cmd . Parameters . AddWithValue ( "@Code" , product . Code ) ;
cmd . Parameters . AddWithValue ( "@Name" , product . Name ) ;
cmd . Parameters . AddWithValue ( "@Units" , product . Units ) ;
2013-11-28 10:39:33 +00:00
cmd . Parameters . AddWithValue ( "@ProductGroupID" , product . ProductGroupID ) ;
cmd . Parameters . AddWithValue ( "@VatID" , product . VatID ) ;
cmd . Parameters . AddWithValue ( "@ServiceTaxID" , product . ServiceTaxID ) ;
2014-08-08 12:05:38 +00:00
cmd . Parameters . AddWithValue ( "@SalePrice" , product . SalePrice ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@Discontinued" , product . Discontinued ) ;
cmd . Parameters . AddWithValue ( "@SortOrder" , product . SortOrder ) ;
connection . ExecuteNonQuery ( cmd ) ;
return true ;
}
public ProductBO GetProduct ( string nameAndUnits )
{
SqlCommand cmd = new SqlCommand ( "SELECT * FROM Products WHERE Name + ' (' + Units + ')' = @Name" ) ;
cmd . Parameters . AddWithValue ( "@Name" , nameAndUnits ) ;
return BusinessObjectDAO < ProductBO > . GetBusinessObject ( connection . ExecuteReader ( cmd ) ) ;
}
public List < ProductDisplayBO > GetProducts ( )
{
2014-08-08 12:05:38 +00:00
SqlCommand cmd = new SqlCommand ( "SELECT p.ProductID, p.Code, p.Name, p.Units, p.ProductGroupID, p.VatID, p.SalePrice, p.ServiceTaxID, p.Discontinued, p.SortOrder, pt.Name AS Type FROM Products p INNER JOIN ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID ORDER BY p.Name" ) ;
2010-03-02 17:56:21 +00:00
return BusinessObjectDAO < ProductDisplayBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
}
public List < ProductDisplaySmallBO > GetFilteredProducts ( Dictionary < string , string > filter )
{
List < ProductDisplaySmallBO > list = new List < ProductDisplaySmallBO > ( ) ;
string query = GetProductQuery ( filter ) ;
using ( IDataReader dr = connection . ExecuteReader ( query ) )
{
while ( dr . Read ( ) )
{
list . Add ( new ProductDisplaySmallBO
{
2013-11-30 11:12:08 +00:00
ProductID = dr . GetGuid ( 0 ) ,
2010-03-02 17:56:21 +00:00
Code = dr . GetInt32 ( 1 ) ,
Name = dr . GetString ( 2 ) ,
Price = dr . GetDecimal ( 3 ) ,
2013-11-30 11:12:08 +00:00
Vat = string . Format ( "{0:#.###%}" , dr . GetDecimal ( 4 ) ) ,
ServiceTax = string . Format ( "{0:#.###%}" , dr . GetDecimal ( 5 ) ) ,
ProuctGroup = dr . GetString ( 6 ) ,
2010-03-02 17:56:21 +00:00
} ) ;
}
dr . Close ( ) ;
}
return list ;
}
private string GetProductQuery ( Dictionary < string , string > filter )
{
string query = @ "
SELECT p . ProductID , p . Code , p . Name + ' ( ' + p . Units + ' ) ' + ShortName AS Name ,
2013-11-30 11:12:08 +00:00
p . SalePrice , tv . Rate AS Vat , ts . Rate AS ServiceTax , pt . Name AS ProductGroup
FROM Products p INNER JOIN Tax tv ON p . VatID = tv . TaxID
2014-08-08 12:05:38 +00:00
INNER JOIN Tax ts ON p . ServiceTaxID = ts . TaxID
2013-11-28 10:39:33 +00:00
INNER JOIN ProductGroups pt ON p . ProductGroupID = pt . ProductGroupID
2014-08-08 12:05:38 +00:00
WHERE p . Discontinued = 0
2010-03-02 17:56:21 +00:00
";
string [ ] n = filter [ "Name" ] . Split ( " " . ToCharArray ( ) ) ;
foreach ( string n1 in n )
if ( n1 ! = null & & n1 ! = string . Empty )
query + = string . Format ( "AND p.Name + ' (' + p.Units + ') ' + ShortName LIKE '%{0}%' \r\n" , n1 ) ;
2013-11-30 11:12:08 +00:00
n = filter [ "ProductGroup" ] . ToLower ( ) . Split ( " " . ToCharArray ( ) ) ;
2010-03-02 17:56:21 +00:00
foreach ( string n1 in n )
if ( n1 ! = null & & n1 ! = string . Empty )
query + = string . Format ( "AND pt.Name LIKE '%{0}%' \r\n" , n1 ) ;
query + = "ORDER BY p.SortOrder DESC;" ;
return query ;
}
public void UpdateShortName ( )
{
List < ProductBO > list = BusinessObjectDAO < ProductBO > . GetBusinessObjects ( connection . ExecuteReader ( "SELECT * FROM Products" ) ) ;
foreach ( var item in list )
{
using ( SqlCommand cmd = new SqlCommand ( "UPDATE Products SET ShortName = @ShortName WHERE ProductID = @ProductID AND ShortName <> @ShortName" ) )
{
cmd . Parameters . AddWithValue ( "@ShortName" , GetShortName ( item . Name ) ) ;
cmd . Parameters . AddWithValue ( "@ProductID" , item . ProductID ) ;
connection . ExecuteScalar ( cmd ) ;
}
}
}
private string GetShortName ( string name )
{
string [ ] t1 = name . Split ( " " . ToCharArray ( ) ) ;
if ( t1 . Length = = 0 )
return "" ;
name = "" ;
foreach ( string t in t1 )
{
if ( ( ! t . StartsWith ( "(" ) ) & & ( t ! = "" ) )
name + = t . Substring ( 0 , 1 ) ;
}
return name ;
}
2013-11-28 10:39:33 +00:00
public List < ProductGroupBO > GetProductGroups ( )
2010-03-02 17:56:21 +00:00
{
2013-11-28 10:39:33 +00:00
SqlCommand cmd = new SqlCommand ( "SELECT * FROM ProductGroups ORDER BY Name" ) ;
return BusinessObjectDAO < ProductGroupBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
2010-03-02 17:56:21 +00:00
}
public List < ProductDisplayBO > GetProducts ( string name , int skip , int count )
{
string query = @ "
WITH ProductsRN AS
( SELECT * , ROW_NUMBER ( ) OVER ( ORDER BY Name ) AS RowNum FROM Products WHERE Name LIKE ' % { 0 } % ' )
2013-11-28 10:39:33 +00:00
SELECT TOP { 1 } p . * , pt . Name AS Type FROM ProductsRN p INNER JOIN ProductGroups pt ON p . ProductGroupID = pt . ProductGroupID WHERE RowNum > { 2 }
2010-03-02 17:56:21 +00:00
";
query = string . Format ( query , name , count , skip ) ;
return BusinessObjectDAO < ProductDisplayBO > . GetBusinessObjects ( connection . ExecuteReader ( query ) ) ;
}
2013-11-28 10:39:33 +00:00
public List < TaxBO > GetTaxes ( )
{
return BusinessObjectDAO < TaxBO > . GetBusinessObjects ( connection . ExecuteReader ( "SELECT * FROM Tax;" ) ) ;
}
2014-08-08 12:05:38 +00:00
public SalesBillItemBO GetDefaultSaleBillItem ( Guid productID )
{
const string query = @ "
SELECT p . ProductID , p . Name + ' ( ' + p . Units + ')' , p . SalePrice ,
( SELECT Rate from Tax WHERE TaxID = p . VatID ) ,
( SELECT Rate from Tax WHERE TaxID = p . ServiceTaxID )
FROM Products p
WHERE p . ProductID = @ProductID
";
using ( var cmd = new SqlCommand ( query ) )
{
cmd . Parameters . AddWithValue ( "@ProductID" , productID ) ;
using ( var dr = connection . ExecuteReader ( cmd ) )
{
dr . Read ( ) ;
var mySale = new SalesBillItemBO
{
productID = dr . GetGuid ( 0 ) ,
Name = dr . GetString ( 1 ) ,
Price = dr . GetDecimal ( 2 ) ,
Vat = dr . GetDecimal ( 3 ) ,
ServiceTax = dr . GetDecimal ( 4 ) ,
Discount = 0 ,
Printed = 0 ,
Quantity = 1
} ;
return mySale ;
}
}
}
public decimal GetProductDiscountLimit ( Guid productID )
{
const string query = @"SELECT t.DiscountLimit FROM ProductGroups t INNER JOIN Products p ON t.ProductGroupID = p.ProductGroupID WHERE p.ProductID = @ProductID" ;
2013-11-28 10:39:33 +00:00
2014-08-08 12:05:38 +00:00
decimal discountLimit ;
using ( var cmd = new SqlCommand ( query ) )
{
cmd . Parameters . AddWithValue ( "@ProductID" , productID ) ;
discountLimit = ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
return discountLimit ;
}
2010-03-02 17:56:21 +00:00
}
}