221 lines
12 KiB
C#
221 lines
12 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Data.SqlClient;
|
|||
|
using Tanshu.Accounts.Contracts;
|
|||
|
using Tanshu.Data.DAO;
|
|||
|
using System.Data;
|
|||
|
using Tanshu.Accounts.DAOFactory;
|
|||
|
|
|||
|
|
|||
|
namespace Tanshu.Accounts.SqlDAO
|
|||
|
{
|
|||
|
public class ProductDAO : BaseDAO, IProductDAO
|
|||
|
{
|
|||
|
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;
|
|||
|
INSERT INTO Products (ProductID, Code, Name, Units, ProductTypeID, SaleLedgerID, SaleTaxID ,SalePrice , PurchaseLedgerID ,PurchaseTaxID , PurchasePrice, Discontinued, MinimumLevel, MaximumLevel, SortOrder) VALUES (@ProductID, @Code, @Name, @Units, @ProductTypeID, @SaleLedgerID, @SaleTaxID, @SalePrice ,@PurchaseLedgerID ,@PurchaseTaxID , @PurchasePrice, @Discontinued, @MinimumLevel, @MaximumLevel, @SortOrder);
|
|||
|
SELECT @timestamp = timestamp FROM Products WHERE ProductID = @ProductID;");
|
|||
|
{
|
|||
|
cmd.Parameters.AddWithValue("@ProductID", product.ProductID);
|
|||
|
cmd.Parameters.Add("@Code", System.Data.SqlDbType.Int);
|
|||
|
cmd.Parameters["@Code"].Direction = System.Data.ParameterDirection.Output;
|
|||
|
cmd.Parameters.AddWithValue("@Code", product.Code);
|
|||
|
cmd.Parameters.AddWithValue("@Name", product.Name);
|
|||
|
cmd.Parameters.AddWithValue("@Units", product.Units);
|
|||
|
cmd.Parameters.AddWithValue("@ProductTypeID", product.ProductTypeID);
|
|||
|
cmd.Parameters.AddWithValue("@SaleLedgerID", product.SaleLedgerID);
|
|||
|
cmd.Parameters.AddWithValue("@SaleTaxID", product.SaleTaxID);
|
|||
|
cmd.Parameters.AddWithValue("@SalePrice", product.SalePrice);
|
|||
|
cmd.Parameters.AddWithValue("@PurchaseLedgerID", product.PurchaseLedgerID);
|
|||
|
cmd.Parameters.AddWithValue("@PurchaseTaxID", product.PurchaseTaxID);
|
|||
|
cmd.Parameters.AddWithValue("@PurchasePrice", product.PurchasePrice);
|
|||
|
cmd.Parameters.AddWithValue("@Discontinued", product.Discontinued);
|
|||
|
cmd.Parameters.AddWithValue("@MinimumLevel", product.MinimumLevel);
|
|||
|
cmd.Parameters.AddWithValue("@MaximumLevel", product.MaximumLevel);
|
|||
|
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
|
|||
|
cmd.Parameters.AddWithValue("@SortOrder", product.SortOrder);
|
|||
|
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
|
|||
|
connection.ExecuteNonQuery(cmd);
|
|||
|
product.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
|
|||
|
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)
|
|||
|
{
|
|||
|
SqlCommand cmd = new SqlCommand(@"UPDATE Products SET Code = @Code ,Name = @Name ,Units = @Units ,ProductTypeID = @ProductTypeID ,SaleLedgerID = @SaleLedgerID ,SaleTaxID = @SaleTaxID ,SalePrice = @SalePrice ,PurchaseLedgerID = @PurchaseLedgerID ,PurchaseTaxID = @PurchaseTaxID ,PurchasePrice = @PurchasePrice ,Discontinued = @Discontinued ,MinimumLevel = @MinimumLevel ,MaximumLevel = @MaximumLevel, SortOrder=@SortOrder WHERE ProductID = @ProductID SELECT @timestamp = timestamp FROM Products WHERE ProductID = @ProductID;");
|
|||
|
cmd.Parameters.AddWithValue("@ProductID", product.ProductID);
|
|||
|
cmd.Parameters.AddWithValue("@Code", product.Code);
|
|||
|
cmd.Parameters.AddWithValue("@Name", product.Name);
|
|||
|
cmd.Parameters.AddWithValue("@Units", product.Units);
|
|||
|
cmd.Parameters.AddWithValue("@ProductTypeID", product.ProductTypeID);
|
|||
|
cmd.Parameters.AddWithValue("@SaleLedgerID", product.SaleLedgerID);
|
|||
|
cmd.Parameters.AddWithValue("@SaleTaxID", product.SaleTaxID);
|
|||
|
cmd.Parameters.AddWithValue("@SalePrice", product.SalePrice);
|
|||
|
cmd.Parameters.AddWithValue("@PurchaseLedgerID", product.PurchaseLedgerID);
|
|||
|
cmd.Parameters.AddWithValue("@PurchaseTaxID", product.PurchaseTaxID);
|
|||
|
cmd.Parameters.AddWithValue("@PurchasePrice", product.PurchasePrice);
|
|||
|
cmd.Parameters.AddWithValue("@Discontinued", product.Discontinued);
|
|||
|
cmd.Parameters.AddWithValue("@MinimumLevel", product.MinimumLevel);
|
|||
|
cmd.Parameters.AddWithValue("@MaximumLevel", product.MaximumLevel);
|
|||
|
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
|
|||
|
cmd.Parameters.AddWithValue("@SortOrder", product.SortOrder);
|
|||
|
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
|
|||
|
connection.ExecuteNonQuery(cmd);
|
|||
|
product.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
|
|||
|
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 decimal GetProductStock(DateTime date, Guid productID, Guid? voucherID)
|
|||
|
{
|
|||
|
// Add this to get product rate as well
|
|||
|
// , CASE WHEN SUM(CASE WHEN t.Type IN ('P', 'Z') THEN i.Quantity ELSE 0 END) = 0 THEN (SELECT PurchasePrice FROM Products p WHERE p.ProductID = i.ProductID) ELSE SUM(CASE WHEN t.Type IN ('P', 'Z') THEN i.Quantity * i.Rate ELSE 0 END) / SUM(CASE WHEN t.Type IN ('P', 'Z') THEN i.Quantity ELSE 0 END) END AS Rate
|
|||
|
string query;
|
|||
|
if (voucherID.HasValue)
|
|||
|
query = @"
|
|||
|
SELECT SUM(i.Quantity * j.Debit) AS Quantity
|
|||
|
FROM Vouchers t INNER JOIN Inventory i ON t.VoucherID = i.VoucherID INNER JOIN Journal j ON t.VoucherID = j.VoucherID INNER JOIN Products p ON p.ProductID = i.ProductID
|
|||
|
WHERE j.CostCenterID = '955F847B-EFDF-4AFA-897C-F0BAF15284A3' AND i.ProductID = @ProductID AND t.Date < @Date
|
|||
|
AND t.VoucherID <> @VoucherID
|
|||
|
GROUP BY i.ProductID";
|
|||
|
else
|
|||
|
query = @"
|
|||
|
SELECT SUM(i.Quantity * j.Debit) AS Quantity
|
|||
|
FROM Vouchers t INNER JOIN Inventory i ON t.VoucherID = i.VoucherID INNER JOIN Journal j ON t.VoucherID = j.VoucherID INNER JOIN Products p ON p.ProductID = i.ProductID
|
|||
|
WHERE j.CostCenterID = '955F847B-EFDF-4AFA-897C-F0BAF15284A3' AND i.ProductID = @ProductID AND t.Date < @Date
|
|||
|
GROUP BY i.ProductID";
|
|||
|
SqlCommand cmd = new SqlCommand(query);
|
|||
|
cmd.Parameters.AddWithValue("@Date", date);
|
|||
|
cmd.Parameters.AddWithValue("@ProductID", productID);
|
|||
|
if (voucherID.HasValue)
|
|||
|
cmd.Parameters.AddWithValue("@VoucherID", voucherID.Value);
|
|||
|
return (decimal)connection.ExecuteScalar(cmd);
|
|||
|
}
|
|||
|
|
|||
|
public List<ProductDisplayBO> GetProducts()
|
|||
|
{
|
|||
|
//SELECT p.ProductID, p.Picture, p.Code, p.Name, p.Units, p.ProductTypeID, p.SaleLedgerID, p.SaleTaxID, p.SalePrice, p.PurchaseLedgerID, p.PurchaseTaxID, p.PurchasePrice, p.Discontinued, p.MinimumLevel, p.MaximumLevel, p.timestamp, p.SortOrder, pt.Name AS Type FROM Products p INNER JOIN ProductTypes pt ON p.ProductTypeID = pt.ProductTypeID ORDER BY p.Name
|
|||
|
SqlCommand cmd = new SqlCommand("SELECT p.ProductID, p.Picture, p.Code, p.Name, p.Units, p.ProductTypeID, p.SaleLedgerID, p.SaleTaxID, p.SalePrice, p.PurchaseLedgerID, p.PurchaseTaxID, p.PurchasePrice, p.Discontinued, p.MinimumLevel, p.MaximumLevel, p.SortOrder, pt.Name AS Type FROM Products p INNER JOIN ProductTypes pt ON p.ProductTypeID = pt.ProductTypeID ORDER BY p.Name");
|
|||
|
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
|
|||
|
{
|
|||
|
Category = dr.GetString(4),
|
|||
|
Code = dr.GetInt32(1),
|
|||
|
Name = dr.GetString(2),
|
|||
|
Price = dr.GetDecimal(3),
|
|||
|
ProductID = dr.GetGuid(0)
|
|||
|
|
|||
|
});
|
|||
|
}
|
|||
|
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,
|
|||
|
p.SalePrice * (1 + t.Rate) AS Price,
|
|||
|
pt.Name AS Category
|
|||
|
FROM Products p INNER JOIN Tax t ON p.SaleTaxID = t.TaxID
|
|||
|
INNER JOIN ProductTypes pt ON p.ProductTypeID = pt.ProductTypeID
|
|||
|
WHERE pt.IsForSale = 1 AND p.SalePrice > 0 AND p.Discontinued = 0
|
|||
|
";
|
|||
|
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);
|
|||
|
|
|||
|
n = filter["Type"].ToLower().Split(" ".ToCharArray());
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
public List<ProductTypeBO> GetProductTypes()
|
|||
|
{
|
|||
|
SqlCommand cmd = new SqlCommand("SELECT * FROM ProductTypes ORDER BY Name");
|
|||
|
return BusinessObjectDAO<ProductTypeBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
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}%')
|
|||
|
SELECT TOP {1} p.*, pt.Name AS Type FROM ProductsRN p INNER JOIN ProductTypes pt ON p.ProductTypeID = pt.ProductTypeID WHERE RowNum > {2}
|
|||
|
";
|
|||
|
query = string.Format(query, name, count, skip);
|
|||
|
return BusinessObjectDAO<ProductDisplayBO>.GetBusinessObjects(connection.ExecuteReader(query));
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|