2026-06-16 17:22:46 +06:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-16 17:51:28 +06:00
|
|
|
|
public async Task<CustomerIntegrationResponse> UpsertCustomerAsync(CustomerIntegrationRequest request)
|
2026-06-16 17:22:46 +06:00
|
|
|
|
{
|
2026-06-16 17:51:28 +06:00
|
|
|
|
CustomerIntegrationResponse response = new();
|
2026-06-16 17:22:46 +06:00
|
|
|
|
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
|
2026-06-16 17:51:28 +06:00
|
|
|
|
|
|
|
|
|
|
response.CustomerNumber = request.CustomerNumber;
|
|
|
|
|
|
response.CompanyCode = request.CompanyCode;
|
|
|
|
|
|
response.IsUpdated = true;
|
2026-06-16 17:22:46 +06:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//Insert Here
|
2026-06-16 17:51:28 +06:00
|
|
|
|
|
|
|
|
|
|
response.CustomerNumber = request.CustomerNumber;
|
|
|
|
|
|
response.CompanyCode = request.CompanyCode;
|
|
|
|
|
|
response.IsUpdated = false;
|
2026-06-16 17:22:46 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|