using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.ModelBinding; using Kendo.Mvc.Extensions; using Kendo.Mvc.UI; using Kreta.BusinessLogic.Classes; using Kreta.BusinessLogic.Helpers; using Kreta.BusinessLogic.Interfaces; using Kreta.BusinessLogic.Security; using Kreta.Client.Tavollet; using Kreta.Core; using Kreta.Core.Configuratiaton; using Kreta.Core.Iktato.Poszeidon.Factory.Interface; using Kreta.Core.SAP.CommunicationModels; using Kreta.Core.SAP.CommunicationModels.TavolletIgenyekKezelese; using Kreta.Core.SAP.Enums; using Kreta.Enums; using Kreta.Enums.ManualEnums; using Kreta.Resources; using Kreta.Web.Areas.HRModul.Logic; using Kreta.Web.Areas.HRModul.Models; using Kreta.Web.Helpers; using Kreta.Web.Helpers.Error; using Kreta.Web.Helpers.Grid; using Kreta.Web.Security; using Newtonsoft.Json; namespace Kreta.Web.Areas.HRModul.ApiControllers { [ApiRoleClaimsAuthorize(true)] [ApiRolePackageAuthorize(KretaClaimPackages.TavolletIgenylo.ClaimValue)] //[ApiFeatureAuthorize(Core.Constants.FeatureName.HRModul)] public class TavolletjelentoApiController : ApiController { private IIktatoRepositoryFactory IktatoRepositoryFactory { get; } private IktatoServiceConfiguration IktatoServiceConfiguration { get; } private readonly IFileServiceHelper fileServiceHelper; public TavolletjelentoApiController(IIktatoRepositoryFactory iktatoRepositoryFactory, IktatoServiceConfiguration iktatoServiceConfiguration, IFileServiceHelper fileServiceHelper) { IktatoRepositoryFactory = iktatoRepositoryFactory ?? throw new ArgumentNullException(nameof(iktatoRepositoryFactory)); IktatoServiceConfiguration = iktatoServiceConfiguration ?? throw new ArgumentNullException(nameof(iktatoServiceConfiguration)); this.fileServiceHelper = fileServiceHelper; } public DataSourceResult GetTavolletjelentoGrid(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var helper = new HRModulHelper(ConnectionTypeExtensions.GetSessionConnectionType()); List LResult = new List(); var model = JsonConvert.DeserializeObject(data); var kozpontiResult = new TavolletjelentoLogic().GetTavolletjelentoLista(model, helper); if (!kozpontiResult?.IsSuccess ?? true) { return new DataSourceResult { Errors = "error" }; } if (kozpontiResult != null && kozpontiResult.IsSuccess && kozpontiResult.TavolletjelentoResultList != null) { bool isIntezmenyvezeto = helper.IsIntezmenyvezeto(ClaimData.FelhasznaloId); foreach (var item in kozpontiResult.TavolletjelentoResultList) { LResult.Add(new TavolletjelentoGridModel() { ID = item.TavolletJelentoId.ToString(), TipusId = item.TavolletTipusId, StatuszId = item.TavolletStatuszId.ToString(), Torolt_BOOL = item.IsTorolt, IsTorolheto = helper.IsTorolheto(item, isIntezmenyvezeto), AlkalmazottNev = item.AlkalmazottNev, SzTSzAzonosito = item.AlkalmazottSzTSz, AlkalmazottGuid = item.AlkalmazottGuid.ToString(), RogzitoGuid = item.RogzitoGuid.ToString(), IdoszakKezdet = item.TavolletKezdete, IdoszakVeg = item.TavolletVege, HasMelleklet_BOOL = item.IsMellekletTartozikHozza, HasMelleklet_BNAME = item.IsMellekletTartozikHozza ? CommonResource.Igen : CommonResource.Nem, RogzitesDatuma = item.RogzitesIdopontja, MunkanapSzam = item.TavolletIdotartamaNap, StatuszId_DNAME = item.TavolletStatuszId.GetDisplayName(ClaimData.SelectedTanevID.Value), TipusId_DNAME = item.TavolletTipusId.GetDisplayName(ClaimData.SelectedTanevID.Value), HatralevoNapokSzama = item.HatralevoNapokSzama ?? default }); } if (LResult.Count > 0) { var result = new DataSourceResult { Data = LResult.ToDataSourceResult(request).Data, Total = LResult.Count }; return result; } } return new DataSourceResult(); } public DataSourceResult GetTavolletjelentoDokGrid(int tavolletId) { var helper = new HRModulHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var result = helper.TavolletjelentoDokList(tavolletId); return result.ToDataSourceResult(); } public DataSourceResult GetAlkalmazottakAdatai(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var model = JsonConvert.DeserializeObject(data); List LResult = new List(); var tanev = new TanevHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var tanevCo = tanev.GetTanevInfo(); var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection("KozpontiKretaConfig"); var client = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); var kozpontiResult = client.GetTavolletAlkalmazottakAdatai(model.ConvertToTavolletAdatokRequestModel(tanevCo.Sorszam)); if (!kozpontiResult?.IsSuccess ?? true) { return new DataSourceResult { Errors = "error" }; } if (kozpontiResult.TavolletAdatokResultList != null) { foreach (var item in kozpontiResult.TavolletAdatokResultList) { LResult.Add(new TavolletAlkalmazottGridModel() { ID = item.AlkalmazottId.ToString(), AlkalmazottNev = item.AlkalmazottNev, SzTSzAzonosito = item.SzTSzAzonosito, IsFointezmenyeE_BNAME = item.IsTavolletFoIntezmenye ? CommonResource.Igen : CommonResource.Nem, SzabadsagKeret = item.EvesKeret, IgenybeVehetoTavolletek = item.FelhasznalhatoKeret, IgenybeVettTavolletek = item.FelhasznaltKeret, FuggobenLevoTavolletek = item.FuggoTavolletIgenyek, }); } if (LResult.Count > 0) { var result = new DataSourceResult { Data = LResult.ToDataSourceResult(request).Data, Total = LResult.Count }; return result; } } return new DataSourceResult(); } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public int GetTavolletIdotartam(TavolletjelentoDateModel model) { if (model.TavolletKezdet.HasValue && model.TavolletVeg.HasValue) { var helper = new HRModulHelper(ConnectionTypeExtensions.GetSessionConnectionType()); return helper.GetTavolletIdotartam(model.TavolletKezdet.Value.ToLocalTime(), model.TavolletVeg.Value.ToLocalTime()); } return 0; } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage SaveTavollet(TavolletjelentoModel model) { TavolletjelentoLogic tavolletJelentoLogic = new TavolletjelentoLogic(); tavolletJelentoLogic.CustomValidation(ModelState, model); if (ModelState.IsValid) { var helper = new HRModulHelper(ConnectionTypeExtensions.GetSessionConnectionType()); model = tavolletJelentoLogic.UpdateModelAuthorizationData(model); var co = tavolletJelentoLogic.ConvertModelToCo(model); var saveModel = tavolletJelentoLogic.SaveOrUpdateTavollet(model, co); if (model.Muvelet == WorkflowMuveletEnum.UjRogzites && saveModel.AlkalmazottList != null && saveModel.AlkalmazottList.Any(i => string.IsNullOrWhiteSpace(i.RogzitoFunkcioTerulet))) { ModelState.AddModelError("NincsFTError", string.Format(HRModulResource.FunkcioTeruletHianyzik, string.Join(", ", saveModel.AlkalmazottList.Where(i => string.IsNullOrWhiteSpace(i.RogzitoFunkcioTerulet)).Select(d => d.AlkalmazottNev)))); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection("KozpontiKretaConfig"); var client = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); var kozpontiResult = client.SaveOrUpdateTavollet(saveModel); if (kozpontiResult.IsSuccess) { if (model.FilesArray != null && model.FilesArray.Count > 0) { var entitys = new List(); foreach (var item in kozpontiResult.AlkalmazottResultList) { entitys.Add(item.TavolletJelentoId); } helper.InsertDokumentum(IktatoRepositoryFactory, IktatoServiceConfiguration, fileServiceHelper, co, entitys); } tavolletJelentoLogic.SendEmail(co, model.EredetiRogzitoGuid); return new HttpResponseMessage(HttpStatusCode.OK); } else { ModelState.AddModelStateErrorsFromString(kozpontiResult.ErrorMessage); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage SaveTobbesTavollet(TavolletjelentoTobbesModel tobbesModel) { TavolletjelentoLogic tavolletJelentoLogic = new TavolletjelentoLogic(); tavolletJelentoLogic.CustomValidation(ModelState, tobbesModel); if (ModelState.IsValid) { var tavolletjelentoModel = new TavolletjelentoModel() { Muvelet = WorkflowMuveletEnum.CsakModositas, StatuszId = (int)TavolletStatuszEnum.Fuggo, MegjegyzesMezo = tobbesModel.MegjegyzesMezo, AlkalmazottList = tobbesModel.TavolletAlkalmazottIdArray, TavolletIdotartamNap = tobbesModel.TavolletIdotartamNap, TavolletOka = tobbesModel.TavolletOka, TavolletKezdet = tobbesModel.TavolletKezdet, TavolletVeg = tobbesModel.TavolletVeg }; var co = tavolletJelentoLogic.ConvertModelToCo(tavolletjelentoModel); var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection("KozpontiKretaConfig"); var client = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); var felhasznaloHelper = new FelhasznaloHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var saveModel = tavolletJelentoLogic.SaveOrUpdateTavollet(tavolletjelentoModel, co); //var saveModel = new TavolletjelentoCRUDRequestModel(); //saveModel.Action = WorkflowMuveletek.Modosit; //saveModel.TavolletTipusId = tobbesModel.TavolletOka.Value; //saveModel.TavolletStatuszId = (int)TavolletStatuszEnum.Fuggo; //saveModel.TavolletKezdete = tobbesModel.TavolletKezdet.Value.ToLocalTime(); //saveModel.TavolletVege = tobbesModel.TavolletVeg.Value.ToLocalTime(); //saveModel.TavolletIdotartamaNap = tobbesModel.TavolletIdotartamNap ?? default; //saveModel.Megjegyzes = tobbesModel.Megjegyzes; //saveModel.RogzitoNev = ClaimData.FelhasznaloNev; //saveModel.RogzitoGuid = Guid.Parse(felhasznaloHelper.GetFelhasznaloEgyediAzonosito(ClaimData.FelhasznaloId)); //saveModel.RogzitoIntezmenyAzonosito = ClaimData.IntezmenyAzonosito; //saveModel.TanevId foreach (var tavollet in tobbesModel.TavolletekDictionary) { saveModel.AlkalmazottList.Add(new TavolletAlkalmazottData { TavolletJelentoId = tavollet.Key, AlkalmazottSzTSz = tavollet.Value.AlkalmazottSzTSz, RogzitoFunkcioTerulet = tavollet.Value.RogzitoFunkcioTerulet, }); } var kozpontiResult = client.SaveOrUpdateTavollet(saveModel); if (kozpontiResult.IsSuccess) { co.StatuszId = (int)TavolletStatuszEnum.Fuggo; tavolletJelentoLogic.SendEmail(co); return new HttpResponseMessage(HttpStatusCode.OK); } else { ModelState.AddModelStateErrorsFromString(kozpontiResult.ErrorMessage); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public void SaveTavolletElfogadas([FromBody] List ids) { var felhasznaloHelper = new FelhasznaloHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection("KozpontiKretaConfig"); var client = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); var saveModels = new List(); var rogzitoGuid = Guid.Parse(felhasznaloHelper.GetFelhasznaloEgyediAzonosito(ClaimData.FelhasznaloId)); foreach (var item in ids) { var saveModel = new IntezmenyiTavolletIgenyElfogadasRequestModel { Action = WorkflowMuveletek.Jovahagy, TavolletTipusId = (int)item.TipusId, TavolletStatuszId = (int)HRModulHelper.GetElfogadasStatuszByTipus(item.TipusId), RogzitoNev = ClaimData.FelhasznaloNev, RogzitoGuid = rogzitoGuid, TavolletJelentoId = item.ID }; saveModels.Add(saveModel); } var kozpontiResult = client.JovahagyTavolletek(saveModels); if (!kozpontiResult.IsSuccess) { throw new StatusError(HttpStatusCode.BadRequest, kozpontiResult.ErrorMessage); } foreach (var item in kozpontiResult.Data) { new TavolletjelentoLogic().SendEmail(item, (int)HRModulHelper.GetElfogadasStatuszByTipus(ids.Where(x => x.ID == item.TavolletJelentoId).First().TipusId) ); } } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public void SaveTavolletElutasitas([FromBody] List ids) { var felhasznaloHelper = new FelhasznaloHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var saveModels = new TavolletjelentoCRUDRequestModel { Action = WorkflowMuveletek.Elutasit, TavolletStatuszId = (int)TavolletStatuszEnum.Elutasitva, RogzitoNev = ClaimData.FelhasznaloNev, RogzitoGuid = Guid.Parse(felhasznaloHelper.GetFelhasznaloEgyediAzonosito(ClaimData.FelhasznaloId)), }; foreach (var item in ids) { saveModels.AlkalmazottList.Add(new TavolletAlkalmazottData { TavolletJelentoId = item.ID }); } var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection("KozpontiKretaConfig"); var client = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); var kozpontiResult = client.SaveOrUpdateTavollet(saveModels); if (!kozpontiResult.IsSuccess) { throw new StatusError(HttpStatusCode.BadRequest, kozpontiResult.ErrorMessage); } foreach (var alkalmazott in kozpontiResult.AlkalmazottResultList) { new TavolletjelentoLogic().SendEmail(alkalmazott, (int)TavolletStatuszEnum.Elutasitva, ids.Single(x => x.ID == alkalmazott.TavolletJelentoId).RogzitoGuid); } } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public void DeleteTavollet(TavolletjelentoDeleteModel model) { var tavolletDeleteModel = new TavolletjelentoLogic().DeleteTavollet(model); var kozpontiKreta = (KozpontiKretaConfiguration)ConfigurationManager.GetSection("KozpontiKretaConfig"); var client = new TavolletClient(kozpontiKreta.KgrUrl, kozpontiKreta.ApiKey); client.DeleteTavollet(tavolletDeleteModel); } } }