using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using Hangfire; using Kreta.Client.Tavollet; using Kreta.Core.Configuratiaton; using Kreta.Core.SAP.CommunicationModels; using Kreta.DataAccessManual; using Kreta.DataAccessManual.ParameterClasses; using Kreta.Enums; using Kreta.Enums.ManualEnums; using Kreta.Job.Tasks.Core; using Kreta.Job.Tasks.Core.Models; using Kreta.Resources; namespace Kreta.Job.Tasks { public class TavolletJob : ITavolletJob { private readonly string KozpontiKretaConfig = "KozpontiKretaConfig"; public void SyncTavolletIktatott() { var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection(KozpontiKretaConfig); var tavolletClient = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); var exceptions = new List(); foreach (var azonosito in KretaServer.KretaServer.Instance.GetOsszesIntezmeny()) { var connectionString = KretaServer.KretaServer.Instance.GetIntezmenyConnectionString(azonosito); Dal.ServiceSystemConnection.Run(connectionString, h => { try { var icDal = h.IntezmenyConfigDAL(); var dal = h.IntezmenyDal(); var intezmenyId = dal.GetIntezmenyIdByAzonosito(azonosito); if (!bool.Parse(icDal.GetIntezmenyConfig(intezmenyId.Value, IntezmenyConfigModulEnum.HRModul.ToString(), IntezmenyConfigTipusEnum.IsEnabled.ToString()))) return; var ds = dal.GetIntezmenyIdAktivTanevIdByAzonosito(azonosito); if (ds.Tables[0].Rows.Count == 0) { return; } var pco = new TavolletIktatottPCO() { TanevId = ds.Tables[0].Rows[0].Field("AktivTanevId"), IntezmenyId = ds.Tables[0].Rows[0].Field("IntezmenyId") }; var iDal = h.HRModulDAL(); var queryResult = iDal.GetTavolletSzinkronizalatlanDok(pco); if (queryResult.Tables[0].Rows.Count == 0) { return; } TavolletDokumentumSyncRequestModel request = MapDatasetToRequestModel(queryResult); if (tavolletClient.SyncTavolletIktatott(request).IsSuccess) { iDal.UpdateSzinkronizalatlan(pco.IntezmenyId, pco.TanevId, request.DokumentumokList.Select(x => x.TavolletJelentoId).Distinct().ToList()); } } catch (Exception ex) { exceptions.Add(ex); } if (exceptions.Any()) { var aggregateException = new AggregateException(exceptions); throw aggregateException.Flatten(); } }); } } public void SendOutTavolletReminderEmails() { var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection(KozpontiKretaConfig); var tavolletClient = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); var exceptions = new List(); DateTime dateTime = DateTime.Now; foreach (var azonosito in KretaServer.KretaServer.Instance.GetOsszesIntezmeny()) { var connectionString = KretaServer.KretaServer.Instance.GetIntezmenyConnectionString(azonosito); TavolletjelentoListaResponseModel fuggoTavolletek = tavolletClient.GetTavolletek( new TavolletjelentoListaRequestModel { IdoszakKezdet = new DateTime(dateTime.Year, dateTime.Month, 1), TavolletStatuszId = (int)Kreta.Core.SAP.Enums.TavolletStatuszok.Fuggo, KretaIntezmenyAzonosito = azonosito, NaptariEv = new int[] { DateTime.Now.Year } }); if (fuggoTavolletek?.TavolletjelentoResultList != null && fuggoTavolletek.TavolletjelentoResultList.Any()) { Dal.ServiceSystemConnection.Run(connectionString, h => { try { var icDal = h.IntezmenyConfigDAL(); var dal = h.IntezmenyDal(); var intezmenyId = dal.GetIntezmenyIdByAzonosito(azonosito); if (!bool.Parse(icDal.GetIntezmenyConfig(intezmenyId.Value, IntezmenyConfigModulEnum.HRModul.ToString(), IntezmenyConfigTipusEnum.IsEnabled.ToString()))) return; var ds = dal.GetIntezmenyIdAktivTanevIdByAzonosito(azonosito); if (ds.Tables[0].Rows.Count == 0) { return; } var tanevId = ds.Tables[0].Rows[0].Field("AktivTanevId"); var alkalmazottDal = h.Alkalmazott(); DataSet tavolletJovahagyok = alkalmazottDal.GetAlkalmazottakNeveEsEmailCimeVzetoOraszamOkAlapjan(tanevId, (int)EmailTipusEnum.Hivatalos, intezmenyId.Value, new List { (int) VezetoiOraszamokTipusEnum.Intezmenyvezeto, (int) VezetoiOraszamokTipusEnum.TagintezmenyVezeto, (int) VezetoiOraszamokTipusEnum.IntezmenyegysegVezeto }); if (tavolletJovahagyok.Tables[0]?.Rows.Count > 0) { var emailsToBeSend = new List(); // Iterate through users foreach (DataRow dataRow in tavolletJovahagyok.Tables[0].Rows) { var emailCim = dataRow.Field("EmailCim"); var emailModel = new EmailModel(); emailModel.TargetEmail = emailCim; emailModel.Subject = EmailResource.TavolletJovahagyasEmlekeztetoTargy; emailModel.Message = EmailResource.TavolletJovahagyasEmlekezteto; emailsToBeSend.Add(emailModel); } if (emailsToBeSend.Any()) { BackgroundJob.Enqueue(email => email.SendMailMessages(emailsToBeSend)); } } } catch (Exception ex) { exceptions.Add(ex); } if (exceptions.Any()) { var aggregateException = new AggregateException(exceptions); throw aggregateException.Flatten(); } }); } } } private TavolletDokumentumSyncRequestModel MapDatasetToRequestModel(DataSet result) { var tavolletJelento = new List(); foreach (DataRow row in result.Tables[0].Rows) { var temp = new TavolletDokumentumData { TavolletJelentoId = row.Field("C_KOZPONTITAVOLLETID"), ElektronikusPeldanyId = row.Field("C_ELEKTRONIKUSPELDANYID"), FajlNev = row.Field("C_FAJLNEV"), RogzitoNev = row.Field("C_NYOMTATASINEV"), RogzitoGuid = Guid.Parse(row.Field("C_EGYEDIAZONOSITO")), RogzitesIdopontja = row.Field("C_IKTATASDATUMA"), }; tavolletJelento.Add(temp); } return new TavolletDokumentumSyncRequestModel { DokumentumokList = tavolletJelento }; } } }