The cloud manager acts as an intermediary for syncing between the local biskilog server manager and the biskilog accounting web application
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.

323 lines
12 KiB

1 year ago
using Cloud_Manager;
using Cloud_Manager.Models.CustomModels;
using Cloud_Manager.Models.Enums;
using Cloud_Manager.Models.Interfaces;
using Cloud_Manager.Models.POSModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
using MySqlConnector;
using System.Data;
using System.Data.Common;
using System.Text.Json;
namespace Cloud_Manager.Services
{
public class ProductRepo : IProduct
{
private readonly BiskAcdbContext m_context;
1 year ago
private readonly IKeyService m_tokenService;
1 year ago
private readonly HttpContext m_httpContext;
public event EventHandler ProductsChanged;
public event EventHandler UnitsChanged;
public event EventHandler BrandsChanged;
public event EventHandler CategoriesChanged;
1 year ago
public ProductRepo(BiskAcdbContext a_context, IKeyService a_tokenService, IHttpContextAccessor a_httpContextAccessor)
1 year ago
{
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 = "")
{
1 year ago
throw new NotImplementedException();
1 year ago
}
private List<ProductUnits> GetAltUnits(DbDataReader a_reader)
{
1 year ago
throw new NotImplementedException();
1 year ago
}
public IEnumerable<Unitofmeasure> GetUnitofmeasures()
{
1 year ago
throw new NotImplementedException();
1 year ago
}
public IEnumerable<Tblbrand> GetBrands(string a_brandKey = "")
{
1 year ago
throw new NotImplementedException();
1 year ago
}
public IEnumerable<Tblcategory> GetCategories(string a_categoryKey = "")
{
1 year ago
throw new NotImplementedException();
1 year ago
}
public async Task SyncProducts(List<Tblproduct> a_item)
{
try
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_item);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL ProductSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
public async Task SyncInventory(List<Tblinventory> a_item)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_item);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL InventorySync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncInventoryEntries(List<Tblinventoryentry> a_item)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_item);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL InventoryEntriesSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncPriceChanges(List<Tblpricechange> a_items)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_items);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL PriceChangeSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncProductAltUnit(List<Productaltunit> a_items)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_items);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL ProductAltUnitSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncRestockAsync(List<Restocklevel> a_items)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_items);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL RestockLevelsSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncUnitOfMeasureAsync(List<Unitofmeasure> a_items)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_items);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL UnitOfMeasureSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncStockAsync(List<Tbstock> a_items)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_items);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL StockSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncBrandsAsync(List<Tblbrand> a_items)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_items);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL BrandSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncCategoriesAsync(List<Tblcategory> a_items)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
string jsonString = JsonSerializer.Serialize(a_items);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL CategorySync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public DateTime GetLastSyncDate(string a_tablename)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
1 year ago
string activeBranch = m_tokenService.GetBaseBranch(apiKey)!;
1 year ago
DateTime? lastSync = m_context.Tblsyncinfos.FirstOrDefault(p => p.TableName == a_tablename && p.BranchId == activeBranch!)?.LastSyncDate;
if (lastSync != null)
{
return (DateTime)lastSync!;
}
}
return new DateTime(2000, 01, 01);
}
public void SetLastSyncDate(string a_tableName, DateTime a_timestamp)
{
1 year ago
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
1 year ago
{
1 year ago
string activeBranch = m_tokenService.GetBaseBranch(apiKey)!;
1 year ago
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL SetTableLastSync(@p0,@p1,@p2)";
command.Parameters.Add(new MySqlParameter("@p0", a_tableName));
command.Parameters.Add(new MySqlParameter("@p1", activeBranch));
command.Parameters.Add(new MySqlParameter("@p2", a_timestamp));
command.ExecuteNonQuery();
}
}
}
#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
}