using Biskilog_Accounting.Server.POSModels; 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.Text.Json; namespace Biskilog_Accounting.Server.Services { public class CompanyService : ICompanyInfo { private readonly BiskAcdbContext m_context; private readonly ITokenService m_tokenService; private readonly HttpContext m_httpContext; private Tblcompanydetail m_companyInfo { get; set; } private IEnumerable m_companyBranches { get; set; } public CompanyService(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor) { m_context = a_context; m_tokenService = a_tokenService; m_httpContext = a_httpContextAccessor?.HttpContext; m_companyInfo = new Tblcompanydetail(); m_companyBranches = new List(); GetCompanyInfoAsync(); GetBranches(); } public IEnumerable FetchBranches() { return m_companyBranches; } public async Task> GetBranches() { m_companyBranches = m_context.Tblbranches; return await Task.FromResult(m_companyBranches); } public string GetBranchName(string a_branchId) { return m_companyBranches.FirstOrDefault(b => b.BranchId == a_branchId).BranchName; } public Task GetCompanyInfoAsync() { m_companyInfo = m_context.Tblcompanydetails.FirstOrDefault(); return Task.FromResult(m_companyInfo); } public string GetCompanyName() { return m_companyInfo.CompanyName; } public async Task SyncBranches(List a_items) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL BranchSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } public DateTime GetLastSyncDate(string a_tablename) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string activeBranch = m_tokenService.GetBaseBranch(token)!; 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 token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string activeBranch = m_tokenService.GetBaseBranch(token)!; 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 SyncSystemRoles(List a_roles) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_roles); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL SystemUserRolesSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } public async Task SyncCompanyDetails(List a_details) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_details); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL CompanyDetailSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } public async Task SyncDriverDetails(List a_details) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_details); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL DriverSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } public async Task SyncTrucks(List a_trucks) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_trucks); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL TruckSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } public async Task SyncTruckAssignments(List a_assignments) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_assignments); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL TruckAssignmentSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } public async Task SyncTruckMappings(List a_mapping) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_mapping); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL TruckDriverMappingSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } public async Task SyncTruckInventory(List a_inventories) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_inventories); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL TruckInventorySync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } } }