EchoTex_Payroll/HRM.DA/Service/ApiIntegrationService/ApiIntegrationService.cs
2024-10-14 10:01:49 +06:00

250 lines
18 KiB
C#

using HRM.BO;
using HRM.DA;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using NPOI.SS.Formula.Functions;
using System.Text;
using Microsoft.Data.SqlClient;
using System.Linq;
using Azure;
using System.Runtime.ConstrainedExecution;
using static iTextSharp.text.pdf.AcroFields;
using System.Net;
using System.Security.Policy;
using static Org.BouncyCastle.Math.EC.ECCurve;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using iTextSharp.text.pdf.qrcode;
using Newtonsoft.Json;
using iTextSharp.text;
using Azure.Core;
namespace HRM.DA
{
public class ApiIntegrationService : IApiIntegrationService
{
private readonly HttpClient _httpClient;
//string tokenUrl = "https://oauthasservices-c4badc49a.ap1.hana.ondemand.com/oauth2/api/v1/token?grant_type=client_credentials";
//string clientId = "SF-Payroll";
//string clientSecret = "Berger@2024";
//string masterDataUrl = "https://e200367-iflmap.hcisbt.ap1.hana.ondemand.com/http/BergerPayroll/MaterData";
//string compensationDataUrl = "https://e200367-iflmap.hcisbt.ap1.hana.ondemand.com/http/BergerPayroll/CompensationData";
string tokenUrl = "https://oauthasservices-c3caaf40c.ap1.hana.ondemand.com/oauth2/api/v1/token?grant_type=client_credentials";
string clientId = "SF-Payroll";
string clientSecret = "Berger@24";
string masterDataUrl = "https://l200836-iflmap.hcisbp.ap1.hana.ondemand.com/http/BergerPayroll/MasterData";
string compensationDataUrl = "https://l200836-iflmap.hcisbp.ap1.hana.ondemand.com/http/BergerPayroll/CompensationData";
public ApiIntegrationService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<Token> GetAccessTokenAsync()
{
Token token = null;
try
{
//using (var httpClient = new HttpClient())
//{
var request = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
var authHeaderValue = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
request.Content = new StringContent("", Encoding.UTF8, "application/x-www-form-urlencoded");
// httpClient.Timeout = TimeSpan.FromMinutes(10);
using (var response = await _httpClient.SendAsync(request))
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string responseString = await response.Content.ReadAsStringAsync();
token = JsonConvert.DeserializeObject<Token>(responseString);
}
else
{
throw new Exception($"Failed to retrieve access token. Status code: {response.StatusCode}, Response: {await response.Content.ReadAsStringAsync()}");
}
}
// }
return token;
}
catch (Exception ex)
{
throw new Exception("An error occurred while retrieving the access token.", ex);
}
}
public async Task<DataIntegrationHeader> GetMasterDataAsync(string accessToken)
{
DataIntegrationHeader header = new DataIntegrationHeader();
header.Items = new List<DataIntegration>();
try
{
using (var httpClient = new HttpClient())
{
//var client = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " +accessToken);
httpClient.Timeout = TimeSpan.FromMinutes(10);
var responseContent = await httpClient.GetStringAsync(masterDataUrl);
// var request = new HttpRequestMessage(HttpMethod.Get, masterDataUrl);
// request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
//// request.Content = new StringContent("", Encoding.UTF8, "application/x-www-form-urlencoded");
// httpClient.Timeout = TimeSpan.FromMinutes(10);
//using (var response = await httpClient.SendAsync(request))
//{
if (responseContent != null)
{
// string responseContent = await response.Content.ReadAsStringAsync();
// DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(responseContent);
// var root = JsonConvert.DeserializeObject<object>(responseContent);
var jsonObject = JObject.Parse(responseContent);
var items = jsonObject["row"];
DataTable dataTable = (DataTable)JsonConvert.DeserializeObject(items.ToString(), (typeof(DataTable)));
foreach (DataRow odRow in dataTable.Rows)
{
DataIntegration dataIntegration = new DataIntegration();
dataIntegration.EmployeeNo = !string.IsNullOrEmpty(odRow["EMPLOYEE_NO"].ToString()) ? odRow["EMPLOYEE_NO"].ToString() : string.Empty;
dataIntegration.EmployeeName = !string.IsNullOrEmpty(odRow["EMPLOYEE_NAME"].ToString()) ? odRow["EMPLOYEE_NAME"].ToString() : string.Empty;
dataIntegration.DesignationCode = !string.IsNullOrEmpty(odRow["DESIGNATION_CODE"].ToString()) ? odRow["DESIGNATION_CODE"].ToString() : string.Empty;
dataIntegration.Designation = !string.IsNullOrEmpty(odRow["DESIGNATION_NAME"].ToString()) ? odRow["DESIGNATION_NAME"].ToString() : string.Empty;
dataIntegration.DateOfBirth = !string.IsNullOrEmpty(odRow["DATE_OF_BIRTH"].ToString()) ? odRow["DATE_OF_BIRTH"].ToString() : string.Empty;
dataIntegration.DateOfJoining = !string.IsNullOrEmpty(odRow["DATE_OF_JOINING"].ToString()) ? odRow["DATE_OF_JOINING"].ToString() : string.Empty;
dataIntegration.DiscontinueDateRegion = !string.IsNullOrEmpty(odRow["DISCONTINUE_DATE_REGION"].ToString()) ? odRow["DISCONTINUE_DATE_REGION"].ToString() : string.Empty;
dataIntegration.ConfirmationDate = !string.IsNullOrEmpty(odRow["CONFIRMATION_DATE"].ToString()) ? odRow["CONFIRMATION_DATE"].ToString() : string.Empty;
dataIntegration.CategoryCode = !string.IsNullOrEmpty(odRow["CATEGORY_CODE"].ToString()) ? odRow["CATEGORY_CODE"].ToString() : string.Empty;
dataIntegration.Category = !string.IsNullOrEmpty(odRow["CATEGORY_NAME"].ToString()) ? odRow["CATEGORY_NAME"].ToString() : string.Empty;
dataIntegration.ReligionCode = !string.IsNullOrEmpty(odRow["RELIGION_CODE"].ToString()) ? odRow["RELIGION_CODE"].ToString() : string.Empty;
dataIntegration.Religion = !string.IsNullOrEmpty(odRow["RELIGION_NAME"].ToString()) ? odRow["RELIGION_NAME"].ToString() : string.Empty;
dataIntegration.MaritalStatus = !string.IsNullOrEmpty(odRow["MARITAL_STATUS"].ToString()) ? odRow["MARITAL_STATUS"].ToString() : string.Empty;
dataIntegration.Gender = !string.IsNullOrEmpty(odRow["GENDER"].ToString()) ? odRow["GENDER"].ToString() : string.Empty;
dataIntegration.TIN = !string.IsNullOrEmpty(odRow["TIN"].ToString()) ? odRow["TIN"].ToString() : string.Empty;
dataIntegration.PayScaleLevelCode = !string.IsNullOrEmpty(odRow["GRADE_CODE"].ToString()) ? odRow["GRADE_CODE"].ToString() : string.Empty;
dataIntegration.PayScaleLevel = !string.IsNullOrEmpty(odRow["GRADE_NAME"].ToString()) ? odRow["GRADE_NAME"].ToString() : string.Empty;
dataIntegration.GradeCode = !string.IsNullOrEmpty(odRow["Pay_Scale_Level_Code"].ToString()) ? odRow["Pay_Scale_Level_Code"].ToString() : string.Empty;
dataIntegration.Grade = !string.IsNullOrEmpty(odRow["Pay_Scale_Level"].ToString()) ? odRow["Pay_Scale_Level"].ToString() : string.Empty;
dataIntegration.IsForeignExpatriate = !string.IsNullOrEmpty(odRow["IS_FOREIGN_EXPATRIATE"].ToString()) ? odRow["IS_FOREIGN_EXPATRIATE"].ToString() : string.Empty;
dataIntegration.PFMembership = !string.IsNullOrEmpty(odRow["IS_PF_MEMBER"].ToString()) ? odRow["IS_PF_MEMBER"].ToString() : string.Empty;
dataIntegration.DivisionCode = !string.IsNullOrEmpty(odRow["DIVISION_CODE"].ToString()) ? odRow["DIVISION_CODE"].ToString() : string.Empty;
dataIntegration.Division = !string.IsNullOrEmpty(odRow["DIVISION_NAME"].ToString()) ? odRow["DIVISION_NAME"].ToString() : string.Empty;
dataIntegration.DepartmentCode = !string.IsNullOrEmpty(odRow["DEPARTMENT_CODE"].ToString()) ? odRow["DEPARTMENT_CODE"].ToString() : string.Empty;
dataIntegration.Department = !string.IsNullOrEmpty(odRow["DEPARTMENT_NAME"].ToString()) ? odRow["DEPARTMENT_NAME"].ToString() : string.Empty;
dataIntegration.SubDepartmentCode = !string.IsNullOrEmpty(odRow["SUB_DEPARTMENT_CODE"].ToString()) ? odRow["SUB_DEPARTMENT_CODE"].ToString() : string.Empty;
dataIntegration.SubDepartment = !string.IsNullOrEmpty(odRow["SUB_DEPARTMENT_NAME"].ToString()) ? odRow["SUB_DEPARTMENT_NAME"].ToString() : string.Empty;
dataIntegration.SectionCode = !string.IsNullOrEmpty(odRow["SECTION_CODE"].ToString()) ? odRow["SECTION_CODE"].ToString() : string.Empty;
dataIntegration.Section = !string.IsNullOrEmpty(odRow["SECTION_NAME"].ToString()) ? odRow["SECTION_NAME"].ToString() : string.Empty;
dataIntegration.LocationCode = !string.IsNullOrEmpty(odRow["LOCATION_CODE"].ToString()) ? odRow["LOCATION_CODE"].ToString() : string.Empty;
dataIntegration.Location = !string.IsNullOrEmpty(odRow["LOCATION_NAME"].ToString()) ? odRow["LOCATION_NAME"].ToString() : string.Empty;
dataIntegration.ZoneCode = !string.IsNullOrEmpty(odRow["ZONE_CODE"].ToString()) ? odRow["ZONE_CODE"].ToString() : string.Empty;
dataIntegration.Zone = !string.IsNullOrEmpty(odRow["ZONE_NAME"].ToString()) ? odRow["ZONE_NAME"].ToString() : string.Empty;
dataIntegration.RegionCode = !string.IsNullOrEmpty(odRow["REGION_CODE"].ToString()) ? odRow["REGION_CODE"].ToString() : string.Empty;
dataIntegration.RegionName = !string.IsNullOrEmpty(odRow["REGION_NAME"].ToString()) ? odRow["REGION_NAME"].ToString() : string.Empty;
dataIntegration.CostCenterCode = !string.IsNullOrEmpty(odRow["COST_CENTER_CODE"].ToString()) ? odRow["COST_CENTER_CODE"].ToString() : string.Empty;
dataIntegration.CostCenterName = !string.IsNullOrEmpty(odRow["COST_CENTER_NAME"].ToString()) ? odRow["COST_CENTER_NAME"].ToString() : string.Empty;
dataIntegration.AssignmentChangeDate = !string.IsNullOrEmpty(odRow["LIFE_CYCLE_EFFECT_DATE"].ToString()) ? odRow["LIFE_CYCLE_EFFECT_DATE"].ToString() : string.Empty;
dataIntegration.LIFE_CYCLE_EVENT = !string.IsNullOrEmpty(odRow["LIFE_CYCLE_EVENT"].ToString()) ? odRow["LIFE_CYCLE_EVENT"].ToString() : string.Empty;
dataIntegration.SalaryBankCode = !string.IsNullOrEmpty(odRow["EMPLOYEE_BANK_CODE"].ToString()) ? odRow["EMPLOYEE_BANK_CODE"].ToString() : string.Empty;
dataIntegration.SalaryBank = !string.IsNullOrEmpty(odRow["EMPLOYEE_BANK_NAME"].ToString()) ? odRow["EMPLOYEE_BANK_NAME"].ToString() : string.Empty;
dataIntegration.SalaryBranchCode = !string.IsNullOrEmpty(odRow["EMPLOYEE_BRANCH_CODE"].ToString()) ? odRow["EMPLOYEE_BRANCH_CODE"].ToString() : string.Empty;
dataIntegration.SalaryBranch = !string.IsNullOrEmpty(odRow["EMPLOYEE_BRANCH_NAME"].ToString()) ? odRow["EMPLOYEE_BRANCH_NAME"].ToString() : string.Empty;
dataIntegration.SalaryAccountNumber = !string.IsNullOrEmpty(odRow["EMPLOYEE_ACCOUNT_NUMBER"].ToString()) ? odRow["EMPLOYEE_ACCOUNT_NUMBER"].ToString() : string.Empty;
dataIntegration.SALARY_ACCOUNT_EFFECT_DATE = !string.IsNullOrEmpty(odRow["EMPLOYEE_ACCOUNT_EFFECT_DATE"].ToString()) ? odRow["EMPLOYEE_ACCOUNT_EFFECT_DATE"].ToString() : string.Empty;
dataIntegration.BasicSalaryEffectDate= !string.IsNullOrEmpty(odRow["COMPENSATION_EFFECT_DATE"].ToString()) ? odRow["COMPENSATION_EFFECT_DATE"].ToString() : string.Empty;
dataIntegration.BasicSalary = !string.IsNullOrEmpty(odRow["BASIC_SALARY"].ToString()) ? odRow["BASIC_SALARY"].ToString() : string.Empty;
dataIntegration.GrossSalary = !string.IsNullOrEmpty(odRow["GROSS_SALARY"].ToString()) ? odRow["GROSS_SALARY"].ToString() : string.Empty;
dataIntegration.SALARY_CHANGE_TYPE = !string.IsNullOrEmpty(odRow["COMPENSATION_EVENT"].ToString()) ? odRow["COMPENSATION_EVENT"].ToString() : string.Empty;
dataIntegration.Company = !string.IsNullOrEmpty(odRow["COMPANY_NAME"].ToString()) ? odRow["COMPANY_NAME"].ToString() : string.Empty;
dataIntegration.CompanyCode = !string.IsNullOrEmpty(odRow["COMPANY_CODE"].ToString()) ? odRow["COMPANY_CODE"].ToString() : string.Empty;
dataIntegration.EmailAddress = !string.IsNullOrEmpty(odRow["EMAIL_ADDRESS"].ToString()) ? odRow["EMAIL_ADDRESS"].ToString() : string.Empty;
dataIntegration.MobileNo = !string.IsNullOrEmpty(odRow["MOBILE_NUMBER"].ToString()) ? odRow["MOBILE_NUMBER"].ToString() : string.Empty;
header.Items.Add(dataIntegration);
}
}
else
{
//throw new Exception($"Failed to retrieve master data. Status code: {response.StatusCode}, Response: {await response.Content.ReadAsStringAsync()}");
throw new Exception($"Failed to retrieve master data");
}
// }
}
return header;
}
catch (Exception ex)
{
throw new Exception("Failed to retrieve master data. Please try again after a few minutes.", ex);
}
}
public async Task<IndividualAllowanceModel> GetConpensationDataAsync(string accessToken)
{
DataIntergrationIndividualAllowanceHeader header = new DataIntergrationIndividualAllowanceHeader();
var data = new IndividualAllowanceModel();
try
{
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Get, compensationDataUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Content = new StringContent("", Encoding.UTF8, "application/x-www-form-urlencoded");
using (var response = await _httpClient.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
var jsonObject = JObject.Parse(responseContent);
JArray items = (JArray)jsonObject["row"];
data.row = new List<EmployeeData>();
foreach (JToken item in items)
{
EmployeeData empData= new EmployeeData();
empData.EMPLOYEE_NO = (string)item["EMPLOYEE_NO"];
empData.COMP_DATA = new List<compdata>();
// Example: Accessing COMP_DATA array if it exists
if (item["COMP_DATA"] != null)
{
JArray compDataArray = (JArray)item["COMP_DATA"];
foreach (JToken compData in compDataArray)
{
var compdata = new compdata();
compdata.ITEM_CODE = (string)compData["ITEM_CODE"];
compdata.ITEM_NAME = (string)compData["ITEM_NAME"];
compdata.AMOUNT = (string)compData["AMOUNT"];
compdata.FROM_DATE = (string)compData["FROM_DATE"];
compdata.TO_DATE = (string)compData["TO_DATE"];
empData.COMP_DATA.Add(compdata);
}
}
data.row.Add(empData);
}
//var obj = root.;
}
else
{
throw new Exception($"Failed to retrieve compensation data. Status code: {response.StatusCode}, Response: {await response.Content.ReadAsStringAsync()}");
}
}
}
return data;
}
catch (Exception ex)
{
throw new Exception("An error occurred while retrieving the master data.", ex);
}
}
}
}