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.

278 lines
11 KiB

using Cloud_Manager.CloudHubs;
using Cloud_Manager.Models.CustomModels;
using Cloud_Manager.Models.Enums;
using Cloud_Manager.Models.Interfaces;
using Cloud_Manager.Models.POSModels;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
using MySqlConnector;
using System.Text.Json;
namespace Cloud_Manager.Services
{
public class SalesService : ISalesInterface
{
private readonly BiskAcdbContext m_context;
private readonly IKeyService m_tokenService;
private readonly HttpContext m_httpContext;
private readonly IHubContext<SalesHub, ISalesHub> m_salesHub;
public event EventHandler TransactionsChanged;
public event EventHandler FetchComplete;
public event EventHandler FetchStart;
public SalesService(BiskAcdbContext a_context, IKeyService a_tokenService,
IHttpContextAccessor a_httpContextAccessor, IHubContext<SalesHub, ISalesHub> a_salesHub)
{
m_context = a_context;
m_tokenService = a_tokenService;
m_httpContext = a_httpContextAccessor?.HttpContext;
m_salesHub = a_salesHub;
}
public async Task SyncCart(List<Tblcart> a_item)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_item);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL SaleSyncUpstream(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public DateTime GetLastSyncDate(string a_tablename)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string activeBranch = m_tokenService.GetBaseBranch(apiKey)!;
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)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string activeBranch = m_tokenService.GetBaseBranch(apiKey)!;
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();
}
}
}
public async Task SyncCancelledTransaction(List<Tblcancelledtransaction> a_item)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_item);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL CancelledSaleSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncCreditPurchase(List<Creditpurchase> a_item)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_item);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL CreditPurchaseSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncCustomerAccount(List<Customeraccount> a_customerAccounts)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_customerAccounts);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL CustomerAccountSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncCustomerPurchase(List<Tblcustomerpurchase> a_customerPurchase)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_customerPurchase);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL CustomerPurchasesSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncDiscountLogs(List<Tbldiscountlog> a_discountLog)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_discountLog);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL DiscountLogsSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncDeliveryDetails(List<Tbldeliverydetail> a_details)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_details);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL DeliveryDetailsSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncDeliveryHead(List<Tbldeliveryhead> a_heads)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_heads);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL DeliveryheadSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncDeliveryRecipients(List<Tbldeliveryrecipient> a_recipients)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_recipients);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL DeliveryRecipientSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncInvoice(List<Tblinvoice> a_invoice)
{
string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!;
if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey))
{
string jsonString = JsonSerializer.Serialize(a_invoice);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
m_context.Database.OpenConnection();
command.CommandText = "CALL InvoiceSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
#region NotImplemented
public Task FetchRecentTransaction(int a_limit)
{
throw new NotImplementedException();
}
public Task FetchTransaction(DateTime a_start, DateTime a_end)
{
throw new NotImplementedException();
}
public IEnumerable<SaleItem> GetTransactions(DateTime a_start, DateTime a_end)
{
throw new NotImplementedException();
}
public IEnumerable<SaleItem> GetRecentTransaction()
{
throw new NotImplementedException();
}
public Task FetchReceipt(string a_receiptId)
{
throw new NotImplementedException();
}
public IEnumerable<SaleItem> GetReceipt(string a_receiptId)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Tblcart>> GetReceiptDetail(string a_receiptId)
{
throw new NotImplementedException();
}
#endregion
}
}