New source control repo for Biskilog POS - secure hub to store & manage source code. Streamlines dev process, tracks changes, & improves collaboration. Ensures reliable software.
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.

228 lines
9.3 KiB

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<Tblbranch> 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<Tblbranch>();
GetCompanyInfoAsync();
GetBranches();
}
public IEnumerable<Tblbranch> FetchBranches()
{
return m_companyBranches;
}
public async Task<IEnumerable<Tblbranch>> 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<Tblcompanydetail> GetCompanyInfoAsync()
{
m_companyInfo = m_context.Tblcompanydetails.FirstOrDefault();
return Task.FromResult(m_companyInfo);
}
public string GetCompanyName()
{
return m_companyInfo.CompanyName;
}
public async Task SyncBranches(List<Tblbranch> 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<Systemuserrole> 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<Tblcompanydetail> 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<Tbldriver> 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<Tbltruck> 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<Tbltruckassignment> 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<TbltruckDrivermapping> 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<Tbltruckinventory> 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();
}
}
}
}
}