311 lines
15 KiB
C#
311 lines
15 KiB
C#
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<CustomerIntegrationResponse> UpsertCustomerAsync(CustomerIntegrationRequest request)
|
|
{
|
|
CustomerIntegrationResponse response = new();
|
|
try
|
|
{
|
|
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode,true);
|
|
try
|
|
{
|
|
var customer = await GetCustomerByCompanyCodeAsync(tc, new CustomerByCompanyCodeRequest() { CompanyCode = request.CompanyCode, CustomerNumber = request.CustomerNumber });
|
|
|
|
if( customer != null && customer.CustomerId>0)
|
|
{
|
|
//Update Here
|
|
UpdateCustomer(tc, customer.CustomerId, request);
|
|
response.CustomerNumber = request.CustomerNumber;
|
|
response.CompanyCode = request.CompanyCode;
|
|
response.IsUpdated = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
//Insert Here
|
|
|
|
CreateCustomer(tc, request);
|
|
response.CustomerNumber = request.CustomerNumber;
|
|
response.CompanyCode = request.CompanyCode;
|
|
response.IsUpdated = false;
|
|
|
|
}
|
|
|
|
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()
|
|
{
|
|
CustomerId =Convert.ToInt32( dr["CustomerId"].ToString()),
|
|
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 bool CreateCustomer(TransactionContext tc, CustomerIntegrationRequest request)
|
|
{
|
|
bool returnValue = false;
|
|
|
|
try
|
|
{
|
|
SqlParameter[] p =
|
|
[
|
|
SqlHelperExtension.CreateInParam("@CustomerNumber", SqlDbType.VarChar, request.CustomerNumber),
|
|
SqlHelperExtension.CreateInParam("@CustomerName", SqlDbType.VarChar, request.CustomerName),
|
|
|
|
SqlHelperExtension.CreateInParam("@AccountGroup", SqlDbType.VarChar, request.AccountGroup),
|
|
SqlHelperExtension.CreateInParam("@AccountGroupDescription", SqlDbType.VarChar, request.AccountGroupDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@CompanyCode", SqlDbType.VarChar, request.CompanyCode),
|
|
SqlHelperExtension.CreateInParam("@CompanyCodeDescription", SqlDbType.VarChar, request.CompanyCodeDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesOrganization", SqlDbType.VarChar, request.SalesOrganization),
|
|
SqlHelperExtension.CreateInParam("@SalesOrganizationDescription", SqlDbType.VarChar, request.SalesOrganizationDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@DistributionChannel", SqlDbType.VarChar, request.DistributionChannel),
|
|
SqlHelperExtension.CreateInParam("@DistributionChannelDescription", SqlDbType.VarChar, request.DistributionChannelDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@Division", SqlDbType.VarChar, request.Division),
|
|
SqlHelperExtension.CreateInParam("@DivisionDescription", SqlDbType.VarChar, request.DivisionDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@MobileNumber", SqlDbType.VarChar, request.MobileNumber),
|
|
SqlHelperExtension.CreateInParam("@EmailAddress", SqlDbType.VarChar, request.EmailAddress),
|
|
|
|
SqlHelperExtension.CreateInParam("@BusinessTaxNumber", SqlDbType.VarChar, request.BusinessTaxNumber),
|
|
SqlHelperExtension.CreateInParam("@CreditLimit", SqlDbType.Decimal, request.CreditLimit),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesOffice", SqlDbType.VarChar, request.SalesOffice),
|
|
SqlHelperExtension.CreateInParam("@SalesOfficeDescription", SqlDbType.VarChar, request.SalesOfficeDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesGroup", SqlDbType.VarChar, request.SalesGroup),
|
|
SqlHelperExtension.CreateInParam("@CustomerGroup", SqlDbType.VarChar, request.CustomerGroup),
|
|
|
|
SqlHelperExtension.CreateInParam("@Status", SqlDbType.VarChar, request.Status),
|
|
SqlHelperExtension.CreateInParam("@PaymentTerms", SqlDbType.VarChar, request.PaymentTerms),
|
|
|
|
SqlHelperExtension.CreateInParam("@SearchTerm", SqlDbType.VarChar, request.SearchTerm),
|
|
|
|
SqlHelperExtension.CreateInParam("@Region", SqlDbType.VarChar, request.Region),
|
|
SqlHelperExtension.CreateInParam("@RegionName", SqlDbType.VarChar, request.RegionName),
|
|
|
|
SqlHelperExtension.CreateInParam("@Area", SqlDbType.VarChar, request.Area),
|
|
SqlHelperExtension.CreateInParam("@AreaName", SqlDbType.VarChar, request.AreaName),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesUnit", SqlDbType.VarChar, request.SalesUnit),
|
|
SqlHelperExtension.CreateInParam("@SalesUnitName", SqlDbType.VarChar, request.SalesUnitName),
|
|
|
|
SqlHelperExtension.CreateInParam("@Territory", SqlDbType.VarChar, request.Territory),
|
|
SqlHelperExtension.CreateInParam("@TerritoryName", SqlDbType.VarChar, request.TerritoryName),
|
|
|
|
SqlHelperExtension.CreateInParam("@Plant", SqlDbType.VarChar, request.Plant),
|
|
SqlHelperExtension.CreateInParam("@PlantName", SqlDbType.VarChar, request.PlantName)
|
|
];
|
|
|
|
_ = tc.ExecuteNonQuerySp(spName: "dbo.CreateCustomer", parameterValues: p);
|
|
|
|
returnValue = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
public bool UpdateCustomer(TransactionContext tc,int customerId, CustomerIntegrationRequest request)
|
|
{
|
|
bool returnValue = false;
|
|
|
|
try
|
|
{
|
|
SqlParameter[] p =
|
|
[
|
|
SqlHelperExtension.CreateInParam("@CustomerId", SqlDbType.Int,customerId),
|
|
SqlHelperExtension.CreateInParam("@CustomerNumber", SqlDbType.VarChar, request.CustomerNumber),
|
|
SqlHelperExtension.CreateInParam("@CompanyCode", SqlDbType.VarChar, request.CompanyCode),
|
|
|
|
SqlHelperExtension.CreateInParam("@CustomerName", SqlDbType.VarChar, request.CustomerName),
|
|
SqlHelperExtension.CreateInParam("@AccountGroup", SqlDbType.VarChar, request.AccountGroup),
|
|
SqlHelperExtension.CreateInParam("@AccountGroupDescription", SqlDbType.VarChar, request.AccountGroupDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@CompanyCodeDescription", SqlDbType.VarChar, request.CompanyCodeDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesOrganization", SqlDbType.VarChar, request.SalesOrganization),
|
|
SqlHelperExtension.CreateInParam("@SalesOrganizationDescription", SqlDbType.VarChar, request.SalesOrganizationDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@DistributionChannel", SqlDbType.VarChar, request.DistributionChannel),
|
|
SqlHelperExtension.CreateInParam("@DistributionChannelDescription", SqlDbType.VarChar, request.DistributionChannelDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@Division", SqlDbType.VarChar, request.Division),
|
|
SqlHelperExtension.CreateInParam("@DivisionDescription", SqlDbType.VarChar, request.DivisionDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@MobileNumber", SqlDbType.VarChar, request.MobileNumber),
|
|
SqlHelperExtension.CreateInParam("@EmailAddress", SqlDbType.VarChar, request.EmailAddress),
|
|
|
|
SqlHelperExtension.CreateInParam("@BusinessTaxNumber", SqlDbType.VarChar, request.BusinessTaxNumber),
|
|
SqlHelperExtension.CreateInParam("@CreditLimit", SqlDbType.Decimal, request.CreditLimit),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesOffice", SqlDbType.VarChar, request.SalesOffice),
|
|
SqlHelperExtension.CreateInParam("@SalesOfficeDescription", SqlDbType.VarChar, request.SalesOfficeDescription),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesGroup", SqlDbType.VarChar, request.SalesGroup),
|
|
SqlHelperExtension.CreateInParam("@CustomerGroup", SqlDbType.VarChar, request.CustomerGroup),
|
|
|
|
SqlHelperExtension.CreateInParam("@Status", SqlDbType.VarChar, request.Status),
|
|
SqlHelperExtension.CreateInParam("@PaymentTerms", SqlDbType.VarChar, request.PaymentTerms),
|
|
|
|
SqlHelperExtension.CreateInParam("@SearchTerm", SqlDbType.VarChar, request.SearchTerm),
|
|
|
|
SqlHelperExtension.CreateInParam("@Region", SqlDbType.VarChar, request.Region),
|
|
SqlHelperExtension.CreateInParam("@RegionName", SqlDbType.VarChar, request.RegionName),
|
|
|
|
SqlHelperExtension.CreateInParam("@Area", SqlDbType.VarChar, request.Area),
|
|
SqlHelperExtension.CreateInParam("@AreaName", SqlDbType.VarChar, request.AreaName),
|
|
|
|
SqlHelperExtension.CreateInParam("@SalesUnit", SqlDbType.VarChar, request.SalesUnit),
|
|
SqlHelperExtension.CreateInParam("@SalesUnitName", SqlDbType.VarChar, request.SalesUnitName),
|
|
|
|
SqlHelperExtension.CreateInParam("@Territory", SqlDbType.VarChar, request.Territory),
|
|
SqlHelperExtension.CreateInParam("@TerritoryName", SqlDbType.VarChar, request.TerritoryName),
|
|
|
|
SqlHelperExtension.CreateInParam("@Plant", SqlDbType.VarChar, request.Plant),
|
|
SqlHelperExtension.CreateInParam("@PlantName", SqlDbType.VarChar, request.PlantName)
|
|
];
|
|
|
|
_ = tc.ExecuteNonQuerySp(
|
|
spName: "dbo.UpdateCustomerById",
|
|
parameterValues: p
|
|
);
|
|
|
|
returnValue = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new InvalidOperationException(e.Message, e);
|
|
}
|
|
|
|
return returnValue;
|
|
}
|
|
}
|