using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Web; using Kreta.BusinessLogic.Classes; using Kreta.BusinessLogic.HelperClasses; using Kreta.BusinessLogic.Helpers.Nyomtatvanyok.Iktatas; using Kreta.BusinessLogic.Utils; using Kreta.Core; using Kreta.Core.ConnectionType; using Kreta.Core.Iktato.Poszeidon.Factory.Interface; using Kreta.DataAccess.Interfaces; using Kreta.DataAccessManual; using Kreta.DataAccessManual.Interfaces; using Kreta.DataAccessManual.Util; using Kreta.Enums; using Kreta.Resources; using Newtonsoft.Json; namespace Kreta.BusinessLogic.Helpers { public class IntezmenyHelper : LogicBase { public IntezmenyHelper(IConnectionType connectionType) : base(connectionType) { } public void ErtesitesekCsokkentettLetrehozasarol(int tanuloId, IntezmenyNevCimCo intezmenyCo) { Dal.CustomConnection.Run(ConnectionType, h => { var tanulo = h.Tanulo().Get(tanuloId); var helper = new UzenetekHelper(new DalHandlerConnectionType(ConnectionType, h)); foreach (var gondviselo in tanulo.Gondviselo.Where(x => !x.Torolt && !x.IsCsokkentettGondviselo)) { var email = gondviselo.Email.SingleOrDefault(x => !x.Torolt && !x.IsHibasanMegadva && x.Alapertelmezett); if (email != null && !string.IsNullOrWhiteSpace(email.EmailCim)) { helper.EmailKuldes(string.Format(EmailResource.GondviseloErtesitesCsokkentettLetrehozasrolEmail, gondviselo.Nev, tanulo.OktatasiAzonosito, tanulo.NyomtatasiNev, intezmenyCo.Nev, intezmenyCo.IntezmenyCim), CommonResource.Ertesites, email.EmailCim, email.Guid); } } var osztalyId = new TanuloHelper(new DalHandlerConnectionType(ConnectionType, h)).GetTanuloAlapkepzesesOsztalyId(tanuloId); if (osztalyId.HasValue) { var osztaly = h.Osztaly().Get(osztalyId.Value); var ofo = osztaly.OsztalyFonok; if (ofo != null) { var email = ofo.Email.SingleOrDefault(x => !x.Torolt && !x.IsPublic && x.Alapertelmezett && !x.IsHibasanMegadva); if (email != null && !string.IsNullOrWhiteSpace(email.EmailCim)) { helper.EmailKuldes(string.Format(EmailResource.CsokkentettGondviseloLetrehozasErtesitesEmail, ofo.NyomtatasiNev, tanulo.OktatasiAzonosito, tanulo.NyomtatasiNev), CommonResource.Ertesites, email.EmailCim, email.Guid); } } } var adminok = new FelhasznaloHelper(new DalHandlerConnectionType(ConnectionType, h)).GetAdminFelhasznalok(); foreach (var felhasznalo in adminok) { var email = felhasznalo.Email.SingleOrDefault(x => !x.Torolt && !x.IsPublic && x.Alapertelmezett && !x.IsHibasanMegadva); if (email != null && !string.IsNullOrWhiteSpace(email.EmailCim)) { helper.EmailKuldes(string.Format(EmailResource.CsokkentettGondviseloLetrehozasErtesitesEmail, felhasznalo.NyomtatasiNev, tanulo.OktatasiAzonosito, tanulo.NyomtatasiNev), CommonResource.Ertesites, email.EmailCim, email.Guid); } } var intezmenyAdminEmail = new AdminHelper(new DalHandlerConnectionType(ConnectionType, h)).GetAdminEmail(); if (intezmenyAdminEmail != null) { helper.EmailKuldes(string.Format(EmailResource.CsokkentettGondviseloLetrehozasErtesitesEmail, CommonResource.Admin, tanulo.OktatasiAzonosito, tanulo.NyomtatasiNev), CommonResource.Ertesites, intezmenyAdminEmail); } }); } public int GetIntezmenyId() { return Dal.CustomConnection.Run(ConnectionType, h => h.IntezmenyDal().GetIntezmenyId(IntezmenyAzonosito)); } public DateTime? GetFrissitesDatum() { return Dal.CustomConnection.Run(ConnectionType, h => h.IntezmenyDal().GetFrissitesDatum()); } public DataSet GetOrganizationNameAndCode() => Dal.CustomConnection.Run(ConnectionType, h => { try { var dal = h.IntezmenyDal(); var systemSettingsJson = dal.GetOrganizationSystemSettingsJSON(); var model = JsonConvert.DeserializeObject(systemSettingsJson); return dal.GetOrganizationIdentyNameWithOmCode(Convert.ToBoolean(model.Value.Value)); } catch { throw; } }); public bool IsSuccessAuthorizedDate() => Dal.CustomConnection.Run(ConnectionType, h => { try { var dal = h.IntezmenyDal(); return dal.IsSuccessAuthorizedDate(); } catch { return false; } }); public IntezmenyCO GetIntezmenyiAdatok() { var ds = Dal.CustomConnection.Run(ConnectionType, h => { return h.IntezmenyDal().GetIntezmenyAdatok(IntezmenyId, TanevId); }); if (ds.Tables[0]?.Rows?.Count > 0) { var row = ds.Tables[0].Rows[0]; return new IntezmenyCO() { IntezmenyId = row.Field("IntezmenyId"), ID = row.Field("IntezmenyAdatokId"), Igazgato = row.Field("IgazgatoNeve"), Iranyitoszam = row.Field("IranyitoSzam"), Nev = row.Field("Nev").Trim(), OMKod = row.Field("OMKod"), Varos = row.Field("Varos"), Telefonszam = row.Field("Telefonszam"), Email = row.Field("EmailCim"), AdminEmail = row.Field("AdminEmailCim"), FenntartoEmail = row.Field("FenntartoEmailCim"), RovidNev = row.Field("RovidNev"), EngedelyezettAllashely = SDAConvert.ToDouble(row["EngedelyezettAllashelyek"]), Orszag = (int)OrszagTipusEnum.Magyarorszag, Emelet = row.Field("Emelet"), Hazszam = row.Field("Hazszam"), KozteruletJellegNev = row.Field("KozteruletJellegeNev"), Kozterulet = row.Field("KozteruletNev"), Ajto = row.Field("Ajto"), IntezmenyAzonosito = row.Field("IntezmenyAzonosito"), FenntartoAzonosito = row.Field("FenntartoAzonosito"), AlternativAzonosito = row.Field("AlternativAzonosito"), IktatoSzervezetAzonosito = row.Field("IktatoSzervezetAzonosito"), PoszeidonBejelentkezesiNev = row.Field("PoszeidonBejelentkezesiNev"), IsCsakLepKezelo = SDAConvert.ToBooleanFromTF(row["IsCsakLepKezelo"]), EnableBAIStatuszVeglegesites = SDAConvert.ToBooleanFromTF(row["IsBaiStatuszAktiv"]), ElfogadottTTF = SDAConvert.ToBooleanFromTF(row["ElfogadottTTF"]), ElfogadottESL = SDAConvert.ToBooleanFromTF(row["ElfogadottESL"]), VeglegesTTF = SDAConvert.ToBooleanFromTF(row["VeglegesTTF"]), VeglegesESL = SDAConvert.ToBooleanFromTF(row["VeglegesESL"]), IsBeiratkozasVeglegesitve = SDAConvert.ToBooleanFromTF(row["VeglegesBeiratkozas"]), VeglegesETTF = SDAConvert.ToBooleanFromTF(row["VeglegesETTF"]), LicenceDatum = row.Field("LicenceDatum")?.ToShortDateString(), IsSzirIntezmeny = SDAConvert.ToBooleanFromTF(row["IsSzirIntezmeny"]), IsArchivIntezmeny = SDAConvert.ToBooleanFromTF(row["IsArchivIntezmeny"]), IsSzakkepzo = SDAConvert.ToBooleanFromTF(row["IsSzakkepzo"]), JuttatasHatarnap = (SDAConvert.ToDateTime(row["JuttatasHatarnap"])?.Day ?? 15) + 1, IsDKTAktiv = SDAConvert.ToBooleanFromTF(row["IsDKTAktiv"]), IsProjektAktiv = SDAConvert.ToBooleanFromTF(row["IsProjektAktiv"]), DKTDiakUrl = row.Field("DKTDiakUrl"), DKTTanarUrl = row.Field("DKTTanarUrl"), IntezmenyGuid = SDAConvert.ToGuid(row["IntezmenyGuid"]), IsSzakkepzoJuttatas = SDAConvert.ToBooleanFromTF(row["IsSzakkepzoJuttatas"]) }; } return null; } public void SaveIntezmenyiAdatok(IntezmenyCO model, bool isNevOMKodModosithato, bool isKretaAdministrator) { ValidateIntezmenyCO(model, isKretaAdministrator); Dal.CustomConnection.Run(ConnectionType, h => { var entity = GetActualIntezmenyAdatok(h); entity.EmailCim = model.Email; if (isKretaAdministrator) { entity.AdminEmailCim = model.AdminEmail; } entity.IgazgatoNeve = model.Igazgato; entity.IranyitoSzam = model.Iranyitoszam; entity.Telefonszam = model.Telefonszam; entity.Varos = model.Varos; entity.EmailCim = model.Email; entity.RovidNev = model.RovidNev; entity.EngedelyezettAllashelyek = model.EngedelyezettAllashely.Value; entity.KozteruletNev = model.Kozterulet; entity.KozteruletJellegeNev = model.KozteruletJellegNev; entity.Hazszam = model.Hazszam; entity.Emelet = model.Emelet; entity.Ajto = model.Ajto; if (isNevOMKodModosithato) { entity.Nev = model.Nev.Trim(); entity.OMKod = model.OMKod; } if (entity.IsSzakkepzo) { entity.JuttatasHatarnap = new DateTime(entity.JuttatasHatarnap?.Year ?? DateTime.Today.Year, entity.JuttatasHatarnap?.Month ?? DateTime.Today.Month, model.JuttatasHatarnap - 1); } IIntezmenyDal dal = h.IntezmenyDal(); dal.Update(entity); }); } public string GetNeptunNaploLink() { return Dal.CustomConnection.Run(ConnectionType, h => { IIntezmenyDal dal = h.IntezmenyDal(); return dal.GetNeptunNaploLink(IntezmenyId); }); } public string GetPoszeidonBejelentkezesiNev() { return Dal.CustomConnection.Run(ConnectionType, h => { IIntezmenyDal dal = h.IntezmenyDal(); return dal.GetPoszeidonBejelentkezesiNev(IntezmenyId); }); } public Image GetIntezmenyFejlecDokumentum() { string imageData = string.Empty; var adatok = Dal.CustomConnection.Run(ConnectionType, h => { return GetActualIntezmenyAdatok(h); }); imageData = adatok.DokumentumFejlec ?? string.Empty; string imageResult = imageData.Replace("data:image/jpeg;base64,", ""); byte[] bytes = Convert.FromBase64String(imageResult); Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); } return new Bitmap(image); } public Image GetIntezmenyLablecDokumentum() { string imageData = string.Empty; var adatok = Dal.CustomConnection.Run(ConnectionType, h => { return GetActualIntezmenyAdatok(h); }); imageData = adatok.DokumentumLablec ?? string.Empty; string imageResult = imageData.Replace("data:image/jpeg;base64,", ""); byte[] bytes = Convert.FromBase64String(imageResult); Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); } return new Bitmap(image); } public IntezmenyFejlecLablecCo GetIntezmenyFejlec() { var adatok = Dal.CustomConnection.Run(ConnectionType, h => { return GetActualIntezmenyAdatok(h); }); var result = new IntezmenyFejlecLablecCo(); result.IsFejlecOrLablecMegjelenit = adatok.IsDokumentumFejlecMegjelenit; result.Base64Img = adatok.DokumentumFejlec; result.HtmlContent = adatok.DokumentumFejlecHtml; return result; } public IntezmenyFejlecLablecCo GetIntezmenyLablec() { var adatok = Dal.CustomConnection.Run(ConnectionType, h => { return GetActualIntezmenyAdatok(h); }); var result = new IntezmenyFejlecLablecCo(); result.IsFejlecOrLablecMegjelenit = adatok.IsDokumentumLablecMegjelenit; result.Base64Img = adatok.DokumentumLablec; result.HtmlContent = adatok.DokumentumLablecHtml; return result; } public void SetIntezmenyFejlecCheckBox(bool isFejlecMegjelenit) { Dal.CustomConnection.Run(ConnectionType, h => { var adatok = GetActualIntezmenyAdatok(h); IIntezmenyDal dal = h.IntezmenyDal(); adatok.IsDokumentumFejlecMegjelenit = isFejlecMegjelenit; dal.Update(adatok); }); } public void SetIntezmenyLablecCheckBox(bool isLablecMegjelenit) { Dal.CustomConnection.Run(ConnectionType, h => { var adatok = GetActualIntezmenyAdatok(h); IIntezmenyDal dal = h.IntezmenyDal(); adatok.IsDokumentumLablecMegjelenit = isLablecMegjelenit; dal.Update(adatok); }); } public void SetIntezmenyFejlec(IntezmenyFejlecLablecCo co) { Dal.CustomConnection.Run(ConnectionType, h => { var adatok = GetActualIntezmenyAdatok(h); IIntezmenyDal dal = h.IntezmenyDal(); adatok.IsDokumentumFejlecMegjelenit = co.IsFejlecOrLablecMegjelenit; adatok.DokumentumFejlec = new NyomtatvanyokHelper(ConnectionType).HtmlConvertToBase64Image(co.HtmlContent); adatok.DokumentumFejlecHtml = co.HtmlContent; dal.Update(adatok); }); } public void DeleteIntezmenyFejlec() { Dal.CustomConnection.Run(ConnectionType, h => { var adatok = GetActualIntezmenyAdatok(h); IIntezmenyDal dal = h.IntezmenyDal(); adatok.IsDokumentumFejlecMegjelenit = false; adatok.DokumentumFejlec = null; adatok.DokumentumFejlecHtml = null; dal.Update(adatok); }); } public void DeleteIntezmenyLablec() { Dal.CustomConnection.Run(ConnectionType, h => { var adatok = GetActualIntezmenyAdatok(h); IIntezmenyDal dal = h.IntezmenyDal(); adatok.IsDokumentumLablecMegjelenit = false; adatok.DokumentumLablec = null; adatok.DokumentumLablecHtml = null; dal.Update(adatok); }); } public void SetIntezmenyLablec(IntezmenyFejlecLablecCo co) { Dal.CustomConnection.Run(ConnectionType, h => { var adatok = GetActualIntezmenyAdatok(h); IIntezmenyDal dal = h.IntezmenyDal(); adatok.IsDokumentumLablecMegjelenit = co.IsFejlecOrLablecMegjelenit; adatok.DokumentumLablec = new NyomtatvanyokHelper(ConnectionType).HtmlConvertToBase64Image(co.HtmlContent); adatok.DokumentumLablecHtml = co.HtmlContent; dal.Update(adatok); }); } public IntezmenyNevCimCo GetAktivIntezmenyNevCim() { var intezmenyCo = new IntezmenyNevCimCo(); var ds = Dal.CustomConnection.Run(ConnectionType, h => { return h.IntezmenyDal().GetAktivIntezmenyNevCim(IntezmenyAzonosito); }); if (ds?.Tables[0]?.Rows?.Count > 0) { var row = ds.Tables[0].Rows[0]; intezmenyCo.IntezmenyAzonosito = IntezmenyAzonosito; intezmenyCo.Nev = row.Field("C_NEV"); intezmenyCo.Varos = row.Field("C_VAROS"); intezmenyCo.Iranyitoszam = row.Field("C_IRANYITOSZAM"); intezmenyCo.Ajto = row.Field("C_AJTO"); intezmenyCo.Emelet = row.Field("C_EMELET"); intezmenyCo.Hazszam = row.Field("C_HAZSZAM"); intezmenyCo.KozteruletJellegNev = row.Field("C_KOZTERULETJELLEGENEV"); intezmenyCo.Kozterulet = row.Field("C_KOZTERULETNEV"); intezmenyCo.OMKod = row.Field("C_OMKOD"); intezmenyCo.Email = row.Field("C_EMAILCIM"); intezmenyCo.Telefonszam = row.Field("C_TELEFONSZAM"); intezmenyCo.IntezmenyId = row.Field("C_INTEZMENYID"); } return intezmenyCo; } private void ValidateIntezmenyCO(IntezmenyCO model, bool isKretaAdministrator) { //OM-2362 //if (string.IsNullOrWhiteSpace(model.Nev)) // throw new ArgumentNullException("Nev"); //if (string.IsNullOrWhiteSpace(model.OMKod)) // throw new ArgumentNullException("OMKod"); //if (model.OMKod.Length != 6) // throw new FormatException("OMKod"); if (string.IsNullOrWhiteSpace(model.Iranyitoszam)) { throw new ArgumentNullException("Iranyitoszam"); } if (string.IsNullOrWhiteSpace(model.Varos)) { throw new ArgumentNullException("Varos"); } if (!model.Telefonszam.IsValidPhone()) { throw new FormatException("Telefonszam"); } if (isKretaAdministrator) { if (string.IsNullOrWhiteSpace(model.AdminEmail)) { throw new ArgumentNullException("AdminEmail"); } if (!model.AdminEmail.IsValidEmail()) { throw new FormatException("AdminEmail"); } } } private IIntezmenyAdatok GetActualIntezmenyAdatok(IDalHandler h) { IIntezmenyDal dal = h.IntezmenyDal(); var intezmeny = dal.Get(IntezmenyId); return intezmeny.IntezmenyAdatok.Where(x => x.TanevId == TanevId && x.Torolt == false) .FirstOrDefault(); } public string GetNexiusId() { return Dal.CustomConnection.Run(ConnectionType, h => { IIntezmenyDal dal = h.IntezmenyDal(); return dal.GetNexiusId(IntezmenyId); }); } public int InsertDokumentum(IIktatoRepositoryFactory iktatoRepositoryFactory, IktatoServiceConfiguration iktatoServiceConfiguration, IEnumerable files, DokumentumTipusEnum dokumentumTipus) { var helper = new PoszeidonHelper(ConnectionType, iktatoRepositoryFactory, iktatoServiceConfiguration); foreach (var item in files) { var ms = new MemoryStream(item.ContentLength); item.InputStream.CopyTo(ms); var bytes = ms.ToArray(); ms.Dispose(); FoszamDefinicioValueModel fdm = new FoszamDefinicioValueModel(); fdm.Szint = DokumentumElemiSzintEnum.Intezmeny; fdm.IntezmenyId = IntezmenyId; if (dokumentumTipus == DokumentumTipusEnum.Munkaterv) { fdm.TanevId = TanevId; } var dokuId = helper.Iktatas(new MemoryStream(bytes), Path.GetFileNameWithoutExtension(item.FileName), FelhasznaloId, DokumentumKategoriaEnum.Alapdokumentumok, dokumentumTipus, null, null, fdm, item.ContentType); return dokuId; } return 0; } public IDictionary GetTelephelyAnyaintezmenyList() { var ds = Dal.CustomConnection.Run(ConnectionType, h => { return h.IntezmenyDal().GetTelephelyAnyaintezmenyList(TanevId); }); return LogicUtil.CreateDropdownContent(ds, "ID", "Nev"); } public IEnumerable GetIntezmenyVarosai() { return Dal.CustomConnection.Run(ConnectionType, h => { return h.IntezmenyDal().GetIntezmenyVarosai(IntezmenyId, TanevId); }); } public bool IsNszfhIntezmeny() { return GetEnvironmentName() == IntezmenyResource.EnvironmentNszfh; } public string GetIntezmenyiDictionaryEnvironmentName() { return GetEnvironmentName(IntezmenyResource.Dictionary); } private string GetEnvironmentName(string key = null) { return Dal.CustomConnection.Run(ConnectionType, h => { IIntezmenyDal dal = h.IntezmenyDal(); return dal.GetEnvironmentName(IntezmenyAzonosito, key); }); } } }