You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
7.1 KiB
201 lines
7.1 KiB
2 years ago
|
using Biskilog_Accounting.Server.POSModels;
|
||
|
using Biskilog_Accounting.Shared.CustomModels;
|
||
|
using Biskilog_Accounting.Shared.Enums;
|
||
|
using Biskilog_Accounting.Shared.Interfaces;
|
||
|
using Biskilog_Accounting.Shared.POSModels;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
using Microsoft.Net.Http.Headers;
|
||
|
using MySqlConnector;
|
||
|
using System.Data;
|
||
|
using System.Data.Common;
|
||
|
|
||
|
namespace Biskilog_Accounting.Server.Services
|
||
|
{
|
||
|
public class ProductRepo : IProduct
|
||
|
{
|
||
|
private readonly BiskAcdbContext m_context;
|
||
|
private readonly ITokenService m_tokenService;
|
||
|
private readonly HttpContext m_httpContext;
|
||
|
|
||
|
public event EventHandler ProductsChanged;
|
||
|
public event EventHandler UnitsChanged;
|
||
|
public event EventHandler BrandsChanged;
|
||
|
public event EventHandler CategoriesChanged;
|
||
|
|
||
|
public ProductRepo(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor)
|
||
|
{
|
||
|
m_context = a_context;
|
||
|
m_tokenService = a_tokenService;
|
||
|
m_httpContext = a_httpContextAccessor?.HttpContext;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// Gets all products from the server
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public IEnumerable<ProductItem> GetProducts(string a_productKey = "")
|
||
|
{
|
||
|
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
|
||
|
|
||
|
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
|
||
|
{
|
||
|
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
|
||
|
|
||
|
using (var command = m_context.Database.GetDbConnection().CreateCommand())
|
||
|
{
|
||
|
command.CommandText = "CALL GetProducts(@p0)";
|
||
|
command.Parameters.Add(new MySqlParameter("@p0", string.Join(", ", accessiblebranches.ToArray())));
|
||
|
|
||
|
m_context.Database.OpenConnection();
|
||
|
|
||
|
using (var reader = command.ExecuteReader())
|
||
|
{
|
||
|
while (reader.Read())
|
||
|
{
|
||
|
List<ProductUnits> pUnits = new List<ProductUnits>();
|
||
|
|
||
|
yield return new ProductItem
|
||
|
{
|
||
|
Product = new Tblproduct
|
||
|
{
|
||
|
Pcode = reader.GetString(0),
|
||
|
ProductName = reader.GetString(1),
|
||
|
Pdesc = reader.GetString(2),
|
||
|
BaseUnit = reader.GetString(3),
|
||
|
Costprice = reader.GetDecimal(4),
|
||
|
Status = reader.GetString(5),
|
||
|
Price = reader.GetDecimal(6),
|
||
|
BranchId = reader.GetString(7),
|
||
|
},
|
||
|
BaseUnit = reader.GetString(3),
|
||
|
Stock = new Tblinventory
|
||
|
{
|
||
|
Quantity = reader.GetInt32(8)
|
||
|
},
|
||
|
Restocklevel = new Restocklevel
|
||
|
{
|
||
|
WarnLevel = reader.GetInt32(9),
|
||
|
Unit = reader.GetString(10),
|
||
|
},
|
||
|
Units = GetAltUnits(reader)
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
private List<ProductUnits> GetAltUnits(DbDataReader a_reader)
|
||
|
{
|
||
|
List<ProductUnits> pUnits = new List<ProductUnits>();
|
||
|
for (int i = 1; i < 5; i++)
|
||
|
{
|
||
|
if (!a_reader.IsDBNull(a_reader.GetOrdinal($"AltUnit{i}")))
|
||
|
{
|
||
|
pUnits.Add(new ProductUnits
|
||
|
{
|
||
|
UnitCode = a_reader.GetFieldValue<string>($"AltUnit{i}"),
|
||
|
QuantityUnit = a_reader.GetFieldValue<int>($"AltUnit{i}QTY"),
|
||
|
PriceUnit = a_reader.GetFieldValue<decimal>($"AltUnit{i}Price"),
|
||
|
DistinctiveCode = a_reader.GetFieldValue<string>($"AltUnit{i}distinctiveCode")
|
||
|
});
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return pUnits;
|
||
|
}
|
||
|
}
|
||
|
return pUnits;
|
||
|
}
|
||
|
|
||
|
public IEnumerable<Unitofmeasure> GetUnitofmeasures()
|
||
|
{
|
||
|
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
|
||
|
|
||
|
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
|
||
|
{
|
||
|
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
|
||
|
|
||
|
return m_context.Unitofmeasures.Where(b => accessiblebranches.Contains(b.BranchId));
|
||
|
}
|
||
|
return new List<Unitofmeasure>();
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
public IEnumerable<Tblbrand> GetBrands(string a_brandKey = "")
|
||
|
{
|
||
|
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
|
||
|
|
||
|
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
|
||
|
{
|
||
|
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
|
||
|
|
||
|
return m_context.Tblbrands.Where(b => accessiblebranches.Contains(b.BranchId));
|
||
|
}
|
||
|
return new List<Tblbrand>();
|
||
|
}
|
||
|
|
||
|
public IEnumerable<Tblcategory> GetCategories(string a_categoryKey = "")
|
||
|
{
|
||
|
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
|
||
|
|
||
|
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
|
||
|
{
|
||
|
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
|
||
|
|
||
|
return m_context.Tblcategories.Where(b => accessiblebranches.Contains(b.BranchId));
|
||
|
}
|
||
|
return new List<Tblcategory>();
|
||
|
}
|
||
|
|
||
|
#region Only Need to implement in the client Side
|
||
|
public Task FetchUnits()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
public Task FetchProducts()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public ProductItem GetProductById(string a_id)
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public ProductItem GetProductByName(string name)
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public void RefreshList()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public string GetUnitName(string a_unitCode)
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
public Task FetchBrands()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public Task FetchCategories()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public IEnumerable<ProductItem> GetLowstockItems()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public Task FetchLowStockProducts()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
|
}
|