OnlineSalesAutoCrop/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs

305 lines
14 KiB
C#

using DocumentFormat.OpenXml.Wordprocessing;
using Ease.NetCore.DataAccess;
using Ease.NetCore.DataAccess.SQL;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using OnlineSalesAutoCrop.CoreAPI.Models.Global;
using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Integrations;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations;
using System;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace OnlineSalesAutoCrop.CoreAPI.Services.Services.Integrations;
public class IntegrationService : IIntegrationService
{
private readonly AppSettings _settings;
public IntegrationService(IOptions<AppSettings> options)
{
_settings = options.Value;
}
public async Task<CustomerByCompanyCodeResponse> GetCustomerByCompanyCodeAsync(CustomerByCompanyCodeRequest request)
{
CustomerByCompanyCodeResponse response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
await GetCustomerByCompanyCodeAsync(tc, request);
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<bool> UpsertCustomerAsync(CustomerIntegrationRequest request)
{
bool response = false;
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
var customer = await GetCustomerByCompanyCodeAsync(tc, new CustomerByCompanyCodeRequest() { CompanyCode = request.CompanyCode, CustomerNumber = request.CustomerNumber });
if( customer != null && customer.CustomerId>0)
{
//Update Here
}
else
{
//Insert Here
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
private async Task<CustomerByCompanyCodeResponse> GetCustomerByCompanyCodeAsync(TransactionContext tc, CustomerByCompanyCodeRequest request)
{
CustomerByCompanyCodeResponse response = new CustomerByCompanyCodeResponse();
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@CustomerNumber", pType: SqlDbType.NVarChar,pValue: request.CustomerNumber ),
SqlHelperExtension.CreateInParam(pName: "@CompanyCode", pType: SqlDbType.NVarChar,pValue: request.CompanyCode ),
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetCustomerByCompanyCode", parameterValues: p))
{
if (dr.Read())
{
response = new CustomerByCompanyCodeResponse()
{
CustomerNumber = dr["CustomerNumber"].ToString(),
CustomerName = dr["CustomerName"].ToString(),
AccountGroup = dr["AccountGroup"].ToString(),
AccountGroupDescription = dr["AccountGroupDescription"].ToString(),
CompanyCode = dr["CompanyCode"].ToString(),
CompanyCodeDescription = dr["CompanyCodeDescription"].ToString(),
SalesOrganization = dr["SalesOrganization"].ToString(),
SalesOrganizationDescription = dr["SalesOrganizationDescription"].ToString(),
DistributionChannel = dr["DistributionChannel"].ToString(),
DistributionChannelDescription = dr["DistributionChannelDescription"].ToString(),
Division = dr["Division"].ToString(),
DivisionDescription = dr["DivisionDescription"].ToString(),
MobileNumber = dr["MobileNumber"].ToString(),
EmailAddress = dr["EmailAddress"].ToString(),
BusinessTaxNumber = dr["BusinessTaxNumber"].ToString(),
CreditLimit = dr["CreditLimit"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["CreditLimit"]),
SalesOffice = dr["SalesOffice"].ToString(),
SalesOfficeDescription = dr["SalesOfficeDescription"].ToString(),
SalesGroup = dr["SalesGroup"].ToString(),
CustomerGroup = dr["CustomerGroup"].ToString(),
Status = dr["Status"].ToString(),
PaymentTerms = dr["PaymentTerms"].ToString(),
SearchTerm = dr["SearchTerm"].ToString(),
Region = dr["Region"].ToString(),
RegionName = dr["RegionName"].ToString(),
Area = dr["Area"].ToString(),
AreaName = dr["AreaName"].ToString(),
SalesUnit = dr["SalesUnit"].ToString(),
SalesUnitName = dr["SalesUnitName"].ToString(),
Territory = dr["Territory"].ToString(),
TerritoryName = dr["TerritoryName"].ToString(),
Plant = dr["Plant"].ToString(),
PlantName = dr["PlantName"].ToString()
};
}
dr.Close();
}
}
catch(Exception ex)
{
throw;
}
return response;
}
public async Task<EmployeeIntegrationResponse> GetEmployeeBySalesOrgAsync(EmployeeBySalesOrgCodeRequest request)
{
EmployeeIntegrationResponse response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
await GetEmployeeBySalesOrgAsync(tc, request);
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<bool> UpsertEmployeeAsync(EmployeeIntegrationRequest request)
{
bool response = false;
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
EmployeeIntegrationResponse employee = await GetEmployeeBySalesOrgAsync(tc, new EmployeeBySalesOrgCodeRequest() { SalesOrgCode = request.SalesOrgCode, EmployeeVendorCode = request.EmployeeVendorCode });
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeVendorCode", pType: SqlDbType.NVarChar, pValue: request.EmployeeVendorCode),
SqlHelperExtension.CreateInParam(pName: "@EmployeeVendorName", pType: SqlDbType.NVarChar, pValue: request.EmployeeVendorName),
SqlHelperExtension.CreateInParam(pName: "@DesignationCode", pType: SqlDbType.NVarChar, pValue: request.DesignationCode),
SqlHelperExtension.CreateInParam(pName: "@DesignationDescription", pType: SqlDbType.NVarChar, pValue: request.DesignationDescription),
SqlHelperExtension.CreateInParam(pName: "@MobileNo", pType: SqlDbType.NVarChar, pValue: request.MobileNo),
SqlHelperExtension.CreateInParam(pName: "@Email", pType: SqlDbType.NVarChar, pValue: request.Email),
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.NVarChar, pValue: request.SalesOrgCode),
SqlHelperExtension.CreateInParam(pName: "@SalesOrgDescription", pType: SqlDbType.NVarChar, pValue: request.SalesOrgDescription),
SqlHelperExtension.CreateInParam(pName: "@DistChannelCode", pType: SqlDbType.NVarChar, pValue: request.DistChannelCode),
SqlHelperExtension.CreateInParam(pName: "@DistChannelDescription", pType: SqlDbType.NVarChar, pValue: request.DistChannelDescription),
SqlHelperExtension.CreateInParam(pName: "@DivisionCode", pType: SqlDbType.NVarChar, pValue: request.DivisionCode),
SqlHelperExtension.CreateInParam(pName: "@DivisionDescription", pType: SqlDbType.NVarChar, pValue: request.DivisionDescription),
SqlHelperExtension.CreateInParam(pName: "@RegionAAl", pType: SqlDbType.NVarChar, pValue: request.RegionAAl),
SqlHelperExtension.CreateInParam(pName: "@AreaCode", pType: SqlDbType.NVarChar, pValue: request.AreaCode),
SqlHelperExtension.CreateInParam(pName: "@AreaDescription", pType: SqlDbType.NVarChar, pValue: request.AreaDescription),
SqlHelperExtension.CreateInParam(pName: "@SalesUnitACCLCode", pType: SqlDbType.NVarChar, pValue: request.SalesUnitACCLCode),
SqlHelperExtension.CreateInParam(pName: "@SalesUnitACCLDescription", pType: SqlDbType.NVarChar, pValue: request.SalesUnitACCLDescription),
SqlHelperExtension.CreateInParam(pName: "@TerritoryCode", pType: SqlDbType.NVarChar, pValue: request.TerritoryCode),
SqlHelperExtension.CreateInParam(pName: "@TerritoryDescription", pType: SqlDbType.NVarChar, pValue: request.TerritoryDescription),
SqlHelperExtension.CreateInParam(pName: "@SalesOfficeCode", pType: SqlDbType.NVarChar, pValue: request.SalesOfficeCode),
SqlHelperExtension.CreateInParam(pName: "@SalesOfficeDescription", pType: SqlDbType.NVarChar, pValue: request.SalesOfficeDescription),
SqlHelperExtension.CreateInParam(pName: "@PlantCode", pType: SqlDbType.NVarChar, pValue: request.PlantCode),
SqlHelperExtension.CreateInParam(pName: "@PlantDescription", pType: SqlDbType.NVarChar, pValue: request.PlantDescription),
SqlHelperExtension.CreateInParam(pName: "@Status", pType: SqlDbType.NVarChar, pValue: request.Status),
SqlHelperExtension.CreateInParam(pName: "@StatusDescription", pType: SqlDbType.NVarChar, pValue: request.StatusDescription),
];
if (employee != null && employee.EmployeeVendorId > 0)
{
_= tc.ExecuteNonQuerySp("dbo.UpdateEmployee", p);
}
else
{
_= tc.ExecuteNonQuerySp("dbo.InsertEmployee", p);
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
private async Task<EmployeeIntegrationResponse> GetEmployeeBySalesOrgAsync(TransactionContext tc, EmployeeBySalesOrgCodeRequest request)
{
EmployeeIntegrationResponse response = new EmployeeIntegrationResponse();
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeVendorCode", pType: SqlDbType.NVarChar,pValue: request.EmployeeVendorCode ),
SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.NVarChar,pValue: request.SalesOrgCode ),
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetEmployeeBySalesOrgCode", parameterValues: p))
{
if (dr.Read())
{
response = new EmployeeIntegrationResponse()
{
EmployeeVendorId = dr.GetInt32(0),
EmployeeVendorCode = dr.GetString(1),
EmployeeVendorName = dr.GetString(2),
DesignationCode = dr.GetString(3),
DesignationDescription = dr.GetString(4),
MobileNo = dr.GetString(5),
Email = dr.GetString(6),
SalesOrgCode = dr.GetString(7),
SalesOrgDescription = dr.GetString(8),
DistChannelCode = dr.GetString(9),
DistChannelDescription = dr.GetString(10),
DivisionCode = dr.GetString(11),
DivisionDescription = dr.GetString(12),
RegionAAl = dr.GetString(13),
AreaCode = dr.GetString(14),
AreaDescription = dr.GetString(15),
SalesUnitACCLCode = dr.GetString(16),
SalesUnitACCLDescription = dr.GetString(17),
TerritoryCode = dr.GetString(18),
TerritoryDescription = dr.GetString(19),
SalesOfficeCode = dr.GetString(20),
SalesOfficeDescription = dr.GetString(21),
PlantCode = dr.GetString(22),
PlantDescription = dr.GetString(23),
Status = dr.GetString(24),
StatusDescription = dr.GetString(25)
};
}
dr.Close();
}
}
catch (Exception ex)
{
throw DBCustomError.GenerateCustomError(ex);
}
return response;
}
}