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 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 a_salesHub) { m_context = a_context; m_tokenService = a_tokenService; m_httpContext = a_httpContextAccessor?.HttpContext; m_salesHub = a_salesHub; } public async Task SyncCart(List 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 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 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 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 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 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 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 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 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 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 GetTransactions(DateTime a_start, DateTime a_end) { throw new NotImplementedException(); } public IEnumerable GetRecentTransaction() { throw new NotImplementedException(); } public Task FetchReceipt(string a_receiptId) { throw new NotImplementedException(); } public IEnumerable GetReceipt(string a_receiptId) { throw new NotImplementedException(); } public Task> GetReceiptDetail(string a_receiptId) { throw new NotImplementedException(); } #endregion } }