OnlineSalesAutoCrop/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/MobileApp/MobileMasterDataService.cs

1078 lines
44 KiB
C#
Raw Normal View History

2026-08-06 16:57:29 +06:00
using DocumentFormat.OpenXml.Office2016.Excel;
using Ease.NetCore.DataAccess;
using Ease.NetCore.DataAccess.SQL;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
2026-07-30 11:35:10 +06:00
using OnlineSalesAutoCrop.CoreAPI.Models;
using OnlineSalesAutoCrop.CoreAPI.Models.Global;
using OnlineSalesAutoCrop.CoreAPI.Models.Requests.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Systems;
2026-08-06 16:57:29 +06:00
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
2026-08-06 11:10:58 +06:00
using Twilio.Http;
namespace OnlineSalesAutoCrop.CoreAPI.Services.Services.MobileApp;
public class MobileMasterDataService : IMobileMasterDataService
{
private readonly AppSettings _settings;
private readonly IUserService _userService;
private readonly IIntegrationHttpService _httpService;
public MobileMasterDataService(IOptions<AppSettings> options, IUserService userService, IIntegrationHttpService httpService)
{
_settings = options.Value;
_userService = userService;
_httpService = httpService;
}
public async Task<MarketHeirarchyByEmpResponse> GetMarketHeirarchiesByEmpAsync(GetMarketHeirarchyByEmpRequest request)
{
MarketHeirarchyByEmpResponse response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
string employeeNumber = request.EmployeeNumber;
response.Companies = await GetCompaniesByEmployeeNumberAsync(tc, employeeNumber);
response.SalesOrgs = await GetSalesOrgsByEmployeeNumberAsync(tc, employeeNumber);
response.DistributionChannels = await GetDistributionChannelsByEmployeeNumberAsync(tc, employeeNumber);
response.Regions = await GetRegionsByEmployeeNumberAsync(tc, employeeNumber);
response.Areas = await GetAreasByEmployeeNumberAsync(tc, employeeNumber);
response.Teritories = await GetTerritoriesByEmployeeNumberAsync(tc, employeeNumber);
response.Plants = await GetPlantsByEmployeeNumberAsync(tc, employeeNumber);
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<PagedResult<GetBrandResponse>> GetBrandsAsync(GetBrandsRequest request)
{
PagedResult<GetBrandResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@BrandCode", pType: SqlDbType.VarChar, pValue: request.BrandCode),
SqlHelperExtension.CreateInParam(pName: "@SalesOrganization", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetBrandsPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetBrandResponse
{
BrandCode = dr.GetString(0),
BrandName = dr.GetString(1)
});
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
2026-07-08 18:15:24 +06:00
public async Task<PagedResult<GetMaterialResponse>> GetMaterialsAsync(GetMaterialsRequest request)
{
PagedResult<GetMaterialResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@BrandCode", pType: SqlDbType.VarChar, pValue: request.BrandCode),
SqlHelperExtension.CreateInParam(pName: "@MaterialCode", pType: SqlDbType.VarChar, pValue: request.MaterialCode),
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetMaterialsPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetMaterialResponse
{
MaterialCode = dr.GetString(0),
MaterialName = dr.GetString(1),
TypeCode = dr.GetString(2),
TypeName = dr.GetString(3),
GroupCode = dr.GetString(4),
GroupName = dr.GetString(5),
BaseUnitMeasurement = dr.GetString(6),
SalesUnitMeasurement = dr.GetString(7),
UnitConversionValue = dr.GetDecimal(8),
BrandCode = dr.GetString(9),
BrandName = dr.GetString(10),
2026-08-13 17:42:16 +06:00
SalesOrg = dr.GetString(11),
IsSealable = dr.IsDBNull(12) ? false : dr.GetInt32(12) > 0
});
2026-07-19 16:05:07 +06:00
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
2026-07-19 16:05:07 +06:00
public async Task<PagedResult<GetMaterialStockReponse>> GetMaterialsStockAsync(GetMaterialStockRequest request)
{
PagedResult<GetMaterialStockReponse> response = new();
try
{
2026-08-03 13:59:53 +06:00
var httpStockResponse = await _httpService.GetMaterialStockAsync(request.PlantCode);
2026-07-19 16:05:07 +06:00
2026-08-03 13:59:53 +06:00
response.Items = httpStockResponse.Select(p => new GetMaterialStockReponse
{
MaterialCode = p.MaterialCode,
PlantCode = p.PlantCode,
AvailableStock = p.AvailableStock,
}).ToList();
2026-07-19 16:05:07 +06:00
2026-08-03 13:59:53 +06:00
//var materialResponse = await GetMaterialsAsync(new GetMaterialsRequest());
2026-07-19 16:05:07 +06:00
2026-08-03 13:59:53 +06:00
//foreach (var item in materialResponse.Items)
//{
// response.Items.Add(
// new GetMaterialStockReponse() { PlantCode = request.PlantCode, MaterialCode = item.MaterialCode, AvailableStock = Random.Shared.Next(5, 500) });
//}
2026-07-19 16:05:07 +06:00
}
catch (Exception)
{
throw;
}
return response;
}
public async Task<PagedResult<GetMaterialPriceResponse>> GetMaterialPricesAsync(GetMaterialPriceRequest request)
{
PagedResult<GetMaterialPriceResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@BrandCode", pType: SqlDbType.VarChar, pValue: request.BrandCode),
SqlHelperExtension.CreateInParam(pName: "@MaterialCode", pType: SqlDbType.VarChar, pValue: request.MaterialCode),
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetMaterialPricePaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetMaterialPriceResponse
{
SalesOrgCode = dr.GetString(0),
CustomerCode = dr.GetString(1),
MaterialCode = dr.GetString(2),
BasePrice = dr.GetDecimal(3),
VatValue =dr.IsDBNull(4) ? null: dr.GetDecimal(4),
VatCalculationType =dr.IsDBNull(5) ? null: dr.GetString(5),
});
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<PagedResult<GetCustomerResponse>> GetCustomersAsync(GetCustomersRequest request)
{
string companyCode = string.Empty;
2026-08-11 18:24:11 +06:00
string salesOrgCode = string.Empty;
2026-08-06 11:10:58 +06:00
decimal usedCreditBalance = 0;
PagedResult<GetCustomerResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@TeritoryCode", pType: SqlDbType.VarChar, pValue: request.TeritoryCode),
SqlHelperExtension.CreateInParam(pName: "@CustomerCode", pType: SqlDbType.VarChar, pValue: request.CustomerCode),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetCustomersPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetCustomerResponse
{
CustomerCode = dr.GetString(0),
CustomerName = dr.GetString(1),
AccountGroupCode = dr.GetString(2),
AccountGroupDescription = dr.GetString(3),
DistributionChannelCode = dr.GetString(4),
DistributionChannelName = dr.GetString(5),
DivisionCode = dr.GetString(6),
DivisionName = dr.GetString(7),
MobileNumber = dr.GetString(8),
2026-08-11 18:24:11 +06:00
EmailAddress = dr.IsDBNull(9) ? null : dr.GetString(9),
TaxNumber = dr.GetString(10),
2026-08-03 18:24:18 +06:00
SalesGroupCode = dr.IsDBNull(11)? null : dr.GetString(11),
SalesGroupName = dr.IsDBNull(12) ? null : dr.GetString(12),
CustomerGroupCode = dr.IsDBNull(13) ? null : dr.GetString(13),
CustomerGroupName = dr.IsDBNull(14) ? null : dr.GetString(14),
CustomerPriceGroupCode = dr.IsDBNull(15) ? null : dr.GetString(15),
CustomerPriceGroupName = dr.IsDBNull(16) ? null : dr.GetString(16),
PaymentTermCode = dr.IsDBNull(17) ? null : dr.GetString(17),
TeritoryCode = dr.GetString(18),
SalesOrgCode = dr.GetString(19),
PlantCode = dr.GetString(20),
IsCreditCustomer = dr.GetInt32(21) > 0 ,
CreditLimit = dr.GetDecimal(22),
CreditBalance = dr.GetDecimal(23),
CreditOverdue = dr.GetDecimal(24),
});
companyCode = dr.GetString(25);
2026-08-11 18:24:11 +06:00
salesOrgCode = dr.GetString(19);
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
2026-08-06 11:10:58 +06:00
if (!string.IsNullOrWhiteSpace(request.CustomerCode))
{
2026-08-11 18:24:11 +06:00
usedCreditBalance = await GetCurrentApprovedOrderAmountByCustomerAsync(tc, request.CustomerCode, salesOrgCode);
2026-08-06 11:10:58 +06:00
}
tc.End();
if (!string.IsNullOrWhiteSpace(request.CustomerCode))
{
var customerLedger = await _httpService.GetCustomerLedgerAsync(request.CustomerCode, companyCode);
2026-08-06 11:10:58 +06:00
if (customerLedger != null)
{
response.Items = response.Items.Select(x =>
{
2026-08-11 18:24:11 +06:00
x.CreditBalance = (customerLedger.CurrentBalance + usedCreditBalance);
x.CreditOverdue = customerLedger.OverdueAmount;
return x;
}).ToList();
}
}
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
2026-08-06 11:10:58 +06:00
private async Task<decimal> GetCurrentApprovedOrderAmountByCustomerAsync(TransactionContext tc, string customerCode, string salesOrgCode)
{
decimal totalApprovedAmount = 0;
try
{
2026-08-06 16:57:29 +06:00
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@CustomerCode", pType: SqlDbType.NVarChar, pValue: customerCode),
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.NVarChar, pValue: salesOrgCode),
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetCustomerApprovedAmount",p))
2026-08-06 11:10:58 +06:00
{
2026-08-06 16:57:29 +06:00
if (dr.Read())
2026-08-06 11:10:58 +06:00
{
totalApprovedAmount = dr.GetDecimal(0);
}
dr.Close();
}
tc.End();
}
catch(Exception)
{
throw;
}
return totalApprovedAmount;
}
private async Task<List<EmpCompanyResponse>> GetCompaniesByEmployeeNumberAsync(TransactionContext tc, string employeeNumber)
{
List<EmpCompanyResponse> response = [];
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.NVarChar, pValue: employeeNumber)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetCompaniesByEmployeeNumber", parameterValues: p))
{
while (dr.Read())
{
response.Add(new EmpCompanyResponse
{
CompanyCode = dr.GetString(0),
CompanyName = dr.GetString(1)
});
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
private async Task<List<SalesOrgResponse>> GetSalesOrgsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber)
{
List<SalesOrgResponse> response = [];
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.NVarChar, pValue: employeeNumber)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetSalesOrgsByEmployeeNumber", parameterValues: p))
{
while (dr.Read())
{
response.Add(new SalesOrgResponse
{
CompanyCode = dr.GetString(0),
SalesOrgCode = dr.GetString(1),
SalesOrgName = dr.GetString(2)
});
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
private async Task<List<DistibutionChannleResponse>> GetDistributionChannelsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber)
{
List<DistibutionChannleResponse> response = [];
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.NVarChar, pValue: employeeNumber)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetDistributionChannelsByEmployeeNumber", parameterValues: p))
{
while (dr.Read())
{
response.Add(new DistibutionChannleResponse
{
CompanyCode = dr.GetString(0),
2026-08-11 18:24:11 +06:00
SalesOrgCode = dr.IsDBNull(1) ? null: dr.GetString(1),
DistributorChannelCode = dr.IsDBNull(2) ? null : dr.GetString(2),
DistributorChannelName = dr.IsDBNull(3) ? null : dr.GetString(3)
});
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
private async Task<List<RegionResponse>> GetRegionsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber)
{
List<RegionResponse> response = [];
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.NVarChar, pValue: employeeNumber)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetRegionsByEmployeeNumber", parameterValues: p))
{
while (dr.Read())
{
response.Add(new RegionResponse
{
CompanyCode = dr.GetString(0),
SalesOrgCode = dr.GetString(1),
RegionCode = dr.GetString(2),
RegionName = dr.GetString(3)
});
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
private async Task<List<AreaResponse>> GetAreasByEmployeeNumberAsync(TransactionContext tc, string employeeNumber)
{
List<AreaResponse> response = [];
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.NVarChar, pValue: employeeNumber)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetAreasByEmployeeNumber", parameterValues: p))
{
while (dr.Read())
{
response.Add(new AreaResponse
{
CompanyCode = dr.GetString(0),
SalesOrgCode = dr.GetString(1),
RegionCode = dr.GetString(2),
AreaCode = dr.GetString(3),
AreaName = dr.GetString(4)
});
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
private async Task<List<TeritoryResponse>> GetTerritoriesByEmployeeNumberAsync(TransactionContext tc, string employeeNumber)
{
List<TeritoryResponse> response = [];
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.NVarChar, pValue: employeeNumber)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetTerritoriesByEmployeeNumber", parameterValues: p))
{
while (dr.Read())
{
response.Add(new TeritoryResponse
{
CompanyCode = dr.GetString(0),
SalesOrgCode = dr.GetString(1),
RegionCode = dr.GetString(2),
AreaCode = dr.GetString(3),
TeritoryCode = dr.GetString(4),
TeritoryName = dr.GetString(5)
});
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
private async Task<List<PlantResponse>> GetPlantsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber)
{
List<PlantResponse> response = [];
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.NVarChar, pValue: employeeNumber)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetPlantsByEmployeeNumber", parameterValues: p))
{
while (dr.Read())
{
response.Add(new PlantResponse
{
CompanyCode = dr.GetString(0),
SalesOrgCode = dr.GetString(1),
PlantCode = dr.GetString(2),
PlantName = dr.GetString(3)
});
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
2026-07-08 18:15:24 +06:00
public async Task<GetAttendanceByEmpResponse> GetAttendanceByEmpAsync(GetAttendanceByEmpRequest request)
{
2026-07-09 18:53:22 +06:00
GetAttendanceByEmpResponse response = new();
2026-07-08 18:15:24 +06:00
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@AttendanceDate", pType: SqlDbType.Date, pValue: request.AttendanceDate)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetAttendanceByEmployee", parameterValues: p))
{
if (dr.Read())
{
response = MapToResponse(dr);
}
dr.Close();
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<GetAttendanceByEmpResponse> UpsertAttendanceByEmpAsync(UpsertAttendanceByEmpRequest request)
{
GetAttendanceByEmpResponse response = new();
try
{
2026-07-09 18:53:22 +06:00
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode,true);
2026-07-08 18:15:24 +06:00
try
{
SqlParameter[] p =
[
2026-07-08 18:37:40 +06:00
SqlHelperExtension.CreateInParam(pName: "@AttendanceDate", pType: SqlDbType.Date, pValue: DateTime.Now),
2026-07-08 18:15:24 +06:00
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
2026-07-09 18:53:22 +06:00
SqlHelperExtension.CreateInParam(pName: "@CheckInTime", pType: SqlDbType.DateTime, pValue: request.CheckInTime != null ? DateTime.Today.Add(request.CheckInTime.Value) : null),
2026-07-08 18:15:24 +06:00
SqlHelperExtension.CreateInParam(pName: "@CheckInLatitude", pType: SqlDbType.VarChar, pValue: request.CheckInLatitude),
SqlHelperExtension.CreateInParam(pName: "@CheckInLongitude", pType: SqlDbType.VarChar, pValue: request.CheckInLongitude),
SqlHelperExtension.CreateInParam(pName: "@CheckInLocationName", pType: SqlDbType.VarChar, pValue: request.CheckInLocationName),
2026-07-09 18:53:22 +06:00
SqlHelperExtension.CreateInParam(pName: "@CheckOutTime", pType: SqlDbType.DateTime, pValue: request.CheckOutTime != null ? DateTime.Today.Add(request.CheckOutTime.Value) : null),
2026-07-08 18:15:24 +06:00
SqlHelperExtension.CreateInParam(pName: "@CheckOutLatitude", pType: SqlDbType.VarChar, pValue: request.CheckOutLatitude),
SqlHelperExtension.CreateInParam(pName: "@CheckOutLongitude", pType: SqlDbType.VarChar, pValue: request.CheckOutLongitude),
SqlHelperExtension.CreateInParam(pName: "@CheckOutLocationName", pType: SqlDbType.VarChar, pValue: request.CheckOutLocationName)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.UpsertAttendanceByEmployee", parameterValues: p))
{
if (dr.Read())
{
response = MapToResponse(dr);
}
dr.Close();
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
private static GetAttendanceByEmpResponse MapToResponse(IDataReader dr)
{
return new GetAttendanceByEmpResponse
{
AttendanceDate = dr.GetDateTime(0),
EmployeeNumber = dr.GetString(1),
2026-07-09 18:53:22 +06:00
CheckInTime = dr.IsDBNull(2) ? null : dr.GetDateTime(2).TimeOfDay,
2026-07-08 18:15:24 +06:00
CheckInLatitude = dr.IsDBNull(3) ? null : dr.GetString(3),
CheckInLongitude = dr.IsDBNull(4) ? null : dr.GetString(4),
CheckInLocationName = dr.IsDBNull(5) ? null : dr.GetString(5),
2026-07-09 18:53:22 +06:00
CheckOutTime = dr.IsDBNull(6) ? null : dr.GetDateTime(6).TimeOfDay,
2026-07-08 18:15:24 +06:00
CheckOutLatitude = dr.IsDBNull(7) ? null : dr.GetString(7),
CheckOutLongitude = dr.IsDBNull(8) ? null : dr.GetString(8),
CheckOutLocationName = dr.IsDBNull(9) ? null : dr.GetString(9)
};
}
public async Task<PagedResult<GetDiscountResponse>> GetDiscountAsync(GetDiscountRequest request)
{
PagedResult<GetDiscountResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@CustomerCode", pType: SqlDbType.VarChar, pValue: request.CustomerCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@MaterialCode", pType: SqlDbType.VarChar, pValue: request.MaterialCode),
SqlHelperExtension.CreateInParam(pName: "@DiscountCode", pType: SqlDbType.VarChar, pValue: request.DiscountCode),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetDiscountPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetDiscountResponse
{
2026-07-30 11:35:10 +06:00
SalesOrgCode = dr.IsDBNull(0) ? null : dr.GetString(0),
DiscountCode = dr.GetString(1),
DiscountName = dr.GetString(2),
2026-08-02 17:24:50 +06:00
DistributorChannelCode = dr.IsDBNull(3) ? null : dr.GetString(3),
DistributorChannelName = dr.IsDBNull(4) ? null : dr.GetString(4),
2026-07-30 11:35:10 +06:00
DiscountType = (DiscountTypeEnum)dr.GetInt32(5)
});
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<PagedResult<GetDiscountMaterialResponse>> GetDiscountMaterialsAsync(GetDiscountMaterialRequest request)
{
PagedResult<GetDiscountMaterialResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@CustomerCode", pType: SqlDbType.VarChar, pValue: request.CustomerCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@MaterialCode", pType: SqlDbType.VarChar, pValue: request.MaterialCode),
SqlHelperExtension.CreateInParam(pName: "@DiscountCode", pType: SqlDbType.VarChar, pValue: request.DiscountCode),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetDiscountMaterialsPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetDiscountMaterialResponse
{
2026-07-30 11:35:10 +06:00
DiscountCode = dr.IsDBNull(0) ? null : dr.GetString(0),
CustomerCode = dr.IsDBNull(1) ? null : dr.GetString(1),
MaterialCode = dr.IsDBNull(2) ? null : dr.GetString(2),
UnitOfMeasurement = dr.IsDBNull(3) ? null : dr.GetString(3),
CalculationType = dr.IsDBNull(4) ? null : dr.GetString(4),
DiscountValue = dr.GetDecimal(5),
StartDate = dr.GetDateTime(6),
EndDate = dr.GetDateTime(7),
2026-07-30 11:35:10 +06:00
Threshold = dr.GetDecimal(8),
2026-08-12 18:29:10 +06:00
SalesOrgCode = dr.GetString(9),
TeritoryCode = dr.GetString(10)
});
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<PagedResult<GetFOCResponse>> GetFOCsAsync(GetFOCRequest request)
{
PagedResult<GetFOCResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@CustomerCode", pType: SqlDbType.VarChar, pValue: request.CustomerCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@MaterialCode", pType: SqlDbType.VarChar, pValue: request.MaterialCode),
SqlHelperExtension.CreateInParam(pName: "@DiscountCode", pType: SqlDbType.VarChar, pValue: request.FOCCode),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetFOCPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetFOCResponse
{
2026-08-02 15:58:59 +06:00
SalesOrgcCode =dr.IsDBNull(0) ? null : dr.GetString(0),
FOCCode = dr.GetString(1),
FOCName = dr.GetString(2),
2026-08-02 15:58:59 +06:00
DistributorChannelCode = dr.IsDBNull(3) ? null : dr.GetString(3),
DistributorChannelName = dr.IsDBNull(4) ? null : dr.GetString(4)
});
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<PagedResult<GetFOCMaterialResponse>> GetFOCMaterialsAsync(GetFOCMaterialRequest request)
{
PagedResult<GetFOCMaterialResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@CustomerCode", pType: SqlDbType.VarChar, pValue: request.CustomerCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@MaterialCode", pType: SqlDbType.VarChar, pValue: request.MaterialCode),
SqlHelperExtension.CreateInParam(pName: "@DiscountCode", pType: SqlDbType.VarChar, pValue: request.FOCCode),
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetFOCMaterialsPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetFOCMaterialResponse
{
FOCCode = dr.GetString(0),
CustomerCode = dr.GetString(1),
MaterialCode = dr.IsDBNull(2)? null : dr.GetString(2),
UnitOfMeasurement = dr.IsDBNull(3) ? null : dr.GetString(3),
MinimumOrderQty = dr.IsDBNull(4) ? null : dr.GetDecimal(4),
ForQuantity = dr.IsDBNull(5) ? null : dr.GetDecimal(5),
FreeQty = dr.IsDBNull(6) ? null : dr.GetDecimal(6),
FOCUnitOfMeasurement = dr.IsDBNull(7) ? null : dr.GetString(7),
StartDate = dr.GetDateTime(8),
EndDate = dr.GetDateTime(9),
2026-08-12 18:29:10 +06:00
SalesOrgCode =dr.GetString(10),
TeritoryCode =dr.GetString(11),
});
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
2026-07-14 12:18:46 +06:00
public async Task<PagedResult<GetPaymentTermResponse>> GetPaymentTermsAsync(GetPaymentTermRequest request)
{
PagedResult<GetPaymentTermResponse> response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.VarChar, pValue: request.SalesOrgCode),
2026-07-14 18:50:43 +06:00
SqlHelperExtension.CreateInParam(pName: "@PaymentTermCode", pType: SqlDbType.VarChar, pValue: request.PaymentTermCode),
2026-07-14 12:18:46 +06:00
SqlHelperExtension.CreateInParam(pName: "@PageNumber", pType: SqlDbType.Int, pValue: request.PageNumber),
SqlHelperExtension.CreateInParam(pName: "@PageSize", pType: SqlDbType.Int, pValue: request.PageSize)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetPaymentTermsPaginated", parameterValues: p))
{
while (dr.Read())
{
response.Items.Add(new GetPaymentTermResponse
{
PaymentTermId = dr.GetInt32(0),
PaymentTermCode = dr.GetString(1),
PaymentTermDescription = dr.GetString(2),
SalesOrgCode = dr.GetString(3),
StartDate = dr.GetDateTime(4),
EndDate = dr.GetDateTime(5),
PaymentTermType = dr.GetString(6),
});
}
dr.Close();
response.TotalCount = response.Items.Count;
response.PageNumber = request.PageNumber;
response.PageSize = request.PageSize;
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception)
{
throw;
}
return response;
}
2026-07-23 15:13:45 +06:00
public async Task<GetPasswordPolicyResponse> GetPasswordPolicyAsync()
{
GetPasswordPolicyResponse response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
2026-07-23 18:25:50 +06:00
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetPasswordPolicy"))
2026-07-23 15:13:45 +06:00
{
if (dr.Read())
{
2026-07-23 18:25:50 +06:00
response=new GetPasswordPolicyResponse()
2026-07-23 15:13:45 +06:00
{
MinLength =dr.IsDBNull(0) ? null: dr.GetInt32(0),
MaxLength = dr.IsDBNull(1) ? null : dr.GetInt32(1),
2026-07-23 18:25:50 +06:00
IsUppercase = dr.GetInt32(2) >0,
IsLowercase = dr.GetInt32(3) > 0,
IsSpecialCharacter = dr.GetInt32(4) > 0,
IsNumber = dr.GetInt32(5) > 0,
PasswordLife = dr.IsDBNull(6) ? null : dr.GetInt32(6),
2026-07-23 15:13:45 +06:00
};
}
dr.Close();
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception)
{
throw;
}
return response;
}
}