diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/EmployeeIntegrationRequest.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/EmployeeIntegrationRequest.cs new file mode 100644 index 0000000..cd97cf4 --- /dev/null +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/EmployeeIntegrationRequest.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace OnlineSalesAutoCrop.CoreAPI.Models.Requests.Integrations; + +public class EmployeeIntegrationRequest +{ + public string EmployeeVendorCode { get; set; } + public string EmployeeVendorName { get; set; } + public string DesignationCode { get; set; } + public string DesignationDescription { get; set; } + public string MobileNo { get; set; } + public string Email { get; set; } + public string SalesOrgCode { get; set; } + public string SalesOrgDescription { get; set; } + public string DistChannelCode { get; set; } + public string DistChannelDescription { get; set; } + public string DivisionCode { get; set; } + public string DivisionDescription { get; set; } + public string RegionAAl { get; set; } + public string AreaCode { get; set; } + public string AreaDescription { get; set; } + public string SalesUnitACCLCode { get; set; } + public string SalesUnitACCLDescription { get; set; } + public string TerritoryCode { get; set; } + public string TerritoryDescription { get; set; } + public string SalesOfficeCode { get; set; } + public string SalesOfficeDescription { get; set; } + public string PlantCode { get; set; } + public string PlantDescription { get; set; } + public string Status { get; set; } + public string StatusDescription { get; set; } +} + +public class EmployeeBySalesOrgCodeRequest +{ + public string EmployeeVendorCode { get; set; } = string.Empty; + public string SalesOrgCode { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Responses/Integrations/EmployeeIntegrationResponse.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Responses/Integrations/EmployeeIntegrationResponse.cs new file mode 100644 index 0000000..05258fe --- /dev/null +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Responses/Integrations/EmployeeIntegrationResponse.cs @@ -0,0 +1,38 @@ +namespace OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations; + +public class EmployeeIntegrationResponse : ResponseBase +{ + + public int EmployeeVendorId { get; set; } + public string EmployeeVendorCode { get; set; } + public string EmployeeVendorName { get; set; } + public string DesignationCode { get; set; } + public string DesignationDescription { get; set; } + public string MobileNo { get; set; } + public string Email { get; set; } + public string SalesOrgCode { get; set; } + public string SalesOrgDescription { get; set; } + public string DistChannelCode { get; set; } + public string DistChannelDescription { get; set; } + public string DivisionCode { get; set; } + public string DivisionDescription { get; set; } + public string RegionAAl { get; set; } + public string AreaCode { get; set; } + public string AreaDescription { get; set; } + public string SalesUnitACCLCode { get; set; } + public string SalesUnitACCLDescription { get; set; } + public string TerritoryCode { get; set; } + public string TerritoryDescription { get; set; } + public string SalesOfficeCode { get; set; } + public string SalesOfficeDescription { get; set; } + public string PlantCode { get; set; } + public string PlantDescription { get; set; } + public string Status { get; set; } + public string StatusDescription { get; set; } +} + + +public class EmployeeIntegrationReqResponse : ResponseBase +{ + +} \ No newline at end of file diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Integrations/IIntegrationService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Integrations/IIntegrationService.cs index 77c84b3..4859b2e 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Integrations/IIntegrationService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Integrations/IIntegrationService.cs @@ -9,4 +9,6 @@ public interface IIntegrationService { Task UpsertCustomerAsync(CustomerIntegrationRequest request); Task GetCustomerByCompanyCodeAsync(CustomerByCompanyCodeRequest request); + Task UpsertEmployeeAsync(EmployeeIntegrationRequest request); + Task GetEmployeeBySalesOrgAsync(EmployeeBySalesOrgCodeRequest request); } diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs index 4cc48b7..411fb99 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs @@ -1,4 +1,5 @@ -using Ease.NetCore.DataAccess; +using DocumentFormat.OpenXml.Wordprocessing; +using Ease.NetCore.DataAccess; using Ease.NetCore.DataAccess.SQL; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Options; @@ -162,6 +163,155 @@ public class IntegrationService : IIntegrationService return response; } + + public async Task 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 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 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; + } + public bool CreateCustomer(TransactionContext tc, CustomerIntegrationRequest request) { bool returnValue = false; @@ -307,4 +457,6 @@ public class IntegrationService : IIntegrationService return returnValue; } + + } diff --git a/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationController.cs b/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationController.cs index 1b16add..03986bb 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationController.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Integrations; +using OnlineSalesAutoCrop.CoreAPI.Models.Responses; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations; using OnlineSalesAutoCrop.CoreAPI.Services.Services.Integrations; @@ -70,5 +71,46 @@ namespace OnlineSalesAutoCrop.CoreAPI.Controllers.IntegretionApi return StatusCode(StatusCodes.Status500InternalServerError, response); } } + + /// + /// Insert or Update Employees for SAP + /// + /// + /// + /// + /// if created return 201 or if updated return 204 true + [HttpGet("Employees")] + [IgnoreAntiforgeryToken] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(EmployeeIntegrationReqResponse))] + public async Task UpsertEmployee(EmployeeIntegrationRequest request) + { + EmployeeIntegrationReqResponse response = new EmployeeIntegrationReqResponse(); + try + { + bool result = await _integrationService.UpsertEmployeeAsync(request); + if (result) + { + response.ReturnMessage.Add($"Employee Created Successfully for EmployeeVendorName :{request.EmployeeVendorName} & SalesOrgCode:{request.SalesOrgCode}"); + response.ReturnStatus = StatusCodes.Status201Created; + return StatusCode(StatusCodes.Status201Created, response); + } + else + { + response.ReturnMessage.Add($"Employee Updated Successfully for EmployeeVendorName :{request.EmployeeVendorName} & SalesOrgCode:{request.SalesOrgCode}"); + response.ReturnStatus = StatusCodes.Status204NoContent; + return StatusCode(StatusCodes.Status204NoContent, response); + } + } + catch (Exception ex) + { + string msg = $"Exception Occur in Employee Operation {request?.EmployeeVendorName}~{request?.SalesOrgCode}"; + _logger.LogError(exception: ex, msg); + response.ReturnStatus = StatusCodes.Status500InternalServerError; + response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError, response); + } + } + + } } \ No newline at end of file