0172fc4e01
Added Nc Option in settlement Merged Vouchers and SaleVoucher table. Need to update the Sql Schema
223 lines
10 KiB
C#
223 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using Tanshu.Accounts.Contracts;
|
|
using Tanshu.Data.DAO;
|
|
using System.Data;
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.SqlDAO
|
|
{
|
|
public class ProductDAO : BaseDAO
|
|
{
|
|
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, ShortName, BarCode, Units, ProductGroupID, VatID ,SalePrice , ServiceTaxID , Discontinued, SortOrder) VALUES (@ProductID, @Code, @Name, '', '', @Units, @ProductGroupID, @VatID, @SalePrice ,@ServiceTaxID ,@Discontinued, @SortOrder);");
|
|
{
|
|
cmd.Parameters.AddWithValue("@ProductID", product.ProductID);
|
|
cmd.Parameters.Add("@Code", SqlDbType.Int);
|
|
cmd.Parameters["@Code"].Direction = ParameterDirection.Output;
|
|
cmd.Parameters.AddWithValue("@Name", product.Name);
|
|
cmd.Parameters.AddWithValue("@Units", product.Units);
|
|
cmd.Parameters.AddWithValue("@ProductGroupID", product.ProductGroupID);
|
|
cmd.Parameters.AddWithValue("@VatID", product.VatID);
|
|
cmd.Parameters.AddWithValue("@ServiceTaxID", product.ServiceTaxID);
|
|
cmd.Parameters.AddWithValue("@SalePrice", product.SalePrice);
|
|
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)
|
|
{
|
|
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;");
|
|
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("@ProductGroupID", product.ProductGroupID);
|
|
cmd.Parameters.AddWithValue("@VatID", product.VatID);
|
|
cmd.Parameters.AddWithValue("@ServiceTaxID", product.ServiceTaxID);
|
|
cmd.Parameters.AddWithValue("@SalePrice", product.SalePrice);
|
|
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()
|
|
{
|
|
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");
|
|
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
|
|
{
|
|
ProductID = dr.GetGuid(0),
|
|
Code = dr.GetInt32(1),
|
|
Name = dr.GetString(2),
|
|
Price = dr.GetDecimal(3),
|
|
Vat = string.Format("{0:#.###%}", dr.GetDecimal(4)),
|
|
ServiceTax = string.Format("{0:#.###%}", dr.GetDecimal(5)),
|
|
ProuctGroup = dr.GetString(6),
|
|
|
|
});
|
|
}
|
|
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, tv.Rate AS Vat, ts.Rate AS ServiceTax, pt.Name AS ProductGroup
|
|
FROM Products p INNER JOIN Tax tv ON p.VatID = tv.TaxID
|
|
INNER JOIN Tax ts ON p.ServiceTaxID = ts.TaxID
|
|
INNER JOIN ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID
|
|
WHERE 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["ProductGroup"].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<ProductGroupBO> GetProductGroups()
|
|
{
|
|
SqlCommand cmd = new SqlCommand("SELECT * FROM ProductGroups ORDER BY Name");
|
|
return BusinessObjectDAO<ProductGroupBO>.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 ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID WHERE RowNum > {2}
|
|
";
|
|
query = string.Format(query, name, count, skip);
|
|
return BusinessObjectDAO<ProductDisplayBO>.GetBusinessObjects(connection.ExecuteReader(query));
|
|
|
|
}
|
|
public List<TaxBO> GetTaxes()
|
|
{
|
|
return BusinessObjectDAO<TaxBO>.GetBusinessObjects(connection.ExecuteReader("SELECT * FROM Tax;"));
|
|
}
|
|
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";
|
|
|
|
decimal discountLimit;
|
|
using (var cmd = new SqlCommand(query))
|
|
{
|
|
cmd.Parameters.AddWithValue("@ProductID", productID);
|
|
discountLimit = (decimal)connection.ExecuteScalar(cmd);
|
|
}
|
|
return discountLimit;
|
|
}
|
|
}
|
|
}
|