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.

279 lines
11 KiB

1 year ago
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;
1 year ago
private readonly IKeyService m_tokenService;
1 year ago
private readonly HttpContext m_httpContext;
private readonly IHubContext<SalesHub, ISalesHub> m_salesHub;
public event EventHandler TransactionsChanged;
public event EventHandler FetchComplete;
public event EventHandler FetchStart;
1 year ago
public SalesService(BiskAcdbContext a_context, IKeyService a_tokenService,
1 year ago
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)
{
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 SaleSyncUpstream(@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();
}
}
}
public async Task SyncCancelledTransaction(List<Tblcancelledtransaction> 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 CancelledSaleSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncCreditPurchase(List<Creditpurchase> 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 CreditPurchaseSync(@p0)";
command.Parameters.Add(new MySqlParameter("@p0", jsonString));
command.ExecuteNonQuery();
}
}
}
public async Task SyncCustomerAccount(List<Customeraccount> a_customerAccounts)
{
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_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)
{
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_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)
{
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_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)
{
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_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)
{
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_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)
{
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_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)
{
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_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();
}
}
}
1 year ago
#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
1 year ago
}
}