using Ease.NetCore.DataAccess; using Ease.NetCore.DataAccess.SQL; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Options; using MySqlX.XDevAPI.Common; using OnlineSalesAutoCrop.CoreAPI.Models.Global; using OnlineSalesAutoCrop.CoreAPI.Models.Requests.MobileApp; using OnlineSalesAutoCrop.CoreAPI.Models.Responses; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.MobileApp; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.MobileApp; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Systems; using System; using System.Collections.Generic; using System.Data; using System.Threading.Tasks; namespace OnlineSalesAutoCrop.CoreAPI.Services.Services.MobileApp; public class MobileMasterDataService : IMobileMasterDataService { private readonly AppSettings _settings; private readonly IUserService _userService; public MobileMasterDataService(IOptions options, IUserService userService) { _settings = options.Value; _userService = userService; } public async Task 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> GetBrandsAsync(GetBrandsRequest request) { PagedResult 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; } public async Task> GetMaterialsAsync(GetMaterialsRequest request) { PagedResult 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), SalesOrg = dr.GetString(11), BasePrice = dr.GetDecimal(12) }); } 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> GetCustomersAsync(GetCustomersRequest request) { PagedResult 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), EmailAddress = dr.GetString(9), TaxNumber = dr.GetString(10), SalesGroupCode = dr.GetString(11), SalesGroupName = dr.GetString(12), CustomerGroupCode = dr.GetString(13), CustomerGroupName = dr.GetString(14), CustomerPriceGroupCode = dr.GetString(15), CustomerPriceGroupName = dr.GetString(16), PaymentTermCode = dr.GetString(17), TeritoryCode = dr.GetString(18), SalesOrgCode = dr.GetString(19), PlantCode = dr.GetString(20) }); } 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; } private async Task> GetCompaniesByEmployeeNumberAsync(TransactionContext tc, string employeeNumber) { List 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> GetSalesOrgsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber) { List 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> GetDistributionChannelsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber) { List 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), SalesOrgCode = dr.GetString(1), DistributorChannelCode = dr.GetString(2), DistributorChannelName = dr.GetString(3) }); } dr.Close(); } } catch (Exception ex) { throw DBCustomError.GenerateCustomError(ex); } return response; } private async Task> GetRegionsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber) { List 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> GetAreasByEmployeeNumberAsync(TransactionContext tc, string employeeNumber) { List 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> GetTerritoriesByEmployeeNumberAsync(TransactionContext tc, string employeeNumber) { List 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> GetPlantsByEmployeeNumberAsync(TransactionContext tc, string employeeNumber) { List 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; } public async Task GetAttendanceByEmpAsync(GetAttendanceByEmpRequest request) { GetAttendanceByEmpResponse response = new(); 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 UpsertAttendanceByEmpAsync(UpsertAttendanceByEmpRequest request) { GetAttendanceByEmpResponse response = new(); try { using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode,true); try { SqlParameter[] p = [ SqlHelperExtension.CreateInParam(pName: "@AttendanceDate", pType: SqlDbType.Date, pValue: DateTime.Now), SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber), SqlHelperExtension.CreateInParam(pName: "@CheckInTime", pType: SqlDbType.DateTime, pValue: request.CheckInTime != null ? DateTime.Today.Add(request.CheckInTime.Value) : null), 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), SqlHelperExtension.CreateInParam(pName: "@CheckOutTime", pType: SqlDbType.DateTime, pValue: request.CheckOutTime != null ? DateTime.Today.Add(request.CheckOutTime.Value) : null), 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), CheckInTime = dr.IsDBNull(2) ? null : dr.GetDateTime(2).TimeOfDay, CheckInLatitude = dr.IsDBNull(3) ? null : dr.GetString(3), CheckInLongitude = dr.IsDBNull(4) ? null : dr.GetString(4), CheckInLocationName = dr.IsDBNull(5) ? null : dr.GetString(5), CheckOutTime = dr.IsDBNull(6) ? null : dr.GetDateTime(6).TimeOfDay, CheckOutLatitude = dr.IsDBNull(7) ? null : dr.GetString(7), CheckOutLongitude = dr.IsDBNull(8) ? null : dr.GetString(8), CheckOutLocationName = dr.IsDBNull(9) ? null : dr.GetString(9) }; } }